Skip to content

Commit 374fc96

Browse files
apply review remarks
1 parent a728e80 commit 374fc96

File tree

7 files changed

+93
-78
lines changed

7 files changed

+93
-78
lines changed

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,69 @@ def dpnp_not_equal(x1, x2, out=None, order="K"):
14591459
return dpnp_array._create_from_usm_ndarray(res_usm)
14601460

14611461

1462+
_power_docstring_ = """
1463+
power(x1, x2, out=None, order="K")
1464+
1465+
Calculates `x1_i` raised to `x2_i` for each element `x1_i` of the input array
1466+
`x1` with the respective element `x2_i` of the input array `x2`.
1467+
1468+
Args:
1469+
x1 (dpnp.ndarray):
1470+
First input array, expected to have numeric data type.
1471+
x2 (dpnp.ndarray):
1472+
Second input array, also expected to have numeric data type.
1473+
out ({None, dpnp.ndarray}, optional):
1474+
Output array to populate. Array must have the correct
1475+
shape and the expected data type.
1476+
order ("C","F","A","K", None, optional):
1477+
Output array, if parameter `out` is `None`.
1478+
Default: "K".
1479+
Returns:
1480+
dpnp.ndarray:
1481+
An array containing the result of element-wise of raising each element
1482+
to a specified power.
1483+
The data type of the returned array is determined by the Type Promotion Rules.
1484+
"""
1485+
1486+
1487+
def _call_pow(src1, src2, dst, sycl_queue, depends=None):
1488+
"""A callback to register in BinaryElementwiseFunc class of dpctl.tensor"""
1489+
1490+
if depends is None:
1491+
depends = []
1492+
1493+
# TODO: remove this check when OneMKL is fixed on Windows
1494+
is_win = platform.startswith("win")
1495+
1496+
if not is_win and vmi._mkl_pow_to_call(sycl_queue, src1, src2, dst):
1497+
# call pybind11 extension for pow() function from OneMKL VM
1498+
return vmi._pow(sycl_queue, src1, src2, dst, depends)
1499+
return ti._pow(src1, src2, dst, sycl_queue, depends)
1500+
1501+
1502+
pow_func = BinaryElementwiseFunc(
1503+
"pow", ti._pow_result_type, _call_pow, _power_docstring_
1504+
)
1505+
1506+
1507+
def dpnp_power(x1, x2, out=None, order="K"):
1508+
"""
1509+
Invokes pow() function from pybind11 extension of OneMKL VM if possible.
1510+
1511+
Otherwise fully relies on dpctl.tensor implementation for pow() function.
1512+
"""
1513+
1514+
# dpctl.tensor only works with usm_ndarray or scalar
1515+
x1_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x1)
1516+
x2_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x2)
1517+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1518+
1519+
res_usm = pow_func(
1520+
x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order
1521+
)
1522+
return dpnp_array._create_from_usm_ndarray(res_usm)
1523+
1524+
14621525
_remainder_docstring_ = """
14631526
remainder(x1, x2, out=None, order='K')
14641527
Calculates the remainder of division for each element `x1_i` of the input array
@@ -1645,68 +1708,6 @@ def dpnp_sign(x, out=None, order="K"):
16451708
return dpnp_array._create_from_usm_ndarray(res_usm)
16461709

16471710

1648-
_power_docstring_ = """
1649-
power(x1, x2, out=None, order="K")
1650-
1651-
Calculates `x1_i` raised to `x2_i` for each element `x1_i` of the input array
1652-
`x1` with the respective element `x2_i` of the input array `x2`.
1653-
1654-
Args:
1655-
x1 (dpnp.ndarray):
1656-
First input array, expected to have numeric data type.
1657-
x2 (dpnp.ndarray):
1658-
Second input array, also expected to have numeric data type.
1659-
out ({None, dpnp.ndarray}, optional):
1660-
Output array to populate. Array must have the correct
1661-
shape and the expected data type.
1662-
order ("C","F","A","K", None, optional):
1663-
Output array, if parameter `out` is `None`.
1664-
Default: "K".
1665-
Returns:
1666-
dpnp.ndarray:
1667-
An array containing the result of element-wise of raising each element
1668-
to a specified power.
1669-
The data type of the returned array is determined by the Type Promotion Rules.
1670-
"""
1671-
1672-
1673-
def _call_pow(src1, src2, dst, sycl_queue, depends=None):
1674-
"""A callback to register in BinaryElementwiseFunc class of dpctl.tensor"""
1675-
1676-
if depends is None:
1677-
depends = []
1678-
1679-
# TODO: remove this check when OneMKL is fixed on Windows
1680-
is_win = platform.startswith("win")
1681-
1682-
if not is_win and vmi._mkl_pow_to_call(sycl_queue, src1, src2, dst):
1683-
# call pybind11 extension for pow() function from OneMKL VM
1684-
return vmi._pow(sycl_queue, src1, src2, dst, depends)
1685-
return ti._pow(src1, src2, dst, sycl_queue, depends)
1686-
1687-
1688-
pow_func = BinaryElementwiseFunc(
1689-
"pow", ti._pow_result_type, _call_pow, _power_docstring_
1690-
)
1691-
1692-
1693-
def dpnp_power(x1, x2, out=None, order="K"):
1694-
"""
1695-
Invokes pow() function from pybind11 extension of OneMKL VM if possible.
1696-
1697-
Otherwise fully relies on dpctl.tensor implementation for pow() function.
1698-
"""
1699-
# dpctl.tensor only works with usm_ndarray or scalar
1700-
x1_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x1)
1701-
x2_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x2)
1702-
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1703-
1704-
res_usm = pow_func(
1705-
x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order
1706-
)
1707-
return dpnp_array._create_from_usm_ndarray(res_usm)
1708-
1709-
17101711
_sin_docstring = """
17111712
sin(x, out=None, order='K')
17121713
Computes sine for each element `x_i` of input array `x`.

