Commit bf802cb
committed
Ship the CUDA delegate in a CUDA-enabled ExecuTorch wheel
## Why this is needed
The previous change ships a default Linux wheel with the core runtime as a
linkable libexecutorch.so, but no GPU delegate. To actually run a model on
an NVIDIA GPU (including a coalesced TensorRT + CUDA .pte), a C++ program
also needs the CUDA delegate as a loadable library.
This follows the way PyTorch distributes CUDA: the default wheel has the CPU
runtime, and a CUDA-specific wheel (built on a CUDA machine / installed from
a CUDA index) adds the GPU binaries. Here the CUDA-enabled ExecuTorch wheel
ships the CUDA delegate shared libraries alongside the same core runtime, so
a C++ consumer can link and load the CUDA backend with no source build.
Today the CUDA backend only exists as a static archive that is baked into
the Python module (_portable_lib.so). There is no standalone loadable
backend a C++ program can use, the way QNN already ships
libqnn_executorch_backend.so. This change adds that.
## What is inside
Added only when the wheel is built with CUDA (Linux):
- executorch/lib/libexecutorch_cuda_backend.so: a loadable CUDA delegate.
It wraps the existing static CUDA backend so that loading the library runs
its "CudaBackend" registration and registers into the one process-wide
runtime in libexecutorch.so. It is libtorch-free.
- executorch/lib/libextension_cuda.so: the CUDA caller-stream extension. It
is a single shared library on purpose so its per-thread caller-stream state
has exactly one instance across the whole process (needed so a TensorRT
delegate and the CUDA backend share one stream).
- executorch/include/executorch/extension/cuda/*.h: the caller-stream headers.
- CMake targets executorch::cuda_backend and executorch::extension_cuda in the
shipped executorch-config.cmake, defined only when the libraries are present.
The default (CPU) wheel is unchanged: none of these are added unless the
wheel is built with CUDA. Windows and macOS wheels are unchanged.
## How to use it
```bash
pip install executorch # from a CUDA-enabled index/build
```
```cmake
find_package(executorch CONFIG REQUIRED)
add_executable(my_runner main.cpp)
# Linking the CUDA backend force-loads it so it registers on startup.
target_link_libraries(my_runner PRIVATE
executorch::runtime
executorch::cuda_backend)
```
```bash
cmake -S . -B build \
-DCMAKE_PREFIX_PATH="$(python -c 'import executorch.utils as u; print(u.cmake_prefix_path)')"
cmake --build build
```
At runtime, get_backend_class("CudaBackend") returns the registered backend.
## Test plan
Verified on Linux x86_64 with an NVIDIA H100 and CUDA 12.8:
- Built the wheel with CUDA enabled and confirmed it contains
libexecutorch_cuda_backend.so and libextension_cuda.so under executorch/lib/,
the CUDA headers, and the CMake config.
- Installed the wheel into a clean virtual environment and built a standalone
C++ program via find_package(executorch) linking executorch::cuda_backend.
The program compiled, linked, ran, and get_backend_class("CudaBackend")
returned non-null, both when the backend was linked directly and when it was
dlopen'd.
- Confirmed it runs with no LD_LIBRARY_PATH: the shipped libraries resolve each
other through an $ORIGIN rpath because they are co-located in executorch/lib/.
- Confirmed the program and the CUDA backend link no libtorch/libc10 (via ldd).
- Confirmed there is a single libextension_cuda.so that both the backend and
the consumer resolve to (the one-instance caller-stream requirement).
Root-caused and fixed a load-time abort during development: the CUDA backend
static archive already carries a whole-archive link option, so also wrapping it
in an explicit whole-archive linked it twice and ran its static registration
twice, hitting a duplicate-registration check. The fix links it once.
Known follow-ups: a CUDA wheel CI job to exercise this automatically, and
running auditwheel on release so the shipped rpath is limited to $ORIGIN.
ghstack-source-id: 2098e23
ghstack-comment-id: 5124122149
Pull-Request: #214781 parent c2a6b8b commit bf802cb
4 files changed
Lines changed: 200 additions & 0 deletions
File tree
- backends/cuda
- runtime
- tools/cmake
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
884 | 884 | | |
885 | 885 | | |
886 | 886 | | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
887 | 893 | | |
888 | 894 | | |
889 | 895 | | |
| |||
1130 | 1136 | | |
1131 | 1137 | | |
1132 | 1138 | | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1133 | 1144 | | |
1134 | 1145 | | |
1135 | 1146 | | |
| |||
1313 | 1324 | | |
1314 | 1325 | | |
1315 | 1326 | | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
1316 | 1366 | | |
1317 | 1367 | | |
1318 | 1368 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 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 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
178 | 255 | | |
0 commit comments