Skip to content

Link torch_cpu_library, not torch, so the CUDA wheel imports with a CPU-only torch - #24060

Merged
shoumikhin merged 1 commit into
pytorch:mainfrom
shoumikhin:fix-cuda-overlink
Oct 21, 2026
Merged

shoumikhin merged 1 commit into
pytorch:mainfrom
shoumikhin:fix-cuda-overlink

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 17:05
@shoumikhin shoumikhin added the release notes: build Changes related to build, including dependency upgrades, build flags, optimizations, etc. label Oct 21, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24060

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 2 Pending

As of commit e5d9535 with merge base d1f7f1f (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…PU-only torch

Four libraries in the Linux CUDA wheel record a load-time dependency on
libtorch_cuda.so (one also on libc10_cuda.so) without using anything from
it: the training and LLM runner Python extensions and the custom and
quantized operator libraries. They get it from linking the torch CMake
target, which in a CUDA build of PyTorch adds libtorch_cuda.so under
--no-as-needed. Next to a CPU-only torch those libraries then fail to load.

Link torch_cpu_library instead. torch is torch_cpu_library plus
torch_cuda_library, so the CPU part is linked exactly as before, including
the --as-needed handling that keeps unused libraries such as MKL out of
DT_NEEDED. Also add a check to the CUDA wheel smoke test that no shipped
library depends on PyTorch's CUDA libraries.
Copilot AI balanced review requested due to automatic review settings October 21, 2026 17:50
@shoumikhin shoumikhin changed the title Link torch_cpu, not torch, so the CUDA wheel imports with a CPU-only torch Link torch_cpu_library, not torch, so the CUDA wheel imports with a CPU-only torch Oct 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@shoumikhin
shoumikhin merged commit 9bf80eb into pytorch:main Oct 21, 2026
427 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: build Changes related to build, including dependency upgrades, build flags, optimizations, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants