[Performance]: XQA multi-block is disabled for FP8 KV on SM89, slowing small-batch decode

Abierto
#22,918 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@Shreyas652 ya está trabajando en esto.

Desde el 27/11/2026.

  • #23000 de @Shreyas652 — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp, python, pytorch

Línea de trabajo

Start in cpp/tensorrt_llm/thop/attentionOp.cpp at the guards in MHA::convertMMHAParamsToXQAParams and MLA::fillXqaParams, then read the related INT8 change in #22216 and report #22213. Check the XQA reference tests and run an op-level test plus racecheck for FP8 on SM89; the issue says these checks have not been run. Done means establishing whether the guard is needed for any configuration and, if it can be removed, validating correctness and the small-batch performance change.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

General perf
Proposal to improve performance

Summary

I was profiling Qwen3-8B on an RTX 4090 (SM89) and found an odd batch-size result with FP8 KV cache: the FP8 checkpoint is slower at batch 2 than at batch 1 or 4. At batch 1–2 it uses masked_multihead_attention_kernel instead of XQA.

The reason seems to be this guard in cpp/tensorrt_llm/thop/attentionOp.cpp, in both MHA::convertMMHAParamsToXQAParams and MLA::fillXqaParams (checked on main @ c4b868196):

if (xqaParams.kv_cache_data_type == DATA_TYPE_E4M3 && mSM < kSM_90)
{
    xqaParams.multi_block_mode = false;
}

With multi-block disabled, DecoderXQARunner::mayHavePerfGain uses num_kv_heads * batch_size * 4.0 >= SM count. Qwen3-8B has 8 KV heads and the 4090 has 128 SMs, so the heuristic rejects XQA below batch 4. I confirmed the MMHA fallback at batches 1 and 2; I have not measured batch 3.

For the NVFP4 checkpoint (BF16 activations, FP8 KV), MMHA is not supported, so it uses single-block XQA instead. That path gets noticeably slower at longer context lengths.

I removed the FP8/SM89 restriction at both call sites and rebuilt TensorRT-LLM v1.4.0rc4. On this GPU, small-batch decode improves, though there is a 2–3.4% regression in some short-context cases. I am not sure whether the FP8 restriction is still guarding a correctness issue.

Change tested

v1.4.0rc4 still has the INT8 guard from before #22216, so this is the diff I used at both sites:

-    if (xqaParams.kv_cache_data_type == DATA_TYPE_INT8
-        || (xqaParams.kv_cache_data_type == DATA_TYPE_E4M3 && mSM < kSM_90))
+    if (xqaParams.kv_cache_data_type == DATA_TYPE_INT8)
     {
         xqaParams.multi_block_mode = false;
     }

On main, #22216 already removed the INT8 part. Only the E4M3 restriction remains. This is related to the earlier INT8 report, #22213, but I have only tested FP8 KV on an RTX 4090.

Decode measurements

These are whole decode-step GPU times from Nsight Systems (--cuda-graph-trace=node), median over the steps in one capture per cell. The unmodified engine is nvcr.io/nvidia/tensorrt-llm/release:1.4.0rc4; the patched engine uses libraries rebuilt from tag v1.4.0rc4 in the matching devel container. Profiled time per token was within 1% of the unprofiled run.

Checkpoint Batch Context Default (ms) Patched (ms)
FP8 1 128 11.76 10.21
FP8 1 2048 11.78 10.32
FP8 1 3900 11.98 10.47
FP8 2 128 13.50 10.32
FP8 2 2048 13.60 10.64
FP8 4 128 10.28 10.53
FP8 4 2048 11.13 11.11
NVFP4 1 128 7.28 7.43
NVFP4 1 2048 8.14 7.59
NVFP4 1 3900 8.96 7.75
NVFP4 2 128 7.37 7.54
NVFP4 2 2048 8.21 7.91
NVFP4 4 128 7.41 7.66
NVFP4 4 2048 8.29 8.31

For the unmodified FP8 checkpoint, MMHA takes about 2.18 ms per step at batch 1 and 3.81 ms at batch 2, compared with about 0.40 ms for the XQA attention path at batch 4. Removing the guard avoids that fallback.

The NVFP4 result is different: it already uses XQA, but with a single block per KV head. In an earlier 1.4.0rc2 trace at batch 1, a single kernel_mha launch took 35.1 µs at context 2048 and 57.9 µs at 3900. With multi-block enabled, those became 17.8 µs and 22.2 µs. The FP8 checkpoint shows the same pattern when XQA is forced (33.7 / 56.5 µs single-block against 15.9 / 21.0 µs).

The short-context slowdown is also visible in the trace. Inside the decode CUDA graph, kernel_mha uses 16 blocks per sequence even at context 128, which is what computeMultiBlockCount gives for the configured max_seq_len of 4096 (I have not traced where the graph capture gets that length). Outside the graph it uses the actual context length. At context 128, a 16-block FP8 launch took 13.6 µs versus 10.4 µs with one block. BF16 KV already uses the same fixed 16-block grid under CUDA graphs, so this behavior is not specific to the patch.

I also repeated the default/patched profiles on 1.4.0rc2; corresponding decode-step times were within 0.06 ms of the rc4 values. The raw tables and build notes are here.

Checks so far

  • XQA reference tests: Built cpp/kernels/xqa from main @ 2c9473ea8b for SM89. RefCheck.llama_V2_70b and RefCheck.attention_sinks pass with multi-block on and forced off. Thirty consecutive multi-block runs passed with identical output hashes.

  • Small-batch kernel tests: I added local cases for 8 KV heads, batches 1/2/4, and contexts 128–4096, including HEAD_GRP_SIZE=4 and BF16 input. They passed the reference checks with multi-block on and off.

  • Serving/GSM8K: Ran all 1,319 questions, 5-shot greedy, through trtllm-serve on rc4. Strict-match accuracy:

    Checkpoint Default (two runs) Patched Correct/incorrect answers that flipped: default vs default Patched vs each default
    FP8 86.7% / 87.1% 87.0% 53 76 / 71
    NVFP4 85.8% / 86.6% 86.2% 38 47 / 43

The patched accuracy falls within the two default runs for each checkpoint, and the paired McNemar tests did not detect a difference (p >= 0.54). There are more per-question changes than between two default runs, especially for FP8. I have not isolated the source of those changes; a different attention reduction order is one possibility. These tests do not establish that enabling multi-block is safe for all FP8 configurations.

I have not run an op-level test for this change, compute-sanitizer racecheck, batch 3, a build of current main, or tests on other GPUs.

Question

Is the E4M3 && SM < 90 guard still required? #22213 describes the earlier INT8 multi-block restriction and the race fixes that allowed INT8 to be re-enabled. I don't know whether FP8 has a separate issue on SM80–SM89. If it does, could you point me to a reproducer or the affected configuration? (I have seen #17964, the SM89 XQA bug at head size 256 that was fixed in #17976. Qwen3-8B has head size 128, so these runs do not cover that path.)

If multi-block must stay disabled, would it make sense to adjust mayHavePerfGain for FP8 KV so that small-batch FP8 decode does not fall back to the much slower MMHA path? Forcing XQA in my earlier rc2 test helped at batch 1–2, although single-block XQA still slowed down with context length.

I'm happy to put together a PR and an op-level test if removing the restriction is acceptable.

Report of performance regression

No response

Misc discussion on performance

No response

Your current environment (if you think it is necessary)

System Information:

  • OS: Ubuntu 24.04.4 LTS (container nvcr.io/nvidia/tensorrt-llm/release:1.4.0rc4)
  • Python version: 3.12.3
  • CUDA version: 13.4 (nvcc 13.4.59)
  • GPU model(s): NVIDIA GeForce RTX 4090 (SM89, 128 SMs)
  • Driver version: 580.126.09
  • TensorRT version: n/a (PyTorch backend)
  • PyTorch version: 2.14.0a0+b2c75dd062.nv26.9
  • TensorRT-LLM version: 1.4.0rc4 (release container); patched libraries built from tag v1.4.0rc4 with scripts/build_wheel.py -a "89"

Setup and data:

  • TensorRT-LLM: release:1.4.0rc4; patched libtensorrt_llm.so, libth_common.so, libdecoder_attention_{0,1}.so, and Python bindings built from v1.4.0rc4 in the matching devel container with build_wheel.py -a "89"
  • Models: nvidia/Qwen3-8B-FP8, nvidia/Qwen3-8B-NVFP4 (Qwen3-8B, 8 KV heads, head dim 128)
  • KV cache: dtype: fp8, enable_block_reuse: false, host_cache_size: 0; CUDA graphs with padding enabled; NVFP4 using the Marlin GEMM backend
  • Scripts, trace summaries, run environments, and GSM8K output: llm-inference-bench, especially results/profile_rc4, results/profile_rc4_patched, results/gsm8k_rc4, and results/gsm8k_rc4_patched. The full .nsys-rep traces are reproducible with the scripts but are not committed.
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.
Lenguaje dominante
Python
Estrellas
15.2k
Forks
3k
Merge medio
2 d 21 h
PR fusionados (30 d)
477

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de NVIDIA/TensorRT-LLM

Todos los issues de NVIDIA/TensorRT-LLM

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.