Skip to content

gpu, gpu-gates: Qwen3.8's MTP store walk projects and appends the keys and values alone - #765

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-mtp-storekv
Oct 24, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-mtp-storekv

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 MTP draft의 prompt 준비 단계에서 아무도 읽지 않는 query와 gate 계산을 빼고 key와 value만 계산하게 해서, GB10의 prefill 시간을 조금 줄이는 작업입니다.

Part of #565.

What and why

Qwen3.8's MTP draft keeps its own key/value store. While a prompt is prefilled, a store walk runs the draft layer over blocks of up to 64 prompt rows and appends each row's keys and values to that store. A later row reads the keys and values of earlier rows and nothing else, so no part of a store walk reads its query side. The walk still projected the query and the per-head gate (a 2560 x 12288 weight, 4.03 GFLOP per 64-row block), normed and turned the queries, and wrote them into a buffer nothing reads.

This PR makes the store walk compute the keys and values alone:

  • Mtp38::store_rows quantizes the mix for the k and v weights only and runs two attention GEMMs instead of three.
  • Mtp38::append_store calls the head-256 norm, rotation and append launch with n_head: 0. At n_head 0 the launch handles the key heads alone: it reads no query source and writes no query. The query and gate window arithmetic is gone from that function.
  • partial_launch in crates/gpu/src/rope_neox.rs accepts n_head 0. The five launchers that use it run the same device body (norm_partial_append), which splits its blocks into query and key heads by h < n_head, so at n_head 0 each block takes one key head. No kernel code changes. The other launchers keep refusing n_head 0.
  • gate_qwen35moe_attn gets a clause, "rope keys alone", that runs the existing rope clause's inputs at n_head 0. It requires the keys and both cache planes to equal the launch with 16 query heads bit for bit, the query buffer to stay at zero bits, and no fault.
  • tools/unsafe-ratchet.txt raises gpu-gates from 103 to 104 unsafe blocks under a PIN line for the clause's one launch.
  • StoreArena no longer holds the qg and q buffers, 4,718,592 bytes at its 64 rows (64 x (12,288 + 6,144) f32 x 4 B). The append's query arguments are the live walk arena's buffers, which the launch does not read at no query head.
  • mtp_arena_bytes in crates/model/src/arch/qwen35moe/place.rs counts those bytes out: 53,526,700 to 48,808,108 bytes at the 248,320-row head. The Qwen3.8 plan pins that depend on it (CARD_PLANS, CARD_PLANS_Q3, BP_DRAFTED and CARD_MAX_CTX_MTP) move with it under dated PIN lines.

Release notes: Changed: on GB10, Qwen3.8's MTP prompt preparation computes only the keys and values it stores, which trims prefill time, and the draft's arena is 4,718,592 bytes smaller; outputs are unchanged.

Code shape

  • No second copy. No new helper in production code: the store walk uses the existing append launcher and partial_launch. The new gate clause reuses rope_gate::run, rope_run, bits_equal and verdict.
  • Model code holds only architecture facts. The change is in the Qwen3.8 MTP walk and the shared rope launch helper, which gained no per-model branch.
  • Dead code is gone. The query and gate GEMM, their quantizer input, the query and gate windows of the append and the arena's query buffers are removed.
  • No silent failure. partial_launch still refuses zero n_kv, ctx or m and m > ctx by name; only n_head 0 is now accepted, with its meaning in the doc of PartialNeoxArgs.
  • Comments state what is true now. tools/check-comments.sh is ok.
  • No relaxed gate. No band, tolerance or lint moved. The unsafe ratchet rises by one block with its PIN(2026-10-24) line and SAFETY comment. The plan pins that follow the smaller arena each carry a PIN(2026-10-24) line with the derivation.
  • cargo fmt and cargo clippy are clean on what I touched: cargo fmt is clean, cargo clippy has not run on this head.

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

What I ran

Output is unchanged

In the timed run below, the greedy output of 128 tokens hashes the same on head and main for every arm and prompt. The draft counts differed in one place: the second prompt of the round 2 main arm drafted 102 tokens where the other five arms drafted 103, with the same 84 accepted. The default MTP width is chosen from measured pass times, so those counts move between runs of the same binary. A separate run on a second DGX Spark fixed the width (BLOOMERY_MTP_WIDTH=fixed) on both checkpoints, so that the draft side is compared on equal terms. It is an output check only, not a timing: FP4-Local drafted 105, 117 and 105 tokens with 90, 89 and 93 accepted on head and on main, and NVFP4 drafted 105, 126 and 105 with 90, 85 and 93 accepted on both. The token hashes match per prompt in each pair.

The store walk's own clauses in gate_qwen4exp_mtp print the same numbers on head and main: keys within 7.148e-3 and values within 6.997e-3 of the eager walks under Auto, 7.226e-3 and 7.338e-3 under Pass, and the warmup stores the host's 64 rows as given.

GPU evidence

The gate runs used an RTX 5090 (built for sm_86) at the head's arena change (the patch is equal to this head):

| 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 |
just gate-gpu-qwen4exp-mtp
(d) the draft program's arena 48808108 (the plan's 48808108) PASS
(s) the store walks (Auto, runs of 3) over a store other ids overwrote, against the eager walks over the same 64 positions: keys within 7.148e-3 (band 5.447e-2), values within 6.997e-3 (band 2.772e-2) PASS
(s) the store walks (Pass, runs of 3) over a store other ids overwrote, against the eager walks over the same 64 positions: keys within 7.226e-3 (band 5.447e-2), values within 7.338e-3 (band 2.772e-2) PASS
gpu-gate.sh: gate_qwen4exp_mtp ran 63 s (exit 1)

The first line is the smaller StoreArena matching the plan's new byte count exactly. The gate exits 1 on head and on main for one clause: (kp-draft) fails with "the checkpoint at 85 was evicted by the host budget" in both runs.

just gate-gpu-qwen35moe-attn
rope m=7 positions 3.. ctx=40 head 256 rot 64: q (read at head stride 512) bit_exact_host=true k in place bit_exact_host=true planes_exact=true rerun=true PASS
rope keys alone: m=7 at no query head, the keys in place and both planes the launch's with 16 query heads bit for bit true, no query written true PASS
rope-256 PASS
rope-256 q8 PASS
gpu-gate.sh: gate_qwen35moe_attn ran 1 s (exit 1)

This gate exits 1 on head and on main for one reason: its fixture-oracle clause needs the fx_ref_qwen35moe reference set, which is missing on that machine. The clauses above it pass.

The timing machine, a DGX Spark (GB10), reported:

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |

Plan pins

The RTX 5090 machine ran the plan tests on the head's code. Without the pin changes, only the arena change in place, hw_qwen4exp_card_plan (8 rows), hw_qwen4exp_q3_card_plan (6 rows) and hw_qwen4exp_bp_mtp_plan (2 rows) fail. With mtp_arena_bytes alone put back to its old value in a copy of the same tree, all three pass, so the failures come from the arena change and nothing else.

cargo test --release -p bloomery-model --lib the_mtp_arena_formula
test arch::qwen35moe::place::tests::mtp::the_mtp_arena_formula ... ok
cargo test --release -p bloomery-model --test qwen4exp_meta -- --ignored
test hw_qwen4exp_card_plan ... ok
test hw_qwen4exp_q3_card_plan ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 10.54s
cargo test --release -p bloomery-model --test qwen4exp_mtp_meta -- --ignored
test hw_qwen4exp_bp_mtp_plan ... ok

cargo check -p bloomery-gpu -p bloomery-gpu-gates --all-targets reports no warning and no error.

hw_qwen4exp_mtp_spec and hw_qwen4exp_mtp_plan open the unshared draft file, which that machine does not have, so they do not run to the end here. For CARD_MAX_CTX_MTP I ran hw_qwen4exp_mtp_plan in a temporary copy that points it at the shared draft file. It printed "A6000 with the draft holds ctx 1213983 (true; predicted 1213982) and breaks with CardOver at 1213984 (true): PASS" and the same for the 3090 (ctx 380583, predicted 380582). The same copy with the old arena value passes under the old pins.

FAIL-first

With partial_launch made to refuse n_head 0 again, the new clause prints "rope keys alone: the launch at no query head refused" with FAIL, rope-256 prints FAIL, and the gate exits with MUTANT-RC refuse-n_head-0 1. The tree was clean after the run.

Speed

One DGX Spark (GB10), Qwen3.8 FP4-Local, default sm_121a build of both trees, MTP on (BLOOMERY_DRAFT=mtp), C = 1. Each request is one of three 4096-id windows of a code corpus (offsets 0, 4096 and 8192) with 128 greedy tokens (/completion, temperature 0, top_k 1, cache_prompt false). Each arm started a fresh server and sent the three requests; the clock cap was lifted for each arm and restored after. One discarded warm load of the main binary came first, then the arms ran in the order head, main, main, head, head, main, so each tree has three arms. Reproduce with BLOOMERY_DRAFT=mtp BLOOMERY_HOST_ROOM=96G target/release/bloomery-serve -m <model dir> --host 127.0.0.1 --port 18674 --parallel 4 --ctx-size 16896, built with cargo oxide build --arch sm_121a -- -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,solar2,minimaxm3,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve. Main is 100c224fc.

Metric (one GB10, FP4-Local, MTP on, C@1, 4096-id prompts, median of three requests per arm) main head change
Prefill tok/s, round 1 (the head's first server start) 1474.7 1420.7 -3.7%
Prefill tok/s, round 2 1476.7 1491.8 +1.0%
Prefill tok/s, round 3 1473.5 1484.8 +0.8%
Prefill tok/s, median of the three rounds 1474.7 1484.8 +0.68%
Time to first token ms, round 1 2782.2 2887.4 +105.2 ms
Time to first token ms, round 2 2780.8 2751.4 -29.4 ms
Time to first token ms, round 3 2784.6 2763.2 -21.4 ms
Time to first token ms, median of the three rounds 2782.2 2763.2 -19.0 ms
Decode tok/s, median of the three rounds 56.46 56.19 -0.48%

Delta (head against main): median prefill +0.68% (1474.7 to 1484.8 tok/s) and median time to first token 19.0 ms lower (2782.2 to 2763.2 ms) over three rounds on one machine, with the head's first-start round 3.7% slower than main; one sitting, so a single observation, and a gain under 1%.

Round 1 is the head binary's first server start on that machine. Its load took 244.8 s against 230.2 s and 232.8 s for the other two head arms, and two of its three requests ran at 1,420.7 and 1,405.8 tok/s, which sets that round apart from every other arm. After that first start, the two later head arms were ahead of all three main arms on each of the three prompts: prefill +0.88, +0.75 and +0.91 %, time to first token 23.9, 20.9 and 25.3 ms lower. Decode is the same within the run-to-run spread. The read probe measured 231.2 to 238.8 GB/s in every arm, so the card was in the same state throughout.

The kernel time behind this, from one profiled 4096-id request with MTP on (Nsight Systems, an sm_121 build of an earlier commit of this patch): the BF16 GEMM at grid 96 took 122.20 ms in 163 launches on main and 97.41 ms in 97 launches on head, and the store walk's append kernel took 1.01 ms at grid 1,664 on main and 0.23 ms at grid 128 on head, about 25.6 ms less GPU time in all. Preparing the MTP prompt cost 78.81 ms of kernel time on main (MTP on against MTP off), so about 53 ms remains. The 19.0 ms gain in time to first token is smaller than the 25.6 ms of kernel time removed, and I have not isolated the difference.

Measured head

The timings are for the patch without the arena change, over main 100c224fc. The arena change touches an allocation and the unread query arguments only, not a launch, copy or sync, so the device code is the timed code. Main has moved since, including a change to the BF16 prompt-site activation copy that the store walk's GEMMs also use, so the time saved there may differ on the newer main. I did not repeat the timings on it.

Hardware

DGX Spark (GB10), driver 595.84, CUDA 13.3, Qwen3.8 FP4-Local for the timings and Qwen3.8 FP4-Local and NVFP4 for the fixed-width output check. The gate clauses and plan tests ran on an RTX 5090, driver 595.99.02, CUDA 13.2. The nvidia-smi lines are under GPU evidence.

What I ran into

None of this blocks the PR.

  • The gain is under 1 % in one sitting on one machine, a single observation. Round 1 ran slower for the head, so the median rests on the two rounds after the head binary's first start; I read that as a first-start effect but did not isolate it.
  • Under profiling, the head request's server prompt time was longer than main's (3,285.8 against 3,141.8 ms) because the GPU idle time was 268.9 ms against 120.2 ms. That request ran once with the profiler attached, and the timed arms without it show head ahead. I did not follow up the idle gap.
  • The walk's query norm used to fault on a non-finite value that existed only in the query weights. That value now shows up in the next draft walk that reads those weights. The keys and values come from the same mix, so a non-finite mix still faults in the key norm.
  • Not run: the fixed-width output check on this head (the device code is the checked code, and the gate clauses above ran on it); hw_qwen4exp_mtp_spec and hw_qwen4exp_mtp_plan unmodified (see Plan pins); a timed A/B on NVFP4, because the store walk does not depend on the weight format and NVFP4 was checked for output only; ptx-compare, because no kernel code changed, only the host's launch arguments; and the gate's fixture-oracle clause, whose reference set is missing.

AI help

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

…s and values alone

A store walk (the prompt's warmup) keeps a row's keys and values and nothing else of it, yet it projected each row's query and gate and normed and turned the queries. It now runs the k and v GEMMs over the mix and the head-256 append at no query head, which turns the keys and appends both planes bit for bit as the launch with its query heads does. The partial-turn launchers take n_head 0 as the key heads alone; gate_qwen35moe_attn's rope clause holds that launch to the full one's keys and planes and to writing no query.

The store walk's arena drops its query rows and turned queries, 4,718,592 B at its 64 rows: the append's unread query arguments are the live walk's buffers. place::mtp_arena_bytes counts 4,718,592 B less, and the Qwen3.8 plan pins that hold the draft's reserve (the drafted card plans, plan (b') with the draft and the largest drafted context) move with it.
@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 24, 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 merge-blocking defect found in the keys-only store walk or its arena accounting.

Non-blocking

⚠ Complete the hardware evidence: crates/gpu/src/arch/qwen3moe/mtp38.rs:2517. This changes launch count and append geometry. The timings cover an earlier patch and base; the gate logs omit the tested head hash and concrete model path. Attach both tested hashes, the model path, owning-gate output and a current-head A/B on the contributor's card. This makes the claimed gain reproducible.

Optimization headroom

  • G. Per-launch and per-sync fixed cost: crates/gpu/src/arch/qwen3moe/mtp38.rs:2467. Reuse the two dense routing tables when successive store_rows calls have the same total row count m. Their existing filled() state can avoid two launches per repeated 64-row call, or 1/32 launch per prompt token [derived].

Checked

Duplicates: 1 new function checked by Grep; no duplicate owner. Dryer reported 0 pairs; GPU and GPU-gates were searched manually.

Complexity: crapper flagged no changed function.

Silent failure: 1 new error arm followed; a refused launch returns false and makes the gate fail.

Tests, source mutation audit: restoring zero-head rejection at crates/gpu/src/rope_neox.rs:2008 fails the new clause. Restoring the old arena term at crates/model/src/arch/qwen35moe/place.rs:1063 fails the formula, Q4/Q3 card-plan and tier-plan assertions.

Gates: arena, planner and unsafe-count changes have dated PIN reasons; no tolerance moved.

Bounds: zero-query blocks access only key heads; short query buffers, block offsets and invalid cache positions were traced through the shared kernel.

Escape catalog: no C1–C4 pattern matched.

Notes

Static review only; no build or GPU gate was run here. The submitted GPU gates exit 1 on clauses the author reports also failing on base. The submitted evidence does not meet the complete GPU-evidence list.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_qwen35moe_attn.rs, crates/gpu/src/arch/qwen3moe/mtp38.rs, crates/gpu/src/rope_neox.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)

Filed outside this PR: #767

@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 1053680 into midagedev:main Oct 24, 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 model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja 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