UserWarning: CUDA initialization: CUDA unknown error

created at 12-15-2021 views: 21


CUDA was newly installed on the server, and an error occurred when using pytorch:

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  /opt/conda/conda-bld/pytorch_1623448255797/work/c10/cuda/CUDAFunctions.cpp:115.)
  return torch._C._cuda_getDeviceCount() > 0


First check whether the graphics card driver, CUDA, cudnn, and pytorch versions match. If they do not match, you need to uninstall and reinstall the corresponding version.

If the versions are all correct, you need to set the environment variables, enter sudo vim ~/.bashrc, and add at the end:

# The first three lines need to be set when installing CUDA
export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda-11.2/bin


Save and exit, try to see if you can use CUDA.

If it still doesn't work, enter apt-get install nvidia-modprobe, there should be no problem.

If the error still occurs, uninstall all of them and reinstall them.

created at:12-15-2021
edited at: 12-15-2021: