"CUDA is not available" after installing a different version of CUDA

deep-learningLearner: <p>Previously, I could run pytorch without problem. After installing a new version (older version) of CUDA, I got following error, and cannot resume this.</p>
<pre><code class="lang-auto">UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling
  warnings.warn('User provided device_type of \'cuda\', but CUDA is not available. Disabling')
</code></pre>
<p>I use Windows 11 with WSL 2.<br>
My GPU is GeForce RTX 3080 and CUDA Version is 11.6 (that was installed at the beginning in the factory of the PC). nvidia-smi result is as below.<br>
±----------------------------------------------------------------------------+<br>
| NVIDIA-SMI 495.29.05    Driver Version: 510.06       CUDA Version: 11.6     |<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  NVIDIA GeForce …  On   | 00000000:01:00.0  On |                  N/A |<br>
| N/A   61C    P8    23W /  N/A |    538MiB / 16384MiB |     N/A      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>
|  No running processes found                                                 |<br>
±----------------------------------------------------------------------------+</p>
<p>I installed Pytorch (for CUDA 11.3) from the official site using following command.<br>
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f <a href="https://download.pytorch.org/whl/cu113/torch_stable.html" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu113/torch_stable.html</a></p>
<p>This worked without no problem, though my CUDA version is 11.6 and Pytorch is for CUDA 11.3</p>
<hr>
<p>After a while, I installed an older version of CUDA (11.5), since I wanted to use CuPy and the latest version of CUDA that CuPY supports is 11.5.</p>
<p>I have downloaded CUDA from following link and installed. <a href="https://developer.nvidia.com/cuda-downloads?target_os=Windows&amp;target_arch=x86_64&amp;target_version=11" class="inline-onebox" rel="noopener nofollow ugc">CUDA Toolkit 11.5 Downloads | NVIDIA Developer</a></p>
<p>As a result, I have been able to use CuPy as expected, but not run Pytorch with GPU. When I run pytorch, I got following error, and CPU runs instead.</p>
<pre><code class="lang-auto">UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling
  warnings.warn('User provided device_type of \'cuda\', but CUDA is not available. Disabling')
</code></pre>
<p>“torch.cuda.is_available()” returned False.</p>
<p>Though I had installed CUDA11.5, nvidia-smi still showed CUDA 11.6, while nvcc --version showed CUDA 11.5</p>
<hr>
<p>Seeing the situation, I have uninstalled CUDA11.5, using “App &amp; Features” function of Windows GUI. I have uninstalled and reinstalled Pytorch.<br>
However, the situations above have still continue.</p>
<ul>
<li>GPU cannot be used by torch with error “CUDA is not available. Disabling”</li>
<li>torch.cuda.is_available() → False</li>
<li>Strangely, CuPy can be still used</li>
<li>(“nvcc” is uninstalled and cannot be used. )</li>
</ul>
<hr>
<p>I have tried for a while and no idea for now. Hope someone can provide me a hint.</p>

ptrblck: <p>Based on your description I would guess that you would need to reinstall PyTorch with the CUDA11.3 runtime as the driver downgrade might have broken your installation.</p>

deep-learningLearner: <p>Thank you for reply.</p>
<p>I have reinstalled from following link,</p>
<p><code>pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html</code></p>
<p>However, the situation is the same.</p>
<p>Following is the result of deviceQuery of CUDA. It suggest there is surely a GPU with driver version 11.6/ runtime version 11.5 …</p>
<pre><code class="lang-auto"> ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "NVIDIA GeForce RTX 3080 Laptop GPU"
  CUDA Driver Version / Runtime Version          11.6 / 11.5
  CUDA Capability Major/Minor version number:    8.6
  Total amount of global memory:                 16384 MBytes (17179344896 bytes)
  (048) Multiprocessors, (128) CUDA Cores/MP:    6144 CUDA Cores
  GPU Max Clock rate:                            1245 MHz (1.25 GHz)
  Memory Clock rate:                             6001 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 4194304 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total shared memory per multiprocessor:        102400 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1536
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Managed Memory:                Yes
  Device supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     &lt; Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) &gt;

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.6, CUDA Runtime Version = 11.5, NumDevs = 1
Result = PASS
</code></pre>
<p>Followings  are current ideas  that may be causing the issue. How do you think?</p>
<ul>
<li>
<p>Cuda toolkit 11.5 is not supported by Pytroch, so need downgrading of the toolkit to 11.3</p>
</li>
<li>
<p>There are different similar directories in /usr/local/ and Pytorch is confusing which to use.   There are “cuda”,  “cuda-11”, and  “cuda-11.5”  folders  in /usr/local</p>
</li>
</ul>

ptrblck: <aside class="quote no-group quote-modified" data-username="deep-learningLearner" data-post="3" data-topic="136596">
<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/deep-learninglearner/48/43888_2.png" class="avatar"> deep-learningLearner:</div>
<blockquote>
<p>Cuda vtoolkit 1.5 is not supported by Pytroch , so need downgrading of the toolkit to 11.3</p>
</blockquote>
</aside>
<p>No, that’s not the case as I’m using CUDA 11.5 in a source build as well as the current CUDA11.3 nightly and stable conda binaries and pip wheels.</p>
<aside class="quote no-group" data-username="deep-learningLearner" data-post="3" data-topic="136596">
<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/deep-learninglearner/48/43888_2.png" class="avatar"> deep-learningLearner:</div>
<blockquote>
<p>There are different similar directories in /usr/local/ and Pytorch is confusing which to use. There are “cuda”, “cuda-11”, and “cuda-11.5” folders in /usr/local</p>
</blockquote>
</aside>
<p>The pip wheels and conda binaries use their own CUDA runtime as specified in the install command. Your local CUDA toolkit will only be used when you are building PyTorch from source or a custom CUDA extension.<br>
To run the binaries you would only need a properly installed driver.</p>

deep-learningLearner: <p>Thank you. It is helpful to know Cuda 11.5 is not the issue.</p>
<p>Just for additional information, GPU worked with a CUDA sample code, so the issue seems to be not in CUDA installment itself, but how that is recognized by Pytorch.</p>
<pre><code class="lang-auto">/usr/local/cuda/samples/4_Finance/BlackScholes$ ./BlackScholes
[./BlackScholes] - Starting...
GPU Device 0: "Ampere" with compute capability 8.6

Initializing data...
...allocating CPU memory for options.
...allocating GPU memory for options.
...generating input data in CPU mem.
...copying input data to GPU mem.
Data init done.

Executing Black-Scholes GPU kernel (512 iterations)...
Options count             : 8000000
BlackScholesGPU() time    : 0.237344 msec
Effective memory bandwidth: 337.063867 GB/s
Gigaoptions per second    : 33.706387

BlackScholes, Throughput = 33.7064 GOptions/s, Time = 0.00024 s, Size = 8000000 options, NumDevsUsed = 1, Workgroup = 128

Reading back GPU results...
Checking the results...
...running CPU calculations.

Comparing the results...
L1 norm: 1.741792E-07
Max absolute error: 1.192093E-05

Shutting down...
...releasing GPU memory.
...releasing CPU memory.
Shutdown done.

[BlackScholes] - Test Summary

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

Test passed
</code></pre>

ptrblck: <aside class="quote no-group quote-modified" data-username="deep-learningLearner" data-post="5" data-topic="136596">
<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/deep-learninglearner/48/43888_2.png" class="avatar"> deep-learningLearner:</div>
<blockquote>
<p>so the issue seems to be not in CUDA installment itself, but how that is used by Pytorch.</p>
</blockquote>
</aside>
<p>Given that the initial setup was working, I also wouldn’t blame PyTorch but would guess that reinstalling the drivers has broken the shipped CUDA runtime.<br>
Try to create a new virtual environment and reinstall the wheels there. If that doesn’t help, try to reinstall your initial setup.</p>
<p>EDIT: in case you haven’t done it yet, restart the laptop first and see if some dangling libs are still loaded and let me know how it goes.</p>

deep-learningLearner: <p>Thanks.</p>
<p>I have tried using Nvidia NGC container with Pytorch.<br>
(<a href="https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch" class="inline-onebox" rel="noopener nofollow ugc">PyTorch | NVIDIA NGC</a>)</p>
<p>Using this, it seems CUDA works (torch.cuda.is_available() returns TRUE.)</p>
<p>I will use the container for now for Pytorch. (It is better if I could use it with local environment, but it seems current solution is  reinstalling CUDA driver, and I am afraid this may cause further confusions and eventually lead to factory reset… considering my configuration skills)</p>

ptrblck: <p>The docker container might be a good workaround for now.<br>
In case you decide to try to fix your environment, check all CUDA and driver installation and see if you might have mixed them (e.g. using a <code>.run</code> file initially and then installing another CUDA toolkit via the <code>.deb</code> file etc.). In such a case, make sure to clean the environment first and reinstall the new driver using your preferred way.</p>

deep-learningLearner: <p>Thank you for the advice. Will do so when I reconfigure local environment.</p>

lorenzznerol: <p>From <a href="https://stackoverflow.com/a/63162492/11154841" rel="noopener nofollow ugc">PyTorch Cuda with anaconda not available</a> follows that you need to do a clean uninstall before you reinstall pytorch and attached packages. You might also check whether there are old files somewhere after the uninstall, see <a href="https://stackoverflow.com/a/61599289/11154841" rel="noopener nofollow ugc">How can l uninstall PyTorch?</a>.</p>
<p>If you want to install pytorch, you always need to install everything needed in one go, messing around with cuda as an additional package is error prone. It is recommended to install pytorch and additional packages in one go from conda:</p>
<blockquote>
<p>Anaconda is our recommended package manager since it installs all dependencies.</p>
</blockquote>
<p>And pip does not install all dependencies, when this post is right:</p>
<blockquote>
<p>It uses preinstalled CUDA and doesn’t download own CUDA Toolkit.</p>
</blockquote>
<p>See <a href="https://stackoverflow.com/a/65355715/11154841" rel="noopener nofollow ugc">How to run pytorch with NVIDIA “cuda toolkit” version instead of the official conda “cudatoolkit” version?</a>.</p>
<p>Therefore, using pip is your actual problem, because pip installed pytorch can be expected to need exactly the right version, else the dependencies can fail.</p>
<p>In short: do not use pip. Use conda with an independent “cudatoolkit”, do not use the system “cuda toolkit”, and that is also what pytorch recommends.</p>

ptrblck: <aside class="quote no-group" data-username="lorenzznerol" data-post="10" data-topic="136596">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/ecccb3/48.png" class="avatar"> lorenzznerol:</div>
<blockquote>
<p>Therefore, using pip is your actual problem, because pip installed pytorch can be expected to need exactly the right version, else the dependencies can fail.</p>
</blockquote>
</aside>
<p>In case “need exactly the right version” refers to a local CUDA toolkit, then it’s wrong.<br>
The pip wheels statically link the CUDA runtime and don’t need a local CUDA toolkit same as the <code>conda</code> binaries. The difference between these two is the way to statically link (pip wheels) vs. dynamically link (conda binaries) the CUDA runtime.</p>

arpit_sahni: <p>hi! How can I install CUDA toolkit for pytorch on windows 11?? the latest pytorch supports is 11.3 but it does not give a option to download a windows 11 version and only gives option for windows 10</p>

ptrblck: <p>I don’t see a Windows 10 - specific install instruction and from the install page it also seems as if Windows 11 should be supported:</p>
<blockquote>
<p>PyTorch is supported on the following Windows distributions:</p>
<ul>
<li><a href="https://www.microsoft.com/en-us/windows">Windows</a> 7 and greater; <a href="https://www.microsoft.com/en-us/software-download/windows10ISO">Windows 10</a> or greater recommended.</li>
</ul>
</blockquote>
<p>Where did you find the Windows 10 limitation?</p>