Skip to content

gpu: gemm_q8_0p_slab reads the Q8_0 planes a row's step at a time for a full card of tiles that share their slabs - #549

Merged
juliankang4 merged 6 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q8-prompt-gemm
Oct 20, 2026
Merged

juliankang4 merged 6 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q8-prompt-gemm

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Qwen3.8의 dense 프롬프트 projection에서 Q8_0 GEMM이 카드를 한 wave 이상 채우면 tile들이 weight slab을 함께 읽는 새 커널로 계산하게 해서, DGX Spark 한 대의 prefill을 빠르게 하는 작업입니다.

Part of #493.

What and why

This PR adds gemm_q8_0p_slab, a second entry for the Q8_0-plane prompt GEMM. The launcher takes it when the grid fills at least one wave of the card (as many blocks as all its SMs hold at once) and the slots fill more than one 64-column tile an expert. Every other launch keeps gemm_q8_0p, whose kernel does not change. On one DGX Spark, Qwen3.8-Flash-Next UD-Q4_K_XL prefills 2,048 tokens at 1296.56 tok/s against 1188.63 tok/s on main (+9.1%), and 512 tokens 7.7% faster. On an RTX 5090, UD-Q3_K_XL prefill is 2.3% faster at 2,048 tokens and 2.4% faster at 512. Decode speed is the same on both cards. On both, the new g32_slab case observes which entry each launch runs, and gate_gemm matches every checked output bit for bit to the contract's transcription.

Main's gemm_q8_0p is bound by L2 requests. An ncu profile of it on GB10, at the z projection (6144 x 2560) with 2,048 columns, shows the L2 tag stage at 86.3% of peak while the IMMA pipe is active 20.7% of the time. Each 64-value step sends two code requests a row (16-byte chunks 32 bytes apart, each reading half of two sectors) and one 2-byte scale request a row. Blocks go tile by tile, so a stack larger than L2 (qkv at 27.8 MB and q at 33 MB, against GB10's 24 MiB) comes from DRAM once per tile.

  • gpu/gemm/gemm32.rs: gemm_q8_0p_slab reads the same planes with the same products, order and bits. Four threads copy a slab row's 64 bytes of a step in one request (w32!(@stage slab)). Each thread pair keeps a 16-byte window of eight scales, read once every four steps, and decodes its scale at the store (@window, @load, @store). An expert of 8 MiB of planes or more (GEMM32_RASTER_BYTES) runs its blocks in groups of 16 tiles, slab by slab (raster32), so the blocks resident at once share each weight slab. A smaller stack stays in L2 and runs tile by tile, as before.
  • Gemm32Kernels::q8_0p_slab(grid, n_slots, n_experts) picks the entry: gemm_q8_0p_slab when the grid is at least one wave and the slots fill more than one tile an expert, else gemm_q8_0p. The extra reads cost a few instructions a step. They pay back when several tiles read each slab across a full card, but not when each slab is read once (16 or 64 columns) or the grid fits in one wave. One wave is the card's SM count times the entry's resident blocks an SM, which Gemm32Kernels::load reads from the card: 48 x 2 = 96 blocks on GB10 and 170 x 2 = 340 on an RTX 5090.
  • Also in gemm32.rs: both entries copy the code plane in 16-byte chunks, and gemm_q8_0p_slab also reads a thread pair's eight scales as one 16-byte load. A window into the code plane is only 4-byte aligned, and one into the scale plane only 2-byte aligned. Before either entry launches, enqueue_gemm32 checks the code plane with tensor::check_alignment, and before a slab launch it checks the scale plane too. A misaligned plane gets a GpuError::Shape that names it (Q8_0 code plane or Q8_0 scale plane), and nothing launches.
  • gpu/gemm/kernels32.rs: the gemm_q8_0p_slab kernel entry and its launch contract.
  • gate_gemm.rs: the new g32_slab case runs one 4112-row stack at K 2560 and at K 2592 (81 blocks a row, so rows start inside a group of eight scales; 11.2 and 11.3 MB of planes, past the raster threshold) at 16 columns (one tile, 33 blocks), 65 columns (two tiles, 66 blocks) and 1100 columns (594 blocks). It captures each launch in a graph, reads the kernel node's entry name and grid, and expects gemm_q8_0p_slab where the slots pass one tile and the grid reaches the wave that Gemm32Kernels::q8_0p_wave() reports for the card. Numerical checks cover all outputs at 16/65 columns and a rotating quarter at 1100; checked outputs match the f64 band and the contract's transcription bit for bit. Every output is checked for writes and contributes to y_fnv. Its g32_slab_selector clause checks q8_0p_slab one block under the wave, at the wave and at one tile. g32_slab_scale_unaligned launches the slab entry with the scales at byte offset 2, and g32_slab_code_unaligned launches each entry (1100 and 16 columns) with the codes at byte offset 4, at each K. Both expect the named refusal with the output untouched. The first commit adds bench arms for gemm_q8_0p at Qwen3.8's dense prompt projections and around the loaded wave.
  • tools/ref/ptx-shapes*.tsv gain a gemm_q8_0p_slab row, and tools/unsafe-ratchet.txt moves from gpu 1986 to gpu 1991 with a PIN(2026-10-20) line.

Release notes: Changed: Qwen3.8's dense Q8_0 prompt projections take a faster GEMM entry, gemm_q8_0p_slab, when a launch fills the card, with the same output bits.

Code shape

  • No second copy. gemm_q8_0p_slab is a new weight arm of the existing w32! and gemm32_block! macros, so it shares the K walk, the MMA and the epilogue with the other six 32-value entries, which keep their code.
  • Model code holds only architecture facts. No model file changes. The choice between the two entries reads only the grid, the slot count and the card.
  • Dead code is gone. Nothing becomes unused: gemm_q8_0p still runs every launch under one wave or at one tile an expert.
  • No silent failure. A wave count that overflows u32 is a named GpuError::shape at load. A code plane that is not 16-byte aligned, or a scale plane that is not at a slab launch, is a named GpuError::Shape before the launch, which leaves the output untouched. A block past the live tiles returns at once.
  • Comments state what is true now.
  • No relaxed gate. No band moved. The ratchet goes from gpu 1986 to gpu 1991: four blocks in the new w32! arms and one in the raster prologue, each with a SAFETY line, under a PIN(2026-10-20) line that names them.
  • rustfmt --check passes on gate_gemm.rs, and clippy reports no warning on a line this PR adds.

Duplication found or removed: none.

What I ran

The head of this PR is 05e0fbfb on upstream e3ce40fd. The g32 gate ran on both cards at this head, and so did the GB10 prefill A/B. The kernel bench, the RTX 5090 prefill A/B and the q5_0p and g38 gate cases were measured at a3f0dde9 on upstream 7c7a3e94, except the RTX 5090 kernel bench, which ran at a32ca4af, whose kernel, launcher and bench code are the same.

The kernel bench and the RTX 5090 A/B predate the current slab body, which has no scalar scale fallback: a thread pair always reads its window of eight scales with one 16-byte load. kernels32.rs is the same as at a3f0dde9, and gemm_q8_0p has the same PTX as main. gemm_q8_0p_slab uses 118 registers on GB10 and 120 on the RTX 5090, with no spill on either (ptxas 13.3.73). The other changes in gemm32.rs are host code: the code-plane alignment check before either Q8_0-plane entry, the scale-plane check before a slab launch, the q8_0p_wave() accessor that the gate and the bench read, and an unused import that upstream removed. The bench arms keep their sizes on both cards, because half of the loaded wave is the SM count on each.

Correctness

I ran just gate-gpu-gemm --case g32 at 05e0fbfb on GB10 (BLOOMERY_ARCH=sm_121) and on the RTX 5090 (sm_120). The oracle is the f64 reference band and the contract's transcription, which every checked output must match bit for bit. The q5_0p and g38 cases run entries other than the two Q8_0-plane ones, whose code this head does not change, so their results come from a3f0dde9.

Card g32 at 05e0fbfb q5_0p at a3f0dde9 g38 at a3f0dde9
GB10 292 PASS, 0 FAIL, rc 0 16 PASS, rc 0 36 PASS, rc 0
RTX 5090 292 PASS, 0 FAIL, rc 0 16 PASS, rc 0 36 PASS, rc 0

The 177 y_fnv output hashes that the g32 run prints are the same on both cards. A hash covers every output of its row; the band and bit comparison covers the outputs a row reports as checked.

g32_slab_selector passes at the loaded wave (wave=96 on GB10, wave=340 on the RTX 5090). In g32_slab at K 2560, m 16 (grid 33, checked=65792) and m 65 (grid 66, checked=267280) report entry=gemm_q8_0p entry_ok=true, and m 1100 (grid 594, checked=1130800) reports entry=gemm_q8_0p_slab entry_ok=true. All three report band_off=0 contract_bits_differ=0 with the same output hashes on both cards (y_fnv=256df6760857058e, 8af9d8d4c394d283 and 9521145de741e977). At K 2592 the same three rows pass with the same entries, the same checked counts and the same hashes on both cards (y_fnv=886af2dc789c238c, 9c8f676e2af435aa and 1beb2efa95ae3d60). The refusal rows report named_shape=true output_untouched=true on both cards at each K: g32_slab_scale_unaligned at byte offset 2 with the detail Q8_0 scale plane, and g32_slab_code_unaligned at byte offset 4 for m 1100 and m 16 with the detail Q8_0 code plane, each is not 16-byte aligned.

Each new clause fails first on the defect it guards. All of these runs were on GB10 except the first. At 05e0fbfb, on the RTX 5090:

  • With the slab @prep window replaced by zeros, K 2592 at m 1100 reports band_off=989051 contract_bits_differ=989443 and FAIL, K 2560 at m 1100 still passes, and the gate exits with rc 1.

At b3f9bcbd:

  • With the slab launch in enqueue_gemm32 changed to launch gemm_q8_0p, the m 1100 row reports entry=gemm_q8_0p entry_ok=false and FAIL while its numbers still match (band_off=0 contract_bits_differ=0 y_fnv=9521145de741e977), and the gate exits with rc 1.
  • Built without the code-plane check, g32_slab_code_unaligned gets result=Ok(()) and reports named_shape=false output_untouched=false FAIL with rc 1, at m 1100 on the slab entry and, in a separate run, at m 16 on gemm_q8_0p.

At 2ef4554f:

  • Built without the scale-alignment check, g32_slab_scale_unaligned gets result=Ok(()) and reports named_shape=false output_untouched=false FAIL, and the gate exits with rc 1.
  • To stand in for a smaller card, I set the loaded wave to 60 on GB10. This is a test setting, not a measurement on a smaller GPU. An expectation that only the 1100-column row takes the slab entry fails the m 65 row there (grid=66 slab=true, contract_bits_differ=0) with rc 1. The loaded-wave expectation passes the same input with rc 0.
  • With the same wave of 60 and only grid >= self.q8_0p_wave() && removed from q8_0p_slab, g32_slab_selector reports FAIL and the gate exits with rc 1.

Kernel bench

gate_gemm --bench-kernels --bench-arm <arm> at Qwen3.8's dense prompt shapes. Head and main were built from the same tree with each side's kernel files and run alternately (head, main, main, head) for each arm: one round on GB10 (two runs a side) and two rounds on the RTX 5090 (four a side). Each run reports its rate in TOPS (2 x T x rows x K over the minimum graph time), and the table gives each side's median at 2,048 columns.

Projection Rows x K GB10 main to head GB10 change RTX 5090 main to head RTX 5090 change
qkv 10240 x 2560 27.13 to 52.41 +93.2% 152.82 to 189.06 +23.7%
z 6144 x 2560 43.66 to 52.78 +20.9% 159.89 to 202.57 +26.7%
out 2560 x 6144 44.96 to 57.45 +27.8% 157.18 to 210.48 +33.9%
q 12288 x 2560 21.81 to 52.71 +141.7% 154.32 to 184.52 +19.6%
kv 512 x 2560 36.57 to 46.02 +25.8% 129.00 to 128.97 (under one wave) -0.0%
shared expert gate and up 640 x 2560 40.05 to 48.31 +20.7% 151.14 to 151.14 (under one wave) +0.0%
shared expert down 2560 x 640 31.52 to 32.38 +2.7% 168.57 to 186.03 +10.4%
hyper-connection down 320 x 10240 44.23 to 48.97 +10.7% 88.37 to 88.34 (under one wave) -0.0%
hyper-connection up 10240 x 320 22.34 to 24.49 +9.7% 141.18 to 145.59 +3.1%

At the wave boundary with two tiles, the arm one wave wide runs gemm_q8_0p_slab and gains 4.0% on GB10 (96 blocks) and 14.1% on the RTX 5090 (340 blocks). The arm two blocks under the wave runs gemm_q8_0p and reads the same as main (GB10 -0.1%, RTX 5090 +0.0%).

The one-tile arms at the wave boundary (wavem1_t16, wave_t16, wavep1_t16) read -0.7%, -0.5% and -0.9% on GB10 and 0.0% on the RTX 5090. They run main's gemm_q8_0p on the head too, because one tile an expert keeps that entry. The sm_121 builds of head and main differ only by the rows of the new gemm_q8_0p_slab entry, and gemm_q8_0p has the same PTX and the same ptxas row (ptxas 13.3.73) in both, so it is the same machine code. The difference comes from how the two builds lay out the binary.

Prefill A/B on one DGX Spark

generate_qwen3moe with Qwen3.8-Flash-Next UD-Q4_K_XL, the first P ids of an English prose corpus as the prompt, BLOOMERY_DRAFT=off, 32 generated tokens, --time, one process a row:

BLOOMERY_REF_MODEL="$Q" BLOOMERY_DRAFT=off generate_qwen3moe --tokens "$T" -n 32 --ctx "$C" --time

Two untimed runs (main, then head, at P 2048) came first. Then P 2048 ran head, main, main, head twice, and P 512 once, all in one sitting. Head is 05e0fbfb, and main is upstream e3ce40fd built from its own tree.

P Arm pp tok/s runs Median decode tok/s (p50) median
2048 main 1174.17, 1191.33, 1186.42, 1190.83 1188.63 30.05
2048 head 1300.62, 1294.69, 1296.20, 1296.91 1296.56 30.06
512 main 878.69, 856.48 867.59 30.19
512 head 934.05, 934.06 934.06 30.30

Delta (head against main, medians): pp2048 +9.1%, pp512 +7.7%; decode unchanged.

Every head row is above every main row. The card's 1 GiB device-memory stream read measured 241.8 GB/s before the sitting and 239.6 GB/s after it, and the kernel log showed no Xid.

Prefill A/B on an RTX 5090

The same command and order with Qwen3.8-Flash-Next UD-Q3_K_XL, measured at head a3f0dde9 after one untimed run, with main built from upstream 7c7a3e94's tree for sm_120:

P Arm pp tok/s runs Median decode tok/s (p50) median
2048 main 1681.48, 1665.08, 1641.17, 1653.30 1659.19 100.36
2048 head 1695.28, 1710.57, 1700.47, 1692.33 1697.88 101.13
512 main 953.14, 988.77 970.96 101.69
512 head 994.21, 995.00 994.61 101.92

Delta (head against main, medians): pp2048 +2.3%, pp512 +2.4%; decode unchanged.

An nsys profile of the head at P 2048 confirms that both entries run in this prefill: gemm_q8_0p_slab 279 launches for 16.1% of GPU time and gemm_q8_0p 217 launches for 5.3%.

Hardware

Three machines: one DGX Spark ran the gate and the kernel bench, a second DGX Spark ran the prefill A/B, and an RTX 5090 machine ran the gate, the kernel bench and its A/B.

nvidia-smi on each card in the gate run at 05e0fbfb:

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |
| NVIDIA-SMI 595.99.02              Driver Version: 595.99.02      CUDA Version: 13.2     |
|   0  NVIDIA GeForce RTX 5090        Off |   00000000:01:00.0 Off |                  N/A |

GB10 is compute capability 12.1 (BLOOMERY_ARCH=sm_121), and the RTX 5090 is 12.0 (sm_120). The DGX Spark that ran the A/B reports the same NVIDIA GB10, 595.84.

just gate-gpu-gemm --case g32 builds gate_gemm for the card and runs it. Its last lines on GB10, then on the RTX 5090:

gemm filter --case g32: only the matching cases ran
gpu-gate.sh: gate_gemm ran 13 s (exit 0)
gemm filter --case g32: only the matching cases ran
gpu-gate.sh: gate_gemm ran 11 s (exit 0)

The q5_0p and g38 summary lines from the GB10 run at a3f0dde9:

GATE q5_0p rc=0 lines=20 pass=16 fail=0
GATE g38 rc=0 lines=40 pass=36 fail=0

Models: unsloth/Qwen3.8-Flash-Next-GGUF at revision 766911a6, UD-Q4_K_XL, on the DGX Spark, and Qwen3.8-Flash-Next UD-Q3_K_XL (first shard sha256 f2ef4328) on the RTX 5090.

What I ran into

None of this blocks the PR. Nothing came up that needs a maintainer's attention.

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 20, 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 slab path accepts a code plane that violates its asynchronous copy alignment.

Must fix

1. Check the code plane's alignment before dispatch

gemm32.rs:297 copies 16 bytes from qs, with every source offset a multiple of 16 bytes. The safe tensor window constructor checks only the element's alignment, so DeviceTensor::<u32>::window_of(&parent, 4, 4112, 640) succeeds for a sufficiently large aligned parent.

Use that window with an aligned scale plane, K=2560, and a filled dense route of 1100 slots. The shape checks accept it; the derived grid is 594 blocks and selects the slab entry when the loaded wave is at most 594. The launcher check validates only d. Every code source address remains 4 modulo 16, reaching undefined device memory operations instead of a named shape error (kind A, R19).

One shared check protects both Q8_0-plane entries and avoids diagnosing an asynchronous device failure later. Inside the Q8_0Plane match arm, insert this before the selector:

crate::tensor::check_alignment::<cuda_device::vector::U32x4, _>(
    "Gemm32Kernels::enqueue_gemm32",
    "Q8_0 code plane",
    qs.buf(),
)?;

Extend the existing alignment gate with an aligned scale plane and a code-plane window at byte offset 4. Assert a shape error naming Q8_0 code plane and untouched output; removing this check must turn the clause red.

Checked

Traced the tensor window, shape validation, selector, launch contract and new slab copy. The current alignment gate offsets only the scale plane. No escape-catalog pattern or ledger lesson applies.

Notes

Validation was static; GPU gates were not run. The PR reports GB10 and RTX 5090 gate output.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/gemm32.rs, crates/gpu/src/gemm/kernels32.rs and carries a gate run on the author's card, which counts as verified.

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

@github-actions github-actions Bot added gpu-evidence Verified on the author's card: the pull request carries the gate run and its output and removed needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 20, 2026
… a full card of tiles that share their slabs

The Q8_0-plane GEMM was bound by L2 requests, not by the tensor pipe: on
GB10 its L2 tag stage ran at 86 % of peak while the IMMA pipe ran at 21 %.
Each 64-value step sent two code requests a row (16-byte chunks 32 bytes
apart, each reading half of two sectors) and one 2-byte scale request a
row, and a stack larger than L2 (qkv, q) came from DRAM once per tile.

gemm_q8_0p_slab reads the same planes with the same products, order and
bits: four threads copy a row's 64 bytes in one instruction, each pair
keeps a 16-byte window of eight scales, read once per four steps and
decoded at the store, and an expert of 8 MiB of planes or more goes in
groups of 16 tiles, slab by slab, so the resident blocks share their
weight slabs.

Those reads cost a few instructions a step. They pay where several tiles
read each slab across a full card, and lose up to 8 % on GB10 where each
slab is read once (16 or 64 columns) or the grid fits in one wave. So
the launcher takes the new entry for a grid of one wave or more (the
card's SMs times the entry's resident blocks an SM, read at load) whose
slots pass one tile an expert (Gemm32Kernels::q8_0p_slab), and
gemm_q8_0p, unchanged, otherwise. The other five 32-value entries keep
their code.

gate_gemm's g32_slab runs one stack past the raster's bytes at one tile
and at two tiles under a wave (gemm_q8_0p) and at a full card
(gemm_q8_0p_slab), checks the entry q8_0p_slab names for each, and
holds every output bit for bit to the contract's transcription.
…cales

At K 2560 every row of the stack starts on a group of eight scales, so
the slab entry's first step always moves the window and the window
@prep binds is never read. K 2592 is 81 blocks a row: most rows start
inside a group, and their first scales come from that window.

@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: finding 1 is fixed at gemm32.rs:1417. Both Q8_0-plane entries check code alignment before launch. The gate checks named refusal and untouched output for both entries.

Verdict: CLEAN: The interdiff closes the alignment defect and preserves bounds for the revised scale loads.

Checked

Duplicates: 0 new definitions in the interdiff. Both dryer pairs use the existing gemm32_block! core; their weight-layout arms differ.
Complexity: slab_case (55) adds graph inspection, refusal checks and a second K. q8p_gemm_arms (19) is unchanged benchmark enumeration.
Silent failure: 1 zero-default site followed. Only inactive row pairs reach it; live scale windows fit within the checked stack.
Tests: removing the alignment checks fails the refusal assertions; forcing the old entry at gemm32.rs:1428 fails entry_ok; zeroing the initial window at gemm32.rs:339 fails the K=2592 numerical assertions. The PR reports red runs for these mutants.
Bounds: standalone arithmetic checks passed for every supported K, scale-window transitions, partial raster groups and the empty-tile mapping.
Gates: numerical bands unchanged. The unsafe ratchet and replaced gate clauses have dated PIN reasons.
Performance: the changed production path adds no launches, copies, allocations or synchronization. The PR supplies gate output and timed prefill A/B evidence.
Escape catalog: no matching pattern.

Notes

Validation here was static plus arithmetic checks; no CUDA build or GPU gate was run. The PR supplies GB10 and RTX 5090 gate output.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/gemm32.rs, crates/gpu/src/gemm/kernels32.rs and carries a gate run on the author's card, which counts as verified.

🔨 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 8049e44 into midagedev:main Oct 20, 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-evidence Verified on the author's card: the pull request carries the gate run and its output 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