Repository navigation
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
Conversation
…orward absorbs the add, the MoE norm output is recomputed not saved
This was referenced Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The RMSNorm fusions from #107: A1, A2, A5, A7.
Inductor writes one kernel for
residual + rms_normand one forrms_norm_backward + residual_add + dW, and recomputes the MoE norm's outputin 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_bf16producesthe input gradient, the residual branch's add, and the weight gradient
together. A block owns
NORM_BACKWARD_ROWS_PER_BLOCKrows and takes eachapart 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).
xanddyare read twice where the split form read them threetimes, no
norm_backward_invbuffer crosses between two launches, and thegradient is never written once only to be rewritten by a separate
add.A2 — the forward absorbs the residual add.
rms_norm_forward_residual_bf16stages 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_normalizedis recomputed, not saved. It leavesGpuBlockActsfor a single shared
GpuBlockScratchfield. The forward writes it for therouter 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 (
dlogitsis untouched in between). Twelvesaved
[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 lookupin 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_bf16andrms_norm_backward_weight_fast_bf16stayas its arm — and as the thing anything wider than
NORM_MAX_COLUMNStakes.Measured
Paired
bin/train.rsat B=16 againstdc6f533in one Modal container, 30steps with the first discarded.
bin/train.rsonly: a standalone harnesscompiles different device code.
dc6f533Per item, from the paired kernel profile (
BASELINE_REF=dc6f533 ./run.sh model profile, B=12, ms per step summed over the 12 blocks):backward.attention_norm.input+.weight+backward.attention_residualbackward.attention_normbackward.ffn_norm.input+.weight+backward.ffn_residualbackward.ffn_normforward.attention_residual+forward.ffn_normforward.attention_residual_normforward.embedding+ oneforward.attention_normforward.embedding_normA5'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 ofthe 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,
cuOccupancyMaxActiveBlocksPerMultiprocessorat the 256-thread launchthese kernels use.
bin/profileprints this now, beside the timings, becausefusing raises liveness and a fused kernel that crosses an occupancy step can
lose despite the traffic it saves.
rms_norm_forward_fast_bf16rms_norm_forward_residual_bf16(new)embedding_forward_norm_bf16(new)rms_norm_backward_fused_bf16(new)rms_norm_backward_x_fast_bf16rms_norm_backward_weight_fast_bf16The 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
xanddyand a wholeread-modify-write of the gradient. Nothing spills.
NORM_BACKWARD_ROWS_PER_BLOCKwas swept in three paired runs of the sameshape, and it is not monotone:
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_BLOCKrows instead ofNORM_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
invand the same rounded output.What is not here
A2's other half — folding the norm into
moe_gather_combine_add_quad'sepilogue, 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_LANESthreads; carrying a row reductionmeans 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 thesame 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) becomesscratch.ffn_normalized(recomputed by the norm backward). The interface is unchanged: same
Bf16Acts<N, D>, same packed row-major[N, D]layout, sameas_device_buffer(). Neitherrouter_backward_weight_split_bf16norrouter_backward_weight_mergeis touched. The only other change on that pathis that both launches now sit after
backward.ffn_normrather 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.