Repository navigation
Shared vectors: an unswizzled run of elements, and the box to deliver it - #53
Merged
Merged
Conversation
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.
feitreim
force-pushed
the
issue-13-shared-vectors
branch
from
July 28, 2026 01:52
efa051a to
cc9c62d
Compare
This was referenced Jul 28, 2026
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.
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
TileBoximpl,scalar element access, and the
ColVecbroadcast on top.layernorm_rowsleaves its feature gate as a result.
The swizzle decision, and why it is the crux
SharedVecdoes not go throughSwizzleat all. It is not aSwizzleNonemode, and adding one is still #14's job.
The argument is that
Swizzle::ATOM_BYTESdoes two jobs inshared.rs— it isthe swizzle period and the width of a TMA box (
SUBTILE_COLS = ATOM_BYTES / E::BYTES) — and an unswizzled vector wants neither:Nwide, 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.
subtiles, which
tma_loadwould then fetch one instruction each. Thenatural "no swizzle" atom is 16 bytes (the TMA line); at bf16 that turns a
[128]vector into 16 boxes of 8.SUBTILE_COLS = 64, soSharedVec<Bf16, 32>would not even typecheck, andthe 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
BaseLdtmthe 8 lanes of a column groupask for the same address, which shared memory answers from one bank read. There
is nothing for a swizzle to spread.
Deciding what
ATOM_BYTESmeans in the absence of a swizzle is a realquestion, but it is a question about tiles: an unswizzled
[R, C]stagingtile does need a
Swizzleimpl, and it needs the box-width job separated fromthe 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 narrowestSharedTilethat typechecks is 128 for one row.What landed
src/shared.rsSharedVec<E, const N: usize>— base pointer plus compile-time length,Copy,built by the kernel like every other shared type here (
from_raw).N * E::BYTESa multiple of 16 (the unswizzled box'sgranularity — a swizzled box gets this free by being an atom wide), and
N <= 256(the descriptor's box cap).at/get/set, andtma_load/tma_load_2d/tma_store/tma_store_2d. One instruction each: an unswizzled box has no atom to be cutinto, so a vector never walks stacked subtiles.
Elementgainsread/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 isnot avoidable:
Element::Unpackedis an opaqueCopytype with no way toindex a lane out of it, so
unpack(word)[i % PER_WORD]does not typecheckgenerically, and bounding
Unpacked = [f32; 2](asstore_fragmentdoes)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
wwriting index
wmust not read-modify-write a neighbour's partial.src/global.rsTileBoxforSharedVec:BOX_COLS = N,BOX_ROWS = 1,SWIZZLE = NONE.BOX_ROWS = 1is what makes a rank-1 layout legal —descriptor_shapealready asserted exactly that, waiting for something to satisfy it.
check_driver_requirementsnow checks the innermost box width in bytes. Avector'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 amatrix instruction.
Dependencies this resolves
#2 — its comment said
TileBoxis implemented only forSharedTileandthat "whatever #13 lands as a shared-vector type is the natural second
TileBoximpl, 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 (plusthe two above);
SharedVecis generic overE: Elementand needs no change.#3 — the open question was whether a block reduction takes a scratch
pointer or a
SharedVec. It can now take aSharedVec, and it is still thekernel's allocation:
from_rawtakes a base pointer, so nothing about theshared 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
Elementis 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
SharedTileis either"load to registers, apply, store back" — which the library already composes out
of
load_tile/store_tileand 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. Filingthe 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 ofload_vec, and the reduction storeforms. 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::BYTESapart, covering exactlyBYTES),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 tripruns the whole loop (global → shared → registers →shared → global) and each claim fails distinguishably:
VECTOR = 128bf16 is 256 bytes, twice a swizzle atom, so a box quietlycapped at one atom delivers half the vector.
(lane, value)and the host appliesBaseLdtm::column, so a wrong broadcast reports the column the hardwarehanded that register.
one of every adjacent pair.
POISON, which no identity bit patterncan be, so an unwritten or short store is not a plausible neighbour.
shared vector rowslices 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
GlobalLayoutat all —the README's "expressible but untried" is now retired.
Nothing about the answer is in either kernel.
Examples
layernorm_rowsis out of its feature gate and in the default build,compiled for
sm_100abymodal run modal_app.py::build.groupnorm_tilekeeps the
layernormfeature — the#[cfg]moved onto the kernel, which#[cuda_module]honours, so the file that documents the gap is the file thatbuilds.
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 layernormthere is now exactly one error,unresolved import kittens::sync::block_reduce_sum. That is #3, ingroupnorm_tileonly. 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_100afor both kernel crates. Clean.modal run modal_app.py— 20 of 20 cases on a B200.modal run modal_app.py::examples—gemmexact,softmaxwithin 2e-3.modal run modal_app.py::gaps— one error under--features layernorm.cargo clippy --all-targets,cargo fmt --checkclean at the root. Theexamples crate's clippy warnings are unchanged from
main(softmax.rs,gemm.rs,bench.rs);layernorm.rscontributes none.Closes #13