Skip to content

A row statistic reaches shared memory by scatter, and a vector slices to a base address (#130) - #201

Merged
feitreim merged 1 commit into
mainfrom
feat/vec-slice-and-row-vec-staging
Aug 6, 2026
Merged

feitreim merged 1 commit into
mainfrom
feat/vec-slice-and-row-vec-staging

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Parts 1 and 2 of #130. Part 3 — in-place shared-memory maps and folds — is the
open-ended half the issue says not to bundle, and is not here.

ldst::store_row_vec / load_row_vec

RegVec — the per-row statistic every attention and normalization kernel
computes — had no path to shared memory at all. A statistic that had to leave
the warp went through sync::block_reduce, which folds it to a scalar, or was
recomputed, and a per-row bias could not be read in.

The scatter is the whole of it, and it is not the ColVec pair transposed.
Under BaseLdtm a column depends only on lane % 4, so store_vec is eight
lanes issuing one address and shared memory broadcasting. A row depends on
lane / 4, so this is M / 8 elements per lane, 8 rows apart, with no run to
widen — and it needs the lane mask the column direction does not: the four lanes
of a quad hold the same row, so only the one RowLayout::owns_row names
writes. That predicate has been on the trait since global::store_row_vec and
this is its second caller.

The load direction is symmetric. Every lane reads, replicas included, because
one copy per lane holding the row is what makes a RegVec well-formed, and a
quad's four identical addresses come out of one bank read.

SharedVec::slice::<LEN>(start)

A base-address addition, per the type's own doc. LEN <= N is a
post-monomorphization assert and so fires at codegen, not on a device; where the
window starts is a runtime cursor and stays the caller's, exactly as at's
index is — a chunked walk's start is a loop variable, and making it a const
parameter would decide the shape of every caller's loop to buy an assertion the
length already carries.

layernorm_rows is the one caller. It was laundering a pointer through a
ParameterChunk type alias and an at() -> from_raw() pair to say "the next
CHUNK elements"; the alias is gone and the two calls are one.

The device case

shared row statistic, beside the two ColVec cases it cannot be a restatement
of. The trap it avoids: a value derived from row_of and stored at row_of
agrees with itself whatever the map says. So every register carries the
(lane, slot) it came out of rather than the row it is destined for, and the
host looks for that register where row_of claims it is — a permutation lands
registers at indices that name where they actually came from, and reports it.
A row no owner claimed comes back poisoned rather than as a plausible neighbour,
and every lane dumps its read-back, so the three replicas of each quad must hold
what their owner wrote.

The same two claims are host tests at fp32 in ldst.rs, where the movers are
plain pointer accesses, at M = 16, 32 and 128.

Gates

gate result
cargo test 114 + 1 + 84 pass
scripts/fmt --check clean
cargo clippy --all-targets -- -D warnings clean
scripts/modal-run build pass
scripts/modal-run device_tests 53 of 53, new case: shared row statistic 32 rows scattered from 8 owning lanes, and read back into all 32
scripts/modal-run examples pass, layernorm 256x128 rows normalized, worst relative error 3.87e-3
scripts/modal-run regcount every gated kernel inside its step

layernorm_rows moved no register. Run against origin/main and against
this branch: 40 in examples/, 48 in experiments/, both times. (The 40/48
crate split is pre-existing and is the composition effect, not this diff.)

Closes #130's parts 1 and 2. Left open: part 3.

@feitreim feitreim closed this Aug 6, 2026
@feitreim feitreim reopened this Aug 6, 2026
@feitreim

feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner Author

GitHub Actions has not dispatched a run for this branch. No ci/cuda run exists for feat/vec-slice-and-row-vec-staging (gh run list shows nothing), a close/reopen did not fire pull_request either, and a gh workflow run cuda.yml -f ref=refs/pull/201/merge sat queued for 10 minutes with no runner — then the cancel API returned a 502. That is runner capacity on the GitHub side, not this diff; ci and cuda both ran green on main and on the previous PR earlier today. It wants a re-push or a maintainer nudge once runners are back.

Every gate was therefore run by hand, and each is the command the workflow step runs:

Tier 1, locally — cargo fmt --check across all four manifests via scripts/fmt --check (clean); cargo metadata --locked across all four (lockfiles current); cargo clippy --all-targets --locked -- -D warnings (clean); cargo test --locked (114 + 1 + 84 pass); RUSTDOCFLAGS=-D warnings cargo doc --no-deps --locked (clean).

Tier 2 — scripts/modal-run build and scripts/modal-run regcount, both to their sentinel. build is the gate that matters for SharedVec::slice: its const { assert!(LEN <= N) } is post-monomorphization and only a real sm_100a codegen can fire it. It also carries the cargo doc --features host that is the only real intra-doc-link gate (CI.md), which covers the links added here.

Tier 3 — scripts/modal-run device_tests (53 of 53) and scripts/modal-run examples (all four teaching kernels plus every experiments/ rung).

The three kernel crates were also typechecked locally against the cuda_cudart redist (CUDA_TOOLKIT_PATH=...), which is what let the device-tests case be written without spending a Modal run per compile error.

… to a base address (#130)

`RegVec` — the per-row statistic every attention and normalization kernel
computes — had no path to shared memory, so one that had to leave the warp went
through `block_reduce`, which folds it to a scalar, or was recomputed.
`ldst::store_row_vec` / `load_row_vec` are that path, and they are not the
`ColVec` pair transposed: under `BaseLdtm` a column depends only on `lane % 4`
so `store_vec` is eight lanes issuing one address, while a row depends on
`lane / 4`, so this is `M / 8` elements per lane 8 rows apart, from the one lane
of each quad `RowLayout::owns_row` names. The load direction is symmetric —
every lane reads, replicas included, because one copy per lane holding the row
is what makes a `RegVec` well-formed.

`SharedVec::slice::<LEN>(start)` is the other half: a base-address addition, per
the type's own doc. `LEN <= N` is a post-monomorphization assert and fires at
codegen; where the window starts is a runtime cursor, because a chunked walk's
start is a loop variable. `layernorm_rows` was laundering a pointer through a
type alias and an `at()` -> `from_raw()` pair to say "the next CHUNK elements",
and now says it once.

The lane->row scatter is the whole correctness question, so it gets a
`device-tests` case: every register carries the `(lane, slot)` it came out of
rather than the row it is destined for, so a value derived from `row_of` and
stored at `row_of` cannot agree with itself — a permutation lands registers at
indices that name where they actually came from. Poison marks a row no owner
claimed, and every lane dumps its read-back so the three replicas of each quad
must hold what their owner wrote. Host tests cover the same claims at fp32,
where the movers are plain pointer accesses.

Parts 1 and 2 of #130. Part 3 — in-place shared-memory maps and folds — is the
open-ended half the issue says not to bundle.
@feitreim
feitreim force-pushed the feat/vec-slice-and-row-vec-staging branch from fee5613 to c1885fe Compare August 6, 2026 19:31
@feitreim
feitreim merged commit 9250caa into main Aug 6, 2026
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.

RegVec cannot reach memory, SharedVec cannot be sliced, and neither can be folded in place

1 participant