Repository navigation
The transposed score band's two missing halves: its mask origin and its per-column statistic (#178) - #179
Merged
Conversation
…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.
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.
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_atmake_causal_at's twin, row origin first as in the other:The band covers keys
key_base..key_base + Magainst queriesquery_base..query_base + N, so its diagonal sits atkey_base - query_baseand this subtracts ini32. §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_atgives, only stronger on this side: a CTA owning a block of keys streams the queries at and after them, sokey_base - query_baseis 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_vecload_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 withsub_row_assign, and the key-parallel one, whose rows are keys and whose columns are those same queries, needs it withsub_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 aColVecfrom aGlobalRowswith the same four arguments. They differ in the memory:load_cols(#172)Nconsecutive elements of one row — a parameter vector along the axis it multipliesCONTIGUOUS_VALUES→ld.global.v2load_col_vec(#178)Nrows of one column, a stride apartSo 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_vecneeds the statistic staged into aSharedVecfirst — 64 stores and a publication per streamed tile, and a ring of them carved by hand since aSharedVeccannot be sliced (§1.4); and open-coding the gather is the scatter #130 was filed to delete, on the other axis.Gates
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.reg::the_transposed_band_masks_about_the_same_diagonal— the surviving elements are the transpose ofmake_causal_at's over the same block pair, at five base pairs of which four havekey_base ≠ query_baseand three havekey_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.global column statistic, overcheck_global_rows' matrix and origin. Every element carries its own flat index, so a mover that walked the row — which is exactly whatload_colsdoes, off the same argument list — deliversGLOBAL_ROW * GLOBAL_PITCH + GLOBAL_COLUMN + valuewhere 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 toglobal column map: sharing a kernel with the otherColVecmover is the one thing that would let the confusion through.I do not have a B200;
gpu-ciis set so the device tests run here.GAPS.md2.2 and 3.4 gain the entries (rebased ontobf1dac0, after #173's own 2.2 edit).tests/gaps.rsnamesmake_causal_t_atin 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 — andload_col_vecbesideload_row_vecrather than besideload_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.