New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[doc] Add note about torch.flip
returning new tensor and not view.
#50041
Changes from 1 commit
f8b19e6
fc83ea0
7f02b2d
ad47b8b
f6eb4cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8332,6 +8332,11 @@ def merge_dicts(*dicts): | |
|
||
Reverse the order of a n-D tensor along given axis in dims. | ||
|
||
.. note:: | ||
Unlike `np.flip`, `torch.flip` returns a new Tensor. | ||
Due to this behavior, `torch.flip` has time complexity | ||
of `O(N)` while `np.flip` is `O(1)`. | ||
|
||
Args: | ||
{input} | ||
dims (a list or tuple): axis to flip on | ||
|
@@ -8365,6 +8370,11 @@ def merge_dicts(*dicts): | |
Note: | ||
Equivalent to input[:,::-1]. Requires the array to be at least 2-D. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm now noticing these notes. I think both the fliplr and flipud suggestions for advanced in indexing will actually throw errors in PyTorch? Would you check? If so, seems like we should remove these notes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct indeed. >>> import torch
>>> t = torch.ones((3,2))
>>> t[:, ::-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: step must be greater than zero
>>> t[:, ::-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: step must be greater than zero
>>> t[::-1, ...]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: step must be greater than zero
>>> t[::1, ...]
tensor([[1., 1.],
[1., 1.],
[1., 1.]]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating it to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second look should we make it,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be more appropriate. Sure. |
||
|
||
.. note:: | ||
Unlike `np.fliplr`, `torch.fliplr` returns a new Tensor. | ||
Due to this behavior, `torch.fliplr` has time complexity | ||
of `O(N)` while `np.fliplr` is `O(1)`. | ||
|
||
Args: | ||
input (Tensor): Must be at least 2-dimensional. | ||
|
||
|
@@ -8391,6 +8401,11 @@ def merge_dicts(*dicts): | |
Note: | ||
Equivalent to input[::-1,...]. Requires the array to be at least 1-D. | ||
|
||
.. note:: | ||
Unlike `np.flipud`, `torch.flipud` returns a new Tensor. | ||
Due to this behavior, `torch.flipud` has time complexity | ||
of `O(N)` while `np.flipud` is `O(1)`. | ||
|
||
Args: | ||
input (Tensor): Must be at least 1-dimensional. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's tweak and simplify this just a bit.
"`torch.flip` makes a copy of :attr:`input`'s data. This is different from `np.flip`, which returns a view."
There are a couple reasons I want to suggest this change:
Then this can be a template to substitute flip{ud, lr} into. Does that sound good to you, @kshitij12345?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first two points make sense.
I think it would be good to mention the time-complexity as it is a major gotcha #16424 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK that's a good point.
Big O notation seems like a lot of machinery to bring to this documentation, though. Could we sneak in the performance discussion with something like (I'm also going to add an explicit reference to NumPy since "np" in the above is not defined):
"This is different from NumPy's
np.flip
, which quickly returns a view."If that's too subtle, what about avoiding big O by doing:
"This is different from NumPy's
np.flip
, which returns a view. Since copying a tensor's data is more work than viewing that data,torch.flip
is expected to be slower thannp.flip
."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
flip
would be used heavily on CV side in augmentation pipelines, so it might be better to mention about time-complexity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
I would prefer,
"This is different from NumPy's np.flip, which returns a view. Since copying a tensor's data is more work than viewing that data, torch.flip is expected to be slower than np.flip."
as
Seems a bit ambiguous as to what is quick and what isn't
So change would be
Just a side note, numpy.flip documentation does mention that it is a constant-time operation. (Maybe we can say torch.flip is linear-time w.r.t number of elements in tensor)
https://numpy.org/doc/stable/reference/generated/numpy.flip.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
".. note::
torch.flip
makes a copy of :attr:input
's data. This is different from NumPy's np.flip, which returns a view in constant time. Since copying a tensor's data is more work than viewing that data, torch.flip is expected to be slower than np.flip."Merging your suggestion about constant time + adding back the first sentence. How does this look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. This feels better. Thanks!