Repository navigation
fix(qwen): mask native KV attention for FP16 and BF16 - #1035
Merged
chaofengw-nv merged 2 commits intoAug 26, 2026
Merged
Conversation
TensorRT 11.1 and 11.2 can apply key_value_lengths to the inactive cache suffix on SM86 and SM121, so bounded Qwen3 builds succeed but generate incorrect tokens. On affected BF16 targets, construct an explicit active-prefix causal mask, preserve IKVCacheUpdate and fused-only attention, and cap prefill chunks at 64 tokens. Other TensorRT releases, SMs, and FP16 remain unchanged. Refs NVIDIA#955 Signed-off-by: yifeif-nv <[email protected]>
Run affected FP16 models through a BF16 native attention and KV-cache boundary while preserving FP16 model projections and outputs. Version the mixed cache contract, retain v1 for same-precision bundles, and fail closed above the qualified cache capacity. Signed-off-by: yifeif-nv <[email protected]>
yifeif-nv
force-pushed
the
fix/qwen-native-kv-explicit-mask
branch
from
August 26, 2026 00:44
152ac4a to
19729fb
Compare
yifeif-nv
marked this pull request as ready for review
August 26, 2026 01:09
Closed
2 tasks done
This was referenced Aug 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
PR #959 fixed Qwen3 native-KV routing and precision plumbing, but it did not change the TensorRT attention input contract. FP16 still reached
IAttentionwith Q/K/V pluskey_value_lengths(four live inputs). On the affected TensorRT 11.1/11.2 kernels, that FP16 wrapper rejects the graph withExpected 3 live inputs, got 4.The same native length input has a separate BF16 failure mode on SM86/SM121: a bounded Qwen3 build can succeed but attend into the inactive fixed-cache suffix and silently generate wrong tokens.
This PR supplies a builder/runtime workaround for both supported native Qwen precisions while retaining fixed-capacity
IKVCacheUpdate, aliased cache storage, and fused attention.Refs #955 and #959.
Exit Criteria
Implementation
[1, 1, Sq, K]causal active-prefix mask and omitattention.key_value_lengthson affected SM86/SM121 + TensorRT 11.1/11.2 builds.native_kv_cache_dtype=bf16only when cache precision differs from model precision. The runtime resolves, allocates, and validates that declared dtype. Same-precision FP16/BF16 bundles remain contract v1.Rebuild with --max-cache-length 16384.Validation
Automated validation:
PYTHONPATH=python python3 -m pytest -q tests/e2e/models/qwen/test_qwen_builder_policy.py tests/builder/test_qwen_llama_native_kv_attention.py tests/builder/test_graph_ops_native_attn.py:76 passed. This covers policy, routing, real TensorRT topology, explicit-mask numerics, and graph ops. The numeric regression executes both pure BF16 and FP16-model/BF16-attention graphs with a poisoned inactive cache suffix and verifies the causal active-prefix result.PYTHONPATH=python python3 -m pytest -q tests/e2e/models/qwen/test_qwen_builder_engine.py:22 passed, including real FP16/BF16 x prefill/decode split engines../build-sm86/test_qwen_native_kv_cache && ./build-sm86/test_qwen_native_kv_cache_dtype && ./build-sm86/test_qwen_sampler && ./build-sm86/test_qwen_tensor_names: passed. The new dtype test covers v1 FP16/BF16 fallback, v2 explicit FP16/BF16 selection, and missing/invalid v2 declarations.git diff --check origin/main...HEAD: passed.SM86 + TensorRT 11.1.0.106 full-model FP16 validation:
Qwen/Qwen3-0.6B --precision fp16 --max-cache-length 16384successfully.Paris; plain prompt continued withParis. The capital of France is also._gemv_mha_v1and all 28 prefill layers using_gemm_mha_v2.--max-cache-length 16384guidance.Targeted SM86 TensorRT 11.1/11.2 attention probes also built and executed correctly with fused tactics. SM120/SM121 was not directly exercised for this PR; its applicability there is guarded by the same release/SM allowlist and covered by the topology and numeric regression tests.
Notes For Future Readers