dpnp/dpnp_iface_mathematical.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,15 +1611,29 @@ def power(
16111611
>>> dp.power(a, 3)
16121612
array([ 0, 1, 8, 27, 64, 125])
16131613
1614+
Raise the bases to different exponents.
1615+
16141616
>>> b = dp.array([1.0, 2.0, 3.0, 3.0, 2.0, 1.0])
16151617
>>> dp.power(a, b)
16161618
array([ 0., 1., 8., 27., 16., 5.])
16171619
1620+
The effect of broadcasting.
1621+
16181622
>>> c = dp.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
16191623
>>> dp.power(a, c)
16201624
array([[ 0, 1, 8, 27, 16, 5],
16211625
[ 0, 1, 8, 27, 16, 5]])
16221626
1627+
The ``**`` operator can be used as a shorthand for ``power`` on
1628+
:class:`dpnp.ndarray`.
1629+
1630+
>>> b = dp.array([1, 2, 3, 3, 2, 1])
1631+
>>> a = dp.arange(6)
1632+
>>> a ** b
1633+
array([ 0, 1, 8, 27, 16, 5])
1634+
1635+
Negative values raised to a non-integral value will result in ``nan``.
1636+
16231637
>>> d = dp.array([-1.0, -4.0])
16241638
>>> dp.power(d, 1.5)
16251639
array([nan, nan])

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ tests/test_sycl_queue.py::test_modf[level_zero:gpu:0]
3636
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-trapz-data19]
3737
tests/test_sycl_queue.py::test_1in_1out[opencl:cpu:0-trapz-data19]
3838

39-
tests/test_sycl_queue.py::test_out_2in_1out[opencl:gpu:0-power-data19-data29]
40-
tests/test_sycl_queue.py::test_out_2in_1out[level_zero:gpu:0-power-data19-data29]
41-
4239
tests/test_umath.py::test_umaths[('divmod', 'ii')]
4340
tests/test_umath.py::test_umaths[('divmod', 'll')]
4441
tests/test_umath.py::test_umaths[('divmod', 'ff')]

tests/test_mathematical.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,22 @@ def test_op_with_scalar(array, val, func, data_type, val_type):
274274
val_ = val_type(val)
275275

276276
if func == "power":
277-
if val_ == 0 and numpy.issubdtype(data_type, numpy.complexfloating):
277+
if (
278+
val_ == 0
279+
and numpy.issubdtype(data_type, numpy.complexfloating)
280+
and not dpnp.all(dpnp_a)
281+
):
278282
pytest.skip(
279283
"(0j ** 0) is different: (NaN + NaNj) in dpnp and (1 + 0j) in numpy"
280284
)
281-
elif is_cpu_device() and data_type == dpnp.complex128:
282-
# TODO: discuss the bahavior with OneMKL team
285+
# TODO: Remove when #1378 (dpctl) is solved
286+
elif (
287+
dpnp_a.dtype == dpnp.complex128
288+
and dpnp_a.size >= 8
289+
and not dpnp.all(dpnp_a)
290+
):
283291
pytest.skip(
284-
"(0j ** 5) is different: (NaN + NaNj) in dpnp and (0j) in numpy"
292+
"[..., 0j ** val] is different for x.size > 8: [..., NaN + NaNj] in dpnp and [..., 0 + 0j] in numpy"
285293
)
286294

287295
if func == "subtract" and val_type == bool and data_type == dpnp.bool:
@@ -347,7 +355,6 @@ def test_divide_scalar(shape, dtype):
347355

348356
@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["()", "(3, 2)"])
349357
@pytest.mark.parametrize("dtype", get_all_dtypes())
350-
@pytest.mark.skip("mute until in-place support in dpctl is done")
351358
def test_power_scalar(shape, dtype):
352359
np_a = numpy.ones(shape, dtype=dtype)
353360
dpnp_a = dpnp.ones(shape, dtype=dtype)
@@ -970,11 +977,7 @@ def test_power(self, dtype):
970977
np_array2 = numpy.array(array2_data, dtype=dtype)
971978
expected = numpy.power(np_array1, np_array2, out=out)
972979

973-
if dtype is dpnp.complex128:
974-
# ((0 + 0j) ** 2) == (Nan + NaNj) in dpnp and == (0 + 0j) in numpy
975-
assert_allclose(expected[1:], result[1:], rtol=1e-06)
976-
else:
977-
assert_allclose(expected, result, rtol=1e-06)
980+
assert_allclose(expected, result, rtol=1e-06)
978981

979982
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
980983
def test_out_dtypes(self, dtype):

tests/test_sycl_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def test_out_2in_1out(func, data1, data2, device):
603603
result = dpnp.empty_like(dp_out)
604604
getattr(dpnp, func)(x1, x2, out=result)
605605

606-
assert_array_equal(result, expected)
606+
assert_allclose(result, expected)
607607

608608
assert_sycl_queue_equal(result.sycl_queue, x1.sycl_queue)
609609
assert_sycl_queue_equal(result.sycl_queue, x2.sycl_queue)

tests/third_party/cupy/math_tests/test_arithmetic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from tests.helper import has_support_aspect64
1010
from tests.third_party.cupy import testing
1111

12-
float_types = [*testing.helper._float_dtypes]
12+
float_types = list(testing.helper._float_dtypes)
1313
complex_types = []
1414
signed_int_types = [numpy.int32, numpy.int64]
1515
unsigned_int_types = []

tests/third_party/cupy/testing/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ def test_func(*args, **kw):
10071007
for dtype in dtypes:
10081008
if (
10091009
numpy.dtype(dtype).type in (numpy.float64, numpy.complex128)
1010-
and not select_default_device().has_aspect_fp64
1010+
and not has_support_aspect64()
10111011
):
10121012
continue
10131013

0 commit comments

Comments
 (0)