ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

GLM-5.3-Flash-Q4_K Metal SSD-streaming (M4 Max, 128 GiB): host-side per-layer bookkeeping dominates token time (15 % GPU), and the async selected-id load is a no-op

Open cropduster opened this issue 2 months ago • 3 comments

Hi antirez, thanks for ds4 — it runs a 178 GiB GLM-5.3-Flash-Q4_K on a 128 GiB M4 Max, which is remarkable.

I spent a day measuring the GLM SSD-streaming decode path before touching it, and I think the findings below are worth an issue rather than a patch: I could not turn any of them into a net win. All numbers are from real runs on this machine; raw stderr is available on request.

Environment

  • current fork build on top of origin/main @ 9139e2a, no local changes for these numbers
  • Mac Studio M4 Max, 128 GiB, macOS 27; Metal 4 tensor API unavailable on this device (log: "Metal 4 tensor API disabled for pre-M5/pre-A19 devices")
  • Model: GLM-5.3-Flash-Q4_K.gguf, 177.77 GiB, 1412 tensors, types f32/q8_0/q4_k/bf16 (129 q4_k = 42 routed layers x 3 expert tensors) — a uniform Q4_K expert layout, no Q5_K, no IQ2_XXS
  • Protocol: --ssd-streaming --ctx 4096 --tokens 128 --temp 0, a prompt that actually generates 128 tokens, cold + warm run, values parsed from stderr with DS4_METAL_STREAMING_EXPERT_TIMING_SUMMARY=1 DS4_METAL_MEMORY_REPORT=1

Finding 1 — the timing summary is easy to misread, and 1-token runs measure the wrong thing

A prompt like "answer with exactly one word" yields a single-token answer: selected_calls=42 and read_avg=11.7 ms. That is 42 layers x 1 token, not a decode rate — the reported generation: 1.8 t/s is the first token, which carries the whole page-fault / cache-fill warmup. With a prompt that fills the token budget the same build reports:

run prefill t/s decode t/s selected_calls read_total
Q4 streaming, cold 2.60 6.21 5334 7813 ms
Q4 streaming, warm 2.64 6.22 5334 7793 ms

selected_calls = 42 x 127 confirms the counter is per layer x token. A sanity line printing the generated token count next to generation: t/s (or a doc note that the timing summary needs a prompt that exhausts --tokens) would prevent this class of misreading.

Finding 2 — the host readback is not the bottleneck; removing it entirely changes nothing

uniform_streaming_selected_cache and stream_addr_q4 are both true for this GGUF, so the decode already runs the address-table path (stage profile: pair=q4_stream_addr_swiglu, down_path=q4_stream_addr_down_simd), not a legacy path. I then removed the synchronous selected-id readback completely. It was gated twice: glm_graph_layer_uses_generic_routed_moe() (IQ2 only, from the initial GLM 5.2 commit 005afed) and, on Metal, an opt-in env var (ds4.c: return getenv("DS4_METAL_ENABLE_GLM_STREAMING_SELECTED_ASYNC_LOAD") != NULL;). With the gate opened for streaming_selected_cache and the flag set:

baseline async path
decode t/s 6.21 / 6.22 6.22 / 6.24
selected_calls / read_total 5334 / 7793 ms 0 / 0 ms
output (temp 0) 584 B byte-identical

DS4_METAL_GLM_STREAMING_ASYNC_PROFILE=1 shows why: async_calls=5334 total=20007 ms avg=3.751 ms signal_start=17 ms flush_router=18 ms shared=42 ms flush_shared=7 ms finish=10945 ms routed=8974 ms. The work moves from the decode thread into the service thread, and the decode thread then waits in finish — no overlap is gained, because there is almost nothing left to overlap with (shared = 42 ms total).

Where the token time actually goes

Stage profile per layer x token in steady state: pair=0.31 ms down=0.25 ms -> GPU ~0.58 ms x 42 layers = ~24 ms of ~161 ms per token (15 %). The rest is host-side expert-cache bookkeeping per layer x token: read_total 61 ms (only without the async path), bind_total 25 ms, missing_bind (split path) 20 ms, readahead 22 ms (14769 calls in 127 tokens), buffer preparation ~11 ms. Cache pressure is real but not the lever either: 48 GB / 56 GB / 85 GB budgets give 5.3 / 5.3 / 4.6 t/s with hit_rate 0.74 / 0.77 / 0.84 — the automatic budget (70.1 GiB, hit_rate 0.83, 4575 evictions) is already the best, and 85 GB starts swapping.

Flags I tested (all neutral)

I also swept the knob surface around this path with the same 128-token protocol, two runs each, output byte-identical in every case: DS4_METAL_ENABLE_Q4_GROUPED_EXPERTS=1 (enable-only gate, default off), DS4_METAL_ENABLE_Q4_BATCH_EXPERT_TABLE=1, DS4_METAL_ENABLE_Q4_EXPERT_ADDRESS_TABLE=1 (already default on), DS4_METAL_DISABLE_STREAMING_EXPERT_READAHEAD=1 (slightly worse: 6.04 vs 6.22 t/s), DS4_METAL_DISABLE_STREAMING_EXPERT_SPLIT=1, DS4_METAL_DISABLE_STREAMING_EXPERT_HIT_VALIDATOR=1, --ssd-streaming-full-layers 2 and 4, --ssd-streaming-preload-experts 2000. All stay within noise of 6.2 t/s, and whatever is removed reappears elsewhere (readahead disabled -> bind_total +0.8 s). The per-layer chain looks serial rather than dominated by any single removable cost — which is why I am not sending a patch.

For completeness: --mtp on this path measures 5.28 / 5.22 t/s (i.e. ~15 % slower than without it) with byte-identical output, and I have switched my launcher default to MTP off for GLM streaming.

Related work in this tracker

  • #437 (M4 Max 128 GiB SSD-streaming benchmark and alternative supply strategies) — same machine class, DeepSeek graph; my numbers are for the GLM path, which does per-layer host-side address resolution in ds4_gpu_glm_routed_moe_one_tensor.
  • #840 (byte-identical streaming speedups on V4 Flash) — I could not reproduce a gain of that class on GLM Q4_K: see the sweep above.
  • #636 (L+2 router lookahead from an offload prototype) — the per-layer chain described there matches what I measure here; a lookahead across layers is what I would try next.
  • #810 (smaller expert cache faster on M5 Pro) — on M4 Max 128 GiB with GLM Q4_K the automatic budget was the best of the four budgets I tested.
  • #495 (--mtp together with --ssd-streaming) — 15 % slower here, byte-identical output.

Question

For the DeepSeek/MXFP4 streaming path the same counters show read_avg=0.000 ms, bind_avg=0.444 ms at 15.6 t/s, i.e. per-layer address resolution looks much cheaper there. Is the intended direction for GLM to move this per-layer/per-token bookkeeping to the GPU (compact/validate style) instead of the host loop over routed experts? And is the Metal async selected-id load meant to stay opt-in for uniform Q2_K/Q4_K layouts, or is enabling it by default just untested?

I can provide raw stderr for all runs, the exact flag matrix, and the (correctness-neutral) patch if that helps. Thanks!

cropduster avatar Sep 15 '26 12:09 cropduster

Update with a corrected measurement protocol, plus a test of #964 and the open report in #989.

Protocol note (important): my earlier numbers on this path were taken with prompts that answer in a single token, which measures the first token (cache fill + page faults), not decode. All numbers below use a prompt that actually generates 128 tokens, and I check cache aging tokens ≈ 127 per run.

1. Phase-0 measurement (GPU vs host vs I/O) — the host-side address bookkeeping is not the bottleneck. Instrumenting the Metal command buffers directly (GPUStartTime/GPUEndTime, union of GPU intervals) and tracing reads gives, for GLM-5.3-Flash-Q4_K with --ssd-streaming --ctx 4096 on M4 Max 128 GiB:

  • gpu_busy_union == gpu_span_sum → command buffers do not overlap; GPU work is fully serialized. 70.7 command buffers per token (~1.7 per layer).
  • I/O: 15,095 reads, 66.34 GiB (0.52 GiB/token) in 20.93 s → 3.17 GiB/s aggregated; the device is busy only 43.5 % of decode time, mean concurrency 1.39 (max 9), p50 read 3.78 ms for 4.50 MiB.
  • Per-token budget (162 ms): ~30 % unavoidable SSD read volume, ~31 % GPU work (serialized), remainder waiting. The address-resolution part is 0.49 ms of 162 ms (0.3 %).

So the GPU-side address resolution idea I floated earlier would buy ~nothing; the ceiling is the serialized per-layer chain (GPU and I/O not overlapping).

2. Control test — expert volume dominates: the same protocol with the Q2_K GGUF (half the expert bytes) gives 21.7 t/s streaming and 28.6 t/s resident, vs 6.2 t/s for Q4_K streaming. Consistent with ~30 % of the budget being read volume plus a per-token cost that scales with slab size.

3. #964 tested on M4 Max with the same protocol (PR head 0a90819): byte-identical output, bind_total −19 % (2.58 s vs 3.17 s), but decode 6.17 t/s vs 6.22 t/s baseline — i.e. no net gain here, and prefill unchanged. The dispatch/scheduling work clearly pays off in a resident setup (your M3 Ultra numbers), it just does not move the serialized streaming chain. Not a criticism of the PR — it looks correct and arithmetic-preserving; it is a data point that our 128 GiB SSD-streaming case is bound elsewhere.

cropduster avatar Sep 15 '26 13:09 cropduster

Follow-up with the final #964 numbers (5 runs per side, same 128-token protocol; two independent builds/testers), plus one mechanism I think is worth having on record.

GLM-5.3-Flash-Q4_K, --ssd-streaming --ctx 4096, M4 Max 128 GiB

baseline (main 7a8ca68) PR #964 (head 0a90819) Δ
decode t/s 6.21 / 6.22 6.28 / 6.24 / 6.17 (5 runs, Ø 6.25) +0.6 % (noise)
bind_total 3.17 s 2.55 s −19 %, reproducible
output 584 B byte-identical in every run arithmetic preserved ✓

Q2 (same protocol): streaming 22.31 / 22.35 t/s cold and ~24.9–25.2 t/s warm, resident 29.55–29.65 t/s → +2.6 % / +3.4 % over the pre-PR baseline. All byte-identical.

Why it does not move our case — structural, not just "we could not reproduce": the PR's tuning path is compiled off on M4 Max (gate in ds4_metal.m; runtime trace shows router_shared=0). So your +27 %/+18 % numbers are reachable in the resident M3 Ultra setup that the PR targets, but not in a 128 GiB SSD-streaming setup — the streaming path is a different chain (GPU ~31 %, SSD reads ~30 %, device busy only 43.5 % of decode time, ~70 command buffers per token that never overlap). Thanks for the work all the same: the output stays bit-identical, which makes merging it much easier to trust.

Mergeability, read-only check: gh reports mergeable=CONFLICTING against current main, but git merge-tree against our fork main shows only three conflicting files (.gitignore, Makefile, ds4_gpu.h) — the engine sources (ds4_metal.m, ds4.c, ds4_server.c, metal/*.metal, tests/) merge automatically. Note the branch is 45 upstream commits behind current main (it predates the Qwen3.8-Flash-Next merge), verified with a control build.

My conclusion: I am not picking it (no gain in our configuration, and it would put our tree into conflict with the PR) — waiting for the upstream merge instead. Happy to re-test on M4 Max once it lands.

cropduster avatar Sep 15 '26 13:09 cropduster

Closing the loop with two negative results, measured on the 128-token protocol. I verified every number below against the raw stderr logs before posting; where a claim rests on code analysis rather than a log, I say so.

1. Read-ahead / pread batching: implemented, measured, reverted. My phase-0 trace showed 15,095 reads for 127 tokens with a mean concurrency of 1.39 (9 pool threads). That turned out to be a duty-cycle average (Σ read time / wall time), not a parallelism deficit, and batching the reads does not pay:

build decode t/s (2 runs)
baseline 6.24 / 6.24
variant: readahead requests issued as pool tasks, earlier I/O start, no serial fcntl on the encode thread 6.08 / 6.10 (−2.4 %)
after revert + diagnostic-only build 6.25 / 6.25 (behaviour-neutral)

All runs: cache aging tokens=127, output byte-identical (584 B). The variant removed ~22 ms/token of host work from the critical path and still lost 2.4 % — consistent with the earlier finding that relieving a single stage just moves the work. Reverted (commit 3a9f312).

One caveat on a number I cannot stand behind: my SSD queue-depth saturation measurement (device saturating at QD≈1–2, ~4.9 GiB/s for 4.5 MiB reads) is not in the raw logs — the iostat recording failed on this machine ("could not record 'disk7' for monitoring"), so treat that curve as indicative, not verified. What is code-verified: expert slabs live in non-contiguous GGUF regions, so a single larger read per slab is not simply available.

2. Speculative/MTP: the expert union already exists — the loss is the accept rate. ds4_session_glm_spec_cycle_impl (ds4.c:73402) runs two tokens through one pass (glm_graph_forward_tokens(..., 2) → glm_graph_encode_ffn_batch), and the routed-MoE expert set is resolved per union, deduplicated across the draft+verify rows. Instrumented (env DS4_METAL_MOE_PATH_PROFILE, commits 84003bc/5e8e350): 74 decode cycles → 3,108 union calls for 6,216 token rows, i.e. one pass per two rows.

Measured cost: verify 282.9 ms/cycle = 6.73 ms per layer pass = 1.74× a single-row pass ⇒ 0.87× per row. So the sharing works; it just cannot beat the arithmetic: with an accept rate of 0.70 (1.73 tokens/cycle) the per-row saving is exactly consumed, and head+draft adds ~22 ms/token (~12 %). Result: 5.10 / 5.12 t/s with MTP vs 6.24 / 6.24 without (−18 %, 2 runs each, byte-identical output). Practical consequence: keep --mtp off for Q4_K SSD-streaming; the only remaining MTP idea I see is holding the MTP layers resident (~3.8 GiB), which is a different experiment.

Bottom line for this configuration: the read volume per token is the binding constraint. Neither I/O structuring nor speculative decoding improves it on a 128 GiB machine; the only measured lever so far is the expert byte size (Q2_K: 22–25 t/s streaming vs 6.2 t/s for Q4_K, same protocol, byte-identical outputs). Happy to test anything else you consider worth trying.

cropduster avatar Sep 15 '26 17:09 cropduster