VinayHajare commited on
Commit
6ecf255
·
verified ·
1 Parent(s): 90bb054

Add DeepSeek-V4 configuration class

Browse files
Files changed (1) hide show
  1. configuration_deepseek_v4.py +186 -0
configuration_deepseek_v4.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ DeepSeek-V4 Configuration
3
+ =========================
4
+ HuggingFace-compatible configuration for the DeepSeek-V4 architecture.
5
+
6
+ Key innovations over DeepSeek-V3:
7
+ - Compressed Sparse Attention (CSA) with sliding window + learned KV compression
8
+ - Manifold-Constrained Hyper-Connections (mHC) replacing residual connections
9
+ - Hash routing for first N layers + sqrtsoftplus scoring
10
+ - Grouped low-rank output projection
11
+ - Attention sink (learnable per-head bias)
12
+ """
13
+
14
+ from transformers import PretrainedConfig
15
+
16
+
17
+ class DeepSeekV4Config(PretrainedConfig):
18
+ """Configuration class for the DeepSeek-V4 model.
19
+
20
+ This config captures ALL architectural parameters from the DeepSeek-V4 technical report
21
+ and official inference code, enabling faithful reproduction.
22
+ """
23
+
24
+ model_type = "deepseek_v4"
25
+
26
+ def __init__(
27
+ self,
28
+ # Core dimensions
29
+ vocab_size: int = 129280,
30
+ hidden_size: int = 7168,
31
+ num_hidden_layers: int = 61,
32
+ num_attention_heads: int = 128,
33
+
34
+ # MLA (Multi-head Latent Attention) parameters
35
+ q_lora_rank: int = 1536,
36
+ head_dim: int = 512, # unified KV head dim (V4 unifies nope+rope+v)
37
+ qk_rope_head_dim: int = 64, # decoupled RoPE dimension
38
+ num_key_value_heads: int = 1, # effectively MQA at compression level
39
+
40
+ # Grouped output projection
41
+ o_groups: int = 16, # number of output groups
42
+ o_lora_rank: int = 1024, # per-group output compression rank
43
+
44
+ # Compressed Sparse Attention
45
+ window_size: int = 128, # sliding window size
46
+ compress_ratios: tuple = None, # per-layer compression ratios (0=no compression)
47
+ compress_rope_theta: float = 160000.0, # RoPE theta for compressed KV
48
+
49
+ # Indexer (learned sparse attention selection)
50
+ index_n_heads: int = 64,
51
+ index_head_dim: int = 128,
52
+ index_topk: int = 1024,
53
+
54
+ # Attention sink
55
+ attention_bias: bool = False, # standard bias in linear layers
56
+ attention_dropout: float = 0.0,
57
+
58
+ # MoE parameters
59
+ moe_intermediate_size: int = 3072,
60
+ n_routed_experts: int = 384,
61
+ n_shared_experts: int = 1,
62
+ num_experts_per_tok: int = 6,
63
+ num_hash_layers: int = 3, # first N layers use hash routing
64
+ scoring_func: str = "sqrtsoftplus", # NEW in V4: sqrt(softplus(x))
65
+ topk_method: str = "noaux_tc",
66
+ norm_topk_prob: bool = True,
67
+ routed_scaling_factor: float = 2.5,
68
+ swiglu_limit: float = 10.0, # NEW in V4: clamp SwiGLU activations
69
+
70
+ # Expert quantization
71
+ expert_dtype: str = "fp4", # NEW in V4: FP4 expert weights
72
+
73
+ # Hyper-Connections (mHC)
74
+ hc_mult: int = 4, # number of hidden state copies
75
+ hc_sinkhorn_iters: int = 20, # Sinkhorn normalization iterations
76
+ hc_eps: float = 1e-6,
77
+
78
+ # Multi-Token Prediction
79
+ num_nextn_predict_layers: int = 1,
80
+
81
+ # Normalization
82
+ rms_norm_eps: float = 1e-6,
83
+ hidden_act: str = "silu",
84
+
85
+ # RoPE parameters
86
+ rope_theta: float = 10000.0,
87
+ rope_scaling: dict = None,
88
+ max_position_embeddings: int = 1048576, # 1M context
89
+
90
+ # Quantization config (for inference)
91
+ quantization_config: dict = None,
92
+
93
+ # Standard HF config
94
+ initializer_range: float = 0.02,
95
+ use_cache: bool = True,
96
+ tie_word_embeddings: bool = False,
97
+ bos_token_id: int = 0,
98
+ eos_token_id: int = 1,
99
+ torch_dtype: str = "bfloat16",
100
+
101
+ **kwargs,
102
+ ):
103
+ super().__init__(
104
+ bos_token_id=bos_token_id,
105
+ eos_token_id=eos_token_id,
106
+ tie_word_embeddings=tie_word_embeddings,
107
+ **kwargs,
108
+ )
109
+
110
+ self.vocab_size = vocab_size
111
+ self.hidden_size = hidden_size
112
+ self.num_hidden_layers = num_hidden_layers
113
+ self.num_attention_heads = num_attention_heads
114
+
115
+ # MLA
116
+ self.q_lora_rank = q_lora_rank
117
+ self.head_dim = head_dim
118
+ self.qk_rope_head_dim = qk_rope_head_dim
119
+ self.num_key_value_heads = num_key_value_heads
120
+
121
+ # Grouped output
122
+ self.o_groups = o_groups
123
+ self.o_lora_rank = o_lora_rank
124
+
125
+ # Compressed Sparse Attention
126
+ self.window_size = window_size
127
+ if compress_ratios is not None:
128
+ self.compress_ratios = list(compress_ratios)
129
+ else:
130
+ # Default: V4-Pro pattern — alternating 128 and 4 compression ratios
131
+ # First 2 layers: 128 (HCA only), then alternating 4,128 pairs, last layer: 0
132
+ self.compress_ratios = [128, 128] + [4, 128] * 29 + [4, 0]
133
+ self.compress_rope_theta = compress_rope_theta
134
+
135
+ # Indexer
136
+ self.index_n_heads = index_n_heads
137
+ self.index_head_dim = index_head_dim
138
+ self.index_topk = index_topk
139
+
140
+ # Attention
141
+ self.attention_bias = attention_bias
142
+ self.attention_dropout = attention_dropout
143
+
144
+ # MoE
145
+ self.moe_intermediate_size = moe_intermediate_size
146
+ self.n_routed_experts = n_routed_experts
147
+ self.n_shared_experts = n_shared_experts
148
+ self.num_experts_per_tok = num_experts_per_tok
149
+ self.num_hash_layers = num_hash_layers
150
+ self.scoring_func = scoring_func
151
+ self.topk_method = topk_method
152
+ self.norm_topk_prob = norm_topk_prob
153
+ self.routed_scaling_factor = routed_scaling_factor
154
+ self.swiglu_limit = swiglu_limit
155
+ self.expert_dtype = expert_dtype
156
+
157
+ # Hyper-Connections
158
+ self.hc_mult = hc_mult
159
+ self.hc_sinkhorn_iters = hc_sinkhorn_iters
160
+ self.hc_eps = hc_eps
161
+
162
+ # MTP
163
+ self.num_nextn_predict_layers = num_nextn_predict_layers
164
+
165
+ # Normalization
166
+ self.rms_norm_eps = rms_norm_eps
167
+ self.hidden_act = hidden_act
168
+
169
+ # RoPE
170
+ self.rope_theta = rope_theta
171
+ self.rope_scaling = rope_scaling or {
172
+ "type": "yarn",
173
+ "factor": 16,
174
+ "original_max_position_embeddings": 65536,
175
+ "beta_fast": 32,
176
+ "beta_slow": 1,
177
+ }
178
+ self.max_position_embeddings = max_position_embeddings
179
+
180
+ # Quantization
181
+ self.quantization_config = quantization_config
182
+
183
+ # Standard
184
+ self.initializer_range = initializer_range
185
+ self.use_cache = use_cache
186
+ self.torch_dtype = torch_dtype