有时候写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"
冗余的报错信息导致我们没办法定位到出错的代码段,这里记录一下可能的解决方案:
-
遇到这种cuda的越界bug,可以优先查看是不是scatter代码处出现越界!比如我的代码是:
mask.scatter_(-1, topk, 0)
去仔细检查mask的维度能否存得下topk的值。
-
耐心debug,但是在多线程代码中无法使用pdb来调试,听说可以关掉多线程或者不用gpu跑,就可以调试并显示出出错的代码位置了。