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

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.


Solution 1:

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