Skip to content

Commit cc83051

Browse files
committed
[CUDA] work with pytorch and preload dlls
1 parent 9171013 commit cc83051

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

docs/execution-providers/CUDA-ExecutionProvider.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ ONNX Runtime built with cuDNN 8.x is not compatible with cuDNN 9.x, and vice ver
3737

3838
Note: Starting with version 1.19, **CUDA 12.x** becomes the default version when distributing [ONNX Runtime GPU packages](https://pypi.org/project/onnxruntime-gpu/) in PyPI.
3939

40+
To reduce the need for manual installations of CUDA and cuDNN, and ensure seamless integration between ONNX Runtime and PyTorch, the `onnxruntime-gpu` Python package offers API to load CUDA and cuDNN dynamic link libraries (DLLs) appropriately. For more details, refer to the [Work with PyTorch](#work-with-pytorch) and [Preload DLLs](#preload-dlls) sections.
41+
4042
### CUDA 12.x
4143

4244
| ONNX Runtime | CUDA | cuDNN | Notes |
@@ -76,6 +78,111 @@ For older versions, please reference the readme and build pages on the release b
7678

7779
For build instructions, please see the [BUILD page](../build/eps.md#cuda).
7880

81+
## Compatibility with PyTorch
82+
83+
The `onnxruntime-gpu` package is designed to work seamlessly with [PyTorch](https://pytorch.org/), provided both are built against the same major version of CUDA and cuDNN. When installing PyTorch with CUDA support (e.g., CUDA 12.x), the necessary CUDA and cuDNN DLLs are included, eliminating the need for separate installations of the CUDA toolkit or cuDNN.
84+
85+
To ensure ONNX Runtime utilizes the DLLs installed by PyTorch, you can preload these libraries before creating an inference session. This can be achieved by either importing PyTorch or by using the `onnxruntime.preload_dlls()` function.
86+
87+
**Example 1: Importing PyTorch**
88+
89+
```python
90+
import torch
91+
import onnxruntime
92+
93+
# Create an inference session with CUDA execution provider
94+
session = onnxruntime.InferenceSession("model.onnx", providers=["CUDAExecutionProvider"])
95+
```
96+
97+
98+
**Example 2: Using `preload_dlls` Function**
99+
100+
```python
101+
import onnxruntime
102+
103+
# Preload necessary DLLs
104+
onnxruntime.preload_dlls()
105+
106+
# Create an inference session with CUDA execution provider
107+
session = onnxruntime.InferenceSession("model.onnx", providers=["CUDAExecutionProvider"])
108+
```
109+
110+
## Preload DLLs
111+
112+
The `onnxruntime-gpu` package provides the `preload_dlls` function to preload CUDA, cuDNN, and Microsoft Visual C++ (MSVC) runtime DLLs. This function offers flexibility in specifying which libraries to load and from which directories.
113+
114+
**Function Signature:**
115+
116+
```python
117+
onnxruntime.preload_dlls(cuda=True, cudnn=True, msvc=True, directory=None)
118+
```
119+
120+
121+
**Parameters:**
122+
123+
- `cuda` (bool): Preload CUDA DLLs if set to `True`.
124+
- `cudnn` (bool): Preload cuDNN DLLs if set to `True`.
125+
- `msvc` (bool): Preload MSVC runtime DLLs if set to `True`.
126+
- `directory` (str or None): Directory to load the DLLs from.
127+
- `None`: Search in default directories.
128+
- `""` (empty string): Search in NVIDIA site packages.
129+
- Specific path: Load DLLs from the specified directory.
130+
131+
**Default Search Order:**
132+
133+
When `directory=None`, the function searches for CUDA and cuDNN DLLs in the following order:
134+
135+
1. On Windows, the `lib` directory under the PyTorch installation.
136+
2. Python site-packages directories for NVIDIA CUDA or cuDNN libraries (e.g., `nvidia_cuda_runtime_cu12`, `nvidia_cudnn_cu12`).
137+
3. Fallback to the default DLL loading behavior.
138+
139+
By preloading the necessary DLLs using default search order, you can ensure that ONNX Runtime operates seamlessly with PyTorch.
140+
141+
**Installing CUDA and cuDNN via `onnxruntime-gpu`:**
142+
143+
You can install the necessary CUDA and cuDNN runtime DLLs alongside the `onnxruntime-gpu` package using pip:
144+
145+
```bash
146+
pip install onnxruntime-gpu[cuda,cudnn]
147+
```
148+
149+
150+
**Preloading DLLs from NVIDIA Site Packages:**
151+
152+
To preload CUDA and cuDNN DLLs from NVIDIA site packages and display debug information:
153+
154+
```python
155+
import onnxruntime
156+
157+
# Preload DLLs from NVIDIA site packages
158+
onnxruntime.preload_dlls(directory="")
159+
160+
# Print debug information
161+
onnxruntime.print_debug_info()
162+
```
163+
164+
165+
**Loading DLLs from Specific Directories:**
166+
167+
To load DLLs from a specified location, set the `directory` parameter to an absolute path or a path relative to the ONNX Runtime package root.
168+
169+
**Example: Loading CUDA from System Installation and cuDNN from NVIDIA Site Package**
170+
171+
```python
172+
import os
173+
import onnxruntime
174+
175+
# Load CUDA DLLs from system installation
176+
cuda_path = os.path.join(os.environ["CUDA_PATH"], "bin")
177+
onnxruntime.preload_dlls(cuda=True, cudnn=False, directory=cuda_path)
178+
179+
# Load cuDNN DLLs from NVIDIA site package
180+
onnxruntime.preload_dlls(cuda=False, cudnn=True, directory="..\\nvidia\\cudnn\\bin")
181+
182+
# Print debug information
183+
onnxruntime.print_debug_info()
184+
```
185+
79186
## Configuration Options
80187

81188
The CUDA Execution Provider supports the following configuration options.

docs/install/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ For ONNX Runtime GPU package, it is required to install [CUDA](https://developer
3838
* In Windows, the path of CUDA `bin` and cuDNN `bin` directories must be added to the `PATH` environment variable.
3939
* In Linux, the path of CUDA `lib64` and cuDNN `lib` directories must be added to the `LD_LIBRARY_PATH` environment variable.
4040

41+
For `onnxruntime-gpu` package, it is possible to work with PyTorch without the need for manual installations of CUDA or cuDNN. Refer to [Work with PyTorch](../execution-providers/CUDA-ExecutionProvider.md#work-with-pytorch) for more information.
42+
4143
## Python Installs
4244

4345
### Install ONNX Runtime CPU

0 commit comments

Comments
 (0)