Skip to content

Shared vectors: an unswizzled run of elements, and the box to deliver it - #53

Merged
feitreim merged 5 commits into
mainfrom
issue-13-shared-vectors
Jul 28, 2026
Merged

feitreim merged 5 commits into
mainfrom
issue-13-shared-vectors

Conversation

@feitreim

Copy link
Copy Markdown
Owner

SharedVec<E, N> — one flat, unswizzled run of elements in shared memory —
with a TMA path in both directions at rank 1 and rank 2, a TileBox impl,
scalar element access, and the ColVec broadcast on top. layernorm_rows
leaves its feature gate as a result.

The swizzle decision, and why it is the crux

SharedVec does not go through Swizzle at all. It is not a SwizzleNone
mode, and adding one is still #14's job.

The argument is that Swizzle::ATOM_BYTES does two jobs in shared.rs — it is
the swizzle period and the width of a TMA box (SUBTILE_COLS = ATOM_BYTES / E::BYTES) — and an unswizzled vector wants neither:

  • Its box is N wide, a number that depends on the vector, not on a mode.
    A mode marker has no element type and no length, so it cannot carry it.
  • Any atom small enough to divide a short vector splits it into stacked
    subtiles
    , which tma_load would then fetch one instruction each. The
    natural "no swizzle" atom is 16 bytes (the TMA line); at bf16 that turns a
    [128] vector into 16 boxes of 8.
  • A 128-byte atom reimposes exactly the padding the issue was filed about —
    SUBTILE_COLS = 64, so SharedVec<Bf16, 32> would not even typecheck, and
    the narrowest legal one spends a whole atom on its single row.

Beneath all three: swizzling exists to keep a 2-D tile's 16-byte chunks off
the same banks when successive rows are read together, and the XOR is over the
row index. A vector has one row for the phase to come from, and its actual
access pattern is a broadcast — under BaseLdtm the 8 lanes of a column group
ask for the same address, which shared memory answers from one bank read. There
is nothing for a swizzle to spread.

Deciding what ATOM_BYTES means in the absence of a swizzle is a real
question, but it is a question about tiles: an unswizzled [R, C] staging
tile does need a Swizzle impl, and it needs the box-width job separated from
the period job first. That is #14, and borrowing the mode marker here would
have answered it by accident. SharedVec<Bf16, 32> is 64 bytes; the narrowest
SharedTile that typechecks is 128 for one row.

What landed

src/shared.rs

  • SharedVec<E, const N: usize> — base pointer plus compile-time length, Copy,
    built by the kernel like every other shared type here (from_raw).
  • Const asserts: N * E::BYTES a multiple of 16 (the unswizzled box's
    granularity — a swizzled box gets this free by being an atom wide), and
    N <= 256 (the descriptor's box cap).
  • at/get/set, and tma_load / tma_load_2d / tma_store /
    tma_store_2d. One instruction each: an unswizzled box has no atom to be cut
    into, so a vector never walks stacked subtiles.
  • Element gains read/write, the scalar half beside the word half.
    This is a required-method addition to a trait Element carries MMA kind and conversion, ready for sub-bf16 types #2 is about to implement for
    F32
    — two one-liners there (*(at as *const f32) and its inverse). It is
    not avoidable: Element::Unpacked is an opaque Copy type with no way to
    index a lane out of it, so unpack(word)[i % PER_WORD] does not typecheck
    generically, and bounding Unpacked = [f32; 2] (as store_fragment does)
    would exclude fp32 — the one element the Block-scope reductions: the fold four warps need, and the fp32 Element under it #3 consumer actually needs. The
    write being per element rather than per word is also load-bearing: warp w
    writing index w must not read-modify-write a neighbour's partial.

src/global.rs

  • TileBox for SharedVec: BOX_COLS = N, BOX_ROWS = 1, SWIZZLE = NONE.
    BOX_ROWS = 1 is what makes a rank-1 layout legal — descriptor_shape
    already asserted exactly that, waiting for something to satisfy it.
  • check_driver_requirements now checks the innermost box width in bytes. A
    vector's box is the first shape in this library that can violate it.

src/ldst.rs — load_vec / store_vec, plain scalar loads rather than a
matrix instruction.

Dependencies this resolves

#2 — its comment said TileBox is implemented only for SharedTile and
that "whatever #13 lands as a shared-vector type is the natural second TileBox
impl, and it will want an unswizzled mode beside it". First half done, second
half deliberately not: the unswizzled mode is #14 and is not needed. What
still gates fp32 is only impl Element for F32, which is #2's ~15 lines (plus
the two above); SharedVec is generic over E: Element and needs no change.

#3 — the open question was whether a block reduction takes a scratch
pointer or a SharedVec. It can now take a SharedVec, and it is still the
kernel's allocation: from_raw takes a base pointer, so nothing about the
shared plan moved into the library. set(warp, partial) / get(w) is the
"one warp writes, four warps read" access, and it is per element for that
reason. The fold itself is still #3's, and under it #2's — SharedVec<F32, 4>
does not exist until Element is implemented for fp32.

Out of scope, deliberately

Ops on shared tiles — the other half of the issue title. Left out, and not
only for review size. A map or reduction over a SharedTile is either
"load to registers, apply, store back" — which the library already composes out
of load_tile/store_tile and which a kernel can write today in three lines —
or a genuinely in-shared-memory op, which immediately needs a decision about
which warp touches which rows. That is #3's Scope, not a new impl. Filing
the remainder against a landed #3 will produce a better answer than guessing at
it here; happy to take a follow-up issue.

Also not done: a RegVec (row) mirror of load_vec, and the reduction store
forms. Neither is demanded by a kernel in the tree.

Testing

Host (cargo test --features host, 63 passing, was 50):
a_vectors_elements_are_flat_and_disjoint (every index a distinct address,
E::BYTES apart, covering exactly BYTES), a_vector_costs_no_padding_where_a_tile_would,
reading_a_vector_widens_the_element_at_that_index (against real bytes),
a_vectors_box_is_its_whole_length_at_one_row (rank 1 and rank 2),
a_vectors_box_is_unswizzled_where_a_tiles_is_not,
an_unswizzled_box_must_still_be_a_whole_number_of_16_byte_lines.

Device — two cases, suite 18 → 20/20 on a B200.

shared vector round trip runs the whole loop (global → shared → registers →
shared → global) and each claim fails distinguishably:

  • VECTOR = 128 bf16 is 256 bytes, twice a swizzle atom, so a box quietly
    capped at one atom delivers half the vector.
  • The register dump is indexed by (lane, value) and the host applies
    BaseLdtm::column, so a wrong broadcast reports the column the hardware
    handed that register.
  • 32 lanes then write four 2-byte elements each; a word-wide write would lose
    one of every adjacent pair.
  • The store destination is seeded with POISON, which no identity bit pattern
    can be, so an unwritten or short store is not a plausible neighbour.

shared vector row slices row 2 of a [128, 4] buffer through the rank-2 box.
Every element carries its source row, so a descriptor that ignored the minor
coordinate reports the row it delivered rather than a mismatch. This is also
the first thing in the tree to exercise a rank-1 GlobalLayout at all —
the README's "expressible but untried" is now retired.

Nothing about the answer is in either kernel.

Examples

layernorm_rows is out of its feature gate and in the default build,
compiled for sm_100a by modal run modal_app.py::build. groupnorm_tile
keeps the layernorm feature — the #[cfg] moved onto the kernel, which
#[cuda_module] honours, so the file that documents the gap is the file that
builds.

What is left for layernorm, verified by running the check rather than
inferring it (modal run modal_app.py::gaps, added here for that purpose):
under --features layernorm there is now exactly one error,
unresolved import kittens::sync::block_reduce_sum. That is #3, in
groupnorm_tile only. Under it sits #2 — the partials want to be fp32.

