davda54 commited on
Commit
a6b0544
·
verified ·
1 Parent(s): beb8435

Delete convert_weights.py

Browse files
Files changed (1) hide show
  1. convert_weights.py +0 -84
convert_weights.py DELETED
@@ -1,84 +0,0 @@
1
- import torch
2
- from safetensors.torch import save_file
3
- from tqdm import tqdm
4
-
5
-
6
- input_dir_path = "/scratch/project_465000144/dasamuel/normistral/normistral-11b-masked-post/global_step10000"
7
- output_dir_path = "/scratch/project_465000144/dasamuel/normistral/normistral-11b-masked-post-hf-60000"
8
-
9
- n_hidden = 5120
10
- n_ffn_hidden = 14336
11
- n_heads = 32
12
- n_kv_heads = 8
13
- n_layers = 40
14
- head_dim = 128
15
- n_tp = 2
16
- n_pp = 2
17
-
18
- weights = {}
19
-
20
- # embedding
21
- embedding_weights = []
22
- for i in range(n_tp):
23
- path = f"{input_dir_path}/layer_01-model_0{i}-model_states.pt"
24
- checkpoint = torch.load(path)
25
-
26
- embedding_weights.append(checkpoint["word_embeddings.weight"].bfloat16())
27
-
28
- weights[f"model.embed_tokens.weight"] = torch.cat(embedding_weights, dim=0)
29
- del embedding_weights
30
-
31
- lm_head_weights = []
32
- for i in range(n_tp):
33
- path = f"{input_dir_path}/layer_{n_layers + 5}-model_0{i}-model_states.pt"
34
- checkpoint = torch.load(path)
35
-
36
- lm_head_weights.append(checkpoint["lm_head.weight"].bfloat16())
37
-
38
- weights[f"lm_head.weight"] = torch.cat(lm_head_weights, dim=0)
39
- del lm_head_weights
40
-
41
-
42
- # transformer layers
43
- for layer in tqdm(range(n_layers)):
44
- q_weights, k_weights, v_weights, o_weights = [], [], [], []
45
- up_weights, gate_weights, down_weights = [], [], []
46
-
47
- for i in range(n_tp):
48
- path = f"{input_dir_path}/layer_{layer+3:02d}-model_0{i}-model_states.pt"
49
- checkpoint = torch.load(path)
50
-
51
- weights[f"model.layers.{layer}.input_layernorm.weight"] = checkpoint["input_layernorm.weight"].bfloat16()
52
- weights[f"model.layers.{layer}.post_attention_layernorm.weight"] = checkpoint["post_attention_layernorm.weight"].bfloat16()
53
-
54
- kv_weight = checkpoint["self_attention.key_value.weight"].bfloat16()
55
- k_weight, v_weight = torch.chunk(kv_weight, 2, dim=0)
56
- k_weights.append(k_weight)
57
- v_weights.append(v_weight)
58
-
59
- q_weights.append(checkpoint["self_attention.query.weight"].bfloat16())
60
- o_weights.append(checkpoint["self_attention.dense.weight"].bfloat16())
61
- down_weights.append(checkpoint["mlp.dense_4h_to_h.weight"].bfloat16())
62
-
63
- up_gate_weight = checkpoint["mlp.dense_h_to_4h.weight"].bfloat16()
64
- up_weight, gate_weight = torch.chunk(up_gate_weight, 2, dim=0)
65
- up_weights.append(up_weight)
66
- gate_weights.append(gate_weight)
67
-
68
- weights[f"model.layers.{layer}.self_attn.q_proj.weight"] = torch.cat(q_weights, dim=0)
69
- weights[f"model.layers.{layer}.self_attn.k_proj.weight"] = torch.cat(k_weights, dim=0)
70
- weights[f"model.layers.{layer}.self_attn.v_proj.weight"] = torch.cat(v_weights, dim=0)
71
- weights[f"model.layers.{layer}.self_attn.o_proj.weight"] = torch.cat(o_weights, dim=1)
72
- weights[f"model.layers.{layer}.mlp.up_proj.weight"] = torch.cat(up_weights, dim=0)
73
- weights[f"model.layers.{layer}.mlp.gate_proj.weight"] = torch.cat(gate_weights, dim=0)
74
- weights[f"model.layers.{layer}.mlp.down_proj.weight"] = torch.cat(down_weights, dim=1)
75
-
76
-
77
- # output layer norm
78
- path = f"{input_dir_path}/layer_{n_layers + 4}-model_00-model_states.pt"
79
- checkpoint = torch.load(path)
80
-
81
- weights[f"model.norm.weight"] = checkpoint["weight"].bfloat16()
82
-
83
- torch.save(weights, f"{output_dir_path}/pytorch_model.bin")
84
- save_file(weights, f"{output_dir_path}/model.safetensors")