Repository navigation
layernorm: the band was never in registers either, and the ladder's rung was wrong (#67) - #75
Merged
Merged
Conversation
…ung was wrong (#67) layernorm_rows had no launcher, no CPU reference and no bench row, so it gets those first: a seed whose row statistics genuinely vary (a softmax-style permutation seed makes groupnorm_tile compute layernorm's answer), an exhaustive error-class table, and two of those classes built as kernels and launched. Diagnosis is the occupancy query, not the 255: shared memory admits 6 CTAs at 33,344 bytes and the driver said 2, which is 255 x 128 against a 65,536 register file. After the change the same query says 6 at byte-identical shared memory. The fix is CHUNK, but not softmax's CHUNK. The ladder says [32, 32] has a zero frame; this kernel gets 128 bytes there, because the probe carries no ColVec parameters and no statistics across chunks. CHUNK = 16 is the zero-frame rung. CHUNK = 32 is seven registers cheaper and 1.46x slower at identical occupancy. 317 -> 5539 GB/s at 8192 blocks. groupnorm_tile is deliberately unchanged and its counters do not move, which is the control.
This was referenced Jul 28, 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.
#67 was filed as an occupancy problem — 255 registers with spills, 2 blocks an
SM where
softmaxgets 6, at near-identical shared memory. That reading isright, and it is half the defect. The other half is that at 255 registers and
a 1552-byte frame the band was not in registers either, so this is not the
mirror image of #47 so much as #47 plus an occupancy cliff on top.
The harness came first
layernorm_rowshad no launcher, no CPU reference and nobench.rsrow, so"faster" and "wrong faster" were the same number. That is fixed before anything
is optimized, and the seed is not a copy of
softmax's — it could not be.A permutation seed gives every row the same 128 values in a different order.
That makes every row's mean and variance identical, which means the whole-tile
statistic
groupnorm_tiletakes is the row statistic, and a kernel thatreduced over the wrong axis would compute the right answer. So would one that
never subtracted a mean, if the mean were zero. Neither is hypothetical — they
are the two kernels in this file.
Nor can the row simply scale or shift its values:
x -> a·x + cis exactly whatlayernorm divides out, so a seed of that form makes every row's output
bit-identical and the check blind to every row error there is. The row has to
change the shape of the distribution.
layernorm::valuedoes it with onerow-dependent power-of-two boost applied to a third of an asymmetric ladder;
mean and variance both move with the class (8.9/28.9, 14.8/53.8, 26.7/105.7)
while the ladder stays 128 distinct values, so
softmax's row-permutationargument still stacks on top of it.
Scored exhaustively over the check's 32,768 cells. "p10" is the tenth-percentile
relative error among the cells an error makes wrong, so nine tenths are worse:
gamma/betapinned to column chunk 0gammaandbetaswappedThe worst row displacement over
1..=129still leaves 31,684 of 32,768 wrong ata p10 of 9x the tolerance. The two bold rows are the ones the seed exists for,
and a
softmax-shaped seed scores zero on both.A table computed by the reference's own functions is an argument, not a
control, so two rows were built as kernels and launched:
the one that can commit. Predicted 28,672; the device failed 28,672, to
the cell.
row in the table, at a p10 of 1.4x the tolerance. If the check sees this one
it sees all of them. Predicted 23,662; the device failed 24,410, the 3%
being cells sitting on the tolerance that the device's fp32/bf16 path moves
across it where the host model is exact
f64.And the control that matters most for trusting the numbers below: the old
whole-band kernel passes this check at all six benchmark sizes. The check is
not tuned to the rewrite.
One correction to inherited reasoning
The tolerance is 2⁻⁷, not
softmax's 2⁻⁸, and the 2⁻⁹ that file quotes is not aconstant. A bf16 has eight significand bits, so half an ulp relative is
2⁻⁸/m— 2⁻⁹ only when the significand is near 2, a full 2⁻⁸ when it is near 1.Softmax's outputs sit at the forgiving end; these do not (the smallest is 0.552,
significand 1.104), and the run measures a worst relative error of 3.87e-3 =
0.99 x 2⁻⁸. That is the rounding floor, not an error, and a 2⁻⁸ tolerance
would have been sitting on it. 2⁻⁷ is one doubling above the floor — the same
factor of two of headroom softmax has, arrived at rather than inherited.
Diagnosis: registers, and the query says so rather than the 255
The hypothesis is "registers cap occupancy". Confirmed with the query, not
inferred from the number:
floor(232448 / 33344) = 6CTAs. The driver says 2.255 x 128 = 32,640against a 65,536-register file is 2.008.byte-identical shared memory — 33,344 either way. Occupancy moved with
nothing but the register count.
The fix, and the rung the ladder got wrong
The suspected fix was
softmax's: a[32, 32]chunk walk. It is the wrongrung, and finding that out is the part worth carrying forward.
The
[32, 32]rung has a zero frame in all five spellings of #60's probe.This kernel gets 128 bytes there — the probe carries no
ColVecparametersand no statistics across chunks, and this kernel's extra live state moves its
cliff one rung down. Built and priced at every rung rather than argued:
CHUNKCHUNK = 32is seven registers cheaper and 1.46x slower, at identicaloccupancy. Those seven registers are the band not fitting. That is the third
time in this repository the register column has ordered time backwards (#47,
#63, now this), and the cleanest instance of it: the two rungs differ in nothing
but whether
ptxaskept the band.Before / after
Same B200, same session,
modal run modal_app.py::bench. Every size checkedbefore it was timed, in both columns.
17.5x. Static counters: 255 / 16 B spill / 1552 B frame -> 47 / 0 / 0.
Occupancy: 2 blocks, 8 warps -> 6 blocks, 24 warps. Sizes are twice
softmax's rows against one plane rather than two, so the two tables shareblock counts and bytes moved and can be read against each other.
Controls
groupnorm_tileis deliberately unchanged and did not move — 236registers on a 1536-byte frame, before and after. It shares this file, the
SharedVecstaging and the[32, COLUMNS]band, so its counters holdingstill is what says the change is the band walk and not something the module
did. It is left alone because it has no launcher and no reference, and a
rewrite nothing can check is not an improvement.
softmax_rowsatCHUNK = 16prices at 50 registers on a 128-byte frameagainst its shipped 66 on 256, and runs 1185 against 950 GB/s — half
the frame, a quarter more throughput, no arithmetic changed. So "frame down,
time down" is not a property of this rewrite. That probe is not in this
PR (see below).
CHUNK16 and 32, so the 1.46x between them isnot occupancy. It is the local-memory traffic itself — Is a streamed local-memory band actually slower? ptxas cannot say, and the ladder now makes the question concrete #63's answer read the
other way round: a streamed band is cheap when the registers it frees buy
resident warps, and this kernel is past the point where they buy anything.
Two things this corrects
softmax's 23 µsfloor at two blocks as "a fixed cost every launch on this harness pays".
layernormreaches 8.4 µs on the same harness. Whatever that 23 µs is, itis not the launch.
softmaxis now the slow kernel in this file by 5.8x at equal bytes andequal blocks. Control 2 says a one-constant change recovers part of it. It is
not shipped here:
softmax.rs's header argues for 32 at length, softmax is latency-bound, not bandwidth-bound: 362 GB/s ceiling and a flat 51us floor #47 isclosed, and rewriting it wants its own issue with its own controls rather than
a drive-by in a layernorm PR. Worth filing.
Scope
examples/src/layernorm.rs,examples/src/bench.rs,examples/src/main.rs,examples/README.md.softmax.rsandflash_forward.rsare untouched.Verified:
cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo testclean locally;modal run modal_app.py::buildgreen (clippy,tests,
cargo doc -D warnings, device builds of both crates);::regcount,::examples,::benchall run on a B200. No new clippy warnings — the 12 inthe build log are pre-existing and none are in
layernorm.rs.Closes #67