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.

The MAPE = torchmetrics.MeanAbsolutePercentageError().cuda() has a memory leak.

This is my first time using torchmetrics. After much debugging, I have isolated MAPE as the source of the problem. The only reason this loop is written so explicitly is to isolate exactly where the issue is.

sL1_list       = []
mae_list       = []
mse_list       = []
mape_list      = []

for e in tqdm(range(100)):
    
    torch.cuda.empty_cache()
    gc.collect()
    
    SL1  = torch.nn.SmoothL1Loss()
    MAE  = torch.nn.L1Loss()
    MSE  = torch.nn.MSELoss()
    MAPE = torchmetrics.MeanAbsolutePercentageError().cuda()
    
    loss           = None
    logits         = None
    spec           = None
    lld            = None
    temp_sL1_list  = []
    temp_mae_list  = []
    temp_mse_list  = []
    temp_mape_list = []
    
    #### TRAIN
    model.train()
    
    for i, (spec, lld, _) in enumerate(tqdm(train_dataloader)):
        
        spec, lld = spec.cuda(), lld.cuda()

        optimizer.zero_grad()
        logits = model(spec)[0][:, :396]
        loss = SL1(logits, lld)
        loss.backward()
        optimizer.step()
        
        break
    
    #### VALID
    model.eval()
        
    for i, (spec, lld, _) in enumerate(tqdm(train_dataloader)):
        
        spec, lld = spec.cuda(), lld.cuda()

        logits = model(spec)[0][:, :396]
        
        temp_sL1_list.append(SL1(logits, lld).item())
        temp_mae_list.append(MAE(logits, lld).item())
        temp_mse_list.append(MSE(logits, lld).item())
#         temp_mape_list.append(MAPE(logits, lld).item())
        
        break

Commenting out the one line for MAPE makes the memory leak go away.

If you look at gc.get_objects(), there is a ton of garbage made by MAPE that I don’t know how to get rid of.

Moving gc.collect() after the MAPE definition seems to fix the issue. We should not be required to re-instantiate the loss every epoch and garbage collect immediately after it is defined.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jhkonancommented, Aug 1, 2022

Thank you for this explanation. Everything is much clearer now — you do great work!

0reactions
justusschockcommented, Aug 1, 2022

Eval-Mode of the model does not prevent it from gradient calculation, it only changes the way certain layers work. You probably still want to wrap a with torch.no_grad around it.

The reset is required for every metric, as you can see in our quickstart tutorial here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maps and Memory Leaks in Go - Teiva Harsanyi - Medium
TL;DR: A map can always grow in memory; it never shrinks. Hence, if it leads to some memory issues, you can try different...
Read more >
Maps and Memory Leaks in Go | Hacker News
Yes, Go maps never shrink. This is good for most use cases in practice. Because in practice, map entry deletions happen seldom. And...
Read more >
Memory leak · Issue #138 · googlemaps/android-maps-compose
I am getting a leak if the map component is cleared and then the ... So in theory the map should not leak...
Read more >
PH27131: WASANNOTATIONHELPER MAP MEMORY LEAK
Outofmemory failure encounted due to WASAnnotationHelper Map memory leak. Depending on the requests, certain resources can be put into the server ...
Read more >
Maps and Memory Leaks in Go : r/golang - Reddit
In Go, a memory leak can only be having memory lying around and referenced that you no longer need. In this case, having...
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