Final RMSNorm

×

Rescale each position’s features using their root mean square, then apply learned gain.

m2(x)=1d∑j=1dxj2y=γfinal⊙xm2(x)+εε=10−5\begin{aligned}m_2(x)&=\frac{1}{d}\sum_{j=1}^d x_j^2\\y&=\gamma_{\mathrm{final}}\odot\frac{x}{\sqrt{m_2(x)+\varepsilon}}\\\varepsilon&=10^{-5}\end{aligned}
in
x61x_{61} [B batch, T positions, 7168 d]
out
zz [B batch, T positions, 7168 d]

Normalized in float32, cast back to the input type, then multiplied by the gain (modeling_deepseek.py 105–110). ε: the layer and final norms take the configuration’s 10⁻⁵ (modeling_deepseek.py 1149, 1151, 1352); the two latent norms inside attention keep DeepseekV3RMSNorm’s default 10⁻⁶ (96, 668, 678).

Parameters7,168

7,168 parameters per instance.

ComponentParameters
Learned gain7,168
Sources

In codeself.norm(hidden_states)

Open pinned code ↗

Direct evidence

RMSNorm owns one gain per feature and computes in float32.

modeling_deepseek.py 94–113 ↗ · moonshotai/Kimi-K2.6 @ 7eb5002f6aad

Excerpt
class DeepseekV3RMSNorm(nn.Module):

    def __init__(self, hidden_size, eps=1e-6):
        """
        DeepseekV3RMSNorm is equivalent to T5LayerNorm
        """
        super().__init__()
        self.weight = nn.Parameter(torch.ones(hidden_size))
        self.variance_epsilon = eps

    def forward(self, hidden_states):
        input_dtype = hidden_states.dtype
        hidden_states = hidden_states.to(torch.float32)
        variance = hidden_states.pow(2).mean(-1, keepdim=True)
        hidden_states = hidden_states * torch.rsqrt(variance +
                                                    self.variance_epsilon)
        return self.weight * hidden_states.to(input_dtype)


ALL_LAYERNORM_LAYERS.append(DeepseekV3RMSNorm)

The model embeds tokens (when handed IDs), numbers positions from the cached length, builds a 4D causal mask, applies all layers, normalizes and projects to vocabulary logits with an untied head, computing every position’s logits in float32.

modeling_deepseek.py 1344–1585 ↗ · moonshotai/Kimi-K2.6 @ 7eb5002f6aad

Excerpt
        self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size,
                                         self.padding_idx)
        self.layers = nn.ModuleList([
            DeepseekV3DecoderLayer(config, layer_idx)
            for layer_idx in range(config.num_hidden_layers)
        ])
        self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
        self.norm = DeepseekV3RMSNorm(config.hidden_size,
                                      eps=config.rms_norm_eps)

… lines 1353–1399 omitted …

        past_key_values_length = 0
        if use_cache:
            use_legacy_cache = not isinstance(past_key_values, Cache)
            if use_legacy_cache:
                past_key_values = DynamicCache.from_legacy_cache(
                    past_key_values)
            past_key_values_length = get_usable_length(past_key_values,
                                                       seq_length)

        if position_ids is None:
            device = input_ids.device if input_ids is not None else inputs_embeds.device
            position_ids = torch.arange(
                past_key_values_length,
                seq_length + past_key_values_length,
                dtype=torch.long,
                device=device,
            )
            position_ids = position_ids.unsqueeze(0)

… lines 1418–1418 omitted …

        if inputs_embeds is None:
            inputs_embeds = self.embed_tokens(input_ids)

… lines 1421–1421 omitted …

        if self._use_flash_attention_2:
            # 2d mask is passed through the layers
            attention_mask = (attention_mask if
                              (attention_mask is not None
                               and 0 in attention_mask) else None)
        else:
            # 4d mask is passed through the layers
            attention_mask = _prepare_4d_causal_attention_mask(
                attention_mask,
                (batch_size, seq_length),
                inputs_embeds,
                past_key_values_length,
            )

… lines 1435–1495 omitted …

        self.lm_head = nn.Linear(config.hidden_size,
                                 config.vocab_size,
                                 bias=False)

… lines 1499–1582 omitted …

        hidden_states = outputs[0]
        logits = self.lm_head(hidden_states)
        logits = logits.float()