Skip to content

Commit

Permalink
Merge pull request #1523 from IntelPython/use_dpctl_sign_negative_in_…
Browse files Browse the repository at this point in the history
…dpnp

use dpctl.tensor.sign and dpctl.tensor.negative in dpnp
  • Loading branch information
vtavana authored Aug 22, 2023
2 parents d159067 + 096857c commit 79ac2b0
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 119 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 @@ -1061,15 +1061,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 @@ -1097,15 +1088,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 @@ -160,8 +160,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 @@ -257,8 +255,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 @@ -427,7 +423,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: 0 additions & 10 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ __all__ += [
"dpnp_nancumsum",
"dpnp_nanprod",
"dpnp_nansum",
"dpnp_negative",
"dpnp_power",
"dpnp_prod",
"dpnp_sign",
"dpnp_sum",
"dpnp_trapz",
]
Expand Down Expand Up @@ -455,10 +453,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 +523,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
88 changes: 88 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,49 @@ 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."""

# TODO: discuss with dpctl if the check is needed to be moved there
if not dpnp.isscalar(x) and x.dtype == dpnp.bool:
raise TypeError(
"DPNP boolean negative, the `-` operator, is not supported, "
"use the `~` operator or the logical_not function instead."
)

# 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 +1568,51 @@ 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."""

# TODO: discuss with dpctl if the check is needed to be moved there
if not dpnp.isscalar(x) and x.dtype == dpnp.bool:
raise TypeError("DPNP boolean sign is not supported.")

# 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
4 changes: 2 additions & 2 deletions dpnp/dpnp_iface_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def left_shift(
>>> np.left_shift(x1, x2)
array([10, 20, 40])
The ``<<`` operator can be used as a shorthand for ``right_shift`` on
The ``<<`` operator can be used as a shorthand for ``left_shift`` on
:class:`dpnp.ndarray`.
>>> x1 << x2
Expand Down Expand Up @@ -437,7 +437,7 @@ def right_shift(
>>> np.right_shift(x1, x2)
array([5, 2, 1])
The ``>>`` operator can be used as a shorthand for ``left_shift`` on
The ``>>`` operator can be used as a shorthand for ``right_shift`` on
:class:`dpnp.ndarray`.
>>> x1 >> x2
Expand Down
38 changes: 19 additions & 19 deletions dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ 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.
An array with a data type of `bool`
containing the results of the logical AND reduction.
Limitations
-----------
Expand Down Expand Up @@ -160,8 +160,8 @@ def allclose(x1, x2, rtol=1.0e-5, atol=1.0e-8, **kwargs):
Limitations
-----------
Parameters ``x1`` and ``x2`` are supported as either :obj:`dpnp.ndarray` or scalar.
Keyword arguments ``kwargs`` are currently unsupported.
Parameters `x1` and `x2` are supported as either :class:`dpnp.ndarray` or scalar.
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`.
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -480,8 +480,8 @@ def isclose(x1, x2, rtol=1e-05, atol=1e-08, equal_nan=False):
Limitations
-----------
``x2`` is supported to be integer if ``x1`` is :obj:`dpnp.ndarray` or
at least either ``x1`` or ``x2`` should be as :obj:`dpnp.ndarray`.
`x2` is supported to be integer if `x1` is :class:`dpnp.ndarray` or
at least either `x1` or `x2` should be as :class:`dpnp.ndarray`.
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 @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
-----------
Expand Down
Loading

0 comments on commit 79ac2b0

Please sign in to comment.