About torch.cuda.empty_cache()

Hi,

This function should not be used by the end-user except in very edge cases.
Pytorch does not release the memory back to the OS when you remove Tensors on the GPU, it keeps it in a pool so that next allocations can be done much faster. As you saw, without this, GPU code is much slower.
You don’t need to call this function explicitly as, even though you don’t see it in nvidia-smi, the memory is available to create more Tensors and so will not cause out of memory problems.

21 Likes