当前位置: 代码迷 >> 综合 >> 【keras】CUDNN_STATUS_ALLOC_FAILED 和 failed to run cuBLAS routine cublas ,keras和tensflow-gpu版本的问题
  详细解决方案

【keras】CUDNN_STATUS_ALLOC_FAILED 和 failed to run cuBLAS routine cublas ,keras和tensflow-gpu版本的问题

热度:99   发布时间:2023-11-25 04:29:57.0

【报错】
首先报错:
could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
尝试解决

https://blog.csdn.net/weixin_44754046/article/details/97663626

在最开头添加:

import tensorflow as tf

config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
sess = tf.Session(config=config)
可以适当的使用GPU,But!

后又报错

failed to run cuBLAS routine cublasSgemm_v2: CUBLAS_STATUS_EXECUTION_FAILED
尝试解决:

https://tbr8.org/how-to-solve-failed-to-run-cublas-routine-cublassgemm-v2-cublas-status-execution-failed-problem/
一般遇到这个问题都是cuda=9.0的,说要打补丁,心里oso,但是我是cudu10 没有补丁可以打,因为官方只有一个文件可下载,没有补丁。

 

排查过程:

1.注意:我在使用以下做验证的时候,会发现自己在使用GPU2070,也就是说环境应该没有问题。

import tensorflow as tfwith tf.device('/cpu:0'):a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:0'):c = a + b# 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
# 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))# 打印所有的设备
from tensorflow.python.client import device_lib
#print(device_lib.list_local_devices())2.再看下自己使用的是GPU还是CPU版本,都是使用GPU2070# 查看所使用的tensorflow是GPU还是CPU版本
import os
from tensorflow.python.client import device_libos.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"if __name__ == "__main__":print(device_lib.list_local_devices())
3.最后认真看报错,发现在node,是在用xception这个网络的时候可分离卷积这里也提示报错。
x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False)(x)
所以我猜测会不会是因为这个网络。
所以我试了几层cnn网络,ok,可以训练,不报错。
那么问题来了,会不会是因为版本太低造成的,因为朋友说cuda 10 至少是tf=1.13和对应的keras,而我是tf=1.8 keras=2.1.5,可以尝试下提高下版本。我用conda安装,发现会自动一键在虚拟环境中安装cuda +cudunn。之前我也是用conda insatall tensorflow-gpu==1.8,pip insatll keras=2.1.5.在公司1060上是没有问题的,且因为一键安装cuda+cudnn,所以如下:在公司GPU=1060的情况下是可以的,但是在自己电脑上GPU=2070就不行,同一份代码,所以问题来了哈,就是GPU的问题。
虽然在虚拟环境下可以一键安装cuda+cudnn,但是还是需要和GPU配套的,网上很多建议2070还是用cuda=1.0和对应配套的cudnn就好,要不然使用过程中会遇到些问题。
所以ok啦,1060可以用cuda10,但是2070不建议用cuda9,不向下兼容。使用conda insatll tensorflow-gpu==1.14 ,conda insatll keras=2.2.5,
关于版本对应tf和keras,请看:https://docs.floydhub.com/guides/environments/conda装tf-gpu的时候会安自动装numpy=1.18,在运行代码时其会报警,大概就是numpy太高了,用numpy =1.13 就好啦,但是我卸载conda uninstall numpy=1.18时会把tensoflow-gpu=1.14和keras=2.2.5都卸载。
所以知道其中一个conda安装的方式了,安装一个需要的库,会把相关的很多其他包都一起安装了。
pip就不会啦,至少不会一锅端,且用conda安装的话才装了几个东西:
就好多东西被安装了。多吧,实际上我只安装了tf-gpu,keras,matplotlib ,opencv-python.以下都是自己配套按照的。
# Name                    Version                   Build  Channel
_tflow_select             2.1.0                       gpu    defaults
absl-py                   0.9.0                    py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
astor                     0.7.1                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
blas                      1.0                         mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
ca-certificates           2019.11.28           hecc5488_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
certifi                   2019.11.28               py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
cudatoolkit               10.0.130                      0    defaults
cudnn                     7.6.5                cuda10.0_0    defaults
cycler                    0.10.0                    <pip>
gast                      0.3.3                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
grpcio                    1.23.0           py36h3db2c7e_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
h5py                      2.9.0           py36hf098a70_1000    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
hdf5                      1.10.4          nompi_hcc15c50_1106    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
icc_rt                    2019.0.0             h0cc432a_1    defaults
imutils                   0.5.3                     <pip>
intel-openmp              2019.4                      245    defaults
joblib                    0.14.1                     py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
keras                     2.2.5                    py36_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
keras-applications        1.0.8                      py_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
keras-preprocessing       1.1.0                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
kiwisolver                1.1.0                     <pip>
libblas                   3.8.0                    14_mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libcblas                  3.8.0                    14_mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libgpuarray               0.7.6             hfa6e2cd_1003    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
liblapack                 3.8.0                    14_mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libprotobuf               3.11.4               h1a1b453_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
m2w64-gcc-libgfortran     5.3.0                         6    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
m2w64-gcc-libs            5.3.0                         7    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
m2w64-gcc-libs-core       5.3.0                         7    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
m2w64-gmp                 6.1.0                         2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
mako                      1.1.0                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
markdown                  3.2.1                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
markupsafe                1.1.1            py36hfa6e2cd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
matplotlib                3.1.3                     <pip>
mkl                       2019.4                      245    defaults
mkl-service               2.3.0            py36hfa6e2cd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
msys2-conda-epoch         20160418                      1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
numpy                     1.18.1           py36hc71023c_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
opencv-python             4.2.0.32                  <pip>
openssl                   1.1.1d               hfa6e2cd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pandas                    1.0.1            py36he350917_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip                       20.0.2                     py_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
protobuf                  3.11.4           py36he025d50_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pygpu                     0.7.6           py36hc8d92b1_1000    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pyparsing                 2.4.6                     <pip>
pyreadline                2.1                   py36_1001    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python                    3.6.5                         1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-dateutil           2.8.1                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pytz                      2019.3                     py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pyyaml                    5.3              py36hfa6e2cd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
scikit-learn              0.22.1           py36h7208079_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
scipy                     1.3.1            py36h29ff71c_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools                45.2.0                   py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
six                       1.14.0                   py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tensorboard               1.14.0                   py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tensorflow                1.14.0          gpu_py36h305fd99_0    defaults
tensorflow-base           1.14.0          gpu_py36h55fc52a_0    defaults
tensorflow-estimator      1.14.0           py36h5ca1d4c_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tensorflow-gpu            1.14.0               h0d30ee6_0    defaults
termcolor                 1.1.0                      py_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
theano                    1.0.4           py36h6538335_1001    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.16.27012          hf0eaf9b_1    defaults
vs2015_win-64             14.0.25420          h55c1224_11    defaults
werkzeug                  1.0.0                      py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wheel                     0.34.2                     py_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wincertstore              0.2                   py36_1003    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wrapt                     1.12.0           py36hfa6e2cd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
yaml                      0.2.2                hfa6e2cd_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
zlib                      1.2.11            h2fa13f4_1006    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
起初本来想按照tf-gpu=1.13的,但用conda install tensorflow-gpu==1.13 说下载的通道没有这个源,所以我用安装了1.14
测试代码如下:import numpy
import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
print(sess.run(c))

成了,且解决了:failed to run cuBLAS routine cublasSgemm_v2: CUBLAS_STATUS_EXECUTION_FAILED问题。把

import tensorflow as tf

config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
sess = tf.Session(config=config)

去掉以后也不会报allow-failed 的错了

 

这是keras版本的表情识别项目遇到的问题。

  相关解决方案