Router

Normalize weights

×

Normalize the selected affinities to sum to one, then scale by 2.827.

Gi=2.827 si Eisel∑jsj Ejsel{\htmlData{light=output normalize}{G_{i}}}=2.827\,\frac{{\htmlData{light=affinities sigmoid}{s_i}}\,{\htmlData{light=selected select}{E^{\mathrm{sel}}_i}}}{\sum_{j}{\htmlData{light=affinities sigmoid}{s_j}}\,{\htmlData{light=selected select}{E^{\mathrm{sel}}_j}}}
in
ss [B batch, T positions, 384 experts]EselE^{\mathrm{sel}} [B batch, T positions, 384 experts]
out
GG [B batch, T positions, 384 experts]
Explanation

Unselected experts get weight zero, so each position’s weights sum to 2.827.

Sources

In codetopk_weight / denominator; topk_weight * self.routed_scaling_factor

Open pinned code ↗

Direct evidence

Routing: sigmoid scores, bias added for selection, the best groups kept by their top-two scores (one group here), the top-8 experts chosen, their unbiased scores renormalized and scaled by the route scale.

modeling_deepseek.py 441–490 ↗ · moonshotai/Kimi-K2.6 @ 7eb5002f6aad

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(),
                                                       0.0)  # [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