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

Support negative indices in ndarray.__getitem__ #107688

Closed
wants to merge 5 commits into from

Conversation

In this case, we copy, but this is part of the set of divergences
described in Quansight-Labs/numpy_pytorch_interop#73.

This does not work with dynamic shapes, but it's not clear to me what
would be the best fix

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Aug 22, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/107688

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit d85d9a5 with merge base ba5eeed (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@lezcano lezcano requested a review from ezyang August 22, 2023 13:20
@lezcano lezcano added module: numpy Related to numpy support, and also numpy compatibility of our operators module: dynamo release notes: dynamo labels Aug 22, 2023
@lezcano
Copy link
Collaborator Author

lezcano commented Aug 22, 2023

@ezyang what's the best way to support this for dynamic shapes?

In this case, we copy, but this is part of the set of divergences
described in Quansight-Labs/numpy_pytorch_interop#73.

This does not work with dynamic shapes, but it's not clear to me what
would be the best fix

cc mruberry rgommers voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx chenyang78 aakhundov

[ghstack-poisoned]
In this case, we copy, but this is part of the set of divergences
described in Quansight-Labs/numpy_pytorch_interop#73.

This does not work with dynamic shapes, but it's not clear to me what
would be the best fix

cc mruberry rgommers voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx chenyang78 aakhundov

[ghstack-poisoned]
Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

The reason it doesn't work with dynamic shapes is because Python slice() is dumb and forces an __index__ call. You just need to rewrite the code to not use slice. This usually means calling some lower level PyTorch API like tensor.narrow; you can figure out what the getitem call desugared to by looking at the resulting ops in the graph.

@lezcano
Copy link
Collaborator Author

lezcano commented Aug 22, 2023

I think the issue here is that I'm just doing an unguarded "if slice.step < 0" within the function. Now, I don't know whether we should start putting guards in the decomposition... that doesn't sound right.

@ezyang
Copy link
Contributor

ezyang commented Aug 22, 2023

Oh, you have another problem which is that you can't actually call __getitem__ translation function with ndarray. For this you should probably just do the conversion manually in Dynamo.

If you think my advice is not relevant, you should post the errors.

In this case, we copy, but this is part of the set of divergences
described in Quansight-Labs/numpy_pytorch_interop#73.

This does not work with dynamic shapes, but it's not clear to me what
would be the best fix

cc mruberry rgommers voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx chenyang78 aakhundov

[ghstack-poisoned]
@lezcano
Copy link
Collaborator Author

lezcano commented Aug 22, 2023

#107689 calls our custom __getitem__. I'll post a more detailed repro tomorrow.

In this case, we copy, but this is part of the set of divergences
described in Quansight-Labs/numpy_pytorch_interop#73.

This does not work with dynamic shapes, but it's not clear to me what
would be the best fix

cc mruberry rgommers voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx chenyang78 aakhundov

[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Aug 23, 2023
This issue was discovered once we were able to trace without breaking
in #107689. Same for the next
one.

Pull Request resolved: #107710
Approved by: https://github.com/ezyang
ghstack dependencies: #107687, #107688
pytorchmergebot pushed a commit that referenced this pull request Aug 23, 2023
Gather does the same thing, but it's much better supported in the
`torch.compile` stack

Pull Request resolved: #107711
Approved by: https://github.com/ezyang
ghstack dependencies: #107687, #107688, #107710
pytorchmergebot pushed a commit that referenced this pull request Aug 23, 2023
As per title. Tests in the next PR

Pull Request resolved: #107746
Approved by: https://github.com/ezyang
ghstack dependencies: #107687, #107688, #107710, #107711
pytorchmergebot pushed a commit that referenced this pull request Aug 23, 2023
@facebook-github-bot facebook-github-bot deleted the gh/Lezcano/222/head branch August 26, 2023 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: dynamo module: numpy Related to numpy support, and also numpy compatibility of our operators open source release notes: dynamo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants