From 96cb790110958c6c7697637bc5969202eb0efb16 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Thu, 24 Aug 2023 09:25:50 -0500 Subject: [PATCH 1/2] implement dpnp.int --- dpnp/dpnp_iface_logic.py | 34 +++--- dpnp/dpnp_iface_mathematical.py | 109 ++++++++++++++++-- dpnp/dpnp_iface_trigonometric.py | 10 +- tests/skipped_tests.tbl | 5 - tests/skipped_tests_gpu.tbl | 4 - tests/skipped_tests_gpu_no_fp64.tbl | 2 - .../cupy/math_tests/test_rounding.py | 2 +- tests/third_party/cupy/testing/helper.py | 6 + 8 files changed, 126 insertions(+), 46 deletions(-) diff --git a/dpnp/dpnp_iface_logic.py b/dpnp/dpnp_iface_logic.py index d867bbbd660..4c0fdc93860 100644 --- a/dpnp/dpnp_iface_logic.py +++ b/dpnp/dpnp_iface_logic.py @@ -94,9 +94,9 @@ def all(x, /, axis=None, out=None, keepdims=False, *, where=True): Returns ------- - dpnp.ndarray - An array with a data type of `bool` - containing the results of the logical AND reduction. + out : dpnp.ndarray + An array with a data type of `bool` + containing the results of the logical AND reduction. Limitations ----------- @@ -198,7 +198,7 @@ def any(x, /, axis=None, out=None, keepdims=False, *, where=True): Returns ------- - dpnp.ndarray + out : dpnp.ndarray An array with a data type of `bool` containing the results of the logical OR reduction. @@ -276,7 +276,7 @@ def equal( Returns ------- out : dpnp.ndarray - Output array of bool type, element-wise comparison of `x1` and `x2`. + Output array of bool type, element-wise comparison of `x1` and `x2`. Limitations ----------- @@ -352,7 +352,7 @@ def greater( Returns ------- out : dpnp.ndarray - Output array of bool type, element-wise comparison of `x1` and `x2`. + Output array of bool type, element-wise comparison of `x1` and `x2`. Limitations ----------- @@ -422,7 +422,7 @@ def greater_equal( Returns ------- out : dpnp.ndarray - Output array of bool type, element-wise comparison of `x1` and `x2`. + Output array of bool type, element-wise comparison of `x1` and `x2`. Limitations ----------- @@ -678,7 +678,7 @@ def less( Returns ------- out : dpnp.ndarray - Output array of bool type, element-wise comparison of `x1` and `x2`. + Output array of bool type, element-wise comparison of `x1` and `x2`. Limitations ----------- @@ -748,7 +748,7 @@ def less_equal( Returns ------- out : dpnp.ndarray - Output array of bool type, element-wise comparison of `x1` and `x2`. + Output array of bool type, element-wise comparison of `x1` and `x2`. Limitations ----------- @@ -818,8 +818,8 @@ def logical_and( Returns ------- out : dpnp.ndarray - Boolean result of the logical AND operation applied to the elements - of `x1` and `x2`; the shape is determined by broadcasting. + Boolean result of the logical AND operation applied to the elements + of `x1` and `x2`; the shape is determined by broadcasting. Limitations ----------- @@ -891,8 +891,8 @@ def logical_not( Returns ------- out : dpnp.ndarray - Boolean result with the same shape as `x` of the NOT operation - on elements of `x`. + Boolean result with the same shape as `x` of the NOT operation + on elements of `x`. Limitations ----------- @@ -953,8 +953,8 @@ def logical_or( Returns ------- out : dpnp.ndarray - Boolean result of the logical OR operation applied to the elements - of `x1` and `x2`; the shape is determined by broadcasting. + Boolean result of the logical OR operation applied to the elements + of `x1` and `x2`; the shape is determined by broadcasting. Limitations ----------- @@ -1027,8 +1027,8 @@ def logical_xor( Returns ------- out : dpnp.ndarray - Boolean result of the logical XOR operation applied to the elements - of `x1` and `x2`; the shape is determined by broadcasting. + Boolean result of the logical XOR operation applied to the elements + of `x1` and `x2`; the shape is determined by broadcasting. Limitations ----------- diff --git a/dpnp/dpnp_iface_mathematical.py b/dpnp/dpnp_iface_mathematical.py index 1145593731f..523f0f64d9e 100644 --- a/dpnp/dpnp_iface_mathematical.py +++ b/dpnp/dpnp_iface_mathematical.py @@ -101,6 +101,7 @@ "negative", "power", "prod", + "rint", "remainder", "round", "sign", @@ -145,7 +146,7 @@ def absolute(x, /, out=None, *, where=True, dtype=None, subok=True, **kwargs): Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray An array containing the absolute value of each element in `x`. Limitations @@ -219,7 +220,7 @@ def add( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The sum of `x1` and `x2`, element-wise. Limitations @@ -275,6 +276,11 @@ def around(x, /, decimals=0, out=None): For full documentation refer to :obj:`numpy.around`. + Returns + ------- + out : dpnp.ndarray + The rounded value of elements of the array. + Limitations ----------- Parameter `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`. @@ -284,7 +290,8 @@ def around(x, /, decimals=0, out=None): See Also -------- - :obj:`dpnp.round` : equivalent function; see for details. + :obj:`dpnp.round` : Equivalent function; see for details. + :obj:`dpnp.rint` : Round elements of the array to the nearest integer. :obj:`dpnp.ceil` : Compute the ceiling of the input, element-wise. :obj:`dpnp.floor` : Return the floor of the input, element-wise. :obj:`dpnp.trunc` : Return the truncated value of the input, element-wise. @@ -659,7 +666,7 @@ def divide( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The quotient `x1/x2`, element-wise. Limitations @@ -865,6 +872,11 @@ def floor_divide( For full documentation refer to :obj:`numpy.floor_divide`. + Returns + ------- + out : dpnp.ndarray + The floordivide of each element of `x`. + Limitations ----------- Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray` @@ -1215,6 +1227,11 @@ def mod( For full documentation refer to :obj:`numpy.mod`. + Returns + ------- + out : dpnp.ndarray + The element-wise remainder of the quotient `floor_divide(x1, x2)`. + Limitations ----------- Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray` @@ -1298,7 +1315,7 @@ def multiply( Returns ------- - y : {dpnp.ndarray, scalar} + out : {dpnp.ndarray, scalar} The product of `x1` and `x2`, element-wise. Limitations @@ -1503,7 +1520,7 @@ def negative( Returns ------- out : dpnp.ndarray - The numerical negative of each element of `x`. + The numerical negative of each element of `x`. Limitations ----------- @@ -1555,7 +1572,7 @@ def power(x1, x2, /, out=None, *, where=True, dtype=None, subok=True, **kwargs): Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The bases in `x1` raised to the exponents in `x2`. Limitations @@ -1702,6 +1719,64 @@ def prod( ) +def rint( + x, + /, + out=None, + *, + order="K", + where=True, + dtype=None, + subok=True, + **kwargs, +): + """ + Round elements of the array to the nearest integer. + + For full documentation refer to :obj:`numpy.rint`. + + Returns + ------- + out : dpnp.ndarray + The rounded value of elements of the array to the nearest integer. + + Limitations + ----------- + Parameter `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`. + 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`. + + See Also + -------- + :obj:`dpnp.round` : Evenly round to the given number of decimals. + :obj:`dpnp.ceil` : Compute the ceiling of the input, element-wise. + :obj:`dpnp.floor` : Return the floor of the input, element-wise. + :obj:`dpnp.trunc` : Return the truncated value of the input, element-wise. + + Examples + -------- + >>> import dpnp as np + >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) + >>> np.rint(a) + array([-2., -2., -0., 0., 2., 2., 2.]) + + """ + + return check_nd_call_func( + numpy.rint, + dpnp_round, + x, + out=out, + where=where, + order=order, + dtype=dtype, + subok=subok, + **kwargs, + ) + + def remainder( x1, x2, @@ -1719,6 +1794,11 @@ def remainder( For full documentation refer to :obj:`numpy.remainder`. + Returns + ------- + out : dpnp.ndarray + The element-wise remainder of the quotient `floor_divide(x1, x2)`. + Limitations ----------- Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray` @@ -1773,6 +1853,11 @@ def round(x, decimals=0, out=None): For full documentation refer to :obj:`numpy.round`. + Returns + ------- + out : dpnp.ndarray + The rounded value of elements of the array. + Limitations ----------- Parameter `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`. @@ -1782,7 +1867,8 @@ def round(x, decimals=0, out=None): See Also -------- - :obj:`dpnp.around` : equivalent function; see for details. + :obj:`dpnp.around` : Equivalent function; see for details. + :obj:`dpnp.rint` : Round elements of the array to the nearest integer. :obj:`dpnp.ceil` : Compute the ceiling of the input, element-wise. :obj:`dpnp.floor` : Return the floor of the input, element-wise. :obj:`dpnp.trunc` : Return the truncated value of the input, element-wise. @@ -1832,7 +1918,7 @@ def sign( Returns ------- out : dpnp.ndarray - The indication of the sign of each element of `x`. + The indication of the sign of each element of `x`. Limitations ----------- @@ -1899,7 +1985,7 @@ def subtract( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The difference of `x1` and `x2`, element-wise. Limitations @@ -1965,7 +2051,7 @@ def sum( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray an array containing the sums. If the sum was computed over the entire array, a zero-dimensional array is returned. The returned array has the data type as described in the `dtype` parameter @@ -2134,7 +2220,6 @@ def true_divide(*args, **kwargs): ----- This function works the same as :obj:`dpnp.divide`. - """ return dpnp.divide(*args, **kwargs) diff --git a/dpnp/dpnp_iface_trigonometric.py b/dpnp/dpnp_iface_trigonometric.py index dc91fa75eda..82df251ad3d 100644 --- a/dpnp/dpnp_iface_trigonometric.py +++ b/dpnp/dpnp_iface_trigonometric.py @@ -429,7 +429,7 @@ def cos( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The cosine of each element of `x`. Limitations @@ -738,7 +738,7 @@ def log( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The natural logarithm of `x`, element-wise. Limitations @@ -982,7 +982,7 @@ def sin( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray The sine of each element of `x`. Limitations @@ -1069,7 +1069,7 @@ def sqrt( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray An array of the same shape as `x`, containing the positive square-root of each element in `x`. If any element in `x` is complex, a complex array is returned (and the square-roots of @@ -1129,7 +1129,7 @@ def square( Returns ------- - y : dpnp.ndarray + out : dpnp.ndarray Element-wise `x * x`, of the same shape and dtype as `x`. Limitations diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index f86aebd50f9..7dfac9fe33f 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -111,8 +111,6 @@ tests/test_umath.py::test_umaths[('positive', 'i')] tests/test_umath.py::test_umaths[('positive', 'l')] tests/test_umath.py::test_umaths[('positive', 'f')] tests/test_umath.py::test_umaths[('positive', 'd')] -tests/test_umath.py::test_umaths[('rint', 'f')] -tests/test_umath.py::test_umaths[('rint', 'd')] tests/test_umath.py::test_umaths[('signbit', 'f')] tests/test_umath.py::test_umaths[('signbit', 'd')] tests/test_umath.py::test_umaths[('spacing', 'f')] @@ -524,7 +522,6 @@ tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_signbit tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_min_max_none tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip4 tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_absolute_negative -tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_sign_negative tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_maximum_nan tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_minimum_nan tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fmax_nan @@ -669,8 +666,6 @@ tests/third_party/cupy/math_tests/test_misc.py::TestConvolve::test_convolve_diff tests/third_party/cupy/math_tests/test_misc.py::TestConvolve::test_convolve_diff_types[full] tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_fix -tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint -tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint_negative tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out_wrong_shape tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_ndarray_cumprod_2dim_with_axis diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index d3bffdb5425..e43f7632453 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -63,8 +63,6 @@ tests/test_umath.py::test_umaths[('positive', 'i')] tests/test_umath.py::test_umaths[('positive', 'l')] tests/test_umath.py::test_umaths[('positive', 'f')] tests/test_umath.py::test_umaths[('positive', 'd')] -tests/test_umath.py::test_umaths[('rint', 'f')] -tests/test_umath.py::test_umaths[('rint', 'd')] tests/test_umath.py::test_umaths[('signbit', 'f')] tests/test_umath.py::test_umaths[('signbit', 'd')] tests/test_umath.py::test_umaths[('spacing', 'f')] @@ -810,8 +808,6 @@ tests/third_party/cupy/math_tests/test_misc.py::TestConvolve::test_convolve_diff tests/third_party/cupy/math_tests/test_misc.py::TestConvolve::test_convolve_diff_types[full] tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_fix -tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint -tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint_negative tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_ndarray_cumprod_2dim_with_axis tests/third_party/cupy/math_tests/test_sumprod.py::TestDiff::test_diff_1dim tests/third_party/cupy/math_tests/test_sumprod.py::TestDiff::test_diff_1dim_with_n diff --git a/tests/skipped_tests_gpu_no_fp64.tbl b/tests/skipped_tests_gpu_no_fp64.tbl index 710eaf88afa..0d920e96034 100644 --- a/tests/skipped_tests_gpu_no_fp64.tbl +++ b/tests/skipped_tests_gpu_no_fp64.tbl @@ -1200,8 +1200,6 @@ tests/third_party/cupy/math_tests/test_hyperbolic.py::TestHyperbolic::test_cosh tests/third_party/cupy/math_tests/test_hyperbolic.py::TestHyperbolic::test_sinh tests/third_party/cupy/math_tests/test_hyperbolic.py::TestHyperbolic::test_tanh -tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_sign_negative - tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_0_{axis=0, func='nansum', keepdims=True, shape=(2, 3, 4), transpose_axes=True}::test_nansum_all tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_0_{axis=0, func='nansum', keepdims=True, shape=(2, 3, 4), transpose_axes=True}::test_nansum_axis_transposed tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_1_{axis=0, func='nansum', keepdims=True, shape=(2, 3, 4), transpose_axes=False}::test_nansum_all diff --git a/tests/third_party/cupy/math_tests/test_rounding.py b/tests/third_party/cupy/math_tests/test_rounding.py index a5fa0b437dd..5c9cbcd4aa6 100644 --- a/tests/third_party/cupy/math_tests/test_rounding.py +++ b/tests/third_party/cupy/math_tests/test_rounding.py @@ -30,7 +30,7 @@ def check_unary_complex_unsupported(self, name, dtype): getattr(xp, name)(a) @testing.for_dtypes(["?", "b", "h", "i", "q", "e", "f", "d"]) - @testing.numpy_cupy_allclose(atol=1e-5) + @testing.numpy_cupy_allclose(type_check=False, atol=1e-5) def check_unary_negative(self, name, xp, dtype): a = xp.array([-3, -2, -1, 1, 2, 3], dtype=dtype) return getattr(xp, name)(a) diff --git a/tests/third_party/cupy/testing/helper.py b/tests/third_party/cupy/testing/helper.py index f9f419e91c3..bab67cb0aeb 100644 --- a/tests/third_party/cupy/testing/helper.py +++ b/tests/third_party/cupy/testing/helper.py @@ -1005,6 +1005,12 @@ def decorator(impl): @_wraps_partial(impl, name) def test_func(*args, **kw): for dtype in dtypes: + if ( + dtype in ["d", "D"] + and not select_default_device().has_aspect_fp64 + ): + continue + try: kw[name] = numpy.dtype(dtype).type impl(*args, **kw) From 62cd2bfd8a3a510b3e1a3cacdb29362dadd9a5bb Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Thu, 24 Aug 2023 16:35:25 -0500 Subject: [PATCH 2/2] address comments --- dpnp/dpnp_iface_mathematical.py | 4 +++- tests/third_party/cupy/testing/helper.py | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dpnp/dpnp_iface_mathematical.py b/dpnp/dpnp_iface_mathematical.py index 523f0f64d9e..0b55a6b9157 100644 --- a/dpnp/dpnp_iface_mathematical.py +++ b/dpnp/dpnp_iface_mathematical.py @@ -101,8 +101,8 @@ "negative", "power", "prod", - "rint", "remainder", + "rint", "round", "sign", "subtract", @@ -291,6 +291,7 @@ def around(x, /, decimals=0, out=None): See Also -------- :obj:`dpnp.round` : Equivalent function; see for details. + :obj:`dpnp.ndarray.round` : Equivalent function. :obj:`dpnp.rint` : Round elements of the array to the nearest integer. :obj:`dpnp.ceil` : Compute the ceiling of the input, element-wise. :obj:`dpnp.floor` : Return the floor of the input, element-wise. @@ -1868,6 +1869,7 @@ def round(x, decimals=0, out=None): See Also -------- :obj:`dpnp.around` : Equivalent function; see for details. + :obj:`dpnp.ndarray.round` : Equivalent function. :obj:`dpnp.rint` : Round elements of the array to the nearest integer. :obj:`dpnp.ceil` : Compute the ceiling of the input, element-wise. :obj:`dpnp.floor` : Return the floor of the input, element-wise. diff --git a/tests/third_party/cupy/testing/helper.py b/tests/third_party/cupy/testing/helper.py index bab67cb0aeb..859356ebb59 100644 --- a/tests/third_party/cupy/testing/helper.py +++ b/tests/third_party/cupy/testing/helper.py @@ -1006,11 +1006,17 @@ def decorator(impl): def test_func(*args, **kw): for dtype in dtypes: if ( - dtype in ["d", "D"] + numpy.dtype(dtype).type in (numpy.float64, numpy.complex128) and not select_default_device().has_aspect_fp64 ): continue + if ( + numpy.dtype(dtype).type == numpy.float16 + and not select_default_device().has_aspect_fp16 + ): + continue + try: kw[name] = numpy.dtype(dtype).type impl(*args, **kw)