|
| 1 | +""" |
| 2 | +Testing utilities. |
| 3 | +
|
| 4 | +Note that this is private API; don't expect it to be stable. |
| 5 | +""" |
| 6 | + |
| 7 | +from types import ModuleType |
| 8 | + |
| 9 | +from ._utils._compat import ( |
| 10 | + array_namespace, |
| 11 | + is_cupy_namespace, |
| 12 | + is_pydata_sparse_namespace, |
| 13 | + is_torch_namespace, |
| 14 | +) |
| 15 | +from ._utils._typing import Array |
| 16 | + |
| 17 | +__all__ = ["xp_assert_close", "xp_assert_equal"] |
| 18 | + |
| 19 | + |
| 20 | +def _check_ns_shape_dtype( |
| 21 | + actual: Array, desired: Array |
| 22 | +) -> ModuleType: # numpydoc ignore=RT03 |
| 23 | + """ |
| 24 | + Assert that namespace, shape and dtype of the two arrays match. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + actual : Array |
| 29 | + The array produced by the tested function. |
| 30 | + desired : Array |
| 31 | + The expected array (typically hardcoded). |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + Arrays namespace. |
| 36 | + """ |
| 37 | + actual_xp = array_namespace(actual) # Raises on scalars and lists |
| 38 | + desired_xp = array_namespace(desired) |
| 39 | + |
| 40 | + msg = f"namespaces do not match: {actual_xp} != f{desired_xp}" |
| 41 | + assert actual_xp == desired_xp, msg |
| 42 | + |
| 43 | + msg = f"shapes do not match: {actual.shape} != f{desired.shape}" |
| 44 | + assert actual.shape == desired.shape, msg |
| 45 | + |
| 46 | + msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}" |
| 47 | + assert actual.dtype == desired.dtype, msg |
| 48 | + |
| 49 | + return desired_xp |
| 50 | + |
| 51 | + |
| 52 | +def xp_assert_equal(actual: Array, desired: Array, err_msg: str = "") -> None: |
| 53 | + """ |
| 54 | + Array-API compatible version of `np.testing.assert_array_equal`. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + actual : Array |
| 59 | + The array produced by the tested function. |
| 60 | + desired : Array |
| 61 | + The expected array (typically hardcoded). |
| 62 | + err_msg : str, optional |
| 63 | + Error message to display on failure. |
| 64 | + """ |
| 65 | + xp = _check_ns_shape_dtype(actual, desired) |
| 66 | + |
| 67 | + if is_cupy_namespace(xp): |
| 68 | + xp.testing.assert_array_equal(actual, desired, err_msg=err_msg) |
| 69 | + elif is_torch_namespace(xp): |
| 70 | + # PyTorch recommends using `rtol=0, atol=0` like this |
| 71 | + # to test for exact equality |
| 72 | + xp.testing.assert_close( |
| 73 | + actual, |
| 74 | + desired, |
| 75 | + rtol=0, |
| 76 | + atol=0, |
| 77 | + equal_nan=True, |
| 78 | + check_dtype=False, |
| 79 | + msg=err_msg or None, |
| 80 | + ) |
| 81 | + else: |
| 82 | + import numpy as np # pylint: disable=import-outside-toplevel |
| 83 | + |
| 84 | + if is_pydata_sparse_namespace(xp): |
| 85 | + actual = actual.todense() |
| 86 | + desired = desired.todense() |
| 87 | + |
| 88 | + # JAX uses `np.testing` |
| 89 | + np.testing.assert_array_equal(actual, desired, err_msg=err_msg) |
| 90 | + |
| 91 | + |
| 92 | +def xp_assert_close( |
| 93 | + actual: Array, |
| 94 | + desired: Array, |
| 95 | + *, |
| 96 | + rtol: float | None = None, |
| 97 | + atol: float = 0, |
| 98 | + err_msg: str = "", |
| 99 | +) -> None: |
| 100 | + """ |
| 101 | + Array-API compatible version of `np.testing.assert_allclose`. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + actual : Array |
| 106 | + The array produced by the tested function. |
| 107 | + desired : Array |
| 108 | + The expected array (typically hardcoded). |
| 109 | + rtol : float, optional |
| 110 | + Relative tolerance. Default: dtype-dependent. |
| 111 | + atol : float, optional |
| 112 | + Absolute tolerance. Default: 0. |
| 113 | + err_msg : str, optional |
| 114 | + Error message to display on failure. |
| 115 | + """ |
| 116 | + xp = _check_ns_shape_dtype(actual, desired) |
| 117 | + |
| 118 | + floating = xp.isdtype(actual.dtype, ("real floating", "complex floating")) |
| 119 | + if rtol is None and floating: |
| 120 | + # multiplier of 4 is used as for `np.float64` this puts the default `rtol` |
| 121 | + # roughly half way between sqrt(eps) and the default for |
| 122 | + # `numpy.testing.assert_allclose`, 1e-7 |
| 123 | + rtol = xp.finfo(actual.dtype).eps ** 0.5 * 4 |
| 124 | + elif rtol is None: |
| 125 | + rtol = 1e-7 |
| 126 | + |
| 127 | + if is_cupy_namespace(xp): |
| 128 | + xp.testing.assert_allclose( |
| 129 | + actual, desired, rtol=rtol, atol=atol, err_msg=err_msg |
| 130 | + ) |
| 131 | + elif is_torch_namespace(xp): |
| 132 | + xp.testing.assert_close( |
| 133 | + actual, desired, rtol=rtol, atol=atol, equal_nan=True, msg=err_msg or None |
| 134 | + ) |
| 135 | + else: |
| 136 | + import numpy as np # pylint: disable=import-outside-toplevel |
| 137 | + |
| 138 | + if is_pydata_sparse_namespace(xp): |
| 139 | + actual = actual.to_dense() |
| 140 | + desired = desired.to_dense() |
| 141 | + |
| 142 | + # JAX uses `np.testing` |
| 143 | + assert isinstance(rtol, float) |
| 144 | + np.testing.assert_allclose( |
| 145 | + actual, desired, rtol=rtol, atol=atol, err_msg=err_msg |
| 146 | + ) |
0 commit comments