Skip to content

WIP: port gemm_sol_final to kittens - #138

Merged
feitreim merged 28 commits into
mainfrom
gemm-sol
Jul 30, 2026
Merged

feitreim merged 28 commits into
mainfrom
gemm-sol

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Status

This is a WIP port of NVLabs gemm_sol_final implemented through ferro-kittens library primitives rather than copied low-level CUDA device code.

Correctness and integration are working. Performance is close to cuBLASLt at 16K, but the requested dead-even target is not met yet at 4K/8K.

Implemented

  • CLC work stealing through ClcQueue / ClcCursor
  • two-CTA clusters and cta_group::2 MMA
  • four TMA/shared-memory stages
  • two TMEM accumulator halves
  • explicitly four-way-unrolled K loop
  • L2-aware N-band output ordering through pipeline::grouped
  • asynchronous producer, MMA, and epilogue warp streams rather than pipeline::Job item boundaries
  • M256xN256 and M512xN256 entries in examples/src/gemm_sol.rs
  • FP16 support in typed global/shared tiles and tcgen05 MMA routing
  • exact BF16 output checks for both variants
  • live FP16 cuBLASLt baseline in the experiments harness

The B200 measurements select M256 at 4K and M512 at 8K/16K. Upstream selects M512 only at 16K; the earlier crossover is retained here because it materially improves 8K on this B200.

Validation run

  • cargo test: 96 unit tests passed, 1 integration test passed
  • modal run modal_app.py::build: passed, including sm_100a codegen and the cuBLAS feature build
  • modal run modal_app.py::examples: passed
    • M256xN256 at 1024x1024x512: every BF16 output exact
    • M512xN256 at 1024x1024x512: every BF16 output exact
    • existing examples and experiments correctness gates also passed

Current B200 performance

Live comparison against cuBLASLt 13.0 FP16 input / FP32 compute / FP16 output, same process, stream, event clock, 5 warmups, minimum of 30 launches:

Shape Variant Ours cuBLASLt Ratio
4096³ M256xN256 1289.0 TFLOP/s 1620.7 TFLOP/s 0.795
8192³ M512xN256 1867.7 TFLOP/s 2138.8 TFLOP/s 0.873
16384³ M512xN256 2045.5 TFLOP/s 2162.3 TFLOP/s 0.946

Measured tuning so far:

  • M512 at 8K improves the port from roughly 1678 to 1868 TFLOP/s.
  • Wider shared epilogue staging helps the small entry by about 2% and is neutral at 16K.
  • Exact one-cluster-per-output CLC grid sizing is effectively neutral versus the oversized raw-ID grid retained by upstream.
  • G=4 versus upstream G=8 at 8K is neutral.

Remaining work

  • Close the 4K/8K gap to the requested near-cuBLASLt target.
  • Isolate whether the remaining loss is TMA feed cadence, MMA issue cadence, or epilogue overlap. Nsight Compute is currently unavailable in the Modal image because its profiler library fails to load, so this likely needs targeted ablation kernels or a different profiling environment.
  • Compare against the upstream raw kernel on the same B200/toolchain to separate port overhead from architecture/toolchain differences.
  • Decide whether to keep the B200-tuned 8K M512 crossover and exact CLC grid or restore upstream policy for strict behavioral parity.
  • Clean up WIP-only API/lint details and tighten documentation before marking ready for review.

Notes

The benchmark baseline is the closest cuBLASLt combination supported by this stack: FP16 inputs, FP32 compute, FP16 output. The kittens kernel writes BF16, so final conversion differs while input compute precision and output width match.

feitreim added 4 commits July 29, 2026 15:54
…s too (#138)

#138 asks which of TMA feed cadence, MMA issue cadence or epilogue overlap the
remaining gap is, with no profiler to ask. This decomposes it two ways that do
not depend on one — arithmetic, then ablation — and they agree.

`bench sol` leads with the term that needs no measurement. Both shared plans
admit one CTA per SM, a cluster is two CTAs, so a wave is 74 clusters and the
launch is one cluster per output tile: 4096^3 is 256 clusters over four waves,
a 0.865 ceiling before any cadence is measured, and 8192^3 is 0.988. The
sawtooth table proves that is the whole shape dependence rather than asserting
it — `N` in tile steps, and the quantization-corrected rate is flat to 0.6% at
M512 across five shapes whose predicted ceiling spans 0.865 to 1.000.

The three suspects are the same device body at three values of a new `ABLATE`
const, so no arm can drift from the kernel it decomposes and the shipped
kernels are that body at `WHOLE`. Every barrier survives every arm; only a
phase leaves. `regcount`'s census is the proof and reads exactly that way, and
the two shipped kernels stay opcode-identical between the crates.

The depth ladder is a straight line in `K` — 0.5% over an eightfold range at
M512 — so the launch is a per-tile constant plus a per-K-block rate, and both
fall out. The M512 K loop runs at **99.4% of tensor-core peak**; its entire
deficit is 12.7 us per tile, of which the drain is 9.99. M256's K loop runs at
75.8%, which is a cadence and not a constant.

Subtracting the arms gives an additive budget that reproduces both launches:

| term | 4096^3 M256 | 8192^3 M512 |
| --- | ---: | ---: |
| arithmetic at peak | 58.1% | 83.4% |
| wave quantization | 9.1% | 1.0% |
| epilogue | 19.7% | 11.9% |
| TMA feed in situ | 5.3% | 1.4% |
| MMA issue cadence | 7.9% | 2.2% |

The epilogue is the largest single loss at both shapes and is 11.9 of the 13.2
points between 8192^3 and peak. It costs the same per byte of `C` in both
variants — 25.3 and 26.2 GB/s per cluster — so it is the LDTM/stmatrix/store
chain running at a fixed rate, not a bandwidth and not a variant. The feed is
not the problem anywhere: it moves the whole launch's operands in half the
launch, at 18-22 TB/s.

Also: #138's profiler finding re-derived and its two escapes closed.
`NVIDIA_DRIVER_CAPABILITIES=all` is already in the container and is not the
lever, `ncu` fails identically on a four-line nvcc kernel, and NVIDIA's own
580 component list has no performance-counter library to withhold.

`scripts/fmt` also picked up three pre-existing hunks in `src/shared.rs`.
… 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 and others added 11 commits July 29, 2026 18:39
…g ladder, and one wait a band where there were two

The epilogue is the largest single term in `gemm_sol` at both shapes with a
gap. #144 measured it at 19.7% and 11.9% of the launch and left two things
fused: the drain's own cost and its failure to overlap. This separates them,
prices the chain a third at a time, and ships the one lever the split says is
worth anything.

Three new `ABLATE` arms make a doubling ladder, each repeating one more pass of
the drain per band with the extra global stores aimed at the cluster's own
first output tile so they stay in L2. An extra pass has nothing left to hide
behind, so it is paid serially by construction: `twice all - whole` is the
drain's own occupancy cost D, and `whole - no drain` is what the launch pays.

E > D at both shapes. None of the drain is hidden, and at M256 -- where the
accumulator IS double-buffered across tiles -- E is 191% of D, so the drain
also slows the phase it runs beside. The LDTM half is free: doubling
`tcgen05.ld` and its waits costs zero. What is left is the register->shared
half, at 60-70% of D.

Two drains built on that. `paired` puts a band's two LDTM issues in flight
before one wait and ships (+0.9% at 8192^3, nothing at 4096^3, two round-robin
passes each, every census column but the wait identical). `wide` puts four
behind one wait and loses.

New: `TmemTile::tile_x8_batched`, an `ldtm.wait` census column, a
`ldtm x8 batched map` device case, six ablation arms, two tables, and a
correctness gate for the arms that compute a right C.
@feitreim
feitreim marked this pull request as ready for review July 30, 2026 01:10
@feitreim
feitreim merged commit 336e3d7 into main Jul 30, 2026
7 of 8 checks passed
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