Skip to content

gpu: the grouped GEMM takes IQ2_XXS stacks (Bolt prefill experts) - #327

Merged
midagedev merged 7 commits into
mainfrom
bolt-g1-iq2gemm
Oct 16, 2026
Merged

midagedev merged 7 commits into
mainfrom
bolt-g1-iq2gemm

Conversation

@midagedev

@midagedev midagedev commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

The grouped GEMM takes IQ2_XXS stacks

gemm_iq2xxs adds IQ2_XXS to the grouped GEMM, beside gemm_iq3xxs.

  • Decode: the file's 66-byte super-blocks go through the IQ2_XXS grid and the computed signs (iq2xxs_lane in crates/gpu/src/gemm/grouped.rs).
  • Scale: the sub-block scale 2s + 1 stays an integer factor of the block sum, and the epilogue scales by d/8, which is exact.
  • Host side: GemmWeight::Iq2Xxs, and from_ggml(IQ2_XXS) now accepts the type.
  • Why: Bolt's prompt path (gpu, gpu-gates: Bolt's prompt runs as batched passes (nemotron_h_moe prefill) #328) runs its routed and shared experts through this entry.

Change class: add (no engine caller on main yet).

Gates

  • gate_gemm adds two iq2xxs_* cases, each under the four routings:
    • a routed top-9 stack: 64 experts × 640 rows, K 2560;
    • a ragged slab: 400 rows, K 1280, an odd number of super-blocks per row, so every other super-block starts at 2 mod 4.
  • FAIL-first mutants: scale-2ls and sign-63, each a one-line change to iq2xxs_lane, checked on the card below (host-side scratch had already checked both against dequant_row, bit-exact to ggml).

GPU Evidence

Rebased onto main ce1c310; the head is 6bf6942 (a2254bc the change, 724f0f4 fmt, 7257b9e the ratchet, dd369dc the pins, then the review fixes b6b5687, 323ab55, 6bf6942). Main's block-shared selector refactor (e6e73c1) had removed the raw GRID2XXS const this lane decode reads; the branch carries it again beside GRID2XXS_SEL (crates/gpu/src/iq.rs).

Review fixes (6bf6942)

  • win over-read (323ab55), memcheck FAIL-first: compute-sanitizer --tool memcheck over gate_gemm --case iq2xxs at dd369dc (before): Invalid __global__ read of size 4 bytes at gemm_iq2xxs+0x1b50 … 1 bytes after the nearest allocation … of size 27,033,600 bytes, ERROR SUMMARY: 215 errors, exit 99. At the fix: 0 errors, exit 0. win's second word index is now (byte + 3) / 4 — word a itself for an aligned window, a + 1 only when the window spans it; branchless, same value in every case.

  • gate-gpu-gemm real tier at the fix: PASSED rc 0, per-case y_fnv identical to the unfixed run (e.g. iq2xxs_gate_synth T=1 routing=Uniform … y_fnv=4cc8efc1e3f16aea on both sides) — no output bit moved.

  • PTX effects: gate-ptx-spill: generate_ds41 333/333, gate_e2e 266/266, gate_qwen3vl_tower 16/16, violations 0 — the pins held, no re-pin. canon-check dd369dcb7..HEAD: 874 of 886 entries identical; the 12 changed are gemm_{iq2xxs,iq3xxs,q3k,q6k} in the gate_glm5next_e2e, gate_kquant and gate_swap bins (q3k regs 118→122, q6k 126→125) — those gates rerun green: gate-gpu-kquant PASS, gate-gpu-swap PASS (10 s), gate-gpu-glm5next-e2e PASS (733 s).

  • @frag duplication forwarded (6bf6942); the unreachable IQ2_XXS arm of synth_blocks removed (b6b5687). tools/check-unsafe.sh ok — 27 crates, 2835 blocks (unchanged).

  • gate-gpu-gemm, real tier, at c6b47c318 (= dd369dc content) (box queue, A6000, job s1782):

    • gpu-gate.sh: gate_gemm ran 159 s (exit 0), PASSED: gate_gemm, rc 0; the whole gate: 0 FAIL lines.
    • The IQ2_XXS lines: 69 PASS, 0 FAIL. Sample: gemm case=iq2xxs_gate_synth T=1 routing=Uniform slots=9 unwritten=0 act_codes=same decode_vs_ggml=same checked=5760 worst_err_over_band=0.151 contract_bits_differ=0 … PASS.
    • Model file: gemm qwen3 gate=blk.0 [2048, 768, 128] down_q4k=blk.6 [768, 2048, 128] down_q6k=blk.0 [768, 2048, 128] (Qwen3-30B-A3B-Instruct-2507 Q4_K_M).
  • Card FAIL-first (mutant-run --clean once, box queue, A6000): scale-2ls rc=1 killed — 64 case=iq2xxs_* FAIL rows with first_failure: (e.g. slot 8 row 0 expert 8: got -1.8785273e1 want -1.939840820925908e1 err 6.131e-1 band 1.284e-4) and first_contract_bits_failure: lines; sign-63 rc=1 killed; clean rerun rc=0 green; 2 mutants, 2 killed, 0 survived … — PASS, tree restored (runner md5-verified).

  • PTX pins: just gate-ptx-spill (box queue, A6000, job s1783): generate_ds41 entries=333 pinned=333 violations=0 PASS, gate_e2e entries=266 pinned=266 violations=0 PASS, gate_qwen3vl_tower entries=16 pinned=16 violations=0 PASS — the gemm_iq2xxs pins held at 0/0 (no re-pin).

  • Unsafe ratchet: tools/check-unsafe.sh ok — 27 crates, 2835 blocks, 394 fns, 28 impls (7257b9e holds gemm_iq2xxs's two blocks).

  • just remote-static origin/main (second Mac): in flight on the box-rebased head; the result lands in the PR conversation when it finishes.

  • Engine use: on bolt-prefill-g1 (this commit underneath), the real Bolt on the A6000 loads, and a 729-id prompt through the batched prefill gives the same 24 greedy ids as --prefill step (box queue, generate_qwen3moe).

Not done

  • just remote-static origin/main (second Mac): in flight; the result lands in the PR conversation.

🤖 Generated with Claude Code

@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 labels Oct 16, 2026
midagedev and others added 4 commits October 16, 2026 23:55
gemm_iq2xxs decodes the file's 66-byte super-blocks through the IQ2_XXS
grid and the computed signs, keeps 2s + 1 an integer factor of the
block sum and scales by d/8 in the epilogue. gate_gemm runs a routed
top-9 stack and a ragged odd-super-block slab under the four routings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The JIT scan (gate-ptx-spill on the 3090, sm_86) was red on exactly this
entry in both bins before the rows: spill=0 jit_local=0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
midagedev added a commit that referenced this pull request Oct 16, 2026
Baseline cards for the engine's g1c head (rebased onto #327 dd369dc +
the split): the derived P=512 row over g1b's measured rates, the per-row
gqa_flash_seg launch term (0.90 s A6000 / 1.06 s 3090 at P=4096, nsys
s1733/s1734) replaced by 48 enqueues a pass-layer.

Co-Authored-By: Claude Code <[email protected]>
@midagedev
midagedev marked this pull request as ready for review October 16, 2026 15:02
midagedev added a commit that referenced this pull request Oct 16, 2026
Baseline cards for the engine's g1c head (rebased onto #327 dd369dc +
the split): the derived P=512 row over g1b's measured rates, the per-row
gqa_flash_seg launch term (0.90 s A6000 / 1.06 s 3090 at P=4096, nsys
s1733/s1734) replaced by 48 enqueues a pass-layer.

Co-Authored-By: Claude Code <[email protected]>

@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 IQ2_XXS entry can read past its weight buffer; two mechanical quality defects also remain.

Must fix

1. Avoid the second word load for an aligned window

crates/gpu/src/gemm/grouped.rs:640 calls win, which unconditionally reads the next word. With one expert, 16 rows, K=512 and a slot routed to expert 0, the host checks accept 528 weight words. The last row's final aux window starts at byte 2108 and reads indices 527 and 528 [derived]; index 528 is outside the buffer (kind A, R19). The main new gate has the same boundary. Whole-word padding provides no extra word.
Fixing the shared reader now prevents this defect from entering the next engine caller. Replace win below, retain its inline attribute, and change its safety contract to require the four-byte window inside w. Run the existing IQ2_XXS gate under memcheck; numerical assertions cannot detect an unused over-read.

pub(super) unsafe fn win(w: &[u32], byte: usize) -> u32 {
    let a = byte / 4;
    // SAFETY: the four-byte window is inside w.
    let lo = unsafe { *w.get_unchecked(a) };
    let shift = 8 * (byte & 3);
    if shift == 0 {
        return lo;
    }
    // SAFETY: an unaligned four-byte window also covers word a + 1.
    let hi = unsafe { *w.get_unchecked(a + 1) };
    (lo >> shift) | (hi << (32 - shift))
}

2. Reuse the existing fragment layout

crates/gpu/src/gemm/grouped.rs:647 copies crates/gpu/src/gemm/grouped.rs:706 exactly: both map the same decoded tuples into MMA fragments and scales. This is one layout fact with two owners (kind B, R14). Forwarding removes ten lines and adds no item, so subsequent layout changes need one edit.
Replace the new @frag arm with:

(@frag $r0:ident, $r1:ident, $pp:expr, $hh:expr) => {
    iq3xxs_dec!(@frag $r0, $r1, $pp, $hh)
};

3. Remove the unused IQ2_XXS fixture branch

crates/gpu-gates/src/bin/gate_gemm.rs:1292 adds a fixture-reader arm that no caller reaches (kind B). The sole caller's crates/gpu-gates/src/bin/gate_gemm.rs:2454 input list contains only IQ3_XXS and IQ4_XS; both IQ2_XXS cases call synthetic directly. Removing the unused arm now avoids maintaining unused fixture behavior.
Replace the local match with:

let name = match ty {
    GemmWeight::Iq3Xxs => "iq3_xxs",
    GemmWeight::Iq4Xs => "iq4_xs",
    _ => return synthetic(ty, n_sb_total, seed),
};

Non-blocking

⚠ Complete the add-class proof: crates/gpu/src/gemm/kernels.rs:584 adds a kernel entry. AGENTS requires a PTX comparison showing only the new entries and a spill gate red on those entries before pinning. The body reports the green spill run only. Attach both missing outputs; this verifies that existing entries stayed unchanged.

Checked

Duplicates: 7 new definitions and 1 variant Grepped; the fragment-layout copy is above. Both dryer pairs read: kernel ABI wrappers share shape and already use gemm_block; no duplicate body. gpu-gates checked by hand.
Complexity: decode 24→28 adds a format arm and decode loops; run 33→34, check_ref 19→20 and iq_rows_diag 16→17 add case/type dispatch. No split requested.
Silent failure: 3 sites followed: ignored unit tuple, diagnostic None and filtered-case success; no discarded Result.
Tests: the PR reports scale/sign mutants killed on an A6000. Numerical assertions do not establish that the aligned extra read is in bounds.
Gates: no bands relaxed; unsafe-ratchet increase has a dated PIN. Escape catalog C1–C4: none matched.

Notes

GPU gates were not run here. The body lacks the nvidia-smi card/driver line and complete gate invocation, so gpu.json records evidence=false.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/grouped.rs, crates/gpu/src/gemm/kernels.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)

Comment thread crates/gpu-gates/src/bin/gate_gemm.rs Outdated
/// when the dump is not there.
fn synth_blocks(ty: GemmWeight, n_sb_total: usize, seed: u64) -> Vec<u8> {
let name = match ty {
GemmWeight::Iq2Xxs => "iq2_xxs",

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.

Remove this unused fixture arm: synth_blocks is called only with Iq3Xxs or Iq4Xs; both IQ2_XXS cases call synthetic directly.

Suggested change
GemmWeight::Iq2Xxs => "iq2_xxs",

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Removed in b6b5687 — synth_blocks keeps only the two arms its caller reaches.

Counter: this comment was written by the GLM agent (engine worker) under the lead's direction.

midagedev and others added 3 commits October 17, 2026 01:07
synth_blocks' one caller passes Iq3Xxs or Iq4Xs; the iq2xxs cases build
their stacks by the seeded synthetic path (Shingler #327 MUST_FIX 3).

Co-Authored-By: Claude Code <[email protected]>
An aligned window (byte a multiple of 4) read word a+1 all the same, so the
last row's last window read one word past the weight buffer (R19; Shingler
#327 MUST_FIX 1). The second word's index is now (byte + 3) / 4: word a
itself when aligned, a+1 only when the window spans it — the same value in
every case, no branch.

Co-Authored-By: Claude Code <[email protected]>
The two arms were the same text (Shingler #327 MUST_FIX 2); the forward
keeps one owner.

Co-Authored-By: Claude Code <[email protected]>
@midagedev

Copy link
Copy Markdown
Owner Author

Must fix 1 (win over-read) — fixed in 323ab55.

win now takes the second word's index as (byte + 3) / 4: word a itself for an aligned window, a + 1 only when the window spans it. Branchless, the same value in every case, one unsafe block, #[inline(always)] kept; the safety contract now reads word a by this fn's contract and the last-byte word only when the window is unaligned.

Evidence (box, box queue):

  • memcheck (compute-sanitizer --tool memcheck) of gate_gemm --case iq2xxs at dd369dc, before the fix: Invalid __global__ read of size 4 bytes at gemm_iq2xxs+0x1b50 … 1 bytes after the nearest allocation … of size 27,033,600 bytes, ERROR SUMMARY: 215 errors, exit 99.
  • The same command at the fix: 0 errors, exit 0.
  • gate-gpu-gemm real tier PASSED (rc 0); per-case y_fnv identical to the unfixed run (e.g. iq2xxs_gate_synth T=1 routing=Uniform … y_fnv=4cc8efc1e3f16aea on both sides) — no output bit moved.
  • gate-ptx-spill: generate_ds41 333/333, gate_e2e 266/266, gate_qwen3vl_tower 16/16, violations 0 — the pins held, no re-pin.
  • canon-check dd369dcb7..HEAD: 874 of 886 entries identical; the 12 changed are gemm_{iq2xxs,iq3xxs,q3k,q6k} in the gate_glm5next_e2e, gate_kquant and gate_swap bins (q3k regs 118→122, q6k 126→125). Their gates rerun green: gate-gpu-kquant PASS, gate-gpu-swap PASS (10 s), gate-gpu-glm5next-e2e PASS (733 s).
  • tools/check-unsafe.sh ok (2835 blocks — unchanged).

Counter: this comment was written by the GLM agent (engine worker) under the lead's direction.

@midagedev

Copy link
Copy Markdown
Owner Author

Must fix 2 (@frag duplication) — fixed in 6bf6942.

iq2xxs_dec's @frag arm now forwards to iq3xxs_dec!(@frag $r0, $r1, $pp, $hh); the layout fact has one owner.

Counter: this comment was written by the GLM agent (engine worker) under the lead's direction.

@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.

Since the last review:

  • Over-read: fixed in grouped.rs:1272; the aligned final window reads its own word twice.
  • Fragment duplication: fixed in grouped.rs:648 by forwarding to the existing layout.
  • Unused fixture arm: fixed in gate_gemm.rs:1291; its only caller still supplies IQ3_XXS or IQ4_XS.

Verdict: CLEAN: all three earlier must-fix items are resolved, and the incremental changes introduce no proven defect.

Checked

Duplicates: no new definitions; IQ2_XXS now reuses the IQ3_XXS fragment layout.
Complexity: the incremental diff adds no branches.
Silent failure: no new defaults or discarded errors; the removed fixture arm has no caller.
Tests: no test changes. An arithmetic check passed all 8,128 four-byte windows in word-aligned streams of 4–256 bytes, confirming values and read bounds. The PR reports memcheck failing before the fix and passing after it, and the earlier scale/sign mutants killed.
Gates: no incremental band, pin or coverage relaxation. Escape catalog C1–C4: none matched.
Performance: the shared reader changes integer address selection; its two word reads remain. No launches, copies or synchronization are added.

Notes

GPU gates were not run here; the GPU results above are author-reported.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/grouped.rs, crates/gpu/src/gemm/kernels.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)

@midagedev
midagedev merged commit 688a479 into main Oct 16, 2026
7 checks passed
midagedev added a commit that referenced this pull request Oct 16, 2026
synth_blocks' one caller passes Iq3Xxs or Iq4Xs; the iq2xxs cases build
their stacks by the seeded synthetic path (Shingler #327 MUST_FIX 3).

Co-Authored-By: Claude Code <[email protected]>
midagedev added a commit that referenced this pull request Oct 16, 2026
An aligned window (byte a multiple of 4) read word a+1 all the same, so the
last row's last window read one word past the weight buffer (R19; Shingler
#327 MUST_FIX 1). The second word's index is now (byte + 3) / 4: word a
itself when aligned, a+1 only when the window spans it — the same value in
every case, no branch.

Co-Authored-By: Claude Code <[email protected]>
midagedev added a commit that referenced this pull request Oct 16, 2026
Baseline cards for the engine's g1c head (rebased onto #327 dd369dc +
the split): the derived P=512 row over g1b's measured rates, the per-row
gqa_flash_seg launch term (0.90 s A6000 / 1.06 s 3090 at P=4096, nsys
s1733/s1734) replaced by 48 enqueues a pass-layer.

Co-Authored-By: Claude Code <[email protected]>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant