【发布时间】:2017-04-05 22:18:42
【问题描述】:
我是第一次尝试 GPU 计算,当然希望能有很大的提速。然而,在 tensorflow 中的一个基本示例,它实际上更糟:
在 cpu:0 上,十次运行中的每一次平均需要 2 秒,gpu:0 需要 2.7 秒,而 gpu:1 比 cpu:0 3 秒差 50%。
代码如下:
import tensorflow as tf
import numpy as np
import time
import random
for _ in range(10):
with tf.Session() as sess:
start = time.time()
with tf.device('/gpu:0'): # swap for 'cpu:0' or whatever
a = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='a')
b = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='b')
c = tf.matmul(a, b)
d = tf.matmul(a, c)
e = tf.matmul(a, d)
f = tf.matmul(a, e)
for _ in range(1000):
sess.run(f)
end = time.time()
print(end - start)
我在这里观察到什么?运行时间是否主要由在 RAM 和 GPU 之间复制数据来控制?
【问题讨论】:
-
尝试增加矩阵并查看
nvidia-smi中的gpu 使用情况与top中的cpu 使用情况。 -
@sygi 谢谢,我不知道
nvidia-smi。它显示 GPU-Util 不超过 2%。不过,python 似乎占用了大部分内存。 40W / 180W的用电量相当稳定 -
看来你写的代码不是gpu绑定的。您可以尝试将
a和b更改为tf.random_uniform([1000, 1000])吗?就内存而言,TF 默认占用所有 GPU 内存(恶心!),但有一个选项可以通过强制动态分配。 -
@sygi 使用 random_uniform 明显更快,非常有趣!
标签: python performance tensorflow gpu