-
Notifications
You must be signed in to change notification settings - Fork 46
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
RFC: disallow 0-D inputs in cumulative_sum
#797
Comments
It would be useful if anyone is aware of any prior discussions about this in NumPy, PyTorch, or other libraries. It doesn't seem to have been mentioned at numpy/numpy#6044, but I didn't look any further in the NumPy tracker. |
Another consideration: >>> np.diff(np.asarray(0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aaronmeurer/miniconda3/envs/array-apis/lib/python3.11/site-packages/numpy/lib/function_base.py", line 1418, in diff
raise ValueError("diff requires input that is at least one dimensional")
ValueError: diff requires input that is at least one dimensional
>>> torch.diff(torch.asarray(0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: diff expects input to be at least one-dimensional |
Personally, I'd be more inclined to require that the input array should have at least one dimension. If a 0D array is considered the array equivalent of a scalar, then performing a cumulative sum over a scalar doesn't make sense to me. |
I think the first question should be what the default is when you have N-dimensions. I would be very surprised if the scalar rull doesn't fall out of that, since the results should only come from For example the NumPy example:
seems clarly incorrect (from todays point of view, 20 years ago we were more forgiving/guessing) and |
The standard requires I'm also somewhat inclining towards disallowing this, or at least leaving it undefined. |
Right, and this is already means it is unspecified. 0-D is N-D and only 1-D is specified.
While I think the default of "ravelling" isn't useful or even very sensible for accumulations, I am not sure I see a big enough gain in prescribing |
I seee that the confusion here really came from I think it would be completely fine to specify that as not allowed, I don't even see it necessary to specify it as not allowed. It is behavior that clearly should be deprecated even if I might not jump at actually doing it. |
I agree that disallowing 0-D inputs makes sense here. Note that in JAX our current implementation raises a |
cumulative_sum
I've opened #851 to close out this issue. |
The standard is not clear what should happen in
cumulative_sum
for 0-D inputs https://data-apis.org/array-api/latest/API_specification/generated/array_api.cumulative_sum.html#cumulative-sumNote that NumPy and PyTorch have different conventions here:
torch.cumsum
unconditionally requires thedim
argument, whereasnp.cumsum
defaults to computing over a flattened array ifaxis=None
. The standard requiresaxis
if the dimensionality is greater than 1. However,axis=0
doesn't really make sense for a 0-D array. NumPy also allows specifyingaxis=0
and gives the same result:Furthermore, there is ambiguity here on what should happen for a 0-D input when
include_initial=True
. The standard says:If the result should be 0-D, then clearly
include_initial
must do nothing, since there is no way to increase the number of elements in the result.This doesn't seem to have been discussed in the original pull request #653 or issue #597, and I don't recall it being brought up at the consortium meetings.
My suggested behavior would be
cumulative_sum
on a 0-D inputx
should be a 0-D output which is the same asx
(i.e., it would work just likesum(x)
). This matches the behavior thatcumulative_sum
always returns an output with the same dimensionality as the input.include_initial
flag would do nothing when the input is 0-D. One can read the existing text as already supporting this behavior, since "the axis along which to compute the cumulative sum" is vacuous.axis
argument must beNone
when the input is 0-D or else the result is an error. This matches the usual "axis must be in the range [-ndim, ndim)" condition, which is not currently spelled out this way forcumulative_sum
but is for other functions in the standard.Alternatively, we could leave the behavior unspecified. To me the above makes sense, but this does break with current
cumsum
conventions. On the other hand, since the name is different, it's not a big deal for libraries to change behavior betweencumsum
andcumulative_sum
(this is at least the approach that NumPy has taken with some of the existing renames with breaking changes).The text was updated successfully, but these errors were encountered: