解决RuntimeError: cuda runtime 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

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

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

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

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


 
 
  1. import os
  2. 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

 

ce-side assert triggered是由于代码中出现了索引越界导致的错误。这个错误是CUDA报错,具体原因可能是代码中的某个索引超出了合法范围。然而,根据引用\[2\]所述,报错位置的代码并不一定是错误的根本位置,因此需要进一步分析。根据引用\[3\]的问题分析,可能是由于维度范围溢出导致的CUDA错误。为了解决这个问题,可以检查代码中的索引操作,确保索引值在合法范围内。此外,还可以尝试使用调试工具来定位错误的具体位置,并查看相关的报错信息以获取更多的线索。 #### 引用[.reference_title] - *1* *2* [RuntimeError: CUDA error: device-side assert triggered Pytorch框架代码运行错误解决方案(亲测有效!...](https://blog.csdn.net/weixin_42112050/article/details/120455407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [「Bug」问题分析 RuntimeError: CUDA error: device-side assert triggered](https://blog.csdn.net/ViatorSun/article/details/125207465)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值