Delta-Vector commited on
Commit
2571066
·
verified ·
1 Parent(s): 610699c

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. chat_template.jinja +103 -0
  3. config.json +48 -0
  4. configuration_glm4_shared_moe.py +236 -0
  5. convert_hf_to_scm.py +126 -0
  6. generation_config.json +10 -0
  7. model-00001-of-00046.safetensors +3 -0
  8. model-00002-of-00046.safetensors +3 -0
  9. model-00003-of-00046.safetensors +3 -0
  10. model-00004-of-00046.safetensors +3 -0
  11. model-00005-of-00046.safetensors +3 -0
  12. model-00006-of-00046.safetensors +3 -0
  13. model-00007-of-00046.safetensors +3 -0
  14. model-00008-of-00046.safetensors +3 -0
  15. model-00009-of-00046.safetensors +3 -0
  16. model-00010-of-00046.safetensors +3 -0
  17. model-00011-of-00046.safetensors +3 -0
  18. model-00012-of-00046.safetensors +3 -0
  19. model-00013-of-00046.safetensors +3 -0
  20. model-00014-of-00046.safetensors +3 -0
  21. model-00015-of-00046.safetensors +3 -0
  22. model-00016-of-00046.safetensors +3 -0
  23. model-00017-of-00046.safetensors +3 -0
  24. model-00018-of-00046.safetensors +3 -0
  25. model-00019-of-00046.safetensors +3 -0
  26. model-00020-of-00046.safetensors +3 -0
  27. model-00021-of-00046.safetensors +3 -0
  28. model-00022-of-00046.safetensors +3 -0
  29. model-00023-of-00046.safetensors +3 -0
  30. model-00024-of-00046.safetensors +3 -0
  31. model-00025-of-00046.safetensors +3 -0
  32. model-00026-of-00046.safetensors +3 -0
  33. model-00027-of-00046.safetensors +3 -0
  34. model-00028-of-00046.safetensors +3 -0
  35. model-00029-of-00046.safetensors +3 -0
  36. model-00030-of-00046.safetensors +3 -0
  37. model-00031-of-00046.safetensors +3 -0
  38. model-00032-of-00046.safetensors +3 -0
  39. model-00033-of-00046.safetensors +3 -0
  40. model-00034-of-00046.safetensors +3 -0
  41. model-00035-of-00046.safetensors +3 -0
  42. model-00036-of-00046.safetensors +3 -0
  43. model-00037-of-00046.safetensors +3 -0
  44. model-00038-of-00046.safetensors +3 -0
  45. model-00039-of-00046.safetensors +3 -0
  46. model-00040-of-00046.safetensors +3 -0
  47. model-00041-of-00046.safetensors +3 -0
  48. model-00042-of-00046.safetensors +3 -0
  49. model-00043-of-00046.safetensors +3 -0
  50. model-00044-of-00046.safetensors +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
