Skip to content

A second GEMM, warp-specialized at one CTA per SM — and the second CTA was worth more (#112) - #112

Merged
feitreim merged 2 commits into
mainfrom
gemm-ws
Jul 29, 2026
Merged

feitreim merged 2 commits into
mainfrom
gemm-ws

Conversation

@feitreim

Copy link
Copy Markdown
Owner

A second GEMM kernel at a different design point, in examples/src/gemm_ws.rs. examples/src/gemm.rs is untouched — this repo keeps losers on purpose, and this one is a loser at the sizes that matter.

The design

NVIDIA's gemm_sol_final (cuda-oxide at our pinned b099f64, crates/rustc-codegen-cuda/examples/gemm_sol_final/{src/kernels.rs,README.md}) in our idiom: 6 warps per CTA — 0–3 epilogue, 4 TMA, 5 pair-UMMA — and two accumulator stages in TMEM, so the MMA warp fills stage s while the epilogue warps drain stage 1-s from the item before.

Two stages × 256 columns is 512 columns, the whole SM's tensor memory, so 1 CTA/SM falls out of the double-buffer rather than being imposed. The shared plan (131 176 B) lands on the same 1. Where gemm.rs gets its overlap across CTAs (2/SM, one CTA's epilogue against another's MMA), this gets it inside one CTA.

The result — a loss

One B200 session, min of 30 timed launches, every row checked element-by-element against the same CPU reference before it was timed, control re-measured in the same container (#98/#109):

shape gemm ws, 4 stages ws, 6 stages best vs gemm
4096³ 951.9 988.0 1002.3 +5.3%
8192³ 1492.9 1390.1 1451.5 −2.8%
16384³ 1669.1 1536.5 1547.6 −7.3%

TFLOP/s. Against cuBLASLt in the same container (1569.8 / 1828.9 / 1904.6): gemm 0.606 / 0.816 / 0.876, this 0.629 / 0.760 / 0.807. CLC is a further +0.6% / +1.0% / +1.2% and changes no sign.

A small win where the launch is short, a growing loss where it is not. The 4096³ row is not a wave artifact: both kernels tile C identically and both run at 86.5% wave efficiency there.

Registers / spill: 168 and no spill (gemm_cg2 is 166). Counted residency: device-tests' tmem residency census cg2 512 rung reads 1 holding / 2 resident; the extra CTA is parked in a blocking tcgen05.alloc at the census's 32 B plan and is not admitted at all at ours.

Where the loss is, and why the scope discipline paid

Two facts narrow it. The 255-register headroom this design point buys is real and inert — nothing asked for it, and DRAIN_N = 128 is a 255-register architectural limit rather than an occupancy one. And six pipeline stages, which tensor memory has already made free, are worth +1.4% / +4.4% / +0.7% — so the K pipeline was mildly starved and is not where the 7% is.

What is left is that an SM holding one CTA has nothing else to hide the epilogue behind. #108 priced this epilogue at 21.4 µs a tile serial, 20.2% of an 8192³ launch — more than enough to be the whole gap, and exactly the thing gemm.rs's second CTA covered for free.

Which is the argument for having kept kittens::global::store_rows byte-identical, as briefed: the stmatrix follow-up (kittens::epilogue::StoreRing, #111, plus the shared→global mover in flight) now has a number to beat — about 7% at 16384³ before this design point is level — instead of a general hope, and it will be measured on the kernel that has ~100 KB of shared memory to spend on it.

Findings about the abstractions

  • src/pipeline.rs is unmodified. run, run_stealing, ClcQueue and grouped all carried over; the rewrite is inside work(), as Job::work's doc promises. The per-item cluster_sync turns out to be exactly the rendezvous the accumulator ping-pong needs, at exactly the right scope — Lcsf's finding again.
  • What the trait cannot express is a warp-asynchronous item stream. The reference's warp groups sit at different items at once via TILE_INFO/TILE_READY; a Job is entered by every thread with one item, so the epilogue here lags the MMA by exactly one item where the reference allows two. That is the price of the trait's guarantee that barriers are re-armed per item — and the same guarantee is why our shape contract is k % 64 == 0 where the reference needs k % 256 == 0. Contract: m % 256, n % 256, k % 64.
  • The epilogue needed no modification at 6 warps. Warps 0–3 still cover 128 accumulator rows at 32 each; store_rows is not warp-collective, so warps 4–5 simply do not enter it.

Files

  • examples/src/gemm_ws.rs — new; module docs carry the mechanism, the numbers and the negative.
  • examples/src/main.rs — pub mod, a status row, a checks() row, and a -- ws arm.
  • modal_app.py — one additive ws_bench entry point (bench's --case routes through bench.rs, which is another agent's file).

Not touched: examples/src/gemm.rs, examples/src/bench.rs, src/pipeline.rs, src/global.rs, src/. No examples/README.md §7 row — that file has an ablation-ladder change in flight; the table above is a §7 row waiting to land.

Gates run

scripts/modal-run build clean (the post-monomorphization asserts fire only there) · scripts/modal-run examples — gemm_ws exact at 512×512×256 and 4096×4096×256, both schedulers, both depths, three traversal widths · scripts/modal-run regcount · scripts/modal-run device_tests · scripts/modal-run ws_bench · scripts/fmt --check clean on all three manifests.

feitreim added 2 commits July 28, 2026 22:50
…per SM

examples/src/gemm_ws.rs is NVIDIA's gemm_sol_final (cuda-oxide b099f64,
crates/rustc-codegen-cuda/examples/gemm_sol_final/) in this repo's idiom: six
warps to a CTA -- 0-3 epilogue, 4 TMA, 5 pair-UMMA -- and two accumulator
stages in tensor memory, which is 512 columns and therefore one CTA an SM.

examples/src/gemm.rs is untouched. Its overlap comes from the second CTA;
this one's comes from the warp split plus the double-buffered accumulator,
and the point of the file is that the two can be A/B'd.

The epilogue is deliberately kittens::global::store_rows, unchanged, so the
delta is the occupancy/specialization structure and not a store path. It
needed no modification at six warps: warps 0-3 still cover 128 accumulator
rows at 32 each and store_rows is not warp-collective.

src/pipeline.rs is unmodified -- run, run_stealing, ClcQueue and grouped all
carry over, and the rewrite is inside work().
One B200 session, min of 30 timed launches, every row checked first:

  shape    gemm     ws s4    ws s6    best vs gemm
  4096^3   951.9    988.0    1002.3   +5.3%
  8192^3  1492.9   1390.1    1451.5   -2.8%
  16384^3 1669.1   1536.5    1547.6   -7.3%

TFLOP/s. Against cuBLASLt in the same container that is 0.876 -> 0.807 at
16384^3. CLC adds +0.6/+1.0/+1.2% and changes no sign.

Two facts narrow the mechanism. regcount says 168 registers and no spill
against gemm's 166 -- the 255-register headroom this design point buys is
real and nothing asked for it. And six pipeline stages, which tensor memory
has already made free, are worth +1.4/+4.4/+0.7% -- so the K pipeline was
mildly starved and is not where the 7% is.

What is left is that an SM holding one CTA has nothing else to hide the
epilogue behind, where gemm's second CTA covered it for free. #108 priced
that epilogue at 20.2% of an 8192^3 launch serial. Which is the argument for
having kept it identical: the stmatrix follow-up now has a number to beat
rather than a hope.

Counted residency: device-tests' cg2 512 rung reads 1 holding / 2 resident;
the second CTA is admitted and parked in tcgen05.alloc there and is not
admitted at all here, because this declares 131176 B where the census
declares 32.
@feitreim feitreim changed the title A second GEMM: warp-specialized, one CTA per SM — and it loses (#?) A second GEMM, warp-specialized at one CTA per SM — and the second CTA was worth more (#112) Jul 29, 2026
@feitreim
feitreim merged commit 7891955 into main Jul 29, 2026
3 checks passed
feitreim added a commit that referenced this pull request Jul 29, 2026
…and the epilogue was never gemm_ws's problem (#118) (#119)

`.x8` LDTM and `stmatrix.x4` on `gemm_ws`, plus the two `no drain` controls the
ladder is subtracted from, and the result is a lever that pays and a hypothesis
that dies.

The levers. Against #116's `staged`, `.x8` is +15.7% / +6.2% / +3.0% at
4096/8192/16384 cubed and `.x4` is +1.2% / +0.0% / +0.1% — the same clean null
it was on `gemm_cg2`. They do not compose here: `staged8` and `staged84` are
within 1.1% and trade places between sessions, and the register column said so
in advance, `.x4` recovering 2 registers (94 -> 92) where on `gemm_cg2` it
recovered 14 (94 -> 80) and that recovery *was* the composition gain. Best rung
is +19.4% / +9.5% / +5.2% over the shipped register epilogue, 0.615 -> 0.726,
0.782 -> 0.856, 0.810 -> 0.852 of cuBLASLt. Zero spill in every rung; 94 is the
largest count in the file against a reachable 255.

#117's mechanism predicted the shortfall and got it. `.x8` removes a latency
the drain never overlaps; this kernel already overlaps it, so `.x8` is worth
67%, 72% and 83% of what it was worth on `gemm_cg2`. By `whole - no drain` the
same epilogue is exposed at 8.62 us a tile here against #114's 20.43, and its
LDTM half at 3.20 against #117's 8.07 — 40-47%, which is what warp
specialization and the accumulator ping-pong buy, stated as a number for the
first time.

"Twice the LDTM (16 against 8)" is true of the PTX and false of the work. Every
census column doubles, not just LDTM, because `gemm_ws` emits its epilogue at
two call sites (`Ws::work` and `Ws::finish`) where the fused job has one. The
bands are `RegTile<32, 64>` in both files.

The floor is the finding. `gemm_ws_no_drain` — the shipped kernel with the
epilogue deleted, the probe that put `gemm_cg2` at 1850 TF/s against the
library's 1808 — reaches 0.829 / 0.923 / 0.888 of cuBLASLt, and at 8192 and
16384 cubed it is *slower than `gemm`'s complete `staged84` launch*. Best
against best is -9.7% / -9.4% / -6.9%; deleting the epilogue outright recovers
12.7 / 7.2 / 3.9 of those points. So no, `gemm_ws` does not beat `gemm`, and it
could not have: #112's 7.3% closes to 6.9% after both kernels spent everything
#116 and #117 found. The deficit is the multiply and the operand stream at one
CTA an SM. #112 blamed the peer CTA and #114 refuted it; `gemm_ws.rs` blamed the
missing peer CTA and this refutes that, by deleting the epilogue and losing
anyway. The stale paragraph is corrected in place rather than removed.

Residency counted, not queried (#77): two new census rungs at this kernel's own
131 176 B and 147 584 B read 1 resident, 1 holding at both, closing the gap the
`cg2 512` rung left at a 32 B plan (1 holding, 2 resident, 100.9 us parked in a
blocking alloc; 0.9 us here).

No default moved. `staged8` is the rung to ship on `gemm_ws` — `.x4` is a null
here alone and in composition — and that is a change worth making on its own.
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.

1 participant