python - PyTorch - RuntimeError:转换:同步失败:cudaErrorIllegalAddress
问题描述
我有一个问题,当我在 Google Colab 上运行我的模型时,我经常收到这个错误:
RuntimeError: transform: failed to synchronize: cudaErrorIllegalAddress: an illegal memory access was encountered
我不会一直发生。有时它可以正常工作,但又不行。
(如果它有效与否似乎与批量大小有关,请参阅底部的编辑。)
仅在CPU上运行时没有问题,因此似乎与GPU/CUDA有关。
错误回溯显示错误发生在backward()
.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-1-c8bbb05fb58a> in <module>()
65 out = model(chars, pos)
66 loss = F.binary_cross_entropy_with_logits(out, labels)
---> 67 loss.backward()
1 frames
/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
164 products. Defaults to ``False``.
165 """"""
--> 166 torch.autograd.backward(self, gradient, retain_graph, create_graph)
167
168 def register_hook(self, hook):
/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
97 Variable._execution_engine.run_backward(
98 tensors, grad_tensors, retain_graph, create_graph,
---> 99 allow_unreachable=True) # allow_unreachable flag
100
101
RuntimeError: transform: failed to synchronize: cudaErrorIllegalAddress: an illegal memory access was encountered
一旦发生此错误,我将无法在该运行时执行与 GPU 相关的任何操作。每当我尝试在 GPU 上创建一些张量左右时,我都会收到一条略有不同的错误消息:
RuntimeError: CUDA error: an illegal memory access was encountered
我需要重新启动运行时/笔记本才能在 GPU 上做一些事情(Pytorch 没有尝试过其他框架)。
这是一个应该能够在 Google Colab 中重现该问题的代码片段:
import os
#os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(torch.__version__, torch.cuda.get_device_name(0))
from torch import nn
from torch.nn import functional as F
class MyModel(nn.Module):
def __init__(self, char_vocab, num_pos, dim, hidden, dropout):
super().__init__()
self.emb_char = nn.Embedding(char_vocab, dim)
self.cnn1 = nn.Conv1d(dim, dim // 2, kernel_size=3)
self.bn1 = nn.BatchNorm1d(dim // 2)
self.cnn2 = nn.Conv1d(dim // 2, dim // 4, kernel_size=2)
self.bn2 = nn.BatchNorm1d(dim // 4)
self.pooling = nn.MaxPool1d(2)
self.lin1 = nn.Linear(11, hidden)
self.bn3 = nn.BatchNorm1d(hidden)
self.lin2 = nn.Linear(hidden, dim // 4)
self.bn4 = nn.BatchNorm1d(dim // 4)
self.out = nn.Linear(dim // 4, 1)
self.drop = nn.Dropout(dropout)
def forward(self, chars, pos):
x = self.emb_char(chars).transpose(1, 2)
x = self.drop(x)
x = self.cnn1(x)
x = self.bn1(x)
x = self.pooling(x)
x = F.relu(x)
x = self.cnn2(x)
x = self.bn2(x)
x = self.pooling(x).squeeze(-1)
x = F.relu(x)
x = self.drop(x)
y = F.relu(self.lin1(pos))
y = self.bn3(y)
y = self.drop(y)
y = F.relu(self.lin2(y))
y = self.bn4(y)
y = self.drop(y)
return self.out(x+y)
model = MyModel(char_vocab=80, num_pos=11, dim=32, hidden=8, dropout=0.5).to(device)
batch_size = 160000
# inputs
chars = torch.randint(0, 79, (batch_size, 8), device=device)
pos = torch.rand(batch_size, 11, device=device)
# labels
labels = torch.ones(batch_size, 1, dtype=torch.float, device=device)
# forward
out = model(chars, pos)
loss = F.binary_cross_entropy_with_logits(out, labels)
# backward
loss.backward()
Pytorch 版本是1.3.1
它运行的 GPU 是P100-PCIE-16GB
.
任何想法如何摆脱这个错误?
编辑:
我添加了一段带有生成输入的代码,可以重现该问题。
这似乎与我正在使用的高批量大小有关。我在代码中使用的批量大小
160000
似乎可靠地导致了这个错误,但是我注意到它并不仅仅取决于批量大小。我试图将其减少到80000
并仍然出现此错误。但我知道,在我之前的运行中,我有更高的批量大小,直到180000
没有问题。同样从60000
工作的批处理大小开始,然后使用相同的运行时,我可以再次运行它,增加的批处理大小140000
没有错误(之前失败)。
请注意,内存使用量非常低/仅 ~1-2 GB设置
CUDA_LAUNCH_BLOCKING=1
会导致不同的错误消息:RuntimeError: fractional_max_pool2d_backward_out_cuda failed with error code 0
但在代码中的同一点并具有相同的回溯。
解决方案
推荐阅读
- erlang - ejabberd live/start 不工作 - 返回错误 - “无法启动 ejabberd 应用程序:配置错误:重复选项:听”
- xml - XML 从 XSD ComplexType 创建元素名称,而不是从元素名称
- javascript - SweetAlert - 更改模态颜色
- javascript - 不确定我的地图对象在示例中使用的空间复杂度是多少?
- java - 为什么执行“boolean t = true,b; b = (t || ((i++) == 0))”后“i”的值没有变化
- go - 为我的大学网站制作自定义 RSS 提要
- android - 查找设备android studio的位置时出错
- python - 通过阈值检测苹果
- python - 如何使用 BeautifulSoup 在 Python 中接收网站链接
- python - 替换除 Python 中第一次出现的子字符串之外的所有内容?