Skip to content

Commit

Permalink
use dpctl.tensor.sign and dpctl.tensor.negative in dpnp
Browse files Browse the repository at this point in the history
  • Loading branch information
vtavana committed Aug 19, 2023
1 parent ab97cf3 commit f171c56
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 73 deletions.
8 changes: 2 additions & 6 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ enum class DPNPFuncName : size_t
DPNP_FN_NANVAR_EXT, /**< Used in numpy.nanvar() impl, requires extra
parameters */
DPNP_FN_NEGATIVE, /**< Used in numpy.negative() impl */
DPNP_FN_NEGATIVE_EXT, /**< Used in numpy.negative() impl, requires extra
parameters */
DPNP_FN_NONZERO, /**< Used in numpy.nonzero() impl */
DPNP_FN_NOT_EQUAL_EXT, /**< Used in numpy.not_equal() impl, requires extra
parameters */
Expand Down Expand Up @@ -441,10 +439,8 @@ enum class DPNPFuncName : size_t
DPNP_FN_SEARCHSORTED_EXT, /**< Used in numpy.searchsorted() impl, requires
extra parameters */
DPNP_FN_SIGN, /**< Used in numpy.sign() impl */
DPNP_FN_SIGN_EXT, /**< Used in numpy.sign() impl, requires extra parameters
*/
DPNP_FN_SIN, /**< Used in numpy.sin() impl */
DPNP_FN_SINH, /**< Used in numpy.sinh() impl */
DPNP_FN_SIN, /**< Used in numpy.sin() impl */
DPNP_FN_SINH, /**< Used in numpy.sinh() impl */
DPNP_FN_SINH_EXT, /**< Used in numpy.sinh() impl, requires extra parameters
*/
DPNP_FN_SORT, /**< Used in numpy.sort() impl */
Expand Down
18 changes: 0 additions & 18 deletions dpnp/backend/kernels/dpnp_krnl_elemwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,15 +1094,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_NEGATIVE][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_negative_c_default<double>};

fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_negative_c_ext<int32_t>};
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_negative_c_ext<int64_t>};
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_negative_c_ext<float>};
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_negative_c_ext<double>};

fmap[DPNPFuncName::DPNP_FN_RECIP][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_recip_c_default<int32_t>};
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_LNG][eft_LNG] = {
Expand Down Expand Up @@ -1130,15 +1121,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_SIGN][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_sign_c_default<double>};

fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_sign_c_ext<int32_t>};
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_sign_c_ext<int64_t>};
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_sign_c_ext<float>};
fmap[DPNPFuncName::DPNP_FN_SIGN_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_sign_c_ext<double>};

fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_square_c_default<int32_t>};
fmap[DPNPFuncName::DPNP_FN_SQUARE][eft_LNG][eft_LNG] = {
Expand Down
5 changes: 0 additions & 5 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_MODF_EXT
DPNP_FN_NANVAR
DPNP_FN_NANVAR_EXT
DPNP_FN_NEGATIVE
DPNP_FN_NEGATIVE_EXT
DPNP_FN_NONZERO
DPNP_FN_ONES
DPNP_FN_ONES_LIKE
Expand Down Expand Up @@ -259,8 +257,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_RNG_ZIPF_EXT
DPNP_FN_SEARCHSORTED
DPNP_FN_SEARCHSORTED_EXT
DPNP_FN_SIGN
DPNP_FN_SIGN_EXT
DPNP_FN_SINH
DPNP_FN_SINH_EXT
DPNP_FN_SORT
Expand Down Expand Up @@ -429,7 +425,6 @@ cpdef dpnp_descriptor dpnp_maximum(dpnp_descriptor x1_obj, dpnp_descriptor x2_ob
dpnp_descriptor out=*, object where=*)
cpdef dpnp_descriptor dpnp_minimum(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
dpnp_descriptor out=*, object where=*)
cpdef dpnp_descriptor dpnp_negative(dpnp_descriptor array1)
cpdef dpnp_descriptor dpnp_power(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
dpnp_descriptor out=*, object where=*)

Expand Down
10 changes: 1 addition & 9 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ __all__ += [
"dpnp_nancumsum",
"dpnp_nanprod",
"dpnp_nansum",
"dpnp_negative",
"dpnp_power",
"dpnp_prod",
"dpnp_remainder",
"dpnp_sign",
"dpnp_sum",
"dpnp_trapz",
Expand Down Expand Up @@ -455,10 +455,6 @@ cpdef utils.dpnp_descriptor dpnp_nansum(utils.dpnp_descriptor x1):
return dpnp_sum(result)


cpdef utils.dpnp_descriptor dpnp_negative(dpnp_descriptor x1):
return call_fptr_1in_1out_strides(DPNP_FN_NEGATIVE_EXT, x1)


cpdef utils.dpnp_descriptor dpnp_power(utils.dpnp_descriptor x1_obj,
utils.dpnp_descriptor x2_obj,
object dtype=None,
Expand Down Expand Up @@ -529,10 +525,6 @@ cpdef utils.dpnp_descriptor dpnp_prod(utils.dpnp_descriptor x1,
return result


cpdef utils.dpnp_descriptor dpnp_sign(utils.dpnp_descriptor x1):
return call_fptr_1in_1out_strides(DPNP_FN_SIGN_EXT, x1)


cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor x1,
object axis=None,
object dtype=None,
Expand Down
77 changes: 77 additions & 0 deletions dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,42 @@ def dpnp_multiply(x1, x2, out=None, order="K"):
return dpnp_array._create_from_usm_ndarray(res_usm)


_negative_docstring = """
negative(x, out=None, order="K")
Computes the numerical negative for each element `x_i` of input array `x`.
Args:
x (dpnp.ndarray):
Input array, expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
An array containing the negative of `x`.
"""


negative_func = UnaryElementwiseFunc(
"negative", ti._negative_result_type, ti._negative, _negative_docstring
)


def dpnp_negative(x, out=None, order="K"):
"""Invokes negative() from dpctl.tensor implementation for negative() function."""

# dpctl.tensor only works with usm_ndarray
x1_usm = dpnp.get_usm_ndarray(x)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

res_usm = negative_func(x1_usm, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_not_equal_docstring_ = """
not_equal(x1, x2, out=None, order="K")
Expand Down Expand Up @@ -1525,6 +1561,47 @@ def dpnp_right_shift(x1, x2, out=None, order="K"):
return dpnp_array._create_from_usm_ndarray(res_usm)


_sign_docstring = """
sign(x, out=None, order="K")
Computes an indication of the sign of each element `x_i` of input array `x`
using the signum function.
The signum function returns `-1` if `x_i` is less than `0`,
`0` if `x_i` is equal to `0`, and `1` if `x_i` is greater than `0`.
Args:
x (dpnp.ndarray):
Input array, expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
An array containing the element-wise results. The data type of the
returned array is determined by the Type Promotion Rules.
"""


sign_func = UnaryElementwiseFunc(
"sign", ti._sign_result_type, ti._sign, _sign_docstring
)


def dpnp_sign(x, out=None, order="K"):
"""Invokes sign() from dpctl.tensor implementation for sign() function."""

# dpctl.tensor only works with usm_ndarray
x1_usm = dpnp.get_usm_ndarray(x)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

res_usm = sign_func(x1_usm, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_sin_docstring = """
sin(x, out=None, order='K')
Computes sine for each element `x_i` of input array `x`.
Expand Down
124 changes: 93 additions & 31 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
dpnp_floor_divide,
dpnp_multiply,
dpnp_remainder,
dpnp_negative,
dpnp_sign,
dpnp_subtract,
dpnp_trunc,
)
Expand Down Expand Up @@ -1486,39 +1488,64 @@ def nansum(x1, **kwargs):
return call_origin(numpy.nansum, x1, **kwargs)


def negative(x1, **kwargs):
def negative(
x,
/,
out=None,
*,
order="K",
where=True,
dtype=None,
subok=True,
**kwargs,
):
"""
Negative element-wise.
For full documentation refer to :obj:`numpy.negative`.
Returns
-------
out : dpnp.ndarray
The numerical negative of each element of `x`.
Limitations
-----------
Parameter `x1` is supported as :class:`dpnp.ndarray`.
Parameters `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 arguments `kwargs` are currently unsupported.
Otherwise the function will be executed sequentially on CPU.
Otherwise the functions will be executed sequentially on CPU.
Input array data types are limited by supported DPNP :ref:`Data types`.
See Also
--------
:obj:`dpnp.copysign` : Change the sign of x1 to that of x2, element-wise.
-----------
:obj:`dpnp.copysign` : Change the sign of `x1` to that of `x2`, element-wise.
Examples
--------
>>> import dpnp as np
>>> result = np.negative([1, -1])
>>> [x for x in result]
[-1, 1]
>>> np.negative(np.array([1, -1]))
array([-1, 1])
The ``-`` operator can be used as a shorthand for ``negative`` on
:class:`dpnp.ndarray`.
>>> x = np.array([1., -1.])
>>> -x
array([-1., 1.])
"""

x1_desc = dpnp.get_dpnp_descriptor(
x1, copy_when_strides=False, copy_when_nondefault_queue=False
return check_nd_call_func(
numpy.negative,
dpnp_negative,
x,
out=out,
where=where,
order=order,
dtype=dtype,
subok=subok,
**kwargs,
)
if x1_desc and not kwargs:
return dpnp_negative(x1_desc).get_pyobj()

return call_origin(numpy.negative, x1, **kwargs)


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


def sign(x1, **kwargs):
def sign(
x,
/,
out=None,
*,
order="K",
where=True,
dtype=None,
subok=True,
**kwargs,
):
"""
Returns an element-wise indication of the sign of a number.
For full documentation refer to :obj:`numpy.sign`.
Returns
-------
out : dpnp.ndarray
The indication of the sign of each element of `x`.
Limitations
-----------
Parameter `x1` is supported as :class:`dpnp.ndarray`.
Keyword arguments `kwargs` are currently unsupported.
Otherwise the function will be executed sequentially on CPU.
Parameters `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 functions will be executed sequentially on CPU.
Input array data types are limited by supported DPNP :ref:`Data types`.
Examples
--------
>>> import dpnp as np
>>> result = np.sign(np.array([-5., 4.5]))
>>> [x for x in result]
[-1.0, 1.0]
"""

x1_desc = dpnp.get_dpnp_descriptor(
x1, copy_when_strides=False, copy_when_nondefault_queue=False
)
if x1_desc and not kwargs:
return dpnp_sign(x1_desc).get_pyobj()

return call_origin(numpy.sign, x1, **kwargs)
>>> np.sign(np.array([-5., 4.5]))
array([-1.0, 1.0])
>>> np.sign(np.array(0))
array(0)
>>> np.sign(np.array(5-2j))
array([1+0j])
"""

if numpy.iscomplexobj(x):
return call_origin(
numpy.sign,
x,
out=out,
where=where,
order=order,
dtype=dtype,
subok=subok,
**kwargs,
)
else:
return check_nd_call_func(
numpy.sign,
dpnp_sign,
x,
out=out,
where=where,
order=order,
dtype=dtype,
subok=subok,
**kwargs,
)


def subtract(
Expand Down
4 changes: 2 additions & 2 deletions dpnp/dpnp_iface_trigonometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def cos(
-----------
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 arguments `kwargs` are currently unsupported.
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`.
Expand Down Expand Up @@ -989,7 +989,7 @@ def sin(
-----------
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 arguments `kwargs` are currently unsupported.
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`.
Expand Down
Loading

0 comments on commit f171c56

Please sign in to comment.