Skip to content

[BE] Merge majority of libtorch builds into wheel CD builds - #174753

Closed
malfet wants to merge 3 commits into
gh/malfet/700/basefrom
gh/malfet/700/head
Closed

malfet wants to merge 3 commits into
gh/malfet/700/basefrom
gh/malfet/700/head

Conversation

@malfet

@malfet malfet commented Feb 11, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Because all one has to do is just extract files from wheels to generate a libtorch artifacts

Claude-coded

cc @albanD

[ghstack-poisoned]
@malfet
malfet requested a review from a team as a code owner February 11, 2026 04:17
@pytorch-bot

pytorch-bot Bot commented Feb 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 2 New Failures, 3 Unrelated Failures

As of commit 6e64143 with merge base d6f69da (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Feb 11, 2026
malfet added a commit that referenced this pull request Feb 11, 2026
Because all one has to do is just extract files from wheels to generate a libtorch artifacts

Claude-coded


ghstack-source-id: fab6b5d
Pull-Request: #174753
@malfet malfet added the ciflow/binaries Trigger all binary build and upload jobs on the PR label Feb 11, 2026

@seemethere seemethere left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

[ghstack-poisoned]
malfet added a commit that referenced this pull request Feb 17, 2026
Because all one has to do is just extract files from wheels to generate a libtorch artifacts

Claude-coded

ghstack-source-id: 673fd23
Pull-Request: #174753
[ghstack-poisoned]
malfet added a commit that referenced this pull request Feb 17, 2026
Because all one has to do is just extract files from wheels to generate a libtorch artifacts

Claude-coded

ghstack-source-id: 882320b
Pull-Request: #174753
@malfet

malfet commented Feb 17, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "Looks like it generates something"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Mar 19, 2026
After following PR:#174753
The new approach extracts libtorch from wheels via ``.ci/libtorch/extract_libtorch_from_wheel.py`` instead of using dedicated Docker images to build libtorch. We don't need these Docker builds anymore.
Pull Request resolved: #177802
Approved by: https://github.com/huydhn, https://github.com/malfet
ryanzhang22 pushed a commit to ryanzhang22/pytorch that referenced this pull request Mar 19, 2026
After following PR:pytorch#174753
The new approach extracts libtorch from wheels via ``.ci/libtorch/extract_libtorch_from_wheel.py`` instead of using dedicated Docker images to build libtorch. We don't need these Docker builds anymore.
Pull Request resolved: pytorch#177802
Approved by: https://github.com/huydhn, https://github.com/malfet
@github-actions
github-actions Bot deleted the gh/malfet/700/head branch March 20, 2026 02:22
EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
After following PR:pytorch#174753
The new approach extracts libtorch from wheels via ``.ci/libtorch/extract_libtorch_from_wheel.py`` instead of using dedicated Docker images to build libtorch. We don't need these Docker builds anymore.
Pull Request resolved: pytorch#177802
Approved by: https://github.com/huydhn, https://github.com/malfet
tinglvv added a commit that referenced this pull request May 19, 2026
PR #174753 switched libtorch packaging from a dedicated build to
extracting from pre-built wheels. The extraction script copies libraries
from torch/lib/ but doesn't bundle NVIDIA dependency libraries (cudnn,
nccl, nvshmem, cublas, etc.) or fix RPATH, causing runtime failures
like `libnvshmem_host.so.3 => not found`.

The wheel's .so files have RPATH set to $ORIGIN/../../nvidia/*/lib for
pip site-packages layout, which doesn't exist in the libtorch directory
structure. The old build_libtorch.sh handled this by copying deps and
patching RPATH to $ORIGIN in a post-processing step.

This restores that behavior in the extraction path: for shared-with-deps
variants, copy NVIDIA libs from pip site-packages into libtorch/lib/,
then rewrite RPATH to $ORIGIN on all .so files. The CI workflow is
updated to pip install the NVIDIA packages before extraction.

