Skip to content

perf(ds41): 5-7x prefill and 1.7-2.1x decode on R9700 + Ryzen AI Max hybrid targets - #811

Merged
davide221 merged 6 commits into
mainfrom
feat/ds41-gorgon-speed
Oct 6, 2026
Merged

davide221 merged 6 commits into
mainfrom
feat/ds41-gorgon-speed

Conversation

@davide221

@davide221 davide221 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

DeepSeek V4.1 Flash on a Radeon AI PRO R9700 next to a Ryzen AI Max+ PRO 495 with 192 GB, every expert resident (hot experts and the dense work on the R9700, the rest on the iGPU). Prefill was host bound and decode fell with context; this makes prefill 4.8x faster at 2K and 7.5x at 15K-30K, and decode 1.7x faster at short context and 2.1x after a 30K prompt (decode now stays flat with context). Six commits, one per layer of the change plus the review fixes, each building and passing the tests on its own:

  1. perf(ggml-cuda): bit-exact RDNA4 Q8_0 and gfx1151 ROCmFP2 decode kernels
  2. perf(ds4): fused, bit-exact router and HC collapse ops
  3. perf(ds41): flat long-context decode on hybrid targets
  4. perf(ds41): heterogeneous layer-major prefill
  5. feat(ds41): drafter swap for long prompts and the ds41-gorgon profile
  6. fix(ds41): restore-exact pipeline bands and review fixes for the hybrid prefill

Defaults are unchanged outside the two bit-exact kernels (1): every other switch is off unless the new ds41-gorgon profile sets it.

Impact

Same box, same GGUF (ROCMFP2S), expert placement, router bias, DSpark drafter and 12 GB expert budget in both columns; greedy; fresh server per column.

Scenario main this PR gain
decode, code prompt (36 tokens) 29.1 tok/s 48.7 tok/s 1.67x
prefill, 2K prompt 155 tok/s 751 tok/s 4.8x
decode after the 2K prompt 18.8 tok/s 32.6 tok/s 1.73x
prefill, 15K prompt 138 tok/s 1033 tok/s 7.5x
prefill, 30.7K prompt 135 tok/s 1012 tok/s 7.5x
decode after the 30.7K prompt 14.5 tok/s 31.0 tok/s 2.1x

With the prefix cache, a six-turn agent session (each turn appends a source file) prefills its new tokens at 726-803 tok/s on turns of 4.9K-8.3K new tokens and 503 tok/s on a 1.6K-token turn.

What each commit moves (tok/s): (1) code decode 29.1 -> 34.7; (2) 34.7 -> 36.3; (3) 36.3 -> 47.4, decode after 2K 21.8 -> 29.9; (4) prefill 2K ~155 -> 751 and 15K ~140 -> 697; (5) 15K prefill 697 -> 1033 (drafter swap); (6) on a 14.1K / 25.4K-token prefill, 1026 / 1006 -> 985 / 1019 with per-chunk bands and the swap out of the profile.

Implementation

  • Decode kernels (1). mul_mat_vec_q8_0_rdna4 computes exactly the eight-wave Q8_0 MMVQ arithmetic with fewer waves per row and rows sharing activation loads; the gfx1151 ROCmFP2 MoE MMVQ issues several iterations' loads before the dot products; the HIP mmvf K loops are unrolled. Same terms in the same order, bit-identical. bench_ds41_q8_mmvq times the DS4.1 dense projections with the column-identity check.
  • Fused ops (2). GGML_OP_DS4_HC modes 4-6: the sqrt(softplus) router, biased top-k with the protected-expert rule and the normalised weights in two kernels instead of about ten, and the staggered HC collapse in one kernel. Both round exactly as the chains they replace; test_ds4_fused_ops_cuda checks ids, weights and collapse bit for bit.
  • Long-context decode (3). V4.1 decode and verify lanes are Explicit with a shared top-k selection, so they run the D=512 flash kernel; on a hybrid target the graph built an F16 [raw | compressed] concat and cast it to F32 every layer and step. F16 K/V lanes feed segmented F16 K/V straight to the split-KV kernel; short lanes take the split-KV schedule through an identity row list; one F16 mask per step; the visibility-bounds scan and the indexed-rows top-k get cheaper.
  • Prefill (4). The layer-major residual stays on the primary between layers and Engram applies there; both owners' partials join on the device; plain ROCmFP2 expert stacks take the grouped MUL_MAT_ID graph and the gfx1151 WMMA MoE kernel; RDNA4 runs the D512 WMMA attention kernels. A two-slot pipeline runs each band's secondary-owner FFN behind the other slot while the primary routes and runs the next band, the input crossing on a side stream (ggml_backend_cuda_copy_tensor_async_side) and the partial returning with an event (..._async_nowait). Each chunk (chunks start at every restore point) runs as two equal bands of at least 64 rows, so the bands depend on the chunk alone and two bands in flight hold one chunk's rows; a pipelined prefill that runs out of memory is retried once with the arenas retired and the pipeline off (same bands). Engram rows (2 x 101 GB of tables inside the GGUF, read from the drive) are fetched by a reader thread from the start of the pass, so the reads overlap the layers before each Engram layer. LUCE_DS41_DECODER_BOUNDED_REPLAY implements the decoder SWA bounded replay of the V4.1 report (rows before the last n_swa run only the layers up to the last KV source); a prompt that saves a prefix snapshot inside it is prefilled in two parts split at the snapshot, each with replay, as a later request restoring that snapshot runs them.
  • Drafter swap and profile (5). LUCE_DS4_DRAFT_SWAP keeps a pinned host copy of the DSpark drafter; a prefill longer than 10240 tokens frees the drafter's device memory for larger bands and restores it before decode (about 1.4 s over PCIe Gen3 x4, worth it from about 10K tokens); if the swap-in fails, that request decodes without DSpark. Since (6) ds41-gorgon leaves the swap off: with per-chunk bands it no longer speeds up prefill on this box. Single-sequence serving only (paged serving loads no drafter). The ds41-gorgon profile sets every switch above at its measured value; the expert placement and router bias come from the command line.

Runtime Flags / Configuration

Default-on paths:

  • The Q8_0 RDNA4 and gfx1151 ROCmFP2 MMVQ kernels and the mmvf unroll (bit-identical). LUCE_CUDA_MMVQ_Q8_RDNA4=0 and LUCE_CUDA_MMVQ_MOE_FP2_PREFETCH=0 turn them off.

Opt-in (all set by --profile ds41-gorgon):

  • Decode: LUCE_DS4_FUSE_ROUTER, LUCE_DS4_FUSE_COLLAPSE, LUCE_DS4_HYBRID_F16_VERIFY_KV, LUCE_DS4_F16_KV_LANES, LUCE_DS4_EXPLICIT_SPLIT, LUCE_DS4_MASK_F16_ONCE, GGML_CUDA_MLA_SPLIT_KV_COUNT (4/8/16).
  • Prefill: LUCE_DS41_DECODER_BOUNDED_REPLAY, LUCE_DS4_LAYER_MAJOR_DEVICE_RESIDUAL, LUCE_DS4_ENGRAM_DEVICE, LUCE_DS4_SPLIT_DEVICE_JOIN, LUCE_DS4_GROUPED_HYBRID_PREFILL, LUCE_ROCMFP2_WMMA_PREFILL, LUCE_DS4_PREFILL_PIPELINE, LUCE_DS4_PREFILL_ARENA_CHUNK_MB, LUCE_DS4_DEVICE_TOPK, LUCE_MOE_SIDE_COPY, LUCE_DS4_PREFILL_F16_KV_ALL.
  • LUCE_DS4_DRAFT_SWAP.

Not bit-exact relative to main: bounded replay (approximate by design, the model is trained for it) and the split-KV schedule for short lanes. The router and collapse fusions, the kernels in (1), the side copy and the Engram read-ahead are exact. A request that restores a snapshot reproduces the request that saved it, as on main. The opt-in LUCE_DS4_DRAFT_SWAP is the exception: a cache hit whose own prefill stays under 10K tokens keeps the drafter and runs smaller chunks than the swapped-out full prefill, so its answer can differ.

Validation

Check Result
Each commit builds (HIP gfx1151;gfx1201) and passes test_feature_gate (incl. the ds41-gorgon launch-policy case) and test_ds4_fused_ops_cuda pass
CUDA sm_86 build of the top (CI's libcuda stub setup) compiles and links
Defaults byte-identical to main (--profile ds41-lucebox): code, 2K and 30.7K outputs identical md5s
Paged batching (--paged-attention --max-concurrency 4 --ds4-prefill sparse): defaults identical to main; with every switch coherent, no errors pass
Engram read-ahead and the 128-row pipeline: code, 2K and 30.7K outputs vs the same branch without them identical md5s
Cache hit vs full prefill: a 21,965-token two-turn prompt, then the same turn restoring the snapshot the first one saved, on two fresh servers, ds41-gorgon identical answers (before (6): diverged at character 335)
Same, with LUCE_DS4_DRAFT_SWAP=1 (opt-in) differs, as described above
(6) unit tests: test_ds4_fused_ops_cuda on gfx1201 and gfx1151 (now also run by both GPU CI jobs, with a 70,000-token collapse case), test_server_unit, test_deepseek4_unit pass

Notes

  • A small agent turn still prefills below the long-turn rate: a turn whose snapshot sits inside its new tokens runs two passes, each paying the 128-row replay tail (about 0.5 s). Folding both into one pass with a snapshot taken inside it is the next step.
  • Formats are orthogonal to DS4.1: MXFP8 dense weights, exact IQ2_XXS/IQ3_XXS HIP decode, mixed-type streamed cache, batch-invariant DSpark verify #800 (MXFP8 dense, IQ2_XXS / IQ3_XXS experts): its nine commits cherry-pick cleanly on top of this branch. The prefill and decode orchestration here applies to those files too; the ROCmFP2 and Q8_0 kernels in (1) and the type-107 WMMA prefill do not, so an IQ-expert WMMA prefill path and a BF16 cache for MXFP8 prefill would be the follow-ups for that GGUF.

🤖 Generated with Claude Code

davide221 and others added 5 commits October 5, 2026 13:01
- mul_mat_vec_q8_0_rdna4: the one-column (and batch-invariant multi-column)
  Q8_0 MMVQ on RDNA4 computes exactly the eight-wave kernel's arithmetic
  (same per-lane FMA chains, partials added in virtual-wave order, same
  butterfly) with fewer waves per row and rows sharing activation loads.
  On by default on RDNA4; LUCE_CUDA_MMVQ_Q8_RDNA4=0 turns it off.
- ROCmFP2 MoE MMVQ on gfx1151: issue up to eight iterations' weight, scale
  and activation loads before the dot products; each lane adds the same
  terms in the same order. On by default on gfx1151;
  LUCE_CUDA_MMVQ_MOE_FP2_PREFETCH=0/1 overrides.
- mmvf: unroll the HIP K loops so loads of several trips are in flight;
  each column's FMA chain keeps its order.
- test/bench_ds41_q8_mmvq: the DS4.1 dense projections through the ggml
  dispatch at 1-4 columns, with the column-identity check.

Outputs are bit-identical to the existing kernels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two GGML_OP_DS4_HC modes replace short op chains the DS4 graph runs every
layer:
- ggml_ds4_router_select / ggml_ds4_router_weights (modes 4 and 5): the
  sqrt(softplus) router, biased top-k with the protected-expert rule, and
  the normalised weights in two kernels instead of about ten.
- ggml_ds4_hc_collapse (mode 6): the staggered HC collapse in one kernel
  instead of a mul, a transpose and a 4-wide sum_rows.
Both round exactly as the chains they replace (the collapse is the device
sum_rows order, (x0 + x2) + (x1 + x3), with contraction off).
test_ds4_fused_ops_cuda checks ids, weights and collapse bit for bit,
ties and protected experts included. LUCE_DS4_FUSE_ROUTER=1 and
LUCE_DS4_FUSE_COLLAPSE=1 turn them on.

DS4.1 on an R9700 + Ryzen AI Max+ PRO 495, output unchanged: decode 34.7 ->
36.3 tok/s on a code prompt, 20.4 -> 21.8 after a 2K prompt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
V4.1 decode and verify lanes are Explicit with a shared top-k selection,
so they run the D=512 flash kernel; on a hybrid target the graph built an
F16 [raw | compressed] concat and cast it to F32 every layer and step,
O(context) work that made decode fall from 29 to 10 tok/s at 64K.

- LUCE_DS4_HYBRID_F16_VERIFY_KV / LUCE_DS4_F16_KV_LANES: F16 K/V for
  hybrid verify and single-token lanes; selection lanes feed segmented F16
  K/V straight to the split-KV kernel (no concat, no cast).
- LUCE_DS4_EXPLICIT_SPLIT: short-context lanes take the split-KV schedule
  through an identity row list; LUCE_DS4_MASK_F16_ONCE: one F16 mask per
  step; GGML_CUDA_MLA_SPLIT_KV_COUNT (4/8/16) sets the split count.
- fattn: the visibility-bounds scan finds the last compressed row from the
  end; the indexed-rows top-k skips its sort for ascending input and counts
  valid rows with one block reduction (owner ranks now read the sorted
  rows).

DS4.1 on an R9700 + Ryzen AI Max+ PRO 495: decode 36.3 -> 47.4 tok/s on a
code prompt and 21.8 -> 29.9 after a 2K prompt; with the rest of the stack
decode stays at 30-31 tok/s after a 30.7K prompt (it fell to about 10 at
64K before).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Long prompts on a two-GPU hybrid target (dense work and hot experts on the
primary, the other experts on a secondary GPU, all resident) were host
bound: the residual crossed the host link every layer, plain ROCmFP2
experts ran one sliced matmul chain each, and the secondary GPU idled
through every round trip. All switches below are off by default.

- LUCE_DS4_LAYER_MAJOR_DEVICE_RESIDUAL, LUCE_DS4_ENGRAM_DEVICE: the
  layer-major residual stays on the primary between layers and Engram is
  applied there; LUCE_DS4_SPLIT_DEVICE_JOIN joins both owners' partials on
  the device.
- LUCE_DS4_GROUPED_HYBRID_PREFILL, LUCE_ROCMFP2_WMMA_PREFILL: plain ROCmFP2
  (type 107) owner stacks take the grouped MUL_MAT_ID graph and the gfx1151
  WMMA MoE kernel (32-route tiles, dead fragments skipped).
- LUCE_DS4_PREFILL_PIPELINE=N: a two-slot pipeline from 128 rows; each
  band's secondary owner runs behind the other slot's while the primary
  routes and runs the next band, its input crossing on a side stream into a per-slot staging
  tensor (ggml_backend_cuda_copy_tensor_async_side), its partial returning
  with an event the join waits on (..._async_nowait). Band rows are capped
  by free memory; an out-of-memory prefill is retried once with the
  prefill arenas retired.
- Engram rows (V4.1: 2 x 101 GB of tables in the GGUF, read from the drive)
  are fetched by a reader thread from the start of a layer-major pass, so
  the reads overlap the layers before each Engram layer.
- LUCE_DS4_DEVICE_TOPK: per-token top-k candidates from the device;
  LUCE_DS4_PREFILL_ARENA_CHUNK_MB=0: one grow-only attention arena.
- RDNA4 runs the D512 WMMA attention kernels when their switches are on,
  and the F16 prefill K/V transport covers dense flash prefill of any band
  size.
- LUCE_DS41_DECODER_BOUNDED_REPLAY: the decoder SWA bounded replay of the
  V4.1 report: rows before the last n_swa run only the layers up to the
  last KV source (layer 20). A prompt that saves a prefix snapshot inside
  it (an agent turn) is prefilled in two parts split at the snapshot, each
  with replay, as a later request restoring the snapshot would run them.
  Band sizes follow free device memory, so a restored prompt can differ
  from a cold one in the last bits, as batching does.

DS4.1 on an R9700 + Ryzen AI Max+ PRO 495, outputs unchanged on the bench
prompts: prefill 160 -> 751 tok/s on a 2K prompt and 144 -> 697 on a 15K
one. A six-turn agent session with prefix reuse prefills its new tokens at
726-803 tok/s on turns of 4.9K-8.3K new tokens and 503 on a 1.6K-token turn
(190-310 when snapshot requests turned replay off).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- LUCE_DS4_DRAFT_SWAP: keep a pinned host copy of the DSpark drafter; a
  prefill longer than 10240 tokens (the longer part of a split one) frees
  the drafter's device memory for it (the chunk is sized as if the drafter
  were absent) and restores it before decode (about 1.4 s over PCIe Gen3
  x4; below ~10K tokens that costs as much as it saves). Single-sequence
  serving only (paged serving loads no drafter).
- ds41-gorgon: DS4.1 on an R9700 beside a 192 GB Ryzen AI Max+ PRO 495 with
  every expert resident; every switch of this stack at its measured value.
  The expert placement and router bias come from the command line.

R9700 + Ryzen AI Max+ PRO 495 192 GB: the swap takes a 15K-token prefill
from 697 to 1026 tok/s. ds41-gorgon overall: prefill 724-751 / 1026-1033 /
1015 tok/s at 2K / 15K / 30.7K; decode 48-49 tok/s on code, 32-33 after a
2K prompt, 30 after 30.7K.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…id prefill

- Pipelined prefill: each caller band (a chunk started at every restore
  point) runs as max(N, 2) equal parts (deepseek4_pipeline_parts), instead
  of the whole pass cut into equal bands sized from free memory. The parts
  depend on the chunk alone, so a request restoring a snapshot reproduces
  the request that saved it, as on main; before, the restored answer
  differed. Two parts in flight hold one chunk's rows.
- The prefill retry applies to pipelined hybrid prefills only, and reruns
  the same bands with the pipeline off instead of smaller bands.
- A failed drafter swap-in decodes the request without DSpark instead of
  failing it; the next request tries again.
- V4.1 explicit verify lanes keep F16 K/V only under
  LUCE_DS4_EXPLICIT_SPLIT, so --ds4-fused-verify-f16-kv alone is unchanged.
- Bounded replay eligibility lives in one helper, and the prefill splits at
  a snapshot only when replay can run; replay is off when the drafter
  captures a layer at or below the cut.
- HC collapse: __fmul_rn products (nvcc ignores the clang contract pragma
  and builds with fast math) and a token stride past the 65535 grid limit.
- Router select/weights assert contiguous inputs of the right size.
- Peer side/nowait copies share one eligibility check: buffer devices must
  match their backends, and GGML_CUDA_NO_PEER_COPY disables them.
- RDNA4 Q8_0 kernel built for HIP only; MIX WMMA grid guard sized for
  32-route tiles; GGML_CUDA_MLA_SPLIT_KV_COUNT read per launch; device
  top-k keeps only normed/probs alive.
- test_ds4_fused_ops_cuda runs in both GPU CI jobs (plus a 70000-token
  collapse case); test_pipeline_parts_follow_each_band; the new switches
  are in docs/ENVIRONMENT.md.

- ds41-gorgon leaves LUCE_DS4_DRAFT_SWAP off: with these bands the swap
  no longer speeds up prefill, and a cache hit after a swapped-out prefill
  runs smaller chunks. It stays opt-in, documented in ENVIRONMENT.md.

R9700 + Ryzen AI Max+ PRO 495, ds41-gorgon, fresh servers: a 21965-token
two-turn prompt answered from the cache now matches the full prefill token
for token on two servers (before: diverged at character 335). Prefill
14.1K / 25.4K tokens: 985 / 1019 tok/s (before: 1026 / 1006 with the swap).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davide221
davide221 merged commit 7567771 into main Oct 6, 2026
8 checks passed
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 7, 2026
… into feat/qwen4exp-strix-halo

# Conflicts:
#	server/deps/llama.cpp/gguf-py/gguf/constants.py
@davide221
davide221 deleted the feat/ds41-gorgon-speed branch October 12, 2026 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant