PyTorch 1.8 cuda cannot use GPU

Mona_Jalal: <p>$ nvidia-smi<br>
Fri Mar  5 22:28:25 2021<br>
±----------------------------------------------------------------------------+<br>
| NVIDIA-SMI 460.39       Driver Version: 460.39       CUDA Version: 11.2     |<br>
|-------------------------------±---------------------±---------------------+<br>
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |<br>
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |<br>
|                               |                      |               MIG M. |<br>
|===============================+======================+======================|<br>
|   0  GeForce GTX 165…  Off  | 00000000:01:00.0 Off |                  N/A |<br>
| N/A   37C    P0     6W /  N/A |     10MiB /  3911MiB |      0%      Default |<br>
|                               |                      |                  N/A |<br>
±------------------------------±---------------------±---------------------+</p>
<p>±----------------------------------------------------------------------------+<br>
| Processes:                                                                  |<br>
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |<br>
|        ID   ID                                                   Usage      |<br>
|=============================================================================|<br>
|    0   N/A  N/A      1224      G   /usr/lib/xorg/Xorg                  4MiB |<br>
|    0   N/A  N/A      1788      G   /usr/lib/xorg/Xorg                  4MiB |<br>
±----------------------------------------------------------------------------+</p>
<p>$ nvcc --version<br>
nvcc: NVIDIA (R) Cuda compiler driver<br>
Copyright (c) 2005-2019 NVIDIA Corporation<br>
Built on Sun_Jul_28_19:07:16_PDT_2019<br>
Cuda compilation tools, release 10.1, V10.1.243</p>
<p>$ python<br>
Python 3.8.5 (default, Jan 27 2021, 15:41:15)<br>
[GCC 9.3.0] on linux</p>
<blockquote>
<blockquote>
<blockquote>
<p>torch.<strong>version</strong><br>
‘1.8.0+cu111’</p>
</blockquote>
</blockquote>
</blockquote>
<p>I have installed PyTorch this way:</p>
<p>$ pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f <a href="https://download.pytorch.org/whl/torch_stable.html" rel="noopener nofollow ugc">https://download.pytorch.org/whl/torch_stable.html</a><br>
Looking in links: <a href="https://download.pytorch.org/whl/torch_stable.html" rel="noopener nofollow ugc">https://download.pytorch.org/whl/torch_stable.html</a><br>
Collecting torch==1.8.0+cu111<br>
Using cached <a href="https://download.pytorch.org/whl/cu111/torch-1.8.0%2Bcu111-cp38-cp38-linux_x86_64.whl" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu111/torch-1.8.0%2Bcu111-cp38-cp38-linux_x86_64.whl</a> (1982.2 MB)<br>
Collecting torchvision==0.9.0+cu111<br>
Using cached <a href="https://download.pytorch.org/whl/cu111/torchvision-0.9.0%2Bcu111-cp38-cp38-linux_x86_64.whl" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu111/torchvision-0.9.0%2Bcu111-cp38-cp38-linux_x86_64.whl</a> (17.6 MB)<br>
Requirement already satisfied: torchaudio==0.8.0 in /home/mona/venv/fall/lib/python3.8/site-packages (0.8.0)<br>
Requirement already satisfied: numpy in /home/mona/venv/fall/lib/python3.8/site-packages (from torch==1.8.0+cu111) (1.20.1)<br>
Requirement already satisfied: typing-extensions in /home/mona/venv/fall/lib/python3.8/site-packages (from torch==1.8.0+cu111) (3.7.4.3)<br>
Requirement already satisfied: pillow&gt;=4.1.1 in /home/mona/venv/fall/lib/python3.8/site-packages (from torchvision==0.9.0+cu111) (8.1.1)<br>
Installing collected packages: torch, torchvision<br>
Successfully installed torch-1.8.0+cu111 torchvision-0.9.0+cu111</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/9/79c7731c4b6a5acdf779cff4080bd7093c695d61.png" data-download-href="https://discuss.pytorch.org/uploads/default/79c7731c4b6a5acdf779cff4080bd7093c695d61" title="Screenshot from 2021-03-05 22-26-40"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/9/79c7731c4b6a5acdf779cff4080bd7093c695d61_2_690x388.png" alt="Screenshot from 2021-03-05 22-26-40" data-base62-sha1="hnj5KPVpPvLv8f0QI72QCUoxuJb" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/9/79c7731c4b6a5acdf779cff4080bd7093c695d61_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/9/79c7731c4b6a5acdf779cff4080bd7093c695d61_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/9/79c7731c4b6a5acdf779cff4080bd7093c695d61_2_1380x776.png 2x" data-dominant-color="E4E1E0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-03-05 22-26-40</span><span class="informations">3840×2160 804 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>You are still or again facing the initialization error from your <a href="https://discuss.pytorch.org/t/cuda-initialization-cuda-unknown-error-this-may-be-due-to-an-incorrectly-set-up-environment-e-g-changing-env-variable-cuda-visible-devices-after-program-start/110601">previous post</a>, so you might have updated the drivers etc. again without a reboot.</p>

Mona_Jalal: <p>Thanks a lot. I did restart again and still cannot access GPU. Do you know how I can fix it?</p>
<p>(fall) mona@goku:~$ python<br>
Python 3.8.5 (default, Jan 27 2021, 15:41:15)<br>
[GCC 9.3.0] on linux<br>
Type “help”, “copyright”, “credits” or “license” for more information.</p>
<blockquote>
<blockquote>
<blockquote>
<p>import torch<br>
torch.cuda.is_available()<br>
False<br>
torch.<strong>version</strong><br>
‘1.8.0+cu111’</p>
</blockquote>
</blockquote>
</blockquote>
<p>By the way, it’s weird after I did the restart, now the NVIDIA driver is not recognized</p>
<pre><code class="lang-auto">
$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
</code></pre>

Mona_Jalal: <p>I followed these exact instructions and now it works:</p>
<pre><code class="lang-auto">sudo apt purge '^.*nvidia*'
sudo apt install ubuntu-desktop
sudo apt purge "^.*cublas*"
sudo apt purge "^.*cuda*"
sudo mv /etc/X11/xorg.conf /etc/X11/xorg.conf.old
sudo apt autoremove
sudo reboot now
</code></pre>
<p>While rebooting, disable Secure Boot and let then Ubuntu load completely.</p>
<pre><code class="lang-auto">sudo ubuntu-drivers devices
sudo ubuntu-drivers autoinstall
sudo reboot now
</code></pre>
<pre><code class="lang-auto">$ pip uninstall torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 
$ pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html
</code></pre>
<p>and now:</p>
<pre><code class="lang-auto">(fall) mona@goku:~$ python
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.cuda.is_available()
True
</code></pre>
<p>also:</p>
<pre><code class="lang-auto">$ nvidia-smi
Mon Mar  8 13:29:44 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.39       Driver Version: 460.39       CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce GTX 165...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   42C    P8     3W /  N/A |     10MiB /  3911MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1207      G   /usr/lib/xorg/Xorg                  4MiB |
|    0   N/A  N/A      1784      G   /usr/lib/xorg/Xorg                  4MiB |
+-----------------------------------------------------------------------------+
</code></pre>
<p>resources:<br>
[1] <a href="https://bugs.launchpad.net/ubuntu/+source/nvidia-graphics-drivers-440/+bug/1871041/comments/12" class="inline-onebox" rel="noopener nofollow ugc">Comment #12 : Bug #1871041 : Bugs : nvidia-graphics-drivers-440 package : Ubuntu</a><br>
[2] <a href="https://pytorch.org/get-started/locally/" class="inline-onebox" rel="noopener nofollow ugc">Start Locally | PyTorch</a></p>

Mona_Jalal: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
so I got the following error (I guess my system had an auto update), and after following all the steps I have provided in the answers above, I still get the same exact error. Do you have any tips how I could fix this?</p>
<pre><code class="lang-auto">$ nvidia-smi
Failed to initialize NVML: Driver/library version mismatch
</code></pre>
<p>a bit more details:<br>
mona@goku:~$ cat /proc/driver/nvidia/version<br>
NVRM version: NVIDIA UNIX x86_64 Kernel Module  460.39  Thu Jan 21 21:54:06 UTC 2021<br>
GCC version:</p>
<p>mona@goku:~$ lsmod | grep ^nvidia<br>
nvidia_uvm           1019904  0<br>
nvidia_drm             53248  4<br>
nvidia_modeset       1228800  5 nvidia_drm<br>
nvidia              34095104  157 nvidia_uvm,nvidia_modeset</p>

Mona_Jalal: <p>I got to get the driver working but now the cuda is not recognized <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<pre><code class="lang-auto">
mona@goku:~$  pip uninstall torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 
Found existing installation: torch 1.8.0+cu111
Uninstalling torch-1.8.0+cu111:
  Would remove:
    /home/mona/venv/phosa/bin/convert-caffe2-to-onnx
    /home/mona/venv/phosa/bin/convert-onnx-to-caffe2
    /home/mona/venv/phosa/lib/python3.8/site-packages/caffe2/*
    /home/mona/venv/phosa/lib/python3.8/site-packages/torch-1.8.0+cu111.dist-info/*
    /home/mona/venv/phosa/lib/python3.8/site-packages/torch/*
Proceed (y/n)? y
  Successfully uninstalled torch-1.8.0+cu111
Found existing installation: torchvision 0.9.0+cu111
Uninstalling torchvision-0.9.0+cu111:
  Would remove:
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision-0.9.0+cu111.dist-info/*
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision.libs/libcudart.05b13ab8.so.11.0
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision.libs/libjpeg.ceea7512.so.62
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision.libs/libpng16.7f72a3c5.so.16
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision.libs/libz.1328edc3.so.1
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchvision/*
Proceed (y/n)? y
  Successfully uninstalled torchvision-0.9.0+cu111
Found existing installation: torchaudio 0.8.0
Uninstalling torchaudio-0.8.0:
  Would remove:
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchaudio-0.8.0.dist-info/*
    /home/mona/venv/phosa/lib/python3.8/site-packages/torchaudio/*
Proceed (y/n)? y
  Successfully uninstalled torchaudio-0.8.0
(phosa) mona@goku:~/research/code/phosa$ nvidia-smi
Thu Mar 25 23:15:15 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.39       Driver Version: 460.39       CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce GTX 165...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   40C    P0     7W /  N/A |     10MiB /  3911MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1239      G   /usr/lib/xorg/Xorg                  4MiB |
|    0   N/A  N/A      1802      G   /usr/lib/xorg/Xorg                  4MiB |
+-----------------------------------------------------------------------------+
(phosa) mona@goku:~/research/code/phosa$ pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Collecting torch==1.8.1+cu111
  Downloading https://download.pytorch.org/whl/cu111/torch-1.8.1%2Bcu111-cp38-cp38-linux_x86_64.whl (1982.2 MB)
     |████████████████████████████████| 1982.2 MB 21 kB/s 
Collecting torchvision==0.9.1+cu111
  Downloading https://download.pytorch.org/whl/cu111/torchvision-0.9.1%2Bcu111-cp38-cp38-linux_x86_64.whl (17.6 MB)
     |████████████████████████████████| 17.6 MB 11.3 MB/s 
Collecting torchaudio==0.8.1
  Downloading torchaudio-0.8.1-cp38-cp38-manylinux1_x86_64.whl (1.9 MB)
     |████████████████████████████████| 1.9 MB 5.1 MB/s 
Requirement already satisfied: typing-extensions in /home/mona/venv/phosa/lib/python3.8/site-packages (from torch==1.8.1+cu111) (3.7.4.3)
Requirement already satisfied: numpy in /home/mona/venv/phosa/lib/python3.8/site-packages (from torch==1.8.1+cu111) (1.19.5)
Requirement already satisfied: pillow&gt;=4.1.1 in /home/mona/venv/phosa/lib/python3.8/site-packages (from torchvision==0.9.1+cu111) (8.1.0)
Installing collected packages: torch, torchvision, torchaudio
Successfully installed torch-1.8.1+cu111 torchaudio-0.8.1 torchvision-0.9.1+cu111
mona@goku:~/research/code/phosa$ python
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.cuda.is_available()
/home/mona/venv/phosa/lib/python3.8/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero. (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:109.)
  return torch._C._cuda_getDeviceCount() &gt; 0
False
&gt;&gt;&gt;
</code></pre>
<p>then I reboot</p>
<p>and now it is working</p>
<p>how I installed the new driver: <a href="https://askubuntu.com/a/1326173/165324" rel="noopener nofollow ugc">https://askubuntu.com/a/1326173/165324</a></p>

ptrblck: <p>Since your systems seems to update drivers behind your back quite often (which doesn’t seem to be wanted), you could disable these automatic updates and manually update the drivers when needed.</p>

mohamed2120: <p>I’m using windows 11.<br>
i used your link.<br>
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f <a href="https://download.pytorch.org/whl/torch_stable.html" rel="noopener nofollow ugc">https://download.pytorch.org/whl/torch_stable.html</a><br>
and it fixed my issue,</p>
<p>Thanks,</p>