解决:Not creating XLA devices, tf_xla_enable_xla_devices not set

实验环境

Windows 10 NVIDIA GeForce GTX 1050 CUDA11.0 CUDNN 8.0 Tesorflow-gpu 2.4.1

提示如下

分析原因

据说是Tesorflow-gpu 2.4.1,默认情况下,不再注册XLA:CPU和XLA:GPU设备。如果确实需要它们,请可使用

os.environ[TF_XLA_FLAGS] = --tf_xla_enable_xla_devices

但是此标志最终将在后续发行版中删除。

解决方法

import tensorflow as tf
import os
os.environ[TF_CPP_MIN_LOG_LEVEL]=2

print(tf.__version__)
a = tf.constant(1.)
b = tf.constant(2.)
print(a+b)
print(GPU:, tf.test.is_gpu_available())

改为

import tensorflow as tf
import os
os.environ[TF_XLA_FLAGS] = --tf_xla_enable_xla_devices
os.environ[TF_CPP_MIN_LOG_LEVEL]=2
print(tf.__version__)
a = tf.constant(1.)
b = tf.constant(2.)
print(a+b)
print(GPU:, tf.test.is_gpu_available())

即可! 你将会发现,在代码开头加了

os.environ[TF_XLA_FLAGS] = --tf_xla_enable_xla_devices

将不会有

Not creating XLA devices, tf_xla_enable_xla_devices not set

提示!

经验分享 程序员 微信小程序 职场和发展