Optimizer closure causes out-of-memory error

Tia: <p>Hi, I met an out-of-memory error when I use closure in my new optimizer, which needs the value of the loss f(x) to update the parameters x in each iteration. The scheme is like</p>
<blockquote>
<p>g(x) = f’(x)/f(x)<br>
x = x - a*g(x)</p>
</blockquote>
<p>So I define a closure function before optimizer.step</p>
<pre data-code-wrap="python"><code class="lang-python">def closure():
    optimizer.zero_grad()
    outputs = net(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    return loss, outputs
loss, outputs = optimizer.step(closure) 
</code></pre>
<p>It works fine when I apply this optimizer to train a CNN model for MNIST. However, when I use this optimizer to train ResNet34 for cifar10, even on an HPC cluster, the program will be killed after a few iterations because of an out-of-memory error.</p>
<p>I think the memory of the compute node (128G) is large enough, and it works fine when I change the optimizer to torch.optim.SGD, with the same other settings. The corresponding code for SGD is:</p>
<pre data-code-wrap="python"><code class="lang-python">optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
</code></pre>
<p>So the only difference I can notice is the use of closure function in the new optimizer.</p>
<p>I have two questions:</p>
<ol>
<li>Did I use the closure function correctly? So far this new optimizer works fine for some smaller data like MNIST, but since closure is rarely used in optimizers, and there are not many examples, so I am not so sure if I used the closure correctly.</li>
<li>Is the out-of-memory error caused by the use of closure function? It’s not clear for me how the closure and optimizer.step work in PyTorch so I have no idea where this out-of-memory error comes from.</li>
</ol>
<p>Any help is highly appreciated! Thanks!</p>

ptrblck: <p>I’m not sure how your new optimizer is defined, but e.g. <code>LBFGS</code> (which also requires a closure) can be memory intensive as stated in the <a href="https://pytorch.org/docs/stable/optim.html#torch.optim.LBFGS">docs</a>:</p>
<blockquote>
<p>This is a very memory intensive optimizer (it requires additional  <code>param_bytes * (history_size + 1)</code>  bytes). If it doesn’t fit in memory try reducing the history size, or use a different algorithm.</p>
</blockquote>
<ol>
<li>If your optimizer’s <code>step</code> method expects the <code>output</code> and <code>loss</code> as a return value, then the <code>closure</code> looks correct. You could compare it to <a href="https://github.com/pytorch/examples/blob/69d2798ec7fb4f87b320a1848203da5346675b95/time_sequence_prediction/train.py#L57-L64">this example</a>.</li>
<li>Might be and it depends, what <code>optimizer.step</code> is doing with the <code>closure</code>. If you are storing the history of the losses and outputs, then an increased memory usage is expected.</li>
</ol>

Tia: <p>Thank you for your reply! I later realized that I just need the value of the the loss, not all the metadata. so instead of saving loss and outputs, I changed my code as</p>
<pre><code class="lang-python">def closure():
    optimizer.zero_grad()
    outputs = net(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    return loss.detach(), outputs.detach()
loss, outputs = optimizer.step(closure)
</code></pre>
<p>That works fine, no out-of-memory error.</p>