torch.cuda.is_available()报错

ubuntu系统进入休眠后cuda初始化报错

Posted by ThreeStones1029 on November 29, 2023

[TOC]

一、前言

ubuntu20.04,跑代码,系统自动休眠后,程序被异常终止,再次运行后报错

/home/***/anaconda3/envs/nnunet/lib/python3.9/site-packages/torch/cuda/__init__.py:107: UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero. (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:109.)

二、解决方法

想来想去,明明下午还能正常运行,晚上回来看发现系统自动进入休眠了,代码被终止了,尝试重新运行发现报错,但理论上应该没问题的,除了系统休眠了一次。经过搜索在torch官网找到解决方法。

sudo rmmod nvidia_uvm
sudo modprobe nvidia_uvm

运行完成后就能正常使用cuda了。

三、设置系统不进入休眠

3.1.查看当前系统休眠状态

systemctl status sleep.target

image-20231129193623002

可以看到系统是设置了会自动进入休眠的,状态为loaded

3.2.设置不休眠

重新设置让它不进入休眠

sudo systemctl mask sleep.target supend.target hibernate.target hybrid-sleep.target

image-20231129193811899

3.3.再次查看休眠状态

systemctl status sleep.target

image-20231129194114450

变成masked即可

三、参考

1.userwarning-cuda-initialization-cuda-unknown-error-this-may-be-due-to-an-incorrectly-set-up-environment-e-g-changing-env-variable-cuda-visible-devices-after-program-start-setting-the-available-devices-to-be-zero/129335

2.ubuntu20.04禁止自动休眠的几种方式