# GPU memory usage increases by ~90% after torch.load

**URL:** <https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213>\
**Category:** Uncategorized\
**Created:** [October 29, 2017, 10:04am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213 "2017-10-29T10:04:47Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![austin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/austin/32/1915_2.png) [@austin](https://discuss.pytorch.org/u/austin)\
**Post date:** [October 29, 2017, 10:04am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/1 "2017-10-29T10:04:48Z")

</div>

I’m currently training a faster-rcnn model. Normal training consumes ~1900MiB of gpu memory. When I try to resume training from a checkpoint with torch.load, the model takes over 3000MiB. With identical settings specified in a config file.

I believe these are the relevant bits of code:

```
voc_dataset = PascalVOC(DATA_PATH, transform, LIMIT)
voc_loader = DataLoader(voc_dataset, shuffle=SHUFFLE, pin_memory=True)
basecnn = BaseCNN(ARCHITECTURE, REQUIRES_GRAD)
rpn = RegionProposalNetwork(batch_size=RPN_BATCH_SIZE)
detector = Detector(batch_size=CLF_BATCH_SIZE)

frcnn = FasterRCNN(basecnn, rpn, detector)
optimizer = Adam(filter(lambda p: p.requires_grad, frcnn.parameters()), lr=LEARNING_RATE)

if RESUME_PATH:
    experiment = torch.load(RESUME_PATH)
    frcnn.load_state_dict(experiment['model_state'])
    optimizer.load_state_dict(experiment['optimizer_state'])
    frcnn.basecnn.finetune()

frcnn.cuda()

```

Full training code is here: [https://github.com/A-Jacobson/faster\_rcnn/blob/master/train.py](https://github.com/A-Jacobson/faster_rcnn/blob/master/train.py)

---

<div class="post-metadata">

**Author:** ![roytseng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roytseng/32/4233_2.png) [@roytseng](https://discuss.pytorch.org/u/roytseng)\
**Post date:** [April 5, 2018, 8:46pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/2 "2018-04-05T20:46:11Z")

</div>

Yes, I have experienced the same situation before. This is quiet annoying ! I will bump on OOM if I would like to resume training from a checkpoint.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 5, 2018, 9:55pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/3 "2018-04-05T21:55:17Z")

</div>

Have you tried to save and load the `state_dict`? I’ve seen some issues where e.g. the training resumed when using the second approach explained [here](http://pytorch.org/docs/master/notes/serialization.html).

---

<div class="post-metadata">

**Author:** ![roytseng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roytseng/32/4233_2.png) [@roytseng](https://discuss.pytorch.org/u/roytseng)\
**Post date:** [April 6, 2018, 1:16am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/4 "2018-04-06T01:16:39Z")

</div>

Yes. I always follow the best practice to save and load the `state_dict`.  
I found a related issue [here](https://github.com/facebookresearch/fairseq-py/issues/51)  
It says `torch.cuda.empty_cache()`might help, but in my case I still have OOM.  
By the way, I’m using pytorch 0.3.1

---

<div class="post-metadata">

**Author:** ![roytseng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roytseng/32/4233_2.png) [@roytseng](https://discuss.pytorch.org/u/roytseng)\
**Post date:** [April 6, 2018, 1:30am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/5 "2018-04-06T01:30:52Z")

</div>

Good news ! I seems able to avoid OOM by the following loading strategy:

```python
checkpoint = torch.load(ckpt_file, map_location=lambda storage, loc: storage)
net.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer']) # pytorch 0.3.1 has a bug on this, it's fix in master
del checkpoint # dereference seems crucial
torch.cuda.empty_cache()

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 6, 2018, 5:57am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/6 "2018-04-06T05:57:51Z")

</div>

That’s good!  
Btw, shouldn’t the checkpoint be in CPU memory when loading with the `map_location` parameter?

---

<div class="post-metadata">

**Author:** ![roytseng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roytseng/32/4233_2.png) [@roytseng](https://discuss.pytorch.org/u/roytseng)\
**Post date:** [April 6, 2018, 8:00am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/7 "2018-04-06T08:00:39Z")

</div>

Yes. With `map_location=lambda storage, loc: storage`, tensors in checkpoint are in CPU memory at first.  
However, I guess `load_state_dict` may cast tensors to the corresponding device of model parameters internally, and the references to the casted tensors are still held by `checkpoint`. I don’t really trace it down.

---

<div class="post-metadata">

**Author:** ![austin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/austin/32/1915_2.png) [@austin](https://discuss.pytorch.org/u/austin)\
**Post date:** [April 6, 2018, 8:57am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/8 "2018-04-06T08:57:24Z")

</div>

I’ll have to check this on my end. Embarrassingly, I ended up getting a new gpu and the temporary spike didn’t matter all that much anymore.

---

<div class="post-metadata">

**Author:** ![zjoe](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/b9e5f3/32.png) [@zjoe](https://discuss.pytorch.org/u/zjoe)\
**Post date:** [May 30, 2018, 3:20pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/9 "2018-05-30T15:20:01Z")

</div>

Hi roytseng, I face the same problem when using 0.3.1, and it still blocking me. I have tried del+cuda.empty\_cache() but it doesn’t work in my case. I notice your comment “pytorch 0.3.1 has a bug on this, it’s fix in master”, could you explain more on this? Links to the original issues or commits would be really helpful(i have checked commits about optimizers but do not find it). Thanks in advance!

---

<div class="post-metadata">

**Author:** ![roytseng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roytseng/32/4233_2.png) [@roytseng](https://discuss.pytorch.org/u/roytseng)\
**Post date:** [June 5, 2018, 5:51am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/10 "2018-06-05T05:51:05Z")

</div>

Hi zjoe,

I not really remember if the bug in load\_state\_dict of optimizer is related to the memory usage increment or not (I guess it’s not). However, it’s sure that this bug has been fixed in pytorch0.4.

---

<div class="post-metadata">

**Author:** ![Daniel\_Dagnino](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/daniel_dagnino/32/10216_2.png) [@Daniel\_Dagnino](https://discuss.pytorch.org/u/Daniel_Dagnino)\
**Post date:** [August 17, 2018, 2:25pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/11 "2018-08-17T14:25:15Z")

</div>

Hi all,  
Have you been able to fix the problem?  
I am experiencing the same problem and I am using pytorch 0.5, so it does not seem that the new versions solve the problem. I am using:

```auto
if load:
    checkpoint = torch.load('./model.ckpt')
    startEpoch = checkpoint['StartEpoch']
    model.load_state_dict(checkpoint['state_dict'])
    del checkpoint
    torch.cuda.empty_cache()

```

but the problem persists. The problem is located in the point

```auto
loss.backward

```

however, the model is the same; and I am not loading anything else than the model and the epoch number. I need to reduce the batch size which is very annoying.  
Thanks,  
Dani.

---

<div class="post-metadata">

**Author:** ![kangkang\_li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kangkang_li/32/10275_2.png) [@kangkang\_li](https://discuss.pytorch.org/u/kangkang_li)\
**Post date:** [July 3, 2019, 6:11am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/12 "2019-07-03T06:11:39Z")

</div>

have you solved it? I meet this too.

---

<div class="post-metadata">

**Author:** ![1hada](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/e5b9ba/32.png) [@1hada](https://discuss.pytorch.org/u/1hada)\
**Post date:** [September 2, 2020, 9:33pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/13 "2020-09-02T21:33:40Z")

</div>

```auto
del checkpoint # dereference seems crucial

```

Worked for me !  
It seems to have saved around +500mb in my case!  
I was able to save but not load in pytorch. The checkpoint definitely took up valuable valuable gpu memory.

```auto
torch.cuda.empty_cache() 

```

Helped clear around 300mb!

Thank you @roytseng !

---

<div class="post-metadata">

**Author:** ![realiti](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@realiti](https://discuss.pytorch.org/u/realiti)\
**Post date:** [May 13, 2021, 1:37pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/14 "2021-05-13T13:37:09Z")

</div>

This thread is really old, but I have to report this can still be an issue with pytorch 1.8.1. I had to lower model’s batch size from 64 to 40 to just be able to resume from a checkpoint before for example. That’s quite a performance hit actually. This method works beautifully and is still relevant, cheers.

Edit: I can also confirm map\_location=cpu is also works.

---

<div class="post-metadata">

**Author:** ![Bled\_Clement](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bled_clement/32/21076_2.png) [@Bled\_Clement](https://discuss.pytorch.org/u/Bled_Clement)\
**Post date:** [April 5, 2022, 11:44am UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/15 "2022-04-05T11:44:38Z")

</div>

Still an issue. Ran into this today on Pytorch 1.10.

Deleting checkpoint and emptying cache doesn’t work.

Loading model to cpu first works as pointed out by @realiti.

---

<div class="post-metadata">

**Author:** ![DariusOneTrick](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dariusonetrick/32/67091_2.png) [@DariusOneTrick](https://discuss.pytorch.org/u/DariusOneTrick)\
**Post date:** [January 20, 2024, 3:00pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/16 "2024-01-20T15:00:30Z")

</div>

Still an issue. My model takes 31000MiB, and I got a 32GB gpu. If I start training from scratch, stuff works; If I resume from a checkpoint, I get an OOM.

---

<div class="post-metadata">

**Author:** ![KCLam](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kclam/32/67213_2.png) [@KCLam](https://discuss.pytorch.org/u/KCLam)\
**Post date:** [August 1, 2024, 8:08pm UTC](https://discuss.pytorch.org/t/gpu-memory-usage-increases-by-90-after-torch-load/9213/17 "2024-08-01T20:08:47Z")

</div>

This solution helped me.

```auto
state = torch.load(f, map_location=torch.device('cpu'))

```

from [Pytorch GPU memory increase after load operation - Stack Overflow](https://stackoverflow.com/questions/58060885/pytorch-gpu-memory-increase-after-load-operation)
