Skip to content
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

Array API tests fixes #636

Merged
merged 3 commits into from
Dec 5, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/array-api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ jobs:
# edge case failures (https://github.com/cubed-dev/cubed/issues/420)
array_api_tests/test_linalg.py::test_tensordot
array_api_tests/test_linalg.py::test_vecdot
# (getitem with negative step size is not implemented)
Copy link

Choose a reason for hiding this comment

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

:) we actually rewrite such queries in Xarray for Zarr

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh interesting! How do you do that?

Copy link

@dcherian dcherian Dec 4, 2024

Choose a reason for hiding this comment

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

https://github.com/pydata/xarray/blob/99ee8c6ca54057a9b994d7685f36236f2d5a69d9/xarray/core/indexing.py#L1084 and friends. We rewrite the query to normal slice with +ve stride, then reverse in-memory after read :)

This is one of Xarray's magic tricks that very few people know about. We guarantee consistent indexing API over any array for the most part.

In pydata/xarray#8667, I suggested reusing this machinery for cubed.

EDIT: actually my comment is here: pydata/xarray#8834 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Very cool. Would be great to reuse that. BTW Cubed flip is implemented in a similar way, but it's only step=-1 of course.

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #637 to track

array_api_tests/test_array_object.py::test_getitem

# not implemented
array_api_tests/test_array_object.py::test_setitem
Expand Down
2 changes: 2 additions & 0 deletions cubed/array_api/creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def full(
dtype = nxp.int64
elif isinstance(fill_value, float):
dtype = nxp.float64
elif isinstance(fill_value, complex):
dtype = nxp.complex128
else:
raise TypeError("Invalid input to full")
chunksize = to_chunksize(normalize_chunks(chunks, shape=shape, dtype=dtype))
Expand Down
10 changes: 6 additions & 4 deletions cubed/array_api/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def broadcast_to(x, /, shape, *, chunks=None):
):
raise ValueError(f"cannot broadcast shape {x.shape} to shape {shape}")

# TODO: fix case where shape has a dimension of size zero

if chunks is None:
# New dimensions and broadcast dimensions have chunk size 1
# This behaviour differs from dask where it is the full dimension size
xchunks = normalize_chunks(x.chunks, x.shape, dtype=x.dtype)
chunks = tuple((1,) * s for s in shape[:ndim_new]) + tuple(
bd if old > 1 else ((1,) * new if new > 0 else (0,))

def chunklen(shapelen):
return (1,) * shapelen if shapelen > 0 else (0,)

chunks = tuple(chunklen(s) for s in shape[:ndim_new]) + tuple(
bd if old > 1 else chunklen(new)
for bd, old, new in zip(xchunks, x.shape, shape[ndim_new:])
)
else:
Expand Down
3 changes: 2 additions & 1 deletion cubed/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ def test_broadcast_arrays(executor):
@pytest.mark.parametrize(
"shape, chunks, new_shape, new_chunks, new_chunks_expected",
[
# ((5, 1, 6), (3, 1, 3), (5, 0, 6), None, ((3, 2), (0,), (3, 3))), # fails
((), (), (0,), None, ((0,),)),
((5, 1, 6), (3, 1, 3), (5, 0, 6), None, ((3, 2), (0,), (3, 3))),
((5, 1, 6), (3, 1, 3), (5, 4, 6), None, ((3, 2), (1, 1, 1, 1), (3, 3))),
((5, 1, 6), (3, 1, 3), (2, 5, 1, 6), None, ((1, 1), (3, 2), (1,), (3, 3))),
((5, 1, 6), (3, 1, 3), (5, 3, 6), (3, 3, 3), ((3, 2), (3,), (3, 3))),
Expand Down
Loading