python - 将 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')]
该模型是使用以下内容构建的:
- Keras 2.3.1(使用
keras.models.Sequential
) - TensorFlow-GPU 2.1.0
- CUDA 10.1
- cuDNN 7.6.4
- Ubuntu 18.04
- AWS p2.xlarge 实例(配备 Tesla K80 GPU)
我经历了无数的 GitHub 问题、博客文章、SO 问题,都建议确保在启动新进程时在 GPU 上没有任何先前运行的进程仍然处于活动状态,或者将 CUPTI 位置添加到 LD_LIBRARY_PATH,或者使用各种 TF选项...都没有解决问题。任何有关导致该问题的原因以及如何解决的想法都将不胜感激。
解决方案
我有同样的问题。我看到了很多答案并使用了许多建议的代码来解决这个问题,但任何东西都对我有帮助。
对我来说,问题是 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。这解决了我的问题。我希望这也能解决你的问题。
推荐阅读
- kong - 为什么Kong在请求时需要标头主机?
- android - Moshi 在浮点值上崩溃
- flutter - Flutter:必须在调用 super.dispose() 之前处理 Ticker
- python - 加载多个 JSON 文件
- amazon-s3 - Sqoop 无法从 Postgres 导入到 S3
- javascript - 如何访问事件中的对象
- ionic-framework - ionic 4 twitter登录出现错误:登录会话失败
- javascript - 如何在鼠标悬停时暂停间隔并在鼠标不再悬停时恢复
- apache - 如何通过 wss 在 Apache 上运行 websockets?
- spring-boot - Apache Camel SNMP 路由的简单单元测试