Skip to content

qwen38: route a whole ubatch in one run so each routed expert is read once - #740

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prefill-copy
Oct 23, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prefill-copy

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8의 4K prompt prefill에서 routed expert의 weight를 ubatch마다 두 번 읽던 것을 가능한 경우 한 번만 읽게 해서 속도를 올리는 작업입니다.

Part of #565.

What and why

Qwen3.8 runs a 4,096-token ubatch through its routed experts in two runs of 2,048 tokens, so every expert's weights are read twice per ubatch. This PR lets the placement plan give the card route a run of the whole ubatch, so each expert is read once.

The plan widens the run only when nothing else has to move for it. It plans the machine a second time with the extra run buffer held as a named reserve on the stage card: 2,048 more tokens at 170,360 B a token, which is 348,897,280 B. If both plans place every tensor the same way, the reserve stays and the load runs the whole ubatch. If the card is too full, or the second plan does not fit, the run stays at 2,048 tokens and the plan is the one main makes. That keeps small-card and mixed placements as they were.

  • qwen35moe/place.rs: widen_card_route does the second plan and the comparison, tensor_places lists where every row piece sits (device, format, expert list), and card_route_run reads the run width and arena bytes back from the reserve at load, naming a malformed reserve as PlaceError::RouteUnitReserve. PlanInputs::plan_target is the one place that picks the plain plan or the MTP target plan; tier_experts and the serve seat both use it. reserve_rows is shared with check_draft_reserve.
  • body38.rs, wide38.rs, qwen38_open.rs: open_placed takes the run width from card_route_run and hands it to Wide38::new and CardRoute38::new.
  • serve_seats/qwen38.rs and qwen38_place.rs: the serve seat and the shared gate plan call the widening. Other gates keep the 2,048 run. Both serve planning paths and gate_qwen38_serve.rs now take the plain or MTP plan from plan_target instead of choosing it themselves.
  • gemm/mod.rs: GEMM_MAX_SLOTS goes from 36,864 to 40,960, so a run of 4,096 tokens at ten slots a token fits one route table.
  • scratch38.rs, ubatch.rs: a new const assert ties the default ubatch at ten slots a token to GEMM_MAX_SLOTS, and the qwen3moe assert becomes <= because its nine slots a token now fit inside the cap.
  • gate_gemm.rs: the route_table case gains a Qwen3.8 stack of 512 experts at ten slots a token up to 40,960 slots, and the q38 cases run up to 4,096 tokens, which removes Q38_TOKENS and run_case_at. gate_qwen4exp_e2e.rs compares a whole-ubatch run against the split walk bit for bit on the fixture plan.
  • tools/model-tax.tsv: the gate_gemm.rs qwen count drops from 32 to 30.

Release notes: Changed: Qwen3.8 prompts of 4,096 tokens read each routed expert once per ubatch when the plan has room, which prefilled FP4-Local 11.8% to 18.7% faster on one DGX Spark in one sitting.

Code shape

  • No second copy. The plain-or-MTP target plan choice and the reserve lookup each had two copies in the placement code; each is now one function (plan_target, reserve_rows). CARD_ROUTE_ROWS still mirrors the GPU run length because the model crate cannot read the GPU crate, and its value is unchanged.
  • Model code holds only architecture facts. No common owner gained a per-model branch, flag or method. The widening rule lives in the Qwen3.8 placement file.
  • Dead code is gone. Q38_TOKENS and run_case_at are removed.
  • No silent failure. A reserve with the wrong size, a second reserve line, or a reserve on a one-run ubatch is refused by name. The const asserts stop the build when a run's slots go over the cap.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or pin moved. The model-tax.tsv count went down.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": the two copies named above were removed. The helper-wrapper shape of plan_target also appears in deepseek41/place.rs and place_hybrid.rs, where each plan wraps only its own plan_with_slots; plan_target additionally chooses the MTP target plan, so I left those alone.

What I ran

Speed A/B

One DGX Spark, clock cap lifted around each arm, one server load per arm, arms alternating main and head, draft off, one request at a time, greedy. Each arm sends two prompts of 4,096 tokens with n_predict 128. Server: bloomery-serve -m <model folder> --host 127.0.0.1 --port 18671 --parallel 4 --ctx-size 16896 with BLOOMERY_DRAFT=off and BLOOMERY_HOST_ROOM=96G, built for sm_121. Main is upstream 648dd751.

Metric (one GB10, C@1, draft off) main head change
FP4-Local b9ce18a6 prefill tok/s, 4096-id prompt 1079.0 1280.3 +18.7%
NVFP4 fc694b54 prefill tok/s, 4096-id prompt 1134.7 1175.7 +3.6%
FP4-Local decode tok/s 30.22 30.10 -0.4%
NVFP4 decode tok/s 22.41 22.05 -1.6%

Delta (head against main): FP4-Local prefill +18.7% (1079.0 to 1280.3 tok/s), NVFP4 prefill +3.6% (1134.7 to 1175.7 tok/s).

Each arm is one load with two requests, so these are single observations with no significance claim. The first FP4-Local request on main prefilled at 995.1 tok/s, a warm-up value. On main, the second FP4-Local request prefilled at 1162.9 tok/s. On the head, it prefilled at 1300.3 tok/s. For NVFP4, the second request prefilled at 1142.3 tok/s on main. On the head it prefilled at 1162.9 tok/s, so the NVFP4 gain may lie inside the spread between requests. Decode is not touched by the change, and the two decode differences above are within what one pair of requests shows.

In every arm the plan puts all 24,576 routed experts on the card, so the widened run costs no expert placement.

Output is unchanged: the greedy output of the head equals main's on both 128-token requests of each checkpoint.

Mixed placement on an RTX 5090 machine

Placements that split experts between the card and the host are the ones a widened run could have hurt, so I compared main (upstream 1287fad5) and the head on the Qwen3.8 UD-Q4_K_XL GGUF (unsloth Qwen3.8-Flash-Next-GGUF at 766911a6), run order main, head, main, head, one load each with two requests. Server: bloomery-serve-qwen38 --host 127.0.0.1 --port 0 --place gate --ctx-size 4096 with BLOOMERY_DRAFT=off BLOOMERY_RESIDENCY=off, a 2,701-token prompt, n_predict 64, temperature 0.

The plan is the same on both: 4,439 card experts and 20,137 host experts. Resident bytes are 23,152,430,044 in all four loads, and the greedy tokens are equal across arms. Medians over the four requests of each arm:

Arm (RTX 5090, mixed placement, median of 4 requests) decode tok/s prompt tok/s
main 34.81 474.1
head 35.05 473.4

This card is full of experts, so the widening does not apply and the run stays at main's 2,048 tokens.

Tests, FAIL-first and gates

The placement unit tests pass on macOS, 27 passed. The new tests fail on the defect first: I broke the rule five ways, one at a time (dropped the comparison of the widened plan, dropped the stage-card experts condition, dropped the tier deduction, dropped the guard on the reserve size, compared only expert counts instead of every tensor place), and the new test failed each time:

test result: ok. 26 passed; 0 failed; 0 ignored; 0 measured; 442 filtered out; finished in 0.39s
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 467 filtered out; finished in 0.01s
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 467 filtered out; finished in 0.02s
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 467 filtered out; finished in 0.00s
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 467 filtered out; finished in 0.41s
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 467 filtered out; finished in 0.37s

The first line is the unbroken tree; the next five are the five breakages in the order listed. On an RTX 5090 machine, the whole-workspace cargo check --all-targets passes with rc 0, and the fixture budget gate passes:

BLOOMERY_BOX_LOCAL=1 just gate-fixture-budget
qwen38: budget 9482272768, experts a layer [256, 256, 256, 256]
FIXTURE-BUDGET rc=0

Hardware

The A/B ran on one DGX Spark and the mixed placement and gate runs on an RTX 5090 machine. GPU, driver and CUDA lines from nvidia-smi of the same jobs:

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

The DGX Spark container reports CUDA 13.3.1. Models: Qwen3.8 FP4-Local (b9ce18a6) and NVFP4 (fc694b54) for the A/B, and the GGUF above for the mixed placement.

What I ran into

None of this blocks the PR.

  • The A/B and the RTX 5090 runs were on an earlier build of this change. The PR head is that build rebased onto current upstream, with two compile-time asserts in ubatch.rs and scratch38.rs and some comment wording added. The executed code of the change is the same, and I did not rerun the measurements on the PR head. The placement unit tests and the workspace cargo check also ran on macOS at a head with the same executed code (check rc 0, 27 tests passed); the gate and check runs on the RTX 5090 machine were on an earlier build.
  • gate_qwen4exp_e2e (the new whole-ubatch comparison on the fixture plan) did not run to the end here. On the RTX 5090 machine it got through the load and structure checks and stopped for want of the fixture oracle. On the DGX Spark machines it stopped at the recorded fixture budget check, because their copy of the Qwen3.8 fixture is older than the recorded budget. Main stops at the same budget check on those machines.
  • just gate-gpu-gemm with its new 40,960-slot cases was not rerun on this head.
  • GEMM_MAX_SLOTS is the clamp inside the gemm_route kernel, so that entry's PTX changes by one constant. I did not compare the cc 12.1 PTX pins.
  • A6000 and 3090 placements were not run on those cards. The plan on a full card is main's own, which the RTX 5090 mixed-placement run shows.

AI help

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

… once

The card route ran a 4,096-position ubatch in two runs of 2,048 tokens,
so each card expert's weights were read twice a ubatch and the routed
GEMMs launched twice a layer. A plan that has room for the wider run
now gives the card route one run of the whole ubatch: widen_card_route
plans the machine again with that run's extra bytes as a named
stage-card reserve (route_unit_bytes, 348,897,280 B at 4,096 positions)
and keeps the reserve only when that plan places every tensor where the
first one did. A card the experts fill, a unified pool whose PLE table
would leave the host, a host-routed plan and the tier's block route keep
runs of 2,048 tokens, so their plans do not move. The load reads its run
from the reserve (card_route_run) and refuses a reserve of other bytes
by name. GEMM_MAX_SLOTS rises to 40,960 so one route table takes a
4,096-token run at ten slots a token.
@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 tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels 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 failure or proven quality defect survived the checks.

Non-blocking

⚠ Complete the GPU verification: crates/gpu/src/gemm/mod.rs:82. The capacity increase changes both route kernels, while the expanded GEMM gate and whole-ubatch end-to-end gate have not completed. Attach passing just gate-gpu-gemm and just gate-gpu-qwen4exp-e2e output, including FAIL-first for the new whole-run clause, and compare PTX resource columns. The launch-count/geometry class requires the owning gates; a fixture-budget check does not exercise these kernels.

⚠ Identify the measured build: crates/model/src/arch/qwen35moe/place.rs:1288. The A/B tables identify base commits but leave the executed head unnamed; the body says it predates this head. Attach that commit hash, exact model paths, and the commands with their output so the speed claim can be reproduced. Keep the results separated by card.

Checked

Duplicates: 13 new named functions, types, constants and tests checked; no duplicate found. Dryer reported zero pairs in model; GPU and gate paths were searched manually.

Complexity: run_case 1→30 incorporates the existing run_case_at body; Body38::open_with 21→23 adds reserve validation; card_whole 30→31 adds the whole-run check. No complexity-only finding.

Silent failure: 9 candidate sites followed. Failed optional replanning retains the valid original plan; malformed reserves return RouteUnitReserve; gate probes turn errors into failed premises.

Tests: Static mutation checks: drop placement equality or reserve-size validation → placement assertions fail; discard MTP in plan_target → plan comparison fails; restore the old route clamp → expanded route-table checks fail; force the loaded run to 2,048 → whole_run fails. These checks were not executed here.

Gates: No band or tolerance relaxed. Route capacity and exercised token counts increase; no dated gate pin moved.

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

Notes

This was a static review. No builds or GPU gates were executed here. The attached evidence does not identify both executed commits for a completed GPU gate run.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/bin/gate_qwen38_serve.rs, crates/gpu-gates/src/bin/gate_qwen4exp_e2e.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 bc3a01a 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 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 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