File size: 3,385 Bytes
c019e16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import torch
input_dir_path = "/scratch/project_462000086/norwegian_gpt/Megatron-DeepSpeed-fixed/checkpoints/global_step120000"
output_dir_path = "/scratch/project_462000086/norwegian_gpt/Megatron-DeepSpeed-fixed/hf_pilot_checkpoint_120k"
n_hidden = 4096
n_heads = 32
n_layers = 32
n_tp = 4
weights = {}
# embedding
embedding_weights = []
for i in range(n_tp):
path = f"{input_dir_path}/layer_01-model_0{i}-model_states.pt"
checkpoint = torch.load(path)
embedding_weights.append(checkpoint["word_embeddings.weight"].bfloat16())
weights[f"transformer.word_embeddings_layernorm.weight"] = checkpoint["word_embeddings.norm.weight"].bfloat16()
weights[f"transformer.word_embeddings_layernorm.bias"] = checkpoint["word_embeddings.norm.bias"].bfloat16()
weights[f"transformer.word_embeddings.weight"] = torch.cat(embedding_weights, dim=0)
weights[f"lm_head.weight"] = torch.cat(embedding_weights, dim=0)
del embedding_weights
# transformer layers
for layer in range(n_layers):
qkv_weights = []
qkv_biases = []
o_weights = []
up_weights = []
up_biases = []
down_weights = []
for i in range(n_tp):
path = f"{input_dir_path}/layer_{layer+3:02d}-model_0{i}-model_states.pt"
checkpoint = torch.load(path)
weights[f"transformer.h.{layer}.input_layernorm.weight"] = checkpoint["input_layernorm.weight"].bfloat16()
weights[f"transformer.h.{layer}.input_layernorm.bias"] = checkpoint["input_layernorm.bias"].bfloat16()
weights[f"transformer.h.{layer}.self_attention.dense.bias"] = checkpoint["self_attention.dense.bias"].bfloat16()
weights[f"transformer.h.{layer}.post_attention_layernorm.weight"] = checkpoint["post_attention_layernorm.weight"].bfloat16()
weights[f"transformer.h.{layer}.post_attention_layernorm.bias"] = checkpoint["post_attention_layernorm.bias"].bfloat16()
weights[f"transformer.h.{layer}.mlp.dense_4h_to_h.bias"] = checkpoint["mlp.dense_4h_to_h.bias"].bfloat16()
qkv_weights.append(checkpoint["self_attention.query_key_value.weight"].bfloat16())
qkv_biases.append(checkpoint["self_attention.query_key_value.bias"].bfloat16())
o_weights.append(checkpoint["self_attention.dense.weight"].bfloat16())
up_weights.append(checkpoint["mlp.dense_h_to_4h.weight"].bfloat16())
up_biases.append(checkpoint["mlp.dense_h_to_4h.bias"].bfloat16())
down_weights.append(checkpoint["mlp.dense_4h_to_h.weight"].bfloat16())
weights[f"transformer.h.{layer}.self_attention.query_key_value.weight"] = torch.cat(qkv_weights, dim=0)
weights[f"transformer.h.{layer}.self_attention.query_key_value.bias"] = torch.cat(qkv_biases, dim=0)
weights[f"transformer.h.{layer}.self_attention.dense.weight"] = torch.cat(o_weights, dim=1)
weights[f"transformer.h.{layer}.mlp.dense_h_to_4h.weight"] = torch.cat(up_weights, dim=0)
weights[f"transformer.h.{layer}.mlp.dense_h_to_4h.bias"] = torch.cat(up_biases, dim=0)
weights[f"transformer.h.{layer}.mlp.dense_4h_to_h.weight"] = torch.cat(down_weights, dim=1)
# output layer norm
path = f"{input_dir_path}/layer_36-model_00-model_states.pt"
checkpoint = torch.load(path)
weights[f"transformer.ln_f.bias"] = checkpoint["bias"].bfloat16()
weights[f"transformer.ln_f.weight"] = checkpoint["weight"].bfloat16()
torch.save(weights, f"{output_dir_path}/pytorch_model.bin")
|