Search code examples
macosmachine-learningpytorchgpu

AssertionError: Torch not compiled with CUDA MacOS


I cloned a git repo and I'm trying to run the code. However, I realised that some operations were loaded to the GPU. The issue is that MacOS doesnt support NVIDA so I keep getting assertion error when I run the code(as seen below). Any suggestion will be appreciated.

Note that the code is quite bulky so I can't go through every file and offload these operations. Below is a sample code.

for batch_idx, data in enumerate(valid_generator):
    x, y, x1, y1 = data[0].squeeze(-1).to(device), data[1].squeeze(-1).to(device), data[2].squeeze(-1).to(device), data[3].squeeze(-1).to(device)



    for param in transform.parameters():
        param.requires_grad = False
        param.volatile = True

    
    names = sorted(glob.glob(data))

  

And I get the error below

raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

Solution

  • It might be enough to add this at the beginning of your code(the line telling PyTorch to use the CPU instead):

    device = torch.device("cpu")
    

    Then, you need to make a global replacement (you will probably have a chance to perform it automatically in all project's code with the help of your IDE) of .cuda() to .to(device)

    In case the error continues, try setting the value of the variable CUDA_VISIBLE_DEVICES in the shell manually:

    export CUDA_VISIBLE_DEVICES=""
    

    In case it is impossible to perform mass method substitution in tangled code, or if you intend to work with models in the future:

    Consider using a remote machine with a GPU. This is usually what Mac users do, in my experience. You can use Google Colab or other similar services if your model is not very large.

    Wish you luck :)