Repository navigation
gemm: make the reference B vary along K, so the exact check covers the K walk - #55
Merged
Merged
Conversation
…e K walk `b_value` took a `depth` argument and computed `depth * 5 % 5`, which is identically zero, so `B` was constant along `K`. `pass gemm 512x256x256 exact` was therefore consistent with a kernel that indexed `B` wrongly along K -- the one axis `mma_walk_cg2`'s chunk arithmetic actually computes. The new generator is `(column * 4 + depth * 5) % 21 - 10`. The modulus is not arbitrary: `A`'s values sum to zero over one period of `depth`, so a `B` whose depth period were coprime to 7 would make the sum over one combined period factor as `(sum A)(sum B) = 0` -- the dot product would be bounded independently of K and two different K walks would collide by accident. Mod 5, mod 11 and mod 13 all do collide at some legal K; mod 21 shares the factor 7, the partial sums grow with K, and a swept check of every legal K to 8192 finds no wrong K walk it cannot see. Exactness is preserved with room to spare: operands are integers in [-3, 3] and [-10, 10], both exact in bf16, and every partial sum is bounded by 3 * 10 * K = 245,760 at K = 8192, against fp32's exact integer range of 2^24. The largest value the reference actually takes over any legal K is 16,416. Positive control, on a B200: pinning B's TMA K coordinate to 0 -- a kernel reading one plane of B for every K step -- now fails 131072 of 131072 elements, and passes `exact` against the old reference.
feitreim
force-pushed
the
issue-48-gemm-reference
branch
from
July 28, 2026 01:47
4ac6911 to
47afacc
Compare
This was referenced Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
b_valuetook adepthargument and computeddepth * 5 % 5— identicallyzero.
Bwas constant alongK, sopass gemm 512x256x256 exactwasconsistent with a kernel that indexed
Bwrongly along K: reading the sameplane every step, reversing the walk, skipping chunks. That is precisely the
axis
mma_walk_cg2's chunk arithmetic computes.One file, one generator, and the reference's period along the column axis.
The new generator
Why 21, and not the obvious small fix. The tempting change is
depth * 5 % 5→depth * 2 % 5, and it is wrong in an interesting way.A's values over one period ofdepthare0..7shifted to sum to zero. IfB's depth period is coprime to 7, then by CRT the sum over one combinedperiod factors as
(Σ A)(Σ B) = 0. The dot product is then a function ofK mod 7Palone — bounded independently ofK, living in a space of a fewdozen values — and two different K walks collide by accident. This is not
hypothetical. Sweeping every legal
K(every multiple ofBLOCK_Kto 8192)against 18 wrong K walks:
(col*3 + d*5) % 5(current, broken)(col*3 + d*2) % 5(the obvious fix)(col*3 + d*4) % 11(col*3 + d*4) % 13(col*4 + d*5) % 2121 shares the factor 7, which defeats the factorization: the partial sums grow
with
Krather than staying bounded, and no wrong K walk collides anywhere.The walks swept are: B constant along K; constant within a stage; constant
within a chunk; the whole walk reversed; a stage reversed; a chunk reversed;
chunk order reversed; chunks 0 and 1 swapped; off by one plane, one chunk, one
stage; back one chunk; each plane twice; each chunk twice; stage order
reversed; stages 0 and 1 swapped; the three-deep ring slot mistaken for the K
index; the chunk descriptor double-stepped.
Why it stays exact at the largest K. Operands are integers in
[-3, 3]and[-10, 10]; bf16 holds every integer to 256 exactly, so nothing is loststaging them. Every product is an integer
≤ 30, so every partial sum isbounded by
3 · 10 · K= 245,760 at the benchmark'sK = 8192, againstfp32's exact integer range of
2²⁴= 16,777,216 — a factor of 68 of headroom.The largest value the reference actually takes over any legal K is 16,416.
The comparison stays
==.The residual, stated rather than hoped for. When
Kis a multiple of 21the tail vanishes and
B's column period collapses from 21 to 7, so a columnerror that is a multiple of 7 becomes invisible. The K axis stays exact even
there. No
Kthis project runs is a multiple of 21 — they are all powers oftwo. This is bounded and provable, unlike mod 11/13, whose blind K are
irregular accidents.
Positive control
Same discipline as the register measurements: break it deliberately, confirm
the check reacts, revert.
The control is a one-line change to the kernel's own K walk for
Bonly —pinning its TMA K coordinate to 0, so every stage reads K-block 0 and
Bisconstant along K:
On a B200:
pass gemm 512x256x256 exactFAIL gemm 131072 of 131072 elements wrong—C[0,0] = 10, want 529pass gemm 512x256x256 exactThe third row is the issue, reproduced: the old check certifies a kernel that
never walks K. The control was then reverted and the correct kernel re-run
against the new reference —
pass, again — so the passing result is not theperturbed build.
Softmax's plane axis
Looked; it is not weak, and I have changed nothing.
permutation(p, r, c) = (53p + 37r + 11c) % 128. 53 is odd, so it is abijection mod 128, and since
11⁻¹ ≡ 35 (mod 128), a one-plane step isexactly a 63-column rotation of the row's permutation — as far from the
identity as the axis allows. A wrong plane substitutes
weight[i+53]forweight[i], a factor of2^(53/8) ≈ 99(or2^(-75/8)when it wraps).Minimum relative error over every cell of the check, against the 2⁻⁸
tolerance:
One thing worth a follow-up, on the row axis rather than the plane axis.
37 · 128 ≡ 0 (mod 128), so the permutation has period 128 inrow— exactlyone CTA's band. Any row error that is a whole multiple of 128 rows is
invisible. It is narrower than the gemm hole was: the kernel feeds the same
ROWS * blockIdx.xto both the load and the store, so a shift moves theoutput too and lands somewhere, and with the destination zeroed an unwritten
band is caught. It bites only if the load and store row coordinates diverge —
a wrong descriptor row stride, or a refactor that computes them separately.
Real, but a different bug class, and out of scope here rather than folded in
silently. Happy to file it.
Verified
modal run modal_app.py::build— clippy (library, harness, examples), hosttests, and sm_100a device builds of both crates: clean.
modal run modal_app.py::examples(B200) —pass gemm 512x256x256 exact,pass softmax … worst relative error 1.97e-3(unchanged).modal run modal_app.py::bench(B200) — all six gemm sizes check andtime,
8192x8192x8192included, where the exactness arithmetic is moststressed: 1.0505 ms, 1046.6 TFLOP/s, 46.5% of peak. No FAIL rows. Softmax
unaffected.
modal run modal_app.py(B200) — device suite still 18 of 18.cargo fmt --checkclean in all three crates.The bench and device-suite runs above were on
f404406. The branch was thenrebased onto
d661b60(#54'salloc_clusterlift, which also editsgemm.rs) — it merges clean, since that change is in the kernel and this oneis in the host reference — and
build+exampleswere re-run on the rebasedtip: still
pass gemm 512x256x256 exact.Toolchain untouched, nothing in
src/touched, no library bug found.Closes #48