chat_template.jinja ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gMASK]<sop>
2
+ {%- if tools -%}
3
+ <|system|>
4
+ # Tools
5
+
6
+ You may call one or more functions to assist with the user query.
7
+
8
+ You are provided with function signatures within <tools></tools> XML tags:
9
+ <tools>
10
+ {% for tool in tools %}
11
+ {{ tool | tojson(ensure_ascii=False) }}
12
+ {% endfor %}
13
+ </tools>
14
+
15
+ For each function call, output the function name and arguments within the following XML format:
16
+ <tool_call>{function-name}
17
+ <arg_key>{arg-key-1}</arg_key>
18
+ <arg_value>{arg-value-1}</arg_value>
19
+ <arg_key>{arg-key-2}</arg_key>
20
+ <arg_value>{arg-value-2}</arg_value>
21
+ ...
22
+ </tool_call>{%- endif -%}
23
+ {%- macro visible_text(content) -%}
24
+ {%- if content is string -%}
25
+ {{- content }}
26
+ {%- elif content is iterable and content is not mapping -%}
27
+ {%- for item in content -%}
28
+ {%- if item is mapping and item.type == 'text' -%}
29
+ {{- item.text }}
30
+ {%- elif item is string -%}
31
+ {{- item }}
32
+ {%- endif -%}
33
+ {%- endfor -%}
34
+ {%- else -%}
35
+ {{- content }}
36
+ {%- endif -%}
37
+ {%- endmacro -%}
38
+ {%- set ns = namespace(last_user_index=-1) %}
39
+ {%- for m in messages %}
40
+ {%- if m.role == 'user' %}
41
+ {% set ns.last_user_index = loop.index0 -%}
42
+ {%- endif %}
43
+ {%- endfor %}
44
+ {% for m in messages %}
45
+ {%- if m.role == 'user' -%}<|user|>
46
+ {{ visible_text(m.content) }}
47
+ {{- '/nothink' if (enable_thinking is defined and not enable_thinking and not visible_text(m.content).endswith("/nothink")) else '' -}}
48
+ {%- elif m.role == 'assistant' -%}
49
+ <|assistant|>
50
+ {%- set reasoning_content = '' %}
51
+ {%- set content = visible_text(m.content) %}
52
+ {%- if m.reasoning_content is string %}
53
+ {%- set reasoning_content = m.reasoning_content %}
54
+ {%- else %}
55
+ {%- if '</think>' in content %}
56
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
57
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
58
+ {%- endif %}
59
+ {%- endif %}
60
+ {%- if loop.index0 > ns.last_user_index and reasoning_content -%}
61
+ {{ '\n<think>' + reasoning_content.strip() + '</think>'}}
62
+ {%- else -%}
63
+ {{ '\n<think></think>' }}
64
+ {%- endif -%}
65
+ {%- if content.strip() -%}
66
+ {{ '\n' + content.strip() }}
67
+ {%- endif -%}
68
+ {% if m.tool_calls %}
69
+ {% for tc in m.tool_calls %}
70
+ {%- if tc.function %}
71
+ {%- set tc = tc.function %}
72
+ {%- endif %}
73
+ {{ '\n<tool_call>' + tc.name }}
74
+ {% set _args = tc.arguments %}
75
+ {% for k, v in _args.items() %}
76
+ <arg_key>{{ k }}</arg_key>
77
+ <arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
78
+ {% endfor %}
79
+ </tool_call>{% endfor %}
80
+ {% endif %}
81
+ {%- elif m.role == 'tool' -%}
82
+ {%- if m.content is string -%}
83
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
84
+ {{- '<|observation|>' }}
85
+ {%- endif %}
86
+ {{- '\n<tool_response>\n' }}
87
+ {{- m.content }}
88
+ {{- '\n</tool_response>' }}
89
+ {%- else -%}
90
+ <|observation|>{% for tr in m.content %}
91
+
92
+ <tool_response>
93
+ {{ tr.output if tr.output is defined else tr }}
94
+ </tool_response>{% endfor -%}
95
+ {% endif -%}
96
+ {%- elif m.role == 'system' -%}
97
+ <|system|>
98
+ {{ visible_text(m.content) }}
99
+ {%- endif -%}
100
+ {%- endfor -%}
101
+ {%- if add_generation_prompt -%}
102
+ <|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}}
103
+ {%- endif -%}
config.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Glm4SharedMoeForCausalLM"
4
+ ],
5
+ "attention_bias": true,
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_glm4_shared_moe.Glm4SharedMoeConfig",
9
+ "AutoModel": "modeling_glm4_shared_moe.Glm4SharedMoeModel",
10
+ "AutoModelForCausalLM": "modeling_glm4_shared_moe.Glm4SharedMoeForCausalLM"
11
+ },
12
+ "eos_token_id": [
13
+ 151329,
14
+ 151336,
15
+ 151338
16
+ ],
17
+ "first_k_dense_replace": 1,
18
+ "head_dim": 128,
19
+ "hidden_act": "silu",
20
+ "hidden_size": 4096,
21
+ "initializer_range": 0.02,
22
+ "intermediate_size": 10944,
23
+ "max_position_embeddings": 131072,
24
+ "model_type": "glm4_shared_moe",
25
+ "moe_intermediate_size": 1408,
26
+ "n_group": 1,
27
+ "n_routed_experts": 128,
28
+ "n_shared_experts": 1,
29
+ "norm_topk_prob": true,
30
+ "num_attention_heads": 96,
31
+ "num_experts_per_tok": 8,
32
+ "num_hidden_layers": 46,
33
+ "num_key_value_heads": 8,
34
+ "num_nextn_predict_layers": 1,
35
+ "pad_token_id": 151329,
36
+ "partial_rotary_factor": 0.5,
37
+ "rms_norm_eps": 1e-05,
38
+ "rope_scaling": null,
39
+ "rope_theta": 1000000,
40
+ "routed_scaling_factor": 1.0,
41
+ "tie_word_embeddings": false,
42
+ "topk_group": 1,
43
+ "torch_dtype": "bfloat16",
44
+ "transformers_version": "4.54.1",
45
+ "use_cache": true,
46
+ "use_qk_norm": false,
47
+ "vocab_size": 151552
48
+ }
configuration_glm4_shared_moe.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 The ZhipuAI Inc. team and HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from transformers.configuration_utils import PretrainedConfig
17
+ from transformers.modeling_rope_utils import rope_config_validation
18
+
19
+
20
+ class Glm4SharedMoeConfig(PretrainedConfig):
21
+ r"""
22
+ This is the configuration class to store the configuration of a [`Glm4MoeModel`]. It is used to instantiate a
23
+ Glm4Moe model according to the specified arguments, defining the model architecture. Instantiating a configuration
24
+ with the defaults will yield a similar configuration to that of [THUDM/GLM-4-100B-A10B](https://huggingface.co/THUDM/GLM-4-100B-A10B).
25
+
26
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
27
+ documentation from [`PretrainedConfig`] for more information.
28
+
29
+
30
+ Args:
31
+ vocab_size (`int`, *optional*, defaults to 151552):
32
+ Vocabulary size of the Glm4Moe model. Defines the number of different tokens that can be represented by the
33
+ `inputs_ids` passed when calling [`Glm4MoeModel`]
34
+ hidden_size (`int`, *optional*, defaults to 4096):
35
+ Dimension of the hidden representations.
36
+ intermediate_size (`int`, *optional*, defaults to 10944):
37
+ Dimension of the MLP representations.
38
+ num_hidden_layers (`int`, *optional*, defaults to 46):
39
+ Number of hidden layers in the Transformer encoder.
40
+ num_attention_heads (`int`, *optional*, defaults to 96):
41
+ Number of attention heads for each attention layer in the Transformer encoder.
42
+ partial_rotary_factor (`float`, *optional*, defaults to 0.5):
43
+ The factor of the partial rotary position.
44
+ num_key_value_heads (`int`, *optional*, defaults to 8):
45
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
46
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
47
+ `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
48
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
49
+ by meanpooling all the original heads within that group. For more details, check out [this
50
+ paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `32`.
51
+
52
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
53
+ The non-linear activation function (function or string) in the decoder.
54
+ max_position_embeddings (`int`, *optional*, defaults to 131072):
55
+ The maximum sequence length that this model might ever be used with.
56
+ initializer_range (`float`, *optional*, defaults to 0.02):
57
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
58
+ rms_norm_eps (`float`, *optional*, defaults to 1e-05):
59
+ The epsilon used by the rms normalization layers.
60
+ use_cache (`bool`, *optional*, defaults to `True`):
61
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
62
+ relevant if `config.is_decoder=True`.
63
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
64
+ Whether the model's input and output word embeddings should be tied.
65
+ rope_theta (`float`, *optional*, defaults to 10000.0):
66
+ The base period of the RoPE embeddings.
67
+ rope_scaling (`Dict`, *optional*):
68
+ Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
69
+ and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
70
+ accordingly.
71
+ Expected contents:
72
+ `rope_type` (`str`):
73
+ The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
74
+ 'llama3'], with 'default' being the original RoPE implementation.
75
+ `factor` (`float`, *optional*):
76
+ Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
77
+ most scaling types, a `factor` of x will enable the model to handle sequences of length x *
78
+ original maximum pre-trained length.
79
+ `original_max_position_embeddings` (`int`, *optional*):
80
+ Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
81
+ pretraining.
82
+ `attention_factor` (`float`, *optional*):
83
+ Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
84
+ computation. If unspecified, it defaults to value recommended by the implementation, using the
85
+ `factor` field to infer the suggested value.
86
+ `beta_fast` (`float`, *optional*):
87
+ Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
88
+ ramp function. If unspecified, it defaults to 32.
89
+ `beta_slow` (`float`, *optional*):
90
+ Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
91
+ ramp function. If unspecified, it defaults to 1.
92
+ `short_factor` (`list[float]`, *optional*):
93
+ Only used with 'longrope'. The scaling factor to be applied to short contexts (<
94
+ `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
95
+ size divided by the number of attention heads divided by 2
96
+ `long_factor` (`list[float]`, *optional*):
97
+ Only used with 'longrope'. The scaling factor to be applied to long contexts (<
98
+ `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
99
+ size divided by the number of attention heads divided by 2
100
+ `low_freq_factor` (`float`, *optional*):
101
+ Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
102
+ `high_freq_factor` (`float`, *optional*):
103
+ Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
104
+ attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
105
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
106
+ attention_dropout (`float`, *optional*, defaults to 0.0):
107
+ The dropout ratio for the attention probabilities.
108
+ moe_intermediate_size (`int`, *optional*, defaults to 1408):
109
+ Intermediate size of the routed expert.
110
+ num_experts_per_tok (`int`, *optional*, defaults to 8):
111
+ number of experts per token.
112
+ n_shared_experts (`int`, *optional*, defaults to 1):
113
+ Number of shared experts.
114
+ n_routed_experts (`int`, *optional*, defaults to 128):
115
+ Number of routed experts.
116
+ routed_scaling_factor (`float`, *optional*, defaults to 1.0):
117
+ Scaling factor or routed experts.
118
+ n_group (`int`, *optional*, defaults to 1):
119
+ Number of groups for routed experts.
120
+ topk_group (`int`, *optional*, defaults to 1):
121
+ Number of selected groups for each token(for each token, ensuring the selected experts is only within `topk_group` groups).
122
+ first_k_dense_replace (`int`, *optional*, defaults to 1):
123
+ Number of dense layers in shallow layers(embed->dense->dense->...->dense->moe->moe...->lm_head).
124
+ \--k dense layers--/
125
+ norm_topk_prob (`bool`, *optional*, defaults to `True`):
126
+ Whether to normalize the topk probabilities.
127
+ use_qk_norm (`bool`, *optional*, defaults to `False`):
128
+ Whether to use query-key normalization in the attention
129
+ ```python
130
+ >>> from transformers import Glm4MoeModel, Glm4MoeConfig
131
+
132
+ >>> # Initializing a Glm4Moe style configuration
133
+ >>> configuration = Glm4MoeConfig()
134
+
135
+ >>> # Initializing a model from the GLM-4-MOE-100B-A10B style configuration
136
+ >>> model = Glm4MoeModel(configuration)
137
+
138
+ >>> # Accessing the model configuration
139
+ >>> configuration = model.config
140
+ ```"""
141
+
142
+ model_type = "glm4_shared_moe"
143
+ keys_to_ignore_at_inference = ["past_key_values"]
144
+
145
+ # Default tensor parallel plan for base model `Glm4Moe`
146
+ base_model_tp_plan = {
147
+ "layers.*.self_attn.q_proj": "colwise",
148
+ "layers.*.self_attn.k_proj": "colwise",
149
+ "layers.*.self_attn.v_proj": "colwise",
150
+ "layers.*.self_attn.o_proj": "rowwise",
151
+ "layers.*.mlp.experts.*.gate_proj": "colwise",
152
+ "layers.*.mlp.experts.*.up_proj": "colwise",
153
+ "layers.*.mlp.experts.*.down_proj": "rowwise",
154
+ "layers.*.mlp.gate_proj": "colwise",
155
+ "layers.*.mlp.up_proj": "colwise",
156
+ "layers.*.mlp.down_proj": "rowwise",
157
+ }
158
+ base_model_pp_plan = {
159
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
160
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
161
+ "norm": (["hidden_states"], ["hidden_states"]),
162
+ }
163
+
164
+ def __init__(
165
+ self,
166
+ vocab_size=151552,
167
+ hidden_size=4096,
168
+ intermediate_size=10944,
169
+ num_hidden_layers=46,
170
+ num_attention_heads=96,
171
+ partial_rotary_factor=0.5,
172
+ num_key_value_heads=8,
173
+ hidden_act="silu",
174
+ max_position_embeddings=131072,
175
+ initializer_range=0.02,
176
+ rms_norm_eps=1e-5,
177
+ use_cache=True,
178
+ tie_word_embeddings=False,
179
+ rope_theta=10000.0,
180
+ rope_scaling=None,
181
+ attention_bias=False,
182
+ attention_dropout=0.0,
183
+ moe_intermediate_size=1408,
184
+ num_experts_per_tok=8,
185
+ n_shared_experts=1,
186
+ n_routed_experts=128,
187
+ routed_scaling_factor=1.0,
188
+ n_group=1,
189
+ topk_group=1,
190
+ first_k_dense_replace=1,
191
+ norm_topk_prob=True,
192
+ use_qk_norm=False,
193
+ **kwargs,
194
+ ):
195
+ self.vocab_size = vocab_size
196
+ self.max_position_embeddings = max_position_embeddings
197
+ self.hidden_size = hidden_size
198
+ self.intermediate_size = intermediate_size
199
+ self.num_hidden_layers = num_hidden_layers
200
+ self.num_attention_heads = num_attention_heads
201
+ self.partial_rotary_factor = partial_rotary_factor
202
+
203
+ self.num_key_value_heads = num_key_value_heads
204
+ self.hidden_act = hidden_act
205
+ self.initializer_range = initializer_range
206
+ self.rms_norm_eps = rms_norm_eps
207
+ self.use_cache = use_cache
208
+ self.rope_theta = rope_theta
209
+ self.rope_scaling = rope_scaling
210
+ self.attention_bias = attention_bias
211
+ self.attention_dropout = attention_dropout
212
+ # Validate the correctness of rotary position embeddings parameters
213
+ # BC: if there is a 'type' field, move it to 'rope_type'.
214
+ if self.rope_scaling is not None and "type" in self.rope_scaling:
215
+ self.rope_scaling["rope_type"] = self.rope_scaling["type"]
216
+ rope_config_validation(self)
217
+
218
+ # MoE arguments
219
+ self.moe_intermediate_size = moe_intermediate_size
220
+ self.num_experts_per_tok = num_experts_per_tok
221
+ self.n_group = n_group
222
+ self.topk_group = topk_group
223
+ self.n_shared_experts = n_shared_experts
224
+ self.n_routed_experts = n_routed_experts
225
+ self.routed_scaling_factor = routed_scaling_factor
226
+ self.first_k_dense_replace = first_k_dense_replace
227
+ self.norm_topk_prob = norm_topk_prob
228
+ self.use_qk_norm = use_qk_norm
229
+
230
+ super().__init__(
231
+ tie_word_embeddings=tie_word_embeddings,
232
+ **kwargs,
233
+ )
234
+
235
+
236
+ __all__ = ["Glm4SharedMoeConfig"]
convert_hf_to_scm.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ import re
3
+ import shutil
4
+ import sys
5
+
6
+ import accelerate
7
+ import torch
8
+ from safetensors import safe_open
9
+ from configuration_glm4_shared_moe import Glm4SharedMoeConfig
10
+ from modeling_glm4_shared_moe import Glm4SharedMoeForCausalLM
11
+ from transformers.models.glm4_moe.configuration_glm4_moe import Glm4MoeConfig
12
+
13
+ input_model = sys.argv[1]
14
+ output_model_path = sys.argv[2]
15
+
16
+ auto_map = {
17
+ "AutoConfig": "configuration_glm4_shared_moe.Glm4SharedMoeConfig",
18
+ "AutoModel": "modeling_glm4_shared_moe.Glm4SharedMoeModel",
19
+ "AutoModelForCausalLM": "modeling_glm4_shared_moe.Glm4SharedMoeForCausalLM"
20
+ }
21
+
22
+ cfg_standard_moe = Glm4MoeConfig.from_pretrained(input_model)
23
+ cfg_shared_moe = Glm4SharedMoeConfig(
24
+ auto_map=auto_map,
25
+ vocab_size=cfg_standard_moe.vocab_size,
26
+ hidden_size=cfg_standard_moe.hidden_size,
27
+ intermediate_size=cfg_standard_moe.intermediate_size,
28
+ num_hidden_layers=cfg_standard_moe.num_hidden_layers,
29
+ num_attention_heads=cfg_standard_moe.num_attention_heads,
30
+ num_key_value_heads=cfg_standard_moe.num_key_value_heads,
31
+ hidden_act=cfg_standard_moe.hidden_act,
32
+ max_position_embeddings=cfg_standard_moe.max_position_embeddings,
33
+ initializer_range=cfg_standard_moe.initializer_range,
34
+ rms_norm_eps=cfg_standard_moe.rms_norm_eps,
35
+ use_cache=cfg_standard_moe.use_cache,
36
+ tie_word_embeddings=cfg_standard_moe.tie_word_embeddings,
37
+ rope_theta=cfg_standard_moe.rope_theta,
38
+ rope_scaling=cfg_standard_moe.rope_scaling,
39
+ attention_bias=cfg_standard_moe.attention_bias,
40
+ attention_dropout=cfg_standard_moe.attention_dropout,
41
+ moe_intermediate_size=cfg_standard_moe.moe_intermediate_size,
42
+ num_experts_per_tok=cfg_standard_moe.num_experts_per_tok,
43
+ n_routed_experts=cfg_standard_moe.n_routed_experts,
44
+ n_shared_experts=cfg_standard_moe.n_shared_experts,
45
+ norm_topk_prob=cfg_standard_moe.norm_topk_prob,
46
+ head_dim=cfg_standard_moe.head_dim,
47
+ pad_token_id=cfg_standard_moe.pad_token_id,
48
+ eos_token_id=cfg_standard_moe.eos_token_id,
49
+ routed_scaling_factor=cfg_standard_moe.routed_scaling_factor,
50
+ first_k_dense_replace=cfg_standard_moe.first_k_dense_replace,
51
+ num_nextn_predict_layers=cfg_standard_moe.num_nextn_predict_layers,
52
+ )
53
+
54
+ num_experts = cfg_standard_moe.n_routed_experts
55
+
56
+ with accelerate.init_empty_weights():
57
+ model_shared_moe = Glm4SharedMoeForCausalLM(cfg_shared_moe)
58
+
59
+ model_shared_moe = model_shared_moe.to(torch.bfloat16)
60
+ new_state_dict = {}
61
+ pattern = f"{input_model}/model-*-of-*.safetensors"
62
+ files = sorted(glob.glob(pattern))
63
+
64
+ if len(files) == 0:
65
+ raise FileNotFoundError
66
+ tensors = {}
67
+
68
+ for file_path in files:
69
+ print(f"processing {file_path}")
70
+ with safe_open(file_path, framework="pt", device="cpu") as f:
71
+ for key in f.keys():
72
+ tensor = f.get_tensor(key)
73
+ tensors[key] = tensor
74
+
75
+ for key in tensors:
76
+ try:
77
+ layer_num = int(re.search(r"\d+", key).group())
78
+ if layer_num >= cfg_standard_moe.num_hidden_layers:
79
+ continue
80
+ except:
81
+ pass
82
+ if "experts" not in key or "shared_experts" in key:
83
+ new_state_dict[key] = tensors[key]
84
+ elif "experts.0" in key:
85
+ layer_num = int(re.search(r"\d+", key).group())
86
+ new_state_dict[
87
+ f"model.layers.{layer_num}.mlp.moe_mlp.output_experts.weight"
88
+ ] = torch.stack(
89
+ [
90
+ tensors[f"model.layers.{layer_num}.mlp.experts.{i}.down_proj.weight"]
91
+ for i in range(num_experts)
92
+ ]
93
+ )
94
+ new_state_dict[f"model.layers.{layer_num}.mlp.moe_mlp.experts.weight"] = (
95
+ torch.stack(
96
+ [
97
+ torch.cat(
98
+ [
99
+ tensors[
100
+ f"model.layers.{layer_num}.mlp.experts.{i}.up_proj.weight"
101
+ ],
102
+ tensors[
103
+ f"model.layers.{layer_num}.mlp.experts.{i}.gate_proj.weight"
104
+ ],
105
+ ],
106
+ dim=0,
107
+ )
108
+ for i in range(num_experts)
109
+ ]
110
+ )
111
+ )
112
+ model_shared_moe.load_state_dict(new_state_dict, strict=True, assign=True)
113
+ model_shared_moe.save_pretrained(output_model_path)
114
+ cfg_shared_moe.save_pretrained(output_model_path)
115
+
116
+
117
+ shutil.copy(
118
+ "modeling_glm4_shared_moe.py",
119
+ output_model_path + "/" + "modeling_glm4_shared_moe.py",
120
+ )
121
+ shutil.copy(
122
+ "configuration_glm4_shared_moe.py",
123
+ output_model_path + "/" + "configuration_glm4_shared_moe.py",
124
+ )
125
+ for i in ["chat_template.jinja", "tokenizer_config.json", "tokenizer.json"]:
126
+ shutil.copy(input_model + "/" + i, output_model_path + "/" + i)
generation_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "eos_token_id": [
4
+ 151329,
5
+ 151336,
6
+ 151338
7
+ ],
8
+ "pad_token_id": 151329,
9
+ "transformers_version": "4.54.1"
10
+ }
model-00001-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d45776392254b275a24e244328e0f6cbce58a31dae011c96560f7c656fcd1c2e
3
+ size 4899547528
model-00002-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9f156975edce2bebc2424265823b7206ef4d01942a5b287060b21072fff5f630
3
+ size 4682987888
model-00003-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:216f2d894bcf6c49d8fec4d23ce347c5737828a7f56c9fcce8fdd5b8f09a07e4
3
+ size 4682987888
model-00004-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ebd54e8e5b07c3a92c428d86167908e43df3238d0787d0605bbc647cfa3807d
3
+ size 4682987888
model-00005-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:800c75f575211194ceb2459d75711b2e52a5ce9c368e39e2fc6842552d5bbeec
3
+ size 4682987888
model-00006-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a0555893367e37ff2fff426272903404743574de456c20d72a6eb01bb76f9638
3
+ size 4682987888
model-00007-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a0d3b02e2d94d6f706ecb171b15c7d31f0a8ca8127ebf65ce1475d1a9adae248
3
+ size 4682987888
model-00008-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:263f71ee54b5c440b7277922fb1d2366fb5c573bae182c4ab7fb0b6ac0369162
3
+ size 4682987888
model-00009-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b825805e3185f279ee194ab2c8261abf547ed01d40971dc1758578da0e43743b
3
+ size 4682987888
model-00010-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ae61902a0b3805a5b4fbe99cc28280bdbc897d13b10549c784c8d7f5bdb9bdc3
3
+ size 4682987912
model-00011-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:95ee704695042ffb44b599b327f51f3a630cbec23831ffa25003386b32990926
3
+ size 4682987904
model-00012-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:445e13c51c454d556b4390de5431f49afd128d1eed971b4ac29676bb67e15295
3
+ size 4682987904
model-00013-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7326b4961c34c17f812e5e19ee55ab1fd694fc68f8e98819e96e862999b5d442
3
+ size 4682987904
model-00014-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dfc8befb7da9721cb8528fad81e012ab89b2828fede9a283a7ed20a4332e9746
3
+ size 4682987904
model-00015-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:22015ca4df121eb8cfc5acd01354fc369fcf947f51b27652b6d04bba9cb9696b
3
+ size 4682987904
model-00016-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:780a49d1d3e54e7b37b45b9efc0d02eec3140cefe4845bbd077d2c4a71039c59
3
+ size 4682987904
model-00017-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0be7537161cee75fbff97df03d9e585104f976b31fb73a098f8975b33b3111c
3
+ size 4682987904
model-00018-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5528044fab3e165d495b05dfe2076fbaf06457083eecf508b28cee9b3b4ed5f1
3
+ size 4682987904
model-00019-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3f750c39a564577349c7705ec95cd4aeb73624d100df86ad95f7b8b350c2e660
3
+ size 4682987904
model-00020-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ce649f5393c57a81707a26439f82d89db5df2bb0e4ffc7b2874192e7a0735c1
3
+ size 4682987904
model-00021-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c67d951bdc8567d6e03d2ca9cd69779a96afeb090edb8e257d78fcd0ffcb33e5
3
+ size 4682987904
model-00022-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d1ba8741ebe985e32c17ef26e06526223d306d73ac15f7a6600c24a22e9111ac
3
+ size 4682987904
model-00023-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d7de865761fae8c7e3c92fc7e7af3e7ba14a8846c4a97944e410181fc451d2d7
3
+ size 4682987904
model-00024-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3d1f833392d772aab9f079972ace909b94e28c5c895668f153e35bb9eb100fe8
3
+ size 4682987904
model-00025-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9b734d236f3f095c50c1d1ac72dc8d02ab6a6fd1e90220663a23ea39c1ae591f
3
+ size 4682987904
model-00026-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:282e19852b5b56b728be3aee8b8f41d516347c4f55722eefa59ac7dc270adfe2
3
+ size 4682987904
model-00027-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ca59588d218b55bb4c7cb765788c668a05f1a7acaa8eb50b1864c915b004f96
3
+ size 4682987904
model-00028-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28c33a4dce7f4bab03c1816cb0548e3dd18dafd71a990837b5e943649da04b91
3
+ size 4682987904
model-00029-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f10451b115af4d6c831155d8abce79905c06f5bd42db9551224d2260c2573c37
3
+ size 4682987904
model-00030-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07c29107055802d27415e2220abf4ac727202bc3abdf8e607c75b4603e8185c2
3
+ size 4682987904
model-00031-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d3b8c0fabd37def5075c9cc4510a78f181ae12a4b15c0ea0d9b0907ca8512fce
3
+ size 4682987904
model-00032-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9ec8480d163b94b1f321309a505ac1d3ea48b3f1fa86ba9ad7df0b3c9112ef36
3
+ size 4682987904
model-00033-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8519f57ab1fbd15324f8939503c61328ae8a60940e892192ca19a927f85bb7ea
3
+ size 4682987904
model-00034-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c8076078d3ccdbdec4d375ad01e65b8c741b35e83bf772f52667b5facf492d3
3
+ size 4682987904
model-00035-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d64eca561f4471bdb4438150f7a67b147d6acdc1b028cc0b8770ea979c390136
3
+ size 4682987904
model-00036-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79bbc64f7704ff01f30bb3b9038c5a23bb9d068a44577616db7e8854faee8dd3
3
+ size 4682987904
model-00037-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af65b309aa1d7e7e9bec62a56dd40544b19e595eae1071510a66817ff5c2bbef
3
+ size 4682987904
model-00038-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1607ec1d64f4b46a26770af361e5be819f919d2aab0b0f4f82fac3753f3ff0a
3
+ size 4682987904
model-00039-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca51aec4dc1b1b456ee324edfa7f4b51c0f74fa80c6458111f6523408adb5dcc
3
+ size 4682987904
model-00040-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fcea05440633d1a87c820d1f31c95bd80b372d6937560422153a224dcc966738
3
+ size 4682987904
model-00041-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:16d275875245d7c920608dba86e718c3d351a92e4a02d48973647748087e7f75
3
+ size 4682987904
model-00042-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:37547c91b22403a1044f3cfc7dc227aae89d483b8103292da9038ace6d50c539
3
+ size 4682987904
model-00043-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a326957d379158303e37b2ea9f51db985fd0f56d3b5d5343736ad20d943bc84d
3
+ size 4682987904
model-00044-of-00046.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:46ea1d2d23665d8be7493be796b10b3fd2b9ae6193c935f8fd4f6f6489733c4e
3
+ size 4682987904