2021-01-13 21:11:53 +00:00
---
title: CUDA
2021-11-18 19:00:48 +00:00
description: Instructions to execute ONNX Runtime applications with CUDA
2021-01-13 21:11:53 +00:00
parent: Execution Providers
nav_order: 1
2021-11-18 19:00:48 +00:00
redirect_from: /docs/reference/execution-providers/CUDA-ExecutionProvider
2021-01-13 21:11:53 +00:00
---
# CUDA Execution Provider
2021-05-10 22:19:37 +00:00
{: .no_toc }
2021-01-13 21:11:53 +00:00
The CUDA Execution Provider enables hardware accelerated computation on Nvidia CUDA-enabled GPUs.
2021-05-10 22:19:37 +00:00
## Contents
{: .no_toc }
* TOC placeholder
{:toc}
2021-07-29 01:27:12 +00:00
## Install
2021-12-11 00:22:10 +00:00
Pre-built binaries of ONNX Runtime with CUDA EP are published for most language bindings. Please reference [Install ORT ](../install ).
2021-07-29 01:27:12 +00:00
2021-05-10 22:19:37 +00:00
## Requirements
2021-12-11 00:22:10 +00:00
2021-08-18 23:54:08 +00:00
Please reference table below for official GPU packages dependencies for the ONNX Runtime inferencing package. Note that ONNX Runtime Training is aligned with PyTorch CUDA versions; refer to the Training tab on https://onnxruntime.ai/ for supported versions.
2021-05-10 22:19:37 +00:00
2021-12-04 05:48:12 +00:00
Note: Because of CUDA Minor Version Compatibility, Onnx Runtime built with CUDA 11.4 should be compatible with any CUDA 11.x version.
Please reference [Nvidia CUDA Minor Version Compatibility ](https://docs.nvidia.com/deploy/cuda-compatibility/#minor-version-compatibility ).
2021-05-10 22:19:37 +00:00
|ONNX Runtime|CUDA|cuDNN|Notes|
|---|---|---|---|
2021-11-30 05:46:42 +00:00
|1.10|11.4|8.2.4 (Linux)< br / > 8.2.2.26 (Windows)|libcudart 11.4.43< br / > libcufft 10.5.2.100< br / > libcurand 10.2.5.120< br / > libcublasLt 11.6.1.51< br / > libcublas 11.6.1.51< br / > libcudnn 8.2.4|
|1.9|11.4|8.2.4 (Linux)< br / > 8.2.2.26 (Windows)|libcudart 11.4.43< br / > libcufft 10.5.2.100< br / > libcurand 10.2.5.120< br / > libcublasLt 11.6.1.51< br / > libcublas 11.6.1.51< br / > libcudnn 8.2.4|
|1.8|11.0.3|8.0.4 (Linux)< br / > 8.0.2.39 (Windows)|libcudart 11.0.221< br / > libcufft 10.2.1.245< br / > libcurand 10.2.1.245< br / > libcublasLt 11.2.0.252< br / > libcublas 11.2.0.252< br / > libcudnn 8.0.4|
2021-05-10 22:19:37 +00:00
|1.7|11.0.3|8.0.4 (Linux)< br / > 8.0.2.39 (Windows)|libcudart 11.0.221< br / > libcufft 10.2.1.245< br / > libcurand 10.2.1.245< br / > libcublasLt 11.2.0.252< br / > libcublas 11.2.0.252< br / > libcudnn 8.0.4|
2021-08-18 23:54:08 +00:00
|1.5-1.6|10.2|8.0.3|CUDA 11 can be built from source|
2021-05-10 22:19:37 +00:00
|1.2-1.4|10.1|7.6.5|Requires cublas10-10.2.1.243; cublas 10.1.x will not work|
|1.0-1.1|10.0|7.6.4|CUDA versions from 9.1 up to 10.1, and cuDNN versions from 7.1 up to 7.4 should also work with Visual Studio 2017|
For older versions, please reference the readme and build pages on the release branch.
2021-01-13 21:11:53 +00:00
## Build
2021-09-15 21:23:42 +00:00
For build instructions, please see the [BUILD page ](../build/eps.md#cuda ).
2021-01-13 21:11:53 +00:00
## Configuration Options
The CUDA Execution Provider supports the following configuration options.
### device_id
The device ID.
Default value: 0
2021-08-05 21:15:03 +00:00
### gpu_mem_limit
2021-01-13 21:11:53 +00:00
The size limit of the device memory arena in bytes. This size limit is only for the execution provider's arena. The total device memory usage may be higher.
2021-07-29 01:27:12 +00:00
s: max value of C++ size_t type (effectively unlimited)
2021-01-13 21:11:53 +00:00
### arena_extend_strategy
The strategy for extending the device memory arena.
Value | Description
-|-
kNextPowerOfTwo (0) | subsequent extensions extend by larger amounts (multiplied by powers of two)
kSameAsRequested (1) | extend by the requested amount
Default value: kNextPowerOfTwo
### cudnn_conv_algo_search
The type of search done for cuDNN convolution algorithms.
Value | Description
-|-
EXHAUSTIVE (0) | expensive exhaustive benchmarking using cudnnFindConvolutionForwardAlgorithmEx
HEURISTIC (1) | lightweight heuristic based search using cudnnGetConvolutionForwardAlgorithm_v7
DEFAULT (2) | default algorithm using CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM
Default value: EXHAUSTIVE
### do_copy_in_default_stream
Whether to do copies in the default stream or use separate streams. The recommended setting is true. If false, there are race conditions and possibly better performance.
Default value: true
2021-12-04 02:52:25 +00:00
### cudnn_conv_use_max_workspace
Check [tuning performance for convolution heavy models ](../performance/tune-performance.md#convolution-heavy-models-and-the-cuda-ep ) for details on what this flag does.
2022-01-08 01:54:43 +00:00
This flag is only supported from the V2 version of the provider options struct when used using the C API. The V2 provider options struct can be created using [this ](https://onnxruntime.ai/docs/api/c/struct_ort_api.html#a0d29cbf555aa806c050748cf8d2dc172 ) and updated using [this ](https://onnxruntime.ai/docs/api/c/struct_ort_api.html#a4710fc51f75a4b9a75bde20acbfa0783 ). Please take a look at the sample below for an example.
2021-12-04 02:52:25 +00:00
Default value: 0
2021-05-10 22:19:37 +00:00
## Samples
2021-01-13 21:11:53 +00:00
### Python
```python
import onnxruntime as ort
model_path = '< path to model > '
providers = [
('CUDAExecutionProvider', {
'device_id': 0,
'arena_extend_strategy': 'kNextPowerOfTwo',
2021-08-05 21:15:03 +00:00
'gpu_mem_limit': 2 * 1024 * 1024 * 1024,
2021-01-13 21:11:53 +00:00
'cudnn_conv_algo_search': 'EXHAUSTIVE',
'do_copy_in_default_stream': True,
}),
'CPUExecutionProvider',
]
session = ort.InferenceSession(model_path, providers=providers)
```
### C/C++
2022-01-08 01:54:43 +00:00
#### Using legacy provider options struct
2021-01-13 21:11:53 +00:00
```c++
OrtSessionOptions* session_options = /* ... */;
OrtCUDAProviderOptions options;
options.device_id = 0;
options.arena_extend_strategy = 0;
2021-08-05 21:15:03 +00:00
options.gpu_mem_limit = 2 * 1024 * 1024 * 1024;
2021-01-13 21:11:53 +00:00
options.cudnn_conv_algo_search = OrtCudnnConvAlgoSearch::EXHAUSTIVE;
options.do_copy_in_default_stream = 1;
SessionOptionsAppendExecutionProvider_CUDA(session_options, &options);
```
2021-03-01 18:53:13 +00:00
2022-01-08 01:54:43 +00:00
#### Using V2 provider options struct
```c++
OrtCUDAProviderOptionsV2* cuda_options = nullptr;
CreateCUDAProviderOptions(&cuda_options);
std::vector< const char * > keys{"device_id", "gpu_mem_limit", "arena_extend_strategy", "cudnn_conv_algo_search", "do_copy_in_default_stream", "cudnn_conv_use_max_workspace"};
std::vector< const char * > values{"0", "2147483648", "kSameAsRequested", "DEFAULT", "1", "1"};
UpdateCUDAProviderOptions(cuda_options, keys.data(), values.data(), 6);
OrtSessionOptions* session_options = /* ... */;
SessionOptionsAppendExecutionProvider_CUDA_V2(session_options, cuda_options);
// Finally, don't forget to release the provider options
ReleaseCUDAProviderOptions(cuda_options);
```
```c#
var cudaProviderOptions = new OrtCUDAProviderOptions(); // Dispose this finally
var providerOptionsDict = new Dictionary< string , string > ();
providerOptionsDict["device_id"] = "0";
providerOptionsDict["gpu_mem_limit"] = "2147483648";
providerOptionsDict["arena_extend_strategy"] = "kSameAsRequested";
providerOptionsDict["cudnn_conv_algo_search"] = "DEFAULT";
providerOptionsDict["do_copy_in_default_stream"] = "1";
providerOptionsDict["cudnn_conv_use_max_workspace"] = "1";
cudaProviderOptions.UpdateOptions(providerOptionsDict);
SessionOptions options = SessionOptions.MakeSessionOptionWithCudaProvider(cudaProviderOptions); // Dispose this finally
```