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

use dpctl for trigonometric functions in dpnp #1545

Merged
merged 13 commits into from
Sep 8, 2023
10 changes: 0 additions & 10 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_ALLCLOSE
DPNP_FN_ALLCLOSE_EXT
DPNP_FN_ARANGE
DPNP_FN_ARCCOS
DPNP_FN_ARCCOS_EXT
DPNP_FN_ARCCOSH
DPNP_FN_ARCCOSH_EXT
DPNP_FN_ARCSIN
DPNP_FN_ARCSIN_EXT
DPNP_FN_ARCSINH
DPNP_FN_ARCSINH_EXT
DPNP_FN_ARCTAN
DPNP_FN_ARCTAN_EXT
DPNP_FN_ARCTAN2
DPNP_FN_ARCTAN2_EXT
DPNP_FN_ARCTANH
DPNP_FN_ARCTANH_EXT
DPNP_FN_ARGMAX
Expand Down Expand Up @@ -259,8 +251,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_SUM_EXT
DPNP_FN_SVD
DPNP_FN_SVD_EXT
DPNP_FN_TAN
DPNP_FN_TAN_EXT
DPNP_FN_TANH
DPNP_FN_TANH_EXT
DPNP_FN_TRACE
Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_iface_trigonometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def arctan(
Parameters `where`, `dtype` and `subok` are supported with their default values.
Keyword argument `kwargs` is currently unsupported.
Otherwise the function will be executed sequentially on CPU.
Input array data types are limited by supported DPNP :ref:`Data types`.
Input array data types are limited by supported real-valued floating-point data type.

See Also
--------
Expand Down
16 changes: 11 additions & 5 deletions tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,22 @@ def _test_mathematical(self, name, dtype, lhs, rhs):
else:
result = getattr(dpnp, name)(a_dpnp, b_dpnp)
expected = getattr(numpy, name)(a_np, b_np)
assert_allclose(result, expected, rtol=1e-6)
if numpy.issubdtype(expected.dtype, numpy.floating):
vtavana marked this conversation as resolved.
Show resolved Hide resolved
tol = numpy.max(
[
numpy.finfo(result.dtype).resolution,
numpy.finfo(expected.dtype).resolution,
]
)
else:
tol = 1e-06
assert_allclose(result, expected, rtol=tol)

@pytest.mark.parametrize("dtype", get_all_dtypes())
def test_add(self, dtype, lhs, rhs):
self._test_mathematical("add", dtype, lhs, rhs)

@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
def test_arctan2(self, dtype, lhs, rhs):
self._test_mathematical("arctan2", dtype, lhs, rhs)

Expand Down
19 changes: 3 additions & 16 deletions tests/test_strides.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_strides(func_name, dtype):
"sinh",
"sqrt",
"square",
"tan",
"tanh",
"trunc",
],
Expand All @@ -93,7 +94,7 @@ def test_strides_1arg(func_name, dtype, shape):
numpy_func = _getattr(numpy, func_name)
expected = numpy_func(b)

assert_allclose(result, expected)
assert_allclose(result, expected, rtol=1e-06)


@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True))
Expand Down Expand Up @@ -130,21 +131,6 @@ def test_strides_reciprocal(dtype, shape):
assert_allclose(result, expected, rtol=1e-06)


@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize("shape", [(10,)], ids=["(10,)"])
def test_strides_tan(dtype, shape):
a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape)
b = a[::2]

dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape)
dpb = dpa[::2]

result = dpnp.tan(dpb)
expected = numpy.tan(b)

assert_allclose(result, expected, rtol=1e-06)


@pytest.mark.parametrize(
"func_name",
[
Expand All @@ -161,6 +147,7 @@ def test_strides_tan(dtype, shape):
)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize("shape", [(3, 3)], ids=["(3, 3)"])
@pytest.mark.usefixtures("suppress_invalid_numpy_warnings")
def test_strides_2args(func_name, dtype, shape):
a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape)
b = a.T
Expand Down
44 changes: 43 additions & 1 deletion tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,47 @@ def test_invalid_shape(self, shape):
dpnp.exp(dp_array, out=dp_out)


class TestArccos:
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.usefixtures("suppress_invalid_numpy_warnings")
def test_arccos(self, dtype):
array_data = numpy.arange(10)
out = numpy.empty(10, dtype=dtype)

# DPNP
dp_array = dpnp.array(array_data, dtype=dtype)
dp_out = dpnp.array(out, dtype=dtype)
result = dpnp.arccos(dp_array, out=dp_out)

# original
np_array = numpy.array(array_data, dtype=dtype)
expected = numpy.arccos(np_array, out=out)

assert_array_equal(expected, result)

@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1]
)
def test_invalid_dtype(self, dtype):
dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1]
dp_array = dpnp.arange(10, dtype=dpnp_dtype)
dp_out = dpnp.empty(10, dtype=dtype)

with pytest.raises(TypeError):
dpnp.arccos(dp_array, out=dp_out)

@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize(
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
)
def test_invalid_shape(self, shape, dtype):
dp_array = dpnp.arange(10, dtype=dtype)
dp_out = dpnp.empty(shape, dtype=dtype)

with pytest.raises(ValueError):
dpnp.arccos(dp_array, out=dp_out)


class TestArcsin:
vtavana marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.usefixtures("suppress_invalid_numpy_warnings")
vtavana marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -437,7 +478,8 @@ def test_arctan(self, dtype):
np_array = numpy.array(array_data, dtype=dtype)
expected = numpy.arctan(np_array, out=out)

assert_allclose(expected, result)
tol = numpy.finfo(dtype).resolution
assert_allclose(expected, result, tol)

@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1]
Expand Down
2 changes: 1 addition & 1 deletion tests/third_party/cupy/math_tests/test_trigonometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def check_binary(self, name, xp, dtype):
b = testing.shaped_reverse_arange((2, 3), xp, dtype)
return getattr(xp, name)(a, b)

@testing.for_dtypes(["f", "d"])
@testing.for_dtypes(["e", "f", "d"])
@testing.numpy_cupy_allclose(atol=1e-5)
def check_unary_unit(self, name, xp, dtype):
a = xp.array([0.2, 0.4, 0.6, 0.8], dtype=dtype)
Expand Down