Repository navigation
Commit 223455b
authored
Let the CUDA backend autotune matmuls on GPUs with fewer than 68 SMs (pytorch#23352)
## What is wrong today
The CUDA backend compiles a model with AOTInductor and asks it to
autotune every matrix multiply and convolution with Triton only. It does
not allow the ATen fallback, because that would pull libtorch operators
into the compiled library.
Inductor only offers its Triton matmul and convolution templates on GPUs
it considers big enough: 68 or more streaming multiprocessors (SMs). On
a smaller GPU the Triton templates are skipped. A matmul then has no
choices left, because nothing else is allowed:
```
NoValidChoicesError: No choices to select. Provided reason: No choices exist for backend.
target: aten.addmm.default
```
A convolution falls back to ATen instead, which this backend then
rejects as a missing fallback kernel (`aoti_torch_cuda_convolution`).
So on any CUDA GPU with fewer than 68 SMs, even a model with a single
`nn.Linear` cannot be lowered with `CudaPartitioner`. Today the only way
around it is to patch `torch._inductor.utils.is_big_gpu` in your own
export script.
## What this change does
While the backend's own AOTInductor compile runs, inductor treats the
GPU as big enough, so it considers the Triton templates it would
otherwise skip. The autotuner then picks the fastest Triton choice as
usual.
The override follows the pattern the file already uses for its other
inductor tweaks:
- It is a context manager, `_allow_triton_templates_on_small_gpus`,
entered in `get_extra_aoti_compile_context_manager` next to the existing
ones.
- It keeps the original `is_big_gpu` in a closure and puts it back when
the compile ends, even if the compile fails.
- It answers only on the thread doing the compile. Other threads get the
original answer.
- It is skipped on ROCm, where `is_big_gpu` checks the GPU architecture
instead of the SM count.
GPUs with 68 or more SMs already pass the check, so nothing changes for
them. The Triton-only backend list stays as it is.
## What was tested
- New unit tests in `test_cuda_export.py`, which need no small GPU: with
`is_big_gpu` reporting a small GPU, Inductor's own template check says
yes inside the backend's compile context and no outside it; the override
is skipped on ROCm; the original function and the thread's state are
back after a failed compile; a nested compile keeps the override until
the outer one ends; and another thread gets the original answer. Each
one fails when the part it covers is removed.
- On a CUDA GPU with 20 SMs (Linux aarch64), a module with one
`nn.Linear` lowered with `CudaPartitioner`, with no patch in the caller:
before, `NoValidChoicesError`; after, the `.pte` and `.ptd` files are
written.
- On the same GPU, `test_cuda_export.py`, `test_cuda_partitioner.py` and
`test_cuda_weight_metadata.py`: before, 6 export tests fail (linear, a
multi-operation module and SDPA with Triton kernels off with
`NoValidChoicesError`; conv1d, conv2d and a ResNet block with the
missing convolution kernel); after, they pass. A lowered `nn.Linear` and
`Conv2d` also give the same results as eager PyTorch.1 parent 8a08ecd commit 223455b
2 files changed
Lines changed: 117 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
116 | 150 | | |
117 | 151 | | |
118 | 152 | | |
| |||
904 | 938 | | |
905 | 939 | | |
906 | 940 | | |
907 | | - | |
908 | | - | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
909 | 945 | | |
910 | 946 | | |
911 | 947 | | |
| |||
932 | 968 | | |
933 | 969 | | |
934 | 970 | | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
935 | 974 | | |
936 | 975 | | |
937 | 976 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
136 | 212 | | |
137 | 213 | | |
138 | 214 | | |
| |||
0 commit comments