40 layers › Window layer › Mixture of experts › Router
Score projection
×learnedlinear(x.float(), self.weight.float()) · 811
One logit per expert.
- in
- [B batch, T positions, 5120 d]
- out
- [B batch, T positions, 384 experts]
- parameters
- 1,966,080
Parameters
1,966,080 parameters per instance.
| Component | Calculation | Parameters |
|---|---|---|
| Weight matrix | 1,966,080 | |
| Per-instance total | 1,966,080 | |
This projection has no bias parameter.
The reference module holds the weight output by input, [out, in].
Derived from the configured matrix dimensions; the model total matches the checkpoint’s tensors.
Codemodel.py 792–827
class Gate(nn.Module):
"""MoE gating. The correction bias steers expert selection only; the routing weights come from the
unbiased scores. Image-span tokens use a separate bias (training `noaux_tc_for_vl`)."""
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
n_routed_experts, n_activated_experts = args.get_moe_config(layer_id)
self.dim = args.dim
self.topk = n_activated_experts
self.score_func = args.score_func
self.gate_temp = args.gate_temp
self.norm_topk_prob = args.norm_topk_prob
self.route_scale = args.route_scale
self.weight = nn.Parameter(torch.empty(n_routed_experts, args.dim))
self.bias = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32))
self.bias_vl = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32)) if args.vision_enabled else None
def forward(self, x: torch.Tensor, image_mask: torch.Tensor | None = None) -> tuple[torch.Tensor, torch.Tensor]:
"""x: [n, dim]; image_mask: [n] bool, True for tokens inside an image span."""
scores = linear(x.float(), self.weight.float()) / self.gate_temp
if self.score_func == "softmax":
scores = scores.softmax(dim=-1)
elif self.score_func == "sigmoid":
scores = scores.sigmoid()
else:
scores = F.softplus(scores).sqrt()
bias = self.bias
if image_mask is not None and self.bias_vl is not None:
bias = torch.where(image_mask.unsqueeze(-1), self.bias_vl, bias)
# the bias picks experts but does not scale them: weights come from the raw scores
indices = (scores + bias).topk(self.topk, dim=-1)[1]
weights = scores.gather(1, indices)
if self.norm_topk_prob and self.topk > 1:
weights /= weights.sum(dim=-1, keepdim=True) + 1e-20 # not norm_eps, matches training
weights *= self.route_scale
return weights, indicesSources1
Direct evidence
The router: the square root of softplus of a projection, a selection bias (a second one at image positions), the best six, their unbiased scores normalised and scaled.
gate · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 792–827
Read code excerpt
class Gate(nn.Module):
"""MoE gating. The correction bias steers expert selection only; the routing weights come from the
unbiased scores. Image-span tokens use a separate bias (training `noaux_tc_for_vl`)."""
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
n_routed_experts, n_activated_experts = args.get_moe_config(layer_id)
self.dim = args.dim
self.topk = n_activated_experts
self.score_func = args.score_func
self.gate_temp = args.gate_temp
self.norm_topk_prob = args.norm_topk_prob
self.route_scale = args.route_scale
self.weight = nn.Parameter(torch.empty(n_routed_experts, args.dim))
self.bias = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32))
self.bias_vl = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32)) if args.vision_enabled else None
def forward(self, x: torch.Tensor, image_mask: torch.Tensor | None = None) -> tuple[torch.Tensor, torch.Tensor]:
"""x: [n, dim]; image_mask: [n] bool, True for tokens inside an image span."""
scores = linear(x.float(), self.weight.float()) / self.gate_temp
if self.score_func == "softmax":
scores = scores.softmax(dim=-1)
elif self.score_func == "sigmoid":
scores = scores.sigmoid()
else:
scores = F.softplus(scores).sqrt()
bias = self.bias
if image_mask is not None and self.bias_vl is not None:
bias = torch.where(image_mask.unsqueeze(-1), self.bias_vl, bias)
# the bias picks experts but does not scale them: weights come from the raw scores
indices = (scores + bias).topk(self.topk, dim=-1)[1]
weights = scores.gather(1, indices)
if self.norm_topk_prob and self.topk > 1:
weights /= weights.sum(dim=-1, keepdim=True) + 1e-20 # not norm_eps, matches training
weights *= self.route_scale
return weights, indices