Repository navigation
softmax: give the row axis an odd period, so a whole CTA band cannot hide - #61
Merged
Merged
Conversation
…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`.
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.
permutation's row term was37 · rowtaken mod 128, so it repeated every128 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
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 · rowruns through all 128 residues, which is the most a linearrow 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) % 128isan affine map of the column axis,
c ↦ b·c + (a·row + c). Those maps form agroup 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
rowdividing 128, which is one band, and the band is unreachablefrom inside this family. Two tempting edits, swept over 2 planes × {256, 16384}
rows against nine wrong row walks:
11·c(current, broken)39·row(the obvious fix)(11 + 2·row)·c% 63(11 + 2·row)is the interesting failure: it looks like it breaks thesymmetry, 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
rowand got this bound wrong by 2×):δ128 ∤ δδ = 128k,63 ∤ kδ = 128k,63 | kHeadroom, 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 arule, 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 eachrow'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·rowwhatever the stride, so that one column alone cannot see aband 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 andwrites 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:
On a B200 (
modal run modal_app.py::examples):pass softmax 2x256x128 rows normalized, worst relative error 1.97e-3[0, 128, 1] = 3.58e-6, want 4.26e-6 (1.60e-1)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 exactlywhat 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.
::benchre-run as well: all six softmax sizes check and time, unchangednumbers (2.6 / 10.4 / 80.5 / 280.9 / 346.1 / 364.6 GB/s from 128 to 524,288
rows). Nothing in
examples/README.mdquotes a number that moved, so the onlyedit there is the paragraph on what softmax's check can claim.
Not the row axis: one drive-by
modal run modal_app.py::buildhas been failing onmainsince #59, oncargo doc --features host: a redundant explicit link target inglobal.rs'smodule docs. Tier 1 does not see it (the item is
host-gated), so it reachedmaingreen. One line, included here because tier 2 gates this PR and cannotpass without it.
Left alone: the 7 pre-existing clippy warnings in
examples/, none of them inlines this touches, and the count is still 7.
Closes #56