Skip to content

[inductor] Resolve installed asset paths via get_file_path - #204789

Closed
zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-editable-inductor-lookups
Closed

zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-editable-inductor-lookups

Conversation

@zklaus

@zklaus zklaus commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Part of #195889

BC-breaking?

No

Summary (human written only)

This PR replaces resource lookups in inductor and inductor tests that are based on the legacy __file__ mechanism with the modern importlib based lookup via torch._utils_internal.get_file_path. This is necessary to make things work with editable installs in redirect mode that came in with our move to scikit-build-core.

Agent details (if applicable)

Under a redirect-mode editable install, torch.__file__ points into the source checkout while bin/, include/ and lib/ are installed beside the compiled extension. Three inductor lookups derived those directories from __file__ and so pointed into the checkout:

  • runtime/compile_tasks.py: the bundled bin/ptxas was not found, so TRITON_PTXAS_PATH stayed unset and Triton fell back to its own ptxas.
  • codegen/cpp_wrapper_cpu.py: an out-of-tree backend's extended c_shim_<device>.h was not found and silently not included.
  • cpu_vec_isa.py: the lib directory prepended to LD_LIBRARY_PATH for the vec-ISA probe did not exist, so the probe always took the slower import torch retry.

All three now start from torch._utils_internal.get_file_path("torch"), which is anchored on the import system (#195587). Two tests in test_codecache.py that pinned the old lib path move with it.

get_file_path is called with the single component "torch" and the rest is joined with os.path, the form torch/utils/cpp_extension.py and codegen/mps.py already use. Two of the three files looked up are optional, and a build system that replaces get_file_path may raise for a resource it does not ship (see _resolve_cmake_prefix_path in torch/utils/__init__.py); the single-component form keeps the existing exists() checks in charge of that case. The alternative was the multi-component form wrapped in try/except at each site.

The fourth site named in the issue, the include lookup through _TORCH_PATH in cpp_builder.py, is not changed. It is only used by build_fbcode_re, the remote-build path, which an editable install does not reach, and _TORCH_PATH also feeds the linker script path and the code hash, where the source tree is the right answer.

Tracked in #195887.

Test Plan

Applied on top of an editable install on Linux x86_64, Python 3.12, CPU-only, in a checkout with no leftovers of earlier in-tree builds (no libraries in torch/lib). The changes are pure Python, so no rebuild was needed.

cd test
python -m pytest inductor/test_codecache.py -q -rs -k TestVecISACheckBuild
python -m pytest inductor/test_aot_inductor.py -q -k "AOTInductorTestABICompatibleCpu and (test_simple or test_add)"

TestVecISACheckBuild before the change: 3 passed, 1 skipped ("libc10.so not found in torch lib dir"), and the first LD_LIBRARY_PATH entry of VecISA._build_probe_env() is torch/lib in the checkout, which holds no libraries. test_build_probe_env_prepends_torch_lib_dir_to_loader_path passes there only because it asserted the same __file__-derived path as the code. After the change: 4 passed, and the entry is torch/lib of the install tree.

AOT inductor selection after the change: 8 passed. A torch.compile call with cpp_wrapper=True runs.

Not exercised: the ptxas lookup (needs a CUDA build with BUILD_BUNDLE_PTXAS=1) and the extended shim header (needs an out-of-tree backend).

lintrunner -a --take PYFMT,RUFF,FLAKE8 test/inductor/test_codecache.py torch/_inductor/codegen/cpp_wrapper_cpu.py torch/_inductor/cpu_vec_isa.py torch/_inductor/runtime/compile_tasks.py

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

🤖 Generated with Claude Code

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@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 routes three inductor lookups of installed assets (bin/ptxas, the
extended c_shim header, the vec-ISA lib dir) and two tests through
torch._utils_internal.get_file_path("torch") instead of __file__, so they
resolve correctly under redirect-mode editable installs. Recommendation:
Approve; only one non-blocking test point remains.

Findings (1):

[minor] test/inductor/test_codecache.py:6183
Non-blocking: circular check.
test_build_probe_env_prepends_torch_lib_dir_to_loader_path builds its expected
value with os.path.join(get_file_path("torch"), "lib"), the same expression
VecISA._build_probe_env uses at torch/_inductor/cpu_vec_isa.py:139. If
torch_parent ever resolved into the source checkout again under a redirect-mode
editable install, both sides would move together and the test would still pass.
The PR description criticises the old __file__-based version for exactly this.
Suggested fix: derive the expectation independently, e.g. c_file =
getattr(torch._C, "__file__", None) (skip when None), then compare
os.path.realpath(value.split(os.pathsep)[0]) with
os.path.realpath(os.path.join(os.path.dirname(c_file), "lib")). On a redirect
install torch._C resolves to the installed tree, so its __file__ does not depend
on the __loader__.paths logic in _installed_torch_dir. Alternatively assert the
first entry contains libc10.so, but that is Linux-only and would need a
Linux-only skip.

Reviewed commit: e46f733 (NOT the current head c0f6a45)

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ No Failures

As of commit c0f6a45 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.

os.path.dirname(torch.__file__),
"include",
extend_aoti_c_shim_include,
get_file_path("torch"), "include", extend_aoti_c_shim_include

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Slight nit but we should add an overload that lets you take in the module symbol to reduce import overhead. Like if torch is already imported just pass the torch module object.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for taking a look!

Which overhead do you have in mind? get_file_path does not look anything up by name: it is os.path.join(torch_parent, *path_components), with torch_parent computed once during import torch. The "torch" here is a path component, not a module name, so no import or spec lookup happens per call, and torch._utils_internal is already loaded by import torch.

Given that, I would rather not add a module overload in this PR. It would not save anything, and the function is replaced by an internal implementation at Meta, so a signature change needs to be coordinated on both sides. If you see value in a module-accepting form for other reasons, I am happy to take that up separately.

@jbschlosser jbschlosser added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 19, 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 19, 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 e46f733: nothing blocking; ready for human review.

The PR routes three inductor lookups of installed assets (bin/ptxas, the extended c_shim header, the vec-ISA lib dir) and two tests through torch._utils_internal.get_file_path("torch") instead of file, so they resolve correctly under redirect-mode editable installs. Recommendation: Approve; only one non-blocking test point remains.

⚪ 1 non-blocking finding

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

Non-blocking: circular check. test_build_probe_env_prepends_torch_lib_dir_to_loader_path builds its expected value with os.path.join(get_file_path("torch"), "lib"), the same expression VecISA._build_probe_env uses at torch/_inductor/cpu_vec_isa.py:139. If torch_parent ever resolved into the source checkout again under a redirect-mode editable install, both sides would move together and the test would still pass. The PR description criticises the old file-based version for exactly this. Suggested fix: derive the expectation independently, e.g. c_file = getattr(torch._C, "file", None) (skip when None), then compare os.path.realpath(value.split(os.pathsep)[0]) with os.path.realpath(os.path.join(os.path.dirname(c_file), "lib")). On a redirect install torch._C resolves to the installed tree, so its file does not depend on the loader.paths logic in _installed_torch_dir. Alternatively assert the first entry contains libc10.so, but that is Linux-only and would need a Linux-only skip.

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 19, 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

Part of pytorch#195889

## BC-breaking?

No

## Summary (human written only)

This PR replaces resource lookups in inductor and inductor tests that are based on the legacy `__file__` mechanism with the modern importlib based lookup via `torch._utils_internal.get_file_path`. This is necessary to make things work with editable installs in redirect mode that came in with our move to scikit-build-core.

## Agent details (if applicable)

Under a redirect-mode editable install, `torch.__file__` points into the source checkout while `bin/`, `include/` and `lib/` are installed beside the compiled extension. Three inductor lookups derived those directories from `__file__` and so pointed into the checkout:

- `runtime/compile_tasks.py`: the bundled `bin/ptxas` was not found, so `TRITON_PTXAS_PATH` stayed unset and Triton fell back to its own ptxas.
- `codegen/cpp_wrapper_cpu.py`: an out-of-tree backend's extended `c_shim_<device>.h` was not found and silently not included.
- `cpu_vec_isa.py`: the `lib` directory prepended to `LD_LIBRARY_PATH` for the vec-ISA probe did not exist, so the probe always took the slower `import torch` retry.

All three now start from `torch._utils_internal.get_file_path("torch")`, which is anchored on the import system (pytorch#195587). Two tests in `test_codecache.py` that pinned the old `lib` path move with it.

`get_file_path` is called with the single component `"torch"` and the rest is joined with `os.path`, the form `torch/utils/cpp_extension.py` and `codegen/mps.py` already use. Two of the three files looked up are optional, and a build system that replaces `get_file_path` may raise for a resource it does not ship (see `_resolve_cmake_prefix_path` in `torch/utils/__init__.py`); the single-component form keeps the existing `exists()` checks in charge of that case. The alternative was the multi-component form wrapped in `try/except` at each site.

The fourth site named in the issue, the `include` lookup through `_TORCH_PATH` in `cpp_builder.py`, is not changed. It is only used by `build_fbcode_re`, the remote-build path, which an editable install does not reach, and `_TORCH_PATH` also feeds the linker script path and the code hash, where the source tree is the right answer.

Tracked in pytorch#195887.

### Test Plan

Applied on top of an editable install on Linux x86_64, Python 3.12, CPU-only, in a checkout with no leftovers of earlier in-tree builds (no libraries in `torch/lib`). The changes are pure Python, so no rebuild was needed.

```
cd test
python -m pytest inductor/test_codecache.py -q -rs -k TestVecISACheckBuild
python -m pytest inductor/test_aot_inductor.py -q -k "AOTInductorTestABICompatibleCpu and (test_simple or test_add)"
```

`TestVecISACheckBuild` before the change: 3 passed, 1 skipped ("libc10.so not found in torch lib dir"), and the first `LD_LIBRARY_PATH` entry of `VecISA._build_probe_env()` is `torch/lib` in the checkout, which holds no libraries. `test_build_probe_env_prepends_torch_lib_dir_to_loader_path` passes there only because it asserted the same `__file__`-derived path as the code. After the change: 4 passed, and the entry is `torch/lib` of the install tree.

AOT inductor selection after the change: 8 passed. A `torch.compile` call with `cpp_wrapper=True` runs.

Not exercised: the ptxas lookup (needs a CUDA build with `BUILD_BUNDLE_PTXAS=1`) and the extended shim header (needs an out-of-tree backend).

```
lintrunner -a --take PYFMT,RUFF,FLAKE8 test/inductor/test_codecache.py torch/_inductor/codegen/cpp_wrapper_cpu.py torch/_inductor/cpu_vec_isa.py torch/_inductor/runtime/compile_tasks.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-inductor-lookups onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix-editable-inductor-lookups && git pull --rebase)

@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Could not find commit that was pushed before comment 7317403769

Details for Dev Infra team

Raised by workflow job

@zklaus

zklaus commented Dec 1, 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/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor open source ready for review Automated review passed, ready for a final review by a maintainer. release notes: inductor (aoti) 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.

6 participants