I've been struggling to understand the differences between .clone()
, .detach()
and copy.deepcopy
when using Pytorch. In particular with Pytorch tensors.
I tried writing all my question about their differences and uses cases and became overwhelmed quickly and realized that perhaps have the 4 main properties of Pytorch tensors would clarify much better which one to use that going through every small question. The 4 main properties I realized one needs keep track are:
- if one has a new pointer/reference to a tensor
- if one has a new tensor object instance (and thus most likely this new instance has it's own meta-data like
require_grads
, shape,is_leaf
, etc.) - if it has allocated a new memory for the tensor data (i.e. if this new tensor is a view of a different tensor)
- if it's tracking the history of operations or not (or even if it's tracking a completely new history of operations or the same old one in the case of deep copy)
According to what mined out from the Pytorch forums and the documentation this is my current distinctions for each when used on tensors:
Clone
For clone:
x_cloned = x.clone()
I believe this is how it behaves according to the main 4 properties:
- the cloned
x_cloned
has it's own python reference/pointer to the new object - it has created it's own new tensor object instance (with it's separate meta-data)
- it has allocated a new memory for
x_new
with the same data asx
- it is keeping track of the original history of operations and in addition included this
clone
operation as.grad_fn=<CloneBackward>
it seems that the main use of this as I understand is to create copies of things so that inplace_
operations are safe. In addition coupled with .detach
as .detach().clone()
(the "better" order to do it btw) it creates a completely new tensor that has been detached with the old history and thus stops gradient flow through that path.
Detach
x_detached = x.detach()
- creates a new python reference (the only one that does not is doing
x_new = x
of course). One can useid
for this one I believe - it has created it's own new tensor object instance (with it's separate meta-data)
- it has NOT allocated a new memory for
x_detached
with the same data as x - it cuts the history of the gradients and does not allow it to flow through it. I think it's right to think of it as having no history, as a brand new tensor.
I believe the only sensible use I know of is of creating new copies with it's own memory when coupled with .clone()
as .detach().clone()
. Otherwise, I am not sure what the use it. Since it points to the original data, doing in place ops might be potentially dangerous (since it changes the old data but the change to the old data is NOT known by autograd in the earlier computation graph).
copy.deepcopy
x_deepcopy = copy.deepcopy(x)
- if one has a new pointer/reference to a tensor
- it creates a new tensor instance with it's own meta-data (all of the meta-data should point to deep copies, so new objects if it's implemented as one would expect I hope).
- it has it's own memory allocated for the tensor data
- If it truly is a deep copy, I would expect a deep copy of the history. So it should do a deep replication of the history. Though this seems really expensive but at least semantically consistent with what deep copy should be.
I don't really see a use case for this. I assume anyone trying to use this really meant 1) .detach().clone()
or just 2) .clone()
by itself, depending if one wants to stop gradient flows to the earlier graph with 1 or if they want just to replicate the data with a new memory 2).
So this is the best way I have to understand the differences as of now rather than ask all the different scenarios that one might use them.
So is this right? Does anyone see any major flaw that needs to be correct?
My own worry is about the semantics I gave to deep copy and wonder if it's correct wrt the deep copying the history.
I think a list of common use cases for each would be wonderful.
Resources
these are all the resources I've read and participated to arrive at the conclusions in this question:
- Migration guide to 0.4.0 https://pytorch.org/blog/pytorch-0_4_0-migration-guide/
- Confusion about using clone: https://discuss.pytorch.org/t/confusion-about-using-clone/39673/3
- Clone and detach in v0.4.0: https://discuss.pytorch.org/t/clone-and-detach-in-v0-4-0/16861/2
- Docs for clone:
- Docs for detach (search for the word detach in your browser there is no direct link):
- Difference between detach().clone() and clone().detach(): https://discuss.pytorch.org/t/difference-between-detach-clone-and-clone-detach/34173
- Why am I able to change the value of a tensor without the computation graph knowing about it in Pytorch with detach? Why am I able to change the value of a tensor without the computation graph knowing about it in Pytorch with detach?
- What is the difference between detach, clone and deepcopy in Pytorch tensors in detail? What is the difference between detach, clone and deepcopy in Pytorch tensors in detail?
- Copy.deepcopy() vs clone() https://discuss.pytorch.org/t/copy-deepcopy-vs-clone/55022/10