zhiyucheng commited on
Commit
9ed4126
·
1 Parent(s): 9e1371e

add artifact

Browse files
Files changed (1) hide show
  1. configuration_deepseek.py +225 -0
configuration_deepseek.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
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.utils import logging
18
+
19
+ logger = logging.get_logger(__name__)
20
+
21
+ DEEPSEEK_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
22
+ class DeepseekV3Config(PretrainedConfig):
23
+ r"""
24
+ This is the configuration class to store the configuration of a [`DeepseekV3Model`]. It is used to instantiate an DeepSeek
25
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
26
+ defaults will yield a similar configuration to that of the DeepSeek-V3.
27
+
28
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
29
+ documentation from [`PretrainedConfig`] for more information.
30
+
31
+
32
+ Args:
33
+ vocab_size (`int`, *optional*, defaults to 129280):
34
+ Vocabulary size of the Deep model. Defines the number of different tokens that can be represented by the
35
+ `inputs_ids` passed when calling [`DeepseekV3Model`]
36
+ hidden_size (`int`, *optional*, defaults to 4096):
37
+ Dimension of the hidden representations.
38
+ intermediate_size (`int`, *optional*, defaults to 11008):
39
+ Dimension of the MLP representations.
40
+ moe_intermediate_size (`int`, *optional*, defaults to 1407):
41
+ Dimension of the MoE representations.
42
+ num_hidden_layers (`int`, *optional*, defaults to 32):
43
+ Number of hidden layers in the Transformer decoder.
44
+ num_nextn_predict_layers (`int`, *optional*, defaults to 1):
45
+ Number of nextn predict layers in the DeepSeekV3 Model.
46
+ num_attention_heads (`int`, *optional*, defaults to 32):
47
+ Number of attention heads for each attention layer in the Transformer decoder.
48
+ n_shared_experts (`int`, *optional*, defaults to None):
49
+ Number of shared experts, None means dense model.
50
+ n_routed_experts (`int`, *optional*, defaults to None):
51
+ Number of routed experts, None means dense model.
52
+ routed_scaling_factor (`float`, *optional*, defaults to 1.0):
53
+ Scaling factor or routed experts.
54
+ topk_method (`str`, *optional*, defaults to `gready`):
55
+ Topk method used in routed gate.
56
+ n_group (`int`, *optional*, defaults to None):
57
+ Number of groups for routed experts.
58
+ topk_group (`int`, *optional*, defaults to None):
59
+ Number of selected groups for each token(for each token, ensuring the selected experts is only within `topk_group` groups).
60
+ num_experts_per_tok (`int`, *optional*, defaults to None):
61
+ Number of selected experts, None means dense model.
62
+ moe_layer_freq (`int`, *optional*, defaults to 1):
63
+ The frequency of the MoE layer: one expert layer for every `moe_layer_freq - 1` dense layers.
64
+ first_k_dense_replace (`int`, *optional*, defaults to 0):
65
+ Number of dense layers in shallow layers(embed->dense->dense->...->dense->moe->moe...->lm_head).
66
+ \--k dense layers--/
67
+ norm_topk_prob (`bool`, *optional*, defaults to False):
68
+ Whether to normalize the weights of the routed experts.
69
+ scoring_func (`str`, *optional*, defaults to 'softmax'):
70
+ Method of computing expert weights.
71
+ aux_loss_alpha (`float`, *optional*, defaults to 0.001):
72
+ Auxiliary loss weight coefficient.
73
+ seq_aux = (`bool`, *optional*, defaults to True):
74
+ Whether to compute the auxiliary loss for each individual sample.
75
+ num_key_value_heads (`int`, *optional*):
76
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
77
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
78
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
79
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
80
+ by meanpooling all the original heads within that group. For more details checkout [this
81
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
82
+ `num_attention_heads`.
83
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
84
+ The non-linear activation function (function or string) in the decoder.
85
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
86
+ The maximum sequence length that this model might ever be used with.
87
+ initializer_range (`float`, *optional*, defaults to 0.02):
88
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
89
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
90
+ The epsilon used by the rms normalization layers.
91
+ use_cache (`bool`, *optional*, defaults to `True`):
92
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
93
+ relevant if `config.is_decoder=True`.
94
+ pad_token_id (`int`, *optional*):
95
+ Padding token id.
96
+ bos_token_id (`int`, *optional*, defaults to 1):
97
+ Beginning of stream token id.
98
+ eos_token_id (`int`, *optional*, defaults to 2):
99
+ End of stream token id.
100
+ pretraining_tp (`int`, *optional*, defaults to 1):
101
+ Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
102
+ document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
103
+ necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
104
+ issue](https://github.com/pytorch/pytorch/issues/76232).
105
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
106
+ Whether to tie weight embeddings
107
+ rope_theta (`float`, *optional*, defaults to 10000.0):
108
+ The base period of the RoPE embeddings.
109
+ rope_scaling (`Dict`, *optional*):
110
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
111
+ strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
112
+ `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
113
+ `max_position_embeddings` to the expected new maximum.
114
+ attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
115
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
116
+ attention_dropout (`float`, *optional*, defaults to 0.0):
117
+ The dropout ratio for the attention probabilities.
118
+
119
+ ```python
120
+ >>> from transformers import DeepseekV3Model, DeepseekV3Config
121
+
122
+ >>> # Initializing a Deepseek-V3 style configuration
123
+ >>> configuration = DeepseekV3Config()
124
+
125
+ >>> # Accessing the model configuration
126
+ >>> configuration = model.config
127
+ ```"""
128
+
129
+ model_type = "deepseek_v3"
130
+ keys_to_ignore_at_inference = ["past_key_values"]
131
+
132
+ def __init__(
133
+ self,
134
+ vocab_size=129280,
135
+ hidden_size=7168,
136
+ intermediate_size=18432,
137
+ moe_intermediate_size = 2048,
138
+ num_hidden_layers=61,
139
+ num_nextn_predict_layers=1,
140
+ num_attention_heads=128,
141
+ num_key_value_heads=128,
142
+ n_shared_experts = 1,
143
+ n_routed_experts = 256,
144
+ ep_size = 1,
145
+ routed_scaling_factor = 2.5,
146
+ kv_lora_rank = 512,
147
+ q_lora_rank = 1536,
148
+ qk_rope_head_dim = 64,
149
+ v_head_dim = 128,
150
+ qk_nope_head_dim = 128,
151
+ topk_method = 'noaux_tc',
152
+ n_group = 8,
153
+ topk_group = 4,
154
+ num_experts_per_tok = 8,
155
+ moe_layer_freq = 1,
156
+ first_k_dense_replace = 3,
157
+ norm_topk_prob = True,
158
+ scoring_func = 'sigmoid',
159
+ aux_loss_alpha = 0.001,
160
+ seq_aux = True,
161
+ hidden_act="silu",
162
+ max_position_embeddings=4096,
163
+ initializer_range=0.02,
164
+ rms_norm_eps=1e-6,
165
+ use_cache=True,
166
+ pad_token_id=None,
167
+ bos_token_id=0,
168
+ eos_token_id=1,
169
+ pretraining_tp=1,
170
+ tie_word_embeddings=False,
171
+ rope_theta=10000.0,
172
+ rope_scaling=None,
173
+ attention_bias=False,
174
+ attention_dropout=0.0,
175
+ **kwargs,
176
+ ):
177
+ self.vocab_size = vocab_size
178
+ self.max_position_embeddings = max_position_embeddings
179
+ self.hidden_size = hidden_size
180
+ self.intermediate_size = intermediate_size
181
+ self.moe_intermediate_size = moe_intermediate_size
182
+ self.num_hidden_layers = num_hidden_layers
183
+ self.num_nextn_predict_layers = num_nextn_predict_layers
184
+ self.num_attention_heads = num_attention_heads
185
+ self.n_shared_experts = n_shared_experts
186
+ self.n_routed_experts = n_routed_experts
187
+ self.ep_size = ep_size
188
+ self.routed_scaling_factor = routed_scaling_factor
189
+ self.kv_lora_rank = kv_lora_rank
190
+ self.q_lora_rank = q_lora_rank
191
+ self.qk_rope_head_dim = qk_rope_head_dim
192
+ self.v_head_dim = v_head_dim
193
+ self.qk_nope_head_dim = qk_nope_head_dim
194
+ self.topk_method = topk_method
195
+ self.n_group = n_group
196
+ self.topk_group = topk_group
197
+ self.num_experts_per_tok = num_experts_per_tok
198
+ self.moe_layer_freq = moe_layer_freq
199
+ self.first_k_dense_replace = first_k_dense_replace
200
+ self.norm_topk_prob = norm_topk_prob
201
+ self.scoring_func = scoring_func
202
+ self.aux_loss_alpha = aux_loss_alpha
203
+ self.seq_aux = seq_aux
204
+ # for backward compatibility
205
+ if num_key_value_heads is None:
206
+ num_key_value_heads = num_attention_heads
207
+
208
+ self.num_key_value_heads = num_key_value_heads
209
+ self.hidden_act = hidden_act
210
+ self.initializer_range = initializer_range
211
+ self.rms_norm_eps = rms_norm_eps
212
+ self.pretraining_tp = pretraining_tp
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
+
219
+ super().__init__(
220
+ pad_token_id=pad_token_id,
221
+ bos_token_id=bos_token_id,
222
+ eos_token_id=eos_token_id,
223
+ tie_word_embeddings=tie_word_embeddings,
224
+ **kwargs,
225
+ )