Skip to content

softmax: give the row axis an odd period, so a whole CTA band cannot hide - #61

Merged
feitreim merged 1 commit into
mainfrom
fix/softmax-row-axis-56
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
fix/softmax-row-axis-56

Conversation

@feitreim

Copy link
Copy Markdown
Owner

permutation's row term was 37 · row taken mod 128, so it repeated every
128 rows — one CTA band. A kernel that loaded one band and stored to another
produced bit-identical expected values, and the exact-ish check passed it.

One file, one generator, and the reference's period along the row axis.

The new generator

const STRIDES: usize = 63;

fn permutation(plane: usize, row: usize, column: usize) -> usize {
    let stride = 11 + 2 * (row / ROWS % STRIDES);
    (53 * plane + 37 * row + stride * column) % 128
}

Why the band picks the stride, and not the obvious fix. The issue proposes
"a row multiplier that is not a multiple of 128". 37 already is not one — it is
odd, so 37 · row runs through all 128 residues, which is the most a linear
row term can do. The hole is not the multiplier, and no multiplier closes it.

The reason is structural. A seed of the form (a·row + b·column + c) % 128 is
an affine map of the column axis, c ↦ b·c + (a·row + c). Those maps form a
group of order 2¹³ under composition — a 2-group — and enumerating all 8192 of
them gives its exponent as exactly 128. So every row-affine seed has a
period in row dividing 128, which is one band, and the band is unreachable
from inside this family. Two tempting edits, swept over 2 planes × {256, 16384}
rows against nine wrong row walks:

generator band pinned to 0 off by one band band order reversed off by one row
11·c (current, broken) 0 wrong 0 0 all, min 1.000
39·row (the obvious fix) 0 wrong 0 0 all, min 1.000
(11 + 2·row)·c 0 wrong 0 0 all, min 0.083
band stride, % 63 32,256 of 65,536 64,512 64,512 all, min 0.083

(11 + 2·row) is the interesting failure: it looks like it breaks the
symmetry, and it does not. The stride's period is 64 and the offset's is 128,
they turn together, and the whole seed still repeats every 128 rows — while
costing the row axis its rotation-only errors, since two rows now differ in
both terms and their permutations can agree in gcd(Δstride, 128) columns.
Strictly worse than doing nothing.

What the 2-group cannot supply is an odd period, so the band supplies one.
Two rows get the same permutation only if they agree mod 128 (the offset) and
mod 63 bands (the stride). 63 is the largest odd cycle available: the strides
must be distinct mod 128 and there are only 64 odd residues, so a longer cycle
would repeat a stride and hand the blindness straight back.

How loud it is. Exhaustive over every offset shift and all 63² band pairs
(not sampled — my first pass sampled row and got this bound wrong by 2×):

row displacement δ columns of a row wrong each wrong by at least
128 ∤ δ ≥ 64 of 128 0.083 — 21× the tolerance
δ = 128k, 63 ∤ k ≥ 64 of 128 0.159 — 41× the tolerance
δ = 128k, 63 | k 0 invisible

Headroom, measured rather than argued. The values are untouched — every row
is still a permutation of the multiples of 1/8 below 16, so bf16 holds every
input exactly and the reference is still one 128-entry table. On a B200 the
worst relative error over the checked run is 1.97e-3, a hair past the
2⁻⁹ = 1.95e-3 that rounding the output to bf16 costs on its own: the 2⁻⁸
tolerance is 2.0× what a correct kernel needs, and 46× below the
2^(1/8) − 1 = 9.05% gap between two neighbouring outputs of a row.

The residual, stated rather than hoped for. A constant row displacement is
invisible exactly when it is a multiple of 63 · ROWS = 8064 rows. That is a
rule, not an audit: 63 divides no power of two, and every band offset, grid
dimension and benchmark size in this file is a power of two, so no displacement
this grid's arithmetic can produce is a multiple of 8064. Two smaller ones, in
the doc as well: a band displacement of k ≡ ±32 (mod 63) leaves half of each
row's columns matching (the other half wrong by ≥ 0.996 — the more columns
survive, the larger the error on the ones that do not), and column 0 is
53·plane + 37·row whatever the stride, so that one column alone cannot see a
band error.

Positive control

Same discipline as #55: break it deliberately, confirm the check reacts,
revert. The control pins the kernel's TMA row coordinate for the load to 0
while the store still follows blockIdx.x, so every CTA normalizes band 0 and
writes the result to its own band — the load/store divergence the issue says
this hole is waiting for. Scoring the same launch against both seeds in one
pass costs one B200 run instead of two:

tile.tma_load(source, 0, plane, loaded);

On a B200 (modal run modal_app.py::examples):

kernel reference result
correct new pass softmax 2x256x128 rows normalized, worst relative error 1.97e-3
load row pinned to band 0 new 32,256 of 65,536 cells wrong — [0, 128, 1] = 3.58e-6, want 4.26e-6 (1.60e-1)
load row pinned to band 0 old 0 of 65,536 cells wrong — passes

The third row is the issue, reproduced: the old check certifies a kernel whose
CTAs all read band 0. 32,256 is 126 of the 128 columns of each of the 256 rows
(2 planes × 128) that read the wrong band — the 2 that survive are the columns
where Δstride · column ≡ 0 (mod 128), which is the doc's bound doing exactly
what it says. The control was then reverted and the correct kernel re-run
against the new reference, so the passing row above is not the perturbed build.

::bench re-run as well: all six softmax sizes check and time, unchanged
numbers (2.6 / 10.4 / 80.5 / 280.9 / 346.1 / 364.6 GB/s from 128 to 524,288
rows). Nothing in examples/README.md quotes a number that moved, so the only
edit there is the paragraph on what softmax's check can claim.

Not the row axis: one drive-by

modal run modal_app.py::build has been failing on main since #59, on
cargo doc --features host: a redundant explicit link target in global.rs's
module docs. Tier 1 does not see it (the item is host-gated), so it reached
main green. One line, included here because tier 2 gates this PR and cannot
pass without it.

Left alone: the 7 pre-existing clippy warnings in examples/, none of them in
lines this touches, and the count is still 7.

Closes #56

…hide

The seed's row term is `37 * row` taken mod 128, so it repeats every 128
rows — one CTA band. A kernel that loaded one band and stored to another
produced bit-identical expected values and the check passed it.

No choice of row multiplier fixes that, and the reason is structural: the
seed is an affine map of the column axis, those form a group of order 2^13
under composition, and its exponent is exactly 128. Every row-affine seed
therefore repeats inside a band. `37 -> 39` changes nothing; a stride that
follows the row, `(11 + 2*row)`, still repeats every 128 rows and costs the
row axis its rotation-only errors as well.

What the 2-group cannot supply is an odd period, so the band supplies one:
`row / ROWS % 63` picks the column stride. Two rows agree only if they agree
mod 128 and mod 63 bands, so a row displacement is invisible exactly when it
is a multiple of 8064 rows — and 63 divides no power of two, which is what
every band offset and every size here is.

Positive control on a B200, load row pinned to band 0 while the store row
still follows blockIdx.x: the new seed reports 32,256 of 65,536 cells wrong,
the old one 0.

Also unbreaks `modal run modal_app.py::build`, which has been failing on main
since #59 on a redundant explicit doc link in `global.rs`.
@feitreim
feitreim merged commit 0b021af into main Jul 28, 2026
2 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.

softmax's seed has a blind row axis: 37*128 = 0 mod 128, so the check cannot see a whole-band row error

1 participant