首页 > 解决方案 > 将 TensorFlow GPU 与 Keras 结合使用时,Blas GEMM 启动失败

问题描述

很不言自明。就像我之前和之后的无数人一样,我Blas GEMM launch failed在尝试调用时收到错误消息model.fit()

这是调用nvidia-smi 之前model.compile()的输出:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.50       Driver Version: 430.50       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   45C    P0    74W / 149W |      0MiB / 11441MiB |    100%      Default |   <<<--- 0% Memory usage
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |   <<<--- nothing running
+-----------------------------------------------------------------------------+

调用nvidia-smi 之后model.compile()(以及之前)的输出model.fit()

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.50       Driver Version: 430.50       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   45C    P0    72W / 149W |  10942MiB / 11441MiB |      0%      Default |   <<<--- 96% Memory usage
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1811      C   /usr/bin/python3                           10929MiB |   <<<--- TF model here
+-----------------------------------------------------------------------------+

看起来编译后的 TensorFlow 模型垄断了 96% 的 GPU 内存。我不知道这是否正常,以及在尝试训练模型时是否可能是后来错误的原因。

错误消息本身如下:

tensorflow/stream_executor/stream.cc:2041] 尝试在没有 BLAS 支持的情况下使用 StreamExecutor 执行 BLAS 操作

InternalError: Blas GEMM 启动失败:a.shape=(32, 116032), b.shape=(116032, 256), m=32, n=256, k=116032 [[node dense_1/MatMul (定义在 /home/ ubuntu/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_1645]

函数调用栈:keras_scratch_graph

输出tf.config.experimental.list_physical_devices()

[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
 PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU'),
 PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU'),
 PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

该模型是使用以下内容构建的:

我经历了无数的 GitHub 问题、博客文章、SO 问题,都建议确保在启动新进程时在 GPU 上没有任何先前运行的进程仍然处于活动状态,或者将 CUPTI 位置添加到 LD_LIBRARY_PATH,或者使用各种 TF选项...都没有解决问题。任何有关导致该问题的原因以及如何解决的想法都将不胜感激。

标签: pythontensorflowkeras

解决方案


我有同样的问题。我看到了很多答案并使用了许多建议的代码来解决这个问题,但任何东西都对我有帮助。

对我来说,问题是 GPU 的使用,所以我使用以下代码限制我的 GPU 使用的内存:

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
    try:
        tf.config.experimental.set_virtual_device_configuration(
            gpus[0],
            [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
        logical_gpus = tf.config.experimental.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Virtual devices must be set before GPUs have been initialized
        print(e)

取自https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth。这解决了我的问题。我希望这也能解决你的问题。


推荐阅读