Skip to content

Commit

Permalink
Run tests on ndonnx
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Jan 8, 2025
1 parent b87e0aa commit bf986cf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
15 changes: 5 additions & 10 deletions array_api_compat/common/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,17 +627,13 @@ def device(x: Array, /) -> Device:
to_device : Move array data to a different device.
"""
if is_numpy_array(x):
if is_numpy_array(x) or is_ndonnx_array(x):
return "cpu"
elif is_dask_array(x):
# Peek at the metadata of the jax array to determine type
try:
import numpy as np
if isinstance(x._meta, np.ndarray):
# Must be on CPU since backed by numpy
return "cpu"
except ImportError:
pass
if is_numpy_array(x._meta):
# Must be on CPU since backed by numpy
return "cpu"
return _DASK_DEVICE
elif is_jax_array(x):
# JAX has .device() as a method, but it is being deprecated so that it
Expand Down Expand Up @@ -758,7 +754,7 @@ def to_device(x: Array, device: Device, /, *, stream: Optional[Union[int, Any]]
device : Hardware device the array data resides on.
"""
if is_numpy_array(x):
if is_numpy_array(x) or is_ndonnx_array(x):
if stream is not None:
raise ValueError("The stream argument to to_device() is not supported")
if device == 'cpu':
Expand All @@ -780,7 +776,6 @@ def to_device(x: Array, device: Device, /, *, stream: Optional[Union[int, Any]]
if not hasattr(x, "__array_namespace__"):
# In JAX v0.4.31 and older, this import adds to_device method to x.
import jax.experimental.array_api # noqa: F401
return x.to_device(device, stream=stream)
elif is_pydata_sparse_array(x) and device == _device(x):
# Perform trivial check to return the same array if
# device is same instead of err-ing.
Expand Down
11 changes: 11 additions & 0 deletions docs/supported-array-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ The minimum supported Dask version is 2023.12.0.
## [Sparse](https://sparse.pydata.org/en/stable/)

Similar to JAX, `sparse` Array API support is contained directly in `sparse`.

(ndonnx-support)=
## [ndonnx](https://github.com/quantco/ndonnx)

Similar to JAX, `ndonnx` Array API support is contained directly in `ndonnx`.

(array-api-strict-support)=
## [array-api-strict](https://data-apis.org/array-api-strict/)

array-api-strict exists only to test support for the Array API, so obviously it
does not need any wrappers.
9 changes: 2 additions & 7 deletions tests/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from importlib import import_module
import sys

import pytest

wrapped_libraries = ["numpy", "cupy", "torch", "dask.array"]
all_libraries = wrapped_libraries + [
"array_api_strict", "jax.numpy", "sparse"
"array_api_strict", "jax.numpy", "ndonnx", "sparse"
]

# `sparse` added array API support as of Python 3.10.
if sys.version_info >= (3, 10):
all_libraries.append('sparse')

def import_(library, wrapper=False):
if library == 'cupy':
if library in ('cupy', 'ndonnx'):
pytest.importorskip(library)
if wrapper:
if 'jax' in library:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_array_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@

@pytest.mark.parametrize("use_compat", [True, False, None])
@pytest.mark.parametrize("api_version", [None, "2021.12", "2022.12", "2023.12"])
@pytest.mark.parametrize("library", all_libraries + ['array_api_strict'])
@pytest.mark.parametrize("library", all_libraries)
def test_array_namespace(library, api_version, use_compat):
xp = import_(library)

array = xp.asarray([1.0, 2.0, 3.0])
if use_compat is True and library in {'array_api_strict', 'jax.numpy', 'sparse'}:
if use_compat and library not in wrapped_libraries:
pytest.raises(ValueError, lambda: array_namespace(array, use_compat=use_compat))
return
if library == "ndonnx" and api_version in ("2021.12", "2022.12"):
pytest.skip("Unsupported API version")

namespace = array_api_compat.array_namespace(array, api_version=api_version, use_compat=use_compat)

if use_compat is False or use_compat is None and library not in wrapped_libraries:
Expand Down
27 changes: 22 additions & 5 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from array_api_compat import ( # noqa: F401
is_numpy_array, is_cupy_array, is_torch_array,
is_dask_array, is_jax_array, is_pydata_sparse_array,
is_ndonnx_array,
is_numpy_namespace, is_cupy_namespace, is_torch_namespace,
is_dask_namespace, is_jax_namespace, is_pydata_sparse_namespace,
is_array_api_strict_namespace,
is_array_api_strict_namespace, is_ndonnx_namespace,
)

from array_api_compat import device, is_array_api_obj, is_writeable_array, to_device
Expand All @@ -22,6 +23,7 @@
'dask.array': 'is_dask_array',
'jax.numpy': 'is_jax_array',
'sparse': 'is_pydata_sparse_array',
'ndonnx': 'is_ndonnx_array',
}

is_namespace_functions = {
Expand All @@ -32,6 +34,7 @@
'jax.numpy': 'is_jax_namespace',
'sparse': 'is_pydata_sparse_namespace',
'array_api_strict': 'is_array_api_strict_namespace',
'ndonnx': 'is_ndonnx_namespace',
}


Expand Down Expand Up @@ -135,26 +138,40 @@ def test_to_device_host(library):
@pytest.mark.parametrize("target_library", is_array_functions.keys())
@pytest.mark.parametrize("source_library", is_array_functions.keys())
def test_asarray_cross_library(source_library, target_library, request):
if source_library == "dask.array" and target_library == "torch":
def _xfail(reason: str) -> None:
# Allow rest of test to execute instead of immediately xfailing
# xref https://github.com/pandas-dev/pandas/issues/38902
request.node.add_marker(pytest.mark.xfail(reason=reason))

if source_library == "dask.array" and target_library == "torch":
# TODO: remove xfail once
# https://github.com/dask/dask/issues/8260 is resolved
request.node.add_marker(pytest.mark.xfail(reason="Bug in dask raising error on conversion"))
if source_library == "cupy" and target_library != "cupy":
_xfail(reason="Bug in dask raising error on conversion")
elif (
source_library == "ndonnx"
and target_library not in ("array_api_strict", "ndonnx", "numpy")
):
_xfail(reason="The truth value of lazy Array Array(dtype=Boolean) is unknown")
elif source_library == "ndonnx" and target_library == "numpy":
_xfail(reason="produces numpy array of ndonnx scalar arrays")
elif source_library == "jax.numpy" and target_library == "torch":
_xfail(reason="casts int to float")
elif source_library == "cupy" and target_library != "cupy":
# cupy explicitly disallows implicit conversions to CPU
pytest.skip(reason="cupy does not support implicit conversion to CPU")
elif source_library == "sparse" and target_library != "sparse":
pytest.skip(reason="`sparse` does not allow implicit densification")

src_lib = import_(source_library, wrapper=True)
tgt_lib = import_(target_library, wrapper=True)
is_tgt_type = globals()[is_array_functions[target_library]]

a = src_lib.asarray([1, 2, 3])
a = src_lib.asarray([1, 2, 3], dtype=src_lib.int32)
b = tgt_lib.asarray(a)

assert is_tgt_type(b), f"Expected {b} to be a {tgt_lib.ndarray}, but was {type(b)}"
assert b.dtype == tgt_lib.int32


@pytest.mark.parametrize("library", wrapped_libraries)
def test_asarray_copy(library):
Expand Down

0 comments on commit bf986cf

Please sign in to comment.