Skip to content

Commit 79ac2b0

Browse files
authored
Merge pull request #1523 from IntelPython/use_dpctl_sign_negative_in_dpnp
use dpctl.tensor.sign and dpctl.tensor.negative in dpnp
2 parents d159067 + 096857c commit 79ac2b0

15 files changed

+265
-119
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ enum class DPNPFuncName : size_t
282282
DPNP_FN_NANVAR_EXT, /**< Used in numpy.nanvar() impl, requires extra
283283
parameters */
284284
DPNP_FN_NEGATIVE, /**< Used in numpy.negative() impl */
285-
DPNP_FN_NEGATIVE_EXT, /**< Used in numpy.negative() impl, requires extra
286-
parameters */
287285
DPNP_FN_NONZERO, /**< Used in numpy.nonzero() impl */
288286
DPNP_FN_NOT_EQUAL_EXT, /**< Used in numpy.not_equal() impl, requires extra
289287
parameters */
@@ -441,10 +439,8 @@ enum class DPNPFuncName : size_t
441439
DPNP_FN_SEARCHSORTED_EXT, /**< Used in numpy.searchsorted() impl, requires
442440
extra parameters */
443441
DPNP_FN_SIGN, /**< Used in numpy.sign() impl */
444-
DPNP_FN_SIGN_EXT, /**< Used in numpy.sign() impl, requires extra parameters
445-
*/
446-
DPNP_FN_SIN, /**< Used in numpy.sin() impl */
447-
DPNP_FN_SINH, /**< Used in numpy.sinh() impl */
442+
DPNP_FN_SIN, /**< Used in numpy.sin() impl */
443+
DPNP_FN_SINH, /**< Used in numpy.sinh() impl */
448444
DPNP_FN_SINH_EXT, /**< Used in numpy.sinh() impl, requires extra parameters
449445
*/
450446
DPNP_FN_SORT, /**< Used in numpy.sort() impl */

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -1061,15 +1061,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
10611061
fmap[DPNPFuncName::DPNP_FN_NEGATIVE][eft_DBL][eft_DBL] = {
10621062
eft_DBL, (void *)dpnp_negative_c_default<double>};
10631063

1064-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_INT][eft_INT] = {
1065-
eft_INT, (void *)dpnp_negative_c_ext<int32_t>};
1066-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_LNG][eft_LNG] = {
1067-
eft_LNG, (void *)dpnp_negative_c_ext<int64_t>};
1068-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_FLT][eft_FLT] = {
1069-
eft_FLT, (void *)dpnp_negative_c_ext<float>};
1070-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_DBL][eft_DBL] = {
1071-
eft_DBL, (void *)dpnp_negative_c_ext<double>};
1072-
10731064
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_INT][eft_INT] = {
10741065
eft_INT, (void *)dpnp_recip_c_default<int32_t>};
10751066
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_LNG][eft_LNG] = {
@@ -1097,15 +1088,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
10971088
fmap[DPNPFuncName::DPNP_FN_SIGN][eft_DBL][eft_DBL] = {
10981089
eft_DBL, (void *)dpnp_sign_c_default<double>};
10991090

1100-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_INT][eft_INT] = {
1101-
eft_INT, (void *)dpnp_sign_c_ext<int32_t>};
1102-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_LNG][eft_LNG] = {
1103-
eft_LNG, (void *)dpnp_sign_c_ext<int64_t>};
1104-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_FLT][eft_FLT] = {
1105-
eft_FLT, (void *)dpnp_sign_c_ext<float>};
1106-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_DBL][eft_DBL] = {
1107-
eft_DBL, (void *)dpnp_sign_c_ext<double>};
1108-
11091091
fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_INT][eft_INT] = {
11101092
eft_INT, (void *)dpnp_square_c_default<int32_t>};
11111093
fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_LNG][eft_LNG] = {

dpnp/dpnp_algo/dpnp_algo.pxd

-5
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
160160
DPNP_FN_MODF_EXT
161161
DPNP_FN_NANVAR
162162
DPNP_FN_NANVAR_EXT
163-
DPNP_FN_NEGATIVE
164-
DPNP_FN_NEGATIVE_EXT
165163
DPNP_FN_NONZERO
166164
DPNP_FN_ONES
167165
DPNP_FN_ONES_LIKE
@@ -257,8 +255,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
257255
DPNP_FN_RNG_ZIPF_EXT
258256
DPNP_FN_SEARCHSORTED
259257
DPNP_FN_SEARCHSORTED_EXT
260-
DPNP_FN_SIGN
261-
DPNP_FN_SIGN_EXT
262258
DPNP_FN_SINH
263259
DPNP_FN_SINH_EXT
264260
DPNP_FN_SORT
@@ -427,7 +423,6 @@ cpdef dpnp_descriptor dpnp_maximum(dpnp_descriptor x1_obj, dpnp_descriptor x2_ob
427423
dpnp_descriptor out=*, object where=*)
428424
cpdef dpnp_descriptor dpnp_minimum(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
429425
dpnp_descriptor out=*, object where=*)
430-
cpdef dpnp_descriptor dpnp_negative(dpnp_descriptor array1)
431426
cpdef dpnp_descriptor dpnp_power(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
432427
dpnp_descriptor out=*, object where=*)
433428

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

-10
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ __all__ += [
5656
"dpnp_nancumsum",
5757
"dpnp_nanprod",
5858
"dpnp_nansum",
59-
"dpnp_negative",
6059
"dpnp_power",
6160
"dpnp_prod",
62-
"dpnp_sign",
6361
"dpnp_sum",
6462
"dpnp_trapz",
6563
]
@@ -455,10 +453,6 @@ cpdef utils.dpnp_descriptor dpnp_nansum(utils.dpnp_descriptor x1):
455453
return dpnp_sum(result)
456454

457455

458-
cpdef utils.dpnp_descriptor dpnp_negative(dpnp_descriptor x1):
459-
return call_fptr_1in_1out_strides(DPNP_FN_NEGATIVE_EXT, x1)
460-
461-
462456
cpdef utils.dpnp_descriptor dpnp_power(utils.dpnp_descriptor x1_obj,
463457
utils.dpnp_descriptor x2_obj,
464458
object dtype=None,
@@ -529,10 +523,6 @@ cpdef utils.dpnp_descriptor dpnp_prod(utils.dpnp_descriptor x1,
529523
return result
530524

531525

532-
cpdef utils.dpnp_descriptor dpnp_sign(utils.dpnp_descriptor x1):
533-
return call_fptr_1in_1out_strides(DPNP_FN_SIGN_EXT, x1)
534-
535-
536526
cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor x1,
537527
object axis=None,
538528
object dtype=None,

dpnp/dpnp_algo/dpnp_elementwise_common.py

+88
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,49 @@ def dpnp_multiply(x1, x2, out=None, order="K"):
13901390
return dpnp_array._create_from_usm_ndarray(res_usm)
13911391

13921392

1393+
_negative_docstring = """
1394+
negative(x, out=None, order="K")
1395+
1396+
Computes the numerical negative for each element `x_i` of input array `x`.
1397+
1398+
Args:
1399+
x (dpnp.ndarray):
1400+
Input array, expected to have numeric data type.
1401+
out ({None, dpnp.ndarray}, optional):
1402+
Output array to populate.
1403+
Array have the correct shape and the expected data type.
1404+
order ("C","F","A","K", optional):
1405+
Memory layout of the newly output array, if parameter `out` is `None`.
1406+
Default: "K".
1407+
Returns:
1408+
dpnp.ndarray:
1409+
An array containing the negative of `x`.
1410+
"""
1411+
1412+
1413+
negative_func = UnaryElementwiseFunc(
1414+
"negative", ti._negative_result_type, ti._negative, _negative_docstring
1415+
)
1416+
1417+
1418+
def dpnp_negative(x, out=None, order="K"):
1419+
"""Invokes negative() from dpctl.tensor implementation for negative() function."""
1420+
1421+
# TODO: discuss with dpctl if the check is needed to be moved there
1422+
if not dpnp.isscalar(x) and x.dtype == dpnp.bool:
1423+
raise TypeError(
1424+
"DPNP boolean negative, the `-` operator, is not supported, "
1425+
"use the `~` operator or the logical_not function instead."
1426+
)
1427+
1428+
# dpctl.tensor only works with usm_ndarray
1429+
x1_usm = dpnp.get_usm_ndarray(x)
1430+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1431+
1432+
res_usm = negative_func(x1_usm, out=out_usm, order=order)
1433+
return dpnp_array._create_from_usm_ndarray(res_usm)
1434+
1435+
13931436
_not_equal_docstring_ = """
13941437
not_equal(x1, x2, out=None, order="K")
13951438
@@ -1525,6 +1568,51 @@ def dpnp_right_shift(x1, x2, out=None, order="K"):
15251568
return dpnp_array._create_from_usm_ndarray(res_usm)
15261569

15271570

1571+
_sign_docstring = """
1572+
sign(x, out=None, order="K")
1573+
1574+
Computes an indication of the sign of each element `x_i` of input array `x`
1575+
using the signum function.
1576+
1577+
The signum function returns `-1` if `x_i` is less than `0`,
1578+
`0` if `x_i` is equal to `0`, and `1` if `x_i` is greater than `0`.
1579+
1580+
Args:
1581+
x (dpnp.ndarray):
1582+
Input array, expected to have numeric data type.
1583+
out ({None, dpnp.ndarray}, optional):
1584+
Output array to populate.
1585+
Array have the correct shape and the expected data type.
1586+
order ("C","F","A","K", optional):
1587+
Memory layout of the newly output array, if parameter `out` is `None`.
1588+
Default: "K".
1589+
Returns:
1590+
dpnp.ndarray:
1591+
An array containing the element-wise results. The data type of the
1592+
returned array is determined by the Type Promotion Rules.
1593+
"""
1594+
1595+
1596+
sign_func = UnaryElementwiseFunc(
1597+
"sign", ti._sign_result_type, ti._sign, _sign_docstring
1598+
)
1599+
1600+
1601+
def dpnp_sign(x, out=None, order="K"):
1602+
"""Invokes sign() from dpctl.tensor implementation for sign() function."""
1603+
1604+
# TODO: discuss with dpctl if the check is needed to be moved there
1605+
if not dpnp.isscalar(x) and x.dtype == dpnp.bool:
1606+
raise TypeError("DPNP boolean sign is not supported.")
1607+
1608+
# dpctl.tensor only works with usm_ndarray
1609+
x1_usm = dpnp.get_usm_ndarray(x)
1610+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1611+
1612+
res_usm = sign_func(x1_usm, out=out_usm, order=order)
1613+
return dpnp_array._create_from_usm_ndarray(res_usm)
1614+
1615+
15281616
_sin_docstring = """
15291617
sin(x, out=None, order='K')
15301618
Computes sine for each element `x_i` of input array `x`.

dpnp/dpnp_iface_bitwise.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def left_shift(
374374
>>> np.left_shift(x1, x2)
375375
array([10, 20, 40])
376376
377-
The ``<<`` operator can be used as a shorthand for ``right_shift`` on
377+
The ``<<`` operator can be used as a shorthand for ``left_shift`` on
378378
:class:`dpnp.ndarray`.
379379
380380
>>> x1 << x2
@@ -437,7 +437,7 @@ def right_shift(
437437
>>> np.right_shift(x1, x2)
438438
array([5, 2, 1])
439439
440-
The ``>>`` operator can be used as a shorthand for ``left_shift`` on
440+
The ``>>`` operator can be used as a shorthand for ``right_shift`` on
441441
:class:`dpnp.ndarray`.
442442
443443
>>> x1 >> x2

dpnp/dpnp_iface_logic.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def all(x, /, axis=None, out=None, keepdims=False, *, where=True):
9595
Returns
9696
-------
9797
dpnp.ndarray
98-
An array with a data type of `bool`
99-
containing the results of the logical AND reduction.
98+
An array with a data type of `bool`
99+
containing the results of the logical AND reduction.
100100
101101
Limitations
102102
-----------
@@ -160,8 +160,8 @@ def allclose(x1, x2, rtol=1.0e-5, atol=1.0e-8, **kwargs):
160160
161161
Limitations
162162
-----------
163-
Parameters ``x1`` and ``x2`` are supported as either :obj:`dpnp.ndarray` or scalar.
164-
Keyword arguments ``kwargs`` are currently unsupported.
163+
Parameters `x1` and `x2` are supported as either :class:`dpnp.ndarray` or scalar.
164+
Keyword argument `kwargs` is currently unsupported.
165165
Otherwise the functions will be executed sequentially on CPU.
166166
Input array data types are limited by supported DPNP :ref:`Data types`.
167167
@@ -276,7 +276,7 @@ def equal(
276276
Returns
277277
-------
278278
out : dpnp.ndarray
279-
Output array of bool type, element-wise comparison of `x1` and `x2`.
279+
Output array of bool type, element-wise comparison of `x1` and `x2`.
280280
281281
Limitations
282282
-----------
@@ -352,7 +352,7 @@ def greater(
352352
Returns
353353
-------
354354
out : dpnp.ndarray
355-
Output array of bool type, element-wise comparison of `x1` and `x2`.
355+
Output array of bool type, element-wise comparison of `x1` and `x2`.
356356
357357
Limitations
358358
-----------
@@ -422,7 +422,7 @@ def greater_equal(
422422
Returns
423423
-------
424424
out : dpnp.ndarray
425-
Output array of bool type, element-wise comparison of `x1` and `x2`.
425+
Output array of bool type, element-wise comparison of `x1` and `x2`.
426426
427427
Limitations
428428
-----------
@@ -480,8 +480,8 @@ def isclose(x1, x2, rtol=1e-05, atol=1e-08, equal_nan=False):
480480
481481
Limitations
482482
-----------
483-
``x2`` is supported to be integer if ``x1`` is :obj:`dpnp.ndarray` or
484-
at least either ``x1`` or ``x2`` should be as :obj:`dpnp.ndarray`.
483+
`x2` is supported to be integer if `x1` is :class:`dpnp.ndarray` or
484+
at least either `x1` or `x2` should be as :class:`dpnp.ndarray`.
485485
Otherwise the function will be executed sequentially on CPU.
486486
Input array data types are limited by supported DPNP :ref:`Data types`.
487487
@@ -678,7 +678,7 @@ def less(
678678
Returns
679679
-------
680680
out : dpnp.ndarray
681-
Output array of bool type, element-wise comparison of `x1` and `x2`.
681+
Output array of bool type, element-wise comparison of `x1` and `x2`.
682682
683683
Limitations
684684
-----------
@@ -748,7 +748,7 @@ def less_equal(
748748
Returns
749749
-------
750750
out : dpnp.ndarray
751-
Output array of bool type, element-wise comparison of `x1` and `x2`.
751+
Output array of bool type, element-wise comparison of `x1` and `x2`.
752752
753753
Limitations
754754
-----------
@@ -818,8 +818,8 @@ def logical_and(
818818
Returns
819819
-------
820820
out : dpnp.ndarray
821-
Boolean result of the logical AND operation applied to the elements
822-
of `x1` and `x2`; the shape is determined by broadcasting.
821+
Boolean result of the logical AND operation applied to the elements
822+
of `x1` and `x2`; the shape is determined by broadcasting.
823823
824824
Limitations
825825
-----------
@@ -891,8 +891,8 @@ def logical_not(
891891
Returns
892892
-------
893893
out : dpnp.ndarray
894-
Boolean result with the same shape as `x` of the NOT operation
895-
on elements of `x`.
894+
Boolean result with the same shape as `x` of the NOT operation
895+
on elements of `x`.
896896
897897
Limitations
898898
-----------
@@ -953,8 +953,8 @@ def logical_or(
953953
Returns
954954
-------
955955
out : dpnp.ndarray
956-
Boolean result of the logical OR operation applied to the elements
957-
of `x1` and `x2`; the shape is determined by broadcasting.
956+
Boolean result of the logical OR operation applied to the elements
957+
of `x1` and `x2`; the shape is determined by broadcasting.
958958
959959
Limitations
960960
-----------
@@ -1027,8 +1027,8 @@ def logical_xor(
10271027
Returns
10281028
-------
10291029
out : dpnp.ndarray
1030-
Boolean result of the logical XOR operation applied to the elements
1031-
of `x1` and `x2`; the shape is determined by broadcasting.
1030+
Boolean result of the logical XOR operation applied to the elements
1031+
of `x1` and `x2`; the shape is determined by broadcasting.
10321032
10331033
Limitations
10341034
-----------

0 commit comments

Comments
 (0)