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:
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.
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
Now, you can run the below code and make sure that GPU is available.
my_tensor = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32, device="cpu")
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).
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.
I hope these solutions will resolve the issues you are having.