Skip to content

gpu: gqa_prefill_flash_256_p2_lower, a first key a row - #234

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-prefill-lower
Oct 15, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-prefill-lower

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #59 (plan and PR order in #190), E4: the non-causal symmetric window in prefill, as a const generic. The head-512 prefill entries and the GELU-gated entry are separate PRs.

What and why

Release notes: Under the hood: a lower key bound per row in the pack-of-two prefill flash, for EmbeddingGemma 2's sliding layers; no engine path calls it yet.

EmbeddingGemma 2 is a non-causal encoder. Its sliding layers use a symmetric window: llama.cpp's LLAMA_SWA_TYPE_SYMMETRIC keeps key j for query i when |j - i| <= n_swa / 2, so row t of a pass over n tokens attends over keys [max(0, t - w), min(n, t + w + 1)). The pack-of-two prefill (gqa_prefill_flash_256_p2, the layout of four query heads over two key heads that EmbeddingGemma 2's sliding layers have) already takes the upper bound per row as data (n_keys[t]). It has no lower bound. This PR adds one.

  • prefill_256_p, the body behind the _p4, _p2 and _256_q8 entries, gains a const generic LOWER: bool and a k_first_buf argument. With LOWER, row t reads its first key k_first[t] and attends over [k_first[t], n_keys[t]). The block starts its walk at the tile that holds its smallest first key and stages zeros below that key, as it already does at and past its largest count. A key below a row's first key scores -inf and weighs 0, like a key at or past its count. A warp skips a tile that is below the first keys of all its rows. A first key at or past the row's count raises key_count and leaves the row NaN, like a count of zero.
  • A new entry, gqa_prefill_flash_256_p2_lower, is the pack-of-two walk with LOWER. FlashGqaPrefill::enqueue_256_p2_lower launches it; it takes the same GqaPrefillArgs plus k_first on the device and refuses a short k_first by name.
  • The existing entries pass LOWER = false, which never reads k_first_buf.

The window width is not a kernel constant. The host computes k_first[t] and n_keys[t] for each row, so one entry takes any width: EmbeddingGemma 2's GGUF sliding_window of 1024 is a half-width of 512, and the gate also runs half-widths of 256 and 300. A layer with no window passes k_first = 0 and n_keys = n. EmbeddingGemma 2's global layers have heads of 512 and use the head-512 entries instead.

No engine path calls the entry yet. The E5 body of #190 (gpu-gemma-embedding2) is its first caller, and it computes each row's k_first and n_keys as above.

Code shape

  • No second copy. The lower bound is a const generic of the one pack-generic body, not a copied body. The eight-head gqa_prefill_flash_256 and the 128 body are untouched.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone.
  • No silent failure. A first key at or past its row's count raises key_count and writes NaN in that row, and the launcher refuses a short k_first.
  • Comments state what is true now. The module doc gains one paragraph for the lower bound.
  • No relaxed gate. tools/unsafe-ratchet.txt moves gpu from 1769 to 1771 blocks with a PIN(2026-10-14): line: the new entry's call into prefill_256_p and the walk's read of k_first_buf, each with its SAFETY line.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line): none.

What I ran

All runs are at the head 750ab61cb6d395059cf91ad28ed6fa0d35939224 over the base b3153d7749f34d1aaf1329ea5f11ee4fc62cb177, unless a line names another commit. The card runs used a DGX Spark (aarch64 Linux) in the project's dev container and an x86_64 Linux machine with an RTX 5090. The static checks ran on an Apple Silicon Mac.

just contrib-check (t1, the default) on the RTX 5090:

DONE tier=t1 total=100 red=1 wall=278s red: check-recipes

The one red is check-recipes, whose stall-term self-test fails on this machine with FAIL stall-term: got [post 0 rc=124], want [post 0 rc=143]. The base b3153d77 gives the same line on the same machine (DONE tier=t1 total=100 red=1 wall=295s red: check-recipes), and this PR does not touch tools/.

just mac-static on the Mac: fmt-check 0 diff blocks, and lint at the ratchet:

mac-static: lint rc 0 in 53 s — mac-check: lint ^warning: 41 = the ratchet 41 in tools/lint-ratchet.txt

Gate. hw_p2_lower_is_each_rows_window in crates/gpu/src/flash_gqa_prefill.rs, a test gate-gpu-lib runs. I ran it alone with bash tools/gate.sh --oxide -p bloomery-gpu --release --lib -- --include-ignored hw_p2_lower_is_each_rows_window (BLOOMERY_ARCH=sm_121) on the DGX Spark and -- --ignored hw_p2_lower (BLOOMERY_ARCH=sm_120) on the RTX 5090. The shape is EmbeddingGemma 2's sliding layer: four query heads over two key heads, heads of 256, scale 1.0, 1,200 rows over 1,200 keys (a 16-row tail block and a 48-key tail tile). Every clause is bit for bit or exact, so no band is involved:

  1. At half-widths 256, 300 and 512, each sampled row of the window launch (rows 0 to 31, every 13th row and the last 8) equals bit for bit its one-row launch over a cache that holds NaN in every key row outside that row's window. A rerun gives the same bits, and every output is finite. At 300, every other block's first keys straddle a tile boundary, so a block starts one tile below some of its rows.
  2. First keys of zero give bit for bit the plain _p2 entry's output at the same counts.
  3. A first key of 192 on every row gives bit for bit the plain _p2 entry's output over the cache from key 192 on, with every count lowered by 192.
  4. A dominant key at 229 (inside a tile): rows whose first key is 228 or 229 return its value row exactly, and rows whose first key is 230 do not read it.
  5. A slice skips NaN tiles below all its first keys, and masks a dominant value below its first keys in a tile it does read (ctx=192).
  6. A first key equal to the count, launched alone (row 37), raises key_count at the launch's layer and returns NaN. Launched with a first key past its count (row 500), both rows are NaN and every other row equals the clean launch bit for bit.
  7. A k_first one row short is refused by the launcher, with an error naming enqueue_256_p2_lower and k_first.

The references are the plain _p2 entry, which the engine gates already pin, and each row's own one-row launch. #190's table names host f64 rules for E4; this gate needs none, because every clause is bit for bit.

DGX Spark:

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 243 filtered out; finished in 7.55s

RTX 5090:

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 243 filtered out; finished in 6.85s

FAIL-first. Each mutant was a temporary patch in crates/gpu/src/flash_gqa_prefill.rs, run alone against the gate on the DGX Spark and reverted after its run. The first three ran at ad4537be, whose kernel code is this head's (the two differ only in the test); the last ran at this head, whose tree was clean after it:

mutant the assertion that failed
a slice's live-tile test drops the lower bound clause 5, row 8: a skipped NaN tile or a masked value reached its output
the unmasked softmax path drops the lower bound clause 1, w=256 row 260: not its one-row launch
the same, at ctx=192 only, so clause 1 stays green clause 5, row 17: a skipped NaN tile or a masked value reached its output
k0 >= c relaxed to k0 > c clause 6, an equal first key raises a fault

Clauses 2, 3, 4 and 7 have no mutant of their own.

PTX (add class). just ptx-scan of generate_ds41 and gate_e2e, base against head, on the DGX Spark (sm_121, JIT on the GB10) and with BLOOMERY_ARCH=sm_86:

PTX_COMPARE sm_121 generate_ds41 same=302 added=gqa_prefill_flash_256_p2_lower changed= PASS
PTX_COMPARE sm_121 gate_e2e same=235 added=gqa_prefill_flash_256_p2_lower changed= PASS
PTX_COMPARE sm_86 generate_ds41 same=302 added=gqa_prefill_flash_256_p2_lower changed= PASS
PTX_COMPARE sm_86 gate_e2e same=235 added=gqa_prefill_flash_256_p2_lower changed= PASS

Every existing entry's PTX is unchanged, and the one added entry is the new one. Before its rows went into tools/ref/ptx-shapes.tsv, tools/ptx-spill-check.sh on the GB10 reported it unpinned:

generate_ds41 gqa_prefill_flash_256_p2_lower unpinned spill=0 jit_local=0
gate_e2e gqa_prefill_flash_256_p2_lower unpinned spill=0 jit_local=0

