Torch not compiled with CUDA enabled

OAAA: <p>I am trying to use PyTorch for the first time with Pycharm. When trying to use cuda, it is showing me this error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:/Users/omara/PycharmProjects/test123/test.py", line 4, in &lt;module&gt;
    my_tensor = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32, device="cuda")
  File "C:\Users\omara\anaconda3\envs\deeplearning\lib\site-packages\torch\cuda\__init__.py", line 166, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled
</code></pre>
<p>I already installed cuda toolkit using the pytorch command in the anaconda</p>
<pre><code class="lang-auto">(deeplearning) C:\WINDOWS\system32&gt;conda info

     active environment : deeplearning
    active env location : C:\Users\omara\anaconda3\envs\deeplearning
            shell level : 2
       user config file : C:\Users\omara\.condarc
 populated config files : C:\Users\omara\.condarc
          conda version : 4.9.2
    conda-build version : 3.20.5
         python version : 3.8.5.final.0
       virtual packages : __cuda=11.2=0
                          __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Users\omara\anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\omara\anaconda3\pkgs
                          C:\Users\omara\.conda\pkgs
                          C:\Users\omara\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\omara\anaconda3\envs
                          C:\Users\omara\.conda\envs
                          C:\Users\omara\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.19041
          administrator : True
             netrc file : None
           offline mode : False

</code></pre>
<pre><code class="lang-auto"></code></pre>

Dwight_Foster: <p>how did you install pytorch. Did you use the correct install. This is the pip install: pip install torch===1.7.1+cu110 torchvision===0.8.2+cu110 torchaudio===0.7.2 -f <a href="https://download.pytorch.org/whl/torch_stable.html" rel="noopener nofollow ugc">https://download.pytorch.org/whl/torch_stable.html</a></p>

OAAA: <p>I used<br>
conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch</p>
<p>the torch library is working, if I just use device=cpu instead of device=cuda, then I don’t get any error</p>
<pre><code class="lang-auto">import torch

print(torch.__version__)
my_tensor = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32, device="cpu")
print(my_tensor)
torch.cuda.is_available()
</code></pre>

Dwight_Foster: <p>what happens when you run nvidia-smi and when you print torch.cuda.is_available. What does the pytorch version print out too.</p>

OAAA: <p>Thank you, Dwight for trying to help</p>
<pre><code class="lang-auto">print(torch.cuda.is_available())
</code></pre>
<p>output false for me</p>
<p>but running nvidia-smi from the anaconda prompt showing I have CUDA Version 11.2</p>
<pre><code class="lang-auto">(deeplearning) C:\WINDOWS\system32&gt;nvidia-smi
Sun Feb 21 10:06:38 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.89       Driver Version: 460.89       CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| 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  GeForce RTX 2060   WDDM  | 00000000:01:00.0 Off |                  N/A |
| N/A   40C    P8     9W /  N/A |    164MiB /  6144MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
</code></pre>
<p>and when I run <code>conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch</code></p>
<p>it is showing me, all requested packages already installed</p>
<p>I tried to uninstall CUDA and Pytorch and install them again but nothing changed</p>

OAAA: <p>I created a new environment, and it is working well. I don’t really know what the exact problem is, but it is solved.<br>
Thank you for your assistance, Dwight <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Dwight_Foster: <p>No problem glad you got it to work.</p>

timmyvg: <p>I have print(torch.cuda.is_available()) = False after install torchgeometry. My PyTorch worked well until I installed it. I will update if I find any solution.</p>

ptrblck: <p>Could you check, if <code>torchgeometry</code> might have uninstalled your previous PyTorch installation and installed a CPU-only version instead? During the install step of <code>torchgeometry</code> the logs should indicate this and you might want to install it via <code>pip install ... --no-dependencies</code> or change the requirement for this package.</p>

timmyvg: <p>Yes, you are right. I installed the wrong version (CPU-only version).</p>

desmond13: <p>Hi <a class="mention" href="/u/timmyvg">@timmyvg</a> , how did you uninstall pytorch cpu only and installed a CUDA-enabled pytorch?</p>

desmond13: <p>In the end I switched from Conda to virtualenv and it worked at the first try.</p>
<p>I created my virtualenv with <code>virtualenv virtualenv_name</code></p>
<p>Then I did</p>
<p><code>workon virtualenv_name</code></p>
<p>then, I installed pytorch as it is specified on the official pytorch website (but selecting pip instead of conda) as package manager (<a href="https://pytorch.org/get-started/locally/" class="inline-onebox" rel="noopener nofollow ugc">Start Locally | PyTorch</a>).</p>
<p><code>conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch</code></p>
<p>This worked for me and now I have a CUDA-enabled version of pytorch on my machine.</p>

Eduardo_Barrera: <p>For 2022 readers, please go to the official PyTorch website found <a href="https://pytorch.org/get-started/locally/" rel="noopener nofollow ugc">here</a> and select the appropriate choices in the table they provide. Copy and paste the auto-generated command provided, which will uninstall existing torch/torchvision/torchaudio versions and install the CUDA enabled versions.</p>
<p>If you are working in a conda environment, please remove existing <code>conda install</code> torch versions before adding packages to pip.</p>

kylemeador: <p>In my case,<br>
<code>conda install python=3.10 pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch</code> failed.<br>
Eventually, I installed successfully by adding <code>-c nvidia</code> to the above, resulting in<br>
<code>conda install python=3.10 pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch -c nvidia</code></p>

Kash_Rangan: <p>Hi I have tried all the solutions listed above; but still not able to resolve the error.</p>
<ul>
<li>Tried creating new environment in Conda</li>
<li>Tried installing using both conda and pip</li>
<li>Tried installing both versions 11.7 and 11.8</li>
</ul>
<p>Could you please suggest any other alternatives. ( I am not familiar with virtualenv)</p>

ptrblck: <p>Could you show some install logs of e.g the attempt to install the current <code>2.0.0+cu117</code> pip wheel in an empty and new environment, please?<br>
The log could give us a clue if e.g. <code>pip</code> is unable to find the right wheel if your Python version is too old or any other issue occurs.</p>

Kash_Rangan: <p>Hi I have got a new laptop with RTX 4060 with CUDA 12.0. Realized that PyTorch does not provide support for CUDA 12.0. But the only way we can run is using a Docker container - <a href="https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch" class="inline-onebox" rel="noopener nofollow ugc">PyTorch | NVIDIA NGC</a>. Could you please suggest any alternative approaches. I am new to PyTorch and is there an easier way to get this working.</p>

ptrblck: <p>The PyTorch binaries ship with their own CUDA runtime and CUDA libraries (such as cuBLAS, cuDNN, NCCL, etc.). Your locally installed CUDA toolkit will be used if you build PyTorch from source or custom CUDA extensions. For your 4060 you can install the current stable or nightly PyTorch binaries with CUDA 11.8.</p>

Kash_Rangan: <p>I installed the cuda toolkit 11.8 and then installed the PyTorch 2.0 for cuda 11.8 ( stable version). Still  getting the same error.</p>
<p>When I run nvidia-smi - I still get the cuda version as 12.0. How is it related to the cuda-toolkit 11.8 that I installed.</p>

ptrblck: <p>Could you post the used <code>pip/conda install</code> command as well as the output, since it should show which PyTorch binary is installed and should show the CPU-only binaries.</p>
<aside class="quote no-group" data-username="Kash_Rangan" data-post="19" data-topic="112467">
<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/kash_rangan/48/59056_2.png" class="avatar"> Kash_Rangan:</div>
<blockquote>
<p>When I run nvidia-smi - I still get the cuda version as 12.0.</p>
</blockquote>
</aside>
<p><code>nvidia-smi</code> returns the driver version and the CUDA version corresponding to this driver.</p>
<aside class="quote no-group" data-username="Kash_Rangan" data-post="19" data-topic="112467">
<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/kash_rangan/48/59056_2.png" class="avatar"> Kash_Rangan:</div>
<blockquote>
<p>How is it related to the cuda-toolkit 11.8 that I installed.</p>
</blockquote>
</aside>
<p>Assuming you have installed the <code>cuda-toolkit</code> conda binary, the output of <code>nvidia-smi</code> won’t relate to it. Also, why do you install it instead of using the provided commands?</p>