Long sequential learning with Torch.utils.checkpoint.checkpoint

username: Tobias_Czempiel
cooked: <p>I would like to use model checkpointing to run a CNN + RNN in an end-to-end fashion. One does not necessarily need checkpointing for this problem but I was hoping to backprop into the cnn using a very long sequence e.g. 500 images. Without any further measures to reduce the memory footprint this inevitably breaks any training process due to the cuda out of memory error.</p>
<p>I found some helpful resources:<br>
<a href="https://pytorch.org/docs/stable/checkpoint.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/checkpoint.html</a></p><aside class="onebox githubblob" data-onebox-src="https://github.com/prigoyal/pytorch_memonger/blob/master/tutorial/Checkpointing_for_PyTorch_models.ipynb">
  <header class="source">

      <a href="https://github.com/prigoyal/pytorch_memonger/blob/master/tutorial/Checkpointing_for_PyTorch_models.ipynb" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/prigoyal/pytorch_memonger/blob/master/tutorial/Checkpointing_for_PyTorch_models.ipynb" target="_blank" rel="noopener nofollow ugc">prigoyal/pytorch_memonger/blob/master/tutorial/Checkpointing_for_PyTorch_models.ipynb</a></h4>


    <pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trading compute for memory in PyTorch models using Checkpointing\n",
    "\n",
    "Given the fact that deep learning explorations are bottlenecked by device memory constraints, we implemented a gradient checkpointing technique described in [1] and using this technique, we can achieve 4x-10x larger models ranging across convolutional networks, recurrent networks and medical imaging applications. The implementations of memory optimized versions of models along with their baselines is available [here](https://github.com/prigoyal/pytorch_memonger/tree/master/models). The results of checkpointing are below:\n",
    "\n",
    "&lt;img src=\"results.png\"&gt;\n",
    "\n",
    "As we see from the above results, for ResNet-1001 model, baseline implementation can only fit 15 images and for better packing and layers optimization, we use minibatch size of 8 per run and do 4 runs to simulate minibatch size 32 training. But in the checkpointed model, we can fit ~52 images in minibatch and hence use 32 images in each run. As evident from the results, per iteration &lt;i&gt; speed improves by ~10% &lt;/i&gt; and the model accuracy will be better because of stable batch normalization layers. \n",
    "\n",
    "###  Gradient Checkpointing\n",
    "\n",
    "Gradient Checkpointing technique reorders the forward and reverse computation of the model so as to reduce the maximal length of the autograd tape. This is achieved by dividing the model or parts of the model in various segments and executing the segments without taping them in the forward pass i.e. their taping is delayed until the backward pass. This results in not storing the activations of those segments in memory. During the backward pass, since we need activations to compute the gradients, the forward pass is done again on the segment and the pass is taped. In this way, at any given time, the tape length is the maximum of tape length of various segments and not the sum.\n",
    "\n",
    "##  Using Checkpointing for PyTorch models\n",
    "\n",
</code></pre>


  This file has been truncated. <a href="https://github.com/prigoyal/pytorch_memonger/blob/master/tutorial/Checkpointing_for_PyTorch_models.ipynb" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/csrhddlam/pytorch-checkpoint">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/csrhddlam/pytorch-checkpoint" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/f7d378d03a515d9d65e30e458835d6d1b229ecb121902cfa2b2f786dc727918b/csrhddlam/pytorch-checkpoint" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/csrhddlam/pytorch-checkpoint" target="_blank" rel="noopener nofollow ugc">GitHub - csrhddlam/pytorch-checkpoint</a></h3>

  <p>Contribute to csrhddlam/pytorch-checkpoint development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>If I understood model checkpointing correctly it should be possible to skip the gradient computation in the forward pass and in the backward pass calculate the gradient by rerunning a forward pass for each segment - trading speed for less memory consumption.</p>
<p>I am still a bit clueless on how to implement this as there seems to be no complete example with an RNN available.</p>
<p>Another resource I found is from fairscale</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://fairscale.readthedocs.io/en/stable/api/nn/checkpoint/checkpoint_activations.html">
  <header class="source">
      <img src="https://fairscale.readthedocs.io/en/stable/api/nn/checkpoint/checkpoint_activations.html/../../../_static/images/favicon.png" class="site-icon" width="" height="">

      <a href="https://fairscale.readthedocs.io/en/stable/api/nn/checkpoint/checkpoint_activations.html" target="_blank" rel="noopener nofollow ugc">fairscale.readthedocs.io</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://fairscale.readthedocs.io/en/stable/api/nn/checkpoint/checkpoint_activations.html" target="_blank" rel="noopener nofollow ugc">Activation Checkpoint | FairScale 0.4.0 documentation</a></h3>

  <p>API docs for FairScale. FairScale is a PyTorch extension library for high performance and large scale training.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
Is this a similar kind of model checkpointing compared to pytorch checkpointing?</p>
<p>Any help or pointers to additional reading material is highly appreciated.</p>