Skip to content

The small end is one wave short, and no tile fixes it — three entries and the rule that picks them - #146

Merged
feitreim merged 2 commits into
gemm-solfrom
small-shape-gemm
Jul 30, 2026
Merged

feitreim merged 2 commits into
gemm-solfrom
small-shape-gemm

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Base is gemm-sol (#138), not main.

The arithmetic, before anything is launched

Both shared plans declare more than half of the 233472 B an SM divides
(196864 B and 229632 B, queried not written down), so residency is one CTA an
SM
; a cta_group::2 MMA needs its pair co-resident, so the device holds
74 clusters on 148 SMs. A launch is one cluster per output tile and takes
ceil(tiles / 74) waves of them, and tiles / (waves · 74) is the fraction of
it not idling.

shape entry tiles waves wave eff
4096³ [256, 256] 256 4 0.865
4096³ [512, 256] 128 2 0.865
4096³ [256, 128] 512 7 0.988
8192³ [256, 256] 1024 14 0.988
8192³ [512, 256] 512 7 0.988

Two consequences, both load-bearing:

  1. 4096³ and 8192³ are not the same problem. 13.5% of 4096³'s grid is idle;
    8192³ is already wave-perfect on both entries, so quantization predicts
    nothing about that shape's gap.
  2. The two 4096³ entries quantize identically, which makes that pair a
    controlled comparison of everything except quantization.

This matches #144's independent derivation exactly.

The model is right, measured rather than assumed

bench sol table 2 — n = k = 4096, m in tile steps, nothing else moving:

shape tiles waves wave eff TFLOP/s ÷ wave eff
4096x4096x4096 256 4 0.865 1351.0 1562
4608x4096x4096 288 4 0.973 1469.5 1510
5120x4096x4096 320 5 0.865 1354.4 1566
5632x4096x4096 352 5 0.951 1463.6 1539

Raw swings 8.7%; corrected is flat to ±2%. CLC work stealing is on for
every row, which answers what it is worth here: it removes the static share of
the tail and cannot remove the integral one, and the sawtooth is the integral one
surviving it in full.

And it buys nothing against the baseline, because cuBLASLt pays it too

The harness prints the heuristic's own wave count beside the algorithm it chose.
At those four shapes cuBLASLt reports 3.46 / 3.89 / 4.32 / 4.76 waves — our
256 / 288 / 320 / 352 tiles over 74 clusters, to three digits — on tile=23 at
every shape from 2048³ up. Its rate divided by the same efficiency is
1955 / 1964 / 2018 / 2015, equally flat.

Two implementations on the same cluster tile with the same sawtooth, so
quantization divides out of the ratio exactly. 0.795 at 4096³ is a rate
deficit, not a tiling one, and what is left is #144's per-tile constant and
K-loop cadence — not this PR's scope.

The premise that the gap grows monotonically as the problem shrinks is also not
true past three points: 1024³ 0.567, 2048³ 0.846, 4096³ 0.795, 8192³ 0.868.
2048³ beats 4096³ because cuBLASLt is also one wave short there (0.86 waves,
906.8 TFLOP/s, 40% of dense peak).

Every lever that buys the quantization back loses

All of them are built, checked exact, and kept.

[256, 128] is the tile that reaches 0.988 at 4096³:

shape [256, 256] [256, 128] narrow/wide wave eff
1024³ 124.7 160.9 1.29 0.216 → 0.432
1536³ 364.7 464.1 1.27 0.486 → 0.973
2048³ 751.9 697.2 0.93 0.865 → 0.865
3072³ 1283.2 1034.2 0.81 0.973 → 0.973
4096³ 1343.0 1079.4 0.80 0.865 → 0.988
4096x4096x2048 1181.9 1004.4 0.85 0.865 → 0.988
8192³ 1682.6 1062.1 0.63 0.988 → 0.988

The rows where efficiency does not move price the tile alone: −7% at 2048³,
−19% at 3072³, −37% at 8192³
, which is 1.5× the operand traffic per flop (85.3
against 128 flops per operand byte) and twice as many per-tile constants to pay
it on. At 4096³ that price exceeds the 14% of quantization it buys, and the tile
loses 20%.

[512, 256] at 4096³ is the other direction and a dead heat: 1323.2 against
1343.0, 0.985, inside a 4% spread. Its 0.75× operand traffic per flop is
worth 1.12× at 8192³ where quantization is equal, and is cancelled at 4096³ by
having half as many tiles to amortize a per-tile constant over. #138's crossover
at 8192 is right and now has a mechanism.

Split-K and stream-K are dead on arithmetic, and the vendor agrees.
Splitting K in two at 4096³ takes 256 work units to 512 and efficiency to 0.988
— about 8% of the launch. It also doubles the number of epilogues, and #144
prices the epilogue at 19.7% of that launch, so even a reduction that adds
no traffic at all pays 19.7% for an 8% return. Stream-K splits at most 74 of
256 tiles and so pays ~5.7% for the same 8%: a ~2% return inside rows that repeat
to 3–6%, in exchange for a k-range per work unit, a partial-tile MMA, a fixup
ordering, and an fp32 workspace that the exact-output gate makes mandatory. The
reduction instruction being reachable through ptx_asm! changes neither figure.
cuBLASLt reports splitk=1 reduction=0 workspace=0 B at every shape in both
tables.

The N band is worth nothing measurable. group was a rule inside the kernel
keyed on tiles_m and is a launch parameter now, swept over {1, 2, 4, 8, 16}:

  • 8192³ on [512, 256]: 1880.3 / 1877.7 / 1876.2 / 1878.7 / 1877.6 TFLOP/s —
    flat to 0.2%, extending WIP: port gemm_sol_final to kittens #138's G=4 against G=8 to the whole ladder.
  • 4096³ on [256, 256]: 1317.9 / 1336.3 / 1343.0 / 1311.4 / 1367.0 against rows
    whose own launches spread 1.3–3.6%. G=16 is nominally best by 2.3% over the
    shipped G=2 and is not quotable, so the default does not move.

What did move: a third rung below 2048

Halving N raises the efficiency only while both tile counts fit the same wave
count — at or below half a wave of wide tiles. Both 1.27–1.29× rows are
there; both losing rows are past it. select_variant is that sentence:

pub const fn select_variant(m: usize, n: usize) -> Variant {
    if !n.is_multiple_of(BLOCK_N) {
        Variant::M256xN128            // the only contract that admits this n
    } else if m >= 8_192 && m.is_multiple_of(2 * 256) {
        Variant::M512xN256
    } else if 2 * (m / 256) * (n / BLOCK_N) <= RESIDENT_CLUSTERS {
        Variant::M256xN128
    } else {
        Variant::M256xN256
    }
}

bench sol-small takes both rungs twice round-robin, because a 1024³ launch
is 17 µs and its thirty samples spread 13–21% — a difference at that size is
quoted against its own repeatability or not at all. The two passes give
1.273/1.273 at 1024³, 1.273/1.278 at 1536³, 0.927/0.934 at 2048³ and
0.806/0.811 at 3072³: the ratio reproduces to 0.5%.

Against cuBLASLt that is 1024³ 0.567 → 0.731 and 1536³ 0.597 → 0.760,
and every shape at and above 2048³ stays on the entry it already had — so the
gemm-sol sweep's own three rows are unchanged.

What this closes at 4096³, and what it does not

  • Closed: the question. 13.5% of the 4096³ launch is wave quantization, it is
    measured rather than modelled, and it is not recoverable through the ratio
    because the baseline pays the same 13.5% on the same tile. Three tiles, a
    four-shape wave ladder, a five-point N band and a split-K costing all say so.
  • Not closed: the rate. 1562 TFLOP/s corrected against cuBLASLt's ~1955 is
    0.80, and nothing in tiling or scheduling moves it. The epilogue is the gap, and at 4K it is only a fifth of it (#138) #144 puts that in the
    epilogue (19.7%) and the M256 K loop (75.8% of peak against M512's 99.4%),
    both of which have owners.

Structure

  • Small is one width-generic entry, Small<N, HALF, STAGE>, instantiated
    at [256, 256] and [256, 128]. No duplicated producer, MMA or epilogue.
    mma_shape(N) is a const fn because the MMA warp reads the shape inside a
    width-generic body.
  • Common gives up the B ring to the entry that sizes it, which is what let the
    rest stay non-generic. Common::drain and Common::accumulator take the width
    as a const parameter.
  • tiles_n is now the output tile column count rather than n / 128 halved
    inside two kernels.
  • group is a kernel parameter rather than a rule on tiles_m.
  • The [256, 128] entry widens the shape contract from n % 256 to n % 128,
    and select_variant routes an n only it admits.

Note for whoever owns the epilogue: drain gained an N const parameter and
lost BLOCK_N in its loop bound. Nothing inside it changed otherwise.

Validation

  • modal run modal_app.py::build: passes, including sm_100a codegen of all
    four manifests and the cublas FFI build.
  • modal run modal_app.py::examples: passes. All three entries exact over
    every one of 1048576 BF16 outputs
    at 1024x1024x512, worst |rel| 3.89e-3
    against the fp32 reference. Every timed row in both sweeps re-checks its own
    plan at that size before it is timed.
  • scripts/fmt: clean, including three pre-existing unformatted hunks in
    src/shared.rs that gemm-sol was carrying and that would have taken tier 1
    red.
  • All numbers above are from two containers, bench sol and bench sol-small;
    no figure is mixed with WIP: port gemm_sol_final to kittens #138's or The epilogue is the gap, and at 4K it is only a fifth of it (#138) #144's containers, and the ratios quoted are
    against each run's own live cuBLASLt column.

One shape did not return

4096x4096x1024 on [256, 256] printed nothing for 1200 s and
scripts/modal-run's watchdog stopped the container. It is out of both tables
and was not chased; 4096x4096x2048 runs and is in table 4. A k shorter than
m is otherwise unexercised by this kernel, so whether that is a shape contract
the port does not hold or a one-off is open, and it is the one loose end here.

feitreim added 2 commits July 29, 2026 18:37
… and the rule that picks them

`gemm_sol`'s 0.795 at 4096³ reads like tile quantization. It is not, and the
tables say so before they say anything else.

Residency is one CTA an SM — both shared plans exceed half of the 233472 B an
SM divides — so a `cta_group::2` pair is 74 clusters on 148 SMs. At 4096³ the
`[256, 256]` tile is 256 clusters over 4 waves and 13.5% of the grid idles;
`[512, 256]` is 128 over 2 and idles the same 13.5%, which is what makes that
pair a controlled comparison of everything except quantization. At 8192³ both
are 0.988 and quantization predicts nothing about that shape's gap at all.

The model is measured, not assumed: sweeping `m` in tile steps at `n = k = 4096`
swings throughput 8.7% and the same numbers divided by the predicted wave
efficiency are flat to ±2%. CLC work stealing runs throughout, which settles
what it is worth — it removes the static share of the tail and cannot remove the
integral one.

And it buys nothing against the baseline, because cuBLASLt pays the identical
sawtooth: its heuristic prints 3.46 / 3.89 / 4.32 / 4.76 waves at those four
shapes, which is our own tile count over 74 to three digits, on the same
`tile=23` from 2048³ up. Quantization divides out of the ratio exactly, so
4096³'s deficit is a rate deficit and belongs to #144's per-tile constant and
K-loop cadence.

Every lever that buys the quantization back loses, and all of them are kept:

- `[256, 128]`, which reaches 0.988 at 4096³, is 0.80 there and 0.63 at 8192³.
  The two shapes where efficiency does not move price the tile alone: 1.5× the
  operand traffic per flop and twice the per-tile constants, worth −7% at 2048³
  and −37% at 8192³.
- `[512, 256]` at 4096³ is a dead heat at 0.985, inside a 4% spread. #138's
  crossover at 8192 is right and now has a reason.
- Split-K doubles the epilogue, which #144 prices at 19.7% of the 4096³ launch,
  to recover 8%. Stream-K pays 5.7% for the same 8%. cuBLASLt reports
  `splitk=1 reduction=0 workspace=0 B` at every shape here.
- The N band is flat to 0.2% over `{1,2,4,8,16}` at 8192³ and inside its rows'
  own spread at 4096³, so `group` becomes a launch parameter and its default
  does not move.

What did move is below 2048: halving `N` doubles the wave efficiency only while
both tile counts fit one wave count, and there the narrow tile is 1.27–1.29×.
`select_variant` is that sentence, taken twice round-robin because a 1024³
launch is 17 µs. 1024³ goes 0.567 → 0.731 of cuBLASLt and 1536³ 0.597 → 0.760,
with every shape at and above 2048³ left on the entry it already had.

`Small` is one width-generic entry rather than two, `Common` gives up the B ring
to the entry that sizes it, and all three entries are exact over every BF16
output at 1024x1024x512.
@feitreim
feitreim merged commit 9370d74 into gemm-sol Jul 30, 2026
4 checks passed
feitreim added a commit that referenced this pull request Jul 30, 2026
…to cuBLASLt is the reference's own (#138)

NVLabs' canonical Blackwell `gemm_sol_final`, rebuilt through ferro-kittens' typed
tiles and pipeline primitives rather than transcribed as device code: CLC work
stealing, two-CTA clusters with `cta_group::2` MMA, four TMA/shared stages, two
TMEM accumulator halves, an unrolled K loop, L2-aware output ordering, and three
asynchronous warp streams. FP16 in, FP32 compute, BF16 out; every entry checked
exact over 1 048 576 outputs.

Five PRs of measurement landed with it, and between them they moved the target.

**The reference is not parity.** Upstream `gemm_sol_final`, unported and timed on
one device, one clock, beside cuBLASLt and beside this port, reaches only
0.877/0.911/0.966 of cuBLASLt at 4096/8192/16384 cubed (#145). Two thirds of the
shortfall is upstream's own; a third is port overhead, consistently across all
three sizes. Matching the reference was never a route to parity.

**Wave quantization cancels.** cuBLASLt pays the identical sawtooth on the
identical tile count — 3.46/3.89/4.32/4.76 waves against ours over 74 clusters —
so it divides out of the ratio and 4096 cubed's deficit is a rate, not a tiling
problem (#146). Every tiling lever that buys quantization back was built and lost;
a third `[256, 128]` entry ships for shapes at or below half a wave, worth
1.27-1.29x at 1024/1536 cubed.

**Where the time goes, laddered in K rather than read off a launch** (#144, #148).
An arm's launch conflates a rate with a per-tile constant: un-laddered, the MMA-only
arm read 88.7% of peak and looked like an issue ceiling; laddered it is 100.6%. The
feed is a duty-cycle problem, not bandwidth and not instruction count — the narrow
tile needs its feed running 95.5% of tensor-core busy time and pays 24.5% for the
contention, where the wide tile needs 55.7% and pays nothing. The barrier round
trip is zero.

**One port defect found and fixed.** The MMA warp indexed both rings and both stage
barriers off a runtime `global_k`, where `k_blocks % STAGES == 0` makes each
unrolled position's stage a constant. Handing it as a const folds barrier addresses
to literals, hoists both descriptors out of the K loop, and computes the parity once
a turn instead of four times: +2.2 to +4.3% at 4096 cubed, null at 8192 as
predicted. The gain is in the inter-stage critical path, not the spin loop — an
earlier account of the mechanism was retracted when the PTX contradicted it.

**The drain is exposed whole, and both of its expected levers measure zero** (#147).
A doubling ladder, where an extra pass is serial by construction, separates the
drain's own cost from what the launch pays: the launch pays 191% of a serial drain
at 4096 cubed and 118% at 8192, so there is no overlap to fail. Doubling LDTM and
its waits costs nothing; `.pack::16b` does remove the `cvt` but faults out of bounds,
and a legal `nocvt` oracle is slower, so the convert was never the cost. The
62-70% is the `stmatrix` pass and its write-after-write.

**A PTX count is a hypothesis; SASS or a timed arm is the evidence.** Two levers
filed off PTX instruction counts turned out to be folded entirely by `ptxas` —
three poll bodies differing by an instruction in PTX are byte-identical at three
instructions in SASS. `modal_app.py::_print_sass_loops` disassembles a real cubin
and finds loops by backward branch rather than by mnemonic. What this does and does
not disturb about the repo's existing PTX conclusions is recorded in
`experiments/README.md`.

Against cuBLASLt 0.778 and against upstream 0.917 at 4096 cubed, from 0.758 and
0.891. Nsight Compute is confirmed unavailable on this harness and the reason has a
floor under it, so ablation is the instrument.

Open: **#149**, the `[256, 256]` entry does not return at 4096x4096x1024, a shape
inside its own contract.
feitreim added a commit that referenced this pull request Jul 30, 2026
…ills

Measured rather than assumed, because a compiler-backend bump is exactly the
change that moves registers with nothing watching: `regcount` on `336e3d7` and on
the same tree with the pin moved, same image, same `ptxas`. 44 of 270 rows moved.
The kernel set is identical on both sides, and `regcount --determinism` passes on
the new pin — 270 kernels, every counter, across two builds — which is what makes
the diff a function of the revision rather than of the build.

Not an LLVM bump: `rust-toolchain.toml` is byte-identical and both revisions
validate against `rust-llvm-22.1.2-1cb4e383`. Two files differ inside
`rustc-codegen-cuda/src` (`collector.rs`, `generated_intrinsics.rs`) beside
`cargo-oxide`'s `backend.rs`.

Most movement is downward — `gemm_256x256_k128_s1` 246 -> 168 — and nothing
`examples/` ships regressed. But **four kernels started spilling, and three of
them read as wins on the register count alone**: `gemm_ws_staged` 43 -> 40 with
16/20 spill slots appearing, `gemm_ws_staged_x8` and `_x8x4` 90 -> 80 with 8/12,
and `gemm_sol_m512_mma` flat at 80 with 8/12. A count that falls while spill
slots appear is `ptxas` taking a lower target and paying in local memory, which
is this file's oldest error arriving from the toolchain instead of from a diff.
No row stopped spilling.

The occupancy gate (#95) is green on both sides — two of its six rows moved and
both stay inside their step — so this is a finding and not a blocker. The opcode
census moved in 3 of 74 kernels and only in `selp`, so no rung's phase claim is
disturbed.

One published number does not survive: `gemm_sol_m512_mma` is #144/#146's
`mma only` arm at 88.7% of tensor-core peak, and it now spills at the same 80
registers. That arm is not comparable across the pin and wants re-timing.
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