New conversion scripts
Browse files- convert_hf_to_scm.py +115 -0
- convert_scm_to_hf.py +94 -0
convert_hf_to_scm.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import glob
|
2 |
+
import re
|
3 |
+
import shutil
|
4 |
+
import sys
|
5 |
+
|
6 |
+
import accelerate
|
7 |
+
import torch
|
8 |
+
from configuration_qwen3_shared_moe import Qwen3SharedMoeConfig
|
9 |
+
from modeling_qwen3_shared_moe import Qwen3SharedMoeForCausalLM
|
10 |
+
from safetensors import safe_open
|
11 |
+
from transformers.models.qwen3_moe.configuration_qwen3_moe import Qwen3MoeConfig
|
12 |
+
|
13 |
+
input_model = sys.argv[1]
|
14 |
+
output_model_path = sys.argv[2]
|
15 |
+
|
16 |
+
cfg_standard_moe = Qwen3MoeConfig.from_pretrained(input_model)
|
17 |
+
cfg_shared_moe = Qwen3SharedMoeConfig(
|
18 |
+
vocab_size=cfg_standard_moe.vocab_size,
|
19 |
+
hidden_size=cfg_standard_moe.hidden_size,
|
20 |
+
intermediate_size=cfg_standard_moe.intermediate_size,
|
21 |
+
num_hidden_layers=cfg_standard_moe.num_hidden_layers,
|
22 |
+
num_attention_heads=cfg_standard_moe.num_attention_heads,
|
23 |
+
num_key_value_heads=cfg_standard_moe.num_key_value_heads,
|
24 |
+
hidden_act=cfg_standard_moe.hidden_act,
|
25 |
+
max_position_embeddings=cfg_standard_moe.max_position_embeddings,
|
26 |
+
initializer_range=cfg_standard_moe.initializer_range,
|
27 |
+
rms_norm_eps=cfg_standard_moe.rms_norm_eps,
|
28 |
+
use_cache=cfg_standard_moe.use_cache,
|
29 |
+
tie_word_embeddings=cfg_standard_moe.tie_word_embeddings,
|
30 |
+
rope_theta=cfg_standard_moe.rope_theta,
|
31 |
+
rope_scaling=cfg_standard_moe.rope_scaling,
|
32 |
+
attention_bias=cfg_standard_moe.attention_bias,
|
33 |
+
use_sliding_window=cfg_standard_moe.use_sliding_window,
|
34 |
+
sliding_window=cfg_standard_moe.sliding_window,
|
35 |
+
max_window_layers=cfg_standard_moe.max_window_layers,
|
36 |
+
attention_dropout=cfg_standard_moe.attention_dropout,
|
37 |
+
decoder_sparse_step=cfg_standard_moe.decoder_sparse_step,
|
38 |
+
moe_intermediate_size=cfg_standard_moe.moe_intermediate_size,
|
39 |
+
num_experts_per_tok=cfg_standard_moe.num_experts_per_tok,
|
40 |
+
num_experts=cfg_standard_moe.num_experts,
|
41 |
+
norm_topk_prob=cfg_standard_moe.norm_topk_prob,
|
42 |
+
output_router_logits=cfg_standard_moe.output_router_logits,
|
43 |
+
router_aux_loss_coef=cfg_standard_moe.router_aux_loss_coef,
|
44 |
+
shared_expert_intermediate_size=None,
|
45 |
+
mlp_only_layers=cfg_standard_moe.mlp_only_layers,
|
46 |
+
head_dim=cfg_standard_moe.head_dim,
|
47 |
+
)
|
48 |
+
|
49 |
+
num_experts = cfg_standard_moe.num_experts
|
50 |
+
|
51 |
+
with accelerate.init_empty_weights():
|
52 |
+
model_shared_moe = Qwen3SharedMoeForCausalLM(cfg_shared_moe)
|
53 |
+
|
54 |
+
model_shared_moe = model_shared_moe.to(torch.bfloat16)
|
55 |
+
new_state_dict = {}
|
56 |
+
pattern = f"{input_model}/model-*-of-*.safetensors"
|
57 |
+
files = sorted(glob.glob(pattern))
|
58 |
+
|
59 |
+
if len(files) == 0:
|
60 |
+
raise FileNotFoundError
|
61 |
+
tensors = {}
|
62 |
+
|
63 |
+
for file_path in files:
|
64 |
+
print(f"processing {file_path}")
|
65 |
+
with safe_open(file_path, framework="pt", device="cpu") as f:
|
66 |
+
for key in f.keys():
|
67 |
+
tensor = f.get_tensor(key)
|
68 |
+
tensors[key] = tensor
|
69 |
+
|
70 |
+
for key in tensors:
|
71 |
+
if "experts" not in key:
|
72 |
+
new_state_dict[key] = tensors[key]
|
73 |
+
elif "experts.0" in key:
|
74 |
+
layer_num = int(re.search(r"\d+", key).group())
|
75 |
+
new_state_dict[
|
76 |
+
f"model.layers.{layer_num}.mlp.moe_mlp.output_experts.weight"
|
77 |
+
] = torch.stack(
|
78 |
+
[
|
79 |
+
tensors[f"model.layers.{layer_num}.mlp.experts.{i}.down_proj.weight"]
|
80 |
+
for i in range(num_experts)
|
81 |
+
]
|
82 |
+
)
|
83 |
+
new_state_dict[f"model.layers.{layer_num}.mlp.moe_mlp.experts.weight"] = (
|
84 |
+
torch.stack(
|
85 |
+
[
|
86 |
+
torch.cat(
|
87 |
+
[
|
88 |
+
tensors[
|
89 |
+
f"model.layers.{layer_num}.mlp.experts.{i}.up_proj.weight"
|
90 |
+
],
|
91 |
+
tensors[
|
92 |
+
f"model.layers.{layer_num}.mlp.experts.{i}.gate_proj.weight"
|
93 |
+
],
|
94 |
+
],
|
95 |
+
dim=0,
|
96 |
+
)
|
97 |
+
for i in range(num_experts)
|
98 |
+
]
|
99 |
+
)
|
100 |
+
)
|
101 |
+
model_shared_moe.load_state_dict(new_state_dict, strict=True, assign=True)
|
102 |
+
model_shared_moe.save_pretrained(output_model_path)
|
103 |
+
cfg_shared_moe.save_pretrained(output_model_path)
|
104 |
+
|
105 |
+
|
106 |
+
shutil.copy(
|
107 |
+
"modeling_qwen3_shared_moe.py",
|
108 |
+
output_model_path + "/" + "modeling_qwen3_shared_moe.py",
|
109 |
+
)
|
110 |
+
shutil.copy(
|
111 |
+
"configuration_qwen3_shared_moe.py",
|
112 |
+
output_model_path + "/" + "configuration_qwen3_shared_moe.py",
|
113 |
+
)
|
114 |
+
for i in ["merges.txt", "tokenizer_config.json", "tokenizer.json", "vocab.json"]:
|
115 |
+
shutil.copy(input_model + "/" + i, output_model_path + "/" + i)
|
convert_scm_to_hf.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import glob
|
2 |
+
import re
|
3 |
+
import shutil
|
4 |
+
import sys
|
5 |
+
|
6 |
+
import accelerate
|
7 |
+
import torch
|
8 |
+
from configuration_qwen3_shared_moe import Qwen3SharedMoeConfig
|
9 |
+
from safetensors import safe_open
|
10 |
+
from transformers.models.qwen3_moe.configuration_qwen3_moe import Qwen3MoeConfig
|
11 |
+
from transformers.models.qwen3_moe.modeling_qwen3_moe import Qwen3MoeForCausalLM
|
12 |
+
|
13 |
+
input_model = sys.argv[1]
|
14 |
+
output_model_path = sys.argv[2]
|
15 |
+
|
16 |
+
cfg_shared_moe = Qwen3SharedMoeConfig.from_pretrained(input_model)
|
17 |
+
cfg_standard_moe = Qwen3MoeConfig(
|
18 |
+
vocab_size=cfg_shared_moe.vocab_size,
|
19 |
+
hidden_size=cfg_shared_moe.hidden_size,
|
20 |
+
intermediate_size=cfg_shared_moe.intermediate_size,
|
21 |
+
num_hidden_layers=cfg_shared_moe.num_hidden_layers,
|
22 |
+
num_attention_heads=cfg_shared_moe.num_attention_heads,
|
23 |
+
num_key_value_heads=cfg_shared_moe.num_key_value_heads,
|
24 |
+
hidden_act=cfg_shared_moe.hidden_act,
|
25 |
+
max_position_embeddings=cfg_shared_moe.max_position_embeddings,
|
26 |
+
initializer_range=cfg_shared_moe.initializer_range,
|
27 |
+
rms_norm_eps=cfg_shared_moe.rms_norm_eps,
|
28 |
+
use_cache=cfg_shared_moe.use_cache,
|
29 |
+
tie_word_embeddings=cfg_shared_moe.tie_word_embeddings,
|
30 |
+
rope_theta=cfg_shared_moe.rope_theta,
|
31 |
+
rope_scaling=cfg_shared_moe.rope_scaling,
|
32 |
+
attention_bias=cfg_shared_moe.attention_bias,
|
33 |
+
use_sliding_window=cfg_shared_moe.use_sliding_window,
|
34 |
+
sliding_window=cfg_shared_moe.sliding_window,
|
35 |
+
max_window_layers=cfg_shared_moe.max_window_layers,
|
36 |
+
attention_dropout=cfg_shared_moe.attention_dropout,
|
37 |
+
decoder_sparse_step=cfg_shared_moe.decoder_sparse_step,
|
38 |
+
moe_intermediate_size=cfg_shared_moe.moe_intermediate_size,
|
39 |
+
num_experts_per_tok=cfg_shared_moe.num_experts_per_tok,
|
40 |
+
num_experts=cfg_shared_moe.num_experts,
|
41 |
+
norm_topk_prob=cfg_shared_moe.norm_topk_prob,
|
42 |
+
output_router_logits=cfg_shared_moe.output_router_logits,
|
43 |
+
router_aux_loss_coef=cfg_shared_moe.router_aux_loss_coef,
|
44 |
+
mlp_only_layers=cfg_shared_moe.mlp_only_layers,
|
45 |
+
head_dim=cfg_shared_moe.head_dim,
|
46 |
+
)
|
47 |
+
num_experts = cfg_standard_moe.num_experts
|
48 |
+
|
49 |
+
with accelerate.init_empty_weights():
|
50 |
+
model_standard_moe = Qwen3MoeForCausalLM(cfg_shared_moe)
|
51 |
+
|
52 |
+
model_standard_moe = model_standard_moe.to(torch.bfloat16)
|
53 |
+
new_state_dict = {}
|
54 |
+
pattern = f"{input_model}/model-*-of-*.safetensors"
|
55 |
+
files = sorted(glob.glob(pattern))
|
56 |
+
|
57 |
+
if len(files) == 0:
|
58 |
+
raise FileNotFoundError
|
59 |
+
tensors = {}
|
60 |
+
|
61 |
+
for file_path in files:
|
62 |
+
print(f"processing {file_path}")
|
63 |
+
with safe_open(file_path, framework="pt", device="cpu") as f:
|
64 |
+
for key in f.keys():
|
65 |
+
tensor = f.get_tensor(key)
|
66 |
+
tensors[key] = tensor
|
67 |
+
|
68 |
+
for key in tensors:
|
69 |
+
if "moe_mlp" not in key:
|
70 |
+
new_state_dict[key] = tensors[key]
|
71 |
+
elif "moe_mlp.output_experts" in key:
|
72 |
+
layer_num = int(re.search(r"\d+", key).group())
|
73 |
+
for i, tensor in enumerate(torch.unbind(tensors[key])):
|
74 |
+
new_state_dict[
|
75 |
+
f"model.layers.{layer_num}.mlp.experts.{i}.down_proj.weight"
|
76 |
+
] = tensor.contiguous()
|
77 |
+
elif "moe_mlp.experts" in key:
|
78 |
+
layer_num = int(re.search(r"\d+", key).group())
|
79 |
+
for i, tensor in enumerate(torch.unbind(tensors[key])):
|
80 |
+
(
|
81 |
+
new_state_dict[
|
82 |
+
f"model.layers.{layer_num}.mlp.experts.{i}.up_proj.weight"
|
83 |
+
],
|
84 |
+
new_state_dict[
|
85 |
+
f"model.layers.{layer_num}.mlp.experts.{i}.gate_proj.weight"
|
86 |
+
],
|
87 |
+
) = torch.chunk(tensor, 2, dim=0)
|
88 |
+
|
89 |
+
model_standard_moe.load_state_dict(new_state_dict, strict=True, assign=True)
|
90 |
+
model_standard_moe.save_pretrained(output_model_path)
|
91 |
+
cfg_standard_moe.save_pretrained(output_model_path)
|
92 |
+
|
93 |
+
for i in ["merges.txt", "tokenizer_config.json", "tokenizer.json", "vocab.json"]:
|
94 |
+
shutil.copy(input_model + "/" + i, output_model_path + "/" + i)
|