|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +from torchtune.models.convert_weights import get_mapped_key |
| 8 | + |
| 9 | +# Standard _FROM_META weight mapping of Meta weights to TorchTune + additional bias weight mappings. |
| 10 | +_HF__CODEGEN_2_FROM_META = { |
| 11 | + "tok_embeddings.weight": "transformer.wte.weight", |
| 12 | + "layers.{}.attention_norm.weight": "transformer.h.{}.ln_1.weight", |
| 13 | + "layers.{}.attention_norm.bias": "transformer.h.{}.ln_1.bias", |
| 14 | + "layers.{}.attention.wq.weight": "transformer.h.{}.attn.q_proj.weight", |
| 15 | + "layers.{}.attention.wk.weight": "transformer.h.{}.attn.k_proj.weight", |
| 16 | + "layers.{}.attention.wv.weight": "transformer.h.{}.attn.v_proj.weight", |
| 17 | + "layers.{}.attention.wo.weight": "transformer.h.{}.attn.out_proj.weight", |
| 18 | + "layers.{}.feed_forward.fc_in.weight": "transformer.h.{}.mlp.fc_in.weight", |
| 19 | + "layers.{}.feed_forward.fc_in.bias": "transformer.h.{}.mlp.fc_in.bias", |
| 20 | + "layers.{}.feed_forward.fc_out.weight": "transformer.h.{}.mlp.fc_out.weight", |
| 21 | + "layers.{}.feed_forward.fc_out.bias": "transformer.h.{}.mlp.fc_out.bias", |
| 22 | + "norm.weight": "transformer.ln_f.weight", |
| 23 | + "norm.bias": "transformer.ln_f.bias", |
| 24 | + "output.weight": "lm_head.weight", |
| 25 | + "output.bias": "lm_head.bias", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def codegen_hf_to_meta(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: |
| 30 | + converted_state_dict = {} |
| 31 | + keys_to_remove = [] |
| 32 | + for key in state_dict: |
| 33 | + if ".attn.causal_mask" in key: |
| 34 | + keys_to_remove.append(key) |
| 35 | + for key in keys_to_remove: |
| 36 | + state_dict.pop(key) |
| 37 | + inverted_mapping_dict = {v: k for k, v in _HF__CODEGEN_2_FROM_META.items()} |
| 38 | + for key, value in state_dict.items(): |
| 39 | + if key.endswith("attn.qkv_proj.weight"): |
| 40 | + mp_num = 8 # This number is from modeling_codegen.py |
| 41 | + dim, dim_kv = value.shape |
| 42 | + block = dim // mp_num |
| 43 | + split_size = block // 3 |
| 44 | + |
| 45 | + qkv_blocks = value.reshape(mp_num, block, dim_kv) |
| 46 | + q_blocks = qkv_blocks[:, 0:split_size, :] |
| 47 | + v_blocks = qkv_blocks[:, split_size : 2 * split_size, :] |
| 48 | + k_blocks = qkv_blocks[:, 2 * split_size : 3 * split_size, :] |
| 49 | + |
| 50 | + q = q_blocks.reshape(-1, dim_kv) |
| 51 | + v = v_blocks.reshape(-1, dim_kv) |
| 52 | + k = k_blocks.reshape(-1, dim_kv) |
| 53 | + |
| 54 | + for new_key, new_value in [("q_proj", q), ("k_proj", k), ("v_proj", v)]: |
| 55 | + new_key = key.replace("qkv_proj", new_key) |
| 56 | + new_key = get_mapped_key(new_key, inverted_mapping_dict) |
| 57 | + converted_state_dict[new_key] = new_value |
| 58 | + else: |
| 59 | + mapped_key = get_mapped_key(key, inverted_mapping_dict) |
| 60 | + converted_state_dict[mapped_key] = value |
| 61 | + |
| 62 | + return converted_state_dict |
| 63 | + |
| 64 | + |
| 65 | +def convert_weights(input_dir_or_checkpoint: str, output_file: str) -> None: |
| 66 | + pt_path = os.path.join(input_dir_or_checkpoint, "pytorch_model.bin") |
| 67 | + print("Loading checkpoint from file...") |
| 68 | + sd = torch.load(pt_path, map_location="cpu", weights_only=True) |
| 69 | + print("Converting checkpoint...") |
| 70 | + sd = codegen_hf_to_meta(sd) |
| 71 | + |
| 72 | + print("Saving checkpoint...") |
| 73 | + torch.save(sd, output_file) |
| 74 | + print("Done.") |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + parser = argparse.ArgumentParser( |
| 79 | + description="Convert Codegen weights to Meta format." |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "input_dir", |
| 83 | + type=str, |
| 84 | + help="Path to directory containing checkpoint files, or path to a single checkpoint file.", |
| 85 | + ) |
| 86 | + parser.add_argument("output", type=str, help="Path to the output checkpoint") |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + convert_weights(args.input_dir, args.output) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments