Runtimeerror: cuda error: invalid device ordinal ( Solved )

Runtimeerror_ cuda error_ invalid device ordinal ( Solved )

Runtimeerror: cuda error: invalid device ordinal error occurs if we use any GPU id that is currently configured in the system. When we use any deep learning module like EmotionRecognizer(device=’gpu’,gpu_id=2), we define the GPU id and if it is not configured accordingly then we get this error. Apart from this we also get this error if code by confusion starts counting the GPU Devices from one onwards. But it starts from zero onwards.

Runtimeerror: cuda error: invalid device ordinal ( Solution ) –

There are two approaches to solving this error.  Let’s explore them one by one.

Solution 1: Correcting GPU ID –

Mostly this will fix the issue. If you are not very sure about available GPU devices in the system. Then go for zero as GPU ID. Lets understand with an example.

emotion_detector = EmotionRecognition(device='gpu', gpu_id=0)

Make sure

Solution 2: Use unset CUDA_VISIBLE_DEVICES

Suppose you have misconfigured CUDA_VISIBLE_DEVICES earlier then you can use the command.

It will reset the counter and fix the references.

Solution 3: Check GPU is working properly

Sometimes either because of CUDA driver failure or Hardware Failure if the Interpreter is not able to find all connected GPUs then it will again throw the same error. Here is the code to identify whether GPU is available or not ??

import torch
import time

Let’s run and check.

Runtimeerror cuda error invalid device ordinal check GPU available
Runtimeerror cuda error invalid device ordinal check GPU available

As you can see how can we verify PyTorch Version and check the number of available GPUs with CUDA Support? If no GPUs are there then convert the code CPU friendly.

Solution 4: Upgrade Torch and CUDA  Package –

if you are still getting the same error then you must try to upgrade CUDA Tool kit by downloading it for Windows and Linux. For Torch, you can use pip package manager to upgrade.


Cuda Related Error :

AssertionError: torch not compiled with cuda enabled ( Fix )

ImportError: Could not find ‘nvcuda.dll’ TensorFlow : Solution



Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner