Skip to content

Conversation

GregoryComer
Copy link
Member

@GregoryComer GregoryComer commented Sep 18, 2025

Note: This is an attempt to cherry-pick Mergen's RPATH fix from #13254 onto main. Fixes #14421. Original description below.

Problem: The _portable_lib.so Python extension built on CI couldn't find PyTorch libraries when installed locally because it had hardcoded absolute paths from
the CI build environment.

Error:
ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libtorch_python.dylib
Referenced from:
.../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so Reason: tried:
'/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such file)

Root Cause: The CMake build was linking to PyTorch libraries using absolute paths from the build environment, without setting proper relative RPATHs for
runtime library resolution.

Solution: Added platform-specific relative RPATH settings to the portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines 657-669):

  • macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries relative to the .so file location
  • Linux: Uses $ORIGIN/../../../torch/lib for the same purpose
  • Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency

Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch libraries regardless of the installation location, fixing the runtime linking issue
when using ExecutorTorch wheels built on CI.

Note: The same fix may be needed for _training_lib if it experiences similar issues.

Test Plan:

  # Build the wheel locally
  python setup.py bdist_wheel

  # create fresh conda env
  conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11

  # install
  pip install ./dist/executorch-*.whl

  # Verify
  python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')"

@pytorch-bot
Copy link

pytorch-bot bot commented Sep 18, 2025

🔗 Helpful Links

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

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

❌ 2 New Failures, 1 Unrelated Failure

As of commit 446d855 with merge base 0f22062 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Sep 18, 2025
@github-actions
Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@GregoryComer GregoryComer added ciflow/binaries ciflow/binaries/all Release PRs with this label will build wheels for all python versions labels Sep 18, 2025
BUILD_RPATH "@loader_path/../../../torch/lib"
INSTALL_RPATH "@loader_path/../../../torch/lib"
)
else()
Copy link
Contributor

Choose a reason for hiding this comment

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

@GregoryComer Do you think this will work for windows?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good question. I was planning to test the windows wheel on a local machine. I'll see if we run into any issues.

@GregoryComer
Copy link
Member Author

Fixing lint. Other than that, CI looks good. I took a look at the previous cherry-pick attempt to main (#13290) and it looks like the build failures were due to CMake export changes. That's resolved now.

@GregoryComer GregoryComer marked this pull request as ready for review September 19, 2025 00:30
RPATH Fix for portable_lib Python Extension

Problem: The _portable_lib.so Python extension built on CI couldn't find
PyTorch libraries when installed locally because it had hardcoded
absolute paths from
the CI build environment.

Error:
ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002):
Library not loaded: @rpath/libtorch_python.dylib
Referenced from:
.../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so
Reason: tried:
'/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such
file)

Root Cause: The CMake build was linking to PyTorch libraries using
absolute paths from the build environment, without setting proper
relative RPATHs for
runtime library resolution.

Solution: Added platform-specific relative RPATH settings to the
portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines
657-669):

- macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries
relative to the .so file location
- Linux: Uses $ORIGIN/../../../torch/lib for the same purpose
- Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency

Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch
libraries regardless of the installation location, fixing the runtime
linking issue
when using ExecutorTorch wheels built on CI.

Note: The same fix may be needed for _training_lib if it experiences
similar issues.

Test Plan:

```
  # Build the wheel locally
  python setup.py bdist_wheel

  # create fresh conda env
  conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11

  # install
  pip install ./dist/executorch-*.whl

  # Verify
  python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')"
```
@mergennachin
Copy link
Contributor

Thank you!

@GregoryComer
Copy link
Member Author

I verified that the wheel built from this job, installed into a clean env, can load pybindings on macos.

I also tested on Windows on a clean env, and I can't load the _portable_lib dll, though it's also broken on the release branch. I'll file as release blocking task.

@GregoryComer GregoryComer merged commit 641e737 into pytorch:main Sep 19, 2025
276 of 279 checks passed
@GregoryComer
Copy link
Member Author

@pytorchbot cherry-pick --onto release/1.0 -c critical

pytorchbot pushed a commit that referenced this pull request Sep 19, 2025
**Note: This is an attempt to cherry-pick Mergen's RPATH fix from #13254
onto main. Fixes #14421.
Original description below.**

Problem: The _portable_lib.so Python extension built on CI couldn't find
PyTorch libraries when installed locally because it had hardcoded
absolute paths from
the CI build environment.

Error:
ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002):
Library not loaded: @rpath/libtorch_python.dylib
Referenced from:
.../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so
Reason: tried:
'/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such
file)

Root Cause: The CMake build was linking to PyTorch libraries using
absolute paths from the build environment, without setting proper
relative RPATHs for
runtime library resolution.

Solution: Added platform-specific relative RPATH settings to the
portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines
657-669):

- macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries
relative to the .so file location
- Linux: Uses $ORIGIN/../../../torch/lib for the same purpose
- Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency

Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch
libraries regardless of the installation location, fixing the runtime
linking issue
when using ExecutorTorch wheels built on CI.

Note: The same fix may be needed for _training_lib if it experiences
similar issues.

Test Plan:

```
  # Build the wheel locally
  python setup.py bdist_wheel

  # create fresh conda env
  conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11

  # install
  pip install ./dist/executorch-*.whl

  # Verify
  python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')"
```

Co-authored-by: Mergen Nachin <[email protected]>
(cherry picked from commit 641e737)
@pytorchbot
Copy link
Collaborator

Cherry picking #14422

The cherry pick PR is at #14442 and it is recommended to link a critical cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

abhinaykukkadapu pushed a commit that referenced this pull request Sep 23, 2025
**Note: This is an attempt to cherry-pick Mergen's RPATH fix from #13254
onto main. Fixes #14421.
Original description below.**

Problem: The _portable_lib.so Python extension built on CI couldn't find
PyTorch libraries when installed locally because it had hardcoded
absolute paths from
the CI build environment.

Error:
ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002):
Library not loaded: @rpath/libtorch_python.dylib
Referenced from:
.../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so
Reason: tried:
'/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such
file)

Root Cause: The CMake build was linking to PyTorch libraries using
absolute paths from the build environment, without setting proper
relative RPATHs for
runtime library resolution.

Solution: Added platform-specific relative RPATH settings to the
portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines
657-669):

- macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries
relative to the .so file location
- Linux: Uses $ORIGIN/../../../torch/lib for the same purpose
- Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency

Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch
libraries regardless of the installation location, fixing the runtime
linking issue
when using ExecutorTorch wheels built on CI.

Note: The same fix may be needed for _training_lib if it experiences
similar issues.

Test Plan:

```
  # Build the wheel locally
  python setup.py bdist_wheel

  # create fresh conda env
  conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11

  # install
  pip install ./dist/executorch-*.whl

  # Verify
  python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')"
```

Co-authored-by: Mergen Nachin <[email protected]>
(cherry picked from commit 641e737)
StrycekSimon pushed a commit to nxp-upstream/executorch that referenced this pull request Sep 23, 2025
**Note: This is an attempt to cherry-pick Mergen's RPATH fix from pytorch#13254
onto main. Fixes pytorch#14421.
Original description below.**

Problem: The _portable_lib.so Python extension built on CI couldn't find
PyTorch libraries when installed locally because it had hardcoded
absolute paths from
the CI build environment.

Error:
ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002):
Library not loaded: @rpath/libtorch_python.dylib
Referenced from:
.../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so
Reason: tried:
'/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such
file)

Root Cause: The CMake build was linking to PyTorch libraries using
absolute paths from the build environment, without setting proper
relative RPATHs for
runtime library resolution.

Solution: Added platform-specific relative RPATH settings to the
portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines
657-669):

- macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries
relative to the .so file location
- Linux: Uses $ORIGIN/../../../torch/lib for the same purpose
- Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency

Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch
libraries regardless of the installation location, fixing the runtime
linking issue
when using ExecutorTorch wheels built on CI.

Note: The same fix may be needed for _training_lib if it experiences
similar issues.

Test Plan:

```
  # Build the wheel locally
  python setup.py bdist_wheel

  # create fresh conda env
  conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11

  # install
  pip install ./dist/executorch-*.whl

  # Verify
  python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')"
```

Co-authored-by: Mergen Nachin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/binaries/all Release PRs with this label will build wheels for all python versions ciflow/binaries CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix RPATH issues on MacOS

4 participants