MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / convert_model_names

Function convert_model_names

convert.py:1001–1036  ·  view source on GitHub ↗
(model: LazyModel, params: Params)

Source from the content-addressed store, hash-verified

999 for (name, tensor) in model.items()}
1000
1001def convert_model_names(model: LazyModel, params: Params) -> LazyModel:
1002 tmap = gguf.TensorNameMap(params.arch, params.n_layer)
1003 should_skip: set[gguf.MODEL_TENSOR] = set(gguf.MODEL_TENSOR_SKIP.get(params.arch, []))
1004
1005 tmp = model
1006
1007 # HF models permut or pack some of the tensors, so we need to undo that
1008 for i in itertools.count():
1009 if f"model.layers.{i}.self_attn.q_proj.weight" in model:
1010 print(f"Permuting layer {i}")
1011 tmp[f"model.layers.{i}.self_attn.q_proj.weight"] = permute_lazy(model[f"model.layers.{i}.self_attn.q_proj.weight"], params.n_head, params.n_head)
1012 tmp[f"model.layers.{i}.self_attn.k_proj.weight"] = permute_lazy(model[f"model.layers.{i}.self_attn.k_proj.weight"], params.n_head, params.n_head_kv)
1013 #tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = model[f"model.layers.{i}.self_attn.v_proj.weight"]
1014 elif f"model.layers.{i}.self_attn.W_pack.weight" in model:
1015 print(f"Unpacking and permuting layer {i}")
1016 tmp[f"model.layers.{i}.self_attn.q_proj.weight"] = permute_part_lazy(model[f"model.layers.{i}.self_attn.W_pack.weight"], 0, params.n_head, params.n_head)
1017 tmp[f"model.layers.{i}.self_attn.k_proj.weight"] = permute_part_lazy(model[f"model.layers.{i}.self_attn.W_pack.weight"], 1, params.n_head, params.n_head_kv)
1018 tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = part_lazy (model[f"model.layers.{i}.self_attn.W_pack.weight"], 2)
1019 del tmp[f"model.layers.{i}.self_attn.W_pack.weight"]
1020 else:
1021 break
1022
1023 out: LazyModel = {}
1024 for name, lazy_tensor in model.items():
1025 tensor_type, name_new = tmap.get_type_and_name(name, try_suffixes = (".weight", ".bias")) or (None, None)
1026 if name_new is None:
1027 raise Exception(f"Unexpected tensor name: {name}")
1028
1029 if tensor_type in should_skip:
1030 print(f"skipping tensor {name_new}")
1031 continue
1032
1033 print(f"{name:48s} -> {name_new:40s} | {lazy_tensor.data_type.name:6s} | {lazy_tensor.shape}")
1034 out[name_new] = lazy_tensor
1035
1036 return out
1037
1038def postprocess_transpose(model: LazyModel) -> LazyModel:
1039 """Transpose ffn_down matrices for Axpy ops."""

Callers 1

mainFunction · 0.70

Calls 8

get_type_and_nameMethod · 0.95
permute_lazyFunction · 0.70
permute_part_lazyFunction · 0.70
part_lazyFunction · 0.70
setFunction · 0.50
printFunction · 0.50
getMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected