Repository navigation
Commit f2a0c63
committed
fix(conversion): decompose attention over a single key that TensorRT 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 off by up to about half (BF16) or
holds inf (FP16 and FP32). FP16 and BF16 are wrong for head sizes 2 to 8, FP32
for head sizes 2 to 15. The query length does not matter, and with several
heads some of these shapes happen to come out right. Separately, an FP16
attention whose query and key both have length 1 does not build with a head
size of 32 or 64. 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 these shapes when the key length is known
to be 1: a head size below 16, or FP16 with a query length of 1 at any head
size, since only a few head sizes were measured there. A declined
scaled_dot_product_attention is decomposed into matmuls and softmax, which is
exact here. A declined flash, efficient or cuDNN attention runs in PyTorch, as
other declined cases of those ops already do. An FP32 attention cast from
FP16 inputs, which TensorRT runs as an FP16 layer, is decomposed in FP32 for
the FP16 shapes, like the one-row query case. They are declined on every
TensorRT version, like the shared bfloat16 query case, because the result is
correct either way. Other head sizes and keys longer than 1 keep the attention
layer.
test_single_key compiles these shapes and compares with PyTorch; each case
fails without this change. test_other_short_keys_are_kept checks that nearby
shapes still reach the attention layer.
Signed-off-by: Anthony Shoumikhin <anthony@shoumikh.in>1 parent c516939 commit f2a0c63
3 files changed
Lines changed: 126 additions & 1 deletion
File tree
- py/torch_tensorrt/dynamo
- conversion
- lowering/passes
- tests/py/dynamo/conversion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5635 | 5635 | | |
5636 | 5636 | | |
5637 | 5637 | | |
| 5638 | + | |
| 5639 | + | |
| 5640 | + | |
| 5641 | + | |
| 5642 | + | |
| 5643 | + | |
| 5644 | + | |
| 5645 | + | |
| 5646 | + | |
| 5647 | + | |
| 5648 | + | |
| 5649 | + | |
| 5650 | + | |
| 5651 | + | |
| 5652 | + | |
| 5653 | + | |
| 5654 | + | |
| 5655 | + | |
| 5656 | + | |
| 5657 | + | |
| 5658 | + | |
| 5659 | + | |
| 5660 | + | |
5638 | 5661 | | |
5639 | 5662 | | |
5640 | 5663 | | |
| |||
5724 | 5747 | | |
5725 | 5748 | | |
5726 | 5749 | | |
| 5750 | + | |
| 5751 | + | |
5727 | 5752 | | |
5728 | 5753 | | |
5729 | 5754 | | |
| |||
5830 | 5855 | | |
5831 | 5856 | | |
5832 | 5857 | | |
| 5858 | + | |
| 5859 | + | |
| 5860 | + | |
5833 | 5861 | | |
5834 | 5862 | | |
5835 | 5863 | | |
| |||
5927 | 5955 | | |
5928 | 5956 | | |
5929 | 5957 | | |
| 5958 | + | |
| 5959 | + | |
| 5960 | + | |
5930 | 5961 | | |
5931 | 5962 | | |
5932 | 5963 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
502 | 503 | | |
503 | 504 | | |
504 | 505 | | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
505 | 512 | | |
506 | 513 | | |
507 | 514 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| 21 | + | |
18 | 22 | | |
19 | 23 | | |
20 | 24 | | |
| |||
643 | 647 | | |
644 | 648 | | |
645 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
646 | 733 | | |
647 | 734 | | |
648 | 735 | | |
| |||
0 commit comments