Skip to content

How to fix ‘Runtimeerror: cuda error: invalid device ordinal’?

  • by
  • 2 min read

Python is one of the most popular programming languages in modern times, especially because it’s rather simple for even total beginners to understand while also being powerful enough to handle tasks like automation, computer vision and AI/ML among other things.

That said, programming is still hard and random bugs and glitches keep popping up all the time. In this article, we’re taking a look at the “runtimeerror: cuda error: invalid device ordinal” error in Python and two ways you can use to fix the problem.

Also read: Is Python case sensitive when dealing with identifiers?


Use the right GPU ID

When writing code, every hardware component that you use, especially the GPU and the camera, is assigned a hardware ID. It’s a numeric value that starts at zero and then goes up depending on how many of a particular device you have. So if you have two GPUs, the first one will get zero and the second one will get one as the GPU ID. The same goes for CUDA devices. 

When writing code, pay special attention to any such flags and options where you’re specifying the GPU or Camera ID. If you only have one, you don’t even need to specify anything as most libraries will automatically pull the default hardware available, but in case you do, ensure you’re using the right ID. In most cases, you can get away with just ‘0’ as that’s the default ID for most hardware. 


Check your training model

Sometimes, errors can arise if your training model or any other file that you’re using as a reference for your model is missing. Make sure the required files are placed in the same directory as the script itself and is being called when you run the program.

Also read: Fix: Python was not found; run without arguments to install 

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>