# Libtorch uses much more GPU memory than python?

**URL:** <https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310>\
**Category:** C++\
**Created:** [May 15, 2019, 12:14pm UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310 "2019-05-15T12:14:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riddick\_Gao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/riddick_gao/32/12107_2.png) [@Riddick\_Gao](https://discuss.pytorch.org/u/Riddick_Gao)\
**Post date:** [May 15, 2019, 12:14pm UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310/1 "2019-05-15T12:14:00Z")

</div>

Recently, I am using libtorch to do the prediction. I have saved the original model to .pt model using torch::jit::trace( ) API.

```auto
    traced_script_module = torch.jit.trace(base_model, example)
    traced_script_module.save(ouput_path)

```

Then I loaded it in C++ using torch::jit::load() API and do the model.eval().

```auto
    module = torch::jit::load(model_path);
    module->eval()

```

But I found that libtorch occupied much more GPU memory to do the forward( ) with same image size than original module in python.

So I reloaded the .pt model in python. I found it have the same performance as the original module.

It seems like that C++ uses much more memory than python with same model and same image size.

How can I deal with it? Does any one has the same problem? thanks.

pytorch version: 1.1.0  
python version: 3.7.0  
OS: windows 10  
IDE: VS2015  
cuda10 + cudnn10.0 v7.5.1.10

---

<div class="post-metadata">

**Author:** ![tholzmann](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/c57346/32.png) [@tholzmann](https://discuss.pytorch.org/u/tholzmann)\
**Post date:** [September 27, 2019, 1:20pm UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310/2 "2019-09-27T13:20:23Z")

</div>

Hi,

I am observing the same behaviour. Did you find out the reason for this?

Best,  
Thomas

---

<div class="post-metadata">

**Author:** ![yf225](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yf225/32/3418_2.png) [@yf225](https://discuss.pytorch.org/u/yf225)\
**Post date:** [September 27, 2019, 2:49pm UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310/3 "2019-09-27T14:49:10Z")

</div>

@Riddick_Gao @tholzmann Could you shared the code that can reproduce this problem? And does it only happen on Windows?

cc. @peterjc123

---

<div class="post-metadata">

**Author:** ![tholzmann](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/c57346/32.png) [@tholzmann](https://discuss.pytorch.org/u/tholzmann)\
**Post date:** [October 1, 2019, 6:26am UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310/4 "2019-10-01T06:26:41Z")

</div>

On my side it happens on Windows, I’m currently not able to test it on Linux.

A snipped of my code:

```auto
std::ifstream in( fileName, std::ios_base::binary);
m_TorchDevice = std::unique_ptr<torch::Device>( new torch::Device( torch::DeviceType::CPU) );
m_Model = torch::jit::load( in, *m_TorchDevice ); 
m_Model.eval();
torch::Tensor resultTensor = m_Model.forward( inputsVec ).toTensor(); 

```

Note that I have observed the same behaviour with GPU and CPU.

Best,  
Thomas

---

<div class="post-metadata">

**Author:** ![andy2002ua](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/andy2002ua/32/78114_2.png) [@andy2002ua](https://discuss.pytorch.org/u/andy2002ua)\
**Post date:** [September 2, 2025, 1:11pm UTC](https://discuss.pytorch.org/t/libtorch-uses-much-more-gpu-memory-than-python/45310/5 "2025-09-02T13:11:18Z")

</div>

If anyone is struggling with this problem, try putting  
`torch::NoGradGuard no_grad;`  
right before the `forward` call.
