如何处理运行时错误:CUDA错误:PyTorch中遇到非法内存访问?

Tob*_*ob 5 python pytorch

我正在启用 GPU 的机器上使用 pytorch 1.7 和 cuda 11.0 运行一些代码。我有一些类包含一些不同神经网络的代码。我的第一堂课运行顺利,以下打印语句有效:

A类:

print("Device resnet", torch.cuda.current_device()) # Prints 0
print("Boxes resnet", boxes) # Prints the array of bounding boxes
Run Code Online (Sandbox Code Playgroud)

类 A 创建类 b 的实例,并将框传递给构造函数。但是当我在 b 类中执行类似的 print 语句时,它崩溃了:

print("Device fastrcnn", torch.cuda.current_device()) # Prints 0
print("BOXES in fastrcnn", boxes) # crashes and prints the following error
Run Code Online (Sandbox Code Playgroud)

这会崩溃并打印:

RuntimeError: CUDA error: an illegal memory access was encountered
Run Code Online (Sandbox Code Playgroud)

我该如何调试这个,我已经尝试添加.cpu().cuda()到框以将它们传输到相应的设备,但我遇到了完全相同的崩溃。