Skip to content

[Windows] Skip installed test DLLs during torch import - #199925

Closed
Mohit-25-tech wants to merge 1 commit into
pytorch:mainfrom
Mohit-25-tech:fix/windows-skip-test-dll-preload
Closed

Mohit-25-tech wants to merge 1 commit into
pytorch:mainfrom
Mohit-25-tech:fix/windows-skip-test-dll-preload

Conversation

@Mohit-25-tech

@Mohit-25-tech Mohit-25-tech commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Please open this PR as a draft and mark it ready for review once the description and code are ready to be looked at.

  • Passes lint (spin fixlint) - lint could not complete locally because configured tools were missing.
  • Added/updated tests - Windows regression test added; it could not be run against a compatible source build.
  • Updated documentation (if applicable) - not applicable.
  • Included benchmark results (for PRs impacting perf) - not applicable.

Linked issue or supporting maintainer

Fixes #199894

BC-breaking?

No intended public API break.

Summary (human written only)

From my understanding, the issue was that on Windows, some test-only DLL files were also being loaded during import torch, even though they are only needed by specific tests.

This change skips those test-only DLLs during the normal torch import. The files are still available and can be loaded explicitly when the related tests need them.

Agent details (if applicable)

AI-assisted analysis for review: On Windows, _load_dll_libraries() preloads every DLL under torch/lib. With BUILD_TEST=ON and INSTALL_TEST=ON, that includes aoti_custom_ops.dll, torchbind_test.dll, jitbackend_test.dll, and backend_with_compiler.dll. The patch skips those four names during import. The files remain installed for explicit loading by their tests. A fresh-process Windows regression test checks that a core DLL is loaded while installed test DLLs are not.

Validation completed: Python AST parsing passed for both changed files and the embedded subprocess script. git diff --cached --check passed before commit, and git show --check --stat HEAD reported no whitespace errors. lintrunner -a could not complete because configured lint tools were missing; it produced no usable lint result.

Runtime validation not completed: the Windows regression test, before/after comparison, and explicit torch.ops.load_library() test were not run against this source revision. This checkout has no compatible test-enabled Windows PyTorch build. The installed PyTorch 2.14.0 wheel is a different revision and contains none of the four test DLLs.

Windows import preloads every DLL in torch/lib, including test-only DLLs
installed when INSTALL_TEST is enabled. A load failure in one of those
libraries can abort import before an unrelated test starts. Skip the four
verified test DLL names while retaining normal DLL loading and errors. Add
a subprocess regression test for a test-enabled Windows build.

Test Plan:
- Passed Python AST parsing for torch/__init__.py,
  test/test_windows_dll_loading.py, and the test's embedded script.
- Passed git diff --cached --check.
- lintrunner -a could not complete: configured lint tools were missing.
- Windows regression and explicit DLL loading were not run because this
  worktree has no compatible test-enabled PyTorch source build.
@pytorch-bot

pytorch-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

PR Status: missing actionable issue

Auto PR Triage did not find either of the following when it checked this PR:

  • a linked issue labeled actionable (Fixes #<issue> or Part of #<issue>); or
  • a maintainer named in the description who agreed to the change (Supported by @<maintainer>).

Such PRs are a low priority for maintainers to review and might not get a response given the very high volume of PRs we receive. Please focus on actionable issues to ensure a maintainer will be available to review your PR.

If one of these applies, add the matching line to the PR description. If there is no actionable issue for this change yet, you can open one (or comment on an existing one) and wait for a maintainer to label it actionable. Adding actionable to an already-linked issue automatically requests re-triage of open, non-draft PRs with open source and missing actionable issue, unless automated triage is disabled. Adding a valid actionable-issue link or Supported by line to the description also requests re-triage automatically. Marking the PR ready, reopening it, or re-enabling automated triage rechecks existing links and support. If re-triage is still needed, comment @pytorchbot triage to run Auto PR Triage again.

🔗 Helpful Links

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

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

⚠️ 5 Awaiting Approval

As of commit abc0f23 with merge base 3374f39 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@pytorch-bot

pytorch-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown

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.

@Mohit-25-tech
Mohit-25-tech marked this pull request as ready for review October 6, 2026 15:28
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 6, 2026
@soulitzer soulitzer added missing actionable issue A PR without a linked actionable issue or maintainer sponsor and removed missing actionable issue A PR without a linked actionable issue or maintainer sponsor labels Oct 15, 2026
@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

Hi maintainers,

I'd appreciate guidance on the direction for this PR.

I noticed that #200856 has also been opened for issue #199894 and addresses the same Windows test-DLL preload behavior.

My PR includes a regression test for verifying that test-only DLLs are not automatically loaded during import torch, although I haven't been able to run it against a compatible Windows test-enabled source build.

Since the issue is not marked actionable yet, I don't want to duplicate work or proceed against the preferred contribution workflow.

Would you prefer that I keep this PR open for review, coordinate the test coverage with #200856, or close it in favor of the other implementation?

Thanks for your guidance!

pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
## Linked issue or supporting maintainer

Part of #199894. Supersedes #200856 and #199925: without the preload there is no list of test-only DLLs to maintain.

## BC-breaking?

No for the supported configurations. `import torch` no longer preloads every DLL in `torch/lib`; libraries that are loaded on demand are now loaded only on demand, and the `PATH` fallback for systems without `AddDllDirectory` (pre Windows 8 without KB2533623) is gone.

## Summary (human written only)

On Windows we are preloading all libraries in torch/lib. This is somewhat costly and leads to occasional problems. Furthermore, it is likely no longer necessary because the loading code has become smarter and is now able to load dependencies on demand. This PR is here to test that hypothesis in CI.

## Agent details (if applicable)

Analysis and change prepared with Claude Code, reviewed by me. The commit message carries the full reasoning. Agent notes:

The Windows loader has preloaded every DLL in `torch/lib` since #35362, because `caffe2_nvrtc.dll` was loaded by name from `CUDAHooks.cpp` and plain `LoadLibraryW` did not search directories registered with `AddDllDirectory`. Two later changes made that moot: #45674 (2020-10) switched CUDA builds to the in-tree LazyNVRTC, so `caffe2_nvrtc.dll` is only loaded by name on ROCm, and #49781 (2021-01) moved `at::DynamicLibrary` to `LoadLibraryExW` with `LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`, which searches what `os.add_dll_directory` registers. Meanwhile the preload keeps turning on-demand libraries into import-time failures (#199894, #179024, #182123). The glob only ever preloaded torch's own DLLs with the default search flags; the NVIDIA wheel directories come from the cuda-pathfinder loop (and, on CI, from the toolkit bin dir fallback), which this PR does not touch.

Tested on hardware, since trunk has had no Windows CUDA test shards since #139336: Windows 11 VM with an RTX 2060 (driver 610.88), 2026-10-26 cu132 nightly wheel, `torch/__init__.py` replaced by this PR's version (hash-checked against the base commit). Results:

- With the stock cuda-pathfinder 1.8.4 the import fails on a clean machine with and without this change, for a reason outside this PR (below). The failure point moves from a `WinError 126` on `shm.dll` in the glob to "DLL load failed while importing _C".
- With a per-library `try` in the pathfinder loop (not part of this PR), import, `torch.cuda.is_available()`, cuDNN conv2d, cuBLAS matmul, cuFFT, cuRAND, cuSOLVER and a jiterator kernel (NVRTC) all pass with the glob removed. 8 of 13 `torch/lib` DLLs are resident afterwards; `caffe2_nvrtc`, `torch_global_deps`, `nvToolsExt64_1`, `libiompstubs5md` and `torch.dll` were never needed.
- The #199894 mechanism reproduces: a broken stray `zz_broken_test.dll` in `torch/lib` aborts `import torch` with the glob (WinError 193) and is ignored without it.

Found on the way, independent of this PR: the stock cu132 Windows nightly does not import on a clean machine. `nvidia-cuda-cupti` 13.2.86 ships `cupti64_2026.1.2.dll`, which cuda-pathfinder 1.8.4 does not know; the pathfinder loop in `torch/__init__.py` is one `try` around all eleven libraries, so the CUPTI miss also skips cuDNN, `nvidia\cudnn\bin` is never registered, and `cudnn64_9.dll` (a static import of `torch_python.dll` and `torch_cuda.dll`) cannot be found. CUPTI itself still resolves, because a later library in the loop registers the shared `nvidia\cu13\bin` directory. CI does not see this: the Windows CUDA wheel smoke test runs on a T4 with the CUDA toolkit and cuDNN installed under Program Files, and `torch/__init__.py` adds that directory to the search path. A per-library `try` fixes the import with a warning; a pathfinder release that knows the name fixes it silently.

CI on this PR: the full CPU suite on the 12 `win-vs2022-cpu-py3` shards, the `win-vs2022-cuda13.2-py3` build, and the `binaries_wheel` smoke tests for every Python version (CUDA wheels on a T4: import, CUDA tensor, magma, cuDNN, RNN and CNN smoke; XPU wheels import on CPU runners). Not covered: the full CUDA test suite on Windows.

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

Pull Request resolved: #202206
Approved by: https://github.com/atalman
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
## Linked issue or supporting maintainer

Part of pytorch#199894. Supersedes pytorch#200856 and pytorch#199925: without the preload there is no list of test-only DLLs to maintain.

## BC-breaking?

No for the supported configurations. `import torch` no longer preloads every DLL in `torch/lib`; libraries that are loaded on demand are now loaded only on demand, and the `PATH` fallback for systems without `AddDllDirectory` (pre Windows 8 without KB2533623) is gone.

## Summary (human written only)

On Windows we are preloading all libraries in torch/lib. This is somewhat costly and leads to occasional problems. Furthermore, it is likely no longer necessary because the loading code has become smarter and is now able to load dependencies on demand. This PR is here to test that hypothesis in CI.

## Agent details (if applicable)

Analysis and change prepared with Claude Code, reviewed by me. The commit message carries the full reasoning. Agent notes:

The Windows loader has preloaded every DLL in `torch/lib` since pytorch#35362, because `caffe2_nvrtc.dll` was loaded by name from `CUDAHooks.cpp` and plain `LoadLibraryW` did not search directories registered with `AddDllDirectory`. Two later changes made that moot: pytorch#45674 (2020-10) switched CUDA builds to the in-tree LazyNVRTC, so `caffe2_nvrtc.dll` is only loaded by name on ROCm, and pytorch#49781 (2021-01) moved `at::DynamicLibrary` to `LoadLibraryExW` with `LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`, which searches what `os.add_dll_directory` registers. Meanwhile the preload keeps turning on-demand libraries into import-time failures (pytorch#199894, pytorch#179024, pytorch#182123). The glob only ever preloaded torch's own DLLs with the default search flags; the NVIDIA wheel directories come from the cuda-pathfinder loop (and, on CI, from the toolkit bin dir fallback), which this PR does not touch.

Tested on hardware, since trunk has had no Windows CUDA test shards since pytorch#139336: Windows 11 VM with an RTX 2060 (driver 610.88), 2026-10-26 cu132 nightly wheel, `torch/__init__.py` replaced by this PR's version (hash-checked against the base commit). Results:

- With the stock cuda-pathfinder 1.8.4 the import fails on a clean machine with and without this change, for a reason outside this PR (below). The failure point moves from a `WinError 126` on `shm.dll` in the glob to "DLL load failed while importing _C".
- With a per-library `try` in the pathfinder loop (not part of this PR), import, `torch.cuda.is_available()`, cuDNN conv2d, cuBLAS matmul, cuFFT, cuRAND, cuSOLVER and a jiterator kernel (NVRTC) all pass with the glob removed. 8 of 13 `torch/lib` DLLs are resident afterwards; `caffe2_nvrtc`, `torch_global_deps`, `nvToolsExt64_1`, `libiompstubs5md` and `torch.dll` were never needed.
- The pytorch#199894 mechanism reproduces: a broken stray `zz_broken_test.dll` in `torch/lib` aborts `import torch` with the glob (WinError 193) and is ignored without it.

Found on the way, independent of this PR: the stock cu132 Windows nightly does not import on a clean machine. `nvidia-cuda-cupti` 13.2.86 ships `cupti64_2026.1.2.dll`, which cuda-pathfinder 1.8.4 does not know; the pathfinder loop in `torch/__init__.py` is one `try` around all eleven libraries, so the CUPTI miss also skips cuDNN, `nvidia\cudnn\bin` is never registered, and `cudnn64_9.dll` (a static import of `torch_python.dll` and `torch_cuda.dll`) cannot be found. CUPTI itself still resolves, because a later library in the loop registers the shared `nvidia\cu13\bin` directory. CI does not see this: the Windows CUDA wheel smoke test runs on a T4 with the CUDA toolkit and cuDNN installed under Program Files, and `torch/__init__.py` adds that directory to the search path. A per-library `try` fixes the import with a warning; a pathfinder release that knows the name fixes it silently.

CI on this PR: the full CPU suite on the 12 `win-vs2022-cpu-py3` shards, the `win-vs2022-cuda13.2-py3` build, and the `binaries_wheel` smoke tests for every Python version (CUDA wheels on a T4: import, CUDA tensor, magma, cuDNN, RNN and CNN smoke; XPU wheels import on CPU runners). Not covered: the full CUDA test suite on Windows.

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

Pull Request resolved: pytorch#202206
Approved by: https://github.com/atalman
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
## Linked issue or supporting maintainer

Part of pytorch#199894. Supersedes pytorch#200856 and pytorch#199925: without the preload there is no list of test-only DLLs to maintain.

## BC-breaking?

No for the supported configurations. `import torch` no longer preloads every DLL in `torch/lib`; libraries that are loaded on demand are now loaded only on demand, and the `PATH` fallback for systems without `AddDllDirectory` (pre Windows 8 without KB2533623) is gone.

## Summary (human written only)

On Windows we are preloading all libraries in torch/lib. This is somewhat costly and leads to occasional problems. Furthermore, it is likely no longer necessary because the loading code has become smarter and is now able to load dependencies on demand. This PR is here to test that hypothesis in CI.

## Agent details (if applicable)

Analysis and change prepared with Claude Code, reviewed by me. The commit message carries the full reasoning. Agent notes:

The Windows loader has preloaded every DLL in `torch/lib` since pytorch#35362, because `caffe2_nvrtc.dll` was loaded by name from `CUDAHooks.cpp` and plain `LoadLibraryW` did not search directories registered with `AddDllDirectory`. Two later changes made that moot: pytorch#45674 (2020-10) switched CUDA builds to the in-tree LazyNVRTC, so `caffe2_nvrtc.dll` is only loaded by name on ROCm, and pytorch#49781 (2021-01) moved `at::DynamicLibrary` to `LoadLibraryExW` with `LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`, which searches what `os.add_dll_directory` registers. Meanwhile the preload keeps turning on-demand libraries into import-time failures (pytorch#199894, pytorch#179024, pytorch#182123). The glob only ever preloaded torch's own DLLs with the default search flags; the NVIDIA wheel directories come from the cuda-pathfinder loop (and, on CI, from the toolkit bin dir fallback), which this PR does not touch.

Tested on hardware, since trunk has had no Windows CUDA test shards since pytorch#139336: Windows 11 VM with an RTX 2060 (driver 610.88), 2026-10-26 cu132 nightly wheel, `torch/__init__.py` replaced by this PR's version (hash-checked against the base commit). Results:

- With the stock cuda-pathfinder 1.8.4 the import fails on a clean machine with and without this change, for a reason outside this PR (below). The failure point moves from a `WinError 126` on `shm.dll` in the glob to "DLL load failed while importing _C".
- With a per-library `try` in the pathfinder loop (not part of this PR), import, `torch.cuda.is_available()`, cuDNN conv2d, cuBLAS matmul, cuFFT, cuRAND, cuSOLVER and a jiterator kernel (NVRTC) all pass with the glob removed. 8 of 13 `torch/lib` DLLs are resident afterwards; `caffe2_nvrtc`, `torch_global_deps`, `nvToolsExt64_1`, `libiompstubs5md` and `torch.dll` were never needed.
- The pytorch#199894 mechanism reproduces: a broken stray `zz_broken_test.dll` in `torch/lib` aborts `import torch` with the glob (WinError 193) and is ignored without it.

Found on the way, independent of this PR: the stock cu132 Windows nightly does not import on a clean machine. `nvidia-cuda-cupti` 13.2.86 ships `cupti64_2026.1.2.dll`, which cuda-pathfinder 1.8.4 does not know; the pathfinder loop in `torch/__init__.py` is one `try` around all eleven libraries, so the CUPTI miss also skips cuDNN, `nvidia\cudnn\bin` is never registered, and `cudnn64_9.dll` (a static import of `torch_python.dll` and `torch_cuda.dll`) cannot be found. CUPTI itself still resolves, because a later library in the loop registers the shared `nvidia\cu13\bin` directory. CI does not see this: the Windows CUDA wheel smoke test runs on a T4 with the CUDA toolkit and cuDNN installed under Program Files, and `torch/__init__.py` adds that directory to the search path. A per-library `try` fixes the import with a warning; a pathfinder release that knows the name fixes it silently.

CI on this PR: the full CPU suite on the 12 `win-vs2022-cpu-py3` shards, the `win-vs2022-cuda13.2-py3` build, and the `binaries_wheel` smoke tests for every Python version (CUDA wheels on a T4: import, CUDA tensor, magma, cuDNN, RNN and CNN smoke; XPU wheels import on CPU runners). Not covered: the full CUDA test suite on Windows.

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

Pull Request resolved: pytorch#202206
Approved by: https://github.com/atalman
@atalman

atalman commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

hi @Mohit-25-tech should we close this one ? The issue should be resolved already by #202206

@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

yeah i am closing this one.

This branch was previously deployed

1 inactive deployment
bedrock — abc0f23b Deployed Oct 6, 2026 by pytorchbot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor open source 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.

[Windows] import torch preloads test-only DLLs from torch/lib (aoti_custom_ops.dll), so a failure loading them breaks import

4 participants