fix(ds4): paged V4.1 lanes attend their own rows, whole routes at every width
What was wrong
On ds41-r9700-gorgon with --paged-attention, a sequence's output depended on things other than its own prompt. There were two faults.
- Paged lanes read the wrong raw K/V rows.
- The profile sets both fused F16 verify K/V flags (
LUCE_DS4_HYBRID_F16_VERIFY_KV=1,LUCE_DS4_F16_KV_LANES=1). Their branch inbuild_mla_attention_lane_corereads the raw K/V source from row 0. For a gathered (paged) lane that source is the flat pool of every slot's raw ring, while the lane's mask is laid out for its gathered rows: valid history in time order, padding to a multiple of 16, then the current row. - Where the branch was taken: V4.1's compress ratios are 0, 0, then 2 (layers 2 to 19) and 1 (layers 20 to 39). So it was taken at layers 0 and 1 on every row, and at ratio-2 layers only at position 0 (no compressed history and no row emitted yet). Ratio-1 layers emit a row every token and never took it.
- Slot 1 and up attended slot 0's rows instead of their own.
- Slot 0 was wrong even alone. Before its ring wraps, at a position p that is not a multiple of 16, its real current row sits in the masked padding and pool row ceil16(p) (stale data from an earlier user of the slot) is attended as the current row. After the wrap it reads the right rows in ring order rather than time order, which changes the summation order.
- The profile sets both fused F16 verify K/V flags (
- A row depended on how many rows shared its step.
- In the hybrid expert tier, a paged step with more than one row summed the six routed experts as 4 + 2. A one-row step sums them in one combine. The two orders round differently.
- This does not depend on the F16 flags.
Who is affected
- Fault 1: paged serving with both F16 K/V flags set. Among the shipped profiles that is
ds41-r9700-gorgononly. One paged request alone is affected too (slot 0, see above), not only requests that share steps. - Fault 2: every step of more than one row on a two-GPU paged setup whose GPUs share a runtime, at layers that stream no experts. On
ds41-r9700-gorgonevery expert is resident, so that is every layer.ds41-halo(R9700 + Strix Halo) is not affected with its shipped placement: every layer streams 52 or more experts (the budgets only demote experts, never promote them), and a layer with streamed experts already kept the six routes whole. A custom placement that leaves a layer fully resident would be affected there too. - Not affected: one request at a time with the drafter (the non-paged path takes neither branch), and single-GPU profiles.
Paged output on ds41-r9700-gorgon changes with this fix, including one request alone.
The fix
deepseek4_graph.cpp: a gathered lane no longer takes the fused F16 K/V branch (!gathered_history). It attends its own gathered rows, under the mask laid out for them.deepseek4_fused_verify.inc:- every paged step keeps the six routes in one owner graph, the combine a one-row step uses;
- paged steps never balance routes dynamically (
MoeHybridRouteBalance::Disabled). WithLUCE_MOE_TP_DYNAMIC_ROUTE_BALANCEset, steps of up to five rows would balance and a six-row step would not, so a row would again depend on its company; LUCE_DS4_TP_HEAD4_TAIL2=1still restores the 4 + 2 split for verify lanes, for bisection; paged steps ignore it.
paged_attention_config.h:LUCE_DS4_TP_NATIVE_ROUTE_WIDTHleaves the paged refused list (see the next section).
No new flags or environment variables.
Whole routes and the paged refused list
LUCE_DS4_TP_NATIVE_ROUTE_WIDTH selects exactly the six-route branch that paged steps now always take, and it was on the list of switches paged V4.1 refuses because "with them it has faulted the GPU". The docs do not say which switch of that list faulted. So the question is whether paged steps now run an expert kernel that a one-row step never runs. From the code, they do not:
- Both expert stacks are entirely ROCmFP2. In the V4.1 Flash 1017 ROCMFP2S file, all 40
ffn_gate_exps, 40ffn_up_expsand 40ffn_down_expstensors areQ2_0_ROCMFP2. The hybrid storage copies them into the primary and secondary stacks with their types unchanged (new_like_with_expert_count). - Each row runs as its own one-row chain. Paged serving refuses grouped MMVQ, so for a ROCmFP2 owner
moe_owner_branch_tokenwiseis true. At more than one row,build_batched_routed_graphthen runs gate, up, SwiGLU, down and the route sum per row at one token: the kernels a one-row step runs. - What does see the whole step: the owner route remap (a lookup and a multiply over rows x 6 routes), the per-row copies and the concat of the row outputs, and the owner join. The 4 + 2 form ran the same operations over the whole step on main, with 4 and 2 route columns.
- One-row steps are unchanged. The 4 + 2 split only ever applied above one row, so a lone paged sequence already ran the six-route branch on main.
- If the expert stacks were not ROCmFP2 (other V4.1 files), the owners would run
MUL_MAT_IDover the whole step with six routes. On main that kernel already ran over the whole paged step with four and two routes, and DSpark verify runs it with six.
With this PR, the switch changes nothing in a paged step, so it leaves the refused list (test_feature_gate pins that). The soak below runs the same branch under load.
Evidence
Regression test
server/test/unit/test_ds4_paged_exact_model.cpp builds on HIP as test_ds4_paged_exact_model, wired like test_ds4_paged_prefill_model: an optional full-model test, not registered in ordinary CI. It exits 77 (skip) without the model files, or when the visible GPUs are not the profile's.
test_ds4_paged_exact_model target.gguf placement.json router_bias.bin [hotness.csv]
The invariant it checks: with paged attention, a sequence's output depends only on its own prompt. It must not depend on the other sequences' text or number, on its slot, on the lane order, or on whether it shares a step.
How it runs.
- It starts the engine as the server does for this profile with paged attention and six slots (the paged cap): the profile's environment minus what paged V4.1 leaves out, the profile's device flags, and the expert-device environment. The server's helpers for that are local to
server_main.cpp, so the test restates them. - It decodes each of six prompts (code and prose, 29 to 813 tokens) alone in an idle engine.
- Then, in each case below, every sequence's first 64 greedy tokens must match its alone run. Each case asserts the slots it names.
Cases:
- one lane in slot 3, while the other slots hold different text;
- two lanes, slot 0 inside its first raw ring pass (the test checks the prompt length), in slot order;
- two lanes, slot 0 past its first pass, in reversed order;
- four lanes, in slot order and in reversed order;
- six lanes, in mixed order;
- prompts arriving while others decode.
The engine API returns tokens, not logits, so the test checks greedy tokens.
Results on main 0b378da: R9700 + Ryzen AI Max+ PRO 495, V4.1 Flash 1017 ROCmFP2. Each run takes about 3.5 minutes, model load included.
| tree | result |
|---|---|
| main | FAIL: 21 of 23 sequences differ, in all 7 cases; first differences at tokens 2 to 29 |
| main + F16 K/V hunk only | FAIL: 12 of 23 differ, in the 6 cases where sequences share steps; the lone sequence in slot 3 passes |
| main + whole-routes hunk only | FAIL: 18 of 23 differ, in all 7 cases, from token 2 |
| main + both hunks | PASS: 23 of 23 identical |
| main + this PR (final, with the slot assertions) | PASS: 23 of 23 identical, every case in the slots it names |
- Each hunk alone leaves the test failing, so the test pins both.
- On main, the alone references are themselves wrong for slot 0 (fault 1). So "differs from alone" there means both sides were computed by the faulty code.
Build. These runs used GGML_HIP_GRAPHS=OFF, which is how the product builds this engine. Dockerfile.rocm does not set the flag, so the CMake default (OFF, server/CMakeLists.txt) applies. With graph replay compiled in, the same ggml graph is built and its kernels are replayed rather than launched one by one; the fix changes which graph is built, not how it is launched. A graphs-ON run was not made.
Server (main vs main + this PR)
Setup.
- Flags: the launcher's paged flags,
--profile ds41-r9700-gorgon --paged-attention --max-concurrency 4 --kv-pool-tokens 131072 --ds4-prefill sparse --max-ctx 131072 --kv-cache-dir off, no drafter. - Prompts: code, prose, SQL and a story (31 to 48 prompt tokens), each with a unique salt, 200 greedy tokens. Each prompt ran alone, in pairs and four at once, in 2 passes with the slot order rotated.
- Both builds ran on the same salts, so the same prompts.
| main | main + this PR | |
|---|---|---|
| alone, decode tok/s | 24.1 | 24.2 |
| 2 at once, decode tok/s per request (sum) | 13.6 (27.2) | 16.4 (32.8) |
| 4 at once, decode tok/s per request (sum) | 8.0 (32.1) | 10.2 (40.7) |
| greedy text beside 1 other equals alone | 0 of 8 | 8 of 8 |
| greedy text beside 3 others equals alone | 0 of 8 | 8 of 8 |
- None of the 24 texts (alone, in pairs, four at once) is identical between main and this PR. That includes each request alone: a lone request runs in slot 0, and its short prompt decodes through the slot's first ring pass, where on main every position that is not a multiple of 16 attended a stale pool row instead of its own current row (fault 1).
- Shared steps are faster with the fix: 21% per request for pairs, 27% for four at once. Alone is unchanged.
Soak
Main + this PR, the same flags, four clients at once for 30 minutes, each sending one salted greedy request after another (eight tasks, 128, 256 or 384 tokens). This runs the six-route branch at up to four rows per step for the whole run.
- Duration 1,817 s. 260 requests, 62,018 generated tokens (roughly 15,500 steps of up to four decode rows), 0 errors.
- Throughput was steady: 33.4, 34.3 and 34.2 tok/s across the four clients in the three 10-minute thirds (34.1 overall).
- Kernel log, from the end of the test run through the soak: no amdgpu, kfd or hung-task line (the only kernel lines were 5 unrelated audit notes). The server log has no error, assert or failure line.
- Memory: at load the R9700 had 26.94 of 31.86 GiB in use (4.92 GiB free, 1.50 GiB of it reserved headroom). The Gorgon Halo holds the 146.67 GiB secondary expert stack within its 165.81 GiB budget. Memory was not sampled during the soak; there was no allocation failure.
Known limit
Batch-invariant dense products hold to 8 columns. Hybrid paged steps hold at most 6 rows (one per sequence, at most 6 sequences), so they stay inside that limit.
🤖 Generated with Claude Code