Skip to content

Commit 32cfe1e

Browse files
committed
use dpctl.tensor.sign and dpctl.tensor.negative in dpnp
1 parent ab97cf3 commit 32cfe1e

8 files changed

+171
-71
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 2 additions & 6 deletions
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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,15 +1094,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
10941094
fmap[DPNPFuncName::DPNP_FN_NEGATIVE][eft_DBL][eft_DBL] = {
10951095
eft_DBL, (void *)dpnp_negative_c_default<double>};
10961096

1097-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_INT][eft_INT] = {
1098-
eft_INT, (void *)dpnp_negative_c_ext<int32_t>};
1099-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_LNG][eft_LNG] = {
1100-
eft_LNG, (void *)dpnp_negative_c_ext<int64_t>};
1101-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_FLT][eft_FLT] = {
1102-
eft_FLT, (void *)dpnp_negative_c_ext<float>};
1103-
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_DBL][eft_DBL] = {
1104-
eft_DBL, (void *)dpnp_negative_c_ext<double>};
1105-
11061097
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_INT][eft_INT] = {
11071098
eft_INT, (void *)dpnp_recip_c_default<int32_t>};
11081099
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_LNG][eft_LNG] = {
@@ -1130,15 +1121,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
11301121
fmap[DPNPFuncName::DPNP_FN_SIGN][eft_DBL][eft_DBL] = {
11311122
eft_DBL, (void *)dpnp_sign_c_default<double>};
11321123

1133-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_INT][eft_INT] = {
1134-
eft_INT, (void *)dpnp_sign_c_ext<int32_t>};
1135-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_LNG][eft_LNG] = {
1136-
eft_LNG, (void *)dpnp_sign_c_ext<int64_t>};
1137-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_FLT][eft_FLT] = {
1138-
eft_FLT, (void *)dpnp_sign_c_ext<float>};
1139-
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_DBL][eft_DBL] = {
1140-
eft_DBL, (void *)dpnp_sign_c_ext<double>};
1141-
11421124
fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_INT][eft_INT] = {
11431125
eft_INT, (void *)dpnp_square_c_default<int32_t>};
11441126
fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_LNG][eft_LNG] = {

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
162162
DPNP_FN_MODF_EXT
163163
DPNP_FN_NANVAR
164164
DPNP_FN_NANVAR_EXT
165-
DPNP_FN_NEGATIVE
166-
DPNP_FN_NEGATIVE_EXT
167165
DPNP_FN_NONZERO
168166
DPNP_FN_ONES
169167
DPNP_FN_ONES_LIKE
@@ -259,8 +257,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
259257
DPNP_FN_RNG_ZIPF_EXT
260258
DPNP_FN_SEARCHSORTED
261259
DPNP_FN_SEARCHSORTED_EXT
262-
DPNP_FN_SIGN
263-
DPNP_FN_SIGN_EXT
264260
DPNP_FN_SINH
265261
DPNP_FN_SINH_EXT
266262
DPNP_FN_SORT
@@ -429,7 +425,6 @@ cpdef dpnp_descriptor dpnp_maximum(dpnp_descriptor x1_obj, dpnp_descriptor x2_ob
429425
dpnp_descriptor out=*, object where=*)
430426
cpdef dpnp_descriptor dpnp_minimum(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
431427
dpnp_descriptor out=*, object where=*)
432-
cpdef dpnp_descriptor dpnp_negative(dpnp_descriptor array1)
433428
cpdef dpnp_descriptor dpnp_power(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
434429
dpnp_descriptor out=*, object where=*)
435430

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

Lines changed: 0 additions & 10 deletions
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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,42 @@ 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+
# dpctl.tensor only works with usm_ndarray
1422+
x1_usm = dpnp.get_usm_ndarray(x)
1423+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1424+
1425+
res_usm = negative_func(x1_usm, out=out_usm, order=order)
1426+
return dpnp_array._create_from_usm_ndarray(res_usm)
1427+
1428+
13931429
_not_equal_docstring_ = """
13941430
not_equal(x1, x2, out=None, order="K")
13951431
@@ -1525,6 +1561,47 @@ def dpnp_right_shift(x1, x2, out=None, order="K"):
15251561
return dpnp_array._create_from_usm_ndarray(res_usm)
15261562

15271563

1564+
_sign_docstring = """
1565+
sign(x, out=None, order="K")
1566+
1567+
Computes an indication of the sign of each element `x_i` of input array `x`
1568+
using the signum function.
1569+
1570+
The signum function returns `-1` if `x_i` is less than `0`,
1571+
`0` if `x_i` is equal to `0`, and `1` if `x_i` is greater than `0`.
1572+
1573+
Args:
1574+
x (dpnp.ndarray):
1575+
Input array, expected to have numeric data type.
1576+
out ({None, dpnp.ndarray}, optional):
1577+
Output array to populate.
1578+
Array have the correct shape and the expected data type.
1579+
order ("C","F","A","K", optional):
1580+
Memory layout of the newly output array, if parameter `out` is `None`.
1581+
Default: "K".
1582+
Returns:
1583+
dpnp.ndarray:
1584+
An array containing the element-wise results. The data type of the
1585+
returned array is determined by the Type Promotion Rules.
1586+
"""
1587+
1588+
1589+
sign_func = UnaryElementwiseFunc(
1590+
"sign", ti._sign_result_type, ti._sign, _sign_docstring
1591+
)
1592+
1593+
1594+
def dpnp_sign(x, out=None, order="K"):
1595+
"""Invokes sign() from dpctl.tensor implementation for sign() function."""
1596+
1597+
# dpctl.tensor only works with usm_ndarray
1598+
x1_usm = dpnp.get_usm_ndarray(x)
1599+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1600+
1601+
res_usm = sign_func(x1_usm, out=out_usm, order=order)
1602+
return dpnp_array._create_from_usm_ndarray(res_usm)
1603+
1604+
15281605
_sin_docstring = """
15291606
sin(x, out=None, order='K')
15301607
Computes sine for each element `x_i` of input array `x`.

dpnp/dpnp_iface_mathematical.py

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
dpnp_floor,
5858
dpnp_floor_divide,
5959
dpnp_multiply,
60+
dpnp_negative,
6061
dpnp_remainder,
62+
dpnp_sign,
6163
dpnp_subtract,
6264
dpnp_trunc,
6365
)
@@ -1486,39 +1488,64 @@ def nansum(x1, **kwargs):
14861488
return call_origin(numpy.nansum, x1, **kwargs)
14871489

14881490

1489-
def negative(x1, **kwargs):
1491+
def negative(
1492+
x,
1493+
/,
1494+
out=None,
1495+
*,
1496+
order="K",
1497+
where=True,
1498+
dtype=None,
1499+
subok=True,
1500+
**kwargs,
1501+
):
14901502
"""
14911503
Negative element-wise.
14921504
14931505
For full documentation refer to :obj:`numpy.negative`.
14941506
1507+
Returns
1508+
-------
1509+
out : dpnp.ndarray
1510+
The numerical negative of each element of `x`.
1511+
14951512
Limitations
14961513
-----------
1497-
Parameter `x1` is supported as :class:`dpnp.ndarray`.
1514+
Parameters `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
1515+
Parameters `where`, `dtype` and `subok` are supported with their default values.
14981516
Keyword arguments `kwargs` are currently unsupported.
14991517
Otherwise the function will be executed sequentially on CPU.
15001518
Input array data types are limited by supported DPNP :ref:`Data types`.
15011519
15021520
See Also
15031521
--------
1504-
:obj:`dpnp.copysign` : Change the sign of x1 to that of x2, element-wise.
1522+
:obj:`dpnp.copysign` : Change the sign of `x1` to that of `x2`, element-wise.
15051523
15061524
Examples
15071525
--------
15081526
>>> import dpnp as np
1509-
>>> result = np.negative([1, -1])
1510-
>>> [x for x in result]
1511-
[-1, 1]
1527+
>>> np.negative(np.array([1, -1]))
1528+
array([-1, 1])
1529+
1530+
The ``-`` operator can be used as a shorthand for ``negative`` on
1531+
:class:`dpnp.ndarray`.
15121532
1533+
>>> x = np.array([1., -1.])
1534+
>>> -x
1535+
array([-1., 1.])
15131536
"""
15141537

1515-
x1_desc = dpnp.get_dpnp_descriptor(
1516-
x1, copy_when_strides=False, copy_when_nondefault_queue=False
1538+
return check_nd_call_func(
1539+
numpy.negative,
1540+
dpnp_negative,
1541+
x,
1542+
out=out,
1543+
where=where,
1544+
order=order,
1545+
dtype=dtype,
1546+
subok=subok,
1547+
**kwargs,
15171548
)
1518-
if x1_desc and not kwargs:
1519-
return dpnp_negative(x1_desc).get_pyobj()
1520-
1521-
return call_origin(numpy.negative, x1, **kwargs)
15221549

15231550

15241551
def power(x1, x2, /, out=None, *, where=True, dtype=None, subok=True, **kwargs):
@@ -1759,35 +1786,70 @@ def round_(a, decimals=0, out=None):
17591786
return around(a, decimals, out)
17601787

17611788

1762-
def sign(x1, **kwargs):
1789+
def sign(
1790+
x,
1791+
/,
1792+
out=None,
1793+
*,
1794+
order="K",
1795+
where=True,
1796+
dtype=None,
1797+
subok=True,
1798+
**kwargs,
1799+
):
17631800
"""
17641801
Returns an element-wise indication of the sign of a number.
17651802
17661803
For full documentation refer to :obj:`numpy.sign`.
17671804
1805+
Returns
1806+
-------
1807+
out : dpnp.ndarray
1808+
The indication of the sign of each element of `x`.
1809+
17681810
Limitations
17691811
-----------
1770-
Parameter `x1` is supported as :class:`dpnp.ndarray`.
1771-
Keyword arguments `kwargs` are currently unsupported.
1812+
Parameters `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
1813+
Parameters `where`, `dtype` and `subok` are supported with their default values.
1814+
Keyword argument `kwargs` is currently unsupported.
17721815
Otherwise the function will be executed sequentially on CPU.
17731816
Input array data types are limited by supported DPNP :ref:`Data types`.
17741817
17751818
Examples
17761819
--------
17771820
>>> import dpnp as np
1778-
>>> result = np.sign(np.array([-5., 4.5]))
1779-
>>> [x for x in result]
1780-
[-1.0, 1.0]
1781-
1782-
"""
1783-
1784-
x1_desc = dpnp.get_dpnp_descriptor(
1785-
x1, copy_when_strides=False, copy_when_nondefault_queue=False
1786-
)
1787-
if x1_desc and not kwargs:
1788-
return dpnp_sign(x1_desc).get_pyobj()
1789-
1790-
return call_origin(numpy.sign, x1, **kwargs)
1821+
>>> np.sign(np.array([-5., 4.5]))
1822+
array([-1.0, 1.0])
1823+
>>> np.sign(np.array(0))
1824+
array(0)
1825+
>>> np.sign(np.array(5-2j))
1826+
array([1+0j])
1827+
1828+
"""
1829+
1830+
if numpy.iscomplexobj(x):
1831+
return call_origin(
1832+
numpy.sign,
1833+
x,
1834+
out=out,
1835+
where=where,
1836+
order=order,
1837+
dtype=dtype,
1838+
subok=subok,
1839+
**kwargs,
1840+
)
1841+
else:
1842+
return check_nd_call_func(
1843+
numpy.sign,
1844+
dpnp_sign,
1845+
x,
1846+
out=out,
1847+
where=where,
1848+
order=order,
1849+
dtype=dtype,
1850+
subok=subok,
1851+
**kwargs,
1852+
)
17911853

17921854

17931855
def subtract(

dpnp/dpnp_iface_trigonometric.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def cos(
436436
-----------
437437
Parameter `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
438438
Parameters `where`, `dtype` and `subok` are supported with their default values.
439-
Keyword arguments `kwargs` are currently unsupported.
439+
Keyword argument `kwargs` is currently unsupported.
440440
Otherwise the function will be executed sequentially on CPU.
441441
Input array data types are limited by supported DPNP :ref:`Data types`.
442442
@@ -989,7 +989,7 @@ def sin(
989989
-----------
990990
Parameter `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
991991
Parameters `where`, `dtype` and `subok` are supported with their default values.
992-
Keyword arguments `kwargs` are currently unsupported.
992+
Keyword argument `kwargs` is currently unsupported.
993993
Otherwise the function will be executed sequentially on CPU.
994994
Input array data types are limited by supported DPNP :ref:`Data types`.
995995

0 commit comments

Comments
 (0)