The same run also reports mamba2_step unpinned in both binaries, and that row stays unpinned after this PR; this PR does not touch it, and I did not run the base's spill check. The cc 8.6 run has no card behind it: the spill check there read ptxas stack frames in place of jit_local, and before pinning it reported only the new entry.

Gates just affected selects and I did not run. just affected b3153d77 selects 114 of 161 gate recipes, all through crates/gpu/src/flash_gqa_prefill.rs. Since no existing entry's PTX moved and no engine path calls the new entry, the runtime gate is the new test above, and I did not run the other 113.

Hardware (if the change is for a machine)

DGX Spark, dev container nvidia/cuda:13.3.1-devel-ubuntu24.04:

NVIDIA GB10, 595.84, 12.1

RTX 5090:

NVIDIA GeForce RTX 5090, 595.99.02, 12.0

What I ran into

None of this blocks the PR.

  • The cc 8.6 row of tools/ref/ptx-shapes.tsv for the new entry comes from ptxas -arch=sm_86 -v and was not read on a 3090. If a reading there differs, the row changes to it (model: EmbeddingGemma and EmbeddingGemma 2 on a side seat #190, open question 1).
  • A non-finite value that the block stages for another row can still reach a row whose window excludes it, through its zero weight (0 * NaN). The same already holds for a key between a row's count and the block's largest count; the module doc says so for both.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 15, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: The new gate does not detect removal of the equal-bound fault.

Must fix

1. Assert the equal-bound fault in an isolated launch

crates/gpu/src/flash_gqa_prefill.rs:3977 puts equality and past-count errors in one launch. This is a kind B gate defect: changing k0 >= c to k0 > c at line 791 leaves every assertion green. Row 37 still has zero weights and normalizes to NaN, while row 500 still raises the shared KeyCount fault. Staging, tile decisions and other rows' weight masks stay unchanged. With that mutant, an equal-bound-only launch returns NaN without the required fault.

Isolate this case now so a fault regression cannot pass the new gate. Insert this after let sink = ... and before the existing combined launch:

let equal = at(&count[37..=37], Some(&first[37..=37]), sink)
    .expect("equal first key");
let f = gpu
    .take_fault()
    .expect("the fault word")
    .expect("an equal first key raises a fault");
assert_eq!(f.layer, LAYER as u32);
assert_eq!(f.site(), Some(FaultSite::KeyCount));
assert!(equal.iter().all(|v| v.is_nan()));

Non-blocking

⚠ Supply the add-class PTX proof: crates/gpu/src/flash_gqa_prefill.rs:686 also compiles the existing _p2, _p4 and _256_q8 entries.
Attach base/head ptx-scan output showing unchanged existing entries and exactly the new entry, plus gate-ptx-spill failing on the new rows before pinning. These are the add-class checks in base AGENTS.md; the reported lower-bound gate does not establish them.

Checked

Duplicates: all four dryer pairs read; entry wrappers delegate to the common body and launcher.

Complexity: diagnostics report prefill_256_p 49 → 65 and enqueue_packed 21 → 24. The diff adds lower-bound branches and launcher validation/dispatch; R12 exempts the kernel body from splitting.

Silent failure: refused bounds raise KeyCount and write NaN; absent k_first selects the plain entry.

Tests: the PR reports FAIL-first for lower-mask mutants. Source analysis and a host simulation of the combined fault case confirm the unchanged tile decisions and weight masks for the >= → > mutant. The proposed isolated launch checks its missing fault directly.

Gates: no existing numeric band moved. The unsafe-block ratchet has its dated PIN and reason.

Escape catalog: none of C1–C4 matched.

Notes

GPU evidence is incomplete: no nvidia-smi line or base/head gate-run hash pair. No GPU gate was run during validation.

Card check: needs-gpu. This pull request changes crates/gpu/src/flash_gqa_prefill.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

prefill_256_p gains a const generic LOWER and a k_first_buf argument.
With LOWER, row t attends over keys [k_first[t], n_keys[t]): the block
starts its walk at the tile holding its smallest first key and stages
zeros below it, a key below a row's first scores -inf and weighs 0, a
warp skips a tile below all its rows' first keys, and a first key at or
past the count raises key_count with NaN in that row. The _p4, _p2 and
_256_q8 entries pass LOWER = false and never read k_first_buf.

The new entry gqa_prefill_flash_256_p2_lower (FlashGqaPrefill::
enqueue_256_p2_lower) is the pack-of-two walk with LOWER: the window an
encoder's symmetric sliding layer sets per row, the width host data.

hw_p2_lower_is_each_rows_window holds each sampled row bit for bit its
one-row launch over a cache NaN outside its window at half windows 256,
300 and 512, first keys of zero the plain entry's bits, a first key of 192
the plain entry's bits on the cache from row 192 on, a dominant key read
exactly by the rows whose window holds it, the key_count fault and a short k_first
refused by name. A mixed-slice launch holds the skipped NaN tiles below a
slice's first keys and the softmax mask at a live tile's lower edge.
@juliankang4
juliankang4 force-pushed the dgx-spark-emb2-prefill-lower branch from ad4537b to 750ab61 Compare October 15, 2026 05:36
@juliankang4

Copy link
Copy Markdown
Collaborator Author

The branch is now at 750ab61c, still one commit on b3153d77. The kernel is unchanged; only the gate moved.

1. The equal-bound fault. Applied as given: the gate now launches row 37 alone with its first key equal to its count and asserts the fault word, its layer, KeyCount and an all-NaN row, before the combined launch with row 500. With k0 >= c relaxed to k0 > c at the launch's bound check, the gate fails on an equal first key raises a fault, and the restored tree passes on the DGX Spark and the RTX 5090.

2. The add-class proof. ptx-scan of generate_ds41 and gate_e2e, base against head at sm_121 and sm_86: every existing entry is the same and the one added entry is gqa_prefill_flash_256_p2_lower. Before pinning, the spill check reported the new entry unpinned. The output and its limits (an existing mamba2_step row left unpinned on the GB10, and an sm_86 run read from ptxas) are in the description under "PTX (add class)".

The description is updated to this head: the per-card gate results, a t1 contrib-check on the RTX 5090 with its one red shared with the base, both nvidia-smi lines, and the gates just affected selects that I did not run.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No blocking behavior or quality defect was found in the lower-bound prefill change.

Non-blocking

⚠ Add-class verification is incomplete: crates/gpu/src/flash_gqa_prefill.rs:2444.
The body omits the owning gate runs that AGENTS requires when engine binaries gain an entry. Its PTX comparison uses base b3153d77, while this review's base is 9fc2cc0d.
Run the owning gates and PTX/spill comparison against the review base; attach commands and output. This closes integration coverage before the first engine caller lands.

⚠ Four clauses lack FAIL-first evidence: crates/gpu/src/flash_gqa_prefill.rs:3765.
The body reports no mutants for clauses 2, 3, 4 and 7; the add class requires FAIL-first per clause.
Exercise wrong plain-entry dispatch, shifted cache addressing, an exclusive lower boundary, and removal of the short-buffer guard. Attach each failing assertion while the gate is being introduced.

Checked

Duplicates: lower and existing packed entries share prefill_256_p; packed launchers share enqueue_packed.
Complexity: lower-bound guards extend the shared kernel; packed dispatch gains one arm and a buffer check.
Silent failure: invalid lower bounds raise KeyCount and produce row NaNs; a short k_first returns a named shape error.
Tests: the ignored hw_ gate covers windows, zero and shifted lower bounds, invalid bounds and short buffers. The body reports mutants failing clauses 1, 5 and 6. CUDA runs were not independently repeated.
Gates: unsafe ratchet 1769→1771 has a dated PIN and names both added blocks. No numeric band moved.
Escape catalog: no patterns matched.

Notes

GPU labeling evidence is incomplete: the body supplies isolated tools/gate.sh runs, without just gate-* recipe output.

Card check: needs-gpu. This pull request changes crates/gpu/src/flash_gqa_prefill.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 45c9105 into midagedev:main Oct 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant