Score projection
×One logit per expert.
- in
- [B batch, T positions, 7168 d]
- out
- [B batch, T positions, 384 experts]
Parameters2,752,512 × 29 layers
2,752,512 parameters per instance.
| Component | Calculation | Parameters |
|---|---|---|
| Weight matrix | 7,168 × 384 | 2,752,512 |
This projection has no bias parameter.
The reference module holds the weight output by input, [out, in].
One in each of the 29 layers, each with its own weights.
Sources
In codelinear(x.float(), self.weight.float()) · 565
Direct evidence
The router: square-root softplus scores from one projection in float32; in the first n_hash_layers layers the experts come from a fixed token-to-expert table and there is no bias; elsewhere the top 6 of the scores plus a selection bias; the weights are the unbiased scores of the chosen experts, divided by their sum and multiplied by 2.5.
model.py 546–584 ↗ · deepseek-ai/DeepSeek-V4-Pro @ b5968e9190ef
Excerpt
class Gate(nn.Module):
"""MoE gating: computes expert routing scores and selects top-k experts.
Supports hash-based routing (first n_hash_layers) where expert indices are
predetermined per token ID, and score-based routing (remaining layers)."""
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
self.dim = args.dim
self.topk = args.n_activated_experts
self.score_func = args.score_func
self.route_scale = args.route_scale
self.hash = layer_id < args.n_hash_layers
self.weight = nn.Parameter(torch.empty(args.n_routed_experts, args.dim))
if self.hash:
self.tid2eid = nn.Parameter(torch.empty(args.vocab_size, args.n_activated_experts, dtype=torch.int32), requires_grad=False)
self.bias = None
else:
self.bias = nn.Parameter(torch.empty(args.n_routed_experts, dtype=torch.float32))
def forward(self, x: torch.Tensor, input_ids: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
scores = linear(x.float(), self.weight.float())
if self.score_func == "softmax":
scores = scores.softmax(dim=-1)
elif self.score_func == "sigmoid":
scores = scores.sigmoid()
else:
scores = F.softplus(scores).sqrt()
original_scores = scores
# Bias shifts scores for expert selection (topk) but does not affect routing weights.
if self.bias is not None:
scores = scores + self.bias
if self.hash:
indices = self.tid2eid[input_ids]
else:
indices = scores.topk(self.topk, dim=-1)[1]
weights = original_scores.gather(1, indices)
if self.score_func != "softmax":
weights /= weights.sum(dim=-1, keepdim=True)
weights *= self.route_scale
return weights, indices