Skip to content
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

Add torch.movedim #41480

Closed

Conversation

kshitij12345
Copy link
Collaborator

@kshitij12345 kshitij12345 commented Jul 15, 2020

#38349 #36048

TODO:

  • Tests
  • Docs

@kshitij12345 kshitij12345 marked this pull request as draft July 15, 2020 16:58
@dr-ci
Copy link

dr-ci bot commented Jul 15, 2020

💊 CI failures summary and remediations

As of commit 3ad19aa (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 64 times.

@ezyang ezyang requested a review from zou3519 July 16, 2020 02:14
@ezyang ezyang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 16, 2020
@ezyang
Copy link
Contributor

ezyang commented Jul 16, 2020

@zou3519 I assigned this to you because I remember seeing you implement a baby version of moveaxis for vmap. LMK if this assignment is wrong.

@zou3519
Copy link
Contributor

zou3519 commented Jul 16, 2020

Yeah, I'm happy to review this. @kshitij12345 -- can you let me know when this is ready for review, or if you want me to take a look at it now? (github says the PR is current a draft)

@kshitij12345
Copy link
Collaborator Author

@zou3519

I'll ping you once I feel the PR is complete from my side.
Till then feel free to have a look and share any feedback.

Thank You!

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skimmed through this pretty fast to put up a few initial comments, but ping me when this is ready @kshitij12345 and I'll provide a full review

aten/src/ATen/native/TensorTransformations.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorTransformations.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorTransformations.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorTransformations.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/native_functions.yaml Outdated Show resolved Hide resolved
@mruberry mruberry added the module: numpy Related to numpy support, and also numpy compatibility of our operators label Jul 16, 2020
* move code to TensorShape
* update error msg
* update test to check for updated error msg
@kshitij12345
Copy link
Collaborator Author

@zou3519
Would be great if you can review.
Have added tests. And addressed all other comments except for whether the name should be movedim or moveaxis. I feel it will be better to just alias the name to movedim later. Do let me know how you feel about that.
Thank You!

@mruberry
Would be great if you can have a look as well. Thanks!

test/test_autograd.py Outdated Show resolved Hide resolved
Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't read the test code or the documentation yet but the implementation looks correct to me

aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Show resolved Hide resolved
aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
@kshitij12345 kshitij12345 marked this pull request as ready for review July 17, 2020 03:48
r"""
movedim(input, source, destination) -> Tensor

Move `source` dim/s of the :attr:`input` to position determined by `destination`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

"Moves the dimension(s) of :attr:input at the position(s) in :attr:source to the position(s) in :attr:destination.
Other dimensions of :attr:input that are not explicitly moved remain in their original order and appear at the positions not specified in :attr:destination."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I wasn' t sure how to phrase it better.

* pending src -> source, dst -> destination
* update doc
@kshitij12345
Copy link
Collaborator Author

@mruberry @zou3519

Have made the required changes.

Please review :)

Thanks!

@mruberry
Copy link
Collaborator

@mruberry @zou3519

Have made the required changes.

Please review :)

Thanks!

Thanks @kshitij12345, docs look good to me now! I'll let @zou3519 have final say.

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Comment on lines +1757 to +1758
auto source_iter = std::remove(source_dims.begin(), source_dims.end(), -1);
auto destination_iter = std::remove(destination_dims.begin(), destination_dims.end(), -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An internal linter pointed these out. source_iter and destination_iter are never actually used, can we remove them and just do:

std::remove(source_dims.begin(), source_dims.end(), -1);
std::remove(destination_dims.begin(), destination_dims.end(), -1);

?

Copy link
Contributor

@zou3519 zou3519 Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I thought about it a little more. It would be nice to use source_iter and destination_iter to assert that source_dims and destination_dims have the correct number of elements. So something like

TORCH_INTERNAL_ASSERT(std::distance(source_dims.begin(), source_iter)  == rest_dim);
TORCH_INTERNAL_ASSERT(std::distance(destination_dims.begin(), destination_iter)  == rest_dim);

But either way, we should either use source_iter / destination_iter or delete them

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.
Will use them as suggested in second comment.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@zou3519
Copy link
Contributor

zou3519 commented Jul 23, 2020

Thanks for the contribution, @kshitij12345! There were many times in the past when I wanted to use this feature and I am glad we finally added it to PyTorch

@facebook-github-bot
Copy link
Contributor

@zou3519 merged this pull request in 2666571.

@Trinkle23897 Trinkle23897 mentioned this pull request Jul 24, 2020
4 tasks
@kshitij12345 kshitij12345 deleted the numpy/develop/moveaxis branch July 25, 2020 06:38
facebook-github-bot pushed a commit that referenced this pull request Sep 11, 2020
Summary:
Noticed this bug in `torch.movedim` (#41480). [`std::unique`](https://en.cppreference.com/w/cpp/algorithm/unique) only guarantees uniqueness for _sorted_ inputs. The current check lets through non-unique values when they aren't adjacent to each other in the list, e.g. `(0, 1, 0)` wouldn't raise an exception and instead the algorithm fails later with an internal assert.

Pull Request resolved: #44307

Reviewed By: mrshenli

Differential Revision: D23598311

Pulled By: zou3519

fbshipit-source-id: fd6cc43877c42bb243cfa85341c564b6c758a1bf
@ngimel ngimel mentioned this pull request Sep 11, 2020
bitfort pushed a commit that referenced this pull request Sep 11, 2020
Summary:
Noticed this bug in `torch.movedim` (#41480). [`std::unique`](https://en.cppreference.com/w/cpp/algorithm/unique) only guarantees uniqueness for _sorted_ inputs. The current check lets through non-unique values when they aren't adjacent to each other in the list, e.g. `(0, 1, 0)` wouldn't raise an exception and instead the algorithm fails later with an internal assert.

Pull Request resolved: #44307

Reviewed By: mrshenli

Differential Revision: D23598311

Pulled By: zou3519

fbshipit-source-id: fd6cc43877c42bb243cfa85341c564b6c758a1bf
xuzhao9 pushed a commit that referenced this pull request Sep 18, 2020
Summary:
Noticed this bug in `torch.movedim` (#41480). [`std::unique`](https://en.cppreference.com/w/cpp/algorithm/unique) only guarantees uniqueness for _sorted_ inputs. The current check lets through non-unique values when they aren't adjacent to each other in the list, e.g. `(0, 1, 0)` wouldn't raise an exception and instead the algorithm fails later with an internal assert.

Pull Request resolved: #44307

Reviewed By: mrshenli

Differential Revision: D23598311

Pulled By: zou3519

fbshipit-source-id: fd6cc43877c42bb243cfa85341c564b6c758a1bf
@boeddeker
Copy link
Contributor

I have a question, does movedim do the same as np.moveaxis?
If yes, is there a reason, why it is renamed in torch?
It is annoying to remember the names of torch and numpy.

If I remember correct, many torch functions started to support axis as an alternative to dim.
And since many users are familiar with numpy moveaxis would not confuse users.

@kshitij12345
Copy link
Collaborator Author

@boeddeker Yes, movedim has the same functionality as np.moveaxis.

It was named movedim to be more consistent with torch's scheme of using dim.

The plan was to add an alias for it.
#41480 (comment)
#41480 (comment)

cc: @mruberry

@mruberry
Copy link
Collaborator

@kshitij12345 is correct and we would accept a PR adding the alias. It just hasn't been done yet.

facebook-github-bot pushed a commit that referenced this pull request Dec 3, 2020
Summary:
Reference: #38349 #36048 #41480 (comment)

Pull Request resolved: #48581

Reviewed By: bdhirsh

Differential Revision: D25276307

Pulled By: mruberry

fbshipit-source-id: 3e3e4df1343c5ce5b71457badc43f08c419ec5c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: numpy Related to numpy support, and also numpy compatibility of our operators open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants