Skip to content

RMSNorm is one kernel per direction: the backward absorbs its weight pass and the residual, the forward absorbs the add, and the MoE norm's output is recomputed rather than saved (#107 A1/A2/A5/A7) - #113

Merged
feitreim merged 4 commits into
mainfrom
rmsnorm-fusions
Aug 6, 2026

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

The RMSNorm fusions from #107: A1, A2, A5, A7.

Inductor writes one kernel for residual + rms_norm and one for
rms_norm_backward + residual_add + dW, and recomputes the MoE norm's output
in the backward rather than saving it. The trainer wrote five kernels and saved
the tensor. This is that gap, closed for the four items #107 ranked as the
RMSNorm-centered ones.

Everything crosses the layer boundary in packed bf16 (#108); every statistic,
scale and reduction stays fp32 in registers, so a boundary still costs exactly
one rounding.

What each item became

A1 — the backward is one launch. rms_norm_backward_fused_bf16 produces
the input gradient, the residual branch's add, and the weight gradient
together. A block owns NORM_BACKWARD_ROWS_PER_BLOCK rows and takes each
apart in one go: reduce the row, then immediately spend the same values on
every output while they are still in cache. The weight gradient is what forces
that shape — a column's partial sum spans rows, so it cannot live in the
register of a lane that owns one row's column. It lives in a shared
column-partial array, disjointly owned by the lane that strides onto it, and
rows-per-block becomes purely how many atomics the pass costs (one per column
per block). x and dy are read twice where the split form read them three
times, no norm_backward_inv buffer crosses between two launches, and the
gradient is never written once only to be rewritten by a separate add.

A2 — the forward absorbs the residual add. rms_norm_forward_residual_bf16
stages the summed row in shared memory on its way to the saved stream, so the
statistic and the normalized output cost no second read of it, and the
statistic is taken over exactly the rounded values the saved stream holds.

A5 — ffn_normalized is recomputed, not saved. It leaves GpuBlockActs
for a single shared GpuBlockScratch field. The forward writes it for the
router logits and the dispatch scatter; the backward writes it again from
inside the fused norm backward, which already reads the residual and the row
factor it is made of. The two router weight launches therefore follow the norm
backward instead of preceding it (dlogits is untouched in between). Twelve
saved [N, D] streams become one.

A7 — the embedding gathers and normalizes in one launch.
embedding_forward_norm_bf16, same shape as A2's kernel with the token lookup
in place of the add. The first block is told its input arrives normalized.

The final norm keeps the split path: it has no residual branch to absorb, so
rms_norm_backward_x_fast_bf16 and rms_norm_backward_weight_fast_bf16 stay
as its arm — and as the thing anything wider than NORM_MAX_COLUMNS takes.

Measured

Paired bin/train.rs at B=16 against dc6f533 in one Modal container, 30
steps with the first discarded. bin/train.rs only: a standalone harness
compiles different device code.

tokens/s MFU device memory
baseline dc6f533 81,910.3 36.37% 138.28 GiB
this branch 84,849.7 37.68% 136.21 GiB
+3.59% +1.31 pp −2.07 GiB

Per item, from the paired kernel profile (BASELINE_REF=dc6f533 ./run.sh model profile, B=12, ms per step summed over the 12 blocks):

item baseline kernels ms fused ms Δ launches
A1 (attention norm) backward.attention_norm.input + .weight + backward.attention_residual 8.373 backward.attention_norm 5.943 −2.430 −24
A1 + A5 (ffn norm) backward.ffn_norm.input + .weight + backward.ffn_residual 8.440 backward.ffn_norm 6.178 −2.262 −24
A2 forward.attention_residual + forward.ffn_norm 3.432 forward.attention_residual_norm 2.246 −1.187 −12
A7 forward.embedding + one forward.attention_norm 0.321 forward.embedding_norm 0.154 −0.167 −1
full step 747.88 743.18 −4.70 −61

A5's cost is visible inside A1: the ffn norm backward is 0.235 ms slower than
the attention one across 12 blocks, which is the extra [N, D] bf16 store of
the recomputed operand. It buys 2.07 GiB of VRAM at B=16 (1.5 GiB at the
profile's B=12: 120.2 → 118.7 GiB), so it is a good trade at any batch that is
near the ceiling and a cheap one at any batch that is not.

Registers and occupancy

B200, cuOccupancyMaxActiveBlocksPerMultiprocessor at the 256-thread launch
these kernels use. bin/profile prints this now, beside the timings, because
fusing raises liveness and a fused kernel that crosses an occupancy step can
lose despite the traffic it saves.

kernel registers spill blocks/SM
rms_norm_forward_fast_bf16 24 0 8
rms_norm_forward_residual_bf16 (new) 32 0 8
embedding_forward_norm_bf16 (new) 28 0 8
rms_norm_backward_fused_bf16 (new) 64 0 4
rms_norm_backward_x_fast_bf16 27 0 8
rms_norm_backward_weight_fast_bf16 25 0 8

The fused backward is the one that moves: 64 registers × 256 threads is exactly
a quarter of the register file, so it halves to 4 blocks/SM. It wins anyway —
0.495 ms per launch against the 0.698 the three it replaces cost between them —
because what it stops doing is a whole read of x and dy and a whole
read-modify-write of the gradient. Nothing spills.

NORM_BACKWARD_ROWS_PER_BLOCK was swept in three paired runs of the same
shape, and it is not monotone:

rows/block baseline candidate Δ MFU
8 81,688.5 83,943.7 +2.76% 37.27%
16 81,910.3 84,849.7 +3.59% 37.68%
32 81,728.0 83,555.6 +2.24% 37.10%

The three baselines span 0.27% and the three candidates span 1.5%, so 16 is a
real peak rather than a coin flip. Occupancy is register-bound at 4 blocks/SM
at all three, so the constant only trades waves against atomics.

Parity

All 14 gates pass at the unchanged 3e-3 / 3e-2 tolerances — no gate was
loosened, and none needed to be. Merging the dW reduction into the dX kernel
does change its reduction order (atomics now carry
NORM_BACKWARD_ROWS_PER_BLOCK rows instead of NORM_WEIGHT_ROWS_PER_BLOCK),
and it landed inside the existing noise floor on the first run.

The A5 recompute is bitwise identical to the forward it replaces, not merely
close: same packed input, same strided accumulation, same reduction tree, so
the same inv and the same rounded output.

What is not here

A2's other half — folding the norm into moe_gather_combine_add_quad's
epilogue, so a block's output is normalized where it is produced (≈2.25 GiB/step
and 11 launches). Not attempted and not measured. The gather is an elementwise
quad-lane kernel over N·D / QUAD_LANES threads; carrying a row reduction
means giving up that shape for a block-per-row one, and the norm it would
absorb belongs to the next block, so the block API would have to take its
successor's norm and the final norm's [NP, D] destination as well. A7 is the
same fusion at the one boundary where the producer is already row-shaped, which
is why it is here and this is not.

For the A4 agent

A5 changes where router_backward_weight_split_bf16's operand comes from —
acts.ffn_normalized (saved per block) becomes scratch.ffn_normalized
(recomputed by the norm backward). The interface is unchanged: same
Bf16Acts<N, D>, same packed row-major [N, D] layout, same
as_device_buffer(). Neither router_backward_weight_split_bf16 nor
router_backward_weight_merge is touched. The only other change on that path
is that both launches now sit after backward.ffn_norm rather than before it.
If A4 rebuilds the operand as an MN-major TMA view, the producer here can write
that instead — it is one store in one kernel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant