Repository navigation
softmax: the rung was a quarter of it, and the exp2 polynomial was the rest (#76) - #79
Merged
Merged
Conversation
… 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.
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.
softmax_rowswas the slow kernel in the file by 5.8x, against alayernorm_rowsdoing strictly more arithmetic per element. The rung the issue suspected is real
and it is a quarter of it. The rest is the
exp2polynomial, and findingthat out required putting the arithmetic back one piece at a time rather than
reading a register table.
Four lines of executable change:
CHUNK32 -> 16,exp2->exp2_hwtwice,div_row(total)->mul_row(total.recip()). Everything else in this diff is theargument for them, because
softmax.rs's header argued for 32 at length and aconstant 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. Therow 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#60's ladder gives
[32, 32]a zero frame in all five spellings and thiskernel 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
exp2the rung is worth 950 -> 1178 GB/s. That is 1.25x of a5.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_tileover the sametiles runs 6324 GB/s at
CHUNK = 16and 3807 at 32 — and that the shipped kernelcould 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 = 16and 8192 blocks in one session. The last two rows do not computea softmax and exist only to be timed.
CHUNK = 16exp2polynomial,div_rowexp2_hw,div_rowexp2_hw,recip+mul_rowexp2callsload_tile->store_tileThe FMA
exp2polynomial is the largest single term, at 2.7x.kittens::reg::exp2_approxis a clamp, a shift-trick split and a degree-3minimax polynomial — around a dozen instructions — evaluated twice per
element over 128 elements a thread.
exp2_hwis oneex2.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.f32cost 1.4x, and #47 measured them at nothing. Thatmeasurement 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.mdrather 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 ownarithmetic 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 wastimed, in both columns.
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.f32this 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
CHUNKdoubles the chunks a row iswalked 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:
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
cis written from columnc % 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
f64host 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.
permutationspreads a chunk across the whole ladder at anodd 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_rowsis deliberately untouched and did not move — 47 registers, 0frame, 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 thischange is
softmax's arithmetic and not something the harness did.6. Measured and deliberately not shipped
exp2(x - peak)/totalisexp2(x - peak - log2(total)): fourlog2a 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.
exp2per 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 twoexp2s that "the measurement doesnot favour" the SFU, on the evidence that pointing
exp2at it takessoftmax_probe_128from 168 registers and no spill to 255 with 112 bytes ofspill. 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
exp2in an inner loop:flash_forward.rs, which has nolauncher and so nothing that could check a numerics change, and the running-max
rescale in
reg.rsitself. Whether either should move wants its own issue withits own timed probe, and another agent is in
flash_forward.rsright now. ThisPR does not touch
reg.rs— it states the contradiction insoftmax.rs'sheader and in
examples/README.mdso the note upstream is read as the registerclaim it is.
Verified
modal run modal_app.py::buildgreen (clippy, tests,cargo doc -D warnings,device builds of both crates);
::regcountbefore and after;::examplesand::benchon a B200.cargo fmt --check,cargo clippy --all-targets -D warningsandcargo testclean locally.One honest note on ordering: the Modal workspace hit its spend limit after
the four gate runs, while re-running
::builda final time. The runs above wereall taken on the exact code being shipped; every edit made after them is a
//or
//!comment, insoftmax.rsand one block inbench.rs, and the diff ofnon-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/andsrc/are untouched.Closes #76