【发布时间】:2021-04-07 11:18:40
【问题描述】:
我正在玩 pytorch concatenate,想看看我是否可以使用与输入张量具有不同设备的输出张量,这是代码:
import torch
a = torch.ones(4)
b = torch.ones(4)
c = torch.zeros(8).cuda()
print(c)
ab = torch.cat([a,b], out=c)
print(c)
我在 jupyter 笔记本中运行它。 pytorch 版本:1.7.1
我收到以下错误:
...
\Anaconda3\envs\...\lib\site-packages\torch\_tensor_str.py in __init__(self, tensor)
87
88 else:
---> 89 nonzero_finite_vals = torch.masked_select(tensor_view, torch.isfinite(tensor_view) & tensor_view.ne(0))
90
91 if nonzero_finite_vals.numel() == 0:
RuntimeError: CUDA error: an illegal memory access was encountered
如果您尝试访问张量 c(在本例中为 print),就会发生这种情况。
我在documentation 中找不到任何说我无法这样做的内容,除了这行:
" ...任何相同类型张量的python序列..."
这个错误有点奇怪……有什么想法吗?
【问题讨论】:
-
我无法复制该问题,
cat操作会产生“预期的后端 CUDA 对象但得到了 CPU ...”,这是预期的,因为您将 2 个 cpu 张量连接到一个 gpu 张量中。print语句不会引起任何问题。您确定您粘贴的代码实际上会产生这个确切的错误吗? -
我敢肯定,这是笔记本中唯一的东西……你是在笔记本中运行的吗?火炬版本是一样的吗?很奇怪...... - 编辑:无论python内核是否新鲜,都会发生这种情况
标签: python memory pytorch concatenation