-
Notifications
You must be signed in to change notification settings - Fork 25
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
torch
support indexing with negative step
#144
Comments
It would have to be via a wrapper function, since we don't wrap the tensor objects. Does torch have a function that reverses a tensor? Maybe it should be done by manipulating the strides? |
It doesn't look like it. One of the most recent requests for this feature is pytorch/pytorch#59786, and it links to one of the very old requests, pytorch/pytorch#229. Looks like it's just not implemented. |
Hmm. If you try to do this with strides, you get an error: >>> a = torch.arange(10)
>>> torch.as_strided(a, a.shape, (-8,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: as_strided: Negative strides are not supported at the moment, got strides: [-8] So I think torch just fundamentally doesn't support reversed views right now. The best we can do is a helper to translate a slice into a transformation with What generality of slices do you need support for? Steps less than -1? Start and stop? Slices in multidimensional indices? |
For the time being I've used |
This reminds me of how we've discussed getting around the fact that JAX can't mutate arrays - we've discussed a function for mutating elements at specified indices if possible and copying otherwise (maybe with the JAX ISTM we might want a similar thing here, because it might not be OK to copy if the user is expecting a view. Another possibility is just re-raising, explaining why |
array-api-compat generally isn't the place to implement new APIs that aren't in the standard (see https://data-apis.org/array-api-compat/#scope). However, something that could be in scope for array-api-compat is helper functionality to workaround how different libraries handle copies vs. views. I don't know what that would look like exactly, but if you have any proposals of things that could help I'm open to hearing them. We should open a new issue to discuss this. |
At any rate, this issue makes me realize that a function that converts a slice into a strides and offset could be a generally useful thing. I might implement it in ndindex at some point Quansight-Labs/ndindex#180. |
The array API standard seems to support negative step.
But
array-api-compat.torch
tensors do not:Adding support for negative
step
would be appreciated! (In the meantime, I can useflip
.) Thanks for considering it.The text was updated successfully, but these errors were encountered: