【问题标题】:Tensorflow crash with CUDNN_STATUS_ALLOC_FAILEDTensorFlow 崩溃与 CUDNN_STATUS_ALLOC_FAILED
【发布时间】:2018-07-14 13:48:01
【问题描述】:

在网上搜索了几个小时没有结果,所以想在这里问。

我正在尝试按照 Sentdex 的教程制作一辆自动驾驶汽车,但是在运行模型时,我遇到了一堆致命错误。我已经在整个互联网上搜索了解决方案,而且很多人似乎都有同样的问题。但是,我找到的解决方案(包括this Stack-post)都不适合我。

这是我的软件:

  • Tensorflow:1.5,GPU 版本
  • CUDA:9.0,带有补丁
  • CUDnn: 7
  • Windows 10 专业版
  • Python 3.6

硬件:

  • Nvidia 1070ti,带有最新驱动程序
  • 英特尔 i5 7600K

这是崩溃日志:

2018-02-04 16:29:33.606903: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_blas.cc:444] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED 2018-02-04 16:29:33.608872: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_blas.cc:444] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED 2018-02-04 16:29:33.609308: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_blas.cc:444] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED 2018-02-04 16:29:35.145249: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:385] could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED 2018-02-04 16:29:35.145563: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:352] could not destroy cudnn handle: CUDNN_STATUS_BAD_PARAM 2018-02-04 16:29:35.149896: F C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\kernels\conv_ops.cc:717] Check failed: stream->parent()->GetConvolveAlgorithms( conv_parameters.ShouldIncludeWinogradNonfusedAlgo<T>(), &algorithms)

这是我的代码:

 import tensorflow as tf
    import numpy as np
    import cv2
    import time
    from PIL import ImageGrab
    from getkeys import key_check
    from alexnet import alexnet
    import os
    from sendKeys import PressKey, ReleaseKey, W,A,S,D,Sp

    import random

    WIDTH = 80
    HEIGHT = 60
    LR = 1e-3
    EPOCHS = 10
    MODEL_NAME = 'DiRT-AI-Driver-{}-{}-{}-epochs.model'.format(LR, 'alexnetv2', EPOCHS)

    def straight():
        PressKey(W)
        ReleaseKey(A)
        ReleaseKey(S)
        ReleaseKey(D)
        ReleaseKey(Sp)
    def left():
        PressKey(A)
        ReleaseKey(W)
        ReleaseKey(S)
        ReleaseKey(D)
        ReleaseKey(Sp)
    def right():
        PressKey(D)
        ReleaseKey(A)
        ReleaseKey(S)
        ReleaseKey(W)
        ReleaseKey(Sp)
    def brake():
        PressKey(S)
        ReleaseKey(A)
        ReleaseKey(W)
        ReleaseKey(D)
        ReleaseKey(Sp)
    def handbrake():
        PressKey(Sp)
        ReleaseKey(A)
        ReleaseKey(S)
        ReleaseKey(D)
        ReleaseKey(W)

    model = alexnet(WIDTH, HEIGHT, LR)
    model.load(MODEL_NAME)


    def main():
        last_time = time.time()
        for i in list(range(4))[::-1]:
            print(i+1)
            time.sleep(1)


    paused = False
    while(True):
            if not paused:
                screen = np.array(ImageGrab.grab(bbox=(0,40,1024,768)))
                screen = cv2.cvtColor(screen,cv2.COLOR_BGR2GRAY)
                screen = cv2.resize(screen,(80,60))
                print('Loop took {} seconds'.format(time.time()-last_time))
                last_time = time.time()
                print('took time')
                prediction = model.predict([screen.reshape(WIDTH,HEIGHT,1)])[0]
                print('predicted')
                moves = list(np.around(prediction))
                print('got moves')
                print(moves,prediction)

                if moves == [1,0,0,0,0]:
                    straight()
                elif moves == [0,1,0,0,0]:
                    left()
                elif moves == [0,0,1,0,0]:
                    brake()
                elif moves == [0,0,0,1,0]:
                    right()
                elif moves == [0,0,0,0,1]:
                    handbrake()

            keys = key_check()

            if 'T' in keys:
                if paused:
                    pased = False
                    time.sleep(1)
                else:
                    paused = True
                    ReleaseKey(W)
                    ReleaseKey(A)
                    ReleaseKey(S)
                    ReleaseKey(D)
                    ReleaseKey(Sp)
                    time.sleep(1)


main()

我发现导致 python 崩溃并产生前三个错误的行是这一行:

  • prediction = model.predict([screen.reshape(WIDTH,HEIGHT,1)])[0]

运行代码时,CPU 高达 100%,这表明某些事情严重不正常。 GPU 达到约 40-50%

我尝试过 Tensorflow 1.2 和 1.3 以及 CUDA 8,但效果不佳。安装 CUDA 时,我不安装特定的驱动程序,因为它们对于我的 GPU 来说太旧了。也尝试了不同的 CUDnn,效果不佳。

【问题讨论】:

  • "运行代码时,CPU 高达 100%,这表明某些东西严重异常" – 为什么会这样?即使您使用 GPU,高 CPU 负载也没有问题。
  • 只有在我看到从空闲到 100% CPU 的峰值一直处于无限循环中,但如果你说在这种情况下这是正常的,那应该没问题,不应该成为一部分的问题。

标签: python python-3.x tensorflow neural-network


【解决方案1】:

就我而言,问题的发生是因为另一个导入了tensorflow 的python 控制台正在运行。关闭它解决了这个问题。

我有 Windows 10,主要错误是:

创建cublas句柄失败:CUBLAS_STATUS_ALLOC_FAILED

无法创建 cudnn 句柄:CUDNN_STATUS_ALLOC_FAILED

【讨论】:

    【解决方案2】:

    可能你的 GPU 内存用完了。


    如果您使用的是 TensorFlow 1.x:

    第一个选项)将 allow_growth 设置为 true。

    import tensorflow as tf    
    config = tf.ConfigProto()
    config.gpu_options.allow_growth=True
    sess = tf.Session(config=config)
    

    第二个选项)设置内存分数。

    # change the memory fraction as you want
    
    import tensorflow as tf
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    

    如果您使用的是 TensorFlow 2.x:

    第一个选项)将 set_memory_growth 设置为 true。

    # Currently the ‘memory growth’ option should be the same for all GPUs.
    # You should set the ‘memory growth’ option before initializing GPUs.
    
    import tensorflow as tf
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
      try:
        for gpu in gpus:
          tf.config.experimental.set_memory_growth(gpu, True)
      except RuntimeError as e:
        print(e)
    

    第二个选项)根据需要设置memory_limit。 只需在下面这段代码中更改 gpus 和 memory_limit 的索引即可。

    import tensorflow as tf
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
      try:
        tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
      except RuntimeError as e:
        print(e)
    

    【讨论】:

    • 由于这里有多个选项,我只想指定。 TF 2.4,set_memory_growth 选项对我有用。
    【解决方案3】:

    尝试设置:

    os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' 解决了我的问题

    我的环境:

    Cudnn 7.6.5

    张量流 2.4

    Cuda 工具包 10.1

    RTX 2060

    【讨论】:

      【解决方案4】:

      尝试将 cuda 路径添加到环境变量。似乎是cuda的问题。

      在 ~/.bashrc 中设置 CUDA 路径(用 nano 编辑):

      #Cuda Nvidia path
      $ export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
      $ export CUDA_HOME=/usr/local/cuda
      

      【讨论】:

      • 我删除了所有与 CUDA 相关的内容,进入 %PATH% 并清除了所有 CUDA 变量。重新安装一切后,它现在终于可以工作了!问题是我之前尝试的路径数量。他们可能互相冲突。
      • 好吧,看来我的速度太快了!现在它可能有 20% 的时间有效。其他运行,我得到同样的崩溃。更好,但仍无法按预期工作!
      • 对不起,但我忘了说在编辑 .bashrc 之后你可以做$ source ~/.bashrc 。确保您只有环境变量的 onde 声明。
      • 我在Win10上也遇到了同样的问题,请问如何在Win10上添加新的环境变量呢?
      • Win10上CUDA根路径下没有“lib64”文件夹。
      【解决方案5】:

      我遇到了同样的问题,然后我发现因为我也在使用 GPU 来运行其他东西,即使它没有显示在使用 GPU 的任务管理器(Windows)上。甚至可能是(渲染视频、视频编码或玩繁重的游戏、硬币挖掘……)。 如果您认为它仍在使用重型 GPU,只需将其关闭并解决问题。

      【讨论】:

        【解决方案6】:

        我遇到了几乎相同的问题。通过重新安装 tensorflow-gpu 修复它。

        conda uninstall tensorflow-gpu
        conda install tensorflow-gpu
        

        我认为 pip 应该也可以。

        【讨论】:

          猜你喜欢
          • 2017-04-28
          • 2022-08-21
          • 2019-02-01
          • 2022-01-10
          • 2017-09-07
          • 2016-09-27
          • 2022-01-18
          • 2018-01-02
          • 1970-01-01
          相关资源
          最近更新 更多