Skip to content

Global to and from registers, with no descriptor in the way (#11) - #59

Merged
feitreim merged 1 commit into
mainfrom
global-register-movers
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
global-register-movers

Conversation

@feitreim

Copy link
Copy Markdown
Owner

kittens::global::GlobalRows is a base address and a leading dimension — the
whole of what a thread needs to address a row-major buffer itself — and
load_rows / store_rows move an [M, N] register tile through it at the
coordinates the fragment layout already knows. ThunderKittens'
global_to_register.cuh, for the fp32 epilogue that had to round-trip through
a bf16 shared tile to reach global memory, and for the small operand not worth
a host-built descriptor.

store_rows(GlobalRows::from_slice(&mut c, ldc as usize), row, column, lane, band);

Three decisions, not transcription

  • global.rs, not ldst.rs. ldst is one address derivation
    (fragment_address) shared by ldmatrix and stmatrix; this shares none of
    it, and its question — how a global buffer is described — is global.rs's.
    It also cannot be called store_tile: flash_forward imports
    ldst::store_tile in the very file that wants this one.
  • fp32, not generic over Element. The parameter belongs there and cannot
    go there yet — Element is Bf16-only until Element carries MMA kind and conversion, ready for sub-bf16 types #2, so GlobalRows<E> would
    have exactly one instantiation and it would be the wrong one. A RegTile
    is fp32 and this path exists to move it without rounding. Element carries MMA kind and conversion, ready for sub-bf16 types #2 makes it a
    bound and two E::read/E::write calls.
  • The only mover generic over FragmentLayout. ldmatrix, stmatrix and
    LDTM each fix a lane map in hardware; a plain st.global fixes nothing, so
    these two take any L.

No bounds checking, deliberately: predicating every value would be paid by the
epilogues that do divide. TK's ragged-tail loads still want it, and that plus
the vector shapes is what GAPS §2.2 now says is left.

Examples

gemm's epilogue was the twelve lines of open-coded RegTile::coordinate
arithmetic this library exists to delete. It is one call now and the file has
no GAP block left. flash_forward's import resolves, taking its error
list from two to one (RegTile::add_assign, #31) — confirmed by ::gaps,
not by reading the file.

Registers, both widths

regcount's new global_copy_probe_* price both movers against the loop they
replace, at equal work:

32 columns 128 columns
open-coded, RegTile::coordinate per value 56 regs, 0 B stack 168 regs, 1040 B stack
load_rows + store_rows 48 regs, 0 B stack 44 regs, 528 B stack

Cheaper rather than free, which is #22's direction again: the movers form the
row address once per slot where a call site multiplies out per value, so a
[32, 128] band costs 4 multiplies by the leading dimension instead of 128.
No spill at either width, and every pre-existing kernel in the table is
unchanged.

Checks run

::build clippy + 73 host tests + sm_100a device builds of both kernel crates, all three crates clean
::gaps flash down to add_assign (#31); layernorm unchanged
::regcount table above
device tests (B200) 22 of 22, including the new global rows map
::examples (B200) gemm 512x256x256 exact, softmax unchanged
cargo fmt --check clean in root, device-tests/, examples/

The new device case covers the load only: it reads a [32, 128] band out
of a 64x192 fp32 matrix at (16, 40) — off the buffer's corner, off a
16-column block boundary, at a pitch that is not the band's width — and dumps
by (lane, slot, value) with the host applying the ownership map. It is the
only case in the harness whose source reaches registers with no descriptor
between them. The store direction gets no case, because gemm's epilogue
already is one: store_rows at a runtime ldc wider than the band, non-zero
column origin, four warps, and all 512x256 outputs compared for equality.

Four host tests for the address map itself, including the one property the
store rests on — a warp's threads name every element of the destination
rectangle exactly once.

Closes #11

`global::GlobalRows` is a base address and a leading dimension — the whole
of what a thread needs to address a row-major buffer itself — and
`load_rows`/`store_rows` move an `[M, N]` register tile through it at the
coordinates the fragment layout already knows.

This is ThunderKittens' `global_to_register.cuh`: the path for an fp32
epilogue, which had to round-trip through a bf16 shared tile to reach global
memory, and for a small operand not worth a host-built descriptor.

It lives in `global.rs` rather than `ldst.rs` because `ldst` is one address
derivation over `ldmatrix`/`stmatrix` and this shares none of it — and
because `flash_forward` imports `ldst::store_tile` in the same file that
wants this one. It is fp32 rather than generic over `Element`, which is
bf16-only until #2, and it is the only mover in the crate generic over
`FragmentLayout`: every other one has a lane map fixed in hardware, and a
plain `st.global` fixes nothing.

gemm's epilogue was the twelve lines of open-coded `RegTile::coordinate`
arithmetic this library exists to delete; it is now one call, and the file
has no `GAP` block left. flash's error list goes from two to one (#31).

Measured, both directions, against the loop they replace
(`regcount`'s new `global_copy_probe_*`): 48 registers against 56 at 32
columns, 44 against 168 at 128, no spill anywhere. Cheaper rather than free,
and #22 found the same direction — the movers form the row address once per
slot where a call site multiplies out per value.

Checked: `global rows map` (device) reads a band out of a pitched fp32
matrix at an off-block origin and dumps by thread coordinate, the host
applying the ownership map — the only case here whose source never touches
the TMA engine. The store direction is `gemm`, whose 512x256 output is
compared elementwise for equality, and it still passes. Four host tests for
the address map itself.
@feitreim
feitreim force-pushed the global-register-movers branch from e115add to afcf606 Compare July 28, 2026 02:26
@feitreim
feitreim merged commit f492f47 into main Jul 28, 2026
feitreim added a commit that referenced this pull request Jul 28, 2026
…hide (#61)

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

Direct global to/from register load and store

1 participant