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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# configuration_deepseek.py
|
| 2 |
+
|
| 3 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 4 |
+
from transformers.utils import logging
|
| 5 |
+
|
| 6 |
+
logger = logging.get_logger(__name__)
|
| 7 |
+
|
| 8 |
+
# This can remain empty if no pre-trained configs are being listed.
|
| 9 |
+
DEEPSEEK_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class DeepseekV3Config(PretrainedConfig):
|
| 13 |
+
r"""
|
| 14 |
+
Configuration class for the DeepSeek-V3 model architecture. Inherits from `PretrainedConfig`.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
vocab_size (`int`, *optional*, defaults to 129280):
|
| 18 |
+
Vocabulary size of the DeepSeek model.
|
| 19 |
+
hidden_size (`int`, *optional*, defaults to 7168):
|
| 20 |
+
Dimension of the hidden representations.
|
| 21 |
+
intermediate_size (`int`, *optional*, defaults to 18432):
|
| 22 |
+
Dimension of the MLP representations in dense layers.
|
| 23 |
+
moe_intermediate_size (`int`, *optional*, defaults to 2048):
|
| 24 |
+
Dimension of the MLP representations used by MoE experts.
|
| 25 |
+
num_hidden_layers (`int`, *optional*, defaults to 61):
|
| 26 |
+
Number of hidden layers in the Transformer decoder.
|
| 27 |
+
num_nextn_predict_layers (`int`, *optional*, defaults to 1):
|
| 28 |
+
Number of "next-n predict" layers in the DeepSeekV3 model.
|
| 29 |
+
num_attention_heads (`int`, *optional*, defaults to 128):
|
| 30 |
+
Number of attention heads per attention layer.
|
| 31 |
+
num_key_value_heads (`int`, *optional*):
|
| 32 |
+
The number of key-value heads, used for GQA or MQA. Defaults to `num_attention_heads` if `None`.
|
| 33 |
+
n_shared_experts (`int`, *optional*, defaults to 1):
|
| 34 |
+
Number of shared experts. If None, indicates no shared experts (dense model).
|
| 35 |
+
n_routed_experts (`int`, *optional*, defaults to 256):
|
| 36 |
+
Number of routed experts. If None, indicates no routed experts (dense model).
|
| 37 |
+
ep_size (`int`, *optional*, defaults to 1):
|
| 38 |
+
The world-size used for expert parallelism. Typically set to the distributed world size if MoE is distributed.
|
| 39 |
+
routed_scaling_factor (`float`, *optional*, defaults to 2.5):
|
| 40 |
+
Scaling factor for routed experts' output weights.
|
| 41 |
+
kv_lora_rank (`int`, *optional*, defaults to 512):
|
| 42 |
+
The LoRA rank for Key and Value projections.
|
| 43 |
+
q_lora_rank (`int`, *optional*, defaults to 1536):
|
| 44 |
+
The LoRA rank for Query projections.
|
| 45 |
+
qk_rope_head_dim (`int`, *optional*, defaults to 64):
|
| 46 |
+
The dimension of the "rotary-embedded" portion of the Q/K heads.
|
| 47 |
+
v_head_dim (`int`, *optional*, defaults to 128):
|
| 48 |
+
The dimension of the Value heads.
|
| 49 |
+
qk_nope_head_dim (`int`, *optional*, defaults to 128):
|
| 50 |
+
The dimension of the Q/K heads that do *not* use rotary embeddings.
|
| 51 |
+
topk_method (`str`, *optional*, defaults to "noaux_tc"):
|
| 52 |
+
The gating TopK method in MoE (e.g. "noaux_tc", "topk_gating").
|
| 53 |
+
n_group (`int`, *optional*, defaults to 8):
|
| 54 |
+
Number of groups used in gating for MoE experts.
|
| 55 |
+
topk_group (`int`, *optional*, defaults to 4):
|
| 56 |
+
Number of selected groups for each token (MoE gating).
|
| 57 |
+
num_experts_per_tok (`int`, *optional*, defaults to 8):
|
| 58 |
+
Number of selected experts per token in the MoE gating.
|
| 59 |
+
moe_layer_freq (`int`, *optional*, defaults to 1):
|
| 60 |
+
Frequency of MoE layers among the transformer layers (1 = every layer is MoE).
|
| 61 |
+
first_k_dense_replace (`int`, *optional*, defaults to 3):
|
| 62 |
+
How many initial layers remain dense before MoE layers start appearing.
|
| 63 |
+
norm_topk_prob (`bool`, *optional*, defaults to True):
|
| 64 |
+
Whether to normalize the top-k gating probabilities.
|
| 65 |
+
scoring_func (`str`, *optional*, defaults to "sigmoid"):
|
| 66 |
+
The scoring function used for gating. For instance, "sigmoid" or "softmax".
|
| 67 |
+
aux_loss_alpha (`float`, *optional*, defaults to 0.001):
|
| 68 |
+
Scaling factor for any auxiliary MoE loss (e.g. load balancing).
|
| 69 |
+
seq_aux (`bool`, *optional*, defaults to True):
|
| 70 |
+
If True, auxiliary loss is computed per sample.
|
| 71 |
+
hidden_act (`str`, *optional*, defaults to "silu"):
|
| 72 |
+
Activation function used in MLP layers.
|
| 73 |
+
max_position_embeddings (`int`, *optional*, defaults to 4096):
|
| 74 |
+
Maximum sequence length the model can handle.
|
| 75 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 76 |
+
Standard deviation of the truncated normal initializer.
|
| 77 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-6):
|
| 78 |
+
Epsilon for RMS norm layers.
|
| 79 |
+
use_cache (`bool`, *optional*, defaults to True):
|
| 80 |
+
Whether the model returns `past_key_values`.
|
| 81 |
+
pad_token_id (`int`, *optional*):
|
| 82 |
+
Padding token id. If `None`, the model does not use a special padding token.
|
| 83 |
+
bos_token_id (`int`, *optional*, defaults to 0):
|
| 84 |
+
Beginning-of-sequence token id.
|
| 85 |
+
eos_token_id (`int`, *optional*, defaults to 1):
|
| 86 |
+
End-of-sequence token id.
|
| 87 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
| 88 |
+
Tensor parallelism rank used during pretraining for reproducibility.
|
| 89 |
+
tie_word_embeddings (`bool`, *optional*, defaults to False):
|
| 90 |
+
Whether to tie input and output embeddings.
|
| 91 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 92 |
+
Base period for RoPE embeddings.
|
| 93 |
+
rope_scaling (`dict`, *optional*, defaults to None):
|
| 94 |
+
Dictionary for RoPE scaling parameters. (e.g. {"type":"yarn","factor":40,...})
|
| 95 |
+
attention_bias (`bool`, *optional*, defaults to False):
|
| 96 |
+
Whether to include bias terms in Q/K/V/out projections.
|
| 97 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 98 |
+
Dropout probability for attention probabilities.
|
| 99 |
+
_attn_implementation (`str`, *optional*, defaults to "flash_attention_2"): # New: Attention Implementation Type
|
| 100 |
+
String indicating the attention implementation. Can be "eager", "flash_attention_2", or "sparse_attention" (if implemented).
|
| 101 |
+
**kwargs:
|
| 102 |
+
Additional arguments passed to `PretrainedConfig`.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
model_type = "deepseek_v3"
|
| 106 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 107 |
+
|
| 108 |
+
def __init__(
|
| 109 |
+
self,
|
| 110 |
+
vocab_size=129280,
|
| 111 |
+
hidden_size=7168,
|
| 112 |
+
intermediate_size=18432,
|
| 113 |
+
moe_intermediate_size=2048,
|
| 114 |
+
num_hidden_layers=61,
|
| 115 |
+
num_nextn_predict_layers=1,
|
| 116 |
+
num_attention_heads=128,
|
| 117 |
+
num_key_value_heads=None, # Will be set to num_attention_heads if None
|
| 118 |
+
n_shared_experts=1,
|
| 119 |
+
n_routed_experts=256,
|
| 120 |
+
ep_size=1,
|
| 121 |
+
routed_scaling_factor=2.5,
|
| 122 |
+
kv_lora_rank=512,
|
| 123 |
+
q_lora_rank=1536,
|
| 124 |
+
qk_rope_head_dim=64,
|
| 125 |
+
v_head_dim=128,
|
| 126 |
+
qk_nope_head_dim=128,
|
| 127 |
+
topk_method="noaux_tc",
|
| 128 |
+
n_group=8,
|
| 129 |
+
topk_group=4,
|
| 130 |
+
num_experts_per_tok=8,
|
| 131 |
+
moe_layer_freq=1,
|
| 132 |
+
first_k_dense_replace=3,
|
| 133 |
+
norm_topk_prob=True,
|
| 134 |
+
scoring_func="sigmoid",
|
| 135 |
+
aux_loss_alpha=0.001,
|
| 136 |
+
seq_aux=True,
|
| 137 |
+
hidden_act="silu",
|
| 138 |
+
max_position_embeddings=4096,
|
| 139 |
+
initializer_range=0.02,
|
| 140 |
+
rms_norm_eps=1e-6,
|
| 141 |
+
use_cache=True,
|
| 142 |
+
pad_token_id=None,
|
| 143 |
+
bos_token_id=0,
|
| 144 |
+
eos_token_id=1,
|
| 145 |
+
pretraining_tp=1,
|
| 146 |
+
tie_word_embeddings=False,
|
| 147 |
+
rope_theta=10000.0,
|
| 148 |
+
rope_scaling=None,
|
| 149 |
+
attention_bias=False,
|
| 150 |
+
attention_dropout=0.0,
|
| 151 |
+
_attn_implementation="flash_attention_2", # New: Default to flash_attention_2
|
| 152 |
+
**kwargs,
|
| 153 |
+
):
|
| 154 |
+
# Set defaults
|
| 155 |
+
self.vocab_size = vocab_size
|
| 156 |
+
self.hidden_size = hidden_size
|
| 157 |
+
self.intermediate_size = intermediate_size
|
| 158 |
+
self.moe_intermediate_size = moe_intermediate_size
|
| 159 |
+
self.num_hidden_layers = num_hidden_layers
|
| 160 |
+
self.num_nextn_predict_layers = num_nextn_predict_layers
|
| 161 |
+
self.num_attention_heads = num_attention_heads
|
| 162 |
+
# default num_key_value_heads = num_attention_heads if not specified
|
| 163 |
+
if num_key_value_heads is None:
|
| 164 |
+
num_key_value_heads = num_attention_heads
|
| 165 |
+
self.num_key_value_heads = num_key_value_heads
|
| 166 |
+
|
| 167 |
+
self.n_shared_experts = n_shared_experts
|
| 168 |
+
self.n_routed_experts = n_routed_experts
|
| 169 |
+
self.ep_size = ep_size
|
| 170 |
+
self.routed_scaling_factor = routed_scaling_factor
|
| 171 |
+
self.kv_lora_rank = kv_lora_rank
|
| 172 |
+
self.q_lora_rank = q_lora_rank
|
| 173 |
+
self.qk_rope_head_dim = qk_rope_head_dim
|
| 174 |
+
self.v_head_dim = v_head_dim
|
| 175 |
+
self.qk_nope_head_dim = qk_nope_head_dim
|
| 176 |
+
self.topk_method = topk_method
|
| 177 |
+
self.n_group = n_group
|
| 178 |
+
self.topk_group = topk_group
|
| 179 |
+
self.num_experts_per_tok = num_experts_per_tok
|
| 180 |
+
self.moe_layer_freq = moe_layer_freq
|
| 181 |
+
self.first_k_dense_replace = first_k_dense_replace
|
| 182 |
+
self.norm_topk_prob = norm_topk_prob
|
| 183 |
+
self.scoring_func = scoring_func
|
| 184 |
+
self.aux_loss_alpha = aux_loss_alpha
|
| 185 |
+
self.seq_aux = seq_aux
|
| 186 |
+
self.hidden_act = hidden_act
|
| 187 |
+
self.max_position_embeddings = max_position_embeddings
|
| 188 |
+
self.initializer_range = initializer_range
|
| 189 |
+
self.rms_norm_eps = rms_norm_eps
|
| 190 |
+
self.use_cache = use_cache
|
| 191 |
+
self.rope_theta = rope_theta
|
| 192 |
+
self.rope_scaling = rope_scaling
|
| 193 |
+
self.attention_bias = attention_bias
|
| 194 |
+
self.attention_dropout = attention_dropout
|
| 195 |
+
self._attn_implementation = _attn_implementation # New: set attention implementation type
|
| 196 |
+
self.pretraining_tp = pretraining_tp
|
| 197 |
+
|
| 198 |
+
# Pass everything to PretrainedConfig
|
| 199 |
+
super().__init__(
|
| 200 |
+
pad_token_id=pad_token_id,
|
| 201 |
+
bos_token_id=bos_token_id,
|
| 202 |
+
eos_token_id=eos_token_id,
|
| 203 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 204 |
+
**kwargs,
|
| 205 |
+
)
|