Why will GPU memory consumption decrease when using torch.cuda.memory_allocated?

wangyuyy: <p>I defined a torch.autograd.Funcion module and link it with a cuda module. Strange thing is, when I added logger to report the current memory consumption of the GPU, the memory consumption largely decreased. The only difference of the two experiments is whether to report the logger, which is as follows:</p>
<pre data-code-wrap="python"><code class="lang-python">def forward(...):
....
        logger.info(f"memory before rasterization: {torch.cuda.memory_allocated()/1024**2:,.02f} MB")
        # Invoke C++/CUDA rasterizer
        num_rendered, color, depth_map, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)
        logger.info(f"memory after rasterization: {torch.cuda.memory_allocated()/1024**2:,.02f} MB")

...
def backward(...):
....
        logger.info(f"memory before backward: {torch.cuda.memory_allocated()/1024**2:,.02f} MB")

        # Compute gradients for relevant tensors by invoking backward method
        grad_means2D, grad_colors_precomp, grad_depths_precomp, grad_opacities, grad_means3D, grad_cov3Ds_precomp, grad_sh, grad_scales, grad_rotations = _C.rasterize_gaussians_backward(*args)

        logger.info(f"memory after backward: {torch.cuda.memory_allocated()/1024**2:,.02f} MB")
</code></pre>
<p>if I report the logger, the GPU comsumption is<br>
else if I don’t report the logger, the GPU comsumption is<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5ca5838210e103fc864537d3f58576076baa5a9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e5ca5838210e103fc864537d3f58576076baa5a9" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5ca5838210e103fc864537d3f58576076baa5a9_2_690x160.jpeg" alt="image" data-base62-sha1="wMORrJjOXv0XuObWmQxAhzO1s13" width="690" height="160" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5ca5838210e103fc864537d3f58576076baa5a9_2_690x160.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5ca5838210e103fc864537d3f58576076baa5a9_2_1035x240.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5ca5838210e103fc864537d3f58576076baa5a9_2_1380x320.jpeg 2x" data-dominant-color="0C0F0E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×447 97.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I swear the only difference between the two codes are above. It’s really strange because torch.cuda.memory_allocated should not influence the comsumption of the GPU. The peak memory info of the progress bar is allocated at the same place which is still not changed between two exps.</p>
<p>updated exp results:<br>
I did more experiments.</p>
<ol>
<li>I deleted the loguru.logger report, only to define an variable to query the memory consumption like:<br>
memory_before_rasterization = torch.cuda.memory_allocated(). The result remains the same. Low consumption when defined, while high consumption when not defined.</li>
<li>I deleted the part of the backward, while remain the forward part. The result remain the same.</li>
<li>I deleted the part of the forward, only to find that the GPU consumption increased just like showed before.</li>
<li>The GPU consumption is at low range only when the function torch.cuda.memory_allocated is both called before and after the _C.rasterize_gaussians(*args).</li>
</ol>
<p>I wonder whether somewhat the query of the GPU comsumption will influence the memory comsumption?</p>