Tensorflow 2.4.1 + CUDA 11.1.0 + cuDNN 8.1.1 环境配置踩坑一则

第一个坑

Not creating XLA devices, tf_xla_enable_xla_devices not set

相关帖子

Github #44683

原因

Tensorflow 2.4版本新特性所致

解决办法

实际上这个问题可以忽略,看看2.4版本的release就一目了然,并不是很多博客说的版本对应问题,回退到老版本治标不治本。
如果需要用XLA,添加TF_XLA_FLAGS=–tf_xla_enable_xla_devices即可解决该warning。

默认情况下,不再注册XLA:CPU和XLA:GPU设备。
TF_XLA_FLAGS=--tf_xla_enable_xla_devices
如果确实需要它们,请使用此flag,但是此flag最终将在后续版本中删除。


第二个坑

Could not load dynamic library ‘cusolver64_10.dll’; dlerror: cusolver64_10.dll not found

相关帖子

Github #44159
Stack Overflow

原因

去到CUDA的bin文件夹可以发现并没有cusolver64_10.dll这个文件,但是有cusolver64_11.dll,cusolver64_10.dll按理说应该是对应CUDA 10版本,而11版本对应的是cusolver64_11.dll,估计是一个底层小bug。

解决办法

  • 方法一:重命名 ‘cusolver64_11.dll’ 为 ‘cusolver64_10.dll’
  • 方法二:下载 cusolver64_10.dll 原文件

测试代码

测试GPU是否被检测到

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

测试GPU运算是否可用

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
config = tf.compat.v1.ConfigProto(allow_soft_placement=True)

sess = tf.compat.v1.Session(config=config)
with tf.device('/gpu:0'):
    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)
    print(sess.run(c))

以上输出正常则配置成功!