Verified

  • modal run modal_app.py::build — clippy on all three crates, 63 host tests,
    real cargo oxide build --arch sm_100a for both kernel crates. Clean.
  • modal run modal_app.py — 20 of 20 cases on a B200.
  • modal run modal_app.py::examples — gemm exact, softmax within 2e-3.
  • modal run modal_app.py::gaps — one error under --features layernorm.
  • cargo clippy --all-targets, cargo fmt --check clean at the root. The
    examples crate's clippy warnings are unchanged from main (softmax.rs,
    gemm.rs, bench.rs); layernorm.rs contributes none.

Closes #13

feitreim added 5 commits July 27, 2026 21:50
A vector is not a one-row tile. Swizzling exists to keep a 2-D tile's
16-byte chunks off the same banks when successive rows are read
together, and the phase is the row index -- a vector has one row for it
to come from. So Swizzle is not consulted at all here, rather than
gaining a 'none' mode: ATOM_BYTES is both the swizzle period and the
width of a TMA box, and an unswizzled vector wants neither. Its box is N
wide, which no mode marker can carry, and any atom small enough to
divide a short vector would cut it into stacked subtiles the engine
fetches one instruction each. Deciding what ATOM_BYTES means without a
swizzle is #14's question, about tiles; borrowing the marker to get a
vector would have answered it by accident.

Element gains the scalar half of its packing beside the word half.
Unpacked is an opaque Copy type with no way to index a lane out of it,
so the word form cannot serve an element index however a caller bounds
it -- and a per-element write is what keeps two lanes writing
neighbouring indices from read-modify-writing one word, which is exactly
the block-reduction case (#3) this type exists to make expressible.
The trait was implemented only for SharedTile, so GlobalLayout could
describe a parameter buffer but nothing could hand it a box for one
(#8's finding, and the half of #2 that was not about elements). A
SharedVec answers the same three questions differently: BOX_COLS = N
rather than one swizzle atom, BOX_ROWS = 1 -- which is what makes a
rank-1 layout legal at all, since descriptor_shape asserts exactly that
-- and SWIZZLE_NONE.

A swizzled box satisfies the engine's 16-byte innermost granularity for
free, its width being an atom; an unswizzled one is its own length and
is the first box shape that can violate it. Checked host-side so the
failure names the field instead of arriving as CUDA_ERROR_INVALID_VALUE.
Plain scalar loads rather than a matrix instruction: a ColVec's values
are one element each at columns no ldmatrix shape describes, and under
BaseLdtm a column depends only on lane % 4, so the 8 lanes of a column
group issue the same address and shared memory answers them together.
That access pattern is the second reason a vector wants no swizzle --
there is nothing here for one to spread.
Two cases, 18 -> 20. 'shared vector round trip' runs the whole loop --
global, shared, registers, shared, global -- so the unswizzled box, the
broadcast, the per-element write and the store each fail
distinguishably: VECTOR is twice a swizzle atom, so a box quietly capped
at one delivers half the vector; the register dump is indexed by (lane,
value) and the host applies BaseLdtm::column, so a wrong broadcast
prints the column the hardware handed that register; 32 lanes write four
2-byte elements each, which a word-wide write would corrupt in pairs;
and the destination is seeded with POISON, which no identity can be.

'shared vector row' slices row 2 of a [VECTOR, 4] buffer through the
rank-2 box, and every element carries its source row -- so a descriptor
that ignored the minor coordinate reports the row it delivered.
The kernel is finished, so it belongs in the default build and gets a
real sm_100a compile. groupnorm_tile beside it does not, and the split
is the honest reading of what #13 bought: its statistic spans four
warps, which is storage plus a barrier and still #3. The #[cfg] moves
onto the kernel -- #[cuda_module] honours it -- so the file that
documents the gap is the file that builds.

modal_app.py::gaps prints each gated kernel's error list, because the
README's claim about what is missing is worth nothing unless someone
runs the check. Under --features layernorm there is now exactly one
error, sync::block_reduce_sum.
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.

Shared vectors, and ops on shared tiles

1 participant