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())

报错:

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出问题了

 

您可能感兴趣的与本文相关的镜像

PyTorch 2.8

PyTorch 2.8

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值