Skip to content

gpu: Qwen3.8's decode selected flash merges under the out gate, one launch fewer a selecting layer - #1028

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-next-selected-attn
Oct 30, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-next-selected-attn

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 decode에서 selected flash의 merge가 out gate까지 한 번에 적용해, selecting layer마다 launch가 1개(토큰당 12개) 줄어듭니다.

Part of #565.

What and why

In Qwen3.8 decode, a selecting layer runs the selected flash merge and then a separate gqa_out_gate_f32 launch that multiplies the merge's output by the sigmoid of the query projection's gate. This PR adds gqa_flash_merge_256_gated, which writes each merged value through that gate in the same launch. A selecting layer goes from 14 launches to 13, which is 12 fewer per decoded token, and the Arena38::flash buffer that held the ungated output is gone.

The gated entry reuses the existing OutGate / SigmoidGate that the prefill flash already uses, through a gate parameter on merge_body. Ungated keeps the plain merge unchanged. A refused row writes the gate applied to NaN, as before.

End to end the effect is below what this measurement can separate: decode is +0.288% with the base and head ranges overlapping, and greedy output is identical. What the change lowers is the launch count and the time of the two launches it fuses, both under "What I ran".

Release notes: Under the hood: Qwen3.8 decode merges the selected flash and applies the out gate in one launch, 12 fewer launches per token.

Code shape

  • No second copy. The gate is the existing OutGate / SigmoidGate from flash_gqa_prefill.rs; the decode merge takes it as a generic parameter instead of copying the product.
  • Model code holds only architecture facts. program38.rs and scratch38.rs only drop the separate gate launch and its buffer. The kernel change sits in the common flash code.
  • Dead code is gone. qsa_out, Arena38::flash and the gate launch of the decode path are removed, and the launch counts and gate pins that moved are updated.
  • No silent failure. A refused row still raises its fault and writes NaN.
  • Comments state what is true now. The new comments describe the code as it is.
  • No relaxed gate. The re-pins (NODES_DECODE and the fixture launch counts, the unsafe ratchet, the new entry's PTX shape rows) carry PIN(2026-10-30): and the reason. No band or tolerance moved.
  • 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 GPU runs are on a DGX Spark. The PR head is 93d883929424f4e225f614e274ad17e3787aa494 on main 9fa4e0036cea9712f683a3443593031429de3bf3. The A/B and the first two gates ran on head 2b5cc298c0b55a3b0aa9aaf3d33851dac1ca89d9 on main 989d0106b36ca2141adf5d506c3ebdef0c86a1c9, gate-ptx-spill on the PR head, and the q38_fixture test on head f133b63864b24704b4551d25b416848f0d8cd4cf.

Decode A/B against main, FP4-Local Qwen3.8 with the draft off, one sequence, a 4096-token prompt and 128 decoded tokens, q8 KV (the default), three rounds per arm in alternating order:

Metric Base Head Difference Ranges overlap
decode tok/s (median) 31.505 31.595 +0.288% yes
prefill tok/s (median) 2777.31 2774.42 -0.104% yes
TTFT p95 ms (median) 1480.34 1481.93 -0.108% yes

Greedy output is identical between base and head.

Launches and kernel time over the 128 decoded tokens, from the same run: the merge and the out gate took 3.947 ms and 1.804 ms in 1536 launches each on the base, and the gated merge takes 4.282 ms in 1536 launches on the head, so the two together fall from 5.751 ms to 4.282 ms. The segment kernel is unchanged (23.607 ms on the base, 23.747 ms on the head). Total launches fall from 161,163 to 159,627, which is 12 per token.

GPU gates on the same patch:

  • just gate-gpu-minimaxm3-msa: rc 0, 36 PASS lines, 0 FAIL, gate_minimaxm3_msa: PASS. The selected flash lines include flash every block kept ... selected = dense group-16 pass bit for bit true PASS.
  • BLOOMERY_TIER=fixture just gate-gpu-qwen4exp-e2e: rc 0, 185 PASS lines, 0 FAIL, clauses: 27 ran, 9 left to the real tier. The decode graph holds 111 nodes (structure decode graph_nodes=111 (want 111; ...) PASS), and five eager steps against five graph replays give logits bit for bit true.
  • just gate-ptx-spill, with gqa_flash_merge_256_gated pinned at spill 0 and jit_local 0: generate_ds41 entries=499 pinned=499 and gate_qwen3vl_tower entries=26 pinned=26 ... violations=0 PASS.
  • cargo test --release -p bloomery-gpu-gates --lib -- q38_fixture: test result: ok. 2 passed; 0 failed.

Hardware (if the change is for a machine)

  • Card and driver: NVIDIA GB10, 595.84
  • CUDA: 13.3
  • Arch: sm_121a (cc 12.1)

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 30, 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 must-fix defect was found in the fused merge, its callers, or the updated graph counts.

Non-blocking

⚠ Run the extended hardware regression: crates/gpu/src/flash_gqa_prefill.rs:6372.
The reported commands omit just gate-gpu-lib, which runs the extended decode comparison.
Attach its output and a FAIL-first run with the gated merge passing Ungated. This checks rounding and named faults against the separate merge and gate before landing.

⚠ Complete the measurement evidence: crates/gpu/src/flash_gqa.rs:4699.
The description supplies card/driver prose, commit hashes and result summaries.
Attach the nvidia-smi output, exact model filename, and complete A/B commands with output beside those hashes. These make the reported launch-fusion result reproducible.

Checked

Duplicates: 3 new definitions checked; none duplicated. The merge reuses OutGate and SigmoidGate. Dryer reported 0 pairs; its excluded gpu-gates files were checked manually.

Complexity: enqueue_pass_256_p12_sel rose from 20 to 24 through gate-length validation and gated/ungated dispatch. Both paths retain the common merge body.

Silent failure: 3 paths followed. A short gate returns a named shape error. Invalid counts produce NaN and KeyCount. Non-finite gate products raise F32Product. Device faults reach Head::tokens as errors.

Tests: static mutation analysis only. Bypassing gate.apply at flash_gqa.rs:2753 breaks the bit comparison; removing the gate-length check at :7198 breaks the named-error assertion. Restoring 14 mixer launches at q38_fixture.rs:33 breaks both changed fixture tests.

Gates: graph-count and unsafe-ratchet changes have dated PIN reasons; numeric tolerances are unchanged. Launch-count arithmetic matches all four real-model pins.

Performance: decode launch fusion removes 1 launch and 49,152 bytes of intermediate traffic per selecting layer per token [derived: 24 heads × 256 values × 8 bytes]. No allocation, copy or synchronization was added.

Adversarial checks: short buffers, zero/out-of-range counts, NaN gates, and multiple slots are blocked by length checks, fault reporting, and sequence-local query/gate/output slices.

Escape catalog: no C1–C4 pattern matched.

Notes

CUDA builds and hardware gates were not run in this review environment. GPU evidence is incomplete under CONTRIBUTING.md's evidence list.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_qwen35moe_attn.rs, crates/gpu-gates/src/bin/gate_qwen4exp_e2e.rs, crates/gpu-gates/src/q38_fixture.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 b4e0238 into midagedev:main Oct 30, 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