UserWarning: CUDA initialization: CUDA unknown error

created at 12-15-2021 views: 4

error

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

solution

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

export CUDA_VISIBLE_DEVICES=0,1,2,3

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.

Please log in to leave a comment.