Router, by token ID

Experts by token ID

×

Take the 6 experts a fixed table lists for the token’s ID, and weight them by their scores.

Why

By token ID. DeepSeek-V3 had dense feed-forward layers at the start of the stack; V4 uses experts there too, routed by a fixed function of the token ID (Roller et al., 2021). The report gives no further reason, and §4.2.1 sets it for the first three layers. DeepSeek-V4 report §2.1, p. 7

T=table⁡[It]\mathcal T=\operatorname{table}[{\htmlData{light=token-ids}{I_t}}]
Gi=1.5 si∑j∈Tsj  (i∈T){\htmlData{light=gates hash-select}{G_{i}}}=1.5\,\frac{{\htmlData{light=scores score}{s_{i}}}}{\sum_{j\in\mathcal T}{\htmlData{light=scores score}{s_{j}}}}\;(i\in\mathcal T)
in
ss [B batch, T positions, 256 experts]II [B batch, T positions]
out
GG [B batch, T positions, 256 experts]

The table, tid2eid, has 129,280 rows of 6 expert indices; it is stored in the checkpoint (as 64-bit integers) and not trained (requires_grad=False, model.py 559). The paper calls it a predefined hash function of the token ID (§2.1); the table is published in the checkpoint; how it was made is not described. These layers have no selection bias (model.py 558–560).

Sources

In codeindices = self.tid2eid[input_ids] · 576–577

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

The reference configuration: 43 layers of width 4,096, 64 heads of 512 features with a 64-feature rotary part, a 1,024-feature query latent, a grouped output of 8 groups at rank 1,024, a window of 128, the per-layer compression ratios (0, 0, then 4 and 128 alternating, and 0 for the MTP block; 0 is a layer of window attention only), an indexer of 64 heads of 128 keeping 512, hyper-connections of 4 streams with 20 Sinkhorn iterations, 256 routed experts of 2,048 of which 6 are used with 1 shared, square-root-softplus scores scaled by 1.5, three hash-routed layers, and YaRN over 65,536 positions.

config.json 1–34 ↗ · deepseek-ai/DeepSeek-V4-Flash @ 60d8d70770c6

Excerpt
{
    "vocab_size": 129280,
    "dim": 4096,
    "moe_inter_dim": 2048,
    "n_layers": 43,
    "n_hash_layers": 3,
    "n_heads": 64,
    "n_routed_experts": 256,
    "n_shared_experts": 1,
    "n_activated_experts": 6,
    "score_func": "sqrtsoftplus",
    "route_scale": 1.5,
    "swiglu_limit": 10.0,
    "q_lora_rank": 1024,
    "head_dim": 512,
    "rope_head_dim": 64,
    "o_groups": 8,
    "o_lora_rank": 1024,
    "window_size": 128,
    "original_seq_len": 65536,
    "rope_theta": 10000,
    "rope_factor": 16,
    "beta_fast": 32,
    "beta_slow": 1,
    "index_n_heads": 64,
    "index_head_dim": 128,
    "index_topk": 512,
    "hc_mult": 4,
    "hc_sinkhorn_iters": 20,
    "dtype": "fp8",
    "scale_fmt": "ue8m0",
    "expert_dtype": "fp4",
    "compress_rope_theta": 160000,
    "compress_ratios": [0, 0, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 0]