Skip to content

gpu: head-256 prefill attention for EmbeddingGemma 1 (flash_band first-key bound, pack-1 lower entry) - #351

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-emb1-head256
Oct 16, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-emb1-head256

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Part of #59 (plan and PR order in #190), E8: the kernel half of EmbeddingGemma 1. It adds the head-256 prefill attention entry that the encoder needs, with a gate that checks it against the f64 band owner. The tokenizer half of E8 is a separate PR. Nothing in the server calls this entry yet.

What and why

Release notes: internal: head-256 prefill attention for EmbeddingGemma 1, the kernel the embedding encoder will call.

EmbeddingGemma 1 attends with 256-wide heads over a causal window. The existing head-256 prefill kernels take query heads in packs of 2, 4 or 8. This PR adds the pack-1 variant with the lower-band key bounds, gqa_prefill_flash_256_p1_lower, and gate_gemma_embedding_head256, which compares it with the f64 flash_band owner. The encoder PR can then call a kernel that is already checked.

What changes

Two commits, one contract each.

  1. flash_band::prefill_bound takes the first key's original offset. prefill_bound and judge_prefill count tiles, 16-key accumulation groups and 4-key sum groups from that offset, including the partial groups at both ends of a window. The three existing callers (gate_gemma_embedding2_head512, gate_qwen3moe_flash, gate_qwen35moe_attn) give 0. There is no second bound function and no compatibility wrapper. A unit test checks that 0 and aligned offsets give the same bits as the old closed form, and covers a window [16, 80) that spans two 64-key tiles and an unaligned group.
  2. gqa_prefill_flash_256_p1_lower and its gate. crates/gpu/src/flash_gqa_prefill.rs gains the pack-1 lower entry and enqueue_256_p1_lower. They use the shared prefill_256_p, enqueue_packed and checked_packed. In pack 1 the query load reads two positions per load step. The layout is chosen outside the load loop so the existing packed loop stays as it was. The gate binary, its required feature, the recipe row in tools/gate-paths.tsv, the justfile entry and the spill pins in tools/ref/ptx-shapes* know the new entry. tools/unsafe-ratchet.txt moves gpu from 1869 to 1871 for two new unsafe blocks (the query read and the shared body call), each with a SAFETY comment.

The gate runs heads 3/1 and 6/2, T of 1, 63, 65 and 129, the full input and valid windows (reach 17 and 64), and scale 1 and 0.0625. T=65 runs with ctx=65 and no padding. For each case it checks the f64 bound, that NaN padding is untouched, that a rerun is bit identical and that a single graph node gives the eager bits. A wrong first key or count must turn only its own row into NaN and raise the KeyCount fault. A first-key buffer that is too short must be refused by name.

Comparison with the reference

The reference is the f64 attention and first-order band owner, flash_band in crates/gpu-gates/src/flash_band.rs (blob 84399a3e89c5e9d5fe141ebab4d8f772f8532922). gate_gemma_embedding_head256 computes each query row's exact result and bound in f64 and divides the kernel's largest error by that bound. The comparison log is from c618d7d0, and git range-diff shows its two commits equal (=) to this head's.

Case Largest measured/bound Allowed Result
32 prefill comparisons, heads 3/1 and 6/2 worst measured/bound 0.278 at most 1 PASS on all 32

The pack-1 kernel stays within 0.278 of the f64 band's bound on every one of the 32 comparisons.

What this PR does not do

  • No call from the encoder or the server, no tokenizer (the other E8 PR), no pooling.
  • No throughput claim and no timing, because nothing calls the entry yet.
  • No change to any existing kernel's PTX or resources (shown below), and no new reserve tag.

Code shape

  • No second copy. The pack-1 entry reuses the shared prefill body, launcher and validator. The f64 bound has one owner, and its three existing callers now give the new argument.
  • Model code holds only architecture facts. The shared owners gained no per-model branch or flag. The entry is selected by pack size and the lower-band bound.
  • Dead code is gone. Nothing I made unused is left behind.
  • No silent failure. A bad first key or count is a fault and a NaN row. A short buffer is a named shape error.
  • Comments state what is true now. tools/check-comments.sh has not been run on this head.
  • No relaxed gate. No band or tolerance moved. The gpu unsafe ratchet moved by the two new blocks, with the PIN line in the file.
  • cargo fmt is clean on the touched Rust files, but cargo clippy and the lint count are still running on the card when this was posted. The result goes into this description when it ends.

Duplication found or removed: none.

What I ran

