-
Notifications
You must be signed in to change notification settings - Fork 53
Allow Python integer return for count_nonzero(arr, axis=None)
#932
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
Comments
Not sure. So the proposal is to change the spec (https://data-apis.org/array-api/latest/API_specification/generated/array_api.count_nonzero.html) to make the return type a python |
@seberg I'm not sure I understand the concern. I just checked NumPy, CuPy, PyTorch and JAX - it's a 0-D array in all other libraries, and it's an array scalar for NumPy in case of a 1-D array with axis=0: >>> import numpy as np
>>> np.count_nonzero(np.arange(5))
4
>>> np.count_nonzero(np.arange(5), axis=0)
np.int64(4) The |
@rgommers the point is that Python integers tend to more useful, and it is a subtle change. Yeah, it may have been an oversight, I don't know, but I also don't mind too much if It is a relatively small change, so I don't care too much. But I am not quite convinced it is a clear improvement. E.g. that sklearn function is certainly better off to return Python floats but will change to return float64.
I am proposing to amend it to capture the current state: Returning a Python integer is OK (at least for |
Are there existing examples in the spec where the return type is underspecified between an array and python scalar like this case? |
I doubt it, but the question is whether we are comfortable with changing NumPy here, not with whether this makes sense in the array api. |
Okay. Either way, I don't think array-api-compat is the right place for this discussion. How does this sound?
Maybe the answer to (1) will be "we could change it, but we think it makes more sense for the spec to change", in which case the appropriate way forward is probably to see whether other stakeholders agree with that on the array-api repo. |
Sorry, you are right, I meant to open this issue on array-api, not array-api-compat :/ |
no worries! We can transfer the issue over. (I would still suggest trying to get a little more consensus from NumPy's side before asking others to weigh in on the array-api side) |
No Python scalars, please 🙂 This goes against our design principle, and is not compatible with device support. It forces a synchronous d2h copy to happen for no obvious gain. |
I think the proposal is not to require that python scalars are returned, but rather to allow either python scalars or a 0D-array. |
@leofang I was asking if the spec should allow Python integers, not suggest that it is better for anyone but NumPy's main namespace (mainly to not change things unnecessarily).
Well, this is hard since the usual answer to any of this is 🤷 as it is very hard to know whether this type of change is a nuisance to users or not. I.e. to push this to NumPy, I still would like agreement for: Sure, we are happy to just allow this for NumPy's sake (or accept if NumPy diverges subtly). |
But we spent a great effort in avoiding polymorphic return values. This change would reinstate polymorphism of some sort, and at least from the typing perspective it'd make type addition harder, no? I have a hard time imagining how this change helps downstream consumption. |
It doesn't help anyone from an array API perspective but subtly breaking downstream from a NumPy perspective isn't helpful either. Anyway, chances are nobody will care enough even it may well have some surprising effects downstream (probably not many). |
NumPy returns Python integers in that particular case, and I am not sure I feel it's worth changing it.
The reason is, that if a place only uses
axis=None
(the default), the result ofintp
vs. Python integer, changes promotion rules when combined with e.g. float32.Now, I don't think this will happen a lot, but for
count_nonzeros
I am not sure I don't think this isn't more user-friendly and an OK and very small inconsistency.NumPy itself will be fine (because we never only use
axis=None
),skimage
I saw one function which would returnfloat64
rather than a Python float.(Maybe one of those things where, I feel forcing
intp
return is great, but I am not sure changing NumPy main namespace for it is worthwhile.)See numpy/numpy#28615 (comment)
The text was updated successfully, but these errors were encountered: