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 leakage problem when loss.backward()

See original GitHub issue

Hi.

How can i solve memory leakage problem on loss.backward? Actually my code is a bit complex, which makes it hard to provide whole.

The main memory burden comes from here. Exactly after the execution of loss.backward(). The memory increases for every iteration, which leads to OOM.

loss = bce_extended(logits, y).sum()
with backpack(BatchGrad()):
  if real_sample:
      loss.backward(inputs = list(model.parameters()))

I also tried with disable(): which prevent the memory leak problem. However, it cannot be implemented with with backpack(BatchGrad()): when i want to get the per-sample gradient.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
f-dangelcommented, Aug 28, 2022

Hi,

just wanted to bring it up because I saw there is a .backward(..., create_graph=True) in your code: There’s a memory leak when using full_backward_hooks with create_graph=True in PyTorch (#82528). You could try installing PyTorch with the fix (#82788) to see if that’s causing the memory leak.

1reaction
f-dangelcommented, Sep 1, 2022

Sadly I don’t have any data yet how BackPACK compares to functorch in terms of runtime, e.g. for computing individual gradients.

But I think you will be able to port your existing code to functorch with relatively few changes to try if it works fast enough for your needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PyTorch memory leak on loss.backward on both gpu as well ...
First of all, I couldn't find you using .to(device) or .to(cuda) on your data. it should be in your training loop where you...
Read more >
Memory leak under "full backward hook" with ... - GitHub
This minimal bug-reproducing example illustrates a memory leak in ... memory leakage problem when loss.backward() f-dangel/backpack#258.
Read more >
Loss.backward() cause CPU memory leak - PyTorch Forums
when I run cifar10_tutorial.py in Deep Learning with PyTorch: A 60 minutes Blitz, I find memory leak in loss.backward().
Read more >
PyTorch 101, Part 4: Memory Management and Using Multiple ...
How to diagnose and analyse memory issues should they arise. ... loss function loss.mean().backward() # Average GPU-losses + backward pass optimizer.step().
Read more >
pytorch-memlab - PyPI
A lab to do simple and accurate memory experiments on pytorch. ... the CUDA tensors into CPU memory for courtesy, and of course...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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