Out of memory during torch.save

utsav: <p>Hi,</p>
<p>I was training a model which takes around 16 GB during training and ~5GB during validation but when I try to save a checkpoint(after validation) the GPU seems to run out of memory. These numbers are for a batch size of 64, if I drop the batch size down to even 32 the memory required for training goes down to 9 GB but it still runs out of memory while trying to save the model.</p>
<p>I am saving only the <code>state_dict</code>, using CUDA 8.0 with PyTorch 2.01 and running this on a 16 GB GPU.</p>
<p>Below is the stack trace for the error</p>
<pre><code class="lang-auto">THCudaCheck FAIL file=/pytorch/torch/csrc/generic/serialization.cpp line=38 error=2 : out of memory
Testing Results: Prec@1 52.974 Prec@5 80.941 Loss 1.75623
Traceback (most recent call last):
  File "main.py", line 264, in &lt;module&gt;
    main()
  File "main.py", line 109, in main
    }, is_best)
  File "main.py", line 213, in save_checkpoint
    torch.save(state, filename)
  File "/export/home/utsav/.local/lib/python2.7/site-packages/torch/serialization.py", line 120, in save
    return _save(obj, f, pickle_module, pickle_protocol)
  File "/export/home/utsav/.local/lib/python2.7/site-packages/torch/serialization.py", line 192, in _save
    serialized_storages[key]._write_file(f)
RuntimeError: cuda runtime error (2) : out of memory at /pytorch/torch/csrc/generic/serialization.cpp:38
THCudaCheckWarn FAIL file=/pytorch/torch/lib/THC/THCStream.cpp line=50 error=2 : out of memory
</code></pre>

utsav: <p>Shifting the model to CPU before saving and moving it back to GPU after saving solved it.</p>

ParthTrehan: <p>how did you shift the model to cpu</p>

ptrblck: <p><code>model.cpu()</code> or <code>model.to('cpu')</code> should work.</p>

bily: <p>Hi, I have spotted the same problem. The suggested fix works for single GPU training. However, for distributed training, the model freezes after model.cuda(). Do you have any suggestions for distributed training?</p>

ptrblck: <p>I’m not sure where you are using <code>model.cuda()</code>, since the original issue was seen during saving and the workaround was to push the model to the CPU first.</p>

bily: <p>Thanks for your response. I save model every epoch and need model.cuda() at the beginining of a new epoch after pushing the model to the CPU.</p>

ptrblck: <p>This would indicate that other/additional tensors are stored on the GPU, so that you cannot push the model back to it, so you should delete the other objects on the GPU (e.g. the old model in case it’s still on the GPU).</p>

bily: <p>Models are trained using distributed data parallel and each process contains a model. Only the model at the master process is pushed to CPU and then back to CUDA. Is it possible that the training process freezes due to synchronization problem? How can I delete the other objects on the GPU and create new model and start distributed training?</p>

arnaud-nt2i: <p><a class="mention" href="/u/bily">@bily</a> <a class="mention" href="/u/ptrblck">@ptrblck</a> This is an old post, but I am facing the same unanswered question,<br>
On solution, I see is:</p>
<ol>
<li>on rank 0 or 1 :  model.cpu() + ckpt =  deepcopy(model.module.half()) + torch.save(ckpt, f)</li>
<li>del model + torch.cuda.empty_cache() + gc.collect()</li>
<li>re instantiate ddp training from model= torch.load(f)</li>
</ol>
<p>But this is not elegant, a more natural solution should be to model.cpu()  and right after model.cuda() ( but with ddp compatibility)<br>
Or being able to make a copy to cpu of the model without altering the gpu memory…</p>