How do you run a ONNX model on a GPU?
Asked Answered
F

3

17

I'm trying to run an ONNX model

import onnxruntime as ort
import onnxruntime.backend
model_path = "model.onnx"

#https://microsoft.github.io/onnxruntime/
ort_sess = ort.InferenceSession(model_path)


print( ort.get_device()  )

This prints out

cpu

How can I make it run on my GPU? How can I confirm it's working?

Fusion answered 20/10, 2020 at 19:23 Comment(0)
P
24

You probably installed the CPU version. Try uninstalling onnxruntime and install GPU version, like pip install onnxruntime-gpu.

Then:

>>> import onnxruntime as ort
>>> ort.get_device()
'GPU'
Parvati answered 22/10, 2020 at 1:4 Comment(1)
Single line version would be python -c "import onnxruntime as ort;print(ort.get_device())" Pentstemon
U
9

get_device() command gives you the supported device to the onnxruntime. For CPU and GPU there is different runtime packages are available.

Currently your onnxruntime environment support only CPU because you have installed CPU version of onnxruntime.

If you want to build onnxruntime environment for GPU use following simple steps.

Step 1: uninstall your current onnxruntime

>> pip uninstall onnxruntime

Step 2: install GPU version of onnxruntime environment

>>pip install onnxruntime-gpu

Step 3: Verify the device support for onnxruntime environment

>> import onnxruntime as rt
>> rt.get_device()
'GPU'

Step 4: If you still encounter any issue please check with your cuda and CuDNN versions, that must be compatible to each other. Please refer this link here to understand about the version compatibility between cuda and CuDNN.

Unfriended answered 9/6, 2021 at 12:6 Comment(0)
C
3

your onnxruntime-gpu version should match your cuda and cudnn version,you can check their relations from the offical web site: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html

Casement answered 15/11, 2021 at 11:44 Comment(1)
I think this is the problem in my case. It looks like there's no support for CUDA 11.6 yet. Thanks.Skillless

© 2022 - 2025 — McMap. All rights reserved.