|
4 | 4 | # This source code is licensed under the BSD-style license found in the |
5 | 5 | # LICENSE file in the root directory of this source tree. |
6 | 6 |
|
| 7 | +import re |
7 | 8 | from typing import Any |
8 | 9 |
|
9 | 10 | from torchtitan.protocols.state_dict_adapter import StateDictAdapter |
10 | 11 |
|
| 12 | +from .args import TransformerModelArgs |
| 13 | + |
11 | 14 |
|
12 | 15 | class Llama3StateDictAdapter(StateDictAdapter): |
| 16 | + from_hf_map = { |
| 17 | + "model.embed_tokens.weight": "tok_embeddings.weight", |
| 18 | + "model.layers.{}.self_attn.q_proj.weight": "layers.{}.attention.wq.weight", |
| 19 | + "model.layers.{}.self_attn.k_proj.weight": "layers.{}.attention.wk.weight", |
| 20 | + "model.layers.{}.self_attn.v_proj.weight": "layers.{}.attention.wv.weight", |
| 21 | + "model.layers.{}.self_attn.o_proj.weight": "layers.{}.attention.wo.weight", |
| 22 | + "model.layers.{}.self_attn.rotary_emb.inv_freq": None, |
| 23 | + "model.layers.{}.mlp.gate_proj.weight": "layers.{}.feed_forward.w1.weight", |
| 24 | + "model.layers.{}.mlp.up_proj.weight": "layers.{}.feed_forward.w3.weight", |
| 25 | + "model.layers.{}.mlp.down_proj.weight": "layers.{}.feed_forward.w2.weight", |
| 26 | + "model.layers.{}.input_layernorm.weight": "layers.{}.attention_norm.weight", |
| 27 | + "model.layers.{}.post_attention_layernorm.weight": "layers.{}.ffn_norm.weight", |
| 28 | + "model.norm.weight": "norm.weight", |
| 29 | + "lm_head.weight": "output.weight", |
| 30 | + } |
| 31 | + to_hf_map = {v: k for k, v in from_hf_map.items()} |
| 32 | + |
| 33 | + # HuggingFace permutation function (exact copy from their conversion script) |
13 | 34 | @staticmethod |
14 | | - def to_hf(state_dict: dict[str, Any]) -> dict[str, Any]: |
15 | | - # TODO: implement this |
16 | | - return state_dict |
| 35 | + def _permute(w, n_heads_arg, dim1=None, dim2=None): |
| 36 | + if dim1 is None: |
| 37 | + dim1 = w.shape[0] |
| 38 | + if dim2 is None: |
| 39 | + dim2 = w.shape[1] |
| 40 | + return ( |
| 41 | + w.view(n_heads_arg, dim1 // n_heads_arg // 2, 2, dim2) |
| 42 | + .transpose(1, 2) |
| 43 | + .reshape(dim1, dim2) |
| 44 | + .clone() |
| 45 | + ) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def _reverse_permute(w, n_heads_arg, dim1=None, dim2=None): |
| 49 | + if dim1 is None: |
| 50 | + dim1 = w.shape[0] |
| 51 | + if dim2 is None: |
| 52 | + dim2 = w.shape[1] |
| 53 | + return ( |
| 54 | + w.view(n_heads_arg, 2, dim1 // n_heads_arg // 2, dim2) |
| 55 | + .transpose(1, 2) |
| 56 | + .reshape(dim1, dim2) |
| 57 | + ) |
17 | 58 |
|
18 | 59 | @staticmethod |
19 | | - def from_hf(hf_state_dict: dict[str, Any]) -> dict[str, Any]: |
20 | | - # TODO: implement this |
| 60 | + def to_hf( |
| 61 | + state_dict: dict[str, Any], model_args: TransformerModelArgs |
| 62 | + ) -> dict[str, Any]: |
| 63 | + |
| 64 | + n_heads = model_args.n_heads |
| 65 | + n_kv_heads = ( |
| 66 | + model_args.n_kv_heads if model_args.n_kv_heads is not None else n_heads |
| 67 | + ) |
| 68 | + dim = model_args.dim |
| 69 | + head_dim = dim // n_heads |
| 70 | + hf_state_dict = {} |
| 71 | + |
| 72 | + for key, value in state_dict.items(): |
| 73 | + if "layers" in key: |
| 74 | + abstract_key = re.sub(r"(\d+)", "{}", key, count=1) |
| 75 | + layer_num = re.search(r"\d+", key).group(0) |
| 76 | + new_key = Llama3StateDictAdapter.to_hf_map[abstract_key] |
| 77 | + # We need to permute the weights in wq and wk layer in order to account for the difference between |
| 78 | + # the native Llama and huggingface RoPE implementation. |
| 79 | + if abstract_key == "layers.{}.attention.wq.weight": |
| 80 | + value = Llama3StateDictAdapter._permute(value, n_heads) |
| 81 | + if abstract_key == "layers.{}.attention.wk.weight": |
| 82 | + key_value_dim = head_dim * n_kv_heads |
| 83 | + value = Llama3StateDictAdapter._permute( |
| 84 | + value, n_kv_heads, key_value_dim, dim |
| 85 | + ) |
| 86 | + |
| 87 | + if new_key is None: |
| 88 | + continue |
| 89 | + new_key = new_key.format(layer_num) |
| 90 | + else: |
| 91 | + new_key = Llama3StateDictAdapter.to_hf_map[key] |
| 92 | + |
| 93 | + hf_state_dict[new_key] = value |
21 | 94 | return hf_state_dict |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def from_hf( |
| 98 | + hf_state_dict: dict[str, Any], model_args: TransformerModelArgs |
| 99 | + ) -> dict[str, Any]: |
| 100 | + n_heads = model_args.n_heads |
| 101 | + n_kv_heads = ( |
| 102 | + model_args.n_kv_heads if model_args.n_kv_heads is not None else n_heads |
| 103 | + ) |
| 104 | + dim = model_args.dim |
| 105 | + head_dim = dim // n_heads |
| 106 | + state_dict = {} |
| 107 | + |
| 108 | + for key, value in hf_state_dict.items(): |
| 109 | + if "layers" in key: |
| 110 | + abstract_key = re.sub(r"(\d+)", "{}", key, count=1) |
| 111 | + layer_num = re.search(r"\d+", key).group(0) |
| 112 | + new_key = Llama3StateDictAdapter.from_hf_map[abstract_key] |
| 113 | + print(f"{new_key} in layer {layer_num}") |
| 114 | + |
| 115 | + # We need to permute the weights in wq and wk layer in order to account for the difference between |
| 116 | + # the native Llama and huggingface RoPE implementation. |
| 117 | + if abstract_key == "model.layers.{}.self_attn.q_proj.weight": |
| 118 | + value = Llama3StateDictAdapter._reverse_permute(value, n_heads) |
| 119 | + if abstract_key == "model.layers.{}.self_attn.k_proj.weight": |
| 120 | + key_value_dim = head_dim * n_kv_heads |
| 121 | + value = Llama3StateDictAdapter._reverse_permute( |
| 122 | + value, n_kv_heads, key_value_dim, dim |
| 123 | + ) |
| 124 | + |
| 125 | + if new_key is None: |
| 126 | + continue |
| 127 | + new_key = new_key.format(layer_num) |
| 128 | + else: |
| 129 | + new_key = Llama3StateDictAdapter.from_hf_map[key] |
| 130 | + |
| 131 | + state_dict[new_key] = value |
| 132 | + return state_dict |
0 commit comments