Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes Llama checkpoint convertion compatible with fused up/gate projection #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def convert_checkpoint(p):
"self_attention.dense.weight": (1, "self_attn.o_proj.weight", 1, 0),
"post_attention_layernorm.weight": (0, "post_attention_layernorm.weight", None, 0),
"self_attention.core_attention.rotary_emb.inv_freq": (0, "self_attn.rotary_emb.inv_freq", None, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj.weight", 0, 0),
"mlp.dense_h_to_4h_2.weight": (1, "mlp.up_proj.weight", 0, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj_up_proj.weight", 0, 0),
"mlp.dense_4h_to_h.weight": (1, "mlp.down_proj.weight", 1, 0),
"model.language_model.encoder.final_layernorm.weight": (0, "model.norm.weight", None, 0),
"model.language_model.output_layer.weight": (1, "lm_head.weight", 0, 0),
Expand Down Expand Up @@ -78,9 +77,15 @@ def convert_checkpoint(p):
v = model_llama[f'model.layers.{i}.self_attn.v_proj.weight']
model_llama[f'model.layers.{i}.self_attn.query_key_value.weight'] = torch.cat([q, k, v], dim=0)

gate_proj = model_llama[f'model.layers.{i}.mlp.gate_proj.weight']
up_proj = model_llama[f'model.layers.{i}.mlp.up_proj.weight']
model_llama[f'model.layers.{i}.mlp.gate_proj_up_proj.weight'] = torch.cat([gate_proj, up_proj], dim=0)

model_llama.pop(f'model.layers.{i}.self_attn.q_proj.weight')
model_llama.pop(f'model.layers.{i}.self_attn.k_proj.weight')
model_llama.pop(f'model.layers.{i}.self_attn.v_proj.weight')
model_llama.pop(f'model.layers.{i}.mlp.gate_proj.weight')
model_llama.pop(f'model.layers.{i}.mlp.up_proj.weight')

for p in range(PP):
for i in range(TP):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def convert_checkpoint(p, args, config):
"self_attention.dense.weight": (1, "self_attn.o_proj.weight", 1, 0),
"post_attention_layernorm.weight": (0, "post_attention_layernorm.weight", None, 0),
"self_attention.core_attention.rotary_emb.inv_freq": (0, "self_attn.rotary_emb.inv_freq", None, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj.weight", 0, 0),
"mlp.dense_h_to_4h_2.weight": (1, "mlp.up_proj.weight", 0, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj_up_proj.weight", 0, 0),
"mlp.dense_4h_to_h.weight": (1, "mlp.down_proj.weight", 1, 0),
"model.language_model.encoder.final_layernorm.weight": (0, "model.norm.weight", None, 0),
"model.language_model.output_layer.weight": (1, "lm_head.weight", 0, 0),
Expand All @@ -102,7 +101,7 @@ def convert_checkpoint(p, args, config):
model_llama = {}
for _path in model_paths:
print(f'Loading {_path}')
ts = torch.load(_path)
ts = torch.load(_path, map_location='cpu')
model_llama.update(ts)
print(len(model_llama))

Expand All @@ -117,9 +116,15 @@ def convert_checkpoint(p, args, config):
model_llama[f'model.layers.{i}.self_attn.query.weight'] = q
model_llama[f'model.layers.{i}.self_attn.key_value.weight'] = torch.cat([k, v], dim=0)

gate_proj = model_llama[f'model.layers.{i}.mlp.gate_proj.weight']
up_proj = model_llama[f'model.layers.{i}.mlp.up_proj.weight']
model_llama[f'model.layers.{i}.mlp.gate_proj_up_proj.weight'] = torch.cat([gate_proj, up_proj], dim=0)

model_llama.pop(f'model.layers.{i}.self_attn.q_proj.weight')
model_llama.pop(f'model.layers.{i}.self_attn.k_proj.weight')
model_llama.pop(f'model.layers.{i}.self_attn.v_proj.weight')
model_llama.pop(f'model.layers.{i}.mlp.gate_proj.weight')
model_llama.pop(f'model.layers.{i}.mlp.up_proj.weight')

for i in range(TP):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def fix_query_key_value_ordering(param, checkpoint_version, num_splits, num_head
param = param.view(*input_shape)
return param


def get_tp_pp_degree(path_to_checkpoints):

dir_name = PurePath(path_to_checkpoints).name
Expand Down Expand Up @@ -125,8 +126,7 @@ def convert_checkpoint(config_file,
"self_attention.dense.weight": (1, "self_attn.o_proj.weight", 1, 0),
"post_attention_layernorm.weight": (0, "post_attention_layernorm.weight", None, 0),
"self_attention.core_attention.rotary_emb.inv_freq": (0, "self_attn.rotary_emb.inv_freq", None, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj.weight", 0, 0),
"mlp.dense_h_to_4h_2.weight": (1, "mlp.up_proj.weight", 0, 0),
"mlp.dense_h_to_4h.weight": (1, "mlp.gate_proj_up_proj.weight", 0, 0),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why considering "gate" and "up" proj to be fused for HF checkpoint? Shouldn't you split them from nemo checkpoint instead and then save as separate "gate" and "up" params for HF?

"mlp.dense_4h_to_h.weight": (1, "mlp.down_proj.weight", 1, 0),
"final_layernorm.weight": (0, "model.norm.weight", None, 0),
"output_layer.weight": (1, "lm_head.weight", 0, 0), # this is shared
Expand Down Expand Up @@ -217,6 +217,13 @@ def convert_checkpoint(config_file,
hf_model[hf_key_q], hf_model[hf_key_k], hf_model[hf_key_v] = torch.split(hf_model[hf_key], size_per_seg, dim=0)
hf_model.pop(hf_key)

if "dense_h_to_4h" in k:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not accurate. "gate" and "proj" fusion is per tp rank in the nemo checkpoint. So, you can't first concatenate all tps and then split to "gate" and "proj". Instead you should split them for each tp rank.

hf_key_gate_proj = f"{br_key}{ln_idx}.mlp.gate_proj.weight"
hf_key_up_proj = f"{br_key}{ln_idx}.mlp.up_proj.weight"
size_per_seg = hf_model[hf_key].shape[0] // 2
hf_model[hf_key_gate_proj], hf_model[hf_key_up_proj] = torch.split(hf_model[hf_key], size_per_seg, dim=0)
hf_model.pop(hf_key)

path = Path(output_path)
path.mkdir(parents=True, exist_ok=True)
torch.save(hf_model, str(path)+"/pytorch_model.bin")
Expand Down
2 changes: 1 addition & 1 deletion nemo/nemo/collections/nlp/parts/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def save(data, path, should_save=True, partition=0, num_partitions=1, saver=None
Default: False
"""
if saver is None:
saver = SimplerSaver()
saver = SimpleSaver()
ref_data = _rewrite_data(_get_tensors_folder(path), data, should_save, partition, num_partitions, saver)
if should_save:
saver.add_save_task(ref_data, path)
Expand Down