You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is this issue still up to date? It seems that the latest release of array-api-compat (1.3) makes xp.take accept an axis parameter but also makes it mandatory:
@ogrisel Only when the input array is one-dimensional is the axis kwarg optional. Otherwise, take is equivalent to integer indexing on a multi-dimensional array, in which one would explicitly indicate the axis to index.
Yes, this is unfortunate, as, by making it optional for one-dimensional arrays, we don't have a great option for the signature, as setting the default value to None makes it seem as if the argument is optional for multi-dimensional arrays. My feeling is it would be better if the axis argument is required for all arrays in order to resolve this ambiguity.
The discussion in data-apis/array-api#416 explicitly states that axis is optional, and that was supported by multiple people. So I'd consider this a bug in that PR and in the spec, and the correct resolution seems to me to be to add the missing = None to the signature. The docs are clear enough, so there isn't much of an ambiguity.
The ambiguity stems from the type signature: the signature is not able to encode optionality and "requiredness" at the same time (or based on the input array shape).
We made the axis kwarg optional as a convenience; however, at the time, I had omitted using None as the default to satisfy the general case in which the kwarg is required for >1D. Obviously, this doesn't work for the scenario where axis is optional; hence, my statement concerning the unfortunate aspect of the signature.
Yes, I understand what's going on - it seems clear to me that the spec has a bug that has to be resolved one way or the other, and the written agreement is for optionality for 1-D arrays, hence changing the signature to axis : int = None. It's the path of least resistance, and avoids the surprise that @ogrisel expressed above. We already considered scikit-learn's needs in data-apis/array-api#416 (comment); it's mostly 1-D arrays.
Yes, this is an issue with the current spec. With the current signature, take(x, indices, *, axis), axis is always required, due to the way the keyword-only syntax works.
The problem is that people (such as myself) copy the signatures from the spec directly. So if the Python signature doesn't allow axis to be optional, it won't be, because that's the exact signature I used.
The alternative signature, take(x, indices, *, axis=None) is easy to make work in the function logic:
deftake(x, indices, *, axis=None):
ifx.ndim>1andaxisisNone:
raiseValueError("axis must be provided when ndim > 1")
...
This is somewhat similar to arange where it's impossible to encode the "true" signature into the Python signature so we have to do what is as close as possible.
I'm unclear if I need to do anything for the compat library for numpy/cupy. The axis argument is not optional for ndim > 1 in numpy (it flattens). This generally the sort of thing I would expect the strict numpy.array_api implementation to catch, whereas in the compat library, we allow things that aren't strictly disallowed and do things like pass additional keyword arguments through.
Torch does need to be fixed though because it just wraps torch.index_select which requires the axis argument.
Activity
[-]Implement torch.take with `axis` argument.[/-][+]Implement torch.take with `axis` argument[/+]asmeurer commentedon Mar 29, 2023
Is there an upstream pytorch issue for adding this to pytorch itself? It seems to be impossible to search for in the GitHub issue tracker. @lezcano
lezcano commentedon Mar 29, 2023
https://pytorch.org/docs/stable/generated/torch.index_select.html
ogrisel commentedon Jul 3, 2023
Is this issue still up to date? It seems that the latest release of array-api-compat (1.3) makes
xp.takeaccept anaxisparameter but also makes it mandatory:The fact that it's mandatory was a bit surprising to me, but apparently the signature is in line with the specification:
kgryte commentedon Jul 3, 2023
@ogrisel Only when the input array is one-dimensional is the
axiskwarg optional. Otherwise,takeis equivalent to integer indexing on a multi-dimensional array, in which one would explicitly indicate theaxisto index.asmeurer commentedon Jul 5, 2023
This looks like a bug in the spec. The text says passing it is optional, but the Python signature does not allow for that.
kgryte commentedon Jul 5, 2023
Yes, this is unfortunate, as, by making it optional for one-dimensional arrays, we don't have a great option for the signature, as setting the default value to
Nonemakes it seem as if the argument is optional for multi-dimensional arrays. My feeling is it would be better if theaxisargument is required for all arrays in order to resolve this ambiguity.rgommers commentedon Jul 5, 2023
The discussion in data-apis/array-api#416 explicitly states that
axisis optional, and that was supported by multiple people. So I'd consider this a bug in that PR and in the spec, and the correct resolution seems to me to be to add the missing= Noneto the signature. The docs are clear enough, so there isn't much of an ambiguity.kgryte commentedon Jul 5, 2023
The ambiguity stems from the type signature: the signature is not able to encode optionality and "requiredness" at the same time (or based on the input array shape).
We made the
axiskwarg optional as a convenience; however, at the time, I had omitted usingNoneas the default to satisfy the general case in which the kwarg is required for >1D. Obviously, this doesn't work for the scenario whereaxisis optional; hence, my statement concerning the unfortunate aspect of the signature.rgommers commentedon Jul 5, 2023
Yes, I understand what's going on - it seems clear to me that the spec has a bug that has to be resolved one way or the other, and the written agreement is for optionality for 1-D arrays, hence changing the signature to
axis : int = None. It's the path of least resistance, and avoids the surprise that @ogrisel expressed above. We already considered scikit-learn's needs in data-apis/array-api#416 (comment); it's mostly 1-D arrays.asmeurer commentedon Jul 5, 2023
Yes, this is an issue with the current spec. With the current signature,
take(x, indices, *, axis),axisis always required, due to the way the keyword-only syntax works.The problem is that people (such as myself) copy the signatures from the spec directly. So if the Python signature doesn't allow
axisto be optional, it won't be, because that's the exact signature I used.The alternative signature,
take(x, indices, *, axis=None)is easy to make work in the function logic:This is somewhat similar to
arangewhere it's impossible to encode the "true" signature into the Python signature so we have to do what is as close as possible.Fix optional kwarg in function signature
kgryte commentedon Jul 5, 2023
I opened a PR correcting the signature for
take: data-apis/array-api#644.Fix optional keyword argument in `take` signature (#644)
asmeurer commentedon Jul 15, 2023
I updated numpy.array_api here numpy/numpy#24187
I'm unclear if I need to do anything for the compat library for numpy/cupy. The axis argument is not optional for ndim > 1 in numpy (it flattens). This generally the sort of thing I would expect the strict numpy.array_api implementation to catch, whereas in the compat library, we allow things that aren't strictly disallowed and do things like pass additional keyword arguments through.
Torch does need to be fixed though because it just wraps
torch.index_selectwhich requires theaxisargument.Fix the torch.take() wrapper to make axis optional for ndim = 1
rgommers commentedon Jul 16, 2023
Probably not, I think it works as is.