tensorflow:有没有办法用 tensorflow 指定 XLA_GPU?

数据挖掘 Python 张量流
2022-02-10 04:27:33

以下代码用于指定运行 tf 节点的设备

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')

我已经知道: 这篇文章tensorflow docxla demo

我想知道的是:

有没有办法将 XLA_GPU 指定为运行 tf 节点的设备?

with tf.device('/XLA_GPU:0'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')

执行上面的代码给出ValueError: Unknown attribute: 'device' in '/job:localhost/replica:0/task:0/device:/XLA_GPU:0'

这在 google colab 上是 100% 可重现的。

1个回答

首先通过检查以下输出中的设备名称来检查设备是否存在:

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

一旦您确认“XLA_GPU:0”是您系统上的设备,请使用类似

with tf.device('/device:XLA_GPU:0'):

显然,您可以更改名称。