question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when performing inner loop on a copy

See original GitHub issue

When running the following script, memory usage seems to continually increase (not all iterations, but many of them in the beginning). Explicitly decrementing the reference counts for the functional model/differentiable optimizer seems to help, but not completely fix the issue. Script for reproducing attached. Monitoring GPU memory usage with watch -n 0.1 nvidia-smi. Running PyTorch version 1.3.0, torchvision 0.4.1, higher version 0.1.3@59537fa. Let me know if any other information would be helpful.

Code to reproduce the issue:

import torch, torchvision
import higher
import time
from copy import deepcopy


print(torch.__version__, torchvision.__version__)

inner_loop_copy = True # Setting this to False gives no memory usage increase

device = torch.device('cuda:0')
model = torchvision.models.resnet18().to(device)
opt = torch.optim.SGD(model.parameters(), lr=1e-5)

for idx in range(100):
    print(idx)
    if inner_loop_copy:
        model_ = deepcopy(model)
        opt_ = torch.optim.SGD(model_.parameters(), lr=1e-5)
    else:
        model_ = model
        opt_ = opt

    with higher.innerloop_ctx(model_, opt_) as (fm, do):
        pass

    # del fm, do # Uncommenting this helps, but doesn't completely eliminate the memory usage increase

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-mitchellcommented, Oct 29, 2019

Got it. Not sure I will be able to devote much time to it either. For the moment, though, it seems to be at least partially alleviated by explicitly deleting the functional model/differentiable optimizer.

0reactions
egrefencommented, Dec 12, 2019

Fixed in #15 (we believe). Please let us know if you have ongoing issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak when cloning byte[] in a loop - Android
When I send the data using Arrays.copyOf(), System.arrayCopy() or data.clone(), OutOfMemory is occurs. When I send data directly, there is no ...
Read more >
Memory Leak when looping over out-links of objects - Jazz.net
Memory leaks is DOORS is a complicated issue. Most of the time it is not obvious where the memory is leaked. When interating...
Read more >
Do repeated Local and Global variable use in loops cause ...
The basic answer to "Do repeated Local and Global variable use in loops cause memory leaks?" is no. You can read / write...
Read more >
4 Types of Memory Leaks in JavaScript and How to Get Rid Of ...
The main cause for leaks in garbage collected languages are unwanted references. To understand what unwanted references are, first we need to ...
Read more >
Nested monadic loops may cause space leaks
It doesn't do much — except, as it turns out, eat a lot of memory! ... These nested loops happen often in server-side...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found