Final RMSNorm
×Rescale each position’s features using their root mean square, then apply learned gain.
- in
- [B batch, T positions, 7168 d]
- out
- [B batch, T positions, 7168 d]
Normalized in float32, cast back to the input type, then multiplied by the gain (layernorm.py 52–62). It first adds the last layer’s update to the residual (deepseek_v2.py 1589).
Parameters7,168
7,168 parameters per instance.
| Component | Parameters |
|---|---|
| Learned gain | 7,168 |
Sources
In codeself.norm(hidden_states, residual)
Direct evidence
RMSNorm normalizes in float32, casts to the gain’s type, multiplies by the gain and casts back; the fused form first adds the residual in float32 and returns the normalized sum and the sum, cast back to the input type.
layernorm.py 9–62 ↗ · vllm-project/vllm @ 5df63a7203cd
Excerpt
@register_op
def rms_norm(
x: Tensor, weight: Tensor | None, epsilon: float, variance_size: int | None = None
) -> Tensor:
"""Weighted root-mean-square layer normalization."""
orig_dtype = x.dtype
x = x.to(torch.float32)
x_var = x if variance_size is None else x[..., :variance_size]
variance = x_var.pow(2).mean(dim=-1, keepdim=True)
x = x * torch.rsqrt(variance + epsilon)
if weight is not None:
x = x.to(weight.dtype) * weight
return x.to(orig_dtype)… lines 22–42 omitted …
@register_op(allow_inplace=True)
def fused_add_rms_norm(
x: Tensor,
x_residual: Tensor,
weight: Tensor | None,
epsilon: float,
variance_size: int | None = None,
) -> tuple[Tensor, Tensor]:
"""Fused add and weighted root-mean-square layer normalization."""
orig_dtype = x.dtype
x = x.to(torch.float32)
x = x + x_residual.to(torch.float32)
x_residual = x.to(orig_dtype)
x_var = x if variance_size is None else x[..., :variance_size]
variance = x_var.pow(2).mean(dim=-1, keepdim=True)
x = x * torch.rsqrt(variance + epsilon)
if weight is not None:
x = x.to(weight.dtype) * weight
return x.to(orig_dtype), x_residualThe model embeds the token IDs, computes the llama-4 position factors once, applies the layers with a residual stream that each next norm adds in, and applies the final norm, which adds the last update.
deepseek_v2.py 1459–1592 ↗ · vllm-project/vllm @ 5df63a7203cd
Excerpt
if get_pp_group().is_first_rank:
self.embed_tokens = VocabParallelEmbedding(
config.vocab_size,
self.hidden_size,
quant_config=quant_config,
prefix=f"{prefix}.embed_tokens",
)… lines 1466–1484 omitted …
if get_pp_group().is_last_rank:
self.norm = RMSNorm(self.hidden_size, eps=config.rms_norm_eps)… lines 1487–1507 omitted …
def forward(
self,
input_ids: torch.Tensor | None,
positions: torch.Tensor,
intermediate_tensors: IntermediateTensors | None,
inputs_embeds: torch.Tensor | None = None,
) -> torch.Tensor | IntermediateTensors:
if get_pp_group().is_first_rank:
if inputs_embeds is not None:
hidden_states = inputs_embeds
else:
if input_ids is None:
raise ValueError(
"Either input_ids or inputs_embeds must be provided "
"to DeepseekV2Model.forward"
)
hidden_states = self.embed_input_ids(input_ids)
residual = None
else:
assert intermediate_tensors is not None
hidden_states = intermediate_tensors["hidden_states"]
residual = intermediate_tensors["residual"]
# Compute llama 4 scaling once per forward pass if enabled
llama_4_scaling_config = getattr(self.config, "llama_4_scaling", None)
llama_4_scaling: torch.Tensor | None
if llama_4_scaling_config is not None:
llama_4_scaling = _get_llama_4_scaling(
original_max_position_embeddings=llama_4_scaling_config[
"original_max_position_embeddings"
],
scaling_beta=llama_4_scaling_config["beta"],
positions=positions,
)
else:
llama_4_scaling = None
aux_hidden_states = []
for idx, layer in enumerate(
islice(self.layers, self.start_layer, self.end_layer),
start=self.start_layer,
):
# all gather if we need to use the whole states
if (
hidden_states.shape[0] != positions.shape[0]
and not layer.use_sequence_parallel_moe
):
combined_states = torch.cat([hidden_states, residual], dim=-1)
combined_states = tensor_model_parallel_all_gather(combined_states, 0)
combined_states = combined_states[: positions.shape[0]]
hidden_states, residual = combined_states.split(
[self.hidden_size, self.hidden_size], dim=-1
)
if idx in self.aux_hidden_state_layers:
aux_hidden_state = hidden_states + residual
if aux_hidden_state.shape[0] != positions.shape[0]:
aux_hidden_state = tensor_model_parallel_all_gather(
aux_hidden_state, 0
)
aux_hidden_state = aux_hidden_state[: positions.shape[0]]
aux_hidden_states.append(aux_hidden_state)
hidden_states, residual = layer(
positions, hidden_states, residual, llama_4_scaling
)
if not get_pp_group().is_last_rank:
return IntermediateTensors(
{"hidden_states": hidden_states, "residual": residual}
)
if hidden_states.shape[0] != positions.shape[0]:
combined_states = torch.cat([hidden_states, residual], dim=-1)
combined_states = tensor_model_parallel_all_gather(combined_states, 0)
combined_states = combined_states[: positions.shape[0]]
hidden_states, residual = combined_states.split(
[self.hidden_size, self.hidden_size], dim=-1
)
if self.end_layer in self.aux_hidden_state_layers:
aux_hidden_states.append(hidden_states + residual)
hidden_states, _ = self.norm(hidden_states, residual)
if len(aux_hidden_states) > 0:
return hidden_states, aux_hidden_states
return hidden_states