Memory management in libtorch

I was wondering how memory allocation is working in libtorch. I think with clone one is allocating new memory, but when one is doing say

torch::Tensor y=torch::randn({4,5});
torch::Tensor x=y;

Is this setting x to y’s address or making a copy? I guess I can try some code to check this, but I was wondering in general if there is any documentation of how libtorch manages memory.

x should be a reference to y and you can verify it by manipulating x inplace and see the same changes in y.

1 Like

thank you, I tried out some example codes as well also with function calls. It seems in most cases it is passed by reference.