关于CUDA error: device-side assert triggered问题小技巧

有时候写PyTorch代码的时候,会意外遇到如下的cuda报错:

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.

并且前面还有海量的(根本翻不完的)报错信息:

Assertion idx_dim >= 0 && idx_dim < index_size && "index out of bounds"

在这里插入图片描述

冗余的报错信息导致我们没办法定位到出错的代码段,这里