mark20250409 commited on
Commit
808215e
·
verified ·
1 Parent(s): e724a4e

Upload configuration_deepseek.py with huggingface_hub

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