-
Notifications
You must be signed in to change notification settings - Fork 5
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
TST/BUG: run all tests on all backends; fix backend-specific bugs #88
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
""" | ||
Testing utilities. | ||
|
||
Note that this is private API; don't expect it to be stable. | ||
""" | ||
|
||
from ._compat import ( | ||
array_namespace, | ||
is_cupy_namespace, | ||
is_pydata_sparse_namespace, | ||
is_torch_namespace, | ||
) | ||
from ._typing import Array, ModuleType | ||
|
||
__all__ = ["xp_assert_close", "xp_assert_equal"] | ||
|
||
|
||
def _check_ns_shape_dtype( | ||
actual: Array, desired: Array | ||
) -> ModuleType: # numpydoc ignore=RT03 | ||
""" | ||
Assert that namespace, shape and dtype of the two arrays match. | ||
|
||
Parameters | ||
---------- | ||
actual : Array | ||
The array produced by the tested function. | ||
desired : Array | ||
The expected array (typically hardcoded). | ||
|
||
Returns | ||
------- | ||
Arrays namespace. | ||
""" | ||
actual_xp = array_namespace(actual) # Raises on scalars and lists | ||
desired_xp = array_namespace(desired) | ||
|
||
msg = f"namespaces do not match: {actual_xp} != f{desired_xp}" | ||
assert actual_xp == desired_xp, msg | ||
|
||
msg = f"shapes do not match: {actual.shape} != f{desired.shape}" | ||
assert actual.shape == desired.shape, msg | ||
|
||
msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}" | ||
assert actual.dtype == desired.dtype, msg | ||
|
||
return desired_xp | ||
|
||
|
||
def xp_assert_equal(actual: Array, desired: Array, err_msg: str = "") -> None: | ||
""" | ||
Array-API compatible version of `np.testing.assert_array_equal`. | ||
|
||
Parameters | ||
---------- | ||
actual : Array | ||
The array produced by the tested function. | ||
desired : Array | ||
The expected array (typically hardcoded). | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
if is_cupy_namespace(xp): | ||
xp.testing.assert_array_equal(actual, desired, err_msg=err_msg) | ||
elif is_torch_namespace(xp): | ||
# PyTorch recommends using `rtol=0, atol=0` like this | ||
# to test for exact equality | ||
xp.testing.assert_close( | ||
actual, | ||
desired, | ||
rtol=0, | ||
atol=0, | ||
equal_nan=True, | ||
check_dtype=False, | ||
msg=err_msg or None, | ||
) | ||
else: | ||
import numpy as np # pylint: disable=import-outside-toplevel | ||
|
||
if is_pydata_sparse_namespace(xp): | ||
actual = actual.todense() | ||
desired = desired.todense() | ||
|
||
# JAX uses `np.testing` | ||
np.testing.assert_array_equal(actual, desired, err_msg=err_msg) | ||
|
||
|
||
def xp_assert_close( | ||
actual: Array, | ||
desired: Array, | ||
*, | ||
rtol: float | None = None, | ||
atol: float = 0, | ||
err_msg: str = "", | ||
) -> None: | ||
""" | ||
Array-API compatible version of `np.testing.assert_allclose`. | ||
|
||
Parameters | ||
---------- | ||
actual : Array | ||
The array produced by the tested function. | ||
desired : Array | ||
The expected array (typically hardcoded). | ||
rtol : float, optional | ||
Relative tolerance. Default: dtype-dependent. | ||
atol : float, optional | ||
Absolute tolerance. Default: 0. | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
floating = xp.isdtype(actual.dtype, ("real floating", "complex floating")) | ||
if rtol is None and floating: | ||
# multiplier of 4 is used as for `np.float64` this puts the default `rtol` | ||
# roughly half way between sqrt(eps) and the default for | ||
# `numpy.testing.assert_allclose`, 1e-7 | ||
rtol = xp.finfo(actual.dtype).eps ** 0.5 * 4 | ||
elif rtol is None: | ||
rtol = 1e-7 | ||
|
||
if is_cupy_namespace(xp): | ||
xp.testing.assert_allclose( | ||
actual, desired, rtol=rtol, atol=atol, err_msg=err_msg | ||
) | ||
elif is_torch_namespace(xp): | ||
xp.testing.assert_close( | ||
actual, desired, rtol=rtol, atol=atol, equal_nan=True, msg=err_msg or None | ||
) | ||
else: | ||
import numpy as np # pylint: disable=import-outside-toplevel | ||
|
||
if is_pydata_sparse_namespace(xp): | ||
actual = actual.to_dense() | ||
desired = desired.to_dense() | ||
|
||
# JAX uses `np.testing` | ||
assert isinstance(rtol, float) | ||
np.testing.assert_allclose( | ||
actual, desired, rtol=rtol, atol=atol, err_msg=err_msg | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Needed to import .conftest from the test modules.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Pytest fixtures.""" | ||
|
||
from enum import Enum | ||
from typing import cast | ||
|
||
import pytest | ||
|
||
from array_api_extra._lib._compat import array_namespace | ||
from array_api_extra._lib._compat import device as get_device | ||
from array_api_extra._lib._typing import Device, ModuleType | ||
|
||
|
||
class Library(Enum): | ||
"""All array libraries explicitly tested by array-api-extra.""" | ||
|
||
ARRAY_API_STRICT = "array_api_strict" | ||
NUMPY = "numpy" | ||
NUMPY_READONLY = "numpy_readonly" | ||
lucascolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CUPY = "cupy" | ||
TORCH = "torch" | ||
DASK_ARRAY = "dask.array" | ||
SPARSE = "sparse" | ||
lucascolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
JAX_NUMPY = "jax.numpy" | ||
|
||
def __str__(self) -> str: # type: ignore[explicit-override] # pyright: ignore[reportImplicitOverride] # numpydoc ignore=RT01 | ||
"""Pretty-print parameterized test names.""" | ||
return self.value | ||
|
||
|
||
@pytest.fixture(params=tuple(Library)) | ||
def library(request: pytest.FixtureRequest) -> Library: # numpydoc ignore=PR01,RT03 | ||
""" | ||
Parameterized fixture that iterates on all libraries. | ||
|
||
Returns | ||
------- | ||
The current Library enum. | ||
""" | ||
elem = cast(Library, request.param) | ||
|
||
for marker in request.node.iter_markers("skip_xp_backend"): | ||
skip_library = marker.kwargs.get("library") or marker.args[0] # type: ignore[no-untyped-usage] | ||
if not isinstance(skip_library, Library): | ||
msg = "argument of skip_xp_backend must be a Library enum" | ||
raise TypeError(msg) | ||
if skip_library == elem: | ||
reason = cast(str, marker.kwargs.get("reason", "skip_xp_backend")) | ||
pytest.skip(reason=reason) | ||
|
||
return elem | ||
|
||
|
||
@pytest.fixture | ||
def xp(library: Library) -> ModuleType: # numpydoc ignore=PR01,RT03 | ||
""" | ||
Parameterized fixture that iterates on all libraries. | ||
|
||
Returns | ||
------- | ||
The current array namespace. | ||
""" | ||
name = "numpy" if library == Library.NUMPY_READONLY else library.value | ||
xp = pytest.importorskip(name) | ||
if library == Library.JAX_NUMPY: | ||
import jax # type: ignore[import-not-found] # pyright: ignore[reportMissingImports] | ||
|
||
jax.config.update("jax_enable_x64", True) | ||
|
||
# Possibly wrap module with array_api_compat | ||
return array_namespace(xp.empty(0)) | ||
|
||
|
||
@pytest.fixture | ||
def device( | ||
library: Library, xp: ModuleType | ||
) -> Device: # numpydoc ignore=PR01,RT01,RT03 | ||
""" | ||
Return a valid device for the backend. | ||
|
||
Where possible, return a device that is not the default one. | ||
""" | ||
if library == Library.ARRAY_API_STRICT: | ||
d = xp.Device("device1") | ||
assert get_device(xp.empty(0)) != d | ||
return d | ||
return get_device(xp.empty(0)) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caveat: this is missing data-apis/array-api-compat#231