Repository navigation
Open the RegTile shape set, and mask against a coordinate origin (#23 + #7) - #52
Merged
Merged
Conversation
feitreim
force-pushed
the
issue-23-7-shapes-and-masking
branch
from
July 28, 2026 01:40
dad9a4f to
6cdc9d2
Compare
`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
force-pushed
the
issue-23-7-shapes-and-masking
branch
from
July 28, 2026 01:47
6cdc9d2 to
3306862
Compare
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.
Two issues, one PR, because they are the same blocker seen from two sides:
flash_forwardcould not name its[32, 64]score band (#23), and the unsatisfied layout bound on that band was what keptmake_causaloff 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
is
E0117whatever 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:so a tile's storage is the row array of the column array — computed by projection, never by arithmetic on
MandN, sogeneric_const_exprsstays out of it. Three consequences:M * N / 32values, 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.FragmentLayoutis 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
Methods rather than the issue's free function — every other
RegTileop is a method ands.make_causal_at(…)is what the example already spelled — but otherwise the corrected signature: every one takes a coordinate origin.diagonalon 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_attakes 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 areu32at the call site, soquery_base - key_basewritten there wraps to +4294966272 and masks nothing. It subtracts ini32. A host test carries that exact case.lanestays explicit, on #27's rule. The masks are built out of pure coordinate queries, andreg.rsanddevice-testsboth construct their expectations by calling the map across all 32 lanes on the host, wherewarp::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 mapat three shapes plus the restage); a device case would re-verifyBaseLdtmand 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, theu32wrap,tril/triucomplementarity 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
mainrather than against the numbers in the docs: all 36 pre-existing kernels report identical registers, spills and stack. (Worth noting since two entries inlane_probe's recorded table — 198 and 72 — no longer reproduce onmaineither; they are stale from before later PRs, not something this branch moved.)#7's
mask_probe, at both widths, no spills anywhere:make_causal_atThe 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 —
masktakes&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 aRegVecforlane_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: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, #31to#11, #31;layernormis untouched at#3, #13.Not landed from #7, as the issue's own sequencing asked:
transpose,swap_layout,copybetween layouts. All three need a secondFragmentLayoutto mean anything, andBaseLdtmis still the only one in tree — though after #23 a downstream crate can now write one.Closes #23
Closes #7