array-api icon indicating copy to clipboard operation
array-api copied to clipboard

DOC: explicitly state that `count_nonzero` with `axis=()` is unspecified.

Open ev-br opened this issue 7 months ago • 0 comments

The current docs [1] only state that If a tuple of integers, the number of non-zero values must be computed over multiple axes.

[1] https://data-apis.org/array-api/draft/API_specification/generated/array_api.count_nonzero.html

While one could argue that an empty tuple is not a tuple of integers, it could still be worth it to explicitly state that the behavior with axis=() is unspecificed and thus implementation-defined.

IRL, it is implementation-defined:

In [5]: a = np.arange(3*4*5).reshape(3, 4, 5)

In [6]: np.count_nonzero(a, axis=()).shape
Out[6]: (3, 4, 5)

In [7]: torch.count_nonzero(torch.as_tensor(a), dim=())
Out[7]: tensor(59)

In fact, it'd be best to add the same phrasing to all reduction-like functions which accept tuples for axis.

ev-br avatar May 09 '25 16:05 ev-br