Skip to content

Composed movers: a whole [M, N] band, both memories, both directions - #43

Merged
feitreim merged 3 commits into
mainfrom
issue-22-composed-movers
Jul 27, 2026
Merged

feitreim merged 3 commits into
mainfrom
issue-22-composed-movers

Conversation

@feitreim

@feitreim feitreim commented Jul 27, 2026 •

Copy link
Copy Markdown
Owner

Every mover moved exactly one [16, 16] Fragment, so a kernel that wanted a
[32, 128] band wrote the block-composition loop itself. gemm.rs wrote it,
softmax.rs wrote it twice (once per direction), and device-tests kept
its own copy as drain_band/stage_band — in the test crate, so the next
kernel would have written it again. reg.rs asserted that composition as an
invariant (fragment_blocks_tile_the_bigger_shapes) while no library function
implemented it.

The API

All four directions, so they cannot drift:

impl<const R: usize, const C: usize> TmemTile<R, C> {
    pub unsafe fn tile<const M: usize, const N: usize>(self, row: u32, column: u32)
        -> RegTile<M, N, BaseLdtm> where BaseLdtm: FragmentLayout<M, N>;
    pub unsafe fn store_tile<const M: usize, const N: usize>(self, row: u32, column: u32,
        tile: RegTile<M, N, BaseLdtm>) where BaseLdtm: FragmentLayout<M, N>;
}

pub unsafe fn load_tile<E: Element<Unpacked = [f32; 2]>, const M: usize, const N: usize>(
    chunks: SwizzledChunks<E>, row: u32, column: u32, lane: u32)
    -> RegTile<M, N, BaseLdtm> where BaseLdtm: FragmentLayout<M, N>;
pub unsafe fn store_tile<E: Element<Unpacked = [f32; 2]>, const M: usize, const N: usize>(
    chunks: SwizzledChunks<E>, row: u32, column: u32, lane: u32,
    tile: RegTile<M, N, BaseLdtm>) where BaseLdtm: FragmentLayout<M, N>;

Generic exactly the way RegTile already is — the FragmentLayout<M, N> bound
and no generic_const_exprs. row/column are the band's own origin (a warp
of an M128 accumulator drains tile::<32, N>(32 * warp_id, 0)), not block
indices. The shared side takes SwizzledChunks, like load_fragment/
store_fragment, so callers pass tile.chunk_writer().

Both pairs are built out of one place_block/take_block pair (in tmem.rs,
since the composition is a property of BaseLdtm — the LDTM map — and ldst
already documents itself as its shared-memory mirror). A band therefore cannot
mean one thing in TMEM and another in shared memory. drain_band/stage_band
were lifted, not redesigned.

Registers — modal run modal_app.py::regcount, before and after

The TMEM side, every kernel in device-tests that used the hand-written loops:

kernel before after
fragment_map_16x16 18 18
fragment_map_32x32 40 40
fragment_map_32x128 32 32
fragment_restage_32x128 (drain and store at 128) 40 40
reduction_probe (two drains, 32 and 128 wide) 255 255
sttm_roundtrip 34 36

Spills, stack and smem are unchanged everywhere, including on sttm_roundtrip
(0 spills, 1024-byte frame in both). So the composed form is register-neutral
at both widths #5 and #6 warned about, and costs 2 registers on one probe —
which is not a case for an in-place drain_into: the shapes where a by-value
tile has ever been expensive here (128 columns, row_map 168 → 255) are
exactly the ones that did not move.

The shared side had nothing to compare against, so I measured it directly: the
new band_roundtrip kernel, once with the composed movers and once with the
hand-written loops softmax.rs used to carry (same file, same shape, one
temporary edit, not committed).

band_roundtrip, [32, 128] both directions registers
hand-written composition loops 52
load_tile / store_tile 36

Composed is cheaper by 16 registers, no spills either way. So: no in-place
form, and no regression to report other than the +2 above.

Device coverage

Rebased on 085cf3d (#9 landed mid-flight, and softmax now runs), so
15 → 18 cases with #41's three; all green on a B200.
The TMEM half needed no new case, which
is the point: fragment map (16,16)/(32,32)/(32,128) and sttm restage are
hardware validation of drain_band/stage_band, and they now call
TmemTile::tile/store_tile unchanged.

The shared half had none — nothing in the harness monomorphized
ldst::load_tile/store_tile, so they would have shipped both unmeasured and
unvalidated. band round trip copies rows 0..32 of a [64, 128] staged tile
into its rows 32..64 through both composed movers. 128 columns crosses the
stacked-subtile stride, and the store's row base is non-zero: the two ways a
composition loop can be wrong that a single [16, 16] block cannot show.

pass  band round trip             8192 elements placed exactly
18 of 18 cases passed

And, because #9 landed first, the shared pair is now exercised by a whole
kernel on hardware as well:

pass  gemm              512x256x256 exact
pass  softmax           2x256x128 rows normalized, worst relative error 1.97e-3

One host test comes with the helpers (block_placement_is_the_composed_ coordinate): it ties the movers' storage indexing to RegTile::coordinate in
both directions, which is the invariant reg.rs states and nothing previously
implemented.

Examples

Verified

  • modal run modal_app.py::build — clippy on all three crates, host tests (47),
    real cargo oxide build --arch sm_100a for device-tests and the examples.
  • modal run modal_app.py::regcount — four runs: before, after, the
    hand-written shared-band A/B, and a re-measure after the rebase (identical to
    the after column).
  • modal run modal_app.py — 18/18 on a B200.
  • modal run modal_app.py::examples — gemm exact, softmax within 2e-3, on a
    B200. Two GPU runs rather than one: softmax became a running kernel under
    me, and this PR rewrites its inner loop.
  • cargo clippy --all-targets, cargo fmt --check clean in all three crates.

Closes #22

feitreim added 3 commits July 27, 2026 19:25
Every mover moved exactly one [16, 16] Fragment, so a kernel holding a
RegTile<32, 128> wrote the block-composition loop itself — gemm.rs did,
softmax.rs did twice, and device-tests kept its own copy in the test
crate. reg.rs asserted that composition as an invariant
(fragment_blocks_tile_the_bigger_shapes) while no library function
implemented it.

TmemTile::tile / TmemTile::store_tile and ldst::load_tile /
ldst::store_tile compose the single-block movers out of one pair of
placement helpers, so a band cannot mean one thing in TMEM and another
in shared memory. All four land together for the same reason.

The helpers live in tmem.rs and ldst.rs uses them: the composition is a
property of BaseLdtm, which is the LDTM map, and ldst already documents
itself as its shared-memory mirror.
drain_band/stage_band were the reference implementation this issue
lifted, so deleting them in favour of TmemTile::tile/store_tile is the
strongest test available: `fragment map` at (16,16), (32,32), (32,128)
and `sttm restage` already validate them against silicon, and they are
unchanged.

The shared side had no such case — nothing in the harness monomorphized
ldst::load_tile/store_tile, so they would have shipped both unmeasured
by regcount and unvalidated. `band round trip` copies rows 0..32 of a
[64, 128] staged tile into its rows 32..64 through both composed movers:
128 columns crosses the stacked-subtile stride and the store's row base
is non-zero, which are the two things a composition loop can get wrong
that a single block cannot show.
gemm's twelve-line drain and softmax's two hand-written composition
loops become one call each. flash and layernorm named this API at their
call sites before it existed; they now name the real one, and neither
has a mover error left — flash's remaining list is #7, #11, #23, #31,
layernorm's is #3, #9, #13 plus scalar broadcasts, and softmax's is
exactly #9 (`tma_store`, `tma_store_commit`, `tma_store_wait`) and
nothing else. It stays aspirational for that reason alone.

README §"Came from writing kernels" item 2 and GAPS §2.1/2.1a updated
to match.
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.

Movers work in [16,16] blocks; every kernel rewrites the same composition loop

1 participant