Skip to content

ENH: size() to return None on dask instead of nan #231

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

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions array_api_compat/common/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,19 +788,24 @@ def to_device(x: Array, device: Device, /, *, stream: Optional[Union[int, Any]]
return x.to_device(device, stream=stream)


def size(x):
def size(x: Array) -> int | None:
"""
Return the total number of elements of x.

This is equivalent to `x.size` according to the `standard
<https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.size.html>`__.

This helper is included because PyTorch defines `size` in an
:external+torch:meth:`incompatible way <torch.Tensor.size>`.

It also fixes dask.array's behaviour which returns nan for unknown sizes, whereas
the standard requires None.
"""
# Lazy API compliant arrays, such as ndonnx, can contain None in their shape
if None in x.shape:
return None
return math.prod(x.shape)
out = math.prod(x.shape)
# dask.array.Array.shape can contain NaN
return None if math.isnan(out) else out


def is_writeable_array(x) -> bool:
Expand Down
27 changes: 25 additions & 2 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
is_dask_namespace, is_jax_namespace, is_pydata_sparse_namespace,
)

from array_api_compat import device, is_array_api_obj, is_writeable_array, to_device

from array_api_compat import (
device, is_array_api_obj, is_writeable_array, size, to_device
)
from ._helpers import import_, wrapped_libraries, all_libraries

import pytest
Expand Down Expand Up @@ -92,6 +93,28 @@ def test_is_writeable_array_numpy():
assert not is_writeable_array(x)


@pytest.mark.parametrize("library", all_libraries)
def test_size(library):
xp = import_(library)
x = xp.asarray([1, 2, 3])
assert size(x) == 3


@pytest.mark.parametrize("library", all_libraries)
Copy link
Contributor Author

@crusaderky crusaderky Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually test ndonnx's None case until #232 gets merged.

def test_size_none(library):
if library == "sparse":
pytest.skip("No arange(); no indexing by sparse arrays")

xp = import_(library)
x = xp.arange(10)
x = x[x < 5]

# dask.array now has shape=(nan, ) and size=nan
# ndonnx now has shape=(None, ) and size=None
# Eager libraries have shape=(5, ) and size=5
assert size(x) in (None, 5)


@pytest.mark.parametrize("library", all_libraries)
def test_device(library):
xp = import_(library, wrapper=True)
Expand Down
Loading