【问题标题】:CUDA runtime error (59) : device-side assert triggeredCUDA 运行时错误 (59):设备端断言已触发
【发布时间】:2019-01-12 11:09:39
【问题描述】:

我可以访问 Tesla K20c,我正在 CIFAR10 数据集上运行 ResNet50... 然后我得到错误:

THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generated/../generic/THCTensorMathPointwise.cu line=265 error=59 : device-side assert triggered
Traceback (most recent call last):
  File "main.py", line 109, in <module>
    train(loader_train, model, criterion, optimizer)
  File "main.py", line 54, in train
    optimizer.step()
  File "/usr/local/anaconda35/lib/python3.6/site-packages/torch/optim/sgd.py", line 93, in step
    d_p.add_(weight_decay, p.data)
RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generated/../generic/THCTensorMathPointwise.cu:265

如何解决这个错误?

【问题讨论】:

  • 尝试使用CUDA_LAUNCH_BLOCKING=1 python your_script.py 运行脚本以获得更准确的堆栈跟踪。
  • 在使用 CUDA_LAUNC...=1 运行后,我得到/opt/conda/.../THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [0,0,0] Assertion t &gt;= 0 &amp;&amp; t &lt; n_classes failed. 的错误,这将出现大约 20 次。然后 Traceback 如下:RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1524580978845/work/aten/src/THCUNN/generic/ClassNLLCriterion.cu:116 如何解决?
  • 这是您的目标标签错误:t &gt;= 0 &amp;&amp; t &lt; n_classes。打印您的标签并确保它们是正数且小于您最后一层的输出数。
  • n_classes 应该和最后一层的输出一样.. 对吗?
  • 没错。您的目标可能具有很高的价值。

标签: gpu pytorch


【解决方案1】:

我多次遇到这个问题。我发现这是一个索引问题。

例如,如果您的真实标签从 1 开始:target = [1,2,3,4,5],那么您应该为每个标签减去 1,将其更改为:[0,1,2,3,4]

这每次都能解决我的问题。

【讨论】:

  • 我可以确认,这也是我的案例出错的原因。例如,有效的文本标签已转换为 0..n-1(n 是类数)。但是,NaN 值被转换为 -1,这使它偏离了轨道。
  • @Rainy 你能详细说明“ground truth label 从 1 开始”吗?你是什​​么意思?我收集标签是 1 到 5,为了克服错误,错误中的第一个值应该为零。我说的对吗?
  • @KunjMehta,不仅仅是第一个值应该为零。类索引应该从零开始。例如对于 6 个类,索引值应为 0 到 5。
  • 即使我有你提供的设置,我也会收到错误
  • 拯救了我的一天!谢谢。
【解决方案2】:

一般来说,当遇到cuda runtine errors 时,建议使用CUDA_LAUNCH_BLOCKING=1 标志再次运行您的程序以获得准确的堆栈跟踪。

在您的具体情况下,您的数据目标对于指定数量的类来说太高(或太低)。

【讨论】:

  • 此外,一旦您获得更准确的堆栈跟踪并找到问题所在,您可以将张量移动到 CPU。将张量移动到 CPU 将给出更详细的错误。将CUDA_LAUNCH_BLOCKING=1 与将张量移动到 CPU 相结合是我能够解决我花了 3 天时间解决的问题的唯一方法。
【解决方案3】:

我在运行 BertModel.from_pretrained('bert-base-uncased') 时遇到了这个错误。当错误消息更改为“IndexError: index out of range in self”时,我通过移动到 CPU 找到了解决方案。这导致我发帖this。解决方案是将句子截断为长度为 512。

【讨论】:

    【解决方案4】:

    引发“CUDA 错误:设备端断言已触发”RuntimeError 的一种方法是使用具有超出维度索引的 list 对 GPU torch.Tensor 进行索引。

    因此,这个 sn-p 会引发 IndexError 并显示消息“IndexError: index 3 is out of bounds for dimension 0 with size 3”,而不是 CUDA 错误

    data = torch.randn((3,10), device=torch.device("cuda"))
    data[3,:]
    

    然而,这会引发 CUDA“设备端断言触发”RuntimeError

    data = torch.randn((3,10), device=torch.device("cuda"))
    indices = [1,3]
    data[indices,:]
    

    这可能意味着在类标签的情况下,例如在@Rainy 的回答中,当标签从 1 而不是 0 开始时,导致错误的是最终的类标签(即 label == num_classes 时)。

    另外,当设备为"cpu" 时,抛出的错误为IndexError,例如第一个sn-p 抛出的错误。

    【讨论】:

      【解决方案5】:

      如果你先切换到 CPU,这个错误可能会更详细。一旦切换到 CPU,它将显示确切的错误,这很可能与索引问题有关,即 IndexError: Target 2 is out of bounds in my case and may be related to your case. 问题是“您当前使用了多少个类,输出的形状是什么?”,您可以找到这样的类

      max(train_labels)
      min(train_labels)
      

      在我的情况下,它给了我 2 和 0,问题是由于缺少 1 个索引引起的,所以快速破解是用 1s 快速替换所有 2s ,这可以通过以下代码完成:

      train_=train.copy()
      train_['label'] =train_['label'].replace(2,1)
      

      然后你运行相同的代码并查看结果,它应该可以工作

      class NDataset(torch.utils.data.Dataset):
          def __init__(self, encodings, labels):
              self.encodings = encodings
              self.labels = labels
      
          def __getitem__(self, idx):
              item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
              item['labels'] = torch.tensor(self.labels[idx])
              return item
      
          def __len__(self):
              return len(self.labels)
      
      train_dataset = NDataset(train_encodings, train_labels)
      val_dataset = NDataset(val_encodings, val_labels)
      test_dataset = NDataset(test_encodings, test_labels)
      

      【讨论】:

        【解决方案6】:

        我发现标签的值无效时出现此错误。

        【讨论】:

          猜你喜欢
          • 2021-09-10
          • 2020-04-07
          • 1970-01-01
          • 2019-04-15
          • 2022-09-24
          • 2020-02-03
          • 2018-11-23
          • 2021-10-14
          • 2019-09-10
          相关资源
          最近更新 更多