|
| 1 | +import argparse |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from torchtune.models.convert_weights import get_mapped_key |
| 7 | + |
| 8 | +from torchtune.training import FullModelHFCheckpointer |
| 9 | + |
| 10 | +# Standard _FROM_META weight mapping of Meta weights to TorchTune + additional bias weight mappings. |
| 11 | +_SMOLLM_FROM_META = { |
| 12 | + "tok_embeddings.weight": "tok_embeddings.weight", |
| 13 | + "norm.weight": "norm.scale", |
| 14 | + "output.weight": "output.weight", |
| 15 | + "layers.{}.attention.wk.weight": "layers.{}.attn.k_proj.weight", |
| 16 | + "layers.{}.attention.wq.weight": "layers.{}.attn.q_proj.weight", |
| 17 | + "layers.{}.attention.wv.weight": "layers.{}.attn.v_proj.weight", |
| 18 | + "layers.{}.attention.wo.weight": "layers.{}.attn.output_proj.weight", |
| 19 | + "layers.{}.attention_norm.weight": "layers.{}.sa_norm.scale", |
| 20 | + "layers.{}.ffn_norm.weight": "layers.{}.mlp_norm.scale", |
| 21 | + "layers.{}.feed_forward.w1.weight": "layers.{}.mlp.w1.weight", |
| 22 | + "layers.{}.feed_forward.w2.weight": "layers.{}.mlp.w2.weight", |
| 23 | + "layers.{}.feed_forward.w3.weight": "layers.{}.mlp.w3.weight", |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +def smollm_tune_to_meta(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: |
| 28 | + """ |
| 29 | + Convert a state dict from torchtune's format to Meta's format. This function |
| 30 | + doesn't handle any sharding or splitting of state dicts. It follows the |
| 31 | + state_dict IN -> state_dict OUT pattern. |
| 32 | +
|
| 33 | + Args: |
| 34 | + state_dict (Dict[str, torch.Tensor]): State dict in torchtune's format. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + Dict[str, torch.Tensor]: State dict in Meta's format. |
| 38 | + """ |
| 39 | + converted_state_dict = {} |
| 40 | + inverted_mapping_dict = {v: k for k, v in _SMOLLM_FROM_META.items()} |
| 41 | + for key, value in state_dict.items(): |
| 42 | + new_key = get_mapped_key(key, inverted_mapping_dict) |
| 43 | + converted_state_dict[new_key] = value |
| 44 | + |
| 45 | + return converted_state_dict |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + parser = argparse.ArgumentParser( |
| 50 | + description="Convert SmolLM weights to Meta format." |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "input_dir", |
| 54 | + type=str, |
| 55 | + help="Path to directory containing checkpoint files", |
| 56 | + ) |
| 57 | + parser.add_argument("output", type=str, help="Path to the output checkpoint") |
| 58 | + |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + # Don't necessarily need to use TorchTune checkpointer, can just aggregate checkpoint files by ourselves. |
| 62 | + checkpointer = FullModelHFCheckpointer( |
| 63 | + checkpoint_dir=args.input_dir, |
| 64 | + checkpoint_files=["model.safetensors"], |
| 65 | + output_dir=".", |
| 66 | + model_type="LLAMA", |
| 67 | + ) |
| 68 | + |
| 69 | + print("Loading checkpoint...") |
| 70 | + sd = checkpointer.load_checkpoint() |
| 71 | + |
| 72 | + print("Converting checkpoint...") |
| 73 | + sd = smollm_tune_to_meta(sd["model"]) |
| 74 | + |
| 75 | + torch.save(sd, args.output) |
| 76 | + print(f"Checkpoint saved to {args.output}") |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments