简体   繁体   English

为什么 tf.matmul 在 cpu 上运行

[英]Why tf.matmul running on cpu

I encountered some problems while training my model, specifically, the training speed is sometimes fast and sometimes slow(fluctuating between 2-10 iters per second).我在训练我的model时遇到了一些问题,具体来说,训练速度有时快有时慢(在每秒2-10次迭代之间波动)。 After I used tensorboard to check the.network operation, I found that the tf.matmul() node was running on CPU device.我用tensorboard查看.network运行后,发现tf.matmul()节点在CPU设备上运行。 As follow:如下:

在此处输入图像描述

This makes me quite confused, I try to use tf.device('/gpu:0') to enforce that node running on GPU, I also tried to lower the tensorflow version, but none of these work.这让我很困惑,我尝试使用tf.device('/gpu:0')强制该节点在 GPU 上运行,我还尝试降低 tensorflow 版本,但这些都不起作用。 below is my code:下面是我的代码:

def _repeat(x, n_repeats):
        with tf.variable_scope('_repeat'):
            rep = tf.transpose(
                tf.expand_dims(tf.ones(shape=tf_v.stack([n_repeats, ])), 1), [1, 0])
            rep = tf.cast(rep, tf.int32)
#             with tf.device('/gpu:0'):
            x = tf.matmul(tf.reshape(x, (-1, 1)), rep)
            return tf.reshape(x, [-1])

by the way, the tensorflow version is 2.2.5, GPU is rtx3090.对了,tensorflow版本是2.2.5,GPU是rtx3090。

The possible reason could be that the GPU has not been configured in your system properly.可能的原因可能是您的系统中没有正确配置 GPU。 This is why it's detecting the CPU only.这就是它只检测 CPU 的原因。 Make sure you have Tensorflow GPU installed in your system along with other GPU support requirements.确保您的系统中安装了Tensorflow GPU以及其他GPU 支持要求。

If a TensorFlow operation has both CPU and GPU implementations, by default, the GPU device is prioritized when the operation is assigned.如果 TensorFlow 操作同时具有 CPU 和 GPU 实现,默认情况下,分配操作时优先考虑 GPU 设备。

I tried replicating tf.matmul() in Google colab by selecting "CPU mode" and it could not detect the GPU:我尝试通过选择“CPU 模式”在Google colab中复制tf.matmul() ,但它无法检测到 GPU:

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

# Place tensors on the GPU
with tf.device('/GPU:0'):
  a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
  c = tf.matmul(a, b)
  print(c)

Output: Output:

Num GPUs Available:  0
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

But if we try the same by selecting "GPU mode", the tensor operation detects the GPU.但是,如果我们通过选择“GPU 模式”来尝试同样的操作,张量运算会检测到 GPU。

Output: Output:

Num GPUs Available:  1
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

Please check this link to get more details in this.请检查链接以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM