Repository navigation
Description
Proposal to improve performance
Summary
Enabling multi-block for an INT8 KV cache makes a single XQA kernel launch at a 96k context 7.6× faster on A10G (7.62 → 1.00 ms) and 13.7× faster on H200 (2.80 → 0.20 ms) (main, spec-dec, batch 1). In an end-to-end serving test on A10G, TPOT at a 90k context drops from 77.6 ms to 25.4 ms.
XQA multi-block mode is unconditionally disabled when the KV cache is INT8:
cpp/tensorrt_llm/thop/attentionOp.cpp (main @ c868288566, two places: L534 and L3217)
if (xqaParams.kv_cache_data_type == DATA_TYPE_INT8 // <- here
|| (xqaParams.kv_cache_data_type == DATA_TYPE_E4M3 && mSM < kSM_90))
{
xqaParams.multi_block_mode = false;
}Because of this, decode with an INT8 KV cache runs with one CTA per (sequence, KV head) regardless of context length. On long contexts the generation step is about 2× slower than with a BF16 KV cache (TPOT 48.4 ms vs 22.0 ms at 46k), even though the INT8 cache reads half the bytes.
As far as I can tell, the multi-block split and merge in mha.cu (allowMultiBlockMode, nbSubSeqPerSeq) do not depend on the cache element type. 8-bit caches take a different V-load path (GRP_LOAD_V), which is what the race fixes below touched. The standalone XQA test (cpp/kernels/xqa/test/test.cpp) also always runs with the multi-block sub-sequence count and supports CACHE_ELEM_ENUM=1. There is no comment explaining the INT8 exclusion.
History (my reading of git log)
035b99e0d0(Update TensorRT-LLM #1427, 2024-04) disabled XQA multi-block completely with// WAR for bug 4565783. Real fix is blocked and will be integrated later.66ef1df492(Update TensorRT-LLM #1492, 2024-04) lifted the global disable and narrowed it toINT8 || (E4M3 && sm != 90). The INT8 part has not been revisited since. It moved toattentionOp.cppin16d2467ea8(Update TensorRT-LLM #2755) and now lives inthop/attentionOp.cpp.6f7a13c739([None][perf] SM120 optimizations for Qwen3.6-35B-A3B-NVFP4 #17976) re-enabled FP8 multi-block on SM120. In the same change it fixed a multi-block race inmha.cuwhere a non-last CTA could write its unmerged partial output.26b57136ee([https://nvbugs/6530268][fix] Fix XQA race condition and attn sink numerical accuracy #18141) fixed further shared-memory reuse andldgstsraces in the same path. These are the same 8-bitmha.cucode path that INT8 uses.
Question to maintainers: is the INT8 exclusion still needed, or is it a leftover of the 2024 workaround? If there is a known remaining issue (bug 4565783 or otherwise), could you share the conditions so I can test against them?
Environment
- GPUs: NVIDIA A10G (SM86, driver 570) and H200 (SM90)
- Model shape: head_dim 256, 24 Q heads / 4 KV heads (GQA 6), paged KV with 64 tokens per page, BF16 activations, INT8 KV cache with a per-layer scale, linear speculative decoding (q_seq_len 3, MTP)
- Reproduced on
v1.3.0rc24; kernel re-verified onmain@c868288566(kernel outputs are bit-identical between the two).
Evidence: removing the INT8 clause on SM86 and SM90
- Kernel accuracy: multi-block ON vs OFF changes the error against an FP32 reference the same way for INT8 and BF16 caches (relL2 0.17% → 0.23%). 0 NaN, 0 non-bit-exact reruns, 0 semaphores left non-zero over 283 configurations on SM86 and the same matrix on SM90.
compute-sanitizermemcheck / initcheck: 0 errors. - Upstream XQA unit tests (
test.cpp) built for an INT8 cache pass with multi-block ON on SM86 and SM90. - The new op-level test fails 6/6 without the change and passes 6/6 with it (A10G and H200).
- Serving on A10G: TPOT at 46k 48.4 → 22.7 ms (BF16 KV: 22.0 ms). Decode-path token agreement is the same as with a BF16 KV cache. 926 requests, 0 errors.
Details below.
1. Kernel-level accuracy and time on SM86 (standalone mha.cu)
Compared against an FP32 reference on the dequantized cache. Same inputs with multi-block OFF (1 sub-seq) vs ON (runtime heuristic, and forced 16/64 sub-seqs). Contexts 65 to 96253 (including partial pages and empty sub-sequences), seeds 1–3, batch 1 and batch 4 with mixed lengths, both spec-dec (q_seq_len 3) and plain decode.
| cache | OFF relL2 | ON relL2 | NaN | non-determinism (300 reruns) |
|---|---|---|---|---|
| INT8 | 0.17% | 0.23% | 0 | 0 (bit-identical, semaphores return to 0) |
| BF16 (control) | 0.17% | 0.23% | 0 | 0 |
The small increase with multi-block ON (~√2) comes from the extra BF16 rounding of per-sub-sequence partials. It shows up equally with a BF16 cache, so it is not INT8-specific.
Re-run on main @ c868288566 with the same standalone harness (mha.cu included directly, compiled with the same macros the XQA NVRTC path passes for this config, launched with the runtime's grid). 283 configurations: contexts 65–96253, seeds 1–3, batch 1 and batch 4 with mixed lengths, spec-dec (q_seq_len 3) and plain decode, INT8 and BF16 caches, 20 reruns each (300 for the race-hunt subset), plus stress inputs (near-uniform and small-magnitude INT8 values; same OFF/ON pattern, not in the table).
| cache (main) | OFF relL2 | ON relL2 (runtime / forced 16, 64 sub-seqs) | NaN | non-bit-exact reruns | semaphores left non-zero |
|---|---|---|---|---|---|
| INT8, spec-dec | 0.170–0.177% | 0.209–0.241% | 0 | 0 | 0 |
| INT8, plain decode | 0.169–0.179% | 0.228–0.240% | 0 | 0 | 0 |
| BF16 (control), spec-dec | 0.170–0.180% | 0.219–0.239% | 0 | 0 | 0 |
- Outputs on
mainare bit-identical tov1.3.0rc24and tov1.3.0rc24+6f7a13c739+26b57136eefor all 192 dumped INT8 outputs (attention sinks are not used here, so07dbde4823does not change results). - Kernel time on
main(single kernel launch, INT8, spec-dec, batch 1; CUDA events, L2 flushed before each run, median of 40 runs after 3 warmup runs): 32k 2.63 → 0.37 ms, 64k 5.22 → 0.70 ms, 96k 7.62 → 1.00 ms (OFF → ON with the runtime heuristic, 8 sub-seqs).mainis ~5% faster than rc24 in both modes; the two race fixes do not change the time. compute-sanitizermemcheck / initcheck: 0 errors with multi-block ON.
2. Upstream XQA unit tests built for INT8 on SM86
Upstream XQA unit tests (cpp/kernels/xqa/test/test.cpp), built for SM86 from main @ c868288566, kernel and test sources unmodified. On non-Hopper GPUs the test always sizes the grid with computeNbSubSeqPerSeqMHA, i.e. multi-block ON; I confirmed with nsys that RefCheck.llama_V2_70b and RefCheck.attention_sinks launch with 3, 10 and 16 sub-sequences in some of their cases.
build (-D in both CMAKE_CXX_FLAGS and CMAKE_CUDA_FLAGS) |
RefCheck.llama_V2_70b |
RefCheck.attention_sinks |
|---|---|---|
CACHE_ELEM_ENUM=1 (INT8 cache, FP16 I/O) |
PASS | PASS |
CACHE_ELEM_ENUM=1 INPUT_FP16=0 (INT8 cache, BF16 I/O) |
PASS | PASS |
CACHE_ELEM_ENUM=1, XQA_NB_SUB_SEQ=1 (multi-block forced OFF) |
PASS | PASS |
CACHE_ELEM_ENUM=0 (FP16 cache, control) |
PASS | PASS |
CACHE_ELEM_ENUM=0 INPUT_FP16=0 (BF16 cache, control) |
PASS | PASS |
Spec-dec build (SPEC_DEC=1 IS_SPEC_DEC_TREE=0 SLIDING_WINDOW=1): RefCheck.gpt_oss_spec_swa_rows and RefCheck.sliding_window_invalid_prefix_pages PASS for both INT8 and FP16 caches. RefCheck.llama_V2_70b_3 passes all its reference comparisons, then hits cudaErrorIllegalAddress in its last call runTest<1, HEAD_GRP_SIZE, 62>(8, 2, ...) with both INT8 and FP16 caches; there q_seq_len (62) > seqLen (2) and the guarding assert is compiled out in Release, so this looks like a test issue unrelated to the cache type.
Notes on building the test for INT8 on SM86 (I did not change any kernel or test source):
cpp/kernels/xqa/CMakeLists.txtsetsCMAKE_CUDA_ARCHITECTURES 89-real 90a-realafterproject(), so-DCMAKE_CUDA_ARCHITECTURES=86-realhas no effect.unitTestsalways compilesmha_sm90.cu, which has no INT8 path (SharedMem::OutSwizzleBufis only defined forCACHE_ELEM_ENUM0 and 2), so-DCACHE_ELEM_ENUM=1fails to compile. WithSPEC_DEC=1it also failsstatic_assert(smemSize < kMAX_SMEM_SIZE)when compiled for SM86.- I therefore used a small wrapper CMakeLists with the same flags and sources,
86-real, andmha_sm90.cureplaced by a stub that aborts if called (launchHopperF8MHAis only reached whenprop.majoris 9 or 10). Happy to share it or to send a separate fix for the test build.
3. Hopper (SM90, H200)
On SM90 the runtime still routes INT8-KV generation to the generic HMMA kernel (mha.cu; supportConfigQGMMA excludes INT8), JIT-compiled with -arch=sm_90a. I rebuilt the same standalone harness with -arch=sm_90a and the same macros, and ran the same matrix on an H200 (132 SMs, main @ c868288566, nvcr.io/nvidia/tensorrt-llm/release:1.3.0rc24, nvcc 13.2). The runtime heuristic picks more sub-sequences here (spec-dec batch 1: 16 instead of 8 for contexts ≥ 64k; batch 4: 4 instead of 2).
H200, main |
OFF relL2 | ON relL2 (runtime / forced 16, 64) | NaN | non-bit-exact reruns (20, or 300 for race hunt) | semaphores left non-zero |
|---|---|---|---|---|---|
| INT8, spec-dec (batch 1 and 4) | 0.170–0.177% | 0.210–0.240% | 0 | 0 | 0 |
| INT8, plain decode | 0.169–0.179% | 0.228–0.240% | 0 | 0 | 0 |
| BF16 (control), spec-dec / decode | 0.170–0.180% | 0.216–0.241% | 0 | 0 | 0 |
- On SM90,
mainoutput is bit-identical tov1.3.0rc24and tov1.3.0rc24+6f7a13c739+26b57136ee(324 dumped INT8 outputs). It is not bit-identical to the SM86 output (differences ≤ 1 bf16 ulp). This is expected:mha.cuuses different K/V tile sizes on__CUDA_ARCH__ == 900. Both are equally close to the FP64 reference. - Kernel time (single kernel launch, INT8, spec-dec, batch 1, OFF → ON with the runtime heuristic): 32k 0.96 → 0.14 ms, 64k 1.91 → 0.14 ms, 96k 2.80 → 0.20 ms (13.7×). Plain decode at 96k: 2.58 → 0.15 ms.
- Upstream unit tests on SM90: for an INT8 cache
test.cpppicks the HMMA kernel by itself (useQGMMAis only true for FP8/FP16/BF16 caches on SM90), the same kernel the runtime uses.RefCheck.llama_V2_70bandRefCheck.attention_sinksPASS for INT8 cache with FP16 or BF16 I/O, with multi-block ON (nsys shows 3 and 16 sub-sequences) and forced OFF (XQA_NB_SUB_SEQ=1). FP16/BF16 controls PASS on both the HMMA kernel (XQA_USE_QGMMA=0) and the Hopper kernel (test default). In the spec-dec build,RefCheck.gpt_oss_spec_swa_rowsandRefCheck.sliding_window_invalid_prefix_pagesPASS (INT8 and FP16, HMMA).RefCheck.llama_V2_70b_3fails at the same call as on SM86 with both cache types. - Building note: on SM90 the upstream CMake architecture list is fine.
-DCACHE_ELEM_ENUM=1still fails to compile inmha_sm90.cu(OutSwizzleBuf), so INT8 builds use the same stub in place ofmha_sm90.cu. Kernel and test sources are unmodified.
Op-level unit test on SM90: tests/unittest/_torch/attention/test_trtllm_int8_kv_multiblock.py, run with cd tests && pytest unittest/_torch/attention/test_trtllm_int8_kv_multiblock.py -v on main @ c868288566 built with scripts/build_wheel.py -a 90 in nvcr.io/nvidia/tensorrt-llm/devel:1.4.0rc3 on an H200: 6 passed with the change (3 runs), 6 failed without it (3 runs). Without the change, INT8 plain decode goes to MMHA and INT8 spec-dec to XQA with one sub-sequence. With the change, both INT8 and the BF16-cache control use the HMMA XQA kernel (kernel_mha, block (128,1,2)) with 4–45 sub-sequences. The counts are SM-count dependent (e.g. spec-dec 8k: 4 on H200 vs 8 on A10G), but the test only asserts grid.x > 1, so it needs no SM-specific change.
4. End-to-end serving on SM86 (A10G)
The server is v1.3.0rc24 with local patches that enable INT8 KV serving in the PyTorch backend (not part of this proposal). The "ON" build adds this change and backports the two race fixes 6f7a13c739 and 26b57136ee. PyTorch backend, concurrency 1, temperature 0, 400 output tokens, MTP on. No end-to-end test was run on H200; the SM90 evidence comes from the kernel harness, the XQA unit tests and the op-level test, all built from main @ c868288566 without these local patches.
| context (prompt tokens) | INT8 KV, multi-block OFF | INT8 KV, ON | BF16 KV (reference) |
|---|---|---|---|
| 46,142 | 48.4 ms | 22.7 ms | 22.0 ms |
| 89,902 | 77.6 ms | 25.4 ms | n/a (does not fit) |
- Decode-path agreement: with this change, greedy decode tokens re-scored by prefill (teacher forcing) match at 437/440 positions (99.32%). The BF16 KV server gives the same 99.32%. 440 positions from 5 requests, contexts 0.5k–12k tokens; I did not measure this before the change.
- MTP acceptance length 2.89–2.95 (2.9 before); needle retrieval at 64k / 90k / 94k correct.
- Stability: 926 requests with 0 errors; no XQA, NVRTC, illegal-address or semaphore errors in the logs.
Proposal
Remove only the DATA_TYPE_INT8 || clause in both places of thop/attentionOp.cpp. INT8 KV generation uses the generic mha.cu kernel on SM80–SM90; the Hopper QGMMA kernel does not accept INT8. So the change affects Ampere, Ada and Hopper. Keep the FP8 / SM<90 condition unchanged, since I have not tested it.
I am happy to open the PR with the test results above. I tested on SM86 (A10G) and SM90 (H200), so guidance on which other architectures and test lists you would want covered is welcome.
Note
On main the PyTorch backend does not accept kv_cache_config.dtype="int8" (_VALID_KV_CACHE_DTYPES); INT8 KV is reached through a checkpoint that declares kv_cache_quant_algo=INT8 with dtype="auto". This issue is only about the multi-block gate, not about adding that option.
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/devel:1.4.0rc3) - Python version: 3.12.3
- CUDA version: 13.4 (nvcc 13.4.59)
- GPU model(s): NVIDIA A10G (SM86); NVIDIA H200 (SM90)
- Driver version: 570.211.01 (both)
- TensorRT version: n/a (PyTorch backend)
- PyTorch version: 2.14.0a0+b2c75dd062.nv26.09
- TensorRT-LLM version: main @ c868288 (1.4.0rc4 dev), built from source with
scripts/build_wheel.py -a 86/-a 90
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.
Activity
[#22213][perf] Enable XQA multi-block mode for INT8 KV cache (#22216)