ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

--ssd-streaming: all requests fail on GGUFs with per-layer mixed routed-expert quantization ("Metal model range ... is not covered by mapped model views")

Open andreaborio opened this issue 5 months ago • 4 comments

Summary

A GGUF whose routed-expert tensors are not uniformly quantized across layers — e.g. the usual IQ2_XXS/Q2_K experts with a handful of layers upcast to Q4_K via deepseek4-quantize --tensor-type 'blk.N.ffn_gate_exps=q4_k' (and up/down) — loads fine under --ssd-streaming, but every chat request fails with HTTP 500. The same GGUF works with full residency, and uniformly-quantized GGUFs stream fine.

Mixed per-layer expert precision is an attractive recipe for tight memory budgets (spend a 2-bit budget asymmetrically: Q4_K only on the layers a workload activates hardest), so it would be great if streaming supported it — or at least failed at startup with a clear message instead of per-request encode errors.

Environment

  • Apple M5 Pro, 64 GB, macOS 26.5 (Metal backend, SSD streaming)
  • DeepSeek-V4-Flash, 2-bit family quant (IQ2_XXS gate/up, Q2_K down, Q8_0 attn/shared/output), 6 layers' experts upcast to Q4_K → 97 GB GGUF
  • ds4-server -m model.gguf --ssd-streaming --ssd-streaming-cache-experts 40GB

Observed

Startup is clean (SSD streaming initial metal model map restricted to token embedding, expert cache sized as usual). Then on every request:

ds4: Metal model range 50.64..51.76 GiB is not covered by mapped model views
ds4: Metal model range 52.89..54.01 GiB is not covered by mapped model views
ds4: Metal model range 51.76..52.89 GiB is not covered by mapped model views
ds4: gpu layer 30 ffn batch encode failed
ds4: gpu layer-major prefill layer 30 encode failed

(layer 30 is one of the boosted layers; the three ~1.12 GiB ranges are its fused gate/up/down Q4_K expert tensors.)

Root cause (from reading the source)

Two places assume one global per-expert byte size:

  1. Prefill. With the streaming batch-selected-addr prefill mode enabled (metal_graph_stream_prefill_batch_selected_addr_enabled — note its host gate keys off weights->layer[0] types only), each layer is mapped via the decode-static span set (model_map_span_vec_include_layer_decode_static), which deliberately omits ffn_{gate,up,down}_exps — experts are expected to come from the SSD expert cache. But the Metal encoder's per-layer fast path is IQ2-only (gate_type == IQ2_XXS && down_type == Q2_K), so a Q4_K layer falls through to the generic path that wraps the full fused expert tensors through ds4_gpu_wrap_model_range — and no installed model view covers that range. Encode fails → 500.

  2. Decode (latent). The streaming expert cache is a single-size-class slab allocator: ds4_streaming_routed_expert_bytes() derives the per-expert size from the first routed layer, and ds4_gpu_stream_expert_cache_note_expert_size() is last-writer-wins. Off-size experts can never fit slab slots (g_stream_expert_cache_slab_slot_bytes mismatch), distort the byte budget accounting, and after an mlock-driven cap the exact-size victim requirement in entry_reusable() can starve loads permanently.

Workaround (zero code)

DS4_METAL_DISABLE_STREAMING_PREFILL_BATCH_SELECTED_ADDR=1 makes prefill use the legacy full-layer maps (which do include the exps tensors): the model then serves correctly. Cost: full-layer streaming for all layers, and the decode-cache hazard in (2) remains theoretically live.

Proposed fix (working patch available)

I have a working patch on my fork (branch feat/streaming-mixed-precision, design notes + validation table in STREAMING_MIXED_PRECISION.md):

  • detect "boosted" layers (per-expert bytes ≠ the slab class of the first routed layer) and include their exps tensors in the decode-span builders, so the existing wrap_model_range / wrap_model_exact_range fallbacks find their ranges covered (those layers read experts through pageable mmap views instead of the expert cache);
  • pre-seed the expert-cache slab size class at startup and turn note_expert_size into freeze+reject, so off-size layers deterministically bypass the cache instead of poisoning it; startup logs the uniform/boosted split.

Uniform models are byte-identical by construction (verified by greedy temp-0 replay of recorded outputs); the mixed-precision GGUF above has since run a full benchmark suite through it (thousands of requests, zero transport errors). Untested: CUDA/ROCm builds (no-op stubs only), PRO-shaped 384-expert models, distributed slices.

Happy to open a PR if the approach looks right to you — or to rework it if you'd prefer a different design (e.g. per-layer slab size classes in the expert cache).

andreaborio avatar Jun 11 '26 06:06 andreaborio

not sure if relevant or useful at all, I've just received a Max 5 with 36 (!?!) GB of unified RAM and I could run out of streaming the version 2 of the lighter model without any particular issue, but I haven't specified anything else as parameter.

I guess I am just sharing what works and what, in here, might doesn't 👋

WebReflection avatar Jun 11 '26 13:06 WebReflection

m5 max with 36gb of ram is wild lol, anyway this issue is about uneven bit distribution across layers within a single GGUF. Your build streams fine because all its routed experts share one quant type, so the expert cache's single-size-class assumption holds. The failure only appears when a few layers' experts are upcast (Q4_K islands in an IQ2 sea): those tensors fall outside the cache's slab size and the mapped-views fallback doesn't cover them, so every request 500s.

andreaborio avatar Jun 11 '26 13:06 andreaborio

Thanks @andreaborio I'm merging the commit that fixes the issue. Will be pushed into main after some QA.

antirez avatar Jun 14 '26 11:06 antirez

Should be fixed.

antirez avatar Jun 16 '26 09:06 antirez