ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

Metal pre-M5: decode collapses ~10x once context crosses n_indexer_top_k (GPU 95% idle)

Open GiorgioOppo opened this issue 4 months ago • 0 comments

Metal pre-M5: decode collapses ~10x once context crosses n_indexer_top_k (GPU 95% idle)

Setup: M1 Pro 16 GB, macOS 26.5.2, DeepSeek V4 Flash 2-bit imatrix GGUF, --ssd-streaming --ssd-streaming-cache-experts 320, current main. The M1 reports "Metal 4 tensor API disabled for pre-M5/pre-A19 devices" at startup.

Symptom: greedy decode throughput vs prompt length (12 tokens generated, cold, page cache purged):

prompt tokens decode t/s
~15 3.2
~500 2.21
~650 1.25
~2050 0.25

The knee is exactly at n_indexer_top_k = 512 (DeepSeek V4 Flash shape). Beyond it the cost grows ~2.3 ms per context row per token (≈53 us/row/layer over 43 routed layers).

Evidence gathered:

  • DS4_METAL_DECODE_STAGE_PROFILE=1: attn_hc_pre goes 3.36 -> 68.5 ms/layer between short and 2k prompts (the stall surfaces at the next stage boundary); the actual attention stages (q_path/kv_path/attn_output) stay at 1-2 ms at 2k context.
  • DS4_METAL_GPU_BUSY_PROFILE=1 during decode: ~125 ms of GPU busy per 64 command buffers spanning ~2.8 s of wall time -> GPU ~95% idle. During prefill the same counter shows ~100% busy (39 s busy / 39 s wall at 53 t/s), so the GPU side is healthy.
  • sample during slow decode: main thread 92% in the metal_graph_selected_async_load_finish cond wait (ds4.c:23431); the async worker 90% in [IOSurfaceSharedEvent waitUntilSignaledValue:] (ds4_gpu_wait_selected_readback_ready, ds4_metal.m:8865); pread pool ~98% idle; physical footprint 5.5 GB, no swap.
  • Insensitive to: expert cache size (64/193/320), expert bundle sidecar on/off, and DS4_METAL_DECODE_INDEXER_SPARSE_THRESHOLD=4096 (it gates the indexer scoring, but the gathered-attention switch keys on top_k < n_index_comp directly).

Hypothesis: on pre-M5 Metal the sparse-indexer decode path adds a per-layer serialization on the shared-event handshake whose latency scales with n_comp while the GPU sits idle -- the signal-carrying command buffer appears to schedule ~100 ms late at 2k context. Possibly the M5 tensor-API path hides this and the fallback path was never exercised at long context on pre-M5 hardware.

Repro:

head -c 9000 speed-bench/promessi_sposi.txt > /tmp/prompt_2k.txt
./ds4 -m DeepSeek-V4-Flash-...gguf --ssd-streaming \
  --ssd-streaming-cache-experts 320 \
  --prompt-file /tmp/prompt_2k.txt -n 12 --temp 0

Happy to run patches/diagnostics on this machine.

GiorgioOppo avatar Jul 21 '26 09:07 GiorgioOppo