Repository navigation
fix(dynamo): avoid TensorRT attention bugs with a masked one-row query or a single key - #5074
Draft
shoumikhin wants to merge 3 commits into
Draft
shoumikhin wants to merge 3 commits into
shoumikhin wants to merge 3 commits into
Conversation
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
from
November 18, 2026 19:27
6e588d2 to
3e6e9cd
Compare
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
3 times, most recently
from
November 23, 2026 20:40
f806fba to
2e570df
Compare
shoumikhin
marked this pull request as draft
November 23, 2026 21:54
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
from
November 25, 2026 06:11
2e570df to
f2a0c63
Compare
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
2 times, most recently
from
November 25, 2026 23:14
9cd07c4 to
5cca30d
Compare
TensorRT's attention layer drops the mask when the query length is fixed at 1 and the key is long. The output is attention over every key, as if no mask were set. A causal mask is dropped the same way. TensorRT 11.2, 11.3 and 11.4 all drop it. With 11.4 the output is wrong in FP16 from 513 keys and in BF16 from 57344 keys, the same on an A100, a Jetson Thor and a Jetson Orin. A query with 2 or more rows is masked correctly. TensorRT-RTX 1.7 masks correctly. This is the shape of a decode step over a fixed size KV cache, so such a step attends to the cache slots that are not written yet. decompose_unsupported_attention now gives such an attention more query rows before it reaches the attention layer, on the TensorRT versions above. This covers scaled_dot_product_attention and the efficient and cuDNN attention ops: - With grouped-query attention and no causal mask, the query heads that share a key head become the query rows of one attention per key head. A mask with one entry per query head is regrouped the same way. Each key and value head is still read once. - Otherwise the query row is repeated and the first output row is kept. The result is copied to the memory order the original attention returns, so a reader of its strides, such as as_strided after a transposed query, gets the same values where PyTorch runs the graph. The attention layer, its scaling and its handling of the mask stay as they were. Only 16-bit attention with a mask or a causal mask, a query length fixed at 1 and a key that can be longer than 512 (FP16) or 32768 (BF16) is changed. Shorter keys, a query length that is not fixed (masked correctly when it runs at 1), fused FP8 attention and FP32 attention (apart from the case below) are not. Not covered: an engine with several optimization profiles of which one fixes the query length at 1 (the pass sees one dynamic query length), and an FP32 efficient attention cast from 16-bit inputs that has both a bias and a causal mask (the decomposition takes only one of them; it was wrong before too). The FP16 limit is sharp: 512 keys were right and 513 wrong for every head size from 32 to 256, head count and batch measured. BF16 keys from 32769 to 55296 were still correct. They are changed too, as a margin. In the shapes measured that made them faster (40000 keys, 32 query heads: 1.58 ms -> 0.27 ms with 8 key heads, 1.63 ms -> 0.91 ms with 32), but the FP32 case below is about 1.4x slower there. Shorter keys are left alone, because more query rows were slower there: up to 1.2x in FP16, and up to 1.4x in BF16 at 4096 keys. An FP32 attention whose inputs are cast from FP16 or BF16 can be run by TensorRT as a 16-bit layer again, so it drops the mask too. More query rows would sit between the casts and the layer, so such an attention is decomposed in FP32 instead, as the pass already does where that layer fails to build. Where PyTorch runs the FP32 decomposition, it now also works when a dynamic key is empty. Export takes a dynamic key to be longer than 1, so amax was traced in and failed on zero keys where PyTorch returns zeros. For a dynamic key it now checks with any. A compiled engine never sees zero keys, because its profile starts at 1. The correct result costs more than the wrong one, because the mask is now applied. Measured on an A100 in FP16, head size 64, with CUDA graph replay: - 32 query heads, 2 key heads, 16384 keys: 0.06 ms -> 0.42 ms per attention. - 32 heads, 4096 keys: 0.03 ms -> 0.24 ms, about 7x. Other timing methods gave smaller ratios, down to about 4x. Decomposing the attention into matmuls and softmax was 1.2x to 2.9x faster in the shapes measured. It is not used for 16-bit attention because in FP16 it can overflow, while the attention layer keeps its own scaling. Each new test fails when the part of this change it covers is removed. A version table test pins which TensorRT versions the version check accepts. test_single_query_mask and the causal cases also check the rewritten graph against the original op, since run_test uses that graph as its reference. test_single_query_mask_compiled compiles BF16, the FP32 cast case and a dynamic key length with torch_tensorrt.dynamo.compile. Signed-off-by: Anthony Shoumikhin <[email protected]>
…re_compatible TensorRT 11.4 drops the mask of a one-row query only when the head size is a multiple of 8. Other head sizes are already masked correctly, and TensorRT cannot build them with 2 query rows or with grouped heads folded into rows, so the rewrite made models with head sizes such as 12, 20, 36, 44, 52 or 100 fail to compile. The rewrite now runs only for head sizes that are a multiple of 8. With hardware_compatible=True, TensorRT 11.4 drops the mask of a one-row BF16 query from 8192 keys (7680 keys were still correct, on an A100 and an H100), while the pass acted only above 32768. The key limits now depend on hardware_compatible, with a BF16 limit of 4096 there. The FP16 edge is the same. test_single_query_mask_compiled adds head size 100, BF16 and FP16 with hardware_compatible, and the FP32 efficient attention cast from FP16, and passes a non-default scale to the rewritten attention. The lowering test checks the key just past and right at each limit. Each case fails without its fix. Head size 100 also passes on the base branch, where there is no rewrite: it guards the head size check. Comments now say why the other outputs block the rewrite, when grouped heads are folded, and which ops need the expanded bias. Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
2 times, most recently
from
November 26, 2026 20:23
93968a2 to
c38992e
Compare
…gets wrong TensorRT 11.4's attention layer returns wrong values when the key length is 1 and the head size is small. With one key the softmax is 1, so the right answer is the value itself. Instead the output is far off or holds inf. FP16 and BF16 are wrong for head sizes 2 to 8, FP32 for head sizes 2 to 15, for every query length and head count, with one exception: a one-row query with a head size of 8 is right with 4 or more heads in all (batch times heads). Separately, an FP16 attention whose query and key both have length 1 does not build with a head size of 32 or 64 and fewer than 4 heads in all. A network built with the TensorRT Python API alone fails the same way, so this is a TensorRT bug. Seen on an A100 and an H100. The attention validators now decline exactly these shapes when the key length is known to be 1. A dynamic batch or head count counts as 1 for the heads in all, because the engine's shape range can start there, while export records a dynamic batch as at least 2. A float64 attention counts as FP32 with truncate_double, because TensorRT then runs it in FP32. The pass decomposes a declined scaled_dot_product_attention, efficient or cuDNN attention into matmuls and softmax in FP32, and casts the result back. A float64 one stays in float64. In FP16 the product of the query and the one key can overflow, and in 16 bits the softmax of a hidden key is NaN, where PyTorch gives the value and zeros. FP32 avoids both. Decomposing also keeps efficient attention with a bias in TensorRT. Left to PyTorch, its kernel rejects a bias that comes out of an engine with unaligned strides, and with require_full_compilation=True the model would no longer compile. Efficient and cuDNN attention whose other outputs are read, or that has both a bias and is_causal, cannot be decomposed and is left to PyTorch, as is declined flash attention. For those, a shape that gave wrong values now raises an error with require_full_compilation=True, and an efficient attention with such a bias can fail when called. An FP32 attention cast from FP16 inputs, which TensorRT runs as an FP16 layer, is decomposed in FP32 for the FP16 shapes. These shapes are declined on every TensorRT version, like the shared bfloat16 query case, except on TensorRT-RTX targeting Turing. That target cannot run the FP32 decomposition, so a 16-bit attention keeps its layer there, as before. Whether TensorRT-RTX has this bug was not measured. Other shapes keep the attention layer. PyTorch returns attention in a memory order of its own, such as that of a transposed query, and as_strided reads it, also after a clone or an elementwise op. The decomposition returns a contiguous tensor, so every decomposed attention whose output is not contiguous now copies its result back into that order. This also fixes the attention that the pass already decomposed before. test_single_key compiles these shapes with require_full_compilation=True and compares with PyTorch, including a query and key whose product overflows FP16, a hidden key, and efficient and cuDNN attention with a bias. Each case fails without this change. test_short_keys_validator checks that the plain and the efficient attention validators keep nearby shapes, a one-row query with a head size of 8 and 4 heads in all (also in FP32) and FP32 head sizes of 1 and 16, and decline FP32 head sizes of 2 and 15, FP16 one-row queries with fewer than 4 heads in all, also with a dynamic batch, and a two-row query with a head size of 8 and 4 heads in all. With TensorRT-RTX targeting Turing mocked, they keep every 16-bit shape. Lowering tests check that a float64 attention is decomposed in float64 with truncate_double, that a decomposed attention keeps its memory order for as_strided, and that the attention the decomposition cannot compute is kept. Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin
force-pushed
the
single-query-mask-decompose
branch
from
November 26, 2026 21:40
c38992e to
d4ffcdc
Compare
This branch has not been deployed
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.
What is wrong today
TensorRT 11.4 (11.2 to 11.4 for the first) gets these attention cases wrong:
hardware_compatible=True).What this change does
decompose_unsupported_attentiongives a masked one-row attention more query rows. Query heads that share a key head become its rows; otherwise the row is repeated. Only on affected versions, past the key limit.The validators decline exactly the failing single-key shapes. The pass decomposes those
scaled_dot_product_attention, efficient and cuDNN attentions in FP32 (float64 stays float64). Declined flash attention, and efficient or cuDNN attention that reads other outputs or has a bias and a causal mask, run in PyTorch.FP32 attention on 16-bit casts, which TensorRT runs in 16 bits, is decomposed in FP32 in both cases.
A decomposed attention keeps PyTorch's memory order, so
as_stridedreads the same values.Cost
A rewritten masked decode attention is 4x to 7x slower (A100, FP16).
Not covered
is_causal, is a follow-up.Tests
hardware_compatible, FP32 cast and dynamic key; fully compiled single-key attention with FP16 overflow, a hidden key and a bias.Both files pass on an A100 with TensorRT 11.4. Each new test fails without its fix, except head size 100.