Fixes #183971
tinglvv added a commit that referenced this pull request May 19, 2026
PR #174753 switched libtorch packaging from a dedicated build to
extracting from pre-built wheels. The extraction script copies libraries
from torch/lib/ but doesn't fix RPATH, causing runtime failures like
`libnvshmem_host.so.3 => not found`.

The wheel's .so files have RPATH set to $ORIGIN/../../nvidia/*/lib for
the pip site-packages layout, which doesn't exist in the libtorch
directory structure. The old build_libtorch.sh used patchelf to set
RPATH to $ORIGIN in a post-processing step.

This adds a pure-Python ELF RPATH rewriter to the extraction script
that overwrites the wheel's RPATH with $ORIGIN, so libraries in
libtorch/lib/ find their siblings. NVIDIA deps (cudnn, nccl, nvshmem,
etc.) are found from the user's system CUDA installation, matching the
old libtorch behavior.

Fixes #183971
@tinglvv

tinglvv commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Reporting my finding as to why CI didn't catch the issue #183971:

  1. ciflow/binaries triggers the extraction but there's no Linux libtorch smoke test after
    extraction
  2. The check_binary.sh RPATH checks only run in the wheel test path

To trigger the extraction on the PR, ciflow/binaries_libtorch is the label. But it will only test that extraction succeeds, not that the RPATH is correct (since there's no post-extraction test). A follow-up to add a libtorch smoke test on Linux would be valuable if we were to keep supporting the libtorch binaries.

pytorchmergebot pushed a commit that referenced this pull request Jun 22, 2026
…#187837)

Fixes #187812

## Problem

The Windows libtorch CD packages for x86_64 and arm64 are generated with the **same filename** and uploaded to the **same S3 location**, so the arm64 build clobbers the x86_64 one. Users downloading the Windows x64 libtorch release actually receive Aarch64 binaries (`c10.dll: PE32+ executable (DLL) (GUI) Aarch64`).

## Root cause

`compute_zip_prefix()` in `.ci/libtorch/extract_libtorch_from_wheel.py` returned `libtorch-win-shared-with-deps` for every Windows build regardless of architecture (the docstring even named `libtorch-win-arm64-shared-with-deps`, but the code never produced it). Both x86_64 and arm64 are CPU builds, so the upload subfolder (`DESIRED_CUDA=cpu`) and the zip filename were identical, and the S3 object key is the zip filename.

The arch-blind naming was introduced in #174753 (which added the wheel-extraction approach); the collision became live once the windows-arm64 wheel workflow started running the same extraction in #181586.

## Fix

Thread architecture through the extraction so arm64 produces `libtorch-win-arm64-shared-with-deps-{version}.zip` while x86_64 keeps its existing `libtorch-win-shared-with-deps-{version}.zip` name (preserving the public download URL). `generate_libtorch_extraction_configs` also gives the arm64 job a distinct `build_name` so the GitHub artifact and job names no longer alias.

## Test Plan

Verified prefix logic:

```
x86_64 -> libtorch-win-shared-with-deps
arm64  -> libtorch-win-arm64-shared-with-deps
```

Regenerated the binary build workflows (`python .github/scripts/generate_ci_workflows.py`); the windows-arm64 wheel workflow now passes `--arch arm64` and uploads a distinct `libtorch-arm64-cpu-shared-with-deps-release` artifact, while the x86_64 workflow is unchanged apart from an explicit `--arch x86_64`.

This PR was authored with the assistance of an AI coding assistant (Claude Code).
Pull Request resolved: #187837
Approved by: https://github.com/Skylion007, https://github.com/huydhn, https://github.com/malfet
atalman added a commit that referenced this pull request Jun 23, 2026
…#187973)

Fix Windows libtorch x86_64 and arm64 packages overwriting each other (#187837)

Fixes #187812

## Problem

The Windows libtorch CD packages for x86_64 and arm64 are generated with the **same filename** and uploaded to the **same S3 location**, so the arm64 build clobbers the x86_64 one. Users downloading the Windows x64 libtorch release actually receive Aarch64 binaries (`c10.dll: PE32+ executable (DLL) (GUI) Aarch64`).

## Root cause

`compute_zip_prefix()` in `.ci/libtorch/extract_libtorch_from_wheel.py` returned `libtorch-win-shared-with-deps` for every Windows build regardless of architecture (the docstring even named `libtorch-win-arm64-shared-with-deps`, but the code never produced it). Both x86_64 and arm64 are CPU builds, so the upload subfolder (`DESIRED_CUDA=cpu`) and the zip filename were identical, and the S3 object key is the zip filename.

The arch-blind naming was introduced in #174753 (which added the wheel-extraction approach); the collision became live once the windows-arm64 wheel workflow started running the same extraction in #181586.

## Fix

Thread architecture through the extraction so arm64 produces `libtorch-win-arm64-shared-with-deps-{version}.zip` while x86_64 keeps its existing `libtorch-win-shared-with-deps-{version}.zip` name (preserving the public download URL). `generate_libtorch_extraction_configs` also gives the arm64 job a distinct `build_name` so the GitHub artifact and job names no longer alias.

## Test Plan

Verified prefix logic:

```
x86_64 -> libtorch-win-shared-with-deps
arm64  -> libtorch-win-arm64-shared-with-deps
```

Regenerated the binary build workflows (`python .github/scripts/generate_ci_workflows.py`); the windows-arm64 wheel workflow now passes `--arch arm64` and uploads a distinct `libtorch-arm64-cpu-shared-with-deps-release` artifact, while the x86_64 workflow is unchanged apart from an explicit `--arch x86_64`.

This PR was authored with the assistance of an AI coding assistant (Claude Code).
Pull Request resolved: #187837
Approved by: https://github.com/Skylion007, https://github.com/huydhn, https://github.com/malfet

(cherry picked from commit 599fb5c)

Co-authored-by: atalman <[email protected]>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
After following PR:pytorch#174753
The new approach extracts libtorch from wheels via ``.ci/libtorch/extract_libtorch_from_wheel.py`` instead of using dedicated Docker images to build libtorch. We don't need these Docker builds anymore.
Pull Request resolved: pytorch#177802
Approved by: https://github.com/huydhn, https://github.com/malfet
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…pytorch#187837)

Fixes pytorch#187812

## Problem

The Windows libtorch CD packages for x86_64 and arm64 are generated with the **same filename** and uploaded to the **same S3 location**, so the arm64 build clobbers the x86_64 one. Users downloading the Windows x64 libtorch release actually receive Aarch64 binaries (`c10.dll: PE32+ executable (DLL) (GUI) Aarch64`).

## Root cause

`compute_zip_prefix()` in `.ci/libtorch/extract_libtorch_from_wheel.py` returned `libtorch-win-shared-with-deps` for every Windows build regardless of architecture (the docstring even named `libtorch-win-arm64-shared-with-deps`, but the code never produced it). Both x86_64 and arm64 are CPU builds, so the upload subfolder (`DESIRED_CUDA=cpu`) and the zip filename were identical, and the S3 object key is the zip filename.

The arch-blind naming was introduced in pytorch#174753 (which added the wheel-extraction approach); the collision became live once the windows-arm64 wheel workflow started running the same extraction in pytorch#181586.

## Fix

Thread architecture through the extraction so arm64 produces `libtorch-win-arm64-shared-with-deps-{version}.zip` while x86_64 keeps its existing `libtorch-win-shared-with-deps-{version}.zip` name (preserving the public download URL). `generate_libtorch_extraction_configs` also gives the arm64 job a distinct `build_name` so the GitHub artifact and job names no longer alias.

## Test Plan

Verified prefix logic:

```
x86_64 -> libtorch-win-shared-with-deps
arm64  -> libtorch-win-arm64-shared-with-deps
```

Regenerated the binary build workflows (`python .github/scripts/generate_ci_workflows.py`); the windows-arm64 wheel workflow now passes `--arch arm64` and uploads a distinct `libtorch-arm64-cpu-shared-with-deps-release` artifact, while the x86_64 workflow is unchanged apart from an explicit `--arch x86_64`.

This PR was authored with the assistance of an AI coding assistant (Claude Code).
Pull Request resolved: pytorch#187837
Approved by: https://github.com/Skylion007, https://github.com/huydhn, https://github.com/malfet
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…pytorch#187837)

Fixes pytorch#187812

## Problem

The Windows libtorch CD packages for x86_64 and arm64 are generated with the **same filename** and uploaded to the **same S3 location**, so the arm64 build clobbers the x86_64 one. Users downloading the Windows x64 libtorch release actually receive Aarch64 binaries (`c10.dll: PE32+ executable (DLL) (GUI) Aarch64`).

## Root cause

`compute_zip_prefix()` in `.ci/libtorch/extract_libtorch_from_wheel.py` returned `libtorch-win-shared-with-deps` for every Windows build regardless of architecture (the docstring even named `libtorch-win-arm64-shared-with-deps`, but the code never produced it). Both x86_64 and arm64 are CPU builds, so the upload subfolder (`DESIRED_CUDA=cpu`) and the zip filename were identical, and the S3 object key is the zip filename.

The arch-blind naming was introduced in pytorch#174753 (which added the wheel-extraction approach); the collision became live once the windows-arm64 wheel workflow started running the same extraction in pytorch#181586.

## Fix

Thread architecture through the extraction so arm64 produces `libtorch-win-arm64-shared-with-deps-{version}.zip` while x86_64 keeps its existing `libtorch-win-shared-with-deps-{version}.zip` name (preserving the public download URL). `generate_libtorch_extraction_configs` also gives the arm64 job a distinct `build_name` so the GitHub artifact and job names no longer alias.

## Test Plan

Verified prefix logic:

```
x86_64 -> libtorch-win-shared-with-deps
arm64  -> libtorch-win-arm64-shared-with-deps
```

Regenerated the binary build workflows (`python .github/scripts/generate_ci_workflows.py`); the windows-arm64 wheel workflow now passes `--arch arm64` and uploads a distinct `libtorch-arm64-cpu-shared-with-deps-release` artifact, while the x86_64 workflow is unchanged apart from an explicit `--arch x86_64`.

This PR was authored with the assistance of an AI coding assistant (Claude Code).
Pull Request resolved: pytorch#187837
Approved by: https://github.com/Skylion007, https://github.com/huydhn, https://github.com/malfet
tinglvv added a commit that referenced this pull request Jul 29, 2026
Fixes the "libnvshmem_host.so.3 => not found" regression (#183971)
in the CUDA libtorch shared-with-deps tarball.

Root cause: libtorch is now assembled by extracting torch/lib/ from the pip
wheel (#174753). The wheel is a "small wheel" (#180612): its NVIDIA runtime
deps -- libnvshmem_host.so.3, libcudart.so.13, libcublas, libcudnn, ... -- are
not in torch/lib/ but supplied at runtime by the pip nvidia-* packages via an
RPATH into site-packages/nvidia/*/lib. The libtorch tarball has no such
packages, so the extracted libtorch/lib/ shipped torch's own libtorch_nvshmem.so
(which hard-NEEDs libnvshmem_host.so.3) with nothing to satisfy it.

Fix: for linux CUDA shared-with-deps, bundle_cuda_deps() copies the required
NVIDIA runtime libraries into libtorch/lib/ before the RPATH rewrite. It
resolves NEEDED entries transitively starting from the torch libs, copying any
soname that exists under the CUDA toolkit dirs in the builder container
(/usr/local/cuda/lib64 and the CUPTI dir). Only libraries found there are
copied, so base system libs and the driver stub (libcuda.so.1) are correctly
left to the runtime environment. fix_rpath() then stamps $ORIGIN on the copied
libs so the flat lib/ is self-resolving. This restores the pre-#180612
self-contained tarball while leaving the pip wheel untouched/small.

Chose a transitive NEEDED resolver over reinstating the old hardcoded DEPS_LIST
so it auto-adapts to CUDA version and the static->dynamic linking changes, and
self-filters system libs by construction.

smoke_test_extract_libtorch.py gains check_regression_deps(): if a consumer lib
is present (libtorch_nvshmem.so / libtorch_cuda.so), its NVIDIA runtime dep
(libnvshmem_host.so* / libcudart.so*) must be bundled -- a direct guard against
this regression recurring.

Test Plan:
Local static checks (no CUDA builder / wheel available on this host):

```
python3 -m py_compile .ci/libtorch/extract_libtorch_from_wheel.py
python3 -m py_compile .ci/libtorch/smoke_test_extract_libtorch.py
```

Full validation is the libtorch-extract CI job on a CUDA config: the log should
show "Bundled CUDA dep: libnvshmem_host.so.3 ..." (plus cudart/cublas/cudnn),
the smoke test's check_regression_deps must pass, and `ldd libtorch_nvshmem.so`
on the extracted tree must not report libnvshmem_host.so.3 as not found.

Authored with assistance from an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
tinglvv added a commit that referenced this pull request Jul 29, 2026
Fixes the "libnvshmem_host.so.3 => not found" regression (#183971)
in the CUDA libtorch shared-with-deps tarball.

Root cause: libtorch is now assembled by extracting torch/lib/ from the pip
wheel (#174753). The wheel is a "small wheel" (#180612): its NVIDIA runtime
deps -- libnvshmem_host.so.3, libcudart.so.13, libcublas, libcudnn, ... -- are
not in torch/lib/ but supplied at runtime by the pip nvidia-* packages via an
RPATH into site-packages/nvidia/*/lib. The libtorch tarball has no such
packages, so the extracted libtorch/lib/ shipped torch's own libtorch_nvshmem.so
(which hard-NEEDs libnvshmem_host.so.3) with nothing to satisfy it.

Fix: for linux CUDA shared-with-deps, bundle_cuda_deps() copies the required
NVIDIA runtime libraries into libtorch/lib/ before the RPATH rewrite. It
resolves NEEDED entries transitively starting from the torch libs, copying any
soname that exists under the CUDA toolkit dirs in the builder container
(/usr/local/cuda/lib64 and the CUPTI dir). Only libraries found there are
copied, so base system libs and the driver stub (libcuda.so.1) are correctly
left to the runtime environment. fix_rpath() then stamps $ORIGIN on the copied
libs so the flat lib/ is self-resolving. This restores the pre-#180612
self-contained tarball while leaving the pip wheel untouched/small.

Chose a transitive NEEDED resolver over reinstating the old hardcoded DEPS_LIST
so it auto-adapts to CUDA version and the static->dynamic linking changes, and
self-filters system libs by construction.

smoke_test_extract_libtorch.py: now that the deps are bundled, restore the full
ctypes.CDLL("libtorch.so") load in check_load() -- it resolves the entire NEEDED
chain through the $ORIGIN RPATH, so a missing or mis-rpath'd bundled lib fails
the test. This was previously removed (it failed on CUDA when deps were not
bundled); with bundling it works, and since libcuda.so.1 is dlopen'd lazily by
cudart (not a load-time NEEDED) it succeeds on a driverless builder.
check_regression_deps() is kept as a fast, explicit guard with a clear message
if a consumer lib (libtorch_nvshmem.so / libtorch_cuda.so) is present without
its bundled NVIDIA runtime dep.

Test Plan:
Local static checks (no CUDA builder / wheel available on this host):

```
python3 -m py_compile .ci/libtorch/extract_libtorch_from_wheel.py
python3 -m py_compile .ci/libtorch/smoke_test_extract_libtorch.py
```

Full validation is the libtorch-extract CI job on a CUDA config: the log should
show "Bundled CUDA dep: libnvshmem_host.so.3 ..." (plus cudart/cublas/cudnn),
and the smoke test's check_load must load libtorch.so with no "not found".

Authored with assistance from an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
tinglvv added a commit that referenced this pull request Jul 29, 2026
Fixes the "libnvshmem_host.so.3 => not found" regression (#183971)
in the CUDA libtorch shared-with-deps tarball.

Root cause: libtorch is now assembled by extracting torch/lib/ from the pip
wheel (#174753). The wheel is a "small wheel" (#180612): its NVIDIA runtime
deps -- libnvshmem_host.so.3, libcudart.so.13, libcublas, libcudnn, ... -- are
not in torch/lib/ but supplied at runtime by the pip nvidia-* packages via an
RPATH into site-packages/nvidia/*/lib. The libtorch tarball has no such
packages, so the extracted libtorch/lib/ shipped torch's own libtorch_nvshmem.so
(which hard-NEEDs libnvshmem_host.so.3) with nothing to satisfy it.

Fix: for linux CUDA shared-with-deps, bundle_cuda_deps() copies the required
NVIDIA runtime libraries into libtorch/lib/ before the RPATH rewrite. It
resolves NEEDED entries transitively starting from the torch libs, copying any
soname that exists under the CUDA toolkit dirs in the builder container
(/usr/local/cuda/lib64 and the CUPTI dir). Only libraries found there are
copied, so base system libs and the driver stub (libcuda.so.1) are correctly
left to the runtime environment. fix_rpath() then stamps $ORIGIN on the copied
libs so the flat lib/ is self-resolving. This restores the pre-#180612
self-contained tarball while leaving the pip wheel untouched/small.

Chose a transitive NEEDED resolver over reinstating the old hardcoded DEPS_LIST
so it auto-adapts to CUDA version and the static->dynamic linking changes, and
self-filters system libs by construction.

smoke_test_extract_libtorch.py is a single ctypes.CDLL("libtorch.so") load: it
resolves the whole NEEDED chain through the $ORIGIN RPATH, so a missing or
mis-rpath'd bundled dep (torch or NVIDIA runtime) fails the test. Full dlopen
was previously dropped because it failed when deps were not bundled; with
bundling it works, and since libcuda.so.1 is dlopen'd lazily by cudart (not a
load-time NEEDED) it succeeds on a driverless builder.

Test Plan:
Local static checks (no CUDA builder / wheel available on this host):

```
python3 -m py_compile .ci/libtorch/extract_libtorch_from_wheel.py
python3 -m py_compile .ci/libtorch/smoke_test_extract_libtorch.py
```

Full validation is the libtorch-extract CI job on a CUDA config: the log should
show "Bundled CUDA dep: libnvshmem_host.so.3 ..." (plus cudart/cublas/cudnn),
and the smoke test must load libtorch.so with no "not found".

Authored with assistance from an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
tinglvv added a commit that referenced this pull request Jul 29, 2026
Fixes the "libnvshmem_host.so.3 => not found" regression (#183971)
in the CUDA libtorch shared-with-deps tarball.

Root cause: libtorch is now assembled by extracting torch/lib/ from the pip
wheel (#174753). The wheel is a "small wheel" (#180612): its NVIDIA runtime
deps -- libnvshmem_host.so.3, libcudart.so.13, libcublas, libcudnn, ... -- are
not in torch/lib/ but supplied at runtime by the pip nvidia-* packages via an
RPATH into site-packages/nvidia/*/lib. The libtorch tarball has no such
packages, so the extracted libtorch/lib/ shipped torch's own libtorch_nvshmem.so
(which hard-NEEDs libnvshmem_host.so.3) with nothing to satisfy it.

Fix: for linux CUDA shared-with-deps, bundle_cuda_deps() copies the required
NVIDIA runtime libraries into libtorch/lib/ before the RPATH rewrite. It
resolves NEEDED entries transitively starting from the torch libs, copying any
soname that exists under the CUDA toolkit dirs in the builder container
(/usr/local/cuda/lib64 and the CUPTI dir). Only libraries found there are
copied, so base system libs and the driver stub (libcuda.so.1) are correctly
left to the runtime environment. fix_rpath() then stamps $ORIGIN on the copied
libs so the flat lib/ is self-resolving. This restores the pre-#180612
self-contained tarball while leaving the pip wheel untouched/small.

Chose a transitive NEEDED resolver over reinstating the old hardcoded DEPS_LIST
so it auto-adapts to CUDA version and the static->dynamic linking changes, and
self-filters system libs by construction.

smoke_test_extract_libtorch.py is a single ctypes.CDLL("libtorch.so") load: it
resolves the whole NEEDED chain through the $ORIGIN RPATH, so a missing or
mis-rpath'd bundled dep (torch or NVIDIA runtime) fails the test. Full dlopen
was previously dropped because it failed when deps were not bundled; with
bundling it works, and since libcuda.so.1 is dlopen'd lazily by cudart (not a
load-time NEEDED) it succeeds on a driverless builder.

Test Plan:
Local static checks (no CUDA builder / wheel available on this host):

```
python3 -m py_compile .ci/libtorch/extract_libtorch_from_wheel.py
python3 -m py_compile .ci/libtorch/smoke_test_extract_libtorch.py
```

Full validation is the libtorch-extract CI job on a CUDA config: the log should
show "Bundled CUDA dep: libnvshmem_host.so.3 ..." (plus cudart/cublas/cudnn),
and the smoke test must load libtorch.so with no "not found".

Authored with assistance from an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
pytorchmergebot pushed a commit that referenced this pull request Aug 2, 2026
This PR adds an RPATH rewriter to the extraction logic that resets RPATH to $ORIGIN. NVIDIA deps (cudnn,nccl, nvshmem, etc.) are resolved from the user's system CUDA installation via LD_LIBRARY_PATH/ldconfig, matching the old libtorch behavior.

PR #174753 switched libtorch packaging from a dedicated build to extracting from pre-built wheels. The wheel's .so files have RPATH set to `$ORIGIN/../../nvidia/*/lib` for pip site-packages layout, which doesn't exist in the libtorch directory structure. The old build_libtorch.sh handled this by copying deps and patching RPATH to $ORIGIN in a post-processing step. The extraction script copies libraries from torch/lib/ but missed to fix RPATH, causing runtime failures like `libnvshmem_host.so.3 => not found`.

Note on why this PR is enough:
Before PR #174753, Linux libtorch was built for CPU + all CUDA arches + all ROCm arches as a standalone CMake build. That build ran inside a CUDA Docker container where build_libtorch.sh could copy libgomp (and other DEPS_LIST deps) from the container's filesystem.

  So the old flow was:
  1. generate_libtorch_matrix → standalone build matrix (CPU, CUDA, ROCm)
  2. CI runs build_cuda.sh → sets DEPS_LIST (libgomp, aarch64 extras) + BUILD_PYTHONLESS=1
  3. Sources build_libtorch.sh → CMake build via tools/build_libtorch.py, copies DEPS_LIST into libtorch/lib/, sets rpath to $ORIGIN

  PR #174753 replaced that with: extract from the already-built wheel. The wheel already has libgomp in torch/lib/, so copy_libraries picks it up. The only missing piece was the rpath fix.

Fixes #183971

Created with Assistance from Claude
Pull Request resolved: #184336
Approved by: https://github.com/nWEIdia, https://github.com/atalman, https://github.com/malfet

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/binaries Trigger all binary build and upload jobs on the PR Merged skip-pr-sanity-checks topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants