Skip to content

layernorm: the band was never in registers either, and the ladder's rung was wrong (#67) - #75

Merged
feitreim merged 1 commit into
mainfrom
layernorm-band-in-registers
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
layernorm-band-in-registers

Conversation

@feitreim

Copy link
Copy Markdown
Owner

#67 was filed as an occupancy problem — 255 registers with spills, 2 blocks an
SM where softmax gets 6, at near-identical shared memory. That reading is
right, 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_rows had no launcher, no CPU reference and no bench.rs row, 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_tile takes is the row statistic, and a kernel that
reduced 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 + c is exactly what
layernorm 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::value does it with one
row-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-permutation
argument 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:

error cells wrong p10 median
row displaced by 1 32,765 0.277 0.810
row displaced by 32 (a warp band) 32,738 0.256 0.776
row displaced by 128 (a CTA band) 32,620 0.118 0.501
every CTA reads band 0 16,310 0.118 0.501
third pass pinned to column chunk 0 28,672 0.155 0.560
gamma/beta pinned to column chunk 0 28,672 0.108 0.321
gamma and beta swapped 32,512 0.099 0.797
statistic over the whole tile 32,768 0.030 0.186
mean never subtracted 32,768 0.078 0.144
variance as the raw second moment 23,662 0.011 0.019

The worst row displacement over 1..=129 still leaves 31,684 of 32,768 wrong at
a 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:

  • Third pass pinned to column chunk 0 — the error this PR's own change is
    the one that can commit. Predicted 28,672; the device failed 28,672, to
    the cell.
  • Variance as the raw second moment — chosen because it is the weakest
    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 a
constant. 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:

  • Shared memory admits floor(232448 / 33344) = 6 CTAs. The driver says 2.
  • 255 x 128 = 32,640 against a 65,536-register file is 2.008.
  • After the change, the same query says 6 blocks, 24 warps at
    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 wrong
rung
, 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 ColVec parameters
and 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:

CHUNK regs spill frame blocks/SM GB/s at 8192 blocks
128 — the whole band, as filed 255 16 B 1552 2 317
64 108 0 272 6 —
32 — softmax's rung 40 0 128 6 3776
16 — shipped 47 0 0 6 5539

CHUNK = 32 is seven registers cheaper and 1.46x slower, at identical
occupancy.
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 ptxas kept the band.

Before / after

Same B200, same session, modal run modal_app.py::bench. Every size checked
before it was timed, in both columns.

rows x 128 blocks before, ms after, ms before, GB/s after, GB/s
256 2 0.0324 0.0084 4.1 15.6
1024 8 0.0328 0.0083 16.0 63.3
8192 64 0.0340 0.0084 123.3 496.5
65536 512 0.1206 0.0135 278.3 2490.7
524288 4096 0.8571 0.0549 313.2 4891.3
1048576 8192 1.6926 0.0969 317.2 5538.9

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 share
block counts and bytes moved and can be read against each other.

Controls

  1. groupnorm_tile is deliberately unchanged and did not move — 236
    registers on a 1536-byte frame, before and after. It shares this file, the
    SharedVec staging and the [32, COLUMNS] band, so its counters holding
    still 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.
  2. The mechanism was checked on a kernel this PR does not touch.
    softmax_rows at CHUNK = 16 prices at 50 registers on a 128-byte frame
    against 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).
  3. Occupancy is identical at CHUNK 16 and 32, so the 1.46x between them is
    not 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

Scope

examples/src/layernorm.rs, examples/src/bench.rs, examples/src/main.rs,
examples/README.md. softmax.rs and flash_forward.rs are untouched.

Verified: cargo fmt --check, cargo clippy --all-targets -- -D warnings and
cargo test clean locally; modal run modal_app.py::build green (clippy,
tests, cargo doc -D warnings, device builds of both crates); ::regcount,
::examples, ::bench all run on a B200. No new clippy warnings — the 12 in
the build log are pre-existing and none are in layernorm.rs.

Closes #67

…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.
@feitreim
feitreim merged commit 404304f into main Jul 28, 2026
2 checks passed
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.

layernorm_rows sits at 255 registers with spills and gets 2 blocks/SM where softmax gets 6

1 participant