Router
×Score all 256 experts for each position and keep 8 from the best 4 of 8 groups; their weights are normalized, then scaled by 2.5.
Why
A bias, no loss. An unbalanced load on the experts leads to routing collapse and wastes compute with expert parallelism; the usual remedy is an auxiliary loss, and too large a loss impairs the model. DeepSeek-V3’s authors add a bias to each expert’s affinity, used only to choose the experts, and after every training step lower it for an overloaded expert and raise it for an underloaded one, by 0.001 for the first 14.3T tokens. They report a balanced load through training and better performance than with auxiliary losses alone; a small sequence-wise loss, α = 0.0001, only prevents extreme imbalance within one sequence. DeepSeek-V3 report §2.1.2 and §4.2, pp. 9, 23
4 of 8 groups. In training the routed experts of a layer sat on 8 nodes, and each token was sent to at most 4 of them, chosen by the sum of the highest affinities of the experts on each node, to limit communication; the authors say their training framework then nearly overlaps computation and communication fully. The configuration keeps the same limit at inference: 8 groups of 32 experts, at most 4 of them per token (n_group, topk_group). DeepSeek-V3 report §2.1.2 and §4.2, pp. 10, 23
All 4 steps
- in
- [B batch, T positions, 7168 d]
- out
- [B batch, T positions, 256 experts]
Parameters1,835,264 × 58 layers
1,835,264 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Expert scores | 7,168 × 256 | 1,835,008 |
| Select experts | 256 | |
| Per-instance total | 1,835,264 | |
One in each of the 58 MoE layers, each with its own weights.
Explanation
The implementation returns each position’s 8 weights with their expert indices (modeling_deepseek.py 473); spreads them over all 256 experts, zero for the other 248.
Sources
In codeself.gate
Direct evidence
The router owns one score vector per routed expert and, with noaux_tc selection, a per-expert correction bias used only for selection.
modeling_deepseek.py 393–420 ↗ · deepseek-ai/DeepSeek-V3 @ e815299b0bcb
Excerpt
class MoEGate(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.top_k = config.num_experts_per_tok
self.n_routed_experts = config.n_routed_experts
self.routed_scaling_factor = config.routed_scaling_factor
self.scoring_func = config.scoring_func
self.topk_method = config.topk_method
self.n_group = config.n_group
self.topk_group = config.topk_group
# topk selection algorithm
self.norm_topk_prob = config.norm_topk_prob
self.gating_dim = config.hidden_size
self.weight = nn.Parameter(
torch.empty((self.n_routed_experts, self.gating_dim))
)
if self.topk_method == "noaux_tc":
self.e_score_correction_bias = nn.Parameter(
torch.empty((self.n_routed_experts))
)
self.reset_parameters()
def reset_parameters(self) -> None:
import torch.nn.init as init
init.kaiming_uniform_(self.weight, a=math.sqrt(5))Routing: sigmoid scores, bias added for selection, the 8 groups of 32 experts ranked by the sum of their top-two biased scores and the best 4 kept, the experts of the other groups masked with −∞, the top-8 chosen, their unbiased scores renormalized and scaled by the route scale.
modeling_deepseek.py 422–473 ↗ · deepseek-ai/DeepSeek-V3 @ e815299b0bcb
Excerpt
def forward(self, hidden_states):
bsz, seq_len, h = hidden_states.shape
### compute gating score
hidden_states = hidden_states.view(-1, h)
logits = F.linear(
hidden_states.type(torch.float32), self.weight.type(torch.float32), None
)
if self.scoring_func == "sigmoid":
scores = logits.sigmoid()
else:
raise NotImplementedError(
f"insupportable scoring function for MoE gating: {self.scoring_func}"
)
### select top-k experts
if self.topk_method == "noaux_tc":
assert not self.training
scores_for_choice = scores.view(bsz * seq_len, -1) + self.e_score_correction_bias.unsqueeze(0)
group_scores = (
scores_for_choice.view(bsz * seq_len, self.n_group, -1).topk(2, dim=-1)[0].sum(dim = -1)
) # [n, n_group]
group_idx = torch.topk(
group_scores, k=self.topk_group, dim=-1, sorted=False
)[
1
] # [n, top_k_group]
group_mask = torch.zeros_like(group_scores) # [n, n_group]
group_mask.scatter_(1, group_idx, 1) # [n, n_group]
score_mask = (
group_mask.unsqueeze(-1)
.expand(
bsz * seq_len, self.n_group, self.n_routed_experts // self.n_group
)
.reshape(bsz * seq_len, -1)
) # [n, e]
tmp_scores = scores_for_choice.masked_fill(~score_mask.bool(), float("-inf")) # [n, e]
_, topk_idx = torch.topk(
tmp_scores, k=self.top_k, dim=-1, sorted=False
)
topk_weight = scores.gather(1, topk_idx)
else:
raise NotImplementedError(
f"insupportable TopK function for MoE gating: {self.topk_method}"
)
### norm gate to sum 1
if self.top_k > 1 and self.norm_topk_prob:
denominator = topk_weight.sum(dim=-1, keepdim=True) + 1e-20
topk_weight = topk_weight / denominator
topk_weight = topk_weight * self.routed_scaling_factor # must multiply the scaling factor
return topk_idx, topk_weight