In torch, we can declare an empty torch.Tensor() in a module’s init function, then in the forward function, just resize this torch tensor. I think in this way we can save some time on mallocing new memories.
In pytorch, do we even need to do this? If yes, then should it be a tensor or an autograd.Variable?
Yeah I know, but in my case, my tensor t is like a helper tensor that is independent to input. So if I create a new one at each forward pass, then I also need to run function assign_values(t) each time, which will take some time.
On the other hand, if I am able to re-use gpu memory, then I only need to call assig_value(t) once
Anyway, I figured out how to do this in a way similar to torch. Just create a buffer tensor t in init(), then in forward pass, wrap it with Variable(t)
PyTorch comes with built-in caching allocator for CUDA, so memory allocations are automatically re-used internally without any extra work on your part.