Skip to content

Fix the torch.take() wrapper to make axis optional for ndim = 1 #47

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

Merged
merged 1 commit into from
Jul 19, 2023

Conversation

asmeurer
Copy link
Member

Closes #34

if axis is None:
if x.ndim != 1:
raise ValueError("axis must be specified when ndim > 1")
axis = 0
Copy link

Choose a reason for hiding this comment

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

Or you can dispatch to torch.take, either should be alright.

Copy link
Member Author

Choose a reason for hiding this comment

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

torch.take doesn't support an axis argument.

Copy link

Choose a reason for hiding this comment

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

exactly :D torch.take() == numpy.take(axis=None)

Copy link

Choose a reason for hiding this comment

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

it's a bit more general, as it sees the whole tensor as a 0dim tensor and indexes into it, so it also works for ndim > 1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. The flattening behavior is also there in NumPy, but I think we wanted to avoid that with the array API take. I'm not sure to what degree we should try to avoid that for the compat library, though (c.f. #34 (comment))

Copy link
Member

@betatim betatim left a comment

Choose a reason for hiding this comment

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

Looks like a sensible way to implement this "sometimes optional" behaviour

@asmeurer asmeurer merged commit 546fa3d into data-apis:main Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement torch.take with axis argument
3 participants