kv-cache: reserve the turboquant continuation-prefill workspace before sizing the KV cache (fixes a high-context Xid 31 crash class)

Merged
KE
4mo ago
+158-0

Summary

At high --max-model-len with a turboquant_* KV cache, the KV pool is sized against all remaining VRAM — but the turboquant continuation-prefill workspace allocates lazily at request time. On memory-tight configs the pool claims memory the workspace later needs, and a large-context request then faults with an illegal memory access (Xid 31, MMU FAULT_PDE) instead of a clean error.

This PR reserves the workspace (K+V, 256-byte aligned, per ubatch slot) from available_memory before get_kv_cache_configs sizes the pool, so the boot-time max_model_len validation becomes honest: over-committed configs are refused at startup with the standard estimated-max message instead of crashing mid-request.

Env-gated: VLLM_TQ_RESERVE_PREFILL_WORKSPACE (default on; set 0 to restore legacy sizing — happy to flip the default if you prefer opt-in).

Evidence (2×2080 Ti 22GB, TP=2, Qwen3.8-27B GPTQ-Int4, kv turboquant_k3v4_nc, max-model-len 524288, MTP K=2)

Reliable repro: fresh uncached ~380K-token prefills crash the unpatched engine (Xid 31 on both GPUs, variable crash depth — allocator-layout dependent).

Intervention test, one variable changed (this patch):

  • unpatched: 380K crash ×2, 400K crash ×2
  • patched: 380K / 400K / 450K all complete correctly (needle retrieval verified at 90% depth)
  • patched boot on the same config also refused an over-committed variant with 4.15 GiB KV needed > 3.77 GiB available, estimated max 473824 — the failure the legacy sizing hid.

Quality gate after shipping this on our box: 100-item eval suite at exact parity with baseline; no throughput change measured (3-rep bench).

Notes

  • The reserve formula mirrors get_simultaneous alignment (one fp16 K+V pair at max(max_num_batched_tokens, VLLM_TURBOQUANT_CONTINUATION_WORKSPACE_RESERVE_TOKENS) cached length, replicated per ubatch under DBO).
  • A residual, much-higher boundary (~450K with speculative decoding enabled) appears to involve a separate spec-config-dependent structure — still under investigation on our side; this patch is independent of it.
  • Happy to add a unit test for get_kv_cache_configs accounting if you'd like it in-tree.

🤖 Generated with Claude Code


Summary by cubic

Reserves the turboquant continuation-prefill workspace from available VRAM before KV cache sizing to prevent deep-prefill illegal memory access (Xid 31) and make sizing deterministic. Over-committed configs now fail at startup with the standard estimated-max error instead of crashing.

  • Engages only for turboquant_* caches when max_num_batched_tokens exceeds the continuation threshold; no effect on other caches or small contexts.

  • Reserve mirrors the runtime allocator: two fp16 K+V buffers, 256-byte aligned, replicated per workspace ubatch (2 with DBO, else 1). VLLM_TURBOQUANT_CONTINUATION_WORKSPACE_RESERVE_TOKENS can raise the cached-length floor.

  • Implemented in get_kv_cache_configs: subtracts the per-rank reserve before auto-fit, admission checks, and per-worker block planning; num_gpu_blocks_override still pins blocks exactly.

  • Deterministic single-counting: gpu_worker.determine_available_memory adds back only min(workspace_arena_bytes, continuation_reserve_bytes) under the same gating, so the explicit reserve applies exactly once regardless of allocation timing.

  • Adds an info log with reserved GiB. Controlled by VLLM_TQ_RESERVE_PREFILL_WORKSPACE (default on).

  • Expected side effect: slightly smaller KV budgets on tight VRAM, causing max_model_len to auto-cap or fail early; adjust operator configs if caps are hit.

  • To restore legacy sizing (and its crash risk), set VLLM_TQ_RESERVE_PREFILL_WORKSPACE=0.