The GPU checks ran on a DGX Spark in the project's dev container, on c618d7d0754bcf0263c9e2fe02f330e269816677. The head 751f89ef is the same two commits rebased onto ce1c310ff, and git range-diff showed both as unchanged (=). I did not run the gate again on the rebased head.

Check Result
cargo oxide build --arch sm_121 -- -p bloomery-gpu-gates --features gpu --release --bin gate_gemma_embedding_head256 rc 0
BLOOMERY_GATE_CARD=any bash tools/gpu-gate.sh gate_gemma_embedding_head256 rc 0, 32 comparisons PASS, gate_gemma_embedding_head256: PASS
bash tools/gate.sh --release -p bloomery-gpu-gates --lib -- prefill_bound_counts_tiles_from_first_key --nocapture 1 passed
existing head-512 synthetic gate gate_gemma_embedding2_head512: PASS
python3 tools/recipes.py check 133 gate rows held to the derivation

The unit test, the build of the three existing callers and the head-512 synthetic run used 3b563994, an earlier head of the first commit. flash_band.rs has the same blob (84399a3e89c5e9d5fe141ebab4d8f772f8532922) there and in the final head.

FAIL-first, on the final head, one edit at a time:

Edit Result
pack-1 query read t0 + 2*e + r0 changed to t0 + e build rc 0, gate rc 1, band=false
the LOWER=true of the pack-1 call changed to false build rc 0, gate rc 1, the window comparison and the first-key fault isolation fail
tile count without the first-key offset (on 3b563994) unit test rc 101

Base versus head tools/ptx-compare.sh against 75f72268e50dc172a8b0dc13d7734633440dbcdb, for generate_ds41 and gate_e2e, with --features gpu,deepseek41, at PTX arch sm_86 and sm_121, JIT on the GB10 (cc 12.1). The sm_86 runs use the project's tool. For sm_121 I used a copy of the tool that differs by the one line ARCH=sm_86 to ARCH=sm_121. Each of the four runs ended rc 0 and shows one added entry, gqa_prefill_flash_256_p1_lower, and every existing entry identical:

PTX arch Binary Entries, base to head Added Existing entries
sm_86 generate_ds41 332 to 333 the new entry identical
sm_86 gate_e2e 265 to 266 the new entry identical
sm_121 generate_ds41 332 to 333 the new entry identical
sm_121 gate_e2e 265 to 266 the new entry identical

The new entry has no spill and no JIT local memory in all four runs. ptxas reports 214 registers at sm_86 and 212 at sm_121, and the GB10 JIT reports 212. The spill check on the entry, with a table cut down to its own rows, gave violations=1 FAIL before the pin and violations=0 PASS after it, at both arches. The 0/0 rows in the main spill tables are the zero-spill requirement for the new entry. They come from the GB10 JIT, not from a cc 8.6 card.

Not run: cargo clippy and the lint count (still running, as above), tools/check-comments.sh, just affected, just contrib-check, the full mac-static, and the whole spill gate on the final head. I ran no RTX 5090 or other GPU check, no cc 8.6 JIT and no timing. The gate uses synthetic inputs and no model file.

What I ran into

None of this blocks the PR.

  • Choosing the query layout inside the load loop changed other kernels. The gate accepted it, but tools/ptx-compare.sh reported four existing packed entries changed and the pack-2 JIT registers going from 206 to 208 (rc 3). Moving the layout choice outside the loop fixed it. No check was loosened.
  • The full spill table has 26 older unpinned entries. Run on the head, it reports 27 unpinned entries per binary: the new one and 26 that are in the base scan with the same spill and JIT local values. I pinned only the new entry and left the rest alone.
  • My first harness run had no local card setting and stopped at a card lookup with rc 75, so it was not a valid FAIL. I fixed the setup and now count a mutant only when the gate returns rc 1 with band=false.

Hardware (if the change is for a machine)

DGX Spark: NVIDIA GB10, driver 595.84, compute capability 12.1, CUDA 13.3.1 in the project's dev container (ptxas 13.3.73). The unit test needs no GPU.

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 16, 2026
github-actions[bot]
github-actions Bot previously approved these changes Oct 16, 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: CLEAN: No reachable behavior defect or proven quality defect survived review.

Non-blocking

⚠ Exercise the excluded prefix: crates/gpu-gates/src/bin/gate_gemma_embedding_head256.rs:35.
The cache twin poisons only keys at or past t (prefill_gate.rs:48). The excluded prefix stays finite, so this gate does not detect removing key >= lo from the shared staging guard (flash_gqa_prefill.rs:1108): the per-row softmax still masks those keys.
Add a pack-1 case with positive first keys and NaNs below the block's minimum first key, inside its first key tile. The staging-guard mutant must fail that case.

