Repository navigation
softmax: the band was never in registers, and that was the 362 GB/s (#47) - #66
Merged
Merged
Conversation
) #47 was filed as a latency problem — a flat ~51 us floor out to 64 blocks and a 362 GB/s ceiling, read off the kernel's one blocking TMA load and one blocking TMA store as "nothing is ever in flight". The register table says otherwise before any device is involved. `regcount` now prices the examples crate as well as the harness, and it priced `softmax_rows` at 39 registers on a 1024-byte stack frame. 39 registers cannot hold the 128 fp32 a thread of a `[32, 128]` band owns: ptxas never promoted it, and every pass over the band was a round trip to the same hierarchy the TMA was already using. The register column alone reads that as the cheapest kernel in the file, which is what the stack frame is printed beside it to catch (#60). The fix is one number. `CHUNK = 32` walks the row four chunks at a time — the widest `[32, N]` rung on the ladder with a zero frame — and carries `row_max`/`row_sum` across the chunks. The launch geometry, the barrier protocol and both TMA waits are untouched, so the swap is controlled: only where the band lives changed. 66 registers on a 256-byte frame. rows x 128 x 2 blocks before after 128 2 2.5 5.7 GB/s 512 8 10.2 22.4 4096 64 81.2 174.8 32768 512 280.6 646.9 262144 4096 346.9 922.9 524288 8192 367.1 952.1 Same B200, same session, every size checked before it was timed. The floor went with it: 52.3 us -> 23.0 us at two blocks, against the GEMM's own 22.3 us at two blocks, so what is left there is a cost every launch pays. Three controls, since a number that moves is not an explanation: - Occupancy did not change. `main` now prints `cuOccupancyMaxActiveBlocksPerMultiprocessor`: 6 blocks and 24 warps an SM, shared-memory-capped either way, and neither 39 nor 66 registers a thread binds it. `layernorm_rows` reads 2 at nearly the same shared memory, which is its 255 registers — so the query is register-sensitive and the 6 is not a constant. - It is not instruction issue. Replacing `div_row`'s 128 `div.rn.f32` a thread with one `recip` per row and a `mul_row` measured 952.5 against 952.1 GB/s, and worse in the middle of the sweep. It is not in the tree. - The check sees the column walk this introduced. Pinning the third pass to column 0 fails 65,408 of the check's 65,536 cells by up to a relative 1.0. The residual is stated rather than rounded off: 952 GB/s is still well under HBM, it is not latency and not issue, and at 24 of 64 warp slots what is left points at bytes per CTA — a launch-geometry change and a different issue. Also fixes the two clippy warnings in the files touched (`manual_is_multiple_of` and `neg_cmp_op_on_partial_ord`, the latter with an allow and the reason: the negated form is the only one that counts a NaN as wrong).
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.
#47 was filed as a latency problem: a flat ~51 µs floor out to 64 blocks and a
362 GB/s ceiling, read off the kernel's one blocking TMA load and one blocking
TMA store as "nothing is ever in flight." The symptom is real; the cause is
not that.
Diagnosis
regcountnow prices the examples crate as well as the harness — a registercount is half an occupancy argument and the other half is a launch, and probes
have no launch. It priced
softmax_rowsat 39 registers on a 1024-byte stackframe.
39 registers cannot hold the 128 fp32 a thread of a
[32, 128]band owns.ptxasnever promoted the band; it left it addressable in local memory, andevery pass over it was a round trip to the same hierarchy the TMA was already
using. The register column alone reads that as the cheapest kernel in the
file, which is exactly what #60 put the stack frame beside it to catch.
The ladder from #60 also names the fix:
[32, 32]is the widest[32, N]rungwith a zero frame. So the row is walked four chunks at a time and
row_max/row_sumcarry across them. The launch geometry, the barrierprotocol and both TMA waits are untouched — the only thing that changed is
where the band lives. 66 registers on a 256-byte frame.
Before / after
Same B200, same session,
modal run modal_app.py::bench. Every size checkedagainst the CPU reference before it was timed; nothing here is a number from an
unverified launch.
The flat floor went with it: 52.3 µs → 23.0 µs at two blocks, against the
GEMM's own 22.3 µs at two blocks. What remains there is a fixed cost every
launch on this harness pays, not something
softmaxdoes — and that is themeasurement that rules out the launch/tail hypothesis, since it bounds how much
of the floor could ever have been launch overhead.
Controls
A number that moves is not yet an explanation. Each of these would fail if the
account above were wrong.
mainnow printscuOccupancyMaxActiveBlocksPerMultiprocessorper kernel: 6 blocks, 24warps an SM, shared-memory-capped, and neither 39 nor 66 registers a
thread is the binding constraint. The 2.6× arrived with occupancy held
constant. The same table reads 2 blocks for
layernorm_rowsat nearlyidentical shared memory — that is its 255 registers — so the query is
demonstrably register-sensitive and the 6 is not a constant it prints for
everything.
div_row's 128div.rn.f32athread with one
recipper row and amul_row— four divides instead of128 — measured 952.5 against 952.1 GB/s, and slightly worse in the
middle of the sweep. It is not in the tree, and the comment saying so is.
polices rows; this change moves columns. Pinning the third pass to column
0 fails 65,408 of the check's 65,536 cells, by up to a relative 1.0
against a 3.9e-3 tolerance. Correctness at the shipped kernel is unchanged
at 1.97e-3 worst relative error — the same figure as before, because the
third pass re-reads the input and recomputes
exp2rather than parking abf16 numerator and dividing it, which would round twice and spend the whole
2⁻⁸ tolerance doing so.
What the issue got wrong, and what is left
The issue's proposed fixes — multiple tiles per CTA through
SharedTileRing,tma_store_wait_readoverlapping the store — are not in this PR, and theevidence says they were not the problem. There is still exactly one TMA load
and one TMA store per CTA and both still block, and it is 2.6× faster. Six
resident CTAs were always six loads in flight per SM.
The residual is stated rather than rounded off: 952 GB/s is still well under
HBM. It is not latency (nothing was put in flight) and not issue (control 2).
At 24 of 64 warp slots, shared-memory-capped, what is left points at bytes per
CTA — which is a launch-geometry change and a different issue.
Also
modal_app.py::regcountbuilds and prices both PTX-emitting crates; the--determinismcontrol covers both (identical: 133 kernels, every counter,across two builds).
examples/README.mdgets the write-up, under the register ladder, becausethat is where the cause turned out to live.
softmax.rs, which this PR touches anyway:manual_is_multiple_of, andneg_cmp_op_on_partial_ordwith anallowand the reason —!(error <= TOLERANCE)is the only spelling that counts a NaN as wrong. The remainingfive are in
bench.rs/gemm.rsand are untouched.Verified:
cargo fmt --checkclean in all three crates,modal run modal_app.py::buildgreen (clippy, tests,cargo doc -D warnings,device builds of both crates),
::regcount --determinism,::examples,::bench.Closes #47