Python 为什么Pytorch(CUDA)在GPU上运行缓慢

Python 为什么Pytorch(CUDA)在GPU上运行缓慢,python,machine-learning,pytorch,Python,Machine Learning,Pytorch,我在Linux上玩Pytorch已经有一段时间了,最近我决定尝试在Windows桌面上用我的GPU运行更多的脚本。自从尝试这一点以来,我注意到在相同的脚本上,我的GPU执行时间和CPU执行时间之间存在巨大的性能差异,因此我的GPU明显比CPU慢。为了说明这一点,我在这里找到了一个教程程序() 导入火炬 导入日期时间 打印(火炬版本) dtype=torch.double #设备=火炬。设备(“cpu”) 设备=火炬。设备(“cuda:0”) #N为批量大小;D_in为输入维; #H为隐维;D_o

我在Linux上玩Pytorch已经有一段时间了,最近我决定尝试在Windows桌面上用我的GPU运行更多的脚本。自从尝试这一点以来,我注意到在相同的脚本上,我的GPU执行时间和CPU执行时间之间存在巨大的性能差异,因此我的GPU明显比CPU慢。为了说明这一点,我在这里找到了一个教程程序()

导入火炬
导入日期时间
打印(火炬版本)
dtype=torch.double
#设备=火炬。设备(“cpu”)
设备=火炬。设备(“cuda:0”)
#N为批量大小;D_in为输入维;
#H为隐维;D_out是输出维度。
N、 D_in,H,D_out=64100010010
#创建随机输入和输出数据
x=torch.randn(N,D_in,device=device,dtype=dtype)
y=torch.randn(N,D_out,device=device,dtype=dtype)
#随机初始化权重
w1=火炬。随机数(D_in,H,装置=装置,数据类型=数据类型)
w2=火炬.randn(H,D_out,device=device,dtype=dtype)
start=datetime.datetime.now()
学习率=1e-6
对于范围内的t(5000):
#向前传球:计算预测y
h=x.mm(w1)
h_relu=h.夹具(最小值=0)
y_pred=h_relu.mm(w2)
#计算和打印损耗
损失=(y_pred-y).pow(2).sum().item()
#打印(t,损耗)
#Backprop计算w1和w2相对于损失的梯度
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()
梯度h[h<0]=0
梯度w1=x.t().mm(梯度h)
#使用梯度下降更新权重
w1-=学习率*年级w1
w2-=学习率*年级w2
end=datetime.datetime.now()
打印(结束-开始)
我将Epoch的数量从500增加到5000,因为我已经了解到,由于初始化,第一个CUDA调用非常缓慢。但是,性能问题仍然存在

使用
device=torch.device(“cpu”)
打印出的最终时间通常在3-4秒左右,而
device=torch.device(“cuda:0”)
在13-15秒左右执行

我已经通过多种不同的方式重新安装了Pytork(当然是卸载以前的安装),但问题仍然存在。我希望有人能帮助我,如果我可能错过了一套(没有安装其他API/程序)或在代码中做了一些错误的事情

Python:v3.6

Pyrotch:v0.4.1

GPU:NVIDIA GeForce GTX 1060 6GB


任何帮助都将不胜感激:微微一笑:

主要原因是您使用的是双数据类型而不是float。GPU主要针对32位浮点数的操作进行优化。如果您将dtype更改为torch.float,那么您的GPU运行速度应该比CPU运行速度快,即使包括CUDA初始化之类的内容。

当您以较小的批处理大小运行时,在GPU上运行可能会很昂贵。如果您将更多数据放入gpu,意味着增加批处理大小,那么您可以观察到数据量的显著增加。是的,gpu使用float32比使用double运行得更好。 试试这个

**


**

免责声明:我也在pytorch论坛()上问过这个问题,只是不知道它们有多活跃。你的CUDA版本是什么?你们有多少个GPU?根据Pytorch的说法,Cuda的版本是9.0(使用
torch.version.Cuda
)。我只有1 1060你能用这个密码试试吗?更改第85-87行以查看cuda和cpu速度。如果你用上面的代码发布cuda和cpu的速度结果,我可以提供帮助。最有可能的问题与小计算有关。这确实极大地提高了网络的性能,尽管它并没有使cuda比我的cpu快。事实上,随着网络规模的大幅增加,cuda的快速性也得到了提升。我又增加了两个更大的层,这需要提高cuda相对于cpu的性能。
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)
N, D_in, H, D_out = 128, 1000, 500, 10
dtype = torch.float32