Explanation of exact effect of AMP on memory usage

username: jastern33
cooked: <p>I’m writing a blog post breaking down Pytorch memory usage at each step of training, including special cases like training with mixed precision. I have come up with an accurate memory model but it fails in the case of AMP. <a href="https://colab.research.google.com/drive/1TWV6dKmLHhW0auWIUF9ER2kXb56Zn9UT" rel="noopener nofollow ugc">Here is a link</a> to a Colab notebook with the memory model, where you can replicate what I describe in this post.</p>
<p>From what I understand of how mixed precision works, it should save you exactly half of the memory that the forward pass would normally take (see <a href="https://developer.nvidia.com/blog/mixed-precision-training-deep-neural-networks/?source=post_page-----e9cab0ead93----------------------" rel="noopener nofollow ugc">this</a> and <a href="https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html" rel="noopener nofollow ugc">this</a> and <a href="https://www.sicara.ai/blog/2019-28-10-deep-learning-memory-usage-and-pytorch-optimization-tricks" rel="noopener nofollow ugc">this</a> - this last one uses apex, not the native pytorch amp, but is the first empirical evidence I have seen that amp decreases forward pass memory by half). This is because the forward activations are stored in fp16, although the gradients and model weights are in fp32. However in experiments that I have run, I have seen erratic memory usage for AMP. I would like to understand what is going on, so that I can consistently get the memory savings that I expect.</p>
<p>I have written a code to estimate maximum memory usage based on the model size, the input size, the batch size, the optimizer type, and whether AMP is being used. The first function estimates the memory and the second function runs the code to measure the actual memory usage. The estimator function is accurate to the true memory usage, except when <code>use_amp=True</code>.</p>
<p>One note: There are two different measures of memory usage that are relevant here, and they don’t always line up with each other. 1: The memory reported by torch.cuda.memory_allocated. 2: The memory shown by nvidia-smi after the run.</p>
<p>Here are six runs with slightly different parameters, with different outcomes, and my observations.</p>
<p>Run 1:<br>
Function: test_memory(batch_size=64, use_amp=False)<br>
Estimated max memory: 38121984 (this includes the model, gradients, optimizer momentum values, and forward pass).<br>
True max torch.cuda.memory_allocated: 38121984 (exactly correct)<br>
nvidia-smi memory after run: 501MB<br>
Observation: the prediction works as expected without use_amp</p>
<p>Run 2:<br>
Function: test_memory(batch_size=64, use_amp=True)<br>
Estimated max memory: 35536128 (reducing forward pass memory estimate by 1/2).<br>
True max torch.cuda.memory_allocated: 39634432<br>
nvidia-smi memory after run: 503MB<br>
Observation: max allocated memory is greater than without use_amp. This is the opposite of what I would expect.</p>
<p>Run 3:<br>
Function: test_memory(batch_size=128000, use_amp=False)<br>
Estimated max memory: 10375350784<br>
True max torch.cuda.memory_allocated: 10375350784 (exactly correct)<br>
nvidia-smi memory after run: 10667MiB / 11441MiB<br>
Observation: the prediction works as expected without use_amp. The only difference is that with this much larger batch, according to nvidia-smi, CUDA appears to cache much more data.</p>
<p>Run 4:<br>
Function: test_memory(batch_size=140000, use_amp=True)<br>
Estimated max memory: 5751884032<br>
True max torch.cuda.memory_allocated: 5723155456 (very similar to estimation which uses 1/2 multiplier for forward pass memory).<br>
nvidia-smi memory after run: 11429MiB / 11441MiB<br>
Observation: torch.cuda.memory_allocated is pretty close to the prediction. But the memory reflected in nvidia-smi is about double the memory shown by torch.cuda.memory_allocated.</p>
<p>Run 5:<br>
Function: test_memory(batch_size=140000, use_amp=False)<br>
Estimated max memory: 11470817792 (more than the available memory).<br>
True max torch.cuda.memory_allocated: [Memory Error]<br>
nvidia-smi memory after run: 11427MiB / 11441MiB<br>
Observation: as expected, the GPU runs out of memory. When compared with run 4, this shows that use_amp does save a small amount of memory.</p>
<p>Run 6:<br>
Function: test_memory(batch_size=150000, use_amp=True)<br>
Estimated max memory: [Memory Error]<br>
True max torch.cuda.memory_allocated: [Memory Error]<br>
nvidia-smi memory after run: 11427MiB / 11441MiB<br>
Observation: Practically, we don’t see the memory savings that AMP should enable - with 50% memory savings, we should be able to double the non-AMP batch size from run 3 to 256000.</p>
<p>The erratic behavior can be summarized as follows:<br>
1 - For small batches without AMP, it looks like very little memory is cached, but for large batches without AMP, almost all memory is cached (based on nvidia-smi run 1 vs run 3).<br>
2 - For small batches with AMP, torch.cuda.memory_allocated shows an increase in memory usage, but for large batches with AMP, torch.cuda.memory_allocated shows the expected memory savings of 1/2 the forward pass (based on torch.cuda.memory_allocated run 2 vs run 4).<br>
3 - For large batches, torch.cuda.memory_allocated shows the expected memory savings of 1/2 but nvidia-smi shows no memory savings (based on run 4 torch.cuda.memory_allocated and nvidia-smi). My theory of why this happens is that the forward pass is first computed in fp32, then cast to fp16, resulting in a large amount of cached memory. I looked at the <a href="https://pytorch.org/docs/stable/_modules/torch/cuda/amp/autocast_mode.html#autocast" rel="noopener nofollow ugc">source code for autocast</a> and it was a bit opaque - I don’t see where it explicitly casts to fp16.</p>
<p>All of these results can be replicated with the shared Colab notebook.</p>
<p>How should I expect AMP to affect memory usage? Does CUDA caching restrict available memory? I’m particularly curious about run 4, where torch.cuda.memory_allocated shows the expected AMP memory savings, but nvidia-smi shows no savings and I get a memory error. Most importantly, is it possible in PyTorch to save the expected 50% of memory on the forward pass when using AMP, like you can in Apex?</p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> calling in the big guns on this one</p>

username: jastern33
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> do you have any insight on why I am unable to achieve the expected 50% memory savings with AMP? Or a resource you could point me to where I can read up on why AMP doesn’t provide the expected memory savings in pytorch?</p>