5

在下面的代码中,我绝对有必要在 GPU 中执行完整的功能,而无需单次跳转回 CPU。这是因为我有 4 个 CPU 内核,但我有 1200 个 cuda 内核。从理论上讲,这是可能的,因为 tensorflow feed_forwards、if 语句和变量分配可以在 GPU 上完成(我有 NVIDIA GTX 1060)。

我面临的问题是 tensorflow2.0 在后端自动分配给 GPU 和 CPU,并且没有提到它的哪些操作与 GPU 兼容。当我使用设备作为 GPU 运行以下功能时,我得到

parallel_func could not be transformed and will be staged without change.

它在 GPU 上按顺序运行。

我的问题是在哪里使用 tf.device?哪部分代码将通过签名转换为 GPU 代码,哪些将保留在 CPU 上?我怎样才能将它也转换为 GPU?

@tf.function
def parallel_func(self):
    for i in tf.range(114):                     #want this parallel on GPU
        for count in range(320):                #want this sequential on GPU

            retrivedValue = self.data[i][count]

            if self.var[i]==1:
                self.value[i] = retrievedValue     # assigns, if else
            elif self.var[i]==-1:                  # some links to class data through
                self.value[i] = -retrivedValue     # self.data, self.a and self.b

            state = tf.reshape(tf.Variable([self.a[i], self.b[i][count]]), [-1,2])

            if self.workerSwitch == False:
                action = tf.math.argmax(self.feed_forward(i, count, state))
            else:
                action = tf.math.argmax(self.worker_feed_forward(i, count, state))

            if (action==1 or action==-1):
                self.actionCount +=1
4

1 回答 1

0

旁注:该消息parallel_func could not be transformed and will be staged without change是由签名输出的,并且由于它包含依赖于数据的控制流,因此该函数可能根本无法运行。值得提出有关重现和更详细日志消息的步骤的问题。

于 2019-04-04T12:22:41.753 回答