Repository navigation
Commit 9bf80eb
authored
Link torch_cpu_library, not torch, so the CUDA wheel imports with a CPU-only torch (#24060)
### Problem
The Linux CUDA wheel cannot be used with a CPU-only PyTorch. Four of its
libraries record a load-time dependency (`DT_NEEDED`) on
`libtorch_cuda.so`, one also on `libc10_cuda.so`:
- `extension/training/pybindings/_training_lib`
- `extension/llm/runner/_llm_runner`
- `extension/llm/custom_ops/libcustom_ops_aot_lib.so`
- `kernels/quantized/libquantized_ops_aot_lib.so`
None of them uses those libraries, yet with a CPU-only torch they fail
to load:
```
ImportError: libtorch_cuda.so: cannot open shared object file: No such file or directory
```
It comes from linking the `torch` CMake target (or
`${TORCH_LIBRARIES}`). In a CUDA build of PyTorch that target adds
`libtorch_cuda.so` wrapped in `-Wl,--no-as-needed`, so it is recorded
even when unused, and `--as-needed` on our side cannot drop it.
### Fix
Link `torch_cpu_library` instead of `torch` where these libraries are
built. `torch` is `torch_cpu_library` plus `torch_cuda_library`, so the
CPU half is linked exactly as before, including the `--as-needed`
handling that keeps unused libraries such as MKL out of `DT_NEEDED`.
With a CPU-only PyTorch the link is unchanged.
The CUDA wheel smoke test gets a new check that fails if any shipped
library depends on `libtorch_cuda.so` or `libc10_cuda.so`.
### Testing
Built the CUDA 13.0 wheel on Linux x86_64 before and after the change.
`readelf -d` on `_llm_runner`, before:
```
libexecutorch.so libtorch_python.so libtorch.so libc10.so libc10_cuda.so libtorch_cpu.so libtorch_cuda.so libcudart.so.13
```
after:
```
libexecutorch.so libtorch_python.so libcudart.so.13 libtorch_cpu.so libc10.so
```
The other three lose `libtorch_cuda.so` the same way.
- The new check fails on the old wheel and passes on the new one.
- With CUDA torch 2.14.0 on an A100, the CUDA wheel smoke test gives the
same results before and after, apart from the new check. That includes
running models through the CUDA delegate and building a C++ app against
the wheel. Checks needing the full release setup (every GPU
architecture, the Qualcomm backend) could not run locally.
- The Python tests for the training bindings, the LLM runner bindings,
the custom ops and the quantized ops give the same results before and
after.
- With a PyTorch that links MKL as shared libraries, linking plain
`torch_cpu` would pull MKL into `DT_NEEDED` and the operator libraries
then fail to load (`undefined symbol: __kmpc_flush`).
`torch_cpu_library` keeps MKL out and they load.
- In a fresh environment with torch 2.14.0+cpu: before, the training
bindings, the LLM runner and both operator libraries fail to load.
After, `import executorch` works, all four modules import, and both
operator libraries load.1 parent d1f7f1f commit 9bf80eb
6 files changed
Lines changed: 51 additions & 9 deletions
File tree
- .ci/scripts/wheel
- extension
- llm
- custom_ops
- runner
- training
- tools/cmake
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
146 | 167 | | |
147 | 168 | | |
148 | 169 | | |
| |||
586 | 607 | | |
587 | 608 | | |
588 | 609 | | |
| 610 | + | |
589 | 611 | | |
590 | 612 | | |
591 | 613 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
1453 | 1456 | | |
1454 | | - | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
1455 | 1460 | | |
1456 | 1461 | | |
1457 | | - | |
| 1462 | + | |
1458 | 1463 | | |
1459 | 1464 | | |
1460 | 1465 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
233 | 236 | | |
234 | | - | |
| 237 | + | |
235 | 238 | | |
236 | 239 | | |
237 | 240 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
124 | 128 | | |
125 | 129 | | |
126 | | - | |
| 130 | + | |
127 | 131 | | |
128 | 132 | | |
129 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
55 | 59 | | |
56 | 60 | | |
57 | | - | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | | - | |
61 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
62 | 67 | | |
63 | 68 | | |
64 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
337 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
338 | 341 | | |
339 | 342 | | |
340 | 343 | | |
| |||
0 commit comments