Skip to content

[SymmMem] Find libtorch_nvshmem.so via get_file_path - #204804

Closed
zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-editable-nvshmem-lookup
Closed

zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-editable-nvshmem-lookup

Conversation

@zklaus

@zklaus zklaus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Fixes #195890

BC-breaking?

No

Summary (human written only)

This PR replaces the legacy __file__ based lookup of the RPATH source libtorch_nvshmem.so with a modern importlib based version via our get_file_path helper, which is necessary for editable installs in redirect mode.

Agent details (if applicable)

NvshmemLibFinder.find_device_library builds a list of directories to search for the NVSHMEM device library. Its last step reads the RPATH of torch's own libtorch_nvshmem.so and adds those directories, which is how an NVSHMEM that lives outside site-packages and the system directories is found. It located that library through os.path.dirname(torch.__file__). Under a redirect-mode editable install that is the source checkout, where there is no lib/libtorch_nvshmem.so, so the step was silently skipped.

The lookup now starts from torch._utils_internal.get_file_path("torch"), which is anchored on the import system (#195587). The change only restores that last step: the NVSHMEM_LIB_DIR override, the NVSHMEM wheel directory and the system directories are searched as before and in the same order, so the question raised in the issue, whether falling through to a system NVSHMEM is intended, is not affected.

get_file_path is called with the single component "torch", as in torch/utils/cpp_extension.py, and the rest is joined with os.path.

Tracked in #195887.

Test Plan

Editable install on Linux x86_64, Python 3.12, CUDA 12.9, built with NVSHMEM_HOME pointing at an NVSHMEM 3.7.2 tree outside site-packages. libtorch_nvshmem.so is in the install tree only, and its RPATH contains the NVSHMEM lib directory.

python -c "from torch.distributed._symmetric_memory._nvshmem_triton import NvshmemLibFinder; NvshmemLibFinder.find_device_library()"

Before the change the directories searched are the wheel directory and the three system directories. After the change the list also contains the RPATH entries of libtorch_nvshmem.so, including the NVSHMEM lib directory.

The call still ends in "NVSHMEM device library not found" on this setup, for an unrelated reason: it looks for libnvshmem_device.bc, and the NVSHMEM 3.7.2 wheel ships per-architecture files (libnvshmem_device_sm_NN.bc) instead. No GPU test was run; the function only inspects files.

lintrunner -a --take PYFMT,RUFF,FLAKE8 torch/distributed/_symmetric_memory/_nvshmem_triton.py

Authored with the help of an AI assistant (Claude Code).

🤖 Generated with Claude Code

@pytorch-bot

pytorch-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

The PR changes NvshmemLibFinder.find_device_library to find torch's
lib/libtorch_nvshmem.so with torch._utils_internal.get_file_path("torch")
instead of os.path.dirname(torch.__file__), so the RPATH search step also runs
under a redirect-mode editable install. Recommendation: Approve. Nothing blocks
the change, and the two points raised are optional, non-blocking cleanups.

Findings (2):

[minor] torch/distributed/_symmetric_memory/_nvshmem_triton.py:66
Non-blocking: the function-local `from torch._utils_internal import
get_file_path` is wrapped in `try: ... except ImportError: pass` (lines 65-66,
90-91), but that import cannot fail here. `torch/__init__.py:69-75` imports
`get_file_path` from `torch._utils_internal` at module level, and this module's
own `import torch.distributed` (line 7) loads it too, through
`distributed_c10d.py:75`. The old `import torch` was dead code in the same way,
so the PR did not introduce this. But the PR rewrites that exact line, so this
is a cheap moment to clean it up. Move the import to the top of the module, as
`distributed_c10d.py` and `launcher/api.py` do. There is no circular-import
reason to keep it local, and moving it also removes an outer `try` that exists
only to wrap the inner one.

[minor] torch/distributed/_symmetric_memory/_nvshmem_triton.py:68
Non-blocking: two other places that switched to `get_file_path` for the same
editable-install reason carry a short comment explaining why `__file__` is
wrong: `torch/__init__.py:421-422` and `torch/utils/cpp_extension.py:44-48`. Add
a similar one-line comment here. Without it, a later cleanup could easily
"simplify" this back to `os.path.dirname(torch.__file__)` and quietly bring the
bug back.

Reviewed commit: 27e0db0 (NOT the current head b68854f)

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ No Failures

As of commit b68854f with merge base afdb716 (image):
💚 Looks good so far! There are no failures yet. 💚
🟡 GreenLight: changes can't be reviewed due to merge_rules.yaml restrictions

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

@pytorch-bot pytorch-bot Bot added ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests labels Nov 13, 2026
@zklaus
zklaus marked this pull request as ready for review November 13, 2026 15:54
@zklaus
zklaus requested a review from kwen2501 November 13, 2026 16:14
@ezyang ezyang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 17, 2026
@pytorch-bot pytorch-bot Bot added the in progress PR is being worked on by the author, until the automated review accepts it label Nov 17, 2026

@github-actions github-actions Bot 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.

Automated review of 27e0db0: nothing blocking; ready for human review.

The PR changes NvshmemLibFinder.find_device_library to find torch's lib/libtorch_nvshmem.so with torch._utils_internal.get_file_path("torch") instead of os.path.dirname(torch.file), so the RPATH search step also runs under a redirect-mode editable install. Recommendation: Approve. Nothing blocks the change, and the two points raised are optional, non-blocking cleanups.

⚪ 2 non-blocking findings

from torch._utils_internal import get_file_path

Non-blocking: the function-local from torch._utils_internal import get_file_path is wrapped in try: ... except ImportError: pass (lines 65-66, 90-91), but that import cannot fail here. torch/__init__.py:69-75 imports get_file_path from torch._utils_internal at module level, and this module's own import torch.distributed (line 7) loads it too, through distributed_c10d.py:75. The old import torch was dead code in the same way, so the PR did not introduce this. But the PR rewrites that exact line, so this is a cheap moment to clean it up. Move the import to the top of the module, as distributed_c10d.py and launcher/api.py do. There is no circular-import reason to keep it local, and moving it also removes an outer try that exists only to wrap the inner one.


torch_lib = os.path.join(get_file_path("torch"), "lib")

Non-blocking: two other places that switched to get_file_path for the same editable-install reason carry a short comment explaining why __file__ is wrong: torch/__init__.py:421-422 and torch/utils/cpp_extension.py:44-48. Add a similar one-line comment here. Without it, a later cleanup could easily "simplify" this back to os.path.dirname(torch.__file__) and quietly bring the bug back.

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 17, 2026
@zklaus

zklaus commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -r

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 1, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

## Linked issue or supporting maintainer

Fixes pytorch#195890

## BC-breaking?

No

## Summary (human written only)

This PR replaces the legacy `__file__` based lookup of the RPATH source `libtorch_nvshmem.so` with a modern importlib based version via our `get_file_path` helper, which is necessary for editable installs in redirect mode.

## Agent details (if applicable)

`NvshmemLibFinder.find_device_library` builds a list of directories to search for the NVSHMEM device library. Its last step reads the RPATH of torch's own `libtorch_nvshmem.so` and adds those directories, which is how an NVSHMEM that lives outside site-packages and the system directories is found. It located that library through `os.path.dirname(torch.__file__)`. Under a redirect-mode editable install that is the source checkout, where there is no `lib/libtorch_nvshmem.so`, so the step was silently skipped.

The lookup now starts from `torch._utils_internal.get_file_path("torch")`, which is anchored on the import system (pytorch#195587). The change only restores that last step: the `NVSHMEM_LIB_DIR` override, the NVSHMEM wheel directory and the system directories are searched as before and in the same order, so the question raised in the issue, whether falling through to a system NVSHMEM is intended, is not affected.

`get_file_path` is called with the single component `"torch"`, as in `torch/utils/cpp_extension.py`, and the rest is joined with `os.path`.

Tracked in pytorch#195887.

### Test Plan

Editable install on Linux x86_64, Python 3.12, CUDA 12.9, built with `NVSHMEM_HOME` pointing at an NVSHMEM 3.7.2 tree outside site-packages. `libtorch_nvshmem.so` is in the install tree only, and its RPATH contains the NVSHMEM `lib` directory.

```
python -c "from torch.distributed._symmetric_memory._nvshmem_triton import NvshmemLibFinder; NvshmemLibFinder.find_device_library()"
```

Before the change the directories searched are the wheel directory and the three system directories. After the change the list also contains the RPATH entries of `libtorch_nvshmem.so`, including the NVSHMEM `lib` directory.

The call still ends in "NVSHMEM device library not found" on this setup, for an unrelated reason: it looks for `libnvshmem_device.bc`, and the NVSHMEM 3.7.2 wheel ships per-architecture files (`libnvshmem_device_sm_NN.bc`) instead. No GPU test was run; the function only inspects files.

```
lintrunner -a --take PYFMT,RUFF,FLAKE8 torch/distributed/_symmetric_memory/_nvshmem_triton.py
```

Authored with the help of an AI assistant (Claude Code).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix-editable-nvshmem-lookup onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix-editable-nvshmem-lookup && git pull --rebase)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

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

Details for Dev Infra team

Raised by workflow job

@zklaus zklaus added topic: bug fixes topic category release notes: distributed (symm_mem) release note label for symmetric memory labels Dec 2, 2026
@zklaus

zklaus commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged open source ready for review Automated review passed, ready for a final review by a maintainer. release notes: distributed (symm_mem) release note label for symmetric memory topic: bug fixes topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_nvshmem_triton resolves libtorch_nvshmem.so from the source checkout

5 participants