Skip to content

Every step GEMM gets a cuBLASLt denominator at its exact shape and mode, and the fold, not the walk, is what lags - #79

Merged
feitreim merged 3 commits into
mainfrom
bench/model-shapes
Aug 4, 2026
Merged

feitreim merged 3 commits into
mainfrom
bench/model-shapes

Conversation

@feitreim

@feitreim feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner

What

The square bench prices the tcgen05 GEMM at one mode pairing per element; the training step runs fourteen rectangles across three modes, and the step profile's per-row rates spread 0.82–1.55 PF/s with nothing to divide them by. This PR gives every step GEMM its own denominator at its exact shape and mode:

  • cublaslt::Baseline learns the kernel's other form (Form::AccumulateTransposed: dW += Aᵀ·B over the native [K, MN] panels, β = 1) and the fp32 output element, so the weight-gradient rows divide by the matched cuBLASLt product rather than by a store they are not.
  • bin/model_shapes.rs reports ours vs cuBLASLt at all fourteen shapes — same buffers, same stream, same clock, each cuBLASLt configuration checked against the kernel's output at a rectangle before its time is believed.
  • Tcgen05Gemm::f32_store_transposed fills the mode grid's hole (mode 2, MN-major — the kernel always took mode and transposed independently; no adapter said so), and the bench uses it to price the MN-major walk and the fold's read of C apart at each dW shape, after checking store-T agrees bitwise with a from-zero acc-T.

FEATURES=cublas ./run.sh gemm model_shapes to run. No kernel changes; ptxas budgets unmoved.

Results (B200, cuBLASLt 13.0, main @ cebec1c + this bench)

shape MxKxN mode tiles ours cuBLASLt ratio
24576x3072x9216 qkv fwd f32 store 3456 0.942 ms, 1477 0.727 ms, 1915 0.771
24576x3072x3072 o_proj fwd f32 store 1152 0.332 ms, 1396 0.251 ms, 1848 0.756
6144x3072x8192 gate_up fwd /exp f32 store 768 0.234 ms, 1323 0.167 ms, 1850 0.715
6144x4096x3072 down fwd /exp f32 store 288 0.110 ms, 1409 0.086 ms, 1803 0.781
24576x9216x3072 bwd qkv dx f32 store 1152 0.802 ms, 1736 0.745 ms, 1867 0.929
6144x8192x3072 bwd gate_up dx /exp f32 store 288 0.181 ms, 1706 0.168 ms, 1844 0.925
6144x3072x4096 bwd down dx /exp f32 store 384 0.127 ms, 1214 0.094 ms, 1652 0.735
3072x24576x9216 bwd qkv dW f32 acc-T 432 0.801 ms, 1738 0.705 ms, 1973 0.881
3072x24576x3072 bwd o_proj dW f32 acc-T 144 0.264 ms, 1754 0.236 ms, 1966 0.892
3072x6144x8192 bwd gate_up dW /exp f32 acc-T 384 0.266 ms, 1162 0.177 ms, 1746 0.666
4096x6144x3072 bwd down dW /exp f32 acc-T 192 0.160 ms, 964 0.094 ms, 1639 0.588
24576x3072x50432 lm_head fwd bf16 store 18912 4.906 ms, 1552 3.952 ms, 1927 0.806
24576x50432x3072 bwd lm_head dx bf16 store 1152 4.461 ms, 1707 4.105 ms, 1855 0.920
3072x24576x50432 bwd lm_head dW bf16 acc-T 2364 4.424 ms, 1721 4.146 ms, 1837 0.937

(rates in TFLOP/s; bold = below 0.90; run1 reproduced every row within ~2%)

Walk and fold priced apart at the dW shapes (ours only):

shape store (K-major) store-T (MN-major) walk acc-T fold
3072x24576x9216 1825 1830 −0.3% 1725 +6.1%
3072x24576x3072 1837 1854 −0.9% 1748 +6.1%
3072x6144x8192 1459 1494 −2.4% 1162 +28.6%
4096x6144x3072 1171 1212 −3.4% 966 +25.5%

Reading

The ratio is a function of K depth and drain kind, not of shape family or the transposed walk. Every mode converges to ~0.92–0.94 of cuBLASLt at K ≥ 8–9k and decays as K shrinks: 0.72–0.78 at K = 3–4k for f32 store, 0.81 for bf16 store, 0.59–0.67 for the fold at K = 6k. The fixed per-item epilogue — which pipeline::run serializes against the next item's TMA/MMA — works out to ~6–7 µs (bf16 staged drain), ~10–13 µs (f32 scattered drain), ~30 µs (f32 fold) per [256,256] tile, and the MMA that has to amortize it is ~26 µs per 3072 of K. ferro-kittens #114 measured the drain exposed at square shapes; these rows put a per-shape price on it at the model's K depths.

The two profile suspicions resolve as follows.

  • Transposed accumulate lags store: the transposed walk itself is free — MN-major store beats K-major store by 0.3–3.4% at every dW shape. What lags is the fold (the ld.global.v4 in front of the scattered fp32 drain): +6% at K = 24576, +26–29% at K = 6144. The profile's 0.91–1.06 PF/s weight rows are the per-expert K = 6144 folds, reproduced here at 964–1162 TFLOP/s.
  • 144 output tiles < 148 clusters (bwd o_proj dW): refuted as the cause. cuBLASLt wins that shape by 11% using splitk = 1 at waves = 0.97 — one underfilled wave, same as ours — so there is no split-K story and the underfill itself is worth ≤ 3%. The row's deficit is the same amortized-fold curve as its K = 24576 sibling (0.881 vs 0.892).

No cheap config-level fix exists, which is why this PR ships the measurement and nothing else:

The real remedies live in ferro-kittens, and are known there: hiding the drain behind the next item's MMA needs a second TMEM slot that BLOCK_N = 256 at 2 CTAs/SM cannot allocate (512 columns is the whole budget); the fold's round trip would be deleted outright by reduction TMA stores (cp.reduce.async.bulk.tensor, ferro-kittens #42); the scattered fp32 drain is GAPS.md §2.6 / #174. What this bench adds to those threads is the price at the training step's shapes: ~4.3 ms/step of recoverable time against cuBLASLt parity, most of it in the shallow-K forward stores and the per-expert folds.

…oduct

The square bench prices one mode pairing; the training step runs fourteen
rectangles across three modes. cublaslt::Baseline learns the kernel's other
form (dW += At*B over native [K, MN] panels, beta = 1) and the fp32 output
element, and bin/model_shapes.rs reports every step GEMM at its exact shape
and mode against that matched denominator, same buffers, same clock.
f32_store_transposed fills the mode grid's hole (mode 2, MN-major) and the
bench times store / store-T / acc-T at each dW shape, one variable apart
each, after checking store-T agrees bitwise with a from-zero acc-T.
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