Skip to content

Open the RegTile shape set, and mask against a coordinate origin (#23 + #7) - #52

Merged
feitreim merged 3 commits into
mainfrom
issue-23-7-shapes-and-masking
Jul 28, 2026
Merged

feitreim merged 3 commits into
mainfrom
issue-23-7-shapes-and-masking

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Two issues, one PR, because they are the same blocker seen from two sides: flash_forward could not name its [32, 64] score band (#23), and the unsatisfied layout bound on that band was what kept make_causal off its error list (#7). Shipping either alone would have left a state where a reader who counts compiler errors concludes the other has landed. One commit each so they review apart.

#23 — the orphan-rule finding, and the option it forces

Option 1 (export the macros) is not available. An out-of-tree

impl FragmentLayout<32, 64> for BaseLdtm { … }

is E0117 whatever macro writes it — "impl doesn't have any local type before any uncovered type parameters". FragmentLayout<const M, const N> takes only const parameters, so there is no type position a downstream crate could put a local type in, and no #[macro_export] changes that. Checked by compiling it in a scratch crate against this library, not by reading the reference. Widening the in-tree list (option 2) would have passed the examples-crate acceptance test while leaving the complaint — every new shape is a library change — exactly where it was.

What landed is option 3, in its trait-projection form. RowLayout::Slots<T> becomes generic in the element:

type Slots<T: Copy>: Copy;                    // = [T; M/8]

impl<const M: usize, const N: usize, L: RowLayout<M> + ColLayout<N>> FragmentLayout<M, N> for L {
    type Storage = L::Slots<L::Values>;       // = [[f32; N/4]; M/8]
}

so a tile's storage is the row array of the column array — computed by projection, never by arithmetic on M and N, so generic_const_exprs stays out of it. Three consequences:

  • The shape set is the product of the extents, not a list of pairs. The extent lists now run to every multiple of 16 up to 512: 1024 shapes out of 64 impls. The bound is the register file rather than a guess — a thread holds M * N / 32 values, so with the other extent at its 16 minimum, 512 is already 256 registers, one past what the hardware has. No shape outside the grid fits in registers at all.
  • A layout defined out of tree is a tile layout for free, the moment it has both halves. That is the openness orphan rules do allow, and it is now the supported extension point.
  • FragmentLayout is never implemented directly any more, here or downstream.

Verified out of tree from a scratch crate depending on this library: RegTile<32, 64, BaseLdtm> compiles, and so does a [64, 80] tile on a layout struct defined entirely in that crate.

#7 — the masking API

tile.mask(lane, fill, keep);                              // the mechanism
tile.tril(lane, diagonal, fill);   tile.triu(…);
tile.make_causal(lane, diagonal, fill);   tile.make_causal_t(…);
tile.make_causal_at(lane, query_base, key_base, fill);
tile.right_fill(lane, column, fill);   left_ / upper_ / lower_fill(…);

Methods rather than the issue's free function — every other RegTile op is a method and s.make_causal_at(…) is what the example already spelled — but otherwise the corrected signature: every one takes a coordinate origin. diagonal on the triangular masks, a signed index on the fills, where negative and past-the-extent are the ordinary ragged-tail cases and not errors.

make_causal_at takes the two origins instead of their difference, and that is not sugar: the difference is negative for every band above the diagonal — the fully masked ones — and both bases are u32 at the call site, so query_base - key_base written there wraps to +4294966272 and masks nothing. It subtracts in i32. A host test carries that exact case.

lane stays explicit, on #27's rule. The masks are built out of pure coordinate queries, and reg.rs and device-tests both construct their expectations by calling the map across all 32 lanes on the host, where warp::lane_id() does not exist.

Testing: host, and I concluded no device case is warranted. A mask is lane-local arithmetic over an ownership map the device suite already checks against silicon four ways (fragment map at three shapes plus the restage); a device case would re-verify BaseLdtm and nothing else. What the host does cover, over all 32 lanes: five (query_base, key_base) pairs against a global score matrix, bands wholly above and wholly below the diagonal, the u32 wrap, tril/triu complementarity at seven diagonals including two that miss the tile, the fills at bounds inside and outside it, and the mechanism itself against a predicate that is not a triangle. Device suite stays 18/18; both example kernels still produce correct numbers on a B200.

Registers

modal run modal_app.py::regcount, sm_100a.

#23 is free, measured against main rather than against the numbers in the docs: all 36 pre-existing kernels report identical registers, spills and stack. (Worth noting since two entries in lane_probe's recorded table — 198 and 72 — no longer reproduce on main either; they are stale from before later PRs, not something this branch moved.)

#7's mask_probe, at both widths, no spills anywhere:

32 columns 128 columns
unmasked 31 157
make_causal_at 32 71
open-coded loop 32 71

The claim this prices is the last two rows: the op costs exactly what the index math it deletes costs, at both widths, and one register over no mask at all at 32 — mask takes &mut self, so there is no second band live, which is #38's peak-liveness reading rather than a by-value one. The unmasked column is not a lower bound at 128, and the gap is the probe: with no mask pass between them the load loop and the exponential fuse into one wide batch and ptxas holds more of the band live. The probe accumulates into a RegVec for lane_probe's reason — a [32, 128] accumulator puts it on the 255-register ceiling, where every form reads alike by force and a null would mean nothing.

Flash's remaining error list

Read off a compiler, not off memory — modal run modal_app.py::gaps (added here) checks each aspirational feature on its own:

src/flash_forward.rs:43:5:   error[E0432]: unresolved import `kittens::global::store_rows`
src/flash_forward.rs:224:25: error[E0599]: no method named `add_assign` found for struct `RegTile`

Two errors, both at the ends of the kernel: #11 (fp32 out of registers) and #31 (the in-place tile add). Everything between the score band arriving in registers and the probabilities going back to shared is library API now. The kernel therefore stays aspirational and its README row goes from #7, #11, #23, #31 to #11, #31; layernorm is untouched at #3, #13.

Not landed from #7, as the issue's own sequencing asked: transpose, swap_layout, copy between layouts. All three need a second FragmentLayout to mean anything, and BaseLdtm is still the only one in tree — though after #23 a downstream crate can now write one.

Closes #23
Closes #7

@feitreim
feitreim force-pushed the issue-23-7-shapes-and-masking branch from dad9a4f to 6cdc9d2 Compare July 28, 2026 01:40
feitreim added 3 commits July 27, 2026 21:47
`BaseLdtm` implemented `FragmentLayout` for `(16,16)`, `(32,32)` and
`(32,128)` and nothing else, because each shape was a line of a private
`base_ldtm_shapes!`. Flash wants a `[32, 64]` band and could not add one.

The issue's cheap option -- export the macro -- is not available. An
out-of-tree `impl FragmentLayout<32, 64> for BaseLdtm` is E0117 whatever
macro writes it: the trait takes only const parameters, so no local type
can ever appear in the impl header and the orphan rules reject it.
Checked by compiling it, not by reading the reference.

So take the third option, in its trait-projection form.
`RowLayout::Slots<T>` is generic in the element, which makes a tile's
storage the row array *of* the column array, and `FragmentLayout` a
blanket impl over `RowLayout<M> + ColLayout<N>`. No `(M, N)` needs an
impl, no array length is computed from `M` and `N`, and
`generic_const_exprs` stays out of it. The extent lists then run to every
multiple of 16 up to 512 -- 1024 shapes out of 64 impls, and every shape
that fits in a thread's registers at all, since `M * N / 32` is already
256 at the corner.

The blanket impl is also the openness that orphan rules do allow: a
layout defined in another crate is a tile layout the moment it has both
halves, with no line here.

`regcount` on sm_100a: every one of the 36 pre-existing kernels reports
the same registers, spills and stack as on main. The storage is the same
`[[f32; N/4]; M/8]`; only its spelling moved.
`RegTile::mask(lane, fill, keep)` selects at the layout's own logical
`(row, column)`, in place, and `tril`/`triu`, `make_causal`/
`make_causal_t` and the four fills are one line each on top of it.

Every one takes a coordinate origin the issue as filed does not have.
ThunderKittens masks a tile against its own `row == column`, which is
right for exactly one block of a tiled kernel: a flash band is a
`[queries, keys]` sub-block whose diagonal sits at
`query_base - key_base`. The origin-free form compiles, runs, and is
wrong everywhere off the diagonal -- so `diagonal: i32` on the
triangular masks, and a signed index on the fills, where negative and
past-the-extent are the ordinary ragged-tail cases rather than errors.

`make_causal_at(lane, query_base, key_base, fill)` takes the two origins
instead of their difference, and that is not sugar: the difference is
negative for every band above the diagonal -- the fully masked ones --
and both bases are `u32` at the call site, so `query_base - key_base`
written there wraps to +4294966272 and masks nothing. It subtracts in
`i32`. The host tests carry that case.

`lane` stays explicit, on #27's rule: the masks are built out of pure
coordinate queries, and `reg.rs` and `device-tests` both construct their
expectations by calling the map across all 32 lanes on the *host*, where
`warp::lane_id()` does not exist. That is what keeps the coverage from
restating the kernel.

Host-tested exhaustively over all 32 lanes: bands wholly above and
wholly below the diagonal, five (query_base, key_base) pairs against a
global score matrix, the `u32` wrap, tril/triu complementarity at seven
diagonals, and the fills at bounds inside and outside the tile. No device
case: a mask is lane-local arithmetic over an ownership map the suite
already checks against silicon four ways, so a device case would
re-verify `BaseLdtm` and nothing else. Suite stays 18/18.

`mask_probe` in `device-tests` is the codegen half (`regcount`,
sm_100a, no spills):

                   regs   stack
     32 unmasked     31     256
     32 causal       32     256
     32 by hand      32     256
    128 unmasked    157    1536
    128 causal       71    1536
    128 by hand      71    1536

`make_causal_at` and the open-coded loop it replaces are register-
identical at both widths, and one register over no mask at all at 32 --
`mask` takes `&mut self`, so there is no second band live, which is the
peak-liveness reading #38 arrived at. The unmasked column is not a lower
bound at 128: with no mask pass between them the load loop and the
exponential fuse into one wide batch and ptxas holds more of the band
live.
`flash_forward` names its `[32, 64]` score band and masks it with
`make_causal_at` -- the two calls #23 and #7 were filed from. The kernel
stays aspirational: `cargo check --features flash` now reports exactly
two errors, `global::store_rows` (#11) and `RegTile::add_assign` (#31),
both at the ends of the kernel, with everything between the band
arriving in registers and the probabilities going back to shared now
library API.

That list is read off a compiler rather than off memory:
`modal run modal_app.py::gaps` checks each aspirational feature on its
own, so an error belongs to a known kernel, and reports the expected
non-zero exit instead of raising on it.

The README keeps the reason #7 looked closed when it was not -- an
unsatisfied layout bound on the receiver suppresses method resolution,
so `make_causal_at` never appeared as its own error and counting errors
would have said it had landed.
@feitreim
feitreim force-pushed the issue-23-7-shapes-and-masking branch from 6cdc9d2 to 3306862 Compare July 28, 2026 01:47
@feitreim
feitreim merged commit 6181068 into main Jul 28, 2026
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.

RegTile's shape set is closed to out-of-tree kernels Masking and shape utilities: make_causal, tril/triu, fills, transpose

1 participant