有时候,当PyTorch运行时,GPU显存已满,会报错:
RuntimeError: CUDA out of memory. Tried to allocate 916.00 MiB (GPU 0; 6.00 GiB total capacity; 4.47 GiB already allocated; 186.44 MiB free; 4.47 GiB reserved in total by PyTorch; 6.00 GiB total capacity; 4.47 GiB already allocated; 186.44 MiB already allocated. 186.44 MiB free; 4.47 GiB reserved in total by PyTorch)
有时可能不是模型大小造成的,那么如何一步步排查问题呢?
尝试过非常小的模型,但问题仍然存在。怎么解决?
请上传训练源代码,否则几乎无法调试。一个常见的错误是,当你存储每一步的损失来计算总损失时,你没有将值从计算图中分离出来,因此计算图在每一步之后都不能被丢弃,这会累积并占用所有的空间记忆。