ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

glm5.2 branch: DeepSeek Flash decode regression (~2.8x) introduced by "Add GLM 5.2 runtime support"

Open andreaborio opened this issue 4 months ago • 3 comments

While benchmarking our GLM fork line against main we found that the whole glm5.2 branch decodes DeepSeek Flash about 2.8x slower than main, and bisected it to the first commit of the line.

Machine: Apple M5 Pro 64 GB, macOS 26 (Darwin 25.5.0), Metal. Model: DeepSeek-V4-Flash IQ2XXS (w2Q2K, AProj/SExp/Out Q8, chat-v2 imatrix), 80.8 GiB file, so model >> RAM and --ssd-streaming. Probe:

./ds4-bench -m MODEL.gguf --ssd-streaming \
    --prompt-file speed-bench/promessi_sposi.txt \
    --ctx-start 2048 --ctx-max 2048 --step-incr 2048 --gen-tokens 32

Single-commit points are ctx 2048 only, rebuilt via make after each checkout, and their ranges combine two measurement sessions on separate days; the "main" and "our line" rows are interleaved arms from one session (A/B/C x2 back to back) swept at ctx 2048-4096.

commit gen t/s
main (5b95fa1) 6.8-8.3 (4 interleaved runs, ctx 2048-4096)
main (80ebbc3, current HEAD) 6.49 (1 run)
173a9a3 "Add GLM 5.2 runtime support" 1.94-2.21
34b3736 (pre pro-correctness merge) 2.76-2.88
bd89932 (glm5.2 HEAD) 2.73 (1 run)
our GLM branch (bd89932 + our fixes, default env) 1.9-2.8 (8 interleaved runs, ctx 2048-4096)

A clean detail for bisecting: 80ebbc3 (current main, fast) is the direct parent of 173a9a3 (slow), so the whole regression sits inside that one commit. It persists through the branch HEAD, is untouched by streaming env configuration (disabling the expert readahead does not move it), and main is not affected. First token on the glm5.2 line takes ~5.2-7.2 s (gen_first_ms 5166-7180 across all runs); main's bench build predates the first-token/steady columns, so for main only whole-generation t/s is directly comparable, and on the glm5.2 line even the steady phase after the first token (gen_steady_tps 2.6-6.5) stays below main's whole-run 6.8-8.3.

Prefill shows no clear regression once the page cache is warm (fork line 107-120 vs main 116-118 t/s at these sizes in the warm interleaved reps; first-rep deltas are cache-warming noise).

We have not root-caused inside 173a9a3; happy to bisect deeper into it or test patches on this box. Repro scripts available.

andreaborio avatar Jul 10 '26 16:07 andreaborio

Update: 427e281 fixes this in-RAM, but the streaming regression is still here.

Re-tested on an M5 Max (128 GB): current main (427e281) vs the pre-GLM5.2 baseline (80ebbc3), DeepSeek-V4-Flash IQ2XXS, interleaved runs to cancel out page-cache warmth.

Model fits in RAM (page-cache warm) — parity, so the 427e281 Metal kernel rework did fix the in-RAM decode:

build gen t/s steady t/s
current 427e281 ~12.4–12.9 ~15
baseline 80ebbc3 ~12.5–13.3 —

Model ≫ RAM (real SSD streaming — forced with --simulate-used-memory 72 to emulate the original 64 GB box) — regression is still here, ~2–3×, plus a ~6 s first-token every generation:

build gen t/s (4 interleaved runs) steady t/s first-token
current 427e281 0.26 / 2.33 / 1.90 / 2.47 0.26–4.63 (unstable) ~6.0 s
baseline 80ebbc3 6.13 / 6.49 / 6.57 / 6.56 — fast

So the compute path is fixed, but the regression lives in the SSD-streaming / expert-prefetch path, which 427e281 didn't touch. The ~6 s first-token and the fact that it only shows up when actually streaming from disk point at expert prefetch rather than the decode kernels.

One-line repro on any machine (locks memory to force the streaming regime even if the model would otherwise fit):

./ds4-bench -m DeepSeek-V4-Flash-IQ2XXS-...gguf --ssd-streaming --simulate-used-memory 72 \
    --prompt-file speed-bench/promessi_sposi.txt --ctx-start 2048 --ctx-max 2048 --gen-tokens 32

Digging into the prefetch path now; will send a PR if we find it.

gilbert-barajas avatar Jul 20 '26 15:07 gilbert-barajas

Confirmed on ROCm / gfx1151 (Strix Halo) — same root cause, fix 427e281 doesn't cover ROCm

We're seeing the same regression class on AMD ROCm (not Metal), and 427e281 does not fix it for us because the ROCm side of that commit is an empty stub.

Environment

  • Hardware: GMKtec EVO-X2 — AMD Ryzen AI Max+ 395 (Strix Halo), iGPU Radeon 8060S, gfx1151, 128 GB unified LPDDR5X
  • OS: Ubuntu 26.04 LTS, HIP 7.1, ROCm (hipcc /usr/bin/hipcc)
  • GPU memory: UMA BIOS 2 GB + amd-ttm --set 115 → ~117 GB GTT dynamic
  • Model: DeepSeek-V4-Flash IQ2_XXS (w2Q2K, AProj/SExp/Out Q8, chat-v2 imatrix), 80.76 GiB — fits entirely in GTT (no SSD streaming)
  • Corpus: speed-bench/promessi_sposi.txt (SHA f53e0d80, 1.3 MB)
  • Build: make strix-halo (gfx1151, HIPCC), commits 80ebbc3 (baseline) vs efdadd4 (HEAD)

Bisect

The ROCm equivalent of 173a9a3 is commit ef8d923 ("Add ROCm GLM 5.2 support"). 80ebbc3 is the direct parent — same clean bisect as the Metal side.

Benchmark results (Promessi Sposi, 16K→100K sweep)

/usr/bin/time -v ./ds4-bench -m ./ds4flash.gguf \
    --prompt-file speed-bench/promessi_sposi.txt \
    --ctx-start CONTEXT --ctx-max CONTEXT \
    --step-incr 2048 --gen-tokens 64
    
Context 80ebbc3 prefill t/s efdadd4 prefill t/s Δ prefill 80ebbc3 gen t/s efdadd4 gen t/s Δ gen
16K 215.21 209.22 −2.8% 14.18 8.15 −42.6%
32K 210.77 199.54 −5.3% 13.53 7.94 −41.3%
65K 193.81 171.14 −11.7% 12.58 7.60 −39.6%
100K 177.04 138.33 −21.9% 11.66 7.27 −37.7%

Generation is consistently ~40% slower across all context sizes. Prefill is roughly unchanged at 16K but degrades at longer contexts (−22% at 100K), likely due to the new KV-cache compression overhead.

Root cause: fast-path decode Q8_0 n_tok==1 removed in ef8d923

ef8d923 rewrote rocm/ds4_rocm_matmul.cuh. The diff shows the single-token decode fast-path being deleted:

Before (80ebbc3, ds4_rocm_matmul.cuh ~line 248):

if (n_tok == 1 && !cuda_runtime_config()->q8_prequant_decode) {
    // Dedicated decode kernel: matmul_q8_0_preq_rows_w32_kernel
    uint32_t rows_per_block = cfg->q8_decode_rpb;
    matmul_q8_0_preq_rows_w32_kernel<<<...>>>(out, wptr, xq, xscale, ...);
    return;
}

After (efdadd4): the entire n_tok == 1 branch is gone. Decode (n_tok==1 = generation phase) now falls through to the generic batch prefill path, which is much slower for single tokens.

This is the exact same pattern as the Metal regression — but for ROCm.

Why 427e281 doesn't help us

On Metal, 427e281 ("Optimize Metal prefill and decode kernels") restored the in-RAM decode path. But in rocm/ds4_rocm_matmul.cuh, the same commit only adds an empty stub (return 0 body, all args cast to void):

// Added by 427e281 to rocm/ds4_rocm_matmul.cuh:
extern "C" int ds4_gpu_matmul_f16_pair_compressor_store_tensor(...) {
    (void)out_kv; (void)out_score; ...  // all args unused
    return 0;  // ← no-op
}

So the ROCm fast-path decode was never restored.

Key difference from the Metal issue

  • On Metal: model fits in RAM → 427e281 fixed it; only SSD streaming remains broken (PR #578).
  • On ROCm: model fits in GTT (80.76 GiB of 117 GiB) → still broken because the fast-path was never restored.

Reproduction

# Any Strix Halo machine (gfx1151), ROCm 7.1
cd ~/ds4-repo
make strix-halo

# Baseline (fast): checkout 80ebbc3, rebuild, benchmark
git checkout 80ebbc3 && make strix-halo
./ds4-bench -m ds4flash.gguf --prompt-file speed-bench/promessi_sposi.txt \
    --ctx-start 16384 --ctx-max 16384 --gen-tokens 64
# → gen ~14 t/s

# Regressed (slow): checkout efdadd4, rebuild, benchmark
git checkout efdadd4 && make strix-halo
./ds4-bench -m ds4flash.gguf --prompt-file speed-bench/promessi_sposi.txt \
    --ctx-start 16384 --ctx-max 16384 --gen-tokens 64
# → gen ~8 t/s

Suggested fix

Restore the n_tok == 1 fast-path in rocm/ds4_rocm_matmul.cuh (cuda_matmul_q8_0_tensor_labeled), equivalent to what existed in 80ebbc3. The removed kernel matmul_q8_0_preq_rows_w32_kernel and its launch config are still present in the file above the deleted block.

beedweb avatar Jul 21 '26 22:07 beedweb

Confirming this regression on Apple Silicon, with a bisect, memory forensics, and a test of PR #578.

Setup: MacBook Pro M5 Max, 128 GB unified, macOS 26.5.2 (25F84), iogpu.wired_limit_mb=122880, vm.global_user_wire_limit ≈ 108.8 GiB. Model: DeepSeek-V4-Flash-Q4KExperts-F16HC-F16Compressor-F16Indexer-Q8Attn-Q8Shared-Q8Out-chat-v2-imatrix-0731.gguf (153.3 GiB, all 129 routed-expert tensors uniformly Q4_K). Server: ds4-server -m <model> -c 32768 --ssd-streaming (auto expert cache: 87.80 GiB = 6.75 prefill + 81.05 dynamic). Reproducer: 3 consecutive greedy (temp 0) 1536-token generations of the same coding prompt; tok/s is wall-clock end-to-end.

Decode throughput (generation 1 → 2 → 3):

build gen 1 gen 2 gen 3 compressor peak outcome
80ebbc3 (pre-regression) 15.2 14.8 14.7 1.5 GiB stable
0a7ad77 (current main-ish) 14.2 5.3 4.9 43 GiB 2.9× collapse
0a7ad77, stock flags only 14.4 — — 71 GiB server killed by our watchdog mid-gen-2
0a7ad77 + --ssd-streaming-cache-experts 55GB ~12.4 ~12.4 ~12.3 low stable (mitigation)
0a7ad77 + PR #578 13.2 13.8 13.8 0.96 GiB stable

This also causes kernel panics. Three on this machine in 6 days (matches #483): watchdog timeout: no checkins from watchdogd in 92 seconds, all with the same memoryStatus signature — free = 0, pagesWanted ≈ 3200 / pagesReclaimed = 0, compressor at 35–43 GiB. The worst run logged 820M compressions / 794M decompressions (18× a normal day): the OS compresses the unpinned expert-cache pages, ds4 re-touches them, repeat. A watchdog killing the server at compressor > 28 GiB reliably prevents the panic.

Bisect (good 80ebbc3, bad 0a7ad77, probe = the 3-generation reproducer):

  • fc9efd1 good (14.4–14.7 stable)
  • 66cbce5 bad (watchdog fired, compressor 70.4 GiB)
  • 63d9874, 36cd0ca, 36ee8c1 don't build standalone on arm64 (link errors); 005afed builds and serves but every chat request returned empty in our probe — all skipped
  • so the first functional bad commit is in 63d9874..66cbce5, and within that range git log -S mlock -- ds4_metal.m points at exactly one commit: 005afed ("Add GLM 5.2 inference…"), where mlock occurrences in ds4_metal.m go from 78 to 0 — consistent with your attribution here.

PR #578 test: applies cleanly on 0a7ad77, mlock count back to 77. It fully eliminates the memory spiral (compressor peak 0.96 GiB, swap peak 361 MB over a 12-round benchmark) and the round-to-round collapse. It lands ~7% below the pre-regression build on long generations (13.7 vs 14.7 tok/s), so 80ebbc3 remains slightly faster for streaming decode on this machine — but #578 restores stability and, critically, removes the panic risk. Happy to run any variant of this if it helps validate the fix.

One more datapoint: the wire-limit concern from #437 doesn't bite here — vm.global_user_wire_limit (108.8 GiB) comfortably covers the 87.8 GiB auto cache. And the disk KV cache (--kv-disk-dir) is not involved: the collapse reproduces identically without it.


Investigation and write-up AI-assisted — Claude Code running Claude Fable 5 in Ultracode mode; all measurements are from my machine and I reviewed the content.

FilipeRSTeixeira avatar Aug 01 '26 20:08 FilipeRSTeixeira