Skip to content

C is bf16, the accumulator is not, and the baseline moved with it (#108) - #109

Merged
feitreim merged 3 commits into
mainfrom
bf16-c-output
Jul 29, 2026
Merged

feitreim merged 3 commits into
mainfrom
bf16-c-output

Conversation

@feitreim

Copy link
Copy Markdown
Owner

gemm_cg2 writes bf16 C off an unchanged fp32 accumulator, and
cublaslt.rs's output layout moves to CUDA_R_16BF in the same commit.

Both columns, measured together, with an fp32 control run back to back

Four B200 containers in one session. The control is a68c390 itself —
the commit before this one, fp32 on both sides — through the same
bench --case gemm. cuBLASLt's heuristic returned a byte-identical algorithm
at every size in both runs
, so nothing here is a different kernel.

shape ours fp32 ours bf16 ours theirs fp32 theirs bf16 fp32 ratio bf16 ratio
4096³ 0.1426 0.1441 −1.0% 0.0859 0.0898 0.603 0.623
8192³ 0.7489 0.7344 +2.0% 0.6163 0.5964 0.823 0.812
16384³ 5.4025 5.2326 +3.2% 4.7637 4.5619 0.882 0.872

One pair of containers is not enough to read those rows. The branch was
measured three times at 8192³ — 0.7344 / 0.7456 / 0.7440 — against the control's
single 0.7489, so the gain is +0.4% to +2.0%, about +1.1% on the mean,
against a 1.5% cross-container spread. At 16384³ the three bf16 measurements
straddle the control (5.2326 / 5.5193 / 5.4765 vs 5.4025).

So: bf16 C is throughput-neutral, and both pre-registered predictions hold.
−1% to +1.5% was predicted and ~+1% measured; 0 to −3 ratio points were predicted
and the ratio is 0.812–0.825 against 0.823, flat to two points down. The library
gains at least as much from the cheaper output as we do — which is exactly why
moving only our side would have manufactured a win: against the published fp32
ratios this would have read as +3.6% and +4.9%.

Which prior rows survive: almost none. Every ours/theirs ratio in §7 —
#92's 0.573–0.694, #102's 0.742/0.793, #87's 0.826/0.886, #107's 0.824–0.881 —
is a pair of fp32-output measurements, as is every absolute millisecond. What
survives is everything that is a property of the tile: counted residency,
registers, GROUP, the shared plan, wave efficiency, wave_reuse. §7 says so
where the numbers are.

The small-size rows moved a lot and none of it is readable — #92 measured
cuBLASLt's own run-to-run variance below 2048³ at 33–77%, and every delta down
there is inside the variance already documented for its own side. Our ratio at
256³ goes 0.336 → 0.463, and that is the baseline getting worse.

Accuracy: the tolerance did not move, because there is still none

The comparison is still ==, at every element, for both sides. The rounding
went into the reference
: check_c rounds the exact fp32 dot product with the
same round-to-nearest-even cvt.rn.bf16x2.f32 uses and compares 16-bit words.
That is available because the sum is still exact — every partial stays under
2²⁴ — so rounding a known integer once is deterministic.

Worst relative error against the exact fp32 reference: 3.86e-3 (2⁻⁸, as
predicted), reported by check_c rather than asserted, printed on every checked
size. Widening the observed value and comparing within a tolerance would have
been strictly weaker: a tolerance admitting correct rounding admits everything
under it. What the check is now blind to is stated in check_c — errors under
about half an ulp of bf16; no failure mode this gate exists for is in that range.

Registers, spill, residency

gemm_cg2 is 166 registers, 0 spill, 528 B frame — the gate reads 166
against a ceiling of 255. Prediction 3 said 168 and unchanged; the count moved
in both directions
(lcsf is 172), which is the seventh time the register
column has failed to say what it was expected to here. Nothing follows it.

Residency is 2 CTAs an SM, counted not assumed — #87's census counted 2 at
exactly this envelope and this change moves neither term of
min(512/columns, shared per SM/plan). No new census rung was run, deliberately.

stmatrix: reachable, no plumbing left, and worth at most ~6%

Reachable — the estimate #94 rejected it on is now zero. ldst::store_tile,
SharedTile::tma_store_2d and TensorMapElement for Bf16 already exist; all
three of #94's missing pieces were the same fact, which was that C was fp32.

Priced, not assumed. Two new probes: 2x runs the epilogue twice per item,
2s runs the LDTM once and the stores twice, both aimed at the cluster's home
tile. 2x − lcf is one epilogue, 2s − lcf is one store loop, 2x − 2s is the
LDTM — no fit, and nothing deleted for a dead-code pass to have a say in.

shape epilogue µs/tile stores µs LDTM µs epilogue % of launch
8192³ 21.44 13.11 8.33 20.2%
16384³ 26.76 19.82 6.95 13.7%

Prediction 6 (5–12 µs) is refuted by about 2×, and how it is wrong is the
useful part.
21.4 µs sits above the whole item boundary as #104 fits it
(8.6–18.3 µs), and the epilogue is only part of that boundary — which can only
be true if the epilogue is partly overlapped in the real kernel and not at all
in the probe. So 2x − lcf is the epilogue's serial cost and the fit's
intercept is its exposed residue. The pre-registration argued the bias ran
the other way.

stmatrix keeps the LDTM and halves the stores, so its ceiling is half the
stores column: ~6.2% at 8192³ and ~5.1% at 16384³ — and gemm_cg2_2s costs
255 registers (0 spill, residency unchanged), so part of that 13.11 µs may be
pressure rather than stores, making it an upper bound on an upper bound. Against
it: 18344 B a CTA is free at unchanged residency, a bf16 [128,64] strip is
16384 B and fits — four single-buffered TMA round trips an item, against
eight for fp32 — with no room to double-buffer.

Verdict: a build-and-measure question rather than a plumbing one, and the
largest lever currently identified.
Two things argue it delivers less than the
ceiling: lcsf (moves when the epilogue runs) is +1.0%/+1.7%, and hot
(moves where its bytes land) is +0.5%/−0.0%. Neither is a store-count
experiment, so neither refutes the 6% — but the epilogue's placement and its
bytes are both worth nothing, and only its issue count is left.

What ran

::build and ::regcount (CPU) green; ::examples exact at both sizes, all
five rungs, both schedulers, lcsf, three traversal widths; bench --case gemm
on the branch and on a68c390; bench --case epilogue twice. Every app
confirmed stopped.

Library change is GlobalRows<E> plus Element::write_pair/read_pair — the
generalization global.rs's own module doc predicted. flash_forward and
device-tests still write fp32 through the same movers, so the losing side
stays reachable and on the gate; an fp32 gemm entry point is not kept, and §7
says why.

Closes #108.

@feitreim
feitreim merged commit 4fc30b8 into main Jul 29, 2026
4 checks passed
feitreim added a commit that referenced this pull request Jul 29, 2026
Tier 1 checks the library, device-tests and examples. A root `cargo fmt`
reaches only the first -- the other two are separate workspaces -- so a change
can be clean locally and red in CI on whitespace alone. That has happened three
times (#97, #104, #109), each costing a review round trip on a gate that runs in
twenty seconds.
feitreim added a commit that referenced this pull request Jul 29, 2026
Two sweeps, and the first retires a decomposition this file has carried
since #86.

`bench --case ablation` takes a phase *out* of the shipped kernel and
measures what the launch stops costing — the exposed cost, by
subtraction, where `gemm-depth`'s intercept infers it and #108's `2x`
measures the serial cost instead. Rungs are corners of
{loads} x {mma} x {drain} so a phase is priced in more than one context,
which is what tells serial and exposed apart rather than assuming either.

At 8192³, per output tile: the epilogue costs 20.4 us with the multiply
beside it and 20.6 without — a ratio of 1.01, against #108's serial 21.4
by a route sharing none of this arithmetic. Nothing overlaps it. And the
`no drain` rung's own fit has no fixed per-tile cost at all, so the item
boundary is the epilogue and the barrier re-arm, the cluster boundaries
and the pipeline refill are all inside the K-proportional term.

`no drain` measures 1850 TFLOP/s at 8192³ and 1841 at K=32768 — 82% of
dense bf16 peak, flat across a 4x change in depth — against cuBLASLt's
1808 in the same container. The epilogue is 111% of the whole gap to the
library, so the asymptote-equals-cuBLASLt reading is not an artifact of
point selection: there is now a measured rung that reaches it with no
fit. And #109's 6.2% ceiling for `stmatrix` is real rather than an
overestimate, which is the question that section left open.

Both of my pre-registered predictions were refuted and are kept as
written. Three corners do not run: every rung with the epilogue on and
the loads off fails to return, the first explanation for it was wrong,
and `Ablation::at` records the observation rather than a mechanism.

BLOCK_K is pinned at 64 by `k_walk`'s one-atom assert, and does not move
arithmetic intensity, so the sweepable quantity is the factorization of
BLOCK_K x STAGES under a fixed shared budget. Two new rungs give an
equal-bytes, equal-residency control pair: doubling BLOCK_K at fixed
depth is worth +38%, but two shallow stages beat one deep one by 14-17%,
so the budget belongs in stages and 64 was already right — for a reason
this file had not stated.

gemm_cg2 is 166 registers, 0 spill, 528 B — byte-identical to #109
across both refactors. Every rung of the tile sweep and both new entry
points are exact on the gate at all three traversal widths.
feitreim added a commit that referenced this pull request Jul 29, 2026
…#109's 8.3 us closes to 8.07 (#117) (#117)

* The LDTM half at .x8 and stmatrix at .x4, ablated against #116's staged epilogue (#117)

* Measure it: .x8 is +23.6/+8.6/+3.6%, .x4 alone is nothing, and #109's 8.3 us of LDTM closes to 8.07

* const assert uses is_multiple_of, which clippy requires and WIDTH_OK already used
feitreim added a commit that referenced this pull request Jul 29, 2026
…ce (#119) (#120)

`gemm`'s default epilogue becomes `staged84` and `gemm_ws`'s becomes `staged8`.
#116, #117 and #118 each ended by saying the rung was worth shipping and that
shipping it belonged in its own change; this is that change and it is the whole
of it. No rung added, none deleted, nothing retuned — every entry point still
launches and is still on the correctness gate.

The two differ on purpose. `.x8` costs +52 and +50 registers identically; what
differs is what `.x4` hands back — 14 on `gemm_cg2` (94 -> 80) and 2 on
`gemm_ws` (94 -> 92) — and that recovery IS the composition gain. There the
pair is +5.1% at 16384^3 against `.x8` alone's +3.6%; here `staged8` and
`staged84` land inside 1.1% and trade places between sessions, so the tie goes
to the rung with less surface.

Confirmed in one container with cuBLASLt re-measured in it (#109's lesson):
`gemm` 0.1432 -> 0.1044, 0.7584 -> 0.6684 and 5.5157 -> 5.0132 ms, which is
+37.2% / +13.5% / +10.0% and 0.583 -> 0.801, 0.827 -> 0.939 and 0.871 -> 0.958
of the library. `gemm_ws` +19.6% / +8.7% / +6.3% and 0.599 -> 0.717,
0.790 -> 0.858, 0.812 -> 0.863. Both reproduce their published ratios; the
16384^3 rows moved because the denominator did.

The constants that describe the shipped launch moved with it: `main.rs` reports
114 816 B and 147 584 B, and `CTAS_PER_SM` does not move on either kernel
because the tensor-memory term of `min(512 / columns, shared per SM / plan)`
binds at both envelopes. `regcount`'s occupancy gate gained
`gemm_cg2_staged_x8x4` and reads 166 / 42 / 80 against a ceiling of 255.

Deliberately unmoved: the ablation cube, `bench --case epilogue` and
`bench --case staged` stay built at `lcf`, and `ws_bench` table 1 now NAMES
`lcf` on both sides rather than taking `gemm`'s default, so its one variable is
still where the overlap comes from.
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.

C should be bf16, the accumulator should not, and the cuBLASLt baseline has to move with it

1 participant