Skip to content

softmax: the rung was a quarter of it, and the exp2 polynomial was the rest (#76) - #79

Merged
feitreim merged 1 commit into
mainfrom
softmax-floor
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
softmax-floor

Conversation

@feitreim

Copy link
Copy Markdown
Owner

softmax_rows was the slow kernel in the file by 5.8x, against a layernorm_rows
doing strictly more arithmetic per element. The rung the issue suspected is real
and it is a quarter of it. The rest is the exp2 polynomial, and finding
that out required putting the arithmetic back one piece at a time rather than
reading a register table.

Four lines of executable change: CHUNK 32 -> 16, exp2 -> exp2_hw twice,
div_row(total) -> mul_row(total.recip()). Everything else in this diff is the
argument for them, because softmax.rs's header argued for 32 at length and a
constant that no longer matches its prose is #65 in code.

1. The rung, at every rung

Built and timed at each [32, N], the shipped arithmetic, not two points. The
row extent is not a free parameter — a warp owns 32 rows of a 128-row tile — so
the column sweep is the only ladder that applies.

CHUNK regs spill frame blocks/SM GB/s at 8192 blocks
128 197 0 1024 6 278
64 40 0 512 6 554
32 — #47's answer 39 0 256 6 1004
16 — shipped 32 0 0 6 4414

#60's ladder gives [32, 32] a zero frame in all five spellings and this
kernel gets 256 bytes there — #67's finding again, and for its reason: the probe
carries no statistics across chunks. The ladder narrows the search; it does not
answer.

At the shipped exp2 the rung is worth 950 -> 1178 GB/s. That is 1.25x of a
5.8x, exactly as the issue warned. Worth noting that the rung is worth far more
than that on the memory path — a pure load_tile -> store_tile over the same
tiles runs 6324 GB/s at CHUNK = 16 and 3807 at 32 — and that the shipped kernel
could not collect it, because it was not memory-bound.

2. The rest of the 5.8x, by ablation

Same kernel, same launch geometry, one thing removed at a time, every row at
CHUNK = 16 and 8192 blocks in one session. The last two rows do not compute
a softmax and exist only to be timed.

at CHUNK = 16 regs frame GB/s what it removes
exp2 polynomial, div_row 50 128 1178 —
exp2_hw, div_row 48 0 3153 the FMA polynomial
exp2_hw, recip + mul_row 32 0 4414 124 of 128 divides
no transcendental, no divide 32 0 6042 both exp2 calls
one pass, load_tile -> store_tile 31 0 6324 the two extra passes

The FMA exp2 polynomial is the largest single term, at 2.7x.
kittens::reg::exp2_approx is a clamp, a shift-trick split and a degree-3
minimax polynomial — around a dozen instructions — evaluated twice per
element
over 128 elements a thread. exp2_hw is one ex2.approx.f32.

This is not an accuracy trade in the direction it looks. The SFU instruction
is good to about 2^-22 and the polynomial to 7.5e-5 = 2^-13.7, so the swap is the
more accurate one, and the check's worst relative error does not move at all —
1.97e-3 either way — because bf16 output rounding dominates both. Nothing was
spent to buy this.

The 128 div.rn.f32 cost 1.4x, and #47 measured them at nothing. That
measurement was not wrong. It was taken under a different binding constraint:
with the polynomial in the loop the kernel was so far from memory that removing
124 of every 128 divides moved it 952.5 -> 952.1 GB/s. With the SFU in the loop
the identical change is 3153 -> 4414. A control is only valid under the bind it
was taken in
, and this is the cleanest instance of that in the repository. The
old control's text is annotated in examples/README.md rather than deleted,
because the way it was right is the part worth keeping.

The three passes over shared memory cost 4.5% — 6324 -> 6042 GB/s for two
extra full reads of the tile plus every row reduction in the kernel. That is the
one thing #47 proposed to fix, and the one thing that was never worth fixing.

Two things checked rather than assumed: occupancy never moved (6 blocks, 24
warps at every rung but 128 and for every ablation — shared memory binds this
kernel), and nothing was put in flight, nor needed to be (one blocking TMA
each way, still; the kernel now runs at 70% of what a bare copy over the same
tiles achieves).

3. The floor was never the launch

#47 read 23 us at two blocks as "a fixed cost every launch on this harness pays".
The ablation says what it was — one session, one two-block launch, the arithmetic
put back a piece at a time: 7.0 us for the copy, 8.9 us with both extra
passes and every reduction, 11.6 us as shipped, 23.7 us at the old rung and
the old exp2. Twelve of those twenty-three microseconds were this kernel's own
arithmetic at the head of a dependent chain with two blocks on a 148-SM device.
#67 was right that the reading did not survive; this is the mechanism.

4. Before / after

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

rows x 128 x 2 planes blocks before, ms after, ms before, GB/s after, GB/s
128 2 0.0254 0.0109 5.2 12.0
512 8 0.0257 0.0107 20.4 48.9
4096 64 0.0266 0.0118 157.9 354.2
32768 512 0.0531 0.0167 632.4 2004.9
262144 4096 0.2926 0.0692 917.5 3880.0
524288 8192 0.5650 0.1223 950.3 4389.6

4.6x. Static counters 66 / 0 spill / 256 B frame -> 32 / 0 / 0, occupancy
6 blocks and 24 warps, unchanged. Never a register count quoted as a speed:
the rung that wins here also has the fewest registers, but the rung at 64
(40 registers) would have beaten 32 (39) on that column and is 1.8x slower.

The 5.8x is now 1.25x, and that residual is not a defect. It is the two
ex2.approx.f32 this kernel owes its own definition and a layernorm does not,
priced at exactly that by the ablation's fourth row (4414 against 6042).

5. Controls

The check was not taken on trust. Halving CHUNK doubles the chunks a row is
walked in, so the walk's errors are what it has to see. Both were built as
kernels and launched
over the check's 65,536 cells:

deliberate error predicted device
third pass reads the first chunk for every chunk 56,960 56,960
second pass sums the first chunk twice 65,536 65,536

The first is the misindexing this change is the one that can commit, and getting
it exact took two corrections that are precisely why it was launched rather than
tabulated. The obvious model — "column c is written from column c % CHUNK" —
predicts 57,344 and is wrong by 384, because the third pass reads and writes
the same tile: chunk 0 is overwritten before chunks 1..7 re-read it, so what
they exponentiate is a softmax output, not an input. Modelling that leaves 64.
The last 64 are cells the device's bf16 intermediate moves across the tolerance
where an f64 host model keeps them inside it. With both, the model is exact.

The second was picked as the weakest error a chunk walk can commit — one too
many trips round a loop, touching only the denominator — and it turns out not to
be weak: it fails every cell. That is the seed's row argument paying off where it
was not designed to. permutation spreads a chunk across the whole ladder at an
odd stride rather than giving it 16 adjacent values, so the first chunk always
carries a real share of the mass; the smallest denominator shift over the check's
512 rows is 4.3%, eleven times the tolerance.

layernorm_rows is deliberately untouched and did not move — 47 registers, 0
frame, 5483 -> 5497 GB/s across the two sessions. It shares the harness, the
[32, N] band walk and the block count, so its holding still is what says this
change is softmax's arithmetic and not something the harness did.

6. Measured and deliberately not shipped

  • Folding the normalizer into the exponent. exp2(x - peak)/total is
    exp2(x - peak - log2(total)): four log2 a thread and no divide at all.
    4390 / 4415 / 4462 GB/s over three sweep rounds against recip's 4363 / 4414
    — the two forms sit inside each other's run-to-run spread. Not shipped: a 1%
    a rerun erases does not buy putting log2_approx's error inside the exponent,
    where the tolerance argument would then have to carry it.
  • Parking the numerator in the tile. One exp2 per element instead of two,
    and 4670 GB/s — the fastest correct form measured here. Not shipped, and
    the reason is now a number rather than a worry: the tile is bf16, so the
    numerator rounds on the way in and the quotient rounds again, taking the
    check's worst relative error 1.97e-3 -> 3.44e-3 against a 3.91e-3
    tolerance
    . 5.8% more throughput for 12% of the headroom left is the wrong
    trade, and a tolerance sitting on its own rounding floor is not a check (layernorm: the band was never in registers either, and the ladder's rung was wrong (#67) #75).

7. A follow-up worth filing, and deliberately not done here

src/reg.rs's module header says of the two exp2s that "the measurement does
not favour" the SFU, on the evidence that pointing exp2 at it takes
softmax_probe_128 from 168 registers and no spill to 255 with 112 bytes of
spill. That is true, and it is a statement about registers. At this kernel's
shape it is 32 registers and a zero frame either way and the SFU is 2.7x
faster
— the fourth time in this repository the register column has ordered
time backwards (#47, #63, #67, now this).

Two kernels still call exp2 in an inner loop: flash_forward.rs, which has no
launcher and so nothing that could check a numerics change, and the running-max
rescale in reg.rs itself. Whether either should move wants its own issue with
its own timed probe, and another agent is in flash_forward.rs right now. This
PR does not touch reg.rs
— it states the contradiction in softmax.rs's
header and in examples/README.md so the note upstream is read as the register
claim it is.

Verified

modal run modal_app.py::build green (clippy, tests, cargo doc -D warnings,
device builds of both crates); ::regcount before and after; ::examples and
::bench on a B200. cargo fmt --check, cargo clippy --all-targets -D warnings and cargo test clean locally.

One honest note on ordering: the Modal workspace hit its spend limit after
the four gate runs, while re-running ::build a final time. The runs above were
all taken on the exact code being shipped; every edit made after them is a //
or //! comment, in softmax.rs and one block in bench.rs, and the diff of
non-comment lines is the four lines quoted at the top.

Scope: examples/src/softmax.rs, examples/src/bench.rs (one comment),
examples/README.md. flash_forward.rs, gemm.rs, layernorm.rs,
device-tests/ and src/ are untouched.

Closes #76

… rest (#76)

CHUNK 32 -> 16, exp2 -> exp2_hw, div_row -> recip + mul_row. Four lines of
code; the rest is the argument for them.

950.3 -> 4389.6 GB/s at 8192 blocks, 66 registers on a 256-byte frame -> 32
on a zero frame, occupancy unmoved at 6 blocks and 24 warps an SM. Every size
checked before it was timed; layernorm_rows untouched beside it at 5483 ->
5497 GB/s as the control.
@feitreim
feitreim merged commit bb9d8d6 into main Jul 28, 2026
2 of 3 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.

softmax is now the slow kernel by 5.8x, and its CHUNK rung is only a quarter of it

1 participant