Skip to content

The transposed score band's two missing halves: its mask origin and its per-column statistic (#178) - #179

Merged
feitreim merged 1 commit into
mainfrom
feat/transposed-band-mask-and-col-stat
Aug 3, 2026
Merged

feitreim merged 1 commit into
mainfrom
feat/transposed-band-mask-and-col-stat

Conversation

@feitreim

@feitreim feitreim commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Closes #178. Consumer is the flash-attention backward rewrite, feitreim/oxide-train#69 — the first kernel in either repo whose score band is K·Qᵀ.

RegTile::make_causal_t_at

make_causal_at's twin, row origin first as in the other:

pub fn make_causal_t_at(&mut self, lane: u32, key_base: u32, query_base: u32, fill: f32)

The band covers keys key_base..key_base + M against queries query_base..query_base + N, so its diagonal sits at key_base - query_base and this subtracts in i32. §3.4 claims every mask "takes a coordinate origin TK's signatures do not have"; that was true of one diagonal.

The reason to prefer it is the reason make_causal_at gives, only stronger on this side: a CTA owning a block of keys streams the queries at and after them, so key_base - query_base is negative on every visit but the first. On the query-parallel side the wrapping case is the fully-masked band, which a kernel can skip; here it is the common case.

global::load_col_vec

pub unsafe fn load_col_vec<E: Element, const N: usize, L: ColLayout<N>>(
    src: GlobalRows<E>, row: u32, column: u32, lane: u32,
) -> ColVec<N, L>

load_row_vec's addresses — one element per band position, down a single global column — delivered onto the other axis. Which axis a saved statistic broadcasts along is a property of the band and not of the statistic: the query-parallel backward reads the log-sum-exp with sub_row_assign, and the key-parallel one, whose rows are keys and whose columns are those same queries, needs it with sub_col_assign.

It is not load_cols, and that is the thing worth getting right in this PR. #173 landed three days ago and also returns a ColVec from a GlobalRows with the same four arguments. They differ in the memory:

reads pairing
load_cols (#172) N consecutive elements of one row — a parameter vector along the axis it multiplies CONTIGUOUS_VALUES → ld.global.v2
load_col_vec (#178) N rows of one column, a stride apart none; there is no run

So the doc on each names the other and says to choose by where the numbers are, not by the type they arrive in, and the device cases are kept separate for the same reason (below).

The alternatives the kernel weighed and did not take: ldst::load_vec needs the statistic staged into a SharedVec first — 64 stores and a publication per streamed tile, and a ring of them carved by hand since a SharedVec cannot be sliced (§1.4); and open-coding the gather is the scatter #130 was filed to delete, on the other axis.

Gates

  • Tier 1 local: scripts/fmt --check, clippy --all-targets -- -D warnings, cargo test — 110 unit tests and 80 doctests, RUSTDOCFLAGS=-D warnings cargo doc --no-deps. All clean.
  • Two new host tests, both of which are the claim rather than a restatement of the code:
    • reg::the_transposed_band_masks_about_the_same_diagonal — the surviving elements are the transpose of make_causal_at's over the same block pair, at five base pairs of which four have key_base ≠ query_base and three have key_base > query_base.
    • global::a_row_statistic_reaches_the_column_axis_over_the_same_addresses — asserted against the element index rather than a literal, so it fails naming the element actually read.
  • Tier 3: a new device case, global column statistic, over check_global_rows' matrix and origin. Every element carries its own flat index, so a mover that walked the row — which is exactly what load_cols does, off the same argument list — delivers GLOBAL_ROW * GLOBAL_PITCH + GLOBAL_COLUMN + value where this expects (GLOBAL_ROW + value) * GLOBAL_PITCH + GLOBAL_COLUMN, and the failure prints which walk ran. This is why it is a separate case and not a line added to global column map: sharing a kernel with the other ColVec mover is the one thing that would let the confusion through.
    I do not have a B200; gpu-ci is set so the device tests run here.

GAPS.md 2.2 and 3.4 gain the entries (rebased onto bf1dac0, after #173's own 2.2 edit). tests/gaps.rs names make_causal_t_at in a new §3.4 claim — the section had none, and its "every one takes a coordinate origin" is exactly the sort of sentence this file exists to keep true — and load_col_vec beside load_row_vec rather than beside load_cols, grouped by the memory the two share rather than by the type.

What this does not do

No store_col_vec. A kernel that produces a per-column statistic across a grid wants an atomic, not a plain store — #173's reasoning for the same omission, and it holds here.

…ts per-column statistic

An attention backward pass is the first kernel to score `K·Qᵀ` rather than
`Q·Kᵀ`, and both halves of what a band needs turned out to exist on one
diagonal only.

`RegTile::make_causal_t_at` is `make_causal_at`'s twin: the block-origin form
of the transposed causal mask, row origin first as in the other. The argument
for the `_at` spelling is stronger here than where it already existed — a CTA
owning a block of keys streams the queries at and after them, so
`key_base - query_base` is negative on every visit but the first, and the `u32`
subtraction a call site would otherwise write wraps to a huge positive number
and masks nothing.

`global::load_col_vec` is `load_row_vec`'s: the same addresses — one element
per band position, down a single global column — delivered as the `ColVec` a
`sub_col_assign` takes instead of the `RegVec` a `sub_row_assign` does. Which
axis a saved statistic broadcasts along is a property of the band, not of the
statistic: the query-parallel backward reads the log-sum-exp per row, and the
key-parallel one needs the same numbers per column.

It is deliberately not `load_cols` (#173), which reads a run *along* one row
and pairs consecutive values into one access. Two movers returning `ColVec`
off the same argument list is a hazard, so the device case is a separate one:
`global column statistic` beside `global column map`, over the same matrix and
origin, where a mover that took the other walk delivers an element that names
itself and the failure prints which walk actually ran.

Gates: `scripts/fmt --check`, `clippy --all-targets -D warnings`, `cargo test`
(110 unit + 80 doctests, including two new host tests — the transposed mask
against the band's global origin, and `load_col_vec` against `load_row_vec`'s
own addresses), `RUSTDOCFLAGS=-D warnings cargo doc`. GAPS.md 2.2 and 3.4 gain
the entries; `tests/gaps.rs` names both.
@feitreim feitreim added the gpu-ci Run the B200 device-test tier on this PR (costs GPU minutes) label Aug 3, 2026
@feitreim
feitreim merged commit a9d3207 into main Aug 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu-ci Run the B200 device-test tier on this PR (costs GPU minutes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The transposed score band has neither a mask origin nor a per-column statistic

1 participant