Skip to content

[Bug]: MLA generation-only thop.attention calls size the workspace by the context requests' total KV length (OOM on mixed steps with long cached contexts) #20506

Closed

Description

@lucifer1004

System Info

  • TensorRT-LLM main @ e88d19c, PyTorch backend, TrtllmAttention with fp8 KV cache
  • MLA model, TP8, chunked prefill, KV cache reuse (prefix cache hits)
  • Seen on SM107. The bug is in host-side workspace sizing and does not depend on the GPU.

Who can help?

Related: #15000 (threads ctx_total_kv_len into getWorkspaceSize), #16399 / #19269 (KV estimator reserve for the fp8 context-MLA workspace). #17558 restructures this code.

Reproduction

Any mixed step (context and generation requests in the same batch) on an MLA model with fp8 KV cache, where the MLA generation half runs through thop.attention. On main this is every mixed step, because the CuTe DSL MLA decode path declines batches with num_contexts != 0 (attention/backends/fmha/cute_dsl_mla.py:450) and falls back.

  1. MLA.forward_context_with_chunked_prefill (attention/mla.py:1312) saves host_total_kv_lens[0], sets it per chunk, and restores it before returning (mla.py:1445). After the context half, host_total_kv_lens[0] is again Σ(KV length of all context requests in the step), cached prefix included.
  2. The generation half then calls thop.attention with attention_input_type=generation_only (mla.py:1765) on the same metadata. In cpp/tensorrt_llm/thop/attentionOp.cpp, is_gen_only (:1478) only changes num_gen_tokens (:1482). The workspace call at :1526-1528 still passes num_contexts, num_ctx_tokens and ctx_total_kv_len = host_total_kv_lens[0] (:1483). The context run is skipped for generation-only calls (:1553).
  3. getWorkspaceSize returns max(context, generation) (:428-433). For fp8 context MLA with separate Q/KV input, the context part includes the K/V staging buffers sized by kv_buf_tokens = max(total_kv_len, chunk_buf_batch × max_num_tokens) (cpp/tensorrt_llm/common/attentionOp.cpp:859-881).
  4. The generation call goes through the mqa op, which is built with v_head_dim = kv_lora_rank (mla.py:598-615). Its staging cost is therefore H_local × (qk_nope + qk_rope + kv_lora_rank) bytes per token, which in our configuration is 8448 B/token.

So every generation-only MLA call in a mixed step sizes a workspace that grows linearly with the step's total context KV length, for a buffer it never uses:

workspace ≈ 8448 B × Σctx_kv + (generation workspace)

We measured this on a single MLA layer of one model at TP8, with N context requests (1 new token each, L cached tokens) plus 1 generation request, calling generation_only. The allocated workspace was 8448 B × Σctx_kv + 33.6 MB at every point, 22 cases in total, all on the line to within a few hundred bytes:

context requests × cached length Σctx_kv workspace
0 0 50.4 MiB
1 × 8K 8,193 98 MiB
16 × 32K 524,304 4.16 GiB
8 × 128K 1,048,584 8.28 GiB
13 × 128K 1,703,949 13.44 GiB
16 × 1M (shared prefix) 16,777,232 132.0 GiB

With host_total_kv_lens[0] set to 0 for the call, every case drops to 50.4 MiB, which is what the generation itself needs.

In serving, 13 concurrent 128K prefix-cache hits plus one decoding request asked for 13.44 GiB (Attention workspace size is not enough, increase the size from 7783958528 bytes to 14427068672 bytes) and ran out of memory. Two things make this worse:

  • The thop workspace only grows (resize_), so the old block is still held while the new one is allocated, and the peak is old + new. The high-water mark then stays for the life of the process.
  • With KV reuse, Σctx_kv is not bounded by the KV pool. Requests that share a long prefix all count it in full, so 16 requests sharing a 1M prefix ask for about 132 GiB.

The KV estimator reserve does not cover this either: runtime_workspace_bytes_per_token uses config.v_head_dim, so it reflects the mha op, not the mqa op.

Expected behavior

A generation-only thop.attention call sizes only the generation workspace: about 50 MB here, independent of the context requests in the step.

Actual behavior

It allocates 8448 B × Σctx_kv extra and can OOM on mixed steps with long cached contexts.

Additional notes

Suggested fix: in thop/attentionOp.cpp, pass zero context counts for generation-only calls:

int64_t const workspace_size = runner->getWorkspaceSize(*op, is_gen_only ? 0 : num_contexts,
    is_gen_only ? 0 : num_ctx_tokens, max_context_q_len, maxCrossKvLength, num_generations,
    max_attention_window_size, num_gen_tokens, max_blocks_per_sequence, is_gen_only ? 0 : ctx_total_kv_len);

getWorkspaceSizeForContext returns 0 when num_ctx_tokens == 0. As far as I can see, the generation run carves its buffers from the start of the workspace and does not read ctx_total_kv_len. An equivalent Python-side workaround is to set host_total_kv_lens[0] = 0 around the generation call in MLA.forward_absorption_generation when num_contexts > 0, and restore it afterwards, as forward_context_with_chunked_prefill already does for its chunks.

What is verified so far: the sizing numbers above. I have not yet run an end-to-end check that outputs are unchanged with the fix; my single-layer probe could not run the generation kernel in isolation. From a quick read of #17558, its getAttentionWorkspaceSize derives the context token count from num_tokens − num_gen_tokens, which may avoid this; I have not verified that.

I have the one-line fix on a branch and can open a PR.

Activity

  1. added
    Inference runtime
    Pytorch
    on Oct 17, 2026
  2. added 3 commits that reference this issue on Oct 18, 2026
  3. lucifer1004 commented on Oct 30, 2026

    @lucifer1004
    CollaboratorAuthor

    Fixed by #17558. The staged thop.attention refactor rewrote attentionOp.cpp, and a generation-only call now sizes its workspace with the context part counted as 0, so the context requests' summed KV length no longer inflates it. Closing as completed; #20520 is closed as no longer needed.

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

    Inference runtimePytorch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions