Skip to content
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 dpnp/tests/third_party/cupy/linalg_tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def test_einsum_unary_views(self, xp, dtype):
def test_einsum_unary_dtype(self, xp, dtype_a, dtype_out):
if not numpy.can_cast(dtype_a, dtype_out):
pytest.skip()
if cupy.issubdtype(dtype_a, cupy.unsignedinteger):
pytest.skip("dpctl-2055")
a = testing.shaped_arange(self.shape_a, xp, dtype_a)
return xp.einsum(self.subscripts, a, dtype=dtype_out)

Expand Down
128 changes: 101 additions & 27 deletions dpnp/tests/third_party/cupy/testing/_array.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import warnings

import numpy
import numpy.testing

import dpnp as cupy
Expand All @@ -6,7 +9,15 @@


def assert_allclose(
actual, desired, rtol=1e-7, atol=0, err_msg="", verbose=True
actual,
desired,
rtol=1e-7,
atol=0,
equal_nan=True,
err_msg="",
verbose=True,
*,
strict=False,
):
"""Raises an AssertionError if objects are not equal up to desired tolerance.

Expand All @@ -22,17 +33,42 @@ def assert_allclose(
.. seealso:: :func:`numpy.testing.assert_allclose`

"""
numpy.testing.assert_allclose(
cupy.asnumpy(actual),
cupy.asnumpy(desired),
rtol=rtol,
atol=atol,
err_msg=err_msg,
verbose=verbose,
)
if numpy.lib.NumpyVersion(numpy.__version__) >= "2.0.0":
numpy.testing.assert_allclose(
cupy.asnumpy(actual),
cupy.asnumpy(desired),
rtol=rtol,
atol=atol,
equal_nan=equal_nan,
err_msg=err_msg,
verbose=verbose,
strict=strict,
)
else:
if strict:
warnings.warn(
"`dpnp.tests.third_party.cupy.testing.assert_allclose` does not support `strict` "
"option with NumPy v1.",
RuntimeWarning,
)
numpy.testing.assert_allclose(
cupy.asnumpy(actual),
cupy.asnumpy(desired),
rtol=rtol,
atol=atol,
equal_nan=equal_nan,
err_msg=err_msg,
verbose=verbose,
)


def assert_array_almost_equal(x, y, decimal=6, err_msg="", verbose=True):
def assert_array_almost_equal(
actual,
desired,
decimal=6,
err_msg="",
verbose=True,
):
"""Raises an AssertionError if objects are not equal up to desired precision.

Args:
Expand All @@ -46,8 +82,8 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg="", verbose=True):
.. seealso:: :func:`numpy.testing.assert_array_almost_equal`
"""
numpy.testing.assert_array_almost_equal(
cupy.asnumpy(x),
cupy.asnumpy(y),
cupy.asnumpy(actual),
cupy.asnumpy(desired),
decimal=decimal,
err_msg=err_msg,
verbose=verbose,
Expand Down Expand Up @@ -87,7 +123,13 @@ def assert_array_max_ulp(a, b, maxulp=1, dtype=None):


def assert_array_equal(
x, y, err_msg="", verbose=True, strides_check=False, **kwargs
actual,
desired,
err_msg="",
verbose=True,
*,
strict=False,
strides_check=False,
):
"""Raises an AssertionError if two array_like objects are not equal.

Expand All @@ -105,22 +147,36 @@ def assert_array_equal(

.. seealso:: :func:`numpy.testing.assert_array_equal`
"""
numpy.testing.assert_array_equal(
cupy.asnumpy(x),
cupy.asnumpy(y),
err_msg=err_msg,
verbose=verbose,
**kwargs,
)
if numpy.lib.NumpyVersion(numpy.__version__) >= "1.24.0":
numpy.testing.assert_array_equal(
cupy.asnumpy(actual),
cupy.asnumpy(desired),
err_msg=err_msg,
verbose=verbose,
strict=strict,
)
else:
if strict:
warnings.warn(
"`dpnp.tests.third_party.cupy.testing.assert_allclose` does not support `strict` "
"option with NumPy v1.",
RuntimeWarning,
)
numpy.testing.assert_array_equal(
cupy.asnumpy(actual),
cupy.asnumpy(desired),
err_msg=err_msg,
verbose=verbose,
)

if strides_check:
if x.strides != y.strides:
if actual.strides != desired.strides:
msg = ["Strides are not equal:"]
if err_msg:
msg = [msg[0] + " " + err_msg]
if verbose:
msg.append(" x: {}".format(x.strides))
msg.append(" y: {}".format(y.strides))
msg.append(" x: {}".format(actual.strides))
msg.append(" y: {}".format(desired.strides))
raise AssertionError("\n".join(msg))


Expand Down Expand Up @@ -163,7 +219,7 @@ def assert_array_list_equal(xlist, ylist, err_msg="", verbose=True):
)


def assert_array_less(x, y, err_msg="", verbose=True):
def assert_array_less(x, y, err_msg="", verbose=True, *, strict=False):
"""Raises an AssertionError if array_like objects are not ordered by less than.

Args:
Expand All @@ -175,6 +231,24 @@ def assert_array_less(x, y, err_msg="", verbose=True):

.. seealso:: :func:`numpy.testing.assert_array_less`
"""
numpy.testing.assert_array_less(
cupy.asnumpy(x), cupy.asnumpy(y), err_msg=err_msg, verbose=verbose
)
if numpy.lib.NumpyVersion(numpy.__version__) >= "2.0.0":
numpy.testing.assert_array_less(
cupy.asnumpy(x),
cupy.asnumpy(y),
err_msg=err_msg,
verbose=verbose,
strict=strict,
)
else:
if strict:
warnings.warn(
"`dpnp.tests.third_party.cupy.testing.assert_allclose` does not support `strict` "
"option with NumPy v1.",
RuntimeWarning,
)
numpy.testing.assert_array_less(
cupy.asnumpy(x),
cupy.asnumpy(y),
err_msg=err_msg,
verbose=verbose,
)
49 changes: 21 additions & 28 deletions dpnp/tests/third_party/cupy/testing/_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _make_positive_masks(impl, args, kw, name, sp_name, scipy_name):
assert error is None
if not isinstance(result, (tuple, list)):
result = (result,)
return [r >= 0 for r in result]
return [cupy.asnumpy(r) >= 0 for r in result]


def _contains_signed_and_unsigned(kw):
Expand Down Expand Up @@ -307,12 +307,9 @@ def decorator(impl):
@_wraps_partial_xp(impl, name, sp_name, scipy_name)
def test_func(*args, **kw):
# Run cupy and numpy
(
cupy_result,
cupy_error,
numpy_result,
numpy_error,
) = _call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
(cupy_result, cupy_error, numpy_result, numpy_error) = (
_call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
)
assert cupy_result is not None or cupy_error is not None
assert numpy_result is not None or numpy_error is not None

Expand Down Expand Up @@ -411,8 +408,10 @@ def test_func(*args, **kw):
if cupy_r.shape == ():
skip = (mask == 0).all()
else:
cupy_r = cupy_r[mask]
numpy_r = numpy_r[mask.asnumpy()]
# mask is numpy.ndarray here which is not supported now
# TODO remove asarray() once dpctl-2053 is addressed
cupy_r = cupy_r[cupy.asarray(mask)].asnumpy()
numpy_r = numpy_r[mask]

if not skip:
check_func(cupy_r, numpy_r)
Expand Down Expand Up @@ -446,7 +445,7 @@ def _convert_output_to_ndarray(c_out, n_out, sp_name, check_sparse_format):
assert scipy.sparse.issparse(n_out)
if check_sparse_format:
assert c_out.format == n_out.format
return c_out.A, n_out.A
return c_out.toarray(), n_out.toarray()
if isinstance(c_out, cupy.ndarray) and isinstance(
n_out, (numpy.ndarray, numpy.generic)
):
Expand All @@ -455,7 +454,6 @@ def _convert_output_to_ndarray(c_out, n_out, sp_name, check_sparse_format):
if (
hasattr(cupy, "poly1d")
and isinstance(c_out, cupy.poly1d)
and hasattr(numpy, "poly1d")
and isinstance(n_out, numpy.poly1d)
):
# poly1d output case.
Expand All @@ -464,9 +462,6 @@ def _convert_output_to_ndarray(c_out, n_out, sp_name, check_sparse_format):
if isinstance(c_out, numpy.generic) and isinstance(n_out, numpy.generic):
# numpy scalar output case.
return c_out, n_out
if isinstance(c_out, numpy.ndarray) and isinstance(n_out, numpy.ndarray):
# fallback on numpy output case.
return c_out, n_out
if numpy.isscalar(c_out) and numpy.isscalar(n_out):
# python scalar output case.
return cupy.array(c_out), numpy.array(n_out)
Expand Down Expand Up @@ -594,7 +589,9 @@ def numpy_cupy_allclose(

def check_func(c, n):
rtol1, atol1 = _resolve_tolerance(type_check, c, rtol, atol)
_array.assert_allclose(c, n, rtol1, atol1, err_msg, verbose)
_array.assert_allclose(
c, n, rtol1, atol1, err_msg=err_msg, verbose=verbose
)

return _make_decorator(
check_func,
Expand Down Expand Up @@ -795,7 +792,9 @@ def numpy_cupy_array_equal(
"""

def check_func(x, y):
_array.assert_array_equal(x, y, err_msg, verbose, strides_check)
_array.assert_array_equal(
x, y, err_msg, verbose, strides_check=strides_check
)

return _make_decorator(
check_func, name, type_check, False, accept_error, sp_name, scipy_name
Expand Down Expand Up @@ -905,12 +904,9 @@ def decorator(impl):
@_wraps_partial_xp(impl, name, sp_name, scipy_name)
def test_func(*args, **kw):
# Run cupy and numpy
(
cupy_result,
cupy_error,
numpy_result,
numpy_error,
) = _call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
(cupy_result, cupy_error, numpy_result, numpy_error) = (
_call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
)

if cupy_error or numpy_error:
_check_cupy_numpy_error(
Expand Down Expand Up @@ -964,12 +960,9 @@ def decorator(impl):
@_wraps_partial_xp(impl, name, sp_name, scipy_name)
def test_func(*args, **kw):
# Run cupy and numpy
(
cupy_result,
cupy_error,
numpy_result,
numpy_error,
) = _call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
(cupy_result, cupy_error, numpy_result, numpy_error) = (
_call_func_numpy_cupy(impl, args, kw, name, sp_name, scipy_name)
)

_check_cupy_numpy_error(
cupy_error, numpy_error, accept_error=accept_error
Expand Down
Loading