RuntimeError: cuda runtime error (59) : device-side assert triggered

Environment: Ubuntu,Pytorch1.0.1

运行程序时发现这个问题

Traceback (most recent call last):
  File "train_pytorch1.py", line 217, in <module>
    loss = F.cross_entropy(output, target)
  File "/usr/local/python3/lib/python3.5/site-packages/torch/nn/functional.py", line 1970, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/usr/local/python3/lib/python3.5/site-packages/torch/nn/functional.py", line 1790, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:111

这个异常大概是和计算损失值有关

查阅资料时发现很多道友都遇到过这种cuda runtime error(59),大部分都是索引异常

根据这篇帖子中一位Pytorch Dev所述,由于cuda的异步性质,断言可能不会指向指向断言从哪里触发的完整正确的堆栈跟踪。

在程序导入模块前,加入下述语句,可以打印出更多的细节

import os
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"

异常输出如下:

/pytorch/aten/src/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: [6,0,0] Assertion `t >= 0 && t < n_classes` failed.
THCudaCheck FAIL file=/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu line=111 error=59 : device-side assert triggered
Traceback (most recent call last):
  File "train_pytorch1.py", line 217, in <module>
    loss = F.cross_entropy(output, target)
  File "/usr/local/python3/lib/python3.5/site-packages/torch/nn/functional.py", line 1970, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/usr/local/python3/lib/python3.5/site-packages/torch/nn/functional.py", line 1790, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:111

根据所打印出的信息,猜测是label的索引出现问题。

于是在打印出读入数据的label,发现果然出现了问题

正确的索引应该是0~44,而程序中读入的是1~45。对label修正后,程序正常运行。

关于调试CUDA断言,推荐一博客,个人认为解释的比较清楚了。Debugging CUDA device-side assert in PyTorch

 

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值