【深度学习 DeBug 小技巧!】RuntimeError: CUDA error: device-side assert triggeredCUDA(用CPU debug 解决后再用GPU)

52 篇文章 2 订阅
22 篇文章 0 订阅

在 Pytorch 到 onnx 转化的过程中,出现以下问题。

/path/model/bin2onnx.py:157: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if sign != 0:
/path/model/bin2onnx.py:172: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if sign != 0:
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [96,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [97,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [98,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [99,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [100,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
../aten/src/ATen/native/cuda/Indexing.cu:922: indexSelectSmallIndex: block: [2,0,0], thread: [101,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
...
RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
1. 将 device 改为 CPU
# device = torch.device("cuda:0")
# ⬆️原代码,⬇️ 修改后
device = torch.device("cpu")

将所有 tensor 、变量所在的 device 改为 CPU 后,报错变得可读性更强的!

IndexError: index out of range in self
2. 通过对每个输入变量进行观测,定位问题
class Embedder(nn.Module):
    def __init__(self, vocab_size, d_model, padding_idx=None):
        super().__init__()
        self.embed = nn.Embedding(vocab_size, d_model, padding_idx)
    def forward(self, x):
        return self.embed(x)

self.coord_embed_x = Embedder(BBOX+COORD_PAD+SVG_END, self.embed_dim, padding_idx=MASK)
self.coord_embed_y = Embedder(BBOX+COORD_PAD+SVG_END, self.embed_dim, padding_idx=MASK)

在本文采用的代码中,Embedder 在定义时就确定了 vocab_size = BBOX+COORD_PAD+SVG_END

因此,输入的形状也需要匹配

pixel_seq = torch.randint(0, BBOX+COORD_PAD+SVG_END, (n_samples, 2), device=device)
xy_seq = torch.randint(0, BBOX+COORD_PAD+SVG_END, (n_samples, 2, 2), device=device)

🔥结论

遇到类似问题,可以先调到 CPU 上,搞清楚真实问题后再逐步排查,效果更佳!

参考文章:
[1] https://blog.csdn.net/weixin_43301333/article/details/121155260
[2] https://blog.csdn.net/BetrayFree/article/details/134267306

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`RuntimeError: CUDA error: device-side assert triggered` 是一个常见的CUDA错误,通常表示在执行CUDA计算时遇到了一个条件,这个条件在CUDA内核(kernel)中被视为无效或不正确的,导致内核停止执行并抛出了异常。 CUDA是NVIDIA的并行计算平台,用于加速GPU上的计算密集型任务。当遇到这个错误,可能的原因包括但不限于: 1. **内存访问错误**:比如试图访问未初始化的内存、越界访问或者写入只读内存。 2. **数据类型不匹配**:如果操作的数据类型不符合预期,也可能触发此错误。 3. **硬件故障或驱动问题**:可能是GPU硬件故障,或者是CUDA驱动版本过旧或不兼容。 解决这类问题的一般步骤包括: 1. **检查代码**:确保你在CUDA代码中对内存和数据的操作是正确的,避免越界或非法内存访问。 2. **打印详细信息**:在CUDA代码中添加适当的日志语句,捕获并记录错误发生时的详细信息,这有助于定位问题。 3. **更新驱动**:确保使用的CUDA驱动是最新的,并与你的硬件兼容。 4. **错误排查工具**:使用NVIDIA的CUDA Visual Profiler 或者其他的性能分析工具来检查潜在的性能问题或资源冲突。 如果你在Python中遇到这个错误,可能是使用了`torch`或`cupy`这样的库,它们会包装底层的CUDA调用。在这些库中,错误更易于调试,因为它们通常会提供更详细的错误消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值