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’.
I understand there might be some tricks like:
// option 1
torch::Tensor * tensor = torch::Tensor new(torch::randn({3,4,5}));
delete tensor;

and
// option 2
torch::Tensor tensor = torch::randn({3,4,5});
// free memory usage by setting it smaller
tensor.resize_(at::IntArrayRef{0});

But is there a simpler and more straightforward way to achieve this?
Note that I’ve already searched the forums and read this related question How to free GPU memory of at::Tensor in ATen, C++? 26 but still got no answer.
Another question:
for an operation like
torch::Tensor a = a.clone();

will tensor a be automatically freed? I’m not sure whether this will cause memory leak.