【问题标题】:the result from torch.concat() is stored in cpu(memory)?torch.concat() 的结果是否存储在 cpu(memory) 中?
【发布时间】:2022-09-23 21:38:38
【问题描述】:

编码

c = torch.rand((2000, 64, 64)).to(\'cuda\')
d = torch.rand((2000, 64, 64)).to(\'cuda\')
t3 = time.time()
s1 = c+d
s2 = torch.concat((a, b), dim=2)
t4 = time.time()

s1的设备是gpu,而s2的设备是cpu。

所以我无法理解。这是什么原理?

  • 一,乙没有定义。 torch.cat((c, d), dim=2) 也存储在设备 cuda 上。顺便说一句:使用torch.rand((2000, 64, 64), device=\'cuda\') 防止在 CPU 上创建并复制到 GPU。

标签: python pytorch cuda


【解决方案1】:

如果操作的所有必要变量都在同一设备上,Torch 将执行操作。

我想 ab 在 CPU 上的位置,因此 torch.concat((a, b), dim=2) 也是。

当您执行.to('cuda') 时,您已将cd 移至GPU,因此s1 也在GPU 上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2021-01-15
    相关资源
    最近更新 更多