Pytorch RuntimeError:CUDA 内存不足,可用内存量很大

xiaoxingxing pytorch 5760

原文标题Pytorch RuntimeError: CUDA out of memory with a huge amount of free memory

在训练模型时,我遇到了以下问题:

RuntimeError: CUDA out of memory. Tried to allocate 304.00 MiB (GPU 0; 8.00 GiB total capacity; 142.76 MiB already allocated; 6.32 GiB free; 158.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

正如我们所见,尝试分配 304 MiB 内存时发生错误,而 6.32 GiB 是空闲的!问题是什么?正如我所看到的,建议的选项是设置 max_split_size_mb 以避免碎片。它会有所帮助以及如何正确地做到这一点?

这是我的 PyTorch 版本:

火炬==1.10.2+cu113

火炬视觉==0.11.3+cu113

torchaudio===0.10.2+cu113

原文链接:https://stackoverflow.com//questions/71498324/pytorch-runtimeerror-cuda-out-of-memory-with-a-huge-amount-of-free-memory

回复

我来回复
  • Erol Gelbul的头像
    Erol Gelbul 评论

    您的问题可能是由于您的 GPU 内存碎片造成的。您可能希望清空缓存分配器使用的缓存内存。

     import torch
     torch.cuda.empty_cache()
    
    2年前 0条评论