| import glob |
| import re |
| import shutil |
| import sys |
|
|
| import accelerate |
| import torch |
| from configuration_afmoe_scm import AfmoeSCMConfig |
| from modeling_afmoe_scm import AfmoeSCMForCausalLM |
| from configuration_afmoe import AfmoeConfig |
| from safetensors import safe_open |
|
|
| input_model = sys.argv[1] |
| output_model_path = sys.argv[2] |
|
|
| auto_map = { |
| "AutoConfig": "configuration_afmoe_scm.AfmoeSCMConfig", |
| "AutoModel": "modeling_afmoe_scm.AfmoeSCMModel", |
| "AutoModelForCausalLM": "modeling_afmoe_scm.AfmoeSCMForCausalLM" |
| } |
|
|
| cfg_standard_moe = AfmoeConfig.from_pretrained(input_model) |
| cfg_shared_moe = AfmoeSCMConfig( |
| auto_map=auto_map, |
| layer_types=cfg_standard_moe.layer_types, |
| global_attn_every_n_layers=cfg_standard_moe.global_attn_every_n_layers, |
| load_balance_coeff=cfg_standard_moe.load_balance_coeff, |
| mup_enabled=cfg_standard_moe.mup_enabled, |
| num_dense_layers=cfg_standard_moe.num_dense_layers, |
| num_expert_groups=cfg_standard_moe.num_expert_groups, |
| num_limited_groups=cfg_standard_moe.num_limited_groups, |
| route_norm=cfg_standard_moe.route_norm, |
| route_scale=cfg_standard_moe.route_scale, |
| score_func=cfg_standard_moe.score_func, |
| topk_group=cfg_standard_moe.topk_group, |
| num_shared_experts=cfg_standard_moe.num_shared_experts, |
| vocab_size=cfg_standard_moe.vocab_size, |
| hidden_size=cfg_standard_moe.hidden_size, |
| intermediate_size=cfg_standard_moe.intermediate_size, |
| num_hidden_layers=cfg_standard_moe.num_hidden_layers, |
| num_attention_heads=cfg_standard_moe.num_attention_heads, |
| num_key_value_heads=cfg_standard_moe.num_key_value_heads, |
| hidden_act=cfg_standard_moe.hidden_act, |
| max_position_embeddings=cfg_standard_moe.max_position_embeddings, |
| initializer_range=cfg_standard_moe.initializer_range, |
| rms_norm_eps=cfg_standard_moe.rms_norm_eps, |
| use_cache=cfg_standard_moe.use_cache, |
| tie_word_embeddings=cfg_standard_moe.tie_word_embeddings, |
| rope_theta=cfg_standard_moe.rope_theta, |
| rope_scaling=cfg_standard_moe.rope_scaling, |
| sliding_window=cfg_standard_moe.sliding_window, |
| attention_dropout=cfg_standard_moe.attention_dropout, |
| moe_intermediate_size=cfg_standard_moe.moe_intermediate_size, |
| num_experts_per_tok=cfg_standard_moe.num_experts_per_tok, |
| num_experts=cfg_standard_moe.num_experts, |
| head_dim=cfg_standard_moe.head_dim, |
| eos_token_id=cfg_standard_moe.eos_token_id, |
| pad_token_id=cfg_standard_moe.pad_token_id, |
| torch_dtype=cfg_standard_moe.torch_dtype, |
| ) |
|
|
| num_experts = cfg_standard_moe.num_experts |
|
|
| with accelerate.init_empty_weights(): |
| model_shared_moe = AfmoeSCMForCausalLM(cfg_shared_moe) |
|
|
| model_shared_moe = model_shared_moe.to(torch.bfloat16) |
| new_state_dict = {} |
| pattern = f"{input_model}/model-*-of-*.safetensors" |
| files = sorted(glob.glob(pattern)) |
|
|
| if len(files) == 0: |
| raise FileNotFoundError |
| tensors = {} |
|
|
| for file_path in files: |
| print(f"processing {file_path}") |
| with safe_open(file_path, framework="pt", device="cpu") as f: |
| for key in f.keys(): |
| tensor = f.get_tensor(key) |
| tensors[key] = tensor |
|
|
| for key in tensors: |
| if "experts" not in key or "shared_experts" in key: |
| new_state_dict[key] = tensors[key] |
| elif "experts.0" in key: |
| layer_num = int(re.search(r"\d+", key).group()) |
| new_state_dict[ |
| f"model.layers.{layer_num}.mlp.moe_mlp.output_experts.weight" |
| ] = torch.stack( |
| [ |
| tensors[f"model.layers.{layer_num}.mlp.experts.{i}.down_proj.weight"] |
| for i in range(num_experts) |
| ] |
| ) |
| new_state_dict[f"model.layers.{layer_num}.mlp.moe_mlp.experts.weight"] = ( |
| torch.stack( |
| [ |
| torch.cat( |
| [ |
| tensors[ |
| f"model.layers.{layer_num}.mlp.experts.{i}.up_proj.weight" |
| ], |
| tensors[ |
| f"model.layers.{layer_num}.mlp.experts.{i}.gate_proj.weight" |
| ], |
| ], |
| dim=0, |
| ) |
| for i in range(num_experts) |
| ] |
| ) |
| ) |
| model_shared_moe.load_state_dict(new_state_dict, strict=True, assign=True) |
| model_shared_moe.save_pretrained(output_model_path) |
| cfg_shared_moe.save_pretrained(output_model_path) |
|
|
|
|
| shutil.copy( |
| "modeling_afmoe_scm.py", |
| output_model_path + "/" + "modeling_afmoe_scm.py", |
| ) |
| shutil.copy( |
| "configuration_afmoe_scm.py", |
| output_model_path + "/" + "configuration_afmoe_scm.py", |
| ) |
| for i in ["special_tokens_map.json", "tokenizer_config.json", "tokenizer.json", "chat_template.jinja"]: |
| shutil.copy(input_model + "/" + i, output_model_path + "/" + i) |
|
|