【发布时间】:2019-02-26 17:39:59
【问题描述】:
我在 Linux 上使用 Pytorch 已经有一段时间了,最近决定尝试在我的 Windows 桌面上使用我的 GPU 运行更多脚本。自从尝试这个之后,我注意到我的 GPU 执行时间和我的 CPU 执行时间在相同的脚本上存在巨大的性能差异,因此我的 GPU 比 CPU 慢得多。为了说明这一点,我只是在这里找到了一个教程程序 (https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-tensors)
import torch
import datetime
print(torch.__version__)
dtype = torch.double
#device = torch.device("cpu")
device = torch.device("cuda:0")
# 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, 1000, 100, 10
# Create random input and output data
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)
# Randomly initialize weights
w1 = torch.randn(D_in, H, device=device, dtype=dtype)
w2 = torch.randn(H, D_out, device=device, dtype=dtype)
start = datetime.datetime.now()
learning_rate = 1e-6
for t in range(5000):
# Forward pass: compute predicted y
h = x.mm(w1)
h_relu = h.clamp(min=0)
y_pred = h_relu.mm(w2)
# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
#print(t, loss)
# Backprop to compute gradients of w1 and w2 with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.t().mm(grad_y_pred)
grad_h_relu = grad_y_pred.mm(w2.t())
grad_h = grad_h_relu.clone()
grad_h[h < 0] = 0
grad_w1 = x.t().mm(grad_h)
# Update weights using gradient descent
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
end = datetime.datetime.now()
print(end-start)
我将 Epoch 的数量从 500 增加到 5000,因为我读到第一个 CUDA 调用由于初始化而非常慢。但是性能问题仍然存在。
device = torch.device("cpu") 打印出来的最后时间是正常的大约 3-4 秒,device = torch.device("cuda:0") 在大约 13-15 秒内执行
我已经通过多种不同的方式重新安装了 Pytorch(当然是卸载之前的安装),但问题仍然存在。我希望有人可以帮助我,如果我可能错过了一组(没有安装其他 API/程序)或在代码中做错了什么。
Python:v3.6
Pytorch:v0.4.1
GPU:NVIDIA GeForce GTX 1060 6GB
任何帮助将不胜感激:slight_smile:
【问题讨论】:
-
免责声明:我也在pytorch论坛(discuss.pytorch.org/t/why-is-pytorch-cuda-running-slow-on-gpu/…)上问过这个问题,只是不知道他们有多活跃。
-
您的 CUDA 版本是多少?你有多少 GPU?
-
根据 Pytorch,Cuda 版本是 9.0(使用
torch.version.cuda)。而我只有 1 1060 -
你可以试试这个代码吗?更改第 85-87 行以查看 cuda 和 cpu 速度。 gist.github.com/salihkaragoz/88d313df6a7c91e64a7c3be0df003e6e
-
如果你用上面的代码发布cuda和cpu的速度结果,我可以帮忙。很可能是与小计算有关的问题。
标签: python machine-learning pytorch