Repository navigation
Commit 8734b06
authored
[CUDA] Use Triton TMA for long-context causal prefill (pytorch#22414)
## Stack dependency
Depends on pytorch#22190. This PR supersedes pytorch#22193 after reordering the stack
so TMA sits directly on the split-K baseline and the optional FP8 linear
path sits above TMA.
## Summary
Use Triton tensor descriptors/TMA for long-context global causal
attention on SM90+ GPUs.
- Dispatch from semantic and hardware properties rather than a model
identity: global causal attention, a device-resident KV bound,
sufficiently long KV context, and prefill-sized queries.
- Select the optimized path from the requested CUDA target, honoring
explicit `TORCH_CUDA_ARCH_LIST`, excluding ROCm, and avoiding CUDA
context creation at module import.
- Support common transformer head dimensions 64 and 128 with separately
profiled tile, stage, and warp configurations.
- For D=128, select a smaller tile for 512-1023 query rows and a larger
tile at 1024+ rows.
- Use real output strides and keep non-unit inner-stride inputs on the
existing portable Triton path.
- Preserve finite outputs for fully masked rows with guarded
online-softmax normalization.
- Keep unsupported or non-beneficial shapes on the existing portable
Triton path; head dimension 256 currently falls back because tested TMA
configurations were slower.
- Preserve online-softmax numerical stability; this stack contains
neither PDL nor the former `phi=5` approximation.
The path is selected automatically inside the SDPA dispatcher. Eligible
prefill shapes use TMA when the AOTInductor compile target is SM90+;
decode, ROCm, pre-SM90 targets, unsupported layouts/head dimensions,
explicit masks, and non-beneficial shapes retain the portable Triton
path. The implementation remains pure Triton and is not tied to
Muse-Glimmer.
## Operator-level performance
RTX 5090, BF16 global causal GQA (`B=1, Hq=16, Hkv=2`), TMA versus the
existing Triton kernel:
| Head dim | Lq | Lkv | Existing | TMA | Speedup |
|---:|---:|---:|---:|---:|---:|
| 64 | 512 | 16,384 | 0.439 ms | 0.268 ms | 1.64x |
| 64 | 4,096 | 131,072 | 13.153 ms | 11.794 ms | 1.12x |
| 128 | 512 | 16,384 | 0.632 ms | 0.534 ms | 1.18x |
| 128 | 1,024 | 131,072 | 7.847 ms | 6.750 ms | 1.16x |
| 128 | 4,096 | 131,072 | 29.212 ms | 27.039 ms | 1.08x |
The latest post-rebase exact-stack A/B attributes the long-context gain
directly to this PR: relative to pytorch#22190, prefill improves from 2,971.25
to 3,152.94 tok/s at 32K (+6.11%) and from 2,226.87 to 2,630.43 tok/s at
130K (+18.12%). Cumulatively versus the measured main baseline, the
stack through this PR improves prefill by 8.59% at 32K and 25.24% at
130K. Decode changes by at most 0.53% relative to pytorch#22190. KV buffers
below 16K remain on the portable attention path.
## Post-rebase automatic-TMA end-to-end performance
This branch is `pytorch#22190 + TMA`, without FP8/Q8 activation or the later
linear optimizations. These figures supersede the earlier PR-summary
measurements. Stable reruns used a single otherwise-idle RTX 5090,
single-GPU `llama-cli`, the P2409802035 protocol, CUDA graphs, 512
decoded tokens, seven runs, and the mean of runs 3-7. The measurements
used the same TMA kernel/configuration as this revision; the subsequent
cleanup only removed the manual feature gate and made eligible-shape
selection automatic.
| Prompt length | ET prefill tok/s | llama.cpp prefill tok/s | ET vs
llama.cpp prefill | ET decode tok/s | llama.cpp decode tok/s | ET vs
llama.cpp decode |
|---:|---:|---:|---:|---:|---:|---:|
| 512 | 2302.19 | 2391.04 | -3.72% | 76.51 | 79.89 | -4.23% |
| 2,048 | 3311.78 | 2607.19 | +27.02% | 76.05 | 77.40 | -1.74% |
| 8,192 | 3304.35 | 2574.57 | +28.35% | 74.37 | 76.96 | -3.37% |
| 32,768 | 3152.94 | 2465.93 | +27.86% | 74.56 | 75.31 | -1.00% |
| 130,048 | 2630.43 | 2228.04 | +18.06% | 69.45 | 70.77 | -1.87% |
Across the five lengths, ET is 18.83% faster geometric mean than
llama.cpp in prefill and 2.45% slower in decode. The long-context
incremental gain in this PR comes from TMA attention; the decode
improvement in the stack comes from pytorch#22190's split-K scheduling.
## NLL evaluation
`notes_v5`, 100 documents / 154,846 next-token labels. Relative to the
A100 BF16 reference, the same/close bucket counts move from `56/44` for
pytorch#22190 to `59/41` for TMA-only. Compared directly with pytorch#22190, TMA-only
has mean token absolute NLL gap 0.014022 (max 1.158961) and mean
document gap 0.000579 (max 0.002262), with document buckets `84 same /
16 close / 0 match / 0 mismatch`.
Buckets use document mean-NLL gap: `<1e-3`, `[1e-3,1e-2)`,
`[1e-2,1e-1)`, and `>=1e-1`. The same/close bucket distribution changes
by 3 documents, within the agreed maximum difference of 5 for enabling
the optimization automatically on eligible targets.
## Test plan
- `lintrunner init` completed; linting this PR's changed files with
`lintrunner -a` is clean. The full-repository run reports only the two
existing ARM `materialize_symints` mypy failures from current `main`.
- Focused TMA routing/correctness tests: passed.
- TMA correctness compares the forced TMA path, forced portable
fallback, and dense-mask reference for head dimensions 64 and 128.
- Covered automatic SM90+ dispatch, pre-SM90/ROCm exclusion,
non-contiguous fallback, fully masked rows, and D=256 portable fallback.
- Fresh 128K TMA-only PTE/PTD export and smoke run on RTX 5090.
- 35-pair final E2E comparison on an otherwise idle dual-RTX-5090 host,
using only GPU0 for execution.
- `git diff --check`.1 parent ed72896 commit 8734b06
2 files changed
Lines changed: 311 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
646 | 648 | | |
647 | 649 | | |
648 | 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 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
649 | 781 | | |
650 | 782 | | |
651 | 783 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
55 | 87 | | |
56 | 88 | | |
57 | 89 | | |
| |||
780 | 812 | | |
781 | 813 | | |
782 | 814 | | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
783 | 911 | | |
784 | 912 | | |
785 | 913 | | |
| |||
875 | 1003 | | |
876 | 1004 | | |
877 | 1005 | | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
878 | 1057 | | |
879 | 1058 | | |
880 | 1059 | | |
| |||
0 commit comments