[Feature]: Separate KV cache token caps for the self-attention and cross-attention pools

Open
#23,132 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start with _split_kv_cache_budget_for_cross in tensorrt_llm/_torch/pyexecutor/_util.py and the V2 manager in kv_cache/kv_cache_manager_v2.py; trace how each pool applies its byte budget and token cap. Then check the V1 split for compatibility and find the relevant tests and KvCacheConfig.max_tokens description. Done when an optional cross-attention token cap limits that pool while the existing setting and behavior remain intact when it is unset.

Written by the indexing model from the issue text.

Description

KV-Cache Management
The feature, motivation and pitch

An encoder-decoder model runs with two KV cache pools, one for decoder self-attention and one for cross-attention. KvCacheConfig.max_tokens is a single number and it caps both, under either KV cache manager. _split_kv_cache_budget_for_cross (tensorrt_llm/_torch/pyexecutor/_util.py) gives each pool a share of the memory budget and the same max_tokens. With V2, each pool's manager takes the smaller of its byte budget and the bytes that hold max_tokens (kv_cache/kv_cache_manager_v2.py). With V1, the split caps each pool's token count at the same value. The only setting that differs per pool is cross_kv_cache_fraction, which is a share of a memory budget and not a size.

The two pools need different token counts, and both needs are known in advance:

  • Cross-attention: a request holds its encoder output for its whole life. That is the encoder length, whatever the output length.
  • Self-attention: a request holds at most the decoder's maximum length, times the beam width.

Whisper is a clear case. Each request holds 1,500 tokens in the cross-attention pool and at most 448 in the self-attention pool. With greedy decoding and 64 concurrent requests, the pools need 96,000 and at most 28,672 tokens. max_tokens must be at least 96,000 to fit the cross-attention pool, so it cannot hold the self-attention pool to its real need. For whisper-large-v3-turbo in fp16, an even split then reserves about 1.8 GiB for a pool that can use at most about 0.55 GiB.

Request: an optional token cap for the cross-attention pool, for example cross_kv_cache_max_tokens.

  • When it is set, max_tokens caps the self-attention pool only.
  • When it is unset, behaviour stays as today.
  • Each pool holds the smaller of its own token cap and its share of the memory budget.
  • The max_tokens description says which pool it caps. Today it reads "The maximum number of tokens that should be stored in the KV cache" and does not say the cap applies to each pool.
Alternatives
  • max_gpu_total_bytes with cross_kv_cache_fraction. This gives each pool an absolute size, but in bytes and through a ratio. The user has to work out bytes per token for each pool, which depends on layer count, heads, dtype and KV quantization, and redo it when any of them changes. Token counts follow directly from the model and the concurrency.
  • free_gpu_memory_fraction with cross_kv_cache_fraction. The sizes then depend on the memory that is free at load, so they differ between replicas and with whatever else shares the GPU.
  • One max_tokens sized for the larger pool. This over-reserves the other pool, by about 3.3 times for Whisper.
Additional context
  • Code references are to main at 9f446f7.
  • V1 is deprecated as of 1.4, so the request is mainly for V2.
  • Related: #22105, which asks to report the cross-attention pool's statistics.
Before submitting a new issue...
  • Make sure you already searched for relevant issues, and checked the documentation and examples for answers to frequently asked questions.
Dominant language
Python
Stars
15.2k
Forks
3k
Avg merge
2d 21h
Merged PRs (30d)
477

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/TensorRT-LLM

All issues in NVIDIA/TensorRT-LLM

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.