perf(ds4): paged hybrid steps overlap the two GPUs through a shared MoE overlap step

Merged
DA
24d ago
+250-18

Summary

On a Radeon AI PRO R9700 + Ryzen AI Max+ PRO 495, DeepSeek V4.1 Flash splits each MoE layer between two GPUs. The R9700 runs attention, the dense layers, the shared expert and the hot routed experts. The Radeon 8065S iGPU runs the cold routed experts.

In the multi-user (paged) step the two GPUs took turns inside every layer. With this PR they work at the same time, and each extra row needs fewer launches. Outputs are bit-identical.

beforeafter
paged step time13 to 18% lower (1 to 4 lanes, 8 rows)
server decode, per request+16% alone, +20% 2 at once, +23% 4 at once

Based on main, with #966's split stack and #981's paged lane isolation in. All numbers below compare against main on the same box (measured as main with #966, with #981 merged in; the PRs that landed after it change no file the V4.1 paged step uses).

The overlap rules live in common code (common/moe_overlap.h, header only), written for any MoE layer split between two GPUs of one runtime. This PR uses them for DeepSeek V4.1's paged steps. Moving Qwen3.8-Flash-Next onto them is a follow-up.

Why the GPUs took turns

  1. The join waited at the start of its split. ggml's scheduler copies a split's inputs from other devices before any node of the split runs. In the paged step, the R9700 split that follows the routes held three things: the hot experts, the shared expert and the join. The join reads the iGPU's sum. So the R9700 waited for the iGPU's whole branch before it ran its own experts.
  2. Unplaced per-token copies (two or more rows). With two or more rows, ROCmFP2 experts are computed token by token. The per-token copies of the input, routes and weights were not assigned to an owner, so the scheduler gave each copy the backend of its neighbour in graph order. The hot owner's first copy comes right after the cold owner's last node, so it ran on the iGPU. The R9700's experts again waited for the whole cold branch.

What changed

  • common/moe_overlap.h (new, header only, model independent). The rules for overlapping the two owners of a MoE layer:
    • queue the expert GPU's routed sum as soon as the routes exist (moe_overlap_queue_expert);
    • join the two F32 sums with a plain add (moe_overlap_join);
    • mark each join so the scheduler ends the main GPU's split right before it, and only there (moe_overlap_mark_join, the scheduler's existing per-node late cross-input split).
  • common/moe_hybrid_ffn_eval.
    • New input MoeHybridGraphInputs::overlap_owners. When it is set, build_moe_owner_join queues three things in order: the secondary owner's sum, then the main owner's routed experts and shared expert, then the join. The shared expert's nodes are placed on the main owner.
    • Token-by-token owners (build_batched_routed_graph): a token's input, routes and weights are column views of the batch, so no copy per token. The secondary owner gathers the input in one copy per layer; the main owner reads it in place. Any copy still needed is placed on its owner.
  • deepseek4/deepseek4_fused_verify.inc. Sets overlap_owners for paged steps whose two owners are GPUs of one runtime, without a device join, a cluster layer or a streamed layer, and marks their joins for the scheduler. One user, and every other path, keeps its graph.
  • common/paged_attention_config.h. LUCE_MOE_Q8_HANDOFF joins the switches paged steps leave out (a profile default is dropped, an explicit setting refused). It acts only on graphs built with a schedule graph, which paged steps now are, and paged mode was never qualified with it.

No new environment variables. The settings refused for paged mode stay refused.

Why it is exact

  • Scheduling only. The overlap changes the order in which nodes are queued and where the scheduler ends a split. Every node runs the same kernel on the same bytes.
  • Same bytes into the same kernels. The column views feed the expert kernels exactly the bytes the per-token copies held. Gathering the input in one copy moves the same values.
  • Same join. The join is the same ggml_add of the same two F32 sums.

Evidence (this branch against main, same box, graphs-OFF builds):

  • Slot logits, bit for bit. 142 of 142 logits rows are identical. The probe covers:

    • 1, 2, 3 and 4 lanes, in every lane order;
    • 8 packed rows (4 lanes x 2);
    • both phases.

    In each build, every (phase, prompt, position) point has one bit pattern whatever the lane count, lane order or row packing: 28 points in 142 runs.

  • test_ds4_paged_exact_model (every case passes):

    • PASS: prompts arriving while others decode (prompt@slot: 4@0 0@1 5@2 2@3)
    • PASS: 23 sequences, 64 greedy tokens each, identical alone and beside others
    • and the one- to six-lane slot and order cases, all PASS.
  • Server greedy texts are identical: alone, 2 at once and 4 at once, before and after (code, prose, a 2K-token document, SQL).

Results

Paged step time

Median of 5 runs, ms per step.

rows in the stepbeforeafterchange
1 lane44.2738.34-13.4%
2 lanes65.5554.84-16.3%
3 lanes82.5767.84-17.8%
4 lanes100.7282.47-18.1%
4 lanes x 2 rows = 8 rows193.43160.80-16.9%

Scheduler splits per step (LUCE_DS4_TP_SPLIT_COUNT): before 161 at every width; after 201 at 1 row and 281 at 2 to 8 rows. Turning on the late split for the whole step instead (the scheduler's global flag, with the existing LUCE_DS4_TP_LATE_JOIN_SPLIT=1 on the same build, measured before main's split stack was merged) gives 281 and 321 splits and a step no faster (slower at 1, 2, 3 lanes and 8 rows, within 0.2 ms at 4 lanes), so only the joins are marked.

Server decode, paged

Manage launch flags: --profile ds41-r9700-gorgon --paged-attention --max-concurrency 4 --kv-pool-tokens 131072 --ds4-prefill sparse --max-ctx 131072, no drafter. Decode tok/s per request, median.

beforeafterchange
alone23.5727.33+16.0%
2 at once16.4119.77+20.5% (total 28.5 -> 34.3)
4 at once9.5811.79+23.1% (total 25.4 -> 30.1)

The 4-at-once total includes a 2K-token prefill in each round.

Both GPUs busy

Kernel trace (rocprofv3, 3 runs), share of the step with both GPUs running kernels, and with neither. Measured on this branch and its base before main's split stack was merged in (the paged step graph of this PR is the same).

rows in the stepbefore: both busyafter: both busybefore: neither busyafter: neither busy
1 lane0.3%11.5%24.9%23.5%
4 lanes0.9%13.9%24.8%19.5%
8 rows0.9%12.7%24.6%19.2%

Other changes in the same trace:

  • While the iGPU runs a layer's cold experts at 4 lanes, the R9700 is now busy 0.36 ms of that time (was 0.03 ms).
  • Launches per extra row fell from 1005 to 791 on the R9700 and from 500 to 336 on the iGPU.

What it does not change

One user with the drafter. overlap_owners needs a paged step, and the one-user profile also uses the device join. The token-by-token path runs only for two or more rows without grouped products, and the one-user profile runs grouped products. So the one-user graph is the graph on main. Measured, two interleaved passes (before, after, before, after), decode tok/s:

pass 1 beforepass 1 afterpass 2 beforepass 2 after
code57.8557.6757.8057.68
prose44.9444.9845.2145.20
2K document49.5849.6050.1249.61

Every difference is within the spread of the before runs themselves (49.58 and 50.12 for the 2K document). Texts are identical in all four runs.

Tests

Graphs-OFF build (this engine's product build):

  • New test_moe_hybrid_overlap_owners_order_and_placement (in test_server_unit, no GPU): a 2-row layer with token-by-token ROCmFP2 owners and a shared expert, built with overlap_owners:
    • every node reachable from the output has an owner or reads a weight;
    • in graph order, every secondary-owner node comes before every main-owner node, and the join comes last;
    • the input crosses to the secondary owner in one copy, the main owner reads it in place, and no owner copies per token.
  • test_feature_gate: LUCE_MOE_Q8_HANDOFF is refused for paged mode. Passed: 5 Failed: 0, exit 0.
  • test_server_unit: Passed: 1277 Failed: 0 Skipped: 4, exit 0.
  • test_deepseek4_unit and test_ds4_moe_combine_cuda (unchanged files, run as a regression check): exit 0; every combine case PASS (top_k 4 and 6, 1 to 401 tokens).

Graphs-ON build (Flash-Next's product build, same common code): luce_server compiles.

The logits probe above needs test-only engine hooks (forced rows per slot, a logits dump), so it stays out of the tree.

Known limits and follow-ups

  • The weight read in the join wait, for paged steps. Main now reads the main owner's next weights while it waits for the secondary owner's sum (ggml_moe_fused_prefetch, with the Engram rows due in the same wait), but only in the device-join branch, which paged steps do not take. Paged overlap steps would take it with the same two calls in the overlap branch (queue the wait work, then the read). That is a follow-up with its own measurement: on an earlier build that had both, the read was worth 0.6 to 1.2 ms per paged step. The copy tail from the same PR is set by Flash-Next's split scheduler only.
  • Flash-Next on the same rules. Qwen3.8-Flash-Next keeps its own copy of these rules in qwen4exp_graph.cpp (moe_join, the late-split setup in its split scheduler, and the routed sum it queues right after its routes). Moving it onto moe_overlap.h is a follow-up.
  • Grouped cold-expert products. The largest remaining per-row cost is the iGPU's cold experts, about +9 ms per extra row, bound by reading expert weights from memory. Grouped products would read each expert once per step instead of once per token. They are refused in paged mode after an unresolved host hang, so reopening them is a separate decision. The new unit test checks the token-by-token path; grouped products would need their own check.
  • Verified up to 8 rows per step. Exactness is verified up to 8 rows per step, the same limit as the paged lane isolation (#981).

🤖 Generated with Claude Code