Router, by token ID

Score projection

×

One logit per expert.

z=xffnWR{\htmlData{light=score-projection}{z}}={\htmlData{light=x}{x_{\mathrm{ffn}}}}W_{{\htmlClass{vl-stream-context}{R}}}
in
xffnx_{\mathrm{ffn}} [B batch, T positions, 4096 d]
out
zz [B batch, T positions, 256 experts]
Parameters1,048,576 × 2 layers

1,048,576 parameters per instance.

ComponentCalculationParameters
Weight matrix4,096 × 2561,048,576

This projection has no bias parameter.

The reference module holds the weight output by input, [out, in].

One in each of the 2 layers, each with its own weights.

Sources

In codelinear(x.float(), self.weight.float()) · 565

Open pinned code ↗

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 1.5.

model.py 546–584 ↗ · deepseek-ai/DeepSeek-V4-Flash @ 60d8d70770c6

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