AssertionError: torch not compiled with cuda enabled ( Fix )

AssertionError torch not compiled with cuda enabled

AssertionError: torch not compiled with Cuda enabled error occurs because of using cuda GPU enable code over normal PyTorch (CPU only ). There are multiple scenarios where you can get this error. Sometimes CUDA enablement is clear and visible. This is easy to fix by making it false or removing the same. But in some scenarios, It is indirectly calling Cuda which is explicitly not visible. Hence There we need to understand the internal working of such parameter or function which is causing the issue. Anyways in this article, we will go throw the most common reasons.

assertionerror: torch not compiled with cuda enabled ( Solution ) –

First thing we can do is to verify which Pytorch version we are using. CPU based or GPU Enable. We can check using below code-

import torch
print("version:",torch.__version__)
print("Is CUDA Supportive ?",torch.cuda.is_available())

This is will help in deciding the approach to fix the issue.

 

Solution 1: Switching from CUDA to CPU version  –

Usually while compiling any neural network in PyTorch, we can pass cuda enable. If we simply remove the same it will remove the error. Refer to the below example, If you are using a similar syntax pattern then remove Cuda while compiling the neural network.

from torch import nn
net = nn.Sequential(
    nn.Linear(18*18, 80),
    nn.ReLU(),
    nn.Linear(80, 80),
    nn.ReLU(),
    nn.Linear(80, 10),
    nn.LogSoftmax()
).cuda()

The correct way is –

assertionerror torch not compiled with cuda enabled solution
assertionerror torch not compiled with cuda enabled solution

Solution 2: Installing cuda supported Pytorch –

See the bottom line is that if you are facing such an incompatibility issue either you adjust your code according to available libraries in the system. Or we install the compatible libraries in our system to get rid of the same error.

You may any package managers to install cuda supported pytorch. Use any of the below commands –

conda –

conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch

pip –

pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113

Solution 3:  set pin_memory=False –

This is one of the same categories where CUDA is not visible directly. But Internally if it is True then it copies the tensors into CUDA space for processing. To Avoid the same we have to make it False. Once more thing, By Default it is True. Hence we have to explicitly make it False while using the get_iterator function in DataLoader class.

udemy computer vision course

torch not compiled with cuda enabled ( Similar Error )-

There are so many errors that have similar solutions but because of the specification added it looks a bit different. Hence to avoid confusion, Here are some variations:

  1. Platform specifications: This error has the generic solution with most platforms like Win10, Mac, Linux, etc.
  2. Addition Modules: Sometimes we get this error in intermediate modules like detectron2 etc. But the solution will be generic in all the cases.
  3. Hardware Specifications: Not Only the Platform but the Underlying hardware like processors like AMD, Jetson, etc have the same impact and solution.

Benefits of CUDA with Torch –

CUDA is a parallel processing framework that provides an application interface to deal with the graphic card utility of the system. In complex operations like deep learning model training where we have to run operations like backpropagation, we need multiprocessing. GPU provides great support for multiprocessing for that we need CUDA (NVIDIA). PyTorch or Tensorflow or any other deep learning framework required GPU handling for high performance. However, it works fine with the CPU in case of small datasets, fewer epochs, etc. But Typically the dataset for any state of art algorithm is usually large in volume. Hence we need CUDA with PyTorch ( Python binding of Torch).

In the end, Let’s understand the assertion error basic fundamental. Assertionerror is the basic check we perform as of prerequisite before the final code run to avoid run time failure. Suppose we are creating some mid output that is consumable by some other service and the system fails there Assertion can avoid wrong data or control flow before the run. Here is an example of another assertionerror.

AssertionError: no inf checks were recorded for this optimizer ( Fix )

What is Pytorch ?

Pytorch is a machine learning library framework used for building applications like Computer Vision and Natural Language Processing. It is developed by Meta Company and is open source. Using it you can easily build deep learning models and make application like image recognition and language processing. If  you compare it with Tensorflow then the main difference between them is that TensorFlow allows better visualization than Pytorch.

 

Thanks

Data Science Learner Team

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner