I’m trying to build a custom C++ extension using the aten api.
Does aten library provide a simple way to manually delete/free a tensor? Please take a look at the example below:
// create a tensor
torch::Tensor tensor = torch::randn({3,4,5});
// manually delete this tensor
delete tensor; // something like this
The target is to free the memory of some large tensors in a function before the function ends, in order to save the total memory usage and avoid ‘CUDA out of memory’.
When a Tensor (or all Tensors referring to a memory block (a Storage)) goes out of scope, the memory goes back to the cache PyTorch keeps. You can free the memory from the cache using
#include <c10/cuda/CUDACachingAllocator.h>
and then calling
c10::cuda::CUDACachingAllocator::emptyCache();
(of course, you could try using torch:: instead of c10:: and see if it is automatically imported somewhere).
Hi Thomas,
Thanks a lot for you reply. I guess I might have not stated the problem clearly. My target is to free the memory of some large tensors manually in a function before these tensors are out of the function’s scope, in order to save the total memory usage and avoid “CUDA out of memory” error.
I have edited the problem description. Can you take a look again? Thanks!