Internal: failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_UNKNOWN: unknown error 解决过程

记录一个在服务器上使用tensorflow-gpu报错的debug过程


总结

碰到如下两个错误:

1. Internal: failed initializing StreamExecutor for CUDA device ordinal 1: Internal: failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_UNKNOWN: unknown error——(不设置os或者是os设置了多张显卡VISIBLE并且包含问题显卡时报该错误)

2. RuntimeError: CUDA runtime implicit initialization on GPU:0 failed. Status: all CUDA-capable devices are busy or unavailable——(os只设置了问题显卡VISIBLE时报该错误)

并且确定GPU没有被占满,理应是可以用的,于是利用 os.environ['CUDA_VISIBLE_DEVICES'] 逐张显卡测试,最终发现当'1'号GPU是VISIBLE的时候就会报错。

初步判定问题为GPU出了问题

解决方法:利用 os.environ['CUDA_VISIBLE_DEVICES'] 避开出问题的显卡。当不设置该参数的时候程序默认是可以看到所有显卡,自然会看到出问题的卡而导致程序报错。


具体整个tensorflow配置过程以及debug过程

conda的镜像源设置如下(北外的镜像源,亲测十分好用):

channels:
  - defaults
custom_channels:
  conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
  msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
  bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
  menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
  pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
default_channels:
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
show_channel_urls: True

在服务器上用以下conda命令安装了tensorflow-gpu

conda install tensorflow-gpu

安装的版本是 tensorflow 2.4.1 + cudatoolkit 10.1

运行以下代码测试GPU是否可用:

import tensorflow as tf

print(tf.test.is_gpu_available())

报错:

扫描二维码关注公众号,回复: 16939024 查看本文章

Internal: failed initializing StreamExecutor for CUDA device ordinal 1: Internal: failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_UNKNOWN: unknown error

后来使用os.environ指定GPU再尝试测试:

import tensorflow as tf
import os

os.environ['CUDA_VISIBLE_DEVICES']='0,1,2'  # 三张GPU
print(tf.test.is_gpu_available())

仍然报同样的错误,于是打算逐张卡测试

import tensorflow as tf
import os

os.environ['CUDA_VISIBLE_DEVICES']='0' # '1','2'
print(tf.test.is_gpu_available())

'0'和'2'都没问题,输出True

而测试'1'卡的时候报了如下错误:

RuntimeError: CUDA runtime implicit initialization on GPU:0 failed. Status: all CUDA-capable devices are busy or unavailable

因此初步判定是1号GPU出问题了

猜你喜欢

转载自blog.csdn.net/jasonso97/article/details/117267324