Skip to content

gemm: make the reference B vary along K, so the exact check covers the K walk - #55

Merged
feitreim merged 1 commit into
mainfrom
issue-48-gemm-reference
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
issue-48-gemm-reference

Conversation

@feitreim

@feitreim feitreim commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

b_value took a depth argument and computed depth * 5 % 5 — identically
zero. B was constant along K, so pass gemm 512x256x256 exact was
consistent with a kernel that indexed B wrongly along K: reading the same
plane 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

fn a_value(row: usize, depth: usize) -> f32 { ((row * 5 + depth * 3) % 7) as f32 - 3.0 }   // unchanged
fn b_value(column: usize, depth: usize) -> f32 { ((column * 4 + depth * 5) % 21) as f32 - 10.0 }

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 of depth are 0..7 shifted to sum to zero. If
B's depth period is coprime to 7, then by CRT the sum over one combined
period factors as (Σ A)(Σ B) = 0. The dot product is then a function of
K mod 7P alone — bounded independently of K, living in a space of a few
dozen values — and two different K walks collide by accident. This is not
hypothetical. Sweeping every legal K (every multiple of BLOCK_K to 8192)
against 18 wrong K walks:

generator K-walk blind spots e.g.
(col*3 + d*5) % 5 (current, broken) 2304 everything, at every K
(col*3 + d*2) % 5 (the obvious fix) at K=4096 B constant along K, reversed, off by one
(col*3 + d*4) % 11 31 off-by-one-stage at several K
(col*3 + d*4) % 13 at K=4096 B constant along K, reversed, off by one
(col*4 + d*5) % 21 none, at any legal K —

21 shares the factor 7, which defeats the factorization: the partial sums grow
with K rather 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 lost
staging them. Every product is an integer ≤ 30, so every partial sum is
bounded by 3 · 10 · K = 245,760 at the benchmark's K = 8192, against
fp32'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 K is a multiple of 21
the tail vanishes and B's column period collapses from 21 to 7, so a column
error that is a multiple of 7 becomes invisible. The K axis stays exact even
there. No K this project runs is a multiple of 21 — they are all powers of
two. 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 B only —
pinning its TMA K coordinate to 0, so every stage reads K-block 0 and B is
constant along K:

b_ring.tile(k).tma_load_2d_multicast_cg2(b_map, /* column */ 0, b_row, arrival, mine);

On a B200:

kernel reference result
correct new pass gemm 512x256x256 exact
B pinned to K-block 0 new FAIL gemm 131072 of 131072 elements wrong — C[0,0] = 10, want 529
B pinned to K-block 0 old pass gemm 512x256x256 exact

The 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 the
perturbed 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 a
bijection mod 128, and since 11⁻¹ ≡ 35 (mod 128), a one-plane step is
exactly 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] for
weight[i], a factor of 2^(53/8) ≈ 99 (or 2^(-75/8) when it wraps).
Minimum relative error over every cell of the check, against the 2⁻⁸
tolerance:

kernel error min relative error
plane off by one 0.9985 caught, 255× the tolerance
row off by one 0.9996 caught
row off by 32 (wrong warp band) 0.9998 caught
column off by one / 16 / 64 1.0000 / 0.9990 / 0.9961 caught
row off by 128 (a whole CTA band) 0.0000 blind

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 in row — exactly
one 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.x to both the load and the store, so a shift moves the
output 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), host
    tests, 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 and
    time, 8192x8192x8192 included, where the exactness arithmetic is most
    stressed: 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 --check clean in all three crates.

The bench and device-suite runs above were on f404406. The branch was then
rebased onto d661b60 (#54's alloc_cluster lift, which also edits
gemm.rs) — it merges clean, since that change is in the kernel and this one
is in the host reference — and build + examples were re-run on the rebased
tip: still pass gemm 512x256x256 exact.

Toolchain untouched, nothing in src/ touched, no library bug found.

Closes #48

…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.
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.

gemm's exact check cannot catch a wrong K index: b_value ignores its depth argument

1 participant