How to Fix "AssertionError: CUDA unavailable, invalid device 0 requested"

Viewed 7359

I'm trying to use my GPU to run the YOLOR model, and I keep getting the error:

Traceback (most recent call last):
  File "D:\yolor\detect.py", line 198, in <module>
    detect()
  File "D:\yolor\detect.py", line 41, in detect
    device = select_device(opt.device)
  File "D:\yolor\utils\torch_utils.py", line 47, in select_device
    assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device  # check availablity
AssertionError: CUDA unavailable, invalid device 0 requested

When I try to check if CUDA is available with the following:

python3
>>import torch
>>print(torch.cuda.is_available())

I get False, which explains the problem. I tried running the command

py -m pip install torch1.9.0+cu111 torchvision0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

I get the error: ERROR: Invalid requirement: 'torch1.9.0+cu111'

Running nvcc --version, I get:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_May__3_19:41:42_Pacific_Daylight_Time_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0

Thus, I'm not really sure what the issue is, or how to fix it.

EDIT: As @Ivan pointed out, I added the == sign, but still get False when checking if CUDA is available.

3 Answers

You forgot to put the == signs between the packages and the version number. According to the PyTorch installation page:

py -m pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

Ok after 1 week of pain I have founded this solution

1- After download NVIDIA Driver:

  • Go to your window and search for "NVIDIA Control Panel"
  • Then at the bottom left there should be "System Information"
  • Then look for "CUDA Cores"
  • Mine is 384 (year my laptop is antique) (NVIDIA GeForce GT 750M)
  • For CUDA Cores: 384 (corresponds to CUDA Toolkit 9.0)
  • For CUDA Cores: 387 (corresponds to CUDA Toolkit 9.1)
  • For other CUDA Cores you will need to do some more research yourself because I'm honestly don't know where to find this (if you are curious about where I found the one above, its on the second comments "https://github.com/pytorch/pytorch/issues/4546"

2- (Optional) Download Anaconda

  • This is the system I use, the choice is your
  • If you are using Anaconda and have been installing and uninstall to fix this problem. I recommend you to clean uninstall the environment since in my case my file got crash because of repeated install and un-install
  • Here is the link to show you how to do it "https://www.youtube.com/watch?v=dcvdOuvWI-Q&t=107s"

3- After install the right CUDA toolkit for your system

  • Go to "https://pytorch.org"
  • Put in your system details and install the right PyTorch for your system
  • (Optional) if you use Tensorflow as well, go here and install the right version for your CUDA

4- After all of that, in your Anaconda environment (or any environment you are using), type:

  • import torch
  • print(torch.cuda.is_available())

if return True then good job

if not: good luck on your coming week

hope it help and good luck on your journey with yolor (I'm learning it too)

If you are working with VSCode dev containers maybe you forgot to add the GPU to the container.

This can be fixed adding to .devcontainer/devcontainer.json

"runArgs": ["--gpus", "all"]
Related