Repository navigation
Description
Summary
A mlxcel-server running batched speculative decoding (dflash drafter) dies with an uncaught C++ exception once MLX's CUDA graph cache has accumulated more than 2x its capacity in lifetime misses:
terminate called after throwing an instance of 'std::runtime_error'
what(): Cache thrashing is happening, please set the environment variable MLX_CUDA_GRAPH_CACHE_SIZE to a larger value than 400 to fix degraded performance.
The whole process aborts: no request-level error, no recovery, all in-flight requests dropped. Reproduced on GB10 with both v0.4.1 and HEAD (3d738cf), so this is not a #816 regression; #816 release testing surfaced it.
Repro
mlxcel-server -m models/qwen3.5-4b-4bit --draft-model models/qwen3.5-4b-dflash --port 18324
# fire 6 concurrent chat completions (varying prompts, max_tokens 48), then repeat
HEAD died after the first burst plus one follow-up request; the v0.4.1 binary died on the second burst of the identical scenario. With MLX_CUDA_GRAPH_CACHE_SIZE=2000 the same scenario completes 13/13 requests across two bursts plus a single request, with dflash engaged (42 rounds, 630 proposed / 107 accepted tokens, 17% acceptance).
Root cause (vendored MLX 0.32.1)
mlx/backend/cuda/lru_cache.hline 91:if (env_name_ && ++cache_misses_ > 2 * capacity_) throw std::runtime_error(...).cache_misses_(line 131) is a lifetime counter: never reset, not on hits, not on trim. Any process that ever inserts more than2 * capacitydistinct graph keys throws, regardless of the hit rate at that moment.mlx/backend/cuda/device.cppline 210:graph_cache_("MLX_CUDA_GRAPH_CACHE_SIZE", /* default_capacity */ 400), so the throw threshold is 800 lifetime misses.mlx/backend/cuda/device.cppline 490: the cache key isgraph_nodes_key_ + ":" + graph_deps_key_, so every distinct captured graph shape is a distinct key.
Batched speculative decoding is exactly the workload that generates many distinct graph shapes: draft and verify phases, times varying batch sizes (B=1 and B>1 arms), times sequence-length bucketing. The abort is therefore inevitable for any sufficiently long-lived speculative server at the default capacity. "Thrashing" is a misdiagnosis when the working set has simply grown over the server's lifetime.
Proposed fixes
- Short term: when the server starts with a drafter or MTP configured and
MLX_CUDA_GRAPH_CACHE_SIZEis unset, set a larger default (2000 was sufficient in testing) before the first MLX CUDA call, and log the chosen value. - Robustness: catch the exception at the FFI boundary in the scheduler decode path and translate it into a request-level failure plus a clear operator hint, instead of process death.
- Upstream: report the lifetime-miss semantics to ml-explore/mlx. A windowed miss rate, or resetting the counter on hits, would distinguish genuine thrashing from a legitimately large lifetime working set. Until then, any long-lived shape-diverse CUDA workload is a time bomb at default settings.
Also observed
Both v0.4.1 and HEAD spam WARN Failed to sync paged state for seq-N: CachePool: expected 32 paged layer lengths for seq-N, got 0 during batched dflash decode (hundreds of lines per burst; requests still complete correctly). Pre-existing as well; noted here so it is on record, likely worth its own investigation.
Environment
GB10 (sm_121, unified memory), make release-cuda, MLX pin 0.32.1 (57c66cac), qwen3.5-4b-4bit target + qwen3.5-4b-dflash drafter (block_size 16), scheduler max_batch_size 4.
Activity
fix(bench): apply the CUDA graph-cache default in speculative_bench t…
fix(server): log CUDA graph-cache capacity after subscriber init (#818)
docs(server): finalize CUDA graph-cache default docs and SAFETY note
fix(server): raise the CUDA graph-cache default to survive long-lived…
inureyes commentedon Jul 19, 2026
Follow-up issues filed for the three deferred proposals from this issue:
The core crash fix (defaulting MLX_CUDA_GRAPH_CACHE_SIZE=2000 on CUDA builds) is merged in bac0fca.
fix(server): survive MLX decode-eval throws instead of aborting the w…
docs(upstream): report the MLX CUDA graph-cache lifetime-miss abort (#…
fix(cuda): keep small masked verify blocks off cuDNN SDPA
fix(bench): apply the SDPA plan-cache default in speculative_bench
fix(cuda): keep small masked verify blocks off cuDNN SDPA
fix(bench): apply the SDPA plan-cache default in speculative_bench
fix(cuda): keep small masked verify blocks off cuDNN SDPA (#1799) (#1817
test(gemma4): run MTP parity tests at the server's MLX cache sizes
test(gemma4): run MTP parity tests at the server's MLX cache sizes
fix(server): raise the CUDA graph-cache default to survive long-lived…
fix(server): survive MLX decode-eval throws instead of aborting the w…
docs(upstream): report the MLX CUDA graph-cache lifetime-miss abort (l…