Skip to content

fix(server): speculative serving aborts the whole process via MLX CUDA graph-cache "Cache thrashing" throw (lifetime miss counter vs default capacity 400) #818

Closed

Description

@inureyes

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.h line 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 than 2 * capacity distinct graph keys throws, regardless of the hit rate at that moment.
  • mlx/backend/cuda/device.cpp line 210: graph_cache_("MLX_CUDA_GRAPH_CACHE_SIZE", /* default_capacity */ 400), so the throw threshold is 800 lifetime misses.
  • mlx/backend/cuda/device.cpp line 490: the cache key is graph_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

  1. Short term: when the server starts with a drafter or MTP configured and MLX_CUDA_GRAPH_CACHE_SIZE is unset, set a larger default (2000 was sufficient in testing) before the first MLX CUDA call, and log the chosen value.
  2. 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.
  3. 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

  1. added
    type:bug
    priority:high
    status:ready
    area:inference
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Jul 19, 2026
  2. added
    status:done
    and removed
    status:review
    on Jul 19, 2026
  3. inureyes commented on Jul 19, 2026

    @inureyes
    MemberAuthor

    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.

  4. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencepriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions