Skip to content

A shared tile out to global memory, by plain vectorized stores (#15) - #113

Merged
feitreim merged 1 commit into
mainfrom
drain-shared-to-global
Jul 29, 2026
Merged

feitreim merged 1 commit into
mainfrom
drain-shared-to-global

Conversation

@feitreim

Copy link
Copy Markdown
Owner

The epilogue's shared→global half without the TMA engine — the route
NVIDIA's own gemm_sol_final takes at cuda-oxide b099f64
(crates/rustc-codegen-cuda/examples/gemm_sol_final/src/kernels.rs, hash
confirmed against the checkout, not assumed from the directory name):
stmatrix into a staging tile, read it back, and issue wide contiguous global
stores. #109 measured our register-side epilogue as issue-bound; this is the
primitive the kernel that answers that will be built on.

kittens::global::store_shared_rows copies a [R, C] SharedTile to a
rectangle of a row-major buffer at a runtime leading dimension.

The swizzle: option (a), and why

The global side is walked contiguously and the tile's own cursor is asked
where each chunk went
(SwizzledChunks::at). Three reasons, in order of how
much they decided it:

  1. The 16-byte chunk is the unit both sides already agree on. SWIZZLE_128B
    permutes 16-byte chunks and never the bytes inside one, and 16 bytes is also
    the widest store PTX has (st.global.v4.b32). So the chunk is simultaneously
    the largest run contiguous in both address spaces and the largest one an
    instruction could have carried anyway. The swizzle cannot cost the global
    side anything, because there was nothing above 16 bytes to lose.
  2. It costs the shared side nothing either. The XOR is over the chunk index
    within one 128-byte row, and a 128-byte row is exactly the 32 banks once
    over — eight lanes reading the eight chunks of a row touch every bank exactly
    once, permuted or not.
  3. Option (b) is not this PR's to decide. SharedVec's docs already file an
    unswizzled tile under Swizzle modes: 32B, 64B, and unswizzled #14 and name its precondition (ATOM_BYTES has to stop
    meaning "TMA box width" first). It would also move the disorder onto the
    stmatrix that fills the tile, where a power-of-two row pitch is the
    conflict the swizzle exists to prevent.

(1) and (2) are arguments about addresses. Neither is measured, and the doc
block says so in those words.

Access width

Reuses GlobalRows::runs_aligned rather than inventing a second test.
access_width descends 16, 8, 4 bytes to the element and takes the first the
cursor admits, so an odd ldc gets narrower stores instead of a
misaligned-address fault. It is a pure public function so a host test reads
the ladder off directly instead of inferring it from which branch ran. The
2-byte rung is guarded on E::BYTES == 2 — a cursor aligned for a 4-byte
element is aligned for a 4-byte access at every column, so at fp32 that arm is
const-dead and not emitted.

Scope: cooperative, not collective

THREADS threads split the chunks by a flat index; no barrier, no fence, no
wait inside. (lane, 32) is warp scope, (threadIdx.x, 128) is the four
epilogue warps — the reference's arrangement, and the one that puts a warp's 32
accesses on consecutive chunks of a row. Split by chunk and not by row because a
row split needs R to divide by the warps and the warp's rows by 32.

There is deliberately no fence.proxy.async.shared::cta, and the doc block
says why at length, because its absence is exactly the thing a reader assumes is
a bug: both ends are generic-proxy, so a bar.sync between the stmatrix
writes and these reads is the whole obligation, and it belongs to the caller,
who is the only one who knows whether the writes were another warp's.

Device test

shared drain / shared drain wide: four warps fill a [64, C] staging tile
with position identities through store_tile, one sync_threads, then drain to
a rectangle at row 8 of an 80×208 bf16 matrix seeded end to end with poison.
The host checks every element of the matrix — including the margins above,
below, left and right that had to stay poison. The pitch is wider than either
tile and not a multiple of one, so a wrong stride is a wrong answer.

column is a kernel argument, so the four DRAIN_COLUMNS [64, 68, 66, 65]
run the four rungs of the ladder off one kernel.

38 of 38 device cases pass on a B200 (scripts/modal-run device_tests);
tier 2 build is green on both kernel crates.

What this does not establish

  • No performance claim of any kind. Nothing here was timed. The bank-conflict
    and coalescing arguments above are about addresses only.
  • The device case cannot say which rung ran. A drain that always issued
    2-byte stores passes all four columns. Only access_width's host tests say
    which width a cursor gets, and they say it against a synthetic base address —
    the device rungs follow from the driver's ≥256-byte allocation alignment,
    which is stated but not asserted.
  • One tile shape family. [64, 64] and [64, 128] bf16 at 128 threads, one
    destination origin, one pitch. No fp32 SharedTile is exercised anywhere (the
    crate has none), so the E::BYTES == 4 path is host-tested only.
  • The absent fence is argued, not demonstrated. As with check_store_ring,
    a case is built so a violation is a wrong value rather than a plausible one;
    it is not built to force one.

The epilogue's shared->global half without the TMA engine, which is what
NVIDIA's own `gemm_sol_final` does at cuda-oxide b099f64: `stmatrix` into a
staging tile, then read it back and issue wide contiguous global stores.

`store_shared_rows` copies a `[R, C]` `SharedTile` to a rectangle of a
row-major buffer at a runtime leading dimension. The 16-byte swizzle chunk is
the unit both sides agree on -- SWIZZLE_128B permutes chunks within a
128-byte row and never the bytes inside one, and 16 bytes is also the widest
store PTX has -- so the global side is walked contiguously and the tile's own
cursor is asked where each chunk went. The width ladder reuses
`GlobalRows::runs_aligned`, descending 16, 8, 4 bytes to the element.

Cooperative, not collective: `THREADS` threads split the chunks by a flat
index, with no barrier, no fence and no wait inside. There is deliberately no
`fence.proxy.async.shared::cta` -- both ends are generic-proxy, so a
`bar.sync` between the `stmatrix` writes and these reads is the whole
obligation, and it belongs to the caller.

Device case `shared drain` / `shared drain wide`: four warps fill a staging
tile with position identities, drain it to a rectangle inset in a
80x208 bf16 matrix seeded with poison, and the host checks every element
including the margins that had to stay poison. Four column origins run the
four rungs of the ladder off one kernel.
@feitreim
feitreim merged commit 5a0ad7f into main Jul 29, 2026
3 checks passed
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.

1 participant