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

Hana-Ali: <p>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.</p>
<p>I keep getting the error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "D:\yolor\detect.py", line 198, in &lt;module&gt;
    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
</code></pre>
<p>When I check CUDA availability with:</p>
<pre><code class="lang-auto">py
&gt;&gt;import torch
&gt;&gt;print(torch.cuda.is_available())
</code></pre>
<p>I get <code>False</code>, which explains the problem. I tried running the command:</p>
<p><code>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</code></p>
<p>I get the error:<code> ERROR: Invalid requirement: 'torch1.9.0+cu111'</code></p>
<p>Running <code>nvcc --version</code>, I get:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Additionally, <code>torch.version.cuda</code> gives <code>None</code>.</p>
<p>Running <code>nvidia-smi</code>, I get:</p>
<pre><code class="lang-auto">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      |
+-----------------------------------------------------------------------------+
</code></pre>
<p>Thus, I’m not really sure what the issue is, or how to fix it. Please help!</p>

ptrblck: <p>The issue is most likely raised by a CPU-only PyTorch installation, which isn’t able to use the GPUs.</p>
<aside class="quote no-group" data-username="Hana-Ali" data-post="1" data-topic="127971">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hana-ali/48/40727_2.png" class="avatar"> Hana-Ali:</div>
<blockquote>
<p>I get the error:<code> ERROR: Invalid requirement: 'torch1.9.0+cu111'</code></p>
</blockquote>
</aside>
<p>I cannot reproduce the issue and was able to install this pip wheel, so you might need to update <code>pip</code> or maybe create a new virtual environment and try to reinstall PyTorch there.</p>

Hana-Ali: <p>Hello,</p>
<p>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!</p>

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

ptrblck: <p>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?</p>

liuhx: <p>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.</p>

ptrblck: <p>Your driver is most likely too old as the <code>cu111</code> binary uses CUDA11.1 Update 1 and based on <a href="https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html">Table 3</a> your Windows setup would need <code>&gt;=456.81</code>.</p>

liuhx: <p>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.</p>

ptrblck: <p>I would upgrade to the latest driver so that you wouldn’t have to worry about some future releases.</p>