Prompt48 commited on
Commit
f8e10fc
·
verified ·
1 Parent(s): 4886785

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\transformers\models\granitemoehybrid\configuration_granitemoehybrid.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//transformers//models//granitemoehybrid//configuration_granitemoehybrid.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 IBM and the HuggingFace Inc. team. All rights reserved.
3
+ #
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
+ """GraniteMoeHybrid model configuration"""
17
+
18
+ from ...configuration_utils import PretrainedConfig
19
+ from ...modeling_rope_utils import rope_config_validation
20
+ from ...utils import logging
21
+
22
+
23
+ logger = logging.get_logger(__name__)
24
+
25
+
26
+ class GraniteMoeHybridConfig(PretrainedConfig):
27
+ r"""
28
+ This is the configuration class to store the configuration of a [`GraniteMoeHybridConfig`]. It is used to
29
+ instantiate an GraniteMoeHybrid model according to the specified arguments, defining the model architecture.
30
+
31
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
32
+ documentation from [`PretrainedConfig`] for more information.
33
+
34
+
35
+ Args:
36
+ vocab_size (`int`, *optional*, defaults to 32000):
37
+ Vocabulary size of the GraniteMoeHybrid model. Defines the number of different tokens that
38
+ can be represented by the `inputs_ids` passed when calling [`GraniteMoeHybridModel`]
39
+ hidden_size (`int`, *optional*, defaults to 4096):
40
+ Dimension of the hidden representations.
41
+ intermediate_size (`int`, *optional*, defaults to 11008):
42
+ Dimension of the MLP representations.
43
+ num_hidden_layers (`int`, *optional*, defaults to 32):
44
+ Number of hidden layers in the Transformer decoder.
45
+ num_attention_heads (`int`, *optional*, defaults to 32):
46
+ Number of attention heads for each attention layer in the Transformer decoder.
47
+ num_key_value_heads (`int`, *optional*):
48
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
49
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
50
+ `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
51
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
52
+ by meanpooling all the original heads within that group. For more details, check out [this
53
+ paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
54
+ `num_attention_heads`.
55
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
56
+ The non-linear activation function (function or string) in the decoder.
57
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
58
+ The maximum sequence length that this model might ever be used with.
59
+ initializer_range (`float`, *optional*, defaults to 0.02):
60
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
61
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
62
+ The epsilon used by the rms normalization layers.
63
+ use_cache (`bool`, *optional*, defaults to `True`):
64
+ Whether or not the model should return the last key/values attentions (not used by all models).
65
+ Only relevant if `config.is_decoder=True`.
66
+ pad_token_id (`int`, *optional*):
67
+ Padding token id.
68
+ bos_token_id (`int`, *optional*, defaults to 1):
69
+ Beginning of stream token id.
70
+ eos_token_id (`int`, *optional*, defaults to 2):
71
+ End of stream token id.
72
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
73
+ Whether to tie weight embeddings
74
+ rope_theta (`float`, *optional*, defaults to 10000.0):
75
+ The base period of the RoPE embeddings.
76
+ rope_scaling (`Dict`, *optional*):
77
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
78
+ strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
79
+ `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
80
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
81
+ these scaling strategies behave:
82
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
83
+ experimental feature, subject to breaking API changes in future versions.
84
+ attention_bias (`bool`, *optional*, defaults to `False`):
85
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
86
+ attention_dropout (`float`, *optional*, defaults to 0.0):
87
+ The dropout ratio for the attention probabilities.
88
+ embedding_multiplier (`float`, *optional*, defaults to 1.0): embedding multiplier.
89
+ logits_scaling (`float`, *optional*, defaults to 1.0): divisor for output logits.
90
+ residual_multiplier (`float`, *optional*, defaults to 1.0): residual multiplier.
91
+ attention_multiplier (`float`, *optional*, defaults to 1.0): attention multiplier.
92
+ num_local_experts (`int`, *optional*, defaults to 8): total number of experts.
93
+ num_experts_per_tok (`int`, *optional*, defaults to 2): number of experts per token.
94
+ output_router_logits (`bool`, *optional*, defaults to `False`):
95
+ Whether or not the router logits should be returned by the model. Enabling this will also
96
+ allow the model to output the auxiliary loss.
97
+ router_aux_loss_coef (`float`, *optional*, defaults to 0.001): router auxiliary loss coefficient
98
+ shared_intermediate_size (`int`, *optional*, defaults to 1024): intermediate size for shared experts.
99
+ position_embedding_type (`str`, *optional*): Positional embedding
100
+ type to be used; defaults to None. Allowed options: `[None, "rope"]`
101
+ layer_types (`List`, *optional*): list of strings to be used as layer types.
102
+ Allowed choices: "mamba", "attention".
103
+ mamba_n_heads (`int`, *optional*, defaults to 128):
104
+ The number of mamba heads used.
105
+ mamba_n_groups (`int`, *optional*, defaults to 1):
106
+ The number of the mamba groups used.
107
+ mamba_d_state (`int`, *optional*, defaults to 256):
108
+ The dimension the mamba latent state space.
109
+ mamba_d_head (`int`, *optional*, defaults to `"auto"`):
110
+ Head embedding dimension size.
111
+ mamba_d_conv (`int`, *optional*, defaults to 4):
112
+ The size of the mamba convolution kernel.
113
+ mamba_expand (`int`, *optional*, defaults to 2):
114
+ Expanding factor (relative to hidden_size) used to determine the mamba intermediate size.
115
+ mamba_chunk_size (`int`, *optional*, defaults to 256):
116
+ The chunks in which to break the sequence when doing prefill/training.
117
+ mamba_conv_bias (`bool`, *optional*, defaults to `True`):
118
+ Flag indicating whether or not to use bias in the convolution layer of the mamba mixer block.
119
+ mamba_proj_bias (`bool`, *optional*, defaults to `False`):
120
+ Flag indicating whether or not to use bias in the input and output projections (["in_proj", "out_proj"])
121
+ of the mamba mixer block.
122
+ ```python
123
+ >>> from transformers import GraniteMoeHybridModel, GraniteMoeHybridConfig
124
+
125
+ >>> # Initializing a GraniteMoeHybrid config
126
+ >>> configuration = GraniteMoeHybridConfig()
127
+
128
+
129
+ >>> # Accessing the model configuration
130
+ >>> configuration = model.config
131
+ ```"""
132
+
133
+ model_type = "granitemoehybrid"
134
+ attribute_map = {
135
+ "layers_block_type": "layer_types",
136
+ }
137
+ keys_to_ignore_at_inference = ["past_key_values"]
138
+
139
+ def __init__(
140
+ self,
141
+ vocab_size=32000,
142
+ hidden_size=4096,
143
+ intermediate_size=11008,
144
+ num_hidden_layers=32,
145
+ num_attention_heads=32,
146
+ num_key_value_heads=None,
147
+ hidden_act="silu",
148
+ max_position_embeddings=2048,
149
+ initializer_range=0.02,
150
+ rms_norm_eps=1e-6,
151
+ use_cache=True,
152
+ pad_token_id=None,
153
+ bos_token_id=1,
154
+ eos_token_id=2,
155
+ tie_word_embeddings=False,
156
+ rope_theta=10000.0,
157
+ rope_scaling=None,
158
+ attention_bias=False,
159
+ attention_dropout=0.0,
160
+ embedding_multiplier=1.0,
161
+ logits_scaling=1.0,
162
+ residual_multiplier=1.0,
163
+ attention_multiplier=1.0,
164
+ num_local_experts=8,
165
+ num_experts_per_tok=2,
166
+ output_router_logits=False,
167
+ router_aux_loss_coef=0.001,
168
+ shared_intermediate_size=1024,
169
+ position_embedding_type=None,
170
+ layer_types=None,
171
+ mamba_n_heads=128,
172
+ mamba_n_groups=1,
173
+ mamba_d_state=256,
174
+ mamba_d_head="auto",
175
+ mamba_d_conv=4,
176
+ mamba_expand=2,
177
+ mamba_chunk_size=256,
178
+ mamba_conv_bias=True,
179
+ mamba_proj_bias=False,
180
+ **kwargs,
181
+ ):
182
+ self.vocab_size = vocab_size
183
+ self.max_position_embeddings = max_position_embeddings
184
+ self.hidden_size = hidden_size
185
+ self.intermediate_size = intermediate_size
186
+ self.num_hidden_layers = num_hidden_layers
187
+ self.num_attention_heads = num_attention_heads
188
+
189
+ # for backward compatibility
190
+ if num_key_value_heads is None:
191
+ num_key_value_heads = num_attention_heads
192
+
193
+ self.num_key_value_heads = num_key_value_heads
194
+ self.hidden_act = hidden_act
195
+ self.initializer_range = initializer_range
196
+ self.rms_norm_eps = rms_norm_eps
197
+ self.use_cache = use_cache
198
+ self.rope_theta = rope_theta
199
+ self.rope_scaling = rope_scaling
200
+ self.attention_bias = attention_bias
201
+ self.embedding_multiplier = embedding_multiplier
202
+ self.logits_scaling = logits_scaling
203
+ self.residual_multiplier = residual_multiplier
204
+ self.attention_multiplier = attention_multiplier
205
+ self.attention_dropout = attention_dropout
206
+ self.num_local_experts = num_local_experts
207
+ self.num_experts_per_tok = num_experts_per_tok
208
+ self.output_router_logits = output_router_logits
209
+ self.router_aux_loss_coef = router_aux_loss_coef
210
+ self.shared_intermediate_size = shared_intermediate_size
211
+ self.position_embedding_type = position_embedding_type
212
+
213
+ mamba_intermediate = mamba_expand * hidden_size
214
+
215
+ if layer_types is not None and any(layer_type not in ["mamba", "attention"] for layer_type in layer_types):
216
+ raise ValueError("layer_types must be a list strings in [`mamba` `attention`]")
217
+
218
+ if mamba_intermediate % mamba_n_heads != 0:
219
+ raise ValueError("mamba_n_heads must divide mamba_expand * hidden_size")
220
+
221
+ # for the mamba_v2, must satisfy the following
222
+ if mamba_d_head == "auto":
223
+ mamba_d_head = mamba_intermediate // mamba_n_heads
224
+
225
+ if mamba_d_head * mamba_n_heads != mamba_intermediate:
226
+ raise ValueError("The dimensions for the Mamba head state do not match the model intermediate_size")
227
+
228
+ self.mamba_n_heads = mamba_n_heads
229
+ self.mamba_d_head = mamba_d_head
230
+ self.mamba_n_groups = mamba_n_groups
231
+ self.mamba_d_state = mamba_d_state
232
+ self.mamba_d_conv = mamba_d_conv
233
+ self.mamba_chunk_size = mamba_chunk_size
234
+ self.mamba_conv_bias = mamba_conv_bias
235
+ self.mamba_proj_bias = mamba_proj_bias
236
+ self.mamba_expand = mamba_expand
237
+ self.layer_types = layer_types
238
+
239
+ super().__init__(
240
+ pad_token_id=pad_token_id,
241
+ bos_token_id=bos_token_id,
242
+ eos_token_id=eos_token_id,
243
+ tie_word_embeddings=tie_word_embeddings,
244
+ **kwargs,
245
+ )
246
+
247
+ if self.position_embedding_type == "rope":
248
+ rope_config_validation(self)
249
+
250
+ # overwrite the function to use in `HybridMambaAttentionDynamicCache`
251
+ @property
252
+ def layers_block_type(self):
253
+ return self.layer_types if self.layer_types else ["mamba"] * self.num_hidden_layers
254
+
255
+
256
+ __all__ = ["GraniteMoeHybridConfig"]