Checked

Duplicates: 5 new functions checked; no duplicate owner found. The dryer pair selects different pack sizes through the existing launcher.
Complexity: diagnostics report run new at 49, prefill_256_p 41→44 for query staging (kernel exemption), and enqueue_packed 18→21 for one dispatch arm.
Silent failure: 3 refusal paths followed—short first-key buffer, invalid first key, invalid count. They return a named shape error or raise KeyCount and write NaNs.
Tests: the PR reports FAIL-first results for the query-load and LOWER mutants. The bound's unit assertions cover tile, 16-key and 4-key offsets. The new gate leaves prefix NaN exclusion untested; the existing pack-2 hardware test checks it on that entry.
Gates: existing callers retain first=0 and unchanged bounds; the unsafe ratchet has a dated PIN. No existing clause or selector was removed.
Escape catalog: C1–C4 do not match.
Adversarial checks: query tails, an unpadded cache, mixed invalid rows and an entirely invalid block are guarded.

Notes

No builds or GPU gates ran during validation. The PR reports FAIL-first runs and identical existing PTX on earlier revisions. Provide the current base/head add-class scan and owning-gate output, plus the required nvidia-smi line and gate recipe output. GPU evidence is incomplete.

Card check: needs-gpu. This pull request changes crates/gpu-gates/Cargo.toml, crates/gpu-gates/src/bin/gate_gemma_embedding2_head512.rs, crates/gpu-gates/src/bin/gate_gemma_embedding_head256.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)

The shared attention bound accounts for partial key tiles and accumulation groups at either end of a cut cache. Existing callers pass zero, which preserves their formulas and results.

The host test checks the zero and aligned offsets against the closed form for uniform attention, and an interval of 64 keys starting inside a tile against its two-tile bound.
Instantiate the packed flash owner at pack one for groups of three query heads per key head. The query staging reads two positions per pass at this pack; existing pack instances retain their indexing.

The synthetic gate compares whole-input and symmetric-window attention against the f64 attention band at three and six query heads, including block and key-tile tails, scaling, NaN padding, reruns, graph replay and count refusals.

@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 review finding requires changes.

Non-blocking

⚠ Refresh the GPU evidence for this head: crates/gpu/src/flash_gqa_prefill.rs:2462. The reported runs precede the reviewed head, a6797a20, and include no nvidia-smi output.
Run the new family gate and the add-class PTX comparison for generate_ds41 and gate_e2e against the current base. Attach the card/driver line, both hashes, commands and output. This establishes the kernel evidence before encoder integration; this change class needs no timed A/B.

Checked

Duplicates: 5 new functions checked. The dryer pair, enqueue_256_p2_lower / enqueue_256_p1_lower, selects different pack sizes through the existing common launcher. No additional owner is justified. GPU-gates searched manually.

Complexity: run is new at 49; its logical split points are fixture setup, eager/band checks, graph checks and refusals. prefill_256_p 41→44 adds a compile-time layout branch; its kernel body stays intact. enqueue_packed 18→21 adds one dispatch arm.

Silent failure: 4 bad-input classes followed: zero count, count beyond context, first key at/past count, and short first-key buffer. Consumers receive KeyCount with a NaN row, or a named Shape error.

Tests: source mutation checks cover the query stride at flash_gqa_prefill.rs:1007 (replace with t0 + e: band fails), offsets at flash_band.rs:106,122,123 (drop them: shifted assertions fail), and fault/NaN/short-buffer refusals (drop them: refusal checks fail). Boundary/odd-group index enumeration found complete, unique loads and stores. GPU gates were not executed in this review.

Gates: first=0 retains the old bound; new zero-spill rows added; the two-block unsafe increase has a dated PIN. No existing tolerance moved.

Adversarial checks: partial query tiles, invalid and wholly invalid intervals, and capture on the wrong stream are blocked by the index guards, fault/store logic and Gpu::capture's stream identity.

Escape catalog: C1–C4 do not match the added code.

Card check: needs-gpu. This pull request changes crates/gpu-gates/Cargo.toml, crates/gpu-gates/src/bin/gate_gemma_embedding2_head512.rs, crates/gpu-gates/src/bin/gate_gemma_embedding_head256.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 bdc7082 into midagedev:main Oct 16, 2026
6 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 gpu-verified 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.

2 participants