Skip to content

gpu: fold Qwen3.8's prompt card sum into the combine on layers without a host leg - #701

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-fusion
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-fusion

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 프롬프트 처리에서 호스트 몫이 없는 층은 카드 합산과 shared expert 결합을 kernel 하나로 합쳐, 4K 프롬프트마다 kernel launch 48개와 memset 48개가 줄고 프롬프트 처리가 조금 빨라집니다.

Part of #565.

What and why

On a layer without a host expert leg, the Qwen3.8 prompt walk summed the card slots and combined them with the shared expert in three steps: a memset that zeroed the host sum, one ds41_ffn_card_acc_10 launch per route run that wrote the card sum, and q38_card_shared_add, which read that sum back. On a GB10 every layer of both checkpoints takes this path, because the plan puts all experts on the card (host_experts=0 card_experts=24576).

This PR adds one kernel, q38_card_sum_shared_add, that sums the card slots and does the combine in a single launch per route run. The acc write and re-read and the host-sum memset are gone. The combine starts from +0.0, as the zeroed host sum did, so a -0.0 card sum still becomes +0.0 and the block output keeps the bits it has on main.

  • crates/gpu/src/q38.rs: the new kernel and its launcher. The slot-order sum is still card_acc_at, now pub(crate). The combine arithmetic and its fault check are one function, combine, which q38_card_shared_add, q38_card_tier_shared_add and the new kernel call; the two existing kernels carried their own copy of it before.
  • crates/gpu/src/arch/qwen3moe/wide38.rs: CardSum says where a route run's sum goes. Tier layers (Join) and layers with a host leg (Acc) are unchanged; layers without one (Block) take the fused launch. The shared expert now runs before the card route on both paths, so its rows exist when the route reads them. Decode (program38.rs) is not touched.
  • tools/ref/ptx-shapes*.tsv and tools/unsafe-ratchet.txt: the new entry is pinned 0 0 in all four PTX tables, and the unsafe count for the gpu crate goes from 2248 to 2254 because the new kernel and the per-run views add six blocks, each with its SAFETY line and a dated PIN.

On the 4K prompt this removes 48 launches and 48 memsets and 2,012,774,400 bytes of memset (48 layers x 4,095 tokens x 2,560 values x 4 B [derived]).

Release notes: Changed: Qwen3.8's prompt pass sums the card experts and adds the shared expert in one kernel on layers without a host leg, which removes 48 launches and 48 memsets from a 4K prompt.

Code shape

  • No second copy. combine is the one owner of the combine arithmetic for the two existing kernels and the new one. The slot-order sum stays in card_acc_at.
  • Model code holds only architecture facts. The change takes its sizes from the route run and the layer's plan; it adds no per-model flag to a common owner.
  • Dead code is gone. Nothing is left unused: the host-sum memset and the acc view on a layer without a host leg are not issued, and the other layers still use them.
  • No silent failure. The launcher refuses no token, a size past one launch's grid and a short buffer by name; a stray place raises ExpertId and NaNs its token, a non-finite value raises F32Product.
  • Comments state what is true now.
  • No relaxed gate. No band or tolerance moves. The PTX rows and the unsafe count carry the PIN(2026-10-23): reason.
  • cargo fmt is clean on what I touched.

Duplication found or removed (path:line): the combine lines that q38_card_shared_add and q38_card_tier_shared_add each carried are now combine. The scan's two other hits (crates/gpu/src/q38.rs:551 against crates/gpu/src/card_slots.rs:220) share only the kernel entry boilerplate; the new kernel calls card_acc_at for the sum.

What I ran

Speed

Kernel profile of one 4,096-token prompt pass, and a device-event timing of one 4,096-token layer without a host leg, both on a GB10 and both main against this branch.

Measurement Main Head Change
Prompt pass wall, ms, kernel profile 5021.738 4967.814 -53.924 (-1.07%)
Kernel launches in the pass 14,592 14,544 -48
Memsets in the pass 123 75 -48
Memset bytes in the pass 2,482,937,860 470,163,460 -2,012,774,400
One 4,096-token layer, median ms, device events 2.7963 2.0167 -0.7796

Delta (head minus main): prompt pass -53.924 ms (-1.07%) under the profile, one layer -0.7796 ms.

The profile ran Qwen3.8-Flash-Next-FP4-Local with the clock cap on, one load per arm and one uncached 4,096-id code prompt. The layer timing builds one layer in two 2,048-token route runs over 512 card experts and 2,560 values with seeded random rows. It runs the main way (the host-sum memset, the card sum, q38_card_shared_add) and the new way (the fused launch per run), with the clock cap lifted. The two outputs are bit-identical over 10,485,760 values. It takes five rounds of 20 timed layers per arm, main first in each round, and the table shows the median. Over 48 layers the layer saving is 37.4 ms [derived], which matches the profile's change for the same work (the card sum, the combine and the memsets together fall from 139.273 ms to 103.088 ms, -36.2 ms [derived]). Of the 53.9 ms the profile saves, 12.0 ms [derived] is idle time between launches, and each profile arm is one capture.

Server A/B

I ran a server A/B on both checkpoints as well: one load per arm, then three uncached 4,096-token prompts with 128 greedy tokens, one client at a time, draft off, 4,224-token context, no cache RAM, clock cap lifted. Server prompt time, median of three:

Checkpoint Loaded first Main, ms Head, ms Head minus main, ms
FP4-Local main 4711.155 4545.544 -165.611
NVFP4 head 4494.909 4560.037 +65.128

The two checkpoints disagree in sign. The arm that loaded second was faster on both checkpoints by more than the change is worth, and requests inside one load moved by up to 158 ms [derived], while the expected saving is about 1% of a 4.6 s pass. I make no end-to-end claim from this A/B. The kernel profile and the layer timing above are the measured terms. Load time and client decode rate did not change.

Output

On both checkpoints, 64 greedy ids, the text and every token's top-100 log probabilities after an uncached 4K prompt are byte-identical between main and head: greedy ids, text and top-100 log probabilities byte-identical.

GPU evidence

Card, driver and CUDA, from nvidia-smi in the layer-timing run:

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

The profile ran with the clock cap on. The layer timing ran with the cap lifted, at 2424 MHz and 52 C by the clock query taken right after the timing, and the cap was restored afterwards:

2026/10/23 02:43:55.627, 2424 MHz, [N/A], 52, 23.72 W

The gate that covers the changed kernel is just gate-gpu-kquant. Its clause 10 gets a new check, q38_card_hostless. Over the leg's real down rows with the host slots NaN, the fused launch equals runtime::combine::combine over a zero host sum and the two old launches over zeros, bit for bit. A card sum of -0.0 and a -0.0 shared row give +0.0, a stray place raises ExpertId and F32Product and NaNs its token, and the launcher refuses no token and an output one value short. On the final head:

just gate-gpu-kquant
NVIDIA GB10, 595.84
q38_card_hostless[2 tokens x 10 slots, host slots NaN] is_runtime_combine_over_zero_hsum=true is_two_launches_over_zeros=true neg_zero_sum_bits=0x00000000 (want 0) fault="none" PASS
q38_card_host[no_token] want=Err(Shape q38::enqueue_card_sum_shared_add) got=Err: q38::enqueue_card_sum_shared_add: 2560 values of 0 tokens PASS
q38_card_host[an_output_a_value_short] want=Err(Shape q38::enqueue_card_sum_shared_add) got=Err: q38::enqueue_card_sum_shared_add: y.len() 5119 < 5120 PASS
PASSED: gate_kquant

FAIL-first. With the new kernel's combine starting from -0.0 instead of +0.0, the same check fails and the gate exits 1:

q38_card_hostless[2 tokens x 10 slots, host slots NaN] is_runtime_combine_over_zero_hsum=false is_two_launches_over_zeros=false neg_zero_sum_bits=0x80000000 (want 0) fault="none" FAIL first_mismatch=slot 0 row 3 got 0x80000000 want 0x00000000
FAIL_FIRST combine from -0.0 rc=1, tree restored

just gate-gpu-qwen38-twocard is not run: it needs two cards, which the test machine did not have. It covers q38_card_tier_shared_add, a kernel that now calls combine.

The PTX shape tables hold the new entry at 0 0, and no pinned entry changed. Comparing the two builds at sm_121a: 399 entries on main and 400 on the head, the one added entry is q38_card_sum_shared_add, and two are changed, q38_card_shared_add and q38_card_tier_shared_add, the two kernels that now call combine.

Reproduce

just gate-gpu-kquant
BLOOMERY_DRAFT=off bloomery-serve -m <Qwen3.8-Flash-Next-FP4-Local folder> --host 127.0.0.1 \
  --port <port> --parallel 1 --ctx-size 4224 --cache-ram 0

Run main's binary and this branch's in turn. Each request sends 4,096 token ids from a code corpus as a greedy POST /completion with cache_prompt off.

Hardware

A DGX Spark (NVIDIA GB10, driver 595.84, CUDA 13.3, sm_121a release builds). The clock cap was lifted for the layer timing and the server A/B, restored afterwards, and left on for the profile.

What I ran into

None of this blocks the PR.

  • The two changed PTX entries have equal registers and no spill (18 and 28 registers). After renumbering registers and labels, q38_card_tier_shared_add differs only in the layout of its finite test, and q38_card_shared_add in an unused register declaration and one swap of two independent lines. gate_kquant holds q38_card_shared_add bit for bit. The tier kernel's own gate needs two cards and was not run.
  • In the layer timing the CPU was 98.7 to 99.7% idle for the last 30 s before the run, but the one-minute load average only fell from 1.24 to 0.82 on that machine. One of the five rounds is slower than the rest (head 2.2215 ms, main 2.7526 ms, a saving of -0.5312 ms); the median is unaffected.
  • The profile and layer timing are single sittings on one DGX Spark. I did not time other prompt lengths, concurrency or TP=2.

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 23, 2026
github-actions[bot]
github-actions Bot previously approved these changes Oct 23, 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 remains in the changed code.

Non-blocking

⚠ Cover the later hostless route run: wide38.rs:1317. The new two-token check calls the launcher directly, so it cannot detect a lost c0 offset in either window. Extend the existing whole-card clause with a prompt longer than ROUTE_ROWS, comparing whole and cut walks. Replacing either offset with zero must fail; this keeps an indexing regression local to its cause.

⚠ Complete the measurement evidence: wide38.rs:1321. This is the “fold a launch into a neighbour” class. The body supplies GB10 gate output and timing tables, but neither measured commit hash nor the profiling and layer-timing commands. Add both hashes, the concrete model file identity, and the exact commands with output so the comparison can be reproduced.

Checked

Duplicates: 10 new definitions checked; no duplicated owner found. Dryer reported zero pairs in gpu; gpu-gates was searched manually. The new kernel reuses card_acc_at, and all three card combines reuse combine.

Complexity: check_hostless is new at 25, with equivalence and fault cases; CardRoute38::enqueue rises 38→44 through destination selection and bounds checks; check_q38_card rises 47→48 through one additional clause. No complexity-only finding.

Silent failure: 4 skip/length sites followed. Construction guarantees the route exists; the hostless route writes y; tier sums run at the join; the minimum buffer length feeds a named shape error.

Tests: mutation review of q38.rs:575 confirms that starting from -0.0 fails the bit assertions, as the supplied FAIL-first output shows. Dropping stray-place handling fails the NaN/fault assertions. Accepting zero tokens or a short output fails the refusal checks. The moved slot refusal remains checked.

Gates: no tolerance or existing PTX ceiling moved. The unsafe-count increase has a dated PIN and reason. The new PTX entry has zero spill/local ceilings.

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

Notes

Local validation was static; no runtime gate was run in this review. The body explains why the two-card gate was not run.

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

…t a host leg

On a layer the load runs without a host leg, the ubatch walk summed the card slots into the unit's acc (ds41_ffn_card_acc_10, one launch a route run), zeroed the host sum and combined both with the shared expert in the back (q38_card_shared_add). q38_card_sum_shared_add does the sum and the combine in one launch a route run, from +0.0 as the zero host sum did, so the block output keeps its bits; the shared expert runs before the route so its rows are ready. Per such layer this drops the acc write and re-read, the host-sum memset and its read, and one launch. The combine's arithmetic and fault check move to one helper that the three combining kernels call.

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

Verdict: CLEAN: The interdiff corrects the rebased unsafe pin to the exact source count and retains its dated reason.

Non-blocking

⚠ Cover the later hostless route run: wide38.rs:1317. The two-token check calls the launcher directly and cannot detect a lost c0 offset in either window. Extend the existing whole-card clause with a prompt longer than ROUTE_ROWS, comparing whole and cut walks. Replacing either offset with zero must fail; this keeps an indexing regression local to its cause.

⚠ Complete the measurement evidence: wide38.rs:1321. The “fold a launch into a neighbour” class requires its owning gate and timed A/B. The body supplies GB10 gate output and timing tables, but lacks both measured commit hashes and the profiling and layer-timing commands. Add both hashes, the concrete model file identity, and the exact commands with output so the comparison can be reproduced.

Checked

Duplicates: 0 new definitions in the interdiff. Dryer reports zero pairs in gpu; gpu-gates was outside its scan and is unchanged since the last review.

Complexity: diagnostics flag check_hostless (new → 25, equivalence and fault cases), CardRoute38::enqueue (38 → 44, destination branches and bounds checks), and check_q38_card (47 → 48, one clause). All are unchanged in the interdiff.

Silent failure: 0 new sites. The interdiff changes only the unsafe-count pin and its explanation.

Tests: no tests changed since the last review. The supplied earlier FAIL-first output shows that changing the combine's initial zero to -0.0 fails the bit checks.

Gates: independent recount matches base 2250 277 10 and head 2256 277 10 for gpu blocks/functions/implementations. The dated PIN accounts for four added blocks in the kernel and two in route views; it leaves no spare allowance.

Escape catalog: C1–C4 do not match the interdiff.

Notes

Validation was static; no runtime gate was run locally. The body explains why the two-card gate was not run. GPU evidence remains incomplete because the measured commit hashes are absent.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_kquant.rs, crates/gpu-gates/src/bin/shared/q38_combine.rs, crates/gpu/src/arch/qwen3moe/card38.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 de68ddb into midagedev:main Oct 23, 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