Google合作实验室上的Pytorch GPU性能比CPU慢

叮叮铃

GPU在大约16秒内训练了这个网络。CPU大约需要13秒。(我正在取消注释/注释适当的行以进行测试)。有人可以看到我的代码或pytorch安装有什么问题吗?(我已经检查了GPU是否可用,并且GPU上有足够的可用内存。

from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())

accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
print(accelerator)
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.0-{platform}-linux_x86_64.whl torchvision
print("done")

#########################

import torch
from datetime import datetime

startTime = datetime.now()

dtype = torch.float
device = torch.device("cpu") # Comment this to run on GPU
# device = torch.device("cuda:0") # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1024, 128, 8

# Create random Tensors to hold input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
t = torch.randn(N, D_out, device=device, dtype=dtype)

# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
w3 = torch.randn(D_out, D_out, device=device, dtype=dtype, requires_grad=True)

learning_rate = 1e-9
for i in range(10000):
    y_pred = x.mm(w1).clamp(min=0).mm(w2).clamp(min=0).mm(w3)

    loss = (y_pred - t).pow(2).sum()

    if i % 1000 == 0:
        print(i, loss.item())

    loss.backward()

    # Manually update weights using gradient descent
    with torch.no_grad():
        w1 -= learning_rate * w1.grad
        w2 -= learning_rate * w2.grad

        # Manually zero the gradients after updating weights
        w1.grad.zero_()
        w2.grad.zero_()

print(datetime.now() - startTime)
iacolippo

我看到您正在计时不应该计时的事物(定义dtype,device等)。在这里,时间有趣的是输入,输出和权重张量的创建。

startTime = datetime.now()
# Create random Tensors to hold input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
t = torch.randn(N, D_out, device=device, dtype=dtype)
torch.cuda.synchronize()
print(datetime.now()-startTime)

# Create random Tensors for weights.
startTime = datetime.now()
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
w3 = torch.randn(D_out, D_out, device=device, dtype=dtype, requires_grad=True)
torch.cuda.synchronize()
print(datetime.now()-startTime)

和训练循环

startTime = datetime.now()

for i in range(10000):
    y_pred = x.mm(w1).clamp(min=0).mm(w2).clamp(min=0).mm(w3)

    loss = (y_pred - t).pow(2).sum()

    if i % 1000 == 0:
        print(i, loss.item())

    loss.backward()

    # Manually update weights using gradient descent
    with torch.no_grad():
        w1 -= learning_rate * w1.grad
        w2 -= learning_rate * w2.grad

        # Manually zero the gradients after updating weights
        w1.grad.zero_()
        w2.grad.zero_()
torch.cuda.synchronize()
print(datetime.now() - startTime)

为什么GPU较慢

我在装有GTX1080和非常好的CPU的计算机上运行它,因此绝对计时较低,但是说明仍然有效。如果您打开Jupyter笔记本并在CPU上运行它:

0:00:00.001786 time to create input/output tensors
0:00:00.003359 time to create weight tensors
0:00:04.030797 time to run training loop

现在将设备设置为cuda,我们将其称为“冷启动”(此笔记本中的GPU以前没有运行过任何操作)

0:00:03.180510 time to create input/output tensors
0:00:00.000642 time to create weight tensors
0:00:03.534751 time to run training loop

您会看到运行训练循环的时间减少了少量,但开销为3秒,因为您需要将张量从CPU移至GPU RAM。

如果您在不关闭Jupyter笔记本的情况下再次运行它:

0:00:00.000421 time to create input/output tensors
0:00:00.000733 time to create weight tensors
0:00:03.501581 time to run training loop

开销消失了,因为Pytorch使用了缓存内存分配器来加快处理速度。

您会注意到,训练循环上的加速非常小,这是因为您正在执行的操作是在非常小的张量上进行的。在处理小型体系结构和数据时,我总是进行快速测试,以查看是否可以通过在GPU上运行来实际获得任何收益。例如,如果设置N, D_in, H, D_out = 64, 5000, 5000, 8,则训练循环在GTX1080上运行3.5秒,在CPU上运行85秒。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用GPU之前是否需要在Google合作实验室上安装GPU库?

Google合作实验室:有关其GPU的误导性信息(某些用户只能使用5%的RAM)

在Google合作实验室上安装faiss

如何编辑Google合作实验室?

对Google合作实验室的终端回答“是”

导入错误:“ Google合作实验室”上的“ scipy.misc import imsave”

在Google合作实验室中设置表格格式

如何在Google合作实验室中显示行号?

Google合作实验室中的openAI Gym NameError

在Google合作实验室中安装imagemagick

Google合作实验室的硬件规格是什么?

Google合作实验室有哪些可用的库?

在Google合作实验室中使用小部件

Google合作实验室中的Swift内核

Google合作实验室的新表单功能

将数据导入Google合作实验室

合并Google合作实验室中的单元格

在Google合作实验室中更改系统时间

检测VM关闭Google合作实验室

Tensorflow在GPU上比在CPU上慢

GPU上的Tensorflow Matmul计算比CPU慢

如何在Google合作实验室中挂载Google共享驱动器?

如何对具有共享链接的用户隐藏Google合作实验室中的秘密密钥?

无法在Google合作实验室中上传本地文件

Google合作实验室会在10-15分钟后断开连接

Google合作实验室中的“游乐场模式”是什么?

如何从上载的png文件打开Google合作实验室笔记本中的图像?

Jupyter Notebook中的Google合作实验室出现IOPub错误

使用matplotlib时,LaTeX方程不会在Google合作实验室中呈现