MCPcopy Index your code
hub / github.com/microsoft/BitNet / convert_model_names

Function convert_model_names

utils/convert.py:1302–1360  ·  view source on GitHub ↗
(model: LazyModel, params: Params, skip_unknown: bool)

Source from the content-addressed store, hash-verified

1300
1301
1302def convert_model_names(model: LazyModel, params: Params, skip_unknown: bool) -> LazyModel:
1303 tmap = gguf.TensorNameMap(ARCH, params.n_layer)
1304 should_skip = set(gguf.MODEL_TENSOR_SKIP.get(ARCH, []))
1305
1306 tmp = model
1307
1308 # merge experts into one tensor
1309 if params.n_experts and params.n_experts > 0:
1310 for i_l in range(params.n_layer):
1311 for w in range(1, 4):
1312 experts = []
1313 for e in range(params.n_experts):
1314 if f"layers.{i_l}.feed_forward.experts.{e}.w{w}.weight" in model:
1315 experts.append(model[f"layers.{i_l}.feed_forward.experts.{e}.w{w}.weight"])
1316 del tmp[f"layers.{i_l}.feed_forward.experts.{e}.w{w}.weight"]
1317 elif f"model.layers.{i_l}.block_sparse_moe.experts.{e}.w{w}.weight" in model:
1318 experts.append(model[f"model.layers.{i_l}.block_sparse_moe.experts.{e}.w{w}.weight"])
1319 del tmp[f"model.layers.{i_l}.block_sparse_moe.experts.{e}.w{w}.weight"]
1320 else:
1321 raise ValueError(f"Expert tensor not found: layers.{i_l}.feed_forward.experts.{e}.w{w}.weight")
1322 tmp[f"layers.{i_l}.feed_forward.experts.w{w}.weight"] = pack_experts_lazy(experts)
1323
1324 # HF models permut or pack some of the tensors, so we need to undo that
1325 for i in itertools.count():
1326 if f"model.layers.{i}.self_attn.q_proj.weight" in model:
1327 logger.debug(f"Permuting layer {i}")
1328 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)
1329 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)
1330 # tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = model[f"model.layers.{i}.self_attn.v_proj.weight"]
1331 elif f"model.layers.{i}.self_attn.W_pack.weight" in model:
1332 logger.debug(f"Unpacking and permuting layer {i}")
1333 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)
1334 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)
1335 tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = part_lazy (model[f"model.layers.{i}.self_attn.W_pack.weight"], 2)
1336 del tmp[f"model.layers.{i}.self_attn.W_pack.weight"]
1337 else:
1338 break
1339
1340 # check if is bitnet
1341 if ARCH == 33:
1342 del tmp['output.weight']
1343
1344 out: LazyModel = {}
1345 for name, lazy_tensor in model.items():
1346 tensor_type, name_new = tmap.get_type_and_name(name, try_suffixes = (".weight", ".bias")) or (None, None)
1347 if name_new is None:
1348 if skip_unknown:
1349 logger.warning(f"Unexpected tensor name: {name} - skipping")
1350 continue
1351 raise ValueError(f"Unexpected tensor name: {name}. Use --skip-unknown to ignore it (e.g. LLaVA)")
1352
1353 if tensor_type in should_skip:
1354 logger.debug(f"skipping tensor {name_new}")
1355 continue
1356
1357 logger.debug(f"{name:48s} -> {name_new:40s} | {lazy_tensor.data_type.name:6s} | {lazy_tensor.shape}")
1358 out[name_new] = lazy_tensor
1359

Callers 1

mainFunction · 0.70

Calls 4

pack_experts_lazyFunction · 0.70
permute_lazyFunction · 0.70
permute_part_lazyFunction · 0.70
part_lazyFunction · 0.70

Tested by

no test coverage detected