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 that CUDA is unavailable, not sure how to fix.

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 check CUDA availability with:

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

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

py -m pip install --user torch==1.9.0+cu111 torchvision==0.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

Additionally, torch.version.cuda gives None.

Running nvidia-smi, I get:

Wed Jul 28 22:42:06 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 471.41       Driver Version: 471.41       CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0  On |                  N/A |
| 39%   36C    P5    17W / 215W |   1533MiB /  8192MiB |      3%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1396    C+G   Insufficient Permissions        N/A      |
|    0   N/A  N/A      2940    C+G   Insufficient Permissions        N/A      |
|    0   N/A  N/A      4812    C+G   ...obeNotificationClient.exe    N/A      |
|    0   N/A  N/A      4876    C+G   ...Roaming\Zoom\bin\Zoom.exe    N/A      |
|    0   N/A  N/A      5200    C+G   ...\app-1.0.9002\Discord.exe    N/A      |
|    0   N/A  N/A      7256    C+G   ...Container\nvcontainer.exe    N/A      |
|    0   N/A  N/A      7876    C+G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      9944    C+G   ...ekyb3d8bbwe\YourPhone.exe    N/A      |
|    0   N/A  N/A     10692    C+G   ...t\Teams\current\Teams.exe    N/A      |
|    0   N/A  N/A     10988    C+G   ...zilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A     11636    C+G   ...perience\NVIDIA Share.exe    N/A      |
|    0   N/A  N/A     11948    C+G   ...perience\NVIDIA Share.exe    N/A      |
|    0   N/A  N/A     13396    C+G   ...lication\WebCompanion.exe    N/A      |
|    0   N/A  N/A     14028    C+G   ...w5n1h2txyewy\SearchUI.exe    N/A      |
|    0   N/A  N/A     14060    C+G   ...zilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A     14664    C+G   ...les\NZXT CAM\NZXT CAM.exe    N/A      |
|    0   N/A  N/A     15760    C+G   ...t\Teams\current\Teams.exe    N/A      |
|    0   N/A  N/A     17764    C+G   ...bbwe\Microsoft.Photos.exe    N/A      |
|    0   N/A  N/A     18328    C+G   ...icrosoft VS Code\Code.exe    N/A      |
|    0   N/A  N/A     18896    C+G   ...es.TextInput.InputApp.exe    N/A      |
|    0   N/A  N/A     23256    C+G   ...y\ShellExperienceHost.exe    N/A      |
+-----------------------------------------------------------------------------+

Thus, I’m not really sure what the issue is, or how to fix it. Please help!

The issue is most likely raised by a CPU-only PyTorch installation, which isn’t able to use the GPUs.

I cannot reproduce the issue and was able to install this pip wheel, so you might need to update pip or maybe create a new virtual environment and try to reinstall PyTorch there.

1 Like

Hello,

I actually realized (way too late) that it was because pip kept installing torch from cached, and so kept installing the cpu only version (only confirmed when I went into torch/version.py). In any case, it still didn’t work in my normal environment, but worked when I ran it in a new fresh conda install. Thank you!

I have this problem too, but my Torch version is GPU and print(torch.cuda.is_available()) prints true.Please help!

In case any CUDA operation fails I would guess your driver might be too old for the used CUDA runtime used in the pip wheels / conda binaries. Could you explain your setup a bit more and which setup was working in the past as well as what the differences are to your current setup?

First of all, thank you very much for your help. Let me explain my Settings. Cuda version is 11.1, driver version is 456.38, the version number can be displayed when running nvidia-smi and nvcc-V. Print (torch 1.8.1+cu111-cp38-cp38-win_amd64); There is no error when running the import Torch, but yolov5 still displays: "AssertionError: CUDA unavailable, invalid device 0 requested ", now I re-create a virtual environment in Conda, I am worried that it may be because the previous version is too high. The torch version now installed is Torch 1.10.0+ cu102-cp38-win_amd64 .Now you may need to reinstall your system or environment because you didn’t install it yourself.The above is my situation, thank you again for your help, I wonder if I have a clear description of my situation.

Your driver is most likely too old as the cu111 binary uses CUDA11.1 Update 1 and based on Table 3 your Windows setup would need >=456.81.

Grateful! Check the components in NVIDIA control panel and find that the CUDA version installed in driver 456.38 is 11.1.7. In this case, is it better to lower the CUDA version or upgrade the driver version? The computer is MSI Dragon GeForce RTX3090.

I would upgrade to the latest driver so that you wouldn’t have to worry about some future releases.