Skip to content

Speed up DeepSeek V4 Flash prefill on ROCm gfx1151 (Strix Halo) - #1339

Open
felix920506 wants to merge 6 commits into
antirez:mainfrom
felix920506:upstream-gfx1151-prefill
Open

felix920506 wants to merge 6 commits into
antirez:mainfrom
felix920506:upstream-gfx1151-prefill

Conversation

@felix920506

Copy link
Copy Markdown

Prefill on Strix Halo is 58-73% faster from 2K to 16K context. Decode speed is unchanged, and official-continuation quality on the 0731 release GGUF is as good as main or slightly better.

The first five commits are standalone fixes found along the way, and each builds on its own:

  1. Compute the ROCm visual attention scale with host math. main does not build with ROCm 7.14, because HIP's rsqrtf is device-only.
  2. Make the paired ROCm Q8_0 matmul match the single kernel. An explicit FMA pins the -ffast-math reassociation. Without it, main fails the paired Q8_0 exactness check in ./ds4_test on ROCm.
  3. Split ROCm model weight copies at pinned registration edges. HIP rejects a host-to-device copy that crosses two pinned registrations.
  4. Link the ROCm kernel tests against the compat object. Before this, test-mxfp4-rocm, test-glm53-kda-rocm and test-glm-attention-rocm failed to link.
  5. Add an IQ2_XXS MMQ pair entry without the output sanitize pass. The ROCm SwiGLU consumer already drops non-finite values.

The sixth commit, Speed up DeepSeek V4 Flash prefill on ROCm gfx1151, is the speedup itself:

  • Projections on batches of 32 or more tokens run as hipBLASLt F16 GEMMs, with each class of shapes autotuned once.
  • One 32-head WMMA attention kernel handles every prefill chunk.
  • The Q2_K routed-down projection is a tiled F16 WMMA kernel. It runs only when the MMQ gate/up pair has written every routed mid row in F16. Every other gate/up route keeps the existing hot/cold split.
  • The indexer's weighted head sum stays in WMMA registers.
  • The rocBLAS F32 path, the custom WMMA row-tile kernels and the transposed F16 weight cache are removed.

The only change outside ROCm is in ds4.c, where ROCm now gets two things Metal already had:

  • the F16 staging buffer;
  • the F16 RMS-fold HC projection.

Metal and CUDA behavior is unchanged. cuda/mmq gains one entry point, ds4_mmq_iq2_xxs_moe_pair_unsanitized, which the CUDA paths do not call.

Test setup

  • Framework-class Strix Halo: Ryzen AI MAX+ 395 / Radeon 8060S (gfx1151), 128 GB.
  • ROCm 7.14, using the rocm/dev-ubuntu-24.04:7.14.0-full container.
  • Main model: DeepSeek-V4-Flash-IQ2XXS-w2Q2K-AProjQ8-SExpQ8-OutQ8-chat-v2-imatrix-0731.gguf, with DeepSeek-V4-Flash-DSpark-support-0731.gguf. Both are checked against their SHA-256 sums.
  • Second model: the Vision-Exp Q2 GGUF (text only), scored against its own matched fixtures.
  • Baseline: main (b3f5018) plus commit 1, because main alone does not build on ROCm.
  • Only one model process ran at a time, on an idle machine, with a MemAvailable - CmaFree watchdog that stops the run below 3 GiB.
    • The lowest usable memory during model runs was 7.9 GiB.
    • There was no OOM, GPU reset or watchdog stop.

Build gates

Check Result
make clean && make strix-halo builds, 0 warnings
ROCm test binaries and score_official build, 0 warnings
make cpu builds, 0 warnings
Each intermediate commit builds
git diff --check clean

Tests that need no model

All of these pass:

  • ./ds4_test --server, ./ds4-eval --self-test-extractors and ./ds4_agent_test
  • tests/test_layer_pack (97/97), tests/test_engine_mgpu_placement (114/114) and tests/test_gpu_args
  • tests/test_gpu_args_cli.sh (86 pass, 0 fail) and tests/test_prompt_prefix
  • q4k-dot-test (4/4), mxfp4-dot-test (4/4) and test-session-state
  • make test-linux-memory test-rocm-memory
  • make test-mxfp4-rocm: zero failures for mid and out at 1, 3, 32, 128 and 512 tokens
  • make test-glm53-kda-rocm test-glm-attention-rocm, also with DS4_ROCM_GLM_SELECTED_ATTN_HEAD_TILE=1
  • tests/test_deepseek41_rocm and tests/test_mmvq_rocm
  • ./ds4_test --metal-kernels, which runs the isolated GPU kernel checks on ROCm

Full ./ds4_test with the 0731 model

Command:

DS4_TEST_MODEL=<0731 q2> DS4_TEST_DSPARK=<0731 DSpark support> ./ds4_test
  • Branch: ds4 tests: ok. Every check passes.
    • Highlights: logprob-vectors, local-golden-vectors (top5 5/5, top20 20/20), metal-tensor-equivalence, dspark-verify-depth, the session tests, long-context and server.
    • Tests that need Metal, Qwen, GLM, MTP or SSD streaming are skipped.
  • Baseline: fails. The failure is the paired Q8_0 exactness check (mismatches=57/77), which commit 2 fixes.

Speed

On the 0731 model:

ds4-bench --rocm --prompt-file speed-bench/promessi_sposi.txt --ctx-start 2048 --ctx-max 16384 --step-incr 2048 --gen-tokens 64
Context Prefill before Prefill after Gen before Gen after
2048 (cold) 195.55 338.81 14.38 14.36
4096 266.65 422.38 14.24 14.22
8192 258.38 414.12 14.01 14.00
12288 252.16 411.24 13.87 13.86
16384 247.58 404.62 13.76 13.75

All numbers are tok/s. The Vision-Exp model gives the same numbers within 1%.

Quality

score_official on each GGUF, before and after the change:

Model / fixture Before After
0731: data/flash (100 cases, API logprobs) @4096 NLL 0.402278, first-token 55/100, API top-1 86.04% NLL 0.399036, first-token 54/100, API top-1 86.17%
Vision-Exp: flash-vision-exp-openrouter-novita-100 @4096 NLL 0.393933, first-token 60/100 NLL 0.394799, first-token 61/100
Vision-Exp: tests/test-vectors/flash-vision-exp, 5 cases (two are about 5K tokens) @8192 NLL 0.380246, 4/5 NLL 0.345902, 4/5
The same 5 cases with DS4_ROCM_MMQ_IQ2=0 (non-MMQ gate/up path) n/a NLL 0.358273, 4/5
  • On 0731, compare_scores.py gives an NLL change of -0.81%, and the branch wins 52 of the 100 cases.
  • With --quality, the logits are bit-identical to main (max-abs 0), so the full-FP32 reference path is untouched.

DSpark

On the 0731 model:

DS4_DSPARK_FIXTURE_TOKENS=64 sh tests/dspark_acceptance_fixture.sh

Both builds pass with errors=0 and no replays. Speeds are tok/s, DSpark first and ordinary decode second:

Case Baseline Branch
hello 12.99 / 15.89 13.32 / 15.90
redis 10.95 / 15.79 13.45 / 15.81
math 7.65 / 15.86 8.71 / 15.86
python_reverse 16.33 / 15.87 15.74 / 16.01
c_add 16.28 / 15.77 17.69 / 15.78

The fixture also passes with the Vision-Exp model and its support GGUF.

Known behavior

  • hipBLASLt timing varies between processes and engines. Algorithm autotune is timed per engine, so two runs can pick different algorithms for the same shape. That moves the full logits by about max-abs 1.
    • Top-1 and top-3 stayed the same in every check run on the 0731 model.
    • With the Vision-Exp model (not 0731), logprob-vectors and local-golden-vectors sit at their thresholds and flip between runs, and they flip on main too. Vision-Exp's own FP32 --quality reference agrees with this branch on the case that flips.
  • Batches under 32 tokens stay off hipBLASLt, and that includes DSpark verification.
    • Letting the attention output projections use hipBLASLt for these batches makes DSpark verification about 20% faster (c_add 12.9 → 18.4 tok/s).
    • But it also makes metal-tensor-equivalence fail on ROCm: two engines in one process pick different top-1 tokens on a 27-token prompt, even when they use the same tuned algorithms.
    • That change is left for a follow-up.
  • Not tested here:
    • CUDA and Metal builds. No hardware was available, and their code paths are unchanged apart from the new, unused cuda/mmq entry point.
    • SSD streaming on ROCm.
    • GLM, Qwen and DeepSeek 4.1 models. They share some of the touched ROCm files; their GLM attention, KDA and DS4.1 ROCm kernel tests pass.

🤖 Generated with Claude Code

hipcc rejects rsqrtf in host code (HIP's is device-only, unlike CUDA's),
so the ROCm build fails at ds4_rocm_deepseek4_vision.cuh. The value feeds
a host-side BLAS call, so use 1/sqrtf, which is defined on both.
-ffast-math reassociated the three-factor Q8_0 product differently in the
single-matrix and paired kernels, so ./ds4_test --metal-kernels saw ~1 ulp
disagreements. Pin the order with an explicit FMA in both.
HIP rejects a host-to-device copy whose source crosses two pinned host
registrations. Split those copies at registration edges, so a span that
covers the tail of one registered range and the head of the next still copies.
ds4_rocm.o needs symbols from ds4_rocm_compat.o, so make test-mxfp4-rocm
and the other ROCm kernel test binaries that linked only ds4_rocm.o and
the unavailable stubs failed to link.
The ROCm routed MoE consumes gate/up through moe_swiglu_weighted, which
already drops non-finite values at read, so the two whole-buffer sanitize
passes (0.8 ms per layer at 2048 tokens) are redundant there.
Prefill in 2048-token steps on Strix Halo goes from 198 tok/s (cold first
chunk) / 267 (4K) / 248 (16K) to 342 / 423 / 407, with decode unchanged.

Prefill-sized (32+ token) Q8/F16 projections run as an F16 hipBLASLt GEMM.
The library's heuristic order is unreliable on gfx1151, so each shape class
times the candidates once and keeps the fastest. This replaces the rocBLAS
F32 path, the custom WMMA row-tile kernels and the transposed F16 weight
cache with its cold per-layer dequant. Smaller batches (decode, DSpark
verification) stay on the native kernels.

Attention uses one 32-head WMMA kernel for every prefill chunk, including
the zero-prefix first chunk. When the MMQ gate/up pair produces every
routed mid row, SwiGLU writes them as F16 and the Q2_K down projection runs
as one tiled F16 WMMA kernel; the other gate/up routes keep the hot/cold
down split. The indexer keeps the weighted head sum in WMMA registers.

See speed-bench/gfx1151-prefill-results.md for the sweep and the
official-continuation scores before and after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant