Skip to content

Add LazyNVRTC - #45674

Closed
malfet wants to merge 1 commit into
pytorch:masterfrom
malfet:malfet/get-rid-of-caffe2-nvrtc
Closed

malfet wants to merge 1 commit into
pytorch:masterfrom
malfet:malfet/get-rid-of-caffe2-nvrtc

Conversation

@malfet

@malfet malfet commented Oct 1, 2020 •

Copy link
Copy Markdown
Collaborator

Instead of dynamically loading caffe2_nvrtc, lazyNVRTC provides the same functionality by binding all the hooks to lazy bind implementation, very similar to the shared library jump tables:
On the first call, each function from the list tries to get a global handle to the respective shared library and replace itself with the dynamically resolved symbol, using the following template:

  auto fn = reinterpret_cast<decltype(&NAME)>(getCUDALibrary().sym(C10_SYMBOLIZE(NAME)));
  if (!fn)
    throw std::runtime_error("Can't get" ## NAME);
  lazyNVRTC.NAME = fn;
  return fn(...)

Fixes #31985

@malfet
malfet requested a review from apaszke as a code owner October 1, 2020 15:49
@malfet
malfet marked this pull request as draft October 1, 2020 15:50
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Oct 1, 2020
@malfet
malfet force-pushed the malfet/get-rid-of-caffe2-nvrtc branch 3 times, most recently from 90d9aa0 to 3885eba Compare October 2, 2020 05:45
@malfet
malfet requested review from ezyang and ngimel October 2, 2020 05:45
@malfet malfet added module: cuda Related to torch.cuda, and CUDA support in general triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 2, 2020
@malfet
malfet marked this pull request as ready for review October 2, 2020 05:46

@facebook-github-bot facebook-github-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.

@malfet has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@codecov

codecov Bot commented Oct 2, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #45674 into master will decrease coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #45674      +/-   ##
==========================================
- Coverage   68.59%   68.59%   -0.01%     
==========================================
  Files         410      410              
  Lines       52667    52667              
==========================================
- Hits        36129    36128       -1     
- Misses      16538    16539       +1     
Impacted Files Coverage Δ
torch/testing/_internal/expecttest.py 77.55% <0.00%> (-1.03%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6dc256...3885eba. Read the comment docs.

Comment thread aten/src/ATen/cuda/detail/LazyNVRTC.cpp Resolved
@ngimel

ngimel commented Oct 2, 2020

Copy link
Copy Markdown
Collaborator

That's cool, looks like we don't need caffe2_nvrtc at all then? Should we clean up the code related to that?

Instead of dynamically loading `caffe2_nvrtc`, lazyNVRTC provides the same functionality by binding all the hooks to lazy bind implementation, very similar to the shared library jump tables:
On the first call, each function from the list tries to get a global handle to the respective shared library and replace itself with the dynamically resolved symbol, using the following template:
```
  auto fn = reinterpret_cast<decltype(&NAME)>(getCUDALibrary().sym(C10_SYMBOLIZE(NAME)));
  if (!fn)
    throw std::runtime_error("Can't get" ## NAME);
  lazyNVRTC.NAME = fn;
  return fn(...)

```
@malfet
malfet force-pushed the malfet/get-rid-of-caffe2-nvrtc branch from 3885eba to 9185949 Compare October 3, 2020 22:29
@malfet

malfet commented Oct 5, 2020

Copy link
Copy Markdown
Collaborator Author

@ngimel I will clean up build and packaging issues in the followup PR

@facebook-github-bot facebook-github-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.

@malfet has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@malfet merged this pull request in 1558a36.

@malfet
malfet deleted the malfet/get-rid-of-caffe2-nvrtc branch October 13, 2020 02:01
jjsjann123 pushed a commit to jjsjann123/nvfuser that referenced this pull request Oct 29, 2022
Summary:
Instead of dynamically loading `caffe2_nvrtc`, lazyNVRTC provides the same functionality by binding all the hooks to lazy bind implementation, very similar to the shared library jump tables:
On the first call, each function from the list tries to get a global handle to the respective shared library and replace itself with the dynamically resolved symbol, using the following template:
```
  auto fn = reinterpret_cast<decltype(&NAME)>(getCUDALibrary().sym(C10_SYMBOLIZE(NAME)));
  if (!fn)
    throw std::runtime_error("Can't get" ## NAME);
  lazyNVRTC.NAME = fn;
  return fn(...)
```
Fixes pytorch/pytorch#31985

Pull Request resolved: pytorch/pytorch#45674

Reviewed By: ezyang

Differential Revision: D24073946

Pulled By: malfet

fbshipit-source-id: 1479a75e5200e14df003144625a859d312885874
jjsjann123 pushed a commit to jjsjann123/nvfuser that referenced this pull request Nov 10, 2022
Summary:
Instead of dynamically loading `caffe2_nvrtc`, lazyNVRTC provides the same functionality by binding all the hooks to lazy bind implementation, very similar to the shared library jump tables:
On the first call, each function from the list tries to get a global handle to the respective shared library and replace itself with the dynamically resolved symbol, using the following template:
```
  auto fn = reinterpret_cast<decltype(&NAME)>(getCUDALibrary().sym(C10_SYMBOLIZE(NAME)));
  if (!fn)
    throw std::runtime_error("Can't get" ## NAME);
  lazyNVRTC.NAME = fn;
  return fn(...)
```
Fixes pytorch/pytorch#31985

Pull Request resolved: pytorch/pytorch#45674

Reviewed By: ezyang

Differential Revision: D24073946

Pulled By: malfet

fbshipit-source-id: 1479a75e5200e14df003144625a859d312885874
zklaus added a commit to zklaus/pytorch that referenced this pull request Oct 27, 2026
On Windows, `_load_dll_libraries` loads every DLL in torch/lib at import time. The glob was added in pytorch#35362 (2020-03) because `caffe2_nvrtc.dll` was loaded lazily by name from `CUDAHooks.cpp`, and plain `LoadLibraryW` does not search directories registered with `AddDllDirectory`, so the by-name load failed on Python 3.8. 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) switched `at::DynamicLibrary` to `LoadLibraryExW` with `LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`, which does search the directories that `os.add_dll_directory` registers.

The preload also has a cost: it loads libraries that are only meant to be loaded on demand, such as the INSTALL_TEST libraries (`aoti_custom_ops`, `torchbind_test`, `jitbackend_test`, `backend_with_compiler`), and any failure in one of them aborts `import torch` (pytorch#199894, pytorch#179024, pytorch#182123).

This PR removes the glob and the fallback PATH patching that only mattered on systems without `AddDllDirectory`, keeping `os.add_dll_directory` for torch/lib and the other DLL directories. Everything that remains in torch/lib is a link-time dependency of `_C.pyd` or of a library it pulls in, which Windows resolves through the registered directories.

Test Plan
Trunk CI has no Windows CUDA test shards (removed in pytorch#139336); the Windows CPU build and all its test shards pass with this change, and the Windows CUDA build passes. The CUDA side was verified on a Windows 11 VM with an RTX 2060 (driver 610.88) using the 2026-10-26 cu132 nightly wheel with this change applied to its `torch/__init__.py`: import, `torch.cuda.is_available()`, cuDNN conv2d, cuBLAS matmul, cuFFT, cuRAND, cuSOLVER and a jiterator (NVRTC) kernel all pass, 8 of the 13 DLLs in torch/lib end up resident, and a deliberately broken stray DLL in torch/lib no longer aborts the import (with the glob it fails with WinError 193). Lint:

```
lintrunner -a torch/__init__.py
```

Authored with an AI assistant (Claude Code); the analysis and the change were reviewed by the human author.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: cuda Related to torch.cuda, and CUDA support in general oncall: jit Add this issue/PR to JIT oncall triage queue 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.

what should i do to disable libcaffe2_nvrtc.so

5 participants