thehekimoghlu commited on
Commit
1c12a9a
·
verified ·
1 Parent(s): 39e360f

Upload configuration_longcat_flash.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. configuration_longcat_flash.py +216 -0
configuration_longcat_flash.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ """LongcatFlash model configuration"""
3
+
4
+ from transformers.configuration_utils import PretrainedConfig
5
+ from transformers.modeling_rope_utils import rope_config_validation
6
+
7
+
8
+ LONGCAT_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
9
+
10
+
11
+ class LongcatFlashConfig(PretrainedConfig):
12
+ r"""
13
+ This is the configuration class to store the configuration of a [`LongcatFlashModel`]. It is used to instantiate an LongcatFlash
14
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
15
+ defaults will yield a similar configuration to that of the LongcatFlash.
16
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
17
+ documentation from [`PretrainedConfig`] for more information.
18
+
19
+
20
+ Args:
21
+ vocab_size (`int`, *optional*, defaults to 131072):
22
+ Vocabulary size of the Deep model. Defines the number of different tokens that can be represented by the
23
+ `inputs_ids` passed when calling [`LongcatFlashModel`]
24
+ hidden_size (`int`, *optional*, defaults to 7168):
25
+ Dimension of the hidden representations.
26
+ ffn_hidden_size (`int`, *optional*, defaults to 18432):
27
+ Dimension of the MLP representations.
28
+ expert_ffn_hidden_size (`int`, *optional*, defaults to 2048):
29
+ Dimension of the MoE representations.
30
+ num_layers (`int`, *optional*, defaults to 61):
31
+ Number of hidden layers in the Transformer decoder.
32
+ num_attention_heads (`int`, *optional*, defaults to 128):
33
+ Number of attention heads for each attention layer in the Transformer decoder.
34
+ num_key_value_heads (`int`, *optional*, defaults to 128):
35
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
36
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
37
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
38
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
39
+ by meanpooling all the original heads within that group. For more details checkout [this
40
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
41
+ `num_attention_heads`.
42
+ n_routed_experts (`int`, *optional*, defaults to 256):
43
+ Number of routed experts.
44
+ routed_scaling_factor (`float`, *optional*, defaults to 2.5):
45
+ Scaling factor or routed experts.
46
+ kv_lora_rank (`int`, *optional*, defaults to 512):
47
+ Rank of the LoRA matrices for key and value projections.
48
+ q_lora_rank (`int`, *optional*, defaults to 1536):
49
+ Rank of the LoRA matrices for query projections.
50
+ qk_rope_head_dim (`int`, *optional*, defaults to 64):
51
+ Dimension of the query/key heads that use rotary position embeddings.
52
+ v_head_dim (`int`, *optional*, defaults to 128):
53
+ Dimension of the value heads.
54
+ qk_nope_head_dim (`int`, *optional*, defaults to 128):
55
+ Dimension of the query/key heads that don't use rotary position embeddings.
56
+ norm_topk_prob (`bool`, *optional*, defaults to `True`):
57
+ Whether to normalize the weights of the routed experts.
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 4096):
61
+ The maximum sequence length that this model might ever be used with.
62
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
63
+ The epsilon used by the rms normalization layers.
64
+ use_cache (`bool`, *optional*, defaults to `True`):
65
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
66
+ relevant if `config.is_decoder=True`.
67
+ pad_token_id (`int`, *optional*):
68
+ Padding token id.
69
+ bos_token_id (`int`, *optional*, defaults to 0):
70
+ Beginning of stream token id.
71
+ eos_token_id (`int`, *optional*, defaults to 1):
72
+ End of stream token id.
73
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
74
+ Whether to tie weight embeddings
75
+ rope_theta (`float`, *optional*, defaults to 10000.0):
76
+ The base period of the RoPE embeddings.
77
+ attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
78
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
79
+ attention_dropout (`float`, *optional*, defaults to 0.0):
80
+ The dropout ratio for the attention probabilities.
81
+ attention_method (`str`, *optional*, defaults to `"MLA"`):
82
+ The attention method to use.
83
+ initializer_range (`float`, *optional*, defaults to 0.006):
84
+ The initializer range for the model.
85
+ router_bias (`bool`, *optional*, defaults to `False`):
86
+ Whether to use a bias in the router.
87
+ zero_expert_num (`int`, *optional*, defaults to `None`):
88
+ The number of zero experts to use.
89
+ zero_expert_type (`str`, *optional*, defaults to `None`):
90
+ The type of zero expert to use.
91
+
92
+ ```python
93
+ >>> from transformers import LongcatFlashModel, LongcatFlashConfig
94
+
95
+ >>> # Initializing a LongcatFlash style configuration
96
+ >>> configuration = LongcatFlashConfig()
97
+
98
+ >>> # Accessing the model configuration
99
+ >>> configuration = model.config
100
+ ```"""
101
+
102
+ model_type = "longcat_flash"
103
+ keys_to_ignore_at_inference = ["past_key_values"]
104
+ base_model_tp_plan = {
105
+ "layers.*.self_attn.k_proj": "colwise",
106
+ "layers.*.self_attn.v_proj": "colwise",
107
+ "layers.*.self_attn.o_proj": "rowwise",
108
+ "layers.*.mlp.experts.*.gate_proj": "local_colwise",
109
+ "layers.*.mlp.experts.*.up_proj": "local_colwise",
110
+ "layers.*.mlp.experts.*.down_proj": "local_rowwise",
111
+ "layers.*.mlps.*.gate_proj": "local_colwise",
112
+ "layers.*.mlps.*.up_proj": "local_colwise",
113
+ "layers.*.mlps.*.down_proj": "local_rowwise",
114
+ }
115
+ base_model_pp_plan = {
116
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
117
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
118
+ "norm": (["hidden_states"], ["hidden_states"]),
119
+ }
120
+
121
+ def __init__(
122
+ self,
123
+ vocab_size=131072,
124
+ hidden_size=7168,
125
+ ffn_hidden_size=18432,
126
+ expert_ffn_hidden_size=2048,
127
+ num_layers=61,
128
+ num_attention_heads=128,
129
+ num_key_value_heads=None,
130
+ n_routed_experts=256,
131
+ routed_scaling_factor=1,
132
+ kv_lora_rank=512,
133
+ q_lora_rank=1536,
134
+ qk_rope_head_dim=64,
135
+ v_head_dim=128,
136
+ qk_nope_head_dim=128,
137
+ mla_scale_q_lora=True,
138
+ mla_scale_kv_lora=True,
139
+ moe_topk=8,
140
+ norm_topk_prob=False,
141
+ hidden_act="silu",
142
+ max_position_embeddings=4096,
143
+ rms_norm_eps=1e-6,
144
+ use_cache=True,
145
+ pad_token_id=None,
146
+ bos_token_id=0,
147
+ eos_token_id=1,
148
+ tie_word_embeddings=False,
149
+ rope_theta=10000.0,
150
+ attention_bias=False,
151
+ attention_dropout=0.0,
152
+ attention_method='MLA',
153
+ initializer_range=0.006,
154
+ router_bias=False,
155
+ zero_expert_num=None,
156
+ zero_expert_type=None,
157
+ **kwargs,
158
+ ):
159
+ self.vocab_size = vocab_size
160
+ self.max_position_embeddings = max_position_embeddings
161
+ self.hidden_size = hidden_size
162
+ self.ffn_hidden_size = ffn_hidden_size
163
+ self.expert_ffn_hidden_size = expert_ffn_hidden_size
164
+ self.num_layers = num_layers
165
+ self.num_attention_heads = num_attention_heads
166
+ self.n_routed_experts = n_routed_experts
167
+ self.routed_scaling_factor = routed_scaling_factor
168
+ self.kv_lora_rank = kv_lora_rank
169
+ self.q_lora_rank = q_lora_rank
170
+ self.qk_rope_head_dim = qk_rope_head_dim
171
+ self.v_head_dim = v_head_dim
172
+ self.qk_nope_head_dim = qk_nope_head_dim
173
+ self.qk_head_dim = qk_nope_head_dim + qk_rope_head_dim
174
+ self.moe_topk = moe_topk
175
+ self.norm_topk_prob = norm_topk_prob
176
+ self.mla_scale_q_lora = mla_scale_q_lora
177
+ self.mla_scale_kv_lora = mla_scale_kv_lora
178
+ self.attention_method = attention_method
179
+ self.initializer_range = initializer_range
180
+ self.router_bias = router_bias
181
+ self.zero_expert_num = zero_expert_num
182
+ self.zero_expert_type = zero_expert_type
183
+
184
+ if self.attention_method == "MLA":
185
+ self.head_dim = qk_rope_head_dim
186
+ else:
187
+ ValueError('attention_method should be one of ["MLA"]')
188
+
189
+
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.rms_norm_eps = rms_norm_eps
196
+ self.use_cache = use_cache
197
+ self.rope_theta = rope_theta
198
+ self.attention_bias = attention_bias
199
+ self.attention_dropout = attention_dropout
200
+
201
+ rope_config_validation(self)
202
+
203
+ super().__init__(
204
+ pad_token_id=pad_token_id,
205
+ bos_token_id=bos_token_id,
206
+ eos_token_id=eos_token_id,
207
+ tie_word_embeddings=tie_word_embeddings,
208
+ **kwargs,
209
+ )
210
+
211
+ @property
212
+ def num_hidden_layers(self):
213
+ return self.num_layers
214
+
215
+
216
+ __all__ = ["LongcatFlashConfig"]