Skip to content

Add CUDA Graph cache management to Qwen3-VL frontends #122

Closed

Description

@strayberry

Context

Qwen3-VL decode uses CUDA Graph replay, but autoregressive decode has one
annoying property: every generated token advances the decode position. That
changes several runtime values:

  • cache_pos
  • RoPE position
  • KV-cache write offset
  • attention KV length

The simple and stable way to handle this is to cache one CUDA Graph per decode
position, or per position-like bucket. The current Qwen3-VL frontends follow
that pattern. For example, the new BF16 path uses:

_prefill_graphs[(patch_count, seq_len, span0, span1)]
_decode_graphs[(cache_pos, rope_pos)]

This is fine for quickstarts and fixed-shape benchmarks, but it is not bounded.
In a long-running process, many prompt shapes or decode positions can keep
adding graph entries until the process exits.

FlashRT already solves this in some other autoregressive frontends. Qwen36 has
graph-cache helpers, LRU-style OrderedDict storage, clear_graphs(), and
tests such as tests/test_qwen36_nvfp4_graph_cache_bound.py. NexN2 also has a
decode graph cap through FLASHRT_NEXN2_GRAPH_CACHE_MAX.

Qwen3-VL has not adopted that kind of graph-cache management yet.

Current Qwen3-VL behavior

  • Prefill graphs are cached by prompt/image shape.
  • Decode graphs are cached by position bucket, such as (cache_pos, rope_pos).
  • Repeated buckets replay efficiently.
  • New prompt lengths, image shapes, or longer decode ranges capture more graphs.
  • Cached graphs are process-local and stay resident until process exit.
  • There is no Qwen3-VL-specific cache limit, LRU eviction, clear_graphs() /
    clear_graph_cache() API, or graph-cache stats.

Why this matters

For fixed local benchmarks, this is acceptable. For serving or arbitrary user
prompts, graph memory can grow over time:

  • different prompt lengths can create many prefill graph buckets;
  • longer generations can create many decode graph buckets;
  • graph memory remains resident for the lifetime of the process;
  • users have no way to bound or reset Qwen3-VL graph memory.

This is not only about the new BF16 Orin path. The existing Qwen3-VL RTX/SM89
frontends also use process-local graph dictionaries, so the fix should ideally
be shared across Qwen3-VL frontends.

Possible follow-ups

  • Reuse or factor out the Qwen36-style graph-cache helper pattern for Qwen3-VL.

  • Add constructor knobs such as:

    • max_prefill_graphs
    • max_decode_graphs
  • Add LRU eviction for prefill/decode graph buckets.

  • Add a public clear_graphs() or clear_graph_cache() API.

  • Expose graph-cache stats for debugging:

    • number of prefill graph buckets
    • number of decode graph buckets
    • current bucket keys
    • approximate memory use if available
  • Add optional warmup for common decode ranges.

  • Longer term, investigate position-agnostic decode graphs or lower-level
    cudaGraphExecUpdate, if compatible with the attention backend and PyTorch
    integration.

Scope

This is only about Qwen3-VL runtime graph-cache management. It should not
change model numerics or the BF16/FP8/NVFP4/INT8 kernel paths.

Activity

  1. LiangSu8899 commented on Jul 1, 2026

    @LiangSu8899
    Member

    Thanks! this is a very constructive suggestion.

    The full serving stack is still being built out, and this is exactly the kind of graph/runtime management it should include: bounded graph caches, cleanup/reset APIs, stats, and subgraph scheduling/management. I’ll update this soon.

  2. heiheiha798 commented on Jul 1, 2026

    @heiheiha798
    Contributor

    I opened PR #125 to address this: #125

    Scope covered in that PR:

    • bounded LRU graph caches for all current Qwen3-VL graph-cache owners:
      • Qwen3VlTorchFrontendRtx
      • Qwen3VlFp8Sm89TextFrontend
      • Qwen3VlFp8Sm89Frontend
    • clear_graphs() APIs
    • lightweight graph_cache_stats()
    • prefill graph eviction keeps staged/static input buffers in sync
    • CI-friendly regression coverage that does not require checkpoints or real CUDA Graph capture

    Validation included in the PR description. Real-checkpoint SM89/SM120 CUDA Graph replay smoke is noted as not run; the PR focuses on the cache-management bug itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions