How to Fix AssertionError: torch not compiled with cuda enabled

AssertionError: torch not compiled with cuda enabled error occurs when CUDA toolkit is not installed in our Python environment.

This issue commonly arises when you are trying to run PyTorch code that requires GPU support on a version of PyTorch that was installed without CUDA support.

To fix the AssertionError, “install the CUDA toolkit on the local machine and upgrade the version of our current PyTorch library.” It is crucial to install the version of Pytorch with CUDA support.

Step 1: Check your PyTorch installation

Check your PyTorch installation using the below code:

import torch

print(torch.version.cuda)
print(torch.cuda.is_available())

Output

None
False

You can see that the output shows that CUDA is not available. You may need to reinstall PyTorch with CUDA support.

Step 2: Uninstall your current PyTorch installation

pip uninstall torch

It will give you the below output.

Screenshot of uninstalling pytorch

Step 3: Install the correct version of CUDA for your system. 

Reinstall PyTorch with CUDA support:

 pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/cu113/torch_stable.html

Output

Screenshot of installing the correct version of CUDA for your system

Now, you can run the below code and make sure that GPU is available.

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()

The output of torch.cuda.is_available() method will be a boolean value indicating whether CUDA is available on the system (True if it is available, False otherwise).

Output

Screenshot of fixing the error

We verified the installation of CUDA by using the torch.cuda.is_available() function is not installed on my machine because it returns False.

If the issue persists, then you can try installing the following command.

pip install cudatoolkit

You can also install the CUDA Toolkit and its dependencies by using the conda:

conda install cudatoolkit

We can also verify the installation by running the command shown below.

nvcc --version

I hope these solutions will resolve the issues you are having.

Related posts

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

RuntimeError: cuda error: an illegal memory access was encountered

RuntimeError: cuda error: invalid device ordinal

RuntimeError: cudnn error: cudnn_status_not_initialized

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.