Skip to content

vllm.model_executor.layers.layernorm

Custom normalization layers.

Classes:

GemmaRMSNorm

Bases: CustomOp

RMS normalization for Gemma.

Two differences from the above RMSNorm
  1. x * (1 + w) instead of x * w.
  2. (x * w).to(orig_dtype) instead of x.to(orig_dtype) * w.

Methods:

  • forward_native –

    PyTorch-native implementation equivalent to forward().

forward_native(x, residual=None)

PyTorch-native implementation equivalent to forward().

LayerNorm

Bases: CustomOp

Standard (mean-centered) LayerNorm.

Drop-in for a bare nn.LayerNorm that dispatches to a fused XPU kernel when one is available, and to the native implementation otherwise.

Normalization runs in the wider of the input and parameter dtypes and the result is cast back to the input dtype, so dtype=torch.float32 gives an fp32 reduction for a lower-precision input.

RMSNorm

Bases: CustomOp

Root mean square normalization.

Computes x -> w * x / sqrt(E[x^2] + eps) where w is the learned weight. Refer to https://arxiv.org/abs/1910.07467

Methods:

  • forward_native –

    PyTorch-native implementation equivalent to forward().

forward_native(x, residual=None)

PyTorch-native implementation equivalent to forward().

RMSNormGated

Bases: CustomOp

RMS Normalization with optional gating.

This is a native PyTorch implementation that supports: - Standard RMS normalization - Group RMS normalization - Optional gating with SiLU activation

Methods:

  • __init__ –

    Initialize RMSNormGated.

  • forward_native –

    PyTorch-native implementation equivalent to forward().

  • forward_static –

    Pure-PyTorch RMS normalization with optional gating.

__init__(hidden_size, eps=1e-05, group_size=None, norm_before_gate=False, device=None, dtype=None, activation='swish')

Initialize RMSNormGated.

Parameters:

  • hidden_size

    (int) –

    Size of the hidden dimension

  • eps

    (float, default: 1e-05 ) –

    Epsilon for numerical stability

  • group_size

    (int | None, default: None ) –

    If not None, do GroupNorm with each group having group_size elements. group_size=None is equivalent to group_size=hidden_size (i.e. there's only 1 group).

  • norm_before_gate

    (bool, default: False ) –

    If True and z is provided: out = norm(x) * silu(z) If False and z is provided: out = norm(x * silu(z))

  • device

    (device | None, default: None ) –

    Device to create parameters on

  • dtype

    (dtype | None, default: None ) –

    Data type for parameters

  • activation

    (str, default: 'swish' ) –

    Activation function name for gating

forward_native(x, z=None)

PyTorch-native implementation equivalent to forward().

forward_static(x, z, weight, epsilon, orig_dtype, group_size=None, norm_before_gate=True, activation='swish') staticmethod

Pure-PyTorch RMS normalization with optional gating.

This static method contains the full native logic so that both forward_native and MatcherRMSNormGated (used by the compilation pattern matcher) can share the same implementation.

If z is not None and norm_before_gate is True: out = rms_norm(x) * act(z) If z is not None and norm_before_gate is False: out = rms_norm(x * act(z))