inferencerlabs commited on
Commit
6478e41
·
1 Parent(s): ebb0ff7

Upload complete model

Browse files
Files changed (1) hide show
  1. configuration_solar_open.py +242 -0
configuration_solar_open.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 Upstage AI.
3
+ # Copyright 2025 The ZhipuAI Inc. team and HuggingFace Inc. team.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ # This file has been modified by Upstage AI including
18
+ # - Hyperparameter Adjustments: Modified the model architecture by increasing vocab_size and num_hidden_layers, while decreasing num_attention_heads, intermediate_size, and moe_intermediate_size.
19
+ # RoPE Configuration: Replaced the generic rope_parameters argument with explicit rope_theta and rope_scaling parameters to define Rotary Positional Embeddings settings.
20
+ #
21
+ # Based on code from: https://github.com/huggingface/transformers/blob/main/src/transformers/models/glm4_moe/configuration_glm4_moe.py
22
+
23
+ from transformers.configuration_utils import PretrainedConfig
24
+ from transformers.modeling_rope_utils import rope_config_validation
25
+
26
+
27
+ class SolarOpenConfig(PretrainedConfig):
28
+ r"""
29
+ This is the configuration class to store the configuration of a [`SolarOpenModel`]. It is used to instantiate a
30
+ SolarOpen model according to the specified arguments, defining the model architecture.
31
+
32
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
33
+ documentation from [`PretrainedConfig`] for more information.
34
+
35
+
36
+ Args:
37
+ vocab_size (`int`, *optional*, defaults to 196608):
38
+ Vocabulary size of the SolarOpen model. Defines the number of different tokens that can be represented by the
39
+ `inputs_ids` passed when calling [`SolarOpenModel`]
40
+ hidden_size (`int`, *optional*, defaults to 4096):
41
+ Dimension of the hidden representations.
42
+ intermediate_size (`int`, *optional*, defaults to 10240):
43
+ Dimension of the MLP representations.
44
+ num_hidden_layers (`int`, *optional*, defaults to 48):
45
+ Number of hidden layers in the Transformer encoder.
46
+ num_attention_heads (`int`, *optional*, defaults to 64):
47
+ Number of attention heads for each attention layer in the Transformer encoder.
48
+ partial_rotary_factor (`float`, *optional*, defaults to 1.0):
49
+ The factor of the partial rotary position.
50
+ num_key_value_heads (`int`, *optional*, defaults to 8):
51
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
52
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
53
+ `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
54
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
55
+ by meanpooling all the original heads within that group. For more details, check out [this
56
+ paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `32`.
57
+
58
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
59
+ The non-linear activation function (function or string) in the decoder.
60
+ max_position_embeddings (`int`, *optional*, defaults to 131072):
61
+ The maximum sequence length that this model might ever be used with.
62
+ initializer_range (`float`, *optional*, defaults to 0.02):
63
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
64
+ rms_norm_eps (`float`, *optional*, defaults to 1e-05):
65
+ The epsilon used by the rms normalization layers.
66
+ use_cache (`bool`, *optional*, defaults to `True`):
67
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
68
+ relevant if `config.is_decoder=True`.
69
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
70
+ Whether the model's input and output word embeddings should be tied.
71
+ rope_theta (`float`, *optional*, defaults to 1000000.0):
72
+ The base period of the RoPE embeddings.
73
+ rope_scaling (`Dict`, *optional*):
74
+ Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
75
+ and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
76
+ accordingly.
77
+ Expected contents:
78
+ `rope_type` (`str`):
79
+ The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
80
+ 'llama3'], with 'default' being the original RoPE implementation.
81
+ `factor` (`float`, *optional*):
82
+ Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
83
+ most scaling types, a `factor` of x will enable the model to handle sequences of length x *
84
+ original maximum pre-trained length.
85
+ `original_max_position_embeddings` (`int`, *optional*):
86
+ Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
87
+ pretraining.
88
+ `attention_factor` (`float`, *optional*):
89
+ Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
90
+ computation. If unspecified, it defaults to value recommended by the implementation, using the
91
+ `factor` field to infer the suggested value.
92
+ `beta_fast` (`float`, *optional*):
93
+ Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
94
+ ramp function. If unspecified, it defaults to 32.
95
+ `beta_slow` (`float`, *optional*):
96
+ Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
97
+ ramp function. If unspecified, it defaults to 1.
98
+ `short_factor` (`list[float]`, *optional*):
99
+ Only used with 'longrope'. The scaling factor to be applied to short contexts (<
100
+ `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
101
+ size divided by the number of attention heads divided by 2
102
+ `long_factor` (`list[float]`, *optional*):
103
+ Only used with 'longrope'. The scaling factor to be applied to long contexts (<
104
+ `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
105
+ size divided by the number of attention heads divided by 2
106
+ `low_freq_factor` (`float`, *optional*):
107
+ Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
108
+ `high_freq_factor` (`float`, *optional*):
109
+ Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
110
+ attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
111
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
112
+ attention_dropout (`float`, *optional*, defaults to 0.0):
113
+ The dropout ratio for the attention probabilities.
114
+ moe_intermediate_size (`int`, *optional*, defaults to 1280):
115
+ Intermediate size of the routed expert.
116
+ num_experts_per_tok (`int`, *optional*, defaults to 8):
117
+ number of experts per token.
118
+ n_shared_experts (`int`, *optional*, defaults to 1):
119
+ Number of shared experts.
120
+ n_routed_experts (`int`, *optional*, defaults to 128):
121
+ Number of routed experts.
122
+ routed_scaling_factor (`float`, *optional*, defaults to 1.0):
123
+ Scaling factor or routed experts.
124
+ n_group (`int`, *optional*, defaults to 1):
125
+ Number of groups for routed experts.
126
+ topk_group (`int`, *optional*, defaults to 1):
127
+ Number of selected groups for each token(for each token, ensuring the selected experts is only within `topk_group` groups).
128
+ first_k_dense_replace (`int`, *optional*, defaults to 0):
129
+ Number of dense layers in shallow layers(embed->dense->dense->...->dense->moe->moe...->lm_head).
130
+ \--k dense layers--/
131
+ norm_topk_prob (`bool`, *optional*, defaults to `True`):
132
+ Whether to normalize the topk probabilities.
133
+ use_qk_norm (`bool`, *optional*, defaults to `False`):
134
+ Whether to use query-key normalization in the attention
135
+ ```python
136
+ >>> from transformers import SolarOpenModel, SolarOpenConfig
137
+
138
+ >>> # Initializing a SolarOpen style configuration
139
+ >>> configuration = SolarOpenConfig()
140
+
141
+ >>> # Initializing a model from the SolarOpen style configuration
142
+ >>> model = SolarOpenModel(configuration)
143
+
144
+ >>> # Accessing the model configuration
145
+ >>> configuration = model.config
146
+ ```"""
147
+
148
+ model_type = "solar_open"
149
+ keys_to_ignore_at_inference = ["past_key_values"]
150
+
151
+ # Default tensor parallel plan for base model `SolarOpen`
152
+ base_model_tp_plan = {
153
+ "layers.*.self_attn.q_proj": "colwise",
154
+ "layers.*.self_attn.k_proj": "colwise",
155
+ "layers.*.self_attn.v_proj": "colwise",
156
+ "layers.*.self_attn.o_proj": "rowwise",
157
+ "layers.*.mlp.experts.*.gate_proj": "colwise",
158
+ "layers.*.mlp.experts.*.up_proj": "colwise",
159
+ "layers.*.mlp.experts.*.down_proj": "rowwise",
160
+ "layers.*.mlp.gate_proj": "colwise",
161
+ "layers.*.mlp.up_proj": "colwise",
162
+ "layers.*.mlp.down_proj": "rowwise",
163
+ }
164
+ base_model_pp_plan = {
165
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
166
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
167
+ "norm": (["hidden_states"], ["hidden_states"]),
168
+ }
169
+
170
+ def __init__(
171
+ self,
172
+ vocab_size=196608,
173
+ hidden_size=4096,
174
+ intermediate_size=10240,
175
+ num_hidden_layers=48,
176
+ num_attention_heads=64,
177
+ partial_rotary_factor=1.0,
178
+ num_key_value_heads=8,
179
+ hidden_act="silu",
180
+ max_position_embeddings=131072,
181
+ initializer_range=0.02,
182
+ rms_norm_eps=1e-5,
183
+ use_cache=True,
184
+ tie_word_embeddings=False,
185
+ rope_theta=1000000.0,
186
+ rope_scaling=None,
187
+ attention_bias=False,
188
+ attention_dropout=0.0,
189
+ moe_intermediate_size=1280,
190
+ num_experts_per_tok=8,
191
+ n_shared_experts=1,
192
+ n_routed_experts=128,
193
+ routed_scaling_factor=1.0,
194
+ n_group=1,
195
+ topk_group=1,
196
+ first_k_dense_replace=0,
197
+ norm_topk_prob=True,
198
+ use_qk_norm=False,
199
+ **kwargs,
200
+ ):
201
+ self.vocab_size = vocab_size
202
+ self.max_position_embeddings = max_position_embeddings
203
+ self.hidden_size = hidden_size
204
+ self.intermediate_size = intermediate_size
205
+ self.num_hidden_layers = num_hidden_layers
206
+ self.num_attention_heads = num_attention_heads
207
+ self.partial_rotary_factor = partial_rotary_factor
208
+
209
+ self.num_key_value_heads = num_key_value_heads
210
+ self.hidden_act = hidden_act
211
+ self.initializer_range = initializer_range
212
+ self.rms_norm_eps = rms_norm_eps
213
+ self.use_cache = use_cache
214
+ self.rope_theta = rope_theta
215
+ self.rope_scaling = rope_scaling
216
+ self.attention_bias = attention_bias
217
+ self.attention_dropout = attention_dropout
218
+ # Validate the correctness of rotary position embeddings parameters
219
+ # BC: if there is a 'type' field, move it to 'rope_type'.
220
+ if self.rope_scaling is not None and "type" in self.rope_scaling:
221
+ self.rope_scaling["rope_type"] = self.rope_scaling["type"]
222
+ rope_config_validation(self)
223
+
224
+ # MoE arguments
225
+ self.moe_intermediate_size = moe_intermediate_size
226
+ self.num_experts_per_tok = num_experts_per_tok
227
+ self.n_group = n_group
228
+ self.topk_group = topk_group
229
+ self.n_shared_experts = n_shared_experts
230
+ self.n_routed_experts = n_routed_experts
231
+ self.routed_scaling_factor = routed_scaling_factor
232
+ self.first_k_dense_replace = first_k_dense_replace
233
+ self.norm_topk_prob = norm_topk_prob
234
+ self.use_qk_norm = use_qk_norm
235
+
236
+ super().__init__(
237
+ tie_word_embeddings=tie_word_embeddings,
238
+ **kwargs,
239
+ )
240
+
241
+
242
+ __all__ = ["SolarOpenConfig"]