tensor_mapping.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. # ruff: noqa
  2. from __future__ import annotations
  3. from typing import Sequence
  4. from .constants import MODEL_ARCH, MODEL_TENSOR, MODEL_TENSORS, TENSOR_NAMES
  5. class TensorNameMap:
  6. mappings_cfg: dict[MODEL_TENSOR, tuple[str, ...]] = {
  7. # Token embeddings
  8. MODEL_TENSOR.TOKEN_EMBD: (
  9. "gpt_neox.embed_in", # gptneox
  10. "transformer.wte", # gpt2 gpt-j mpt refact qwen dbrx
  11. "transformer.word_embeddings", # falcon
  12. "word_embeddings", # bloom
  13. "model.embed_tokens", # llama-hf
  14. "tok_embeddings", # llama-pth
  15. "embeddings.word_embeddings", # bert nomic-bert
  16. "language_model.embedding.word_embeddings", # persimmon
  17. "wte", # gpt2
  18. "transformer.embd.wte", # phi2
  19. "model.tok_embeddings", # internlm2
  20. "model.embedding", # mamba-qbert
  21. "backbone.embedding", # mamba
  22. "backbone.embeddings", # mamba-hf
  23. "transformer.in_out_embed", # Grok
  24. ),
  25. # Token type embeddings
  26. MODEL_TENSOR.TOKEN_TYPES: (
  27. "embeddings.token_type_embeddings", # bert nomic-bert
  28. ),
  29. # Normalization of token embeddings
  30. MODEL_TENSOR.TOKEN_EMBD_NORM: (
  31. "word_embeddings_layernorm", # bloom
  32. "embeddings.LayerNorm", # bert
  33. "emb_ln", # nomic-bert
  34. ),
  35. # Position embeddings
  36. MODEL_TENSOR.POS_EMBD: (
  37. "transformer.wpe", # gpt2
  38. "embeddings.position_embeddings", # bert
  39. "wpe", # gpt2
  40. ),
  41. # Output
  42. MODEL_TENSOR.OUTPUT: (
  43. "embed_out", # gptneox
  44. "lm_head", # gpt2 mpt falcon llama-hf baichuan qwen mamba dbrx
  45. "output", # llama-pth bloom internlm2
  46. "word_embeddings_for_head", # persimmon
  47. "lm_head.linear", # phi2
  48. ),
  49. # Output norm
  50. MODEL_TENSOR.OUTPUT_NORM: (
  51. "gpt_neox.final_layer_norm", # gptneox
  52. "transformer.ln_f", # gpt2 gpt-j falcon
  53. "model.norm", # llama-hf baichuan internlm2
  54. "norm", # llama-pth
  55. "transformer.norm_f", # mpt dbrx
  56. "ln_f", # refact bloom qwen gpt2
  57. "language_model.encoder.final_layernorm", # persimmon
  58. "model.final_layernorm", # persimmon
  59. "lm_head.ln", # phi2
  60. "model.norm_f", # mamba-qbert
  61. "backbone.norm_f", # mamba
  62. "transformer.rms_norm", # Grok
  63. ),
  64. # Rope frequencies
  65. MODEL_TENSOR.ROPE_FREQS: (
  66. "rope.freqs", # llama-pth
  67. ),
  68. }
  69. block_mappings_cfg: dict[MODEL_TENSOR, tuple[str, ...]] = {
  70. # Attention norm
  71. MODEL_TENSOR.ATTN_NORM: (
  72. "gpt_neox.layers.{bid}.input_layernorm", # gptneox
  73. "transformer.h.{bid}.ln_1", # gpt2 gpt-j refact qwen
  74. "transformer.blocks.{bid}.norm_1", # mpt
  75. "transformer.h.{bid}.input_layernorm", # falcon7b
  76. "h.{bid}.input_layernorm", # bloom
  77. "transformer.h.{bid}.ln_mlp", # falcon40b
  78. "model.layers.{bid}.input_layernorm", # llama-hf
  79. "layers.{bid}.attention_norm", # llama-pth
  80. "language_model.encoder.layers.{bid}.input_layernorm", # persimmon
  81. "model.layers.{bid}.ln1", # yi
  82. "h.{bid}.ln_1", # gpt2
  83. "transformer.h.{bid}.ln", # phi2
  84. "model.layers.layers.{bid}.norm", # plamo
  85. "model.layers.{bid}.attention_norm", # internlm2
  86. "model.layers.{bid}.norm", # mamba-qbert
  87. "backbone.layers.{bid}.norm", # mamba
  88. "transformer.decoder_layer.{bid}.rms_norm", # Grok
  89. "transformer.blocks.{bid}.norm_attn_norm.norm_1", # dbrx
  90. ),
  91. # Attention norm 2
  92. MODEL_TENSOR.ATTN_NORM_2: (
  93. "transformer.h.{bid}.ln_attn", # falcon40b
  94. ),
  95. # Attention query-key-value
  96. MODEL_TENSOR.ATTN_QKV: (
  97. "gpt_neox.layers.{bid}.attention.query_key_value", # gptneox
  98. "transformer.h.{bid}.attn.c_attn", # gpt2 qwen
  99. "transformer.blocks.{bid}.attn.Wqkv", # mpt
  100. "transformer.blocks.{bid}.norm_attn_norm.attn.Wqkv", # dbrx
  101. "transformer.h.{bid}.self_attention.query_key_value", # falcon
  102. "h.{bid}.self_attention.query_key_value", # bloom
  103. "language_model.encoder.layers.{bid}.self_attention.query_key_value", # persimmon
  104. "model.layers.{bid}.self_attn.query_key_value", # persimmon
  105. "h.{bid}.attn.c_attn", # gpt2
  106. "transformer.h.{bid}.mixer.Wqkv", # phi2
  107. "encoder.layers.{bid}.attn.Wqkv", # nomic-bert
  108. ),
  109. # Attention query
  110. MODEL_TENSOR.ATTN_Q: (
  111. "model.layers.{bid}.self_attn.q_proj", # llama-hf
  112. "layers.{bid}.attention.wq", # llama-pth
  113. "encoder.layer.{bid}.attention.self.query", # bert
  114. "transformer.h.{bid}.attn.q_proj", # gpt-j
  115. "model.layers.layers.{bid}.self_attn.q_proj", # plamo
  116. "model.layers.{bid}.attention.wq", # internlm2
  117. "transformer.decoder_layer.{bid}.multi_head_attention.query" # Grok
  118. ),
  119. # Attention key
  120. MODEL_TENSOR.ATTN_K: (
  121. "model.layers.{bid}.self_attn.k_proj", # llama-hf
  122. "layers.{bid}.attention.wk", # llama-pth
  123. "encoder.layer.{bid}.attention.self.key", # bert
  124. "transformer.h.{bid}.attn.k_proj", # gpt-j
  125. "model.layers.layers.{bid}.self_attn.k_proj", # plamo
  126. "model.layers.{bid}.attention.wk", # internlm2
  127. "transformer.decoder_layer.{bid}.multi_head_attention.key" # Grok
  128. ),
  129. # Attention value
  130. MODEL_TENSOR.ATTN_V: (
  131. "model.layers.{bid}.self_attn.v_proj", # llama-hf
  132. "layers.{bid}.attention.wv", # llama-pth
  133. "encoder.layer.{bid}.attention.self.value", # bert
  134. "transformer.h.{bid}.attn.v_proj", # gpt-j
  135. "model.layers.layers.{bid}.self_attn.v_proj", # plamo
  136. "model.layers.{bid}.attention.wv", # internlm2
  137. "transformer.decoder_layer.{bid}.multi_head_attention.value" # Grok
  138. ),
  139. # Attention output
  140. MODEL_TENSOR.ATTN_OUT: (
  141. "gpt_neox.layers.{bid}.attention.dense", # gptneox
  142. "transformer.h.{bid}.attn.c_proj", # gpt2 refact qwen
  143. "transformer.blocks.{bid}.attn.out_proj", # mpt
  144. "transformer.h.{bid}.self_attention.dense", # falcon
  145. "h.{bid}.self_attention.dense", # bloom
  146. "model.layers.{bid}.self_attn.o_proj", # llama-hf
  147. "layers.{bid}.attention.wo", # llama-pth
  148. "encoder.layer.{bid}.attention.output.dense", # bert
  149. "transformer.h.{bid}.attn.out_proj", # gpt-j
  150. "language_model.encoder.layers.{bid}.self_attention.dense", # persimmon
  151. "model.layers.{bid}.self_attn.dense", # persimmon
  152. "h.{bid}.attn.c_proj", # gpt2
  153. "transformer.h.{bid}.mixer.out_proj", # phi2
  154. "model.layers.layers.{bid}.self_attn.o_proj", # plamo
  155. "model.layers.{bid}.attention.wo", # internlm2
  156. "encoder.layers.{bid}.attn.out_proj", # nomic-bert
  157. "transformer.decoder_layer.{bid}.multi_head_attention.linear", # Grok
  158. "transformer.blocks.{bid}.norm_attn_norm.attn.out_proj", # dbrx
  159. ),
  160. # Attention output norm
  161. MODEL_TENSOR.ATTN_OUT_NORM: (
  162. "encoder.layer.{bid}.attention.output.LayerNorm", # bert
  163. "encoder.layers.{bid}.norm1", # nomic-bert
  164. "transformer.decoder_layer.{bid}.rms_norm_1", # Grok
  165. "transformer.blocks.{bid}.norm_attn_norm.norm_2", # dbrx
  166. ),
  167. # Rotary embeddings
  168. MODEL_TENSOR.ATTN_ROT_EMBD: (
  169. "model.layers.{bid}.self_attn.rotary_emb.inv_freq", # llama-hf
  170. "layers.{bid}.attention.inner_attention.rope.freqs", # llama-pth
  171. "model.layers.layers.{bid}.self_attn.rotary_emb.inv_freq", # plamo
  172. "transformer.h.{bid}.attn.rotary_emb.inv_freq", # codeshell
  173. ),
  174. # Feed-forward norm
  175. MODEL_TENSOR.FFN_NORM: (
  176. "gpt_neox.layers.{bid}.post_attention_layernorm", # gptneox
  177. "transformer.h.{bid}.ln_2", # gpt2 refact qwen
  178. "h.{bid}.post_attention_layernorm", # bloom
  179. "transformer.blocks.{bid}.norm_2", # mpt
  180. "model.layers.{bid}.post_attention_layernorm", # llama-hf
  181. "layers.{bid}.ffn_norm", # llama-pth
  182. "language_model.encoder.layers.{bid}.post_attention_layernorm", # persimmon
  183. "model.layers.{bid}.ln2", # yi
  184. "h.{bid}.ln_2", # gpt2
  185. "model.layers.{bid}.ffn_norm", # internlm2
  186. "transformer.decoder_layer.{bid}.rms_norm_2", # Grok
  187. ),
  188. MODEL_TENSOR.FFN_GATE_INP: (
  189. "layers.{bid}.feed_forward.gate", # mixtral
  190. "model.layers.{bid}.block_sparse_moe.gate", # mixtral
  191. "transformer.decoder_layer.{bid}.router", # Grok
  192. "transformer.blocks.{bid}.ffn.router.layer", # dbrx
  193. ),
  194. # Feed-forward up
  195. MODEL_TENSOR.FFN_UP: (
  196. "gpt_neox.layers.{bid}.mlp.dense_h_to_4h", # gptneox
  197. "transformer.h.{bid}.mlp.c_fc", # gpt2
  198. "transformer.blocks.{bid}.ffn.up_proj", # mpt
  199. "transformer.h.{bid}.mlp.dense_h_to_4h", # falcon
  200. "h.{bid}.mlp.dense_h_to_4h", # bloom
  201. "model.layers.{bid}.mlp.up_proj", # llama-hf refact
  202. "layers.{bid}.feed_forward.w3", # llama-pth
  203. "encoder.layer.{bid}.intermediate.dense", # bert
  204. "transformer.h.{bid}.mlp.fc_in", # gpt-j
  205. "language_model.encoder.layers.{bid}.mlp.dense_h_to_4h", # persimmon
  206. "model.layers.{bid}.mlp.dense_h_to_4h", # persimmon
  207. "transformer.h.{bid}.mlp.w1", # qwen
  208. "h.{bid}.mlp.c_fc", # gpt2
  209. "transformer.h.{bid}.mlp.fc1", # phi2
  210. "model.layers.{bid}.mlp.fc1", # phi2
  211. "model.layers.layers.{bid}.mlp.up_proj", # plamo
  212. "model.layers.{bid}.feed_forward.w3", # internlm2
  213. "encoder.layers.{bid}.mlp.fc11", # nomic-bert
  214. "model.layers.{bid}.mlp.c_fc", # starcoder2
  215. ),
  216. MODEL_TENSOR.FFN_UP_EXP: (
  217. "layers.{bid}.feed_forward.experts.w3", # mixtral (merged)
  218. "transformer.decoder_layer.{bid}.moe.linear_v", # Grok (merged)
  219. "transformer.blocks.{bid}.ffn.experts.mlp.v1", # dbrx
  220. ),
  221. # AWQ-activation gate
  222. MODEL_TENSOR.FFN_ACT: (
  223. "transformer.blocks.{bid}.ffn.act", # mpt
  224. ),
  225. # Feed-forward gate
  226. MODEL_TENSOR.FFN_GATE: (
  227. "model.layers.{bid}.mlp.gate_proj", # llama-hf refact
  228. "layers.{bid}.feed_forward.w1", # llama-pth
  229. "transformer.h.{bid}.mlp.w2", # qwen
  230. "model.layers.layers.{bid}.mlp.gate_proj", # plamo
  231. "model.layers.{bid}.feed_forward.w1", # internlm2
  232. "encoder.layers.{bid}.mlp.fc12", # nomic-bert
  233. ),
  234. MODEL_TENSOR.FFN_GATE_EXP: (
  235. "layers.{bid}.feed_forward.experts.w1", # mixtral (merged)
  236. "transformer.decoder_layer.{bid}.moe.linear", # Grok (merged)
  237. "transformer.blocks.{bid}.ffn.experts.mlp.w1", # dbrx
  238. ),
  239. # Feed-forward down
  240. MODEL_TENSOR.FFN_DOWN: (
  241. "gpt_neox.layers.{bid}.mlp.dense_4h_to_h", # gptneox
  242. "transformer.h.{bid}.mlp.c_proj", # gpt2 refact qwen
  243. "transformer.blocks.{bid}.ffn.down_proj", # mpt
  244. "transformer.h.{bid}.mlp.dense_4h_to_h", # falcon
  245. "h.{bid}.mlp.dense_4h_to_h", # bloom
  246. "model.layers.{bid}.mlp.down_proj", # llama-hf
  247. "layers.{bid}.feed_forward.w2", # llama-pth
  248. "encoder.layer.{bid}.output.dense", # bert
  249. "transformer.h.{bid}.mlp.fc_out", # gpt-j
  250. "language_model.encoder.layers.{bid}.mlp.dense_4h_to_h", # persimmon
  251. "model.layers.{bid}.mlp.dense_4h_to_h", # persimmon
  252. "h.{bid}.mlp.c_proj", # gpt2
  253. "transformer.h.{bid}.mlp.fc2", # phi2
  254. "model.layers.{bid}.mlp.fc2", # phi2
  255. "model.layers.layers.{bid}.mlp.down_proj", # plamo
  256. "model.layers.{bid}.feed_forward.w2", # internlm2
  257. "encoder.layers.{bid}.mlp.fc2", # nomic-bert
  258. "model.layers.{bid}.mlp.c_proj", # starcoder2
  259. ),
  260. MODEL_TENSOR.FFN_DOWN_EXP: (
  261. "layers.{bid}.feed_forward.experts.w2", # mixtral (merged)
  262. "transformer.decoder_layer.{bid}.moe.linear_1", # Grok (merged)
  263. "transformer.blocks.{bid}.ffn.experts.mlp.w2", # dbrx
  264. ),
  265. MODEL_TENSOR.ATTN_Q_NORM: (
  266. "language_model.encoder.layers.{bid}.self_attention.q_layernorm",
  267. "model.layers.{bid}.self_attn.q_layernorm", # persimmon
  268. "model.layers.{bid}.self_attn.q_norm", # cohere
  269. "transformer.blocks.{bid}.attn.q_ln", # sea-lion
  270. ),
  271. MODEL_TENSOR.ATTN_K_NORM: (
  272. "language_model.encoder.layers.{bid}.self_attention.k_layernorm",
  273. "model.layers.{bid}.self_attn.k_layernorm", # persimmon
  274. "model.layers.{bid}.self_attn.k_norm", # cohere
  275. "transformer.blocks.{bid}.attn.k_ln", # sea-lion
  276. ),
  277. MODEL_TENSOR.ROPE_FREQS: (
  278. "language_model.encoder.layers.{bid}.self_attention.rotary_emb.inv_freq", # persimmon
  279. ),
  280. MODEL_TENSOR.LAYER_OUT_NORM: (
  281. "encoder.layer.{bid}.output.LayerNorm", # bert
  282. "encoder.layers.{bid}.norm2", # nomic-bert
  283. "transformer.decoder_layer.{bid}.rms_norm_3", # Grok
  284. ),
  285. MODEL_TENSOR.SSM_IN: (
  286. "model.layers.{bid}.in_proj",
  287. "backbone.layers.{bid}.mixer.in_proj",
  288. ),
  289. MODEL_TENSOR.SSM_CONV1D: (
  290. "model.layers.{bid}.conv1d",
  291. "backbone.layers.{bid}.mixer.conv1d",
  292. ),
  293. MODEL_TENSOR.SSM_X: (
  294. "model.layers.{bid}.x_proj",
  295. "backbone.layers.{bid}.mixer.x_proj",
  296. ),
  297. MODEL_TENSOR.SSM_DT: (
  298. "model.layers.{bid}.dt_proj",
  299. "backbone.layers.{bid}.mixer.dt_proj",
  300. ),
  301. MODEL_TENSOR.SSM_A: (
  302. "model.layers.{bid}.A_log",
  303. "backbone.layers.{bid}.mixer.A_log",
  304. ),
  305. MODEL_TENSOR.SSM_D: (
  306. "model.layers.{bid}.D",
  307. "backbone.layers.{bid}.mixer.D",
  308. ),
  309. MODEL_TENSOR.SSM_OUT: (
  310. "model.layers.{bid}.out_proj",
  311. "backbone.layers.{bid}.mixer.out_proj",
  312. ),
  313. }
  314. mapping: dict[str, tuple[MODEL_TENSOR, str]]
  315. def __init__(self, arch: MODEL_ARCH, n_blocks: int):
  316. self.mapping = {}
  317. for tensor, keys in self.mappings_cfg.items():
  318. if tensor not in MODEL_TENSORS[arch]:
  319. continue
  320. tensor_name = TENSOR_NAMES[tensor]
  321. self.mapping[tensor_name] = (tensor, tensor_name)
  322. for key in keys:
  323. self.mapping[key] = (tensor, tensor_name)
  324. for bid in range(n_blocks):
  325. for tensor, keys in self.block_mappings_cfg.items():
  326. if tensor not in MODEL_TENSORS[arch]:
  327. continue
  328. # TODO: make this configurable
  329. n_experts = 8
  330. for xid in range(n_experts):
  331. tensor_name = TENSOR_NAMES[tensor].format(bid=bid, xid=xid)
  332. self.mapping[tensor_name] = (tensor, tensor_name)
  333. for key in keys:
  334. key = key.format(bid=bid, xid=xid)
  335. self.mapping[key] = (tensor, tensor_name)
  336. def get_type_and_name(
  337. self, key: str, try_suffixes: Sequence[str] = ()
  338. ) -> tuple[MODEL_TENSOR, str] | None:
  339. result = self.mapping.get(key)
  340. if result is not None:
  341. return result
  342. for suffix in try_suffixes:
  343. if key.endswith(suffix):
  344. result = self.mapping.get(key[:-len(suffix)])
  345. if result is not None:
  346. return result[0], result[1] + suffix
  347. return None
  348. def get_name(self, key: str,
  349. try_suffixes: Sequence[str] = ()) -> str | None:
  350. result = self.get_type_and_name(key, try_suffixes=try_suffixes)
  351. if result is None:
  352. return None
  353. return result[1]
  354. def get_type(
  355. self, key: str,
  356. try_suffixes: Sequence[str] = ()) -> MODEL_TENSOR | None:
  357. result = self.get_type_and_name(key, try_suffixes=try_suffixes)
  358. if result is None:
  359. return None
  360. return result[0]
  361. def __getitem__(self, key: str) -> str:
  362. try:
  363. return self.mapping[key][1]
  364. except KeyError:
  365. raise KeyError(key)
  366. def __contains__(self, key: str) -> bool:
  367. return key in self.mapping
  368. def __repr__(self) -> str:
  369. return repr(self.mapping)
  370. def get_tensor_name_map(arch: MODEL_ARCH, n_blocks: int) -> TensorNameMap:
  371. return TensorNameMap(arch, n_blocks)