Skip to content
Advertisement

CPU only pytorch is crashing with error AssertionError: Torch not compiled with CUDA enabled

I’m trying to run the code from this repository and I need to use Pytorch 1.4.0. I’ve installed the CPU only version of pytorch with pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html.

I ran the program by doing py -m train_Kfold_CV --device 0 --fold_id 10 --np_data_dir "C:UsersusernameOneDriveDesktopemadeldeenAttnSleepprepare_datasetsedf_20_npz" but I’m getting this error:

  File "C:UsersusernameAppDataLocalProgramsPythonPython37librunpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:UsersusernameAppDataLocalProgramsPythonPython37librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:UsersusernameOneDriveDesktopemadeldeenAttnSleeptrain_Kfold_CV.py", line 94, in <module>
    main(config, fold_id)
  File "C:UsersusernameOneDriveDesktopemadeldeenAttnSleeptrain_Kfold_CV.py", line 65, in main
    trainer.train()
  File "C:UsersusernameOneDriveDesktopemadeldeenAttnSleepbasebase_trainer.py", line 66, in train
    result, epoch_outs, epoch_trgs = self._train_epoch(epoch, self.epochs)
  File "C:UsersusernameOneDriveDesktopemadeldeenAttnSleeptrainertrainer.py", line 49, in _train_epoch
    loss = self.criterion(output, target, self.class_weights)
  File "C:UsersusernameOneDriveDesktopemadeldeenAttnSleepmodelloss.py", line 6, in weighted_CrossEntropyLoss
    cr = nn.CrossEntropyLoss(weight=torch.tensor(classes_weights).cuda())
  File "C:UsersusernameAppDataLocalProgramsPythonPython37libsite-packagestorchcuda__init__.py", line 196, in _lazy_init
    _check_driver()
  File "C:UsersusernameAppDataLocalProgramsPythonPython37libsite-packagestorchcuda__init__.py", line 94, in _check_driver
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

I’ve changed the number of GPU in the config to 0 and tried adding device = torch.device('cpu') at the begining of the program, but it’s not doing anything. How can I fix this error? I’m using windows 10 with python 3.7.9 if it helps

Thanks

Advertisement

Answer

You are using CPU only pytorch, but your code has statement like cr = nn.CrossEntropyLoss(weight=torch.tensor(classes_weights).cuda()) which is trying to move the tensor to GPU.

To fix it, remove all the .cuda() operations.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement