flashinfer.norm

Kernels for normalization layers.

rmsnorm(input, weight[, eps, out, enable_pdl])

Root mean square normalization.

rmsnorm_quant(out, input, weight, scale[, ...])

Root mean square normalization + fp8 quantization.

fused_add_rmsnorm(input, residual, weight[, ...])

Fused add root mean square normalization.

fused_add_rmsnorm_quant(out, input, ...[, ...])

Fused add root mean square normalization + fp8 quantization.

fused_add_rmsnorm_fp8_block_quant(out, ...)

Fused add-residual + RMSNorm + 1x128 fp8 block quantization, in one pass.

gemma_rmsnorm(input, weight[, eps, out, ...])

Gemma-style root mean square normalization.

gemma_fused_add_rmsnorm(input, residual, weight)

Gemma-style fused add root mean square normalization.

layernorm(input, gemma, beta[, eps])

Layer normalization.

layernorm_quant(out, input, gemma, beta, scale)

Layer normalization + fp8 quantization.

fused_rmsnorm_silu(input, weight[, eps, ...])

Fused RMSNorm + SiLU activation.

fused_qk_rmsnorm_rope(qkv, q_weight, ...[, ...])

Fused QK RMSNorm + 3D RoPE + V copy for video generation DIT self-attention.

fused_dit_residual_layernorm_scale_shift(...)

Fused residual + LayerNorm + scale/shift for DIT self-attention.

fused_dit_gate_residual_layernorm_scale_shift(...)

Fused gate + residual + LayerNorm + scale/shift for DIT self-attention.

fused_dit_gate_residual_layernorm_gamma_beta(...)

Fused gate + residual + LayerNorm(gamma, beta) for DIT self-attention.

Fixed-Width Fused Norms

Fused gated-residual and per-head gated RMSNorm kernels for SM100, SM103 and SM107. They support only the widths in their docstrings (hidden size 8192, head dim 64); check the matching supports_* helper and fall back otherwise.

gated_residual_rmsnorm(branch, residual, ...)

Gated residual RMSNorm for hidden size 8192: blend a normalized branch into the stream.

supports_gated_residual_rmsnorm(branch, ...)

Return whether tensors satisfy the SM100/SM103/SM107 fused-kernel contract and backend ("auto": the Cake package or the CuTe DSL kernel) can run the call on their device.

gated_residual_next_rmsnorm(branch, ...[, ...])

Gated residual RMSNorm plus the next RMSNorm in one launch, for hidden size 8192.

supports_gated_residual_next_rmsnorm(branch, ...)

Return whether inputs satisfy the SM100/SM103/SM107 two-norm kernel contract and backend ("auto": the Cake package or the CuTe DSL kernel) can run the call on their device.

rmsnorm_sigmoid_gate(input, gate[, eps, ...])

Per-head RMSNorm and sigmoid gate, in place, for head dim 64.

supports_rmsnorm_sigmoid_gate(dtype, head_dim)

Whether a BF16, head-dimension-64 call can run on device (the current one when None) through backend ("auto": the Cake package or the CuTe DSL kernel) in the launch form enable_pdl selects.

Training RMSNorm (Cake)

Fused BF16 RMSNorm forward / backward for training on SM100, SM103 and SM107: FP32 statistics and accumulation, one FP32 reciprocal RMS saved per row, fused dx + deterministic FP32 dw backward, optional residual-add fusion and an autograd wrapper.

cake_rmsnorm(x, w[, eps, residual, ...])

Autograd-enabled fused BF16 RMSNorm (optionally with a fused residual add).

cake_rmsnorm_train_forward(x, w, eps, *[, ...])

Fused BF16 RMSNorm forward with FP32 statistics.

cake_rmsnorm_train_backward(g, x, w, rstd, *)

Fused BF16 RMSNorm backward: dx and the FP32 weight gradient dw.

cake_rmsnorm_train_backward_workspace_bytes(...)

Bytes of the caller-owned backward workspace of width hidden on device for up to rows tokens.

cake_rmsnorm_train_backward_workspace(rows, ...)

Allocate a zero-initialized backward workspace (see ..._workspace_bytes).

CakeRMSNormFunction(*args, **kwargs)

Autograd wrapper saving only (x_or_h_new, w, rstd) for the backward.

Training SiLU-gated RMSNorm (Cake)

RMSNorm(x) * weight * SiLU(z) with a separate gate input z, forward / backward for training on SM100, SM103 and SM107: rows of 128 ([T, H, 128] or [T * H, 128]) normalized per head with FP32 statistics and gate, BF16 inputs, outputs and gradients, a weight shared by every token and head whose gradient is summed in a fixed order (repeated calls return the same bits), and an autograd wrapper that saves only its inputs.

cake_rmsnorm_silu_gate(x, z, weight[, eps])

Differentiable RMSNorm(x) * weight * SiLU(z) over the last dimension (128): CakeRMSNormSiLUGateFunction applied to the arguments.

cake_rmsnorm_silu_gate_train_forward(x, z, ...)

SiLU-gated RMSNorm forward: y = BF16(((x * rstd) * weight) * silu(z)).

cake_rmsnorm_silu_gate_train_backward(...[, ...])

SiLU-gated RMSNorm backward: dx, dz and the weight gradient.

cake_rmsnorm_silu_gate_train_backward_workspace_bytes(...)

Bytes of the backward's FP32 weight-gradient partial buffer for rows rows (T * H) on device: chunks * 128 * 4.

cake_rmsnorm_silu_gate_train_backward_workspace(...)

A caller-owned backward workspace for rows rows: an uninitialized FP32 tensor of cake_rmsnorm_silu_gate_train_backward_workspace_bytes() bytes (the backward writes every element it reads).

CakeRMSNormSiLUGateFunction(*args, **kwargs)

Autograd of cake_rmsnorm_silu_gate(): saves x, z and weight (no FP32 intermediate) and recomputes the rest in the backward, so torch.utils.checkpoint(..., use_reentrant=False) sees only the inputs as saved state.