flashinfer.norm¶
Kernels for normalization layers.
|
Root mean square normalization. |
|
Root mean square normalization + fp8 quantization. |
|
Fused add root mean square normalization. |
|
Fused add root mean square normalization + fp8 quantization. |
|
Fused add-residual + RMSNorm + 1x128 fp8 block quantization, in one pass. |
|
Gemma-style root mean square normalization. |
|
Gemma-style fused add root mean square normalization. |
|
Layer normalization. |
|
Layer normalization + fp8 quantization. |
|
Fused RMSNorm + SiLU activation. |
|
Fused QK RMSNorm + 3D RoPE + V copy for video generation DIT self-attention. |
Fused residual + LayerNorm + scale/shift for DIT self-attention. |
|
Fused gate + residual + LayerNorm + scale/shift for DIT self-attention. |
|
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 for hidden size 8192: blend a normalized branch into the stream. |
|
Return whether tensors satisfy the SM100/SM103/SM107 fused-kernel contract and |
|
Gated residual RMSNorm plus the next RMSNorm in one launch, for hidden size 8192. |
|
Return whether inputs satisfy the SM100/SM103/SM107 two-norm kernel contract and |
|
Per-head RMSNorm and sigmoid gate, in place, for head dim 64. |
|
Whether a BF16, head-dimension-64 call can run on |
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.
|
Autograd-enabled fused BF16 RMSNorm (optionally with a fused residual add). |
|
Fused BF16 RMSNorm forward with FP32 statistics. |
|
Fused BF16 RMSNorm backward: |
Bytes of the caller-owned backward workspace of width |
|
Allocate a zero-initialized backward workspace (see |
|
|
The row-count windows of one route and the program each launches. |
|
The weight dtypes (tokens of |
|
Autograd wrapper saving only |
Training RMSNorm, opt-in non-deterministic weight gradient (Cake)¶
The names the training RMSNorm had before the deterministic package took its
det names. Every entry point takes deterministic (default True):
deterministic=Trueruns the deterministic package above and returns its bits.deterministic=Falseruns this package’s backward in the form its registry lists for each token-count window (the backward’s docstring lists the windows). In the windows that ship the single-kernel form, one kernel per call computesdxrow by row and adds thedwsums of its CTAs into FP64 sums in the workspace, in the order the CTAs finish:dwis not guaranteed bitwise reproducible;dxis, and it is within one BF16 ulp ofmax(|dx|, rms(dx))of the deterministic backward’sdx(not always bitwise equal to it). In the windows the backward’s notes name, a twosum single kernel keeps each thread’sdwcolumn sums exact (error-free FP32 products, TwoSum compensation) before the FP64 additions, so itsdwerror does not grow with the token count. The other windows run the deterministic package’s backward pair (main kernel and fixed-order reduce), rendered into this package. The backward workspace (cake_rmsnorm_train_backward_workspace_bytes(..., deterministic=False)) has one layout with fixed offsets: the pair’s counters, the single kernel’s counters and FP64 column sums and, for the pair windows, the partial rows. Zeroed once, it serves any sequence ofdeterministic=Falsecalls it is large enough for with no host write: the kernels leave the counters at rest and the sums at zero. It serves calls of one mode.Under
torch.use_deterministic_algorithms(True),deterministic=FalseraisesRuntimeError. Withwarn_only=Trueit warns; the forward runs this package’s (deterministic) forward programs and the backward runs the deterministic package with its own workspace (a caller’sworkspaceis not used), or raises oneRuntimeErrorwhen that package cannot serve the call ([..., H]inputs, an FP32 weight, a width or residual route it does not export). The workspace helpers withdeterministic=Falsewarn and return the deterministic package’s workspace in that mode.
|
Autograd-enabled fused BF16 RMSNorm (optionally with a fused residual add). |
|
Fused BF16 RMSNorm forward with FP32 statistics. |
|
Fused BF16 RMSNorm backward: |
Bytes of the caller-owned backward workspace of width |
|
|
Allocate a zero-initialized backward workspace (see |
|
Autograd wrapper saving only |
|
Whether |
Name ( |
Deterministic name (always deterministic) |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
Differentiable |
SiLU-gated RMSNorm forward: |
|
SiLU-gated RMSNorm backward: |
|
|
Bytes of the backward's FP32 weight-gradient partial buffer for |
A caller-owned backward workspace for |
|
|
Autograd of |
Training SiLU-gated RMSNorm, opt-in non-deterministic weight gradient (Cake)¶
The names the training SiLU-gated RMSNorm had before the deterministic package
took its det names. Every entry point takes the keyword deterministic
(default True; a value that is not a bool raises TypeError), and
CakeRMSNormSiLUGateFunction takes it as its last forward input:
deterministic=Trueruns the deterministic package above and returns its bits.deterministic=Falseruns the standalone non-deterministic package (flashinfer.cake_rmsnorm_silu_gate_train_backend, whose compute and workspace entry points takedeterministic=Falseonly): the same forward kernel (up to 12 rows per SM on SM100 and SM103 and up to 59 on SM107, a forward with 16 threads to a row, and from 976 rows per SM on SM100 and SM103 and from 800 on SM107, a forward whose CTAs each walk several row blocks: the same bits) and a one-kernel backward:dxanddzare the deterministic backward’s bits, and the CTAs take row blocks (each CTA its own block when the grid has a CTA for every block; past one wave of CTAs they claim the blocks in chunks from the workspace’s claim counter) and add their FP64 weight-gradient column sums into FP64 accumulators of the workspace in the order they finish. The claimed blocks, and so the FP32 partial sums, and that order depend on scheduling: the weight gradient can differ between calls, always within the deterministic backward’s error bound (for FP32 normal gates, terms and sums; with subnormal values both backwards can leave it). Up to 256 rows the backward is one thread-block cluster of 16 CTAs that adds the CTAs’ partial sums in a fixed order on chip: the same bits on every call, within the same bound, and no workspace. The workspace (cake_rmsnorm_silu_gate_train_backward_workspace(..., deterministic=False), a few KiB whatever the token count) is zero after every call: zero it once and reuse it, one per concurrently running stream. A buffer that anything else wrote into is zeroed withcake_rmsnorm_silu_gate_train_reset_workspace(..., deterministic=False).Under
torch.use_deterministic_algorithms(True),deterministic=FalseraisesRuntimeErrorfrom the compute, workspace and autograd entry points; withwarn_only=Truethey warn and run the deterministic kernels. The backward of adeterministic=Falseautograd node that runs after the mode was enabled raises PyTorch’s alert (warn_only=True: a warning, then the non-deterministic backward). The support query and the workspace reset do not depend on that setting.
|
Differentiable |
|
SiLU-gated RMSNorm forward: |
|
SiLU-gated RMSNorm backward: |
Bytes of the caller-owned backward workspace for |
|
A caller-owned backward workspace for |
|
Return a caller-owned backward workspace to its first-use state. |
|
|
Autograd of |
Whether |