Skip to content

Leverage dpctl.tensor.all() and dpctl.tensor.any() implementations #1512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ env:
test_dparray.py
test_fft.py
test_linalg.py
test_logic.py
test_mathematical.py
test_random_state.py
test_sort.py
test_special.py
test_umath.py
test_usm_type.py
third_party/cupy/linalg_tests/test_product.py
third_party/cupy/logic_tests/test_truth.py
third_party/cupy/manipulation_tests/test_join.py
third_party/cupy/math_tests/test_explog.py
third_party/cupy/math_tests/test_misc.py
Expand Down
14 changes: 6 additions & 8 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ enum class DPNPFuncName : size_t
DPNP_FN_ADD, /**< Used in numpy.add() impl */
DPNP_FN_ADD_EXT, /**< Used in numpy.add() impl, requires extra parameters */
DPNP_FN_ALL, /**< Used in numpy.all() impl */
DPNP_FN_ALL_EXT, /**< Used in numpy.all() impl, requires extra parameters */
DPNP_FN_ALLCLOSE, /**< Used in numpy.allclose() impl */
DPNP_FN_ALLCLOSE_EXT, /**< Used in numpy.allclose() impl, requires extra
parameters */
DPNP_FN_ANY, /**< Used in numpy.any() impl */
DPNP_FN_ANY_EXT, /**< Used in numpy.any() impl, requires extra parameters */
DPNP_FN_ARANGE, /**< Used in numpy.arange() impl */
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() impl */
DPNP_FN_ALLCLOSE, /**< Used in numpy.allclose() impl */
DPNP_FN_ALLCLOSE_EXT, /**< Used in numpy.allclose() impl, requires extra
parameters */
DPNP_FN_ANY, /**< Used in numpy.any() impl */
DPNP_FN_ARANGE, /**< Used in numpy.arange() impl */
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() impl */
DPNP_FN_ARCCOS_EXT, /**< Used in numpy.arccos() impl, requires extra
parameters */
DPNP_FN_ARCCOSH, /**< Used in numpy.arccosh() impl */
Expand Down
30 changes: 0 additions & 30 deletions dpnp/backend/kernels/dpnp_krnl_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,21 +640,6 @@ void func_map_init_logic(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_ALL][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_all_default_c<double, bool>};

fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_BLN][eft_BLN] = {
eft_BLN, (void *)dpnp_all_ext_c<bool, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_all_ext_c<int32_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_all_ext_c<int64_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_all_ext_c<float, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_all_ext_c<double, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_C64][eft_C64] = {
eft_C64, (void *)dpnp_all_ext_c<std::complex<float>, bool>};
fmap[DPNPFuncName::DPNP_FN_ALL_EXT][eft_C128][eft_C128] = {
eft_C128, (void *)dpnp_all_ext_c<std::complex<double>, bool>};

fmap[DPNPFuncName::DPNP_FN_ALLCLOSE][eft_INT][eft_INT] = {
eft_BLN, (void *)dpnp_allclose_default_c<int32_t, int32_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ALLCLOSE][eft_LNG][eft_INT] = {
Expand Down Expand Up @@ -732,21 +717,6 @@ void func_map_init_logic(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_ANY][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_any_default_c<double, bool>};

fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_BLN][eft_BLN] = {
eft_BLN, (void *)dpnp_any_ext_c<bool, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_any_ext_c<int32_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_any_ext_c<int64_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_any_ext_c<float, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_any_ext_c<double, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_C64][eft_C64] = {
eft_C64, (void *)dpnp_any_ext_c<std::complex<float>, bool>};
fmap[DPNPFuncName::DPNP_FN_ANY_EXT][eft_C128][eft_C128] = {
eft_C128, (void *)dpnp_any_ext_c<std::complex<double>, bool>};

func_map_logic_2arg_2type_helper<eft_BLN, eft_INT, eft_LNG, eft_FLT,
eft_DBL>(fmap);

Expand Down
4 changes: 0 additions & 4 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
cdef enum DPNPFuncName "DPNPFuncName":
DPNP_FN_ABSOLUTE
DPNP_FN_ABSOLUTE_EXT
DPNP_FN_ALL
DPNP_FN_ALL_EXT
DPNP_FN_ALLCLOSE
DPNP_FN_ALLCLOSE_EXT
DPNP_FN_ANY
DPNP_FN_ANY_EXT
DPNP_FN_ARANGE
DPNP_FN_ARCCOS
DPNP_FN_ARCCOS_EXT
Expand Down
63 changes: 0 additions & 63 deletions dpnp/dpnp_algo/dpnp_algo_logic.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ and the rest of the library
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file

__all__ += [
"dpnp_all",
"dpnp_allclose",
"dpnp_any",
"dpnp_isclose",
]


ctypedef c_dpctl.DPCTLSyclEventRef(*custom_logic_1in_1out_func_ptr_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , const size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*custom_allclose_1in_1out_func_ptr_t)(c_dpctl.DPCTLSyclQueueRef,
void * ,
void * ,
Expand All @@ -56,35 +51,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_allclose_1in_1out_func_ptr_t)(c_dpctl
const c_dpctl.DPCTLEventVectorRef)


cpdef utils.dpnp_descriptor dpnp_all(utils.dpnp_descriptor array1):
array1_obj = array1.get_array()

cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py((1,),
dpnp.bool,
None,
device=array1_obj.sycl_device,
usm_type=array1_obj.usm_type,
sycl_queue=array1_obj.sycl_queue)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(array1.dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ALL_EXT, param1_type, param1_type)

cdef custom_logic_1in_1out_func_ptr_t func = <custom_logic_1in_1out_func_ptr_t > kernel_data.ptr

cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, array1.get_data(), result.get_data(), array1.size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_allclose(utils.dpnp_descriptor array1,
utils.dpnp_descriptor array2,
double rtol_val,
Expand Down Expand Up @@ -125,35 +91,6 @@ cpdef utils.dpnp_descriptor dpnp_allclose(utils.dpnp_descriptor array1,
return result


cpdef utils.dpnp_descriptor dpnp_any(utils.dpnp_descriptor array1):
array1_obj = array1.get_array()

cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py((1,),
dpnp.bool,
None,
device=array1_obj.sycl_device,
usm_type=array1_obj.usm_type,
sycl_queue=array1_obj.sycl_queue)

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(array1.dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ANY_EXT, param1_type, param1_type)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef custom_logic_1in_1out_func_ptr_t func = <custom_logic_1in_1out_func_ptr_t > kernel_data.ptr

cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, array1.get_data(), result.get_data(), array1.size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_isclose(utils.dpnp_descriptor input1,
utils.dpnp_descriptor input2,
double rtol=1e-05,
Expand Down
124 changes: 70 additions & 54 deletions dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"""


import dpctl.tensor as dpt
import numpy

import dpnp
from dpnp.dpnp_algo import *
from dpnp.dpnp_array import dpnp_array
from dpnp.dpnp_utils import *

from .dpnp_algo.dpnp_elementwise_common import (
Expand Down Expand Up @@ -84,24 +86,29 @@
]


def all(x1, /, axis=None, out=None, keepdims=False, *, where=True):
def all(x, /, axis=None, out=None, keepdims=False, *, where=True):
"""
Test whether all array elements along a given axis evaluate to True.

For full documentation refer to :obj:`numpy.all`.

Returns
-------
dpnp.ndarray
An array with a data type of `bool`
containing the results of the logical AND reduction.

Limitations
-----------
Input array is supported as :obj:`dpnp.ndarray`.
Otherwise the function will be executed sequentially on CPU.
Parameters `x` is supported either as :class:`dpnp.ndarray`
or :class:`dpctl.tensor.usm_ndarray`.
Parameters `out` and `where` are supported with default value.
Input array data types are limited by supported DPNP :ref:`Data types`.
Parameter `axis` is supported only with default value `None`.
Parameter `out` is supported only with default value `None`.
Parameter `keepdims` is supported only with default value `False`.
Parameter `where` is supported only with default value `True`.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
:obj:`dpnp.ndarray.all` : equivalent method
:obj:`dpnp.any` : Test whether any element along a given axis evaluates to True.

Notes
Expand All @@ -111,35 +118,37 @@ def all(x1, /, axis=None, out=None, keepdims=False, *, where=True):

Examples
--------
>>> import dpnp as dp
>>> x = dp.array([[True, False], [True, True]])
>>> dp.all(x)
False
>>> x2 = dp.array([-1, 4, 5])
>>> dp.all(x2)
True
>>> x3 = dp.array([1.0, dp.nan])
>>> dp.all(x3)
True
>>> import dpnp as np
>>> x = np.array([[True, False], [True, True]])
>>> np.all(x)
array(False)

>>> np.all(x, axis=0)
array([ True, False])

>>> x2 = np.array([-1, 4, 5])
>>> np.all(x2)
array(True)

>>> x3 = np.array([1.0, np.nan])
>>> np.all(x3)
array(True)

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if axis is not None:
pass
elif out is not None:
pass
elif keepdims is not False:
if dpnp.is_supported_array_type(x):
if out is not None:
pass
elif where is not True:
pass
else:
result_obj = dpnp_all(x1_desc).get_pyobj()
return dpnp.convert_single_elem_array_to_scalar(result_obj)
dpt_array = dpnp.get_usm_ndarray(x)
return dpnp_array._create_from_usm_ndarray(
dpt.all(dpt_array, axis=axis, keepdims=keepdims)
)

return call_origin(
numpy.all, x1, axis=axis, out=out, keepdims=keepdims, where=where
numpy.all, x, axis=axis, out=out, keepdims=keepdims, where=where
)


Expand Down Expand Up @@ -181,24 +190,29 @@ def allclose(x1, x2, rtol=1.0e-5, atol=1.0e-8, **kwargs):
return call_origin(numpy.allclose, x1, x2, rtol=rtol, atol=atol, **kwargs)


def any(x1, /, axis=None, out=None, keepdims=False, *, where=True):
def any(x, /, axis=None, out=None, keepdims=False, *, where=True):
"""
Test whether any array element along a given axis evaluates to True.

For full documentation refer to :obj:`numpy.any`.

Returns
-------
dpnp.ndarray
An array with a data type of `bool`
containing the results of the logical OR reduction.

Limitations
-----------
Input array is supported as :obj:`dpnp.ndarray`.
Otherwise the function will be executed sequentially on CPU.
Parameters `x` is supported either as :class:`dpnp.ndarray`
or :class:`dpctl.tensor.usm_ndarray`.
Parameters `out` and `where` are supported with default value.
Input array data types are limited by supported DPNP :ref:`Data types`.
Parameter `axis` is supported only with default value `None`.
Parameter `out` is supported only with default value `None`.
Parameter `keepdims` is supported only with default value `False`.
Parameter `where` is supported only with default value `True`.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
:obj:`dpnp.ndarray.any` : equivalent method
:obj:`dpnp.all` : Test whether all elements along a given axis evaluate to True.

Notes
Expand All @@ -208,35 +222,37 @@ def any(x1, /, axis=None, out=None, keepdims=False, *, where=True):

Examples
--------
>>> import dpnp as dp
>>> x = dp.array([[True, False], [True, True]])
>>> dp.any(x)
True
>>> x2 = dp.array([0, 0, 0])
>>> dp.any(x2)
False
>>> x3 = dp.array([1.0, dp.nan])
>>> dp.any(x3)
True
>>> import dpnp as np
>>> x = np.array([[True, False], [True, True]])
>>> np.any(x)
array(True)

>>> np.any(x, axis=0)
array([ True, True])

>>> x2 = np.array([0, 0, 0])
>>> np.any(x2)
array(False)

>>> x3 = np.array([1.0, np.nan])
>>> np.any(x3)
array(True)

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if axis is not None:
pass
elif out is not None:
pass
elif keepdims is not False:
if dpnp.is_supported_array_type(x):
if out is not None:
pass
elif where is not True:
pass
else:
result_obj = dpnp_any(x1_desc).get_pyobj()
return dpnp.convert_single_elem_array_to_scalar(result_obj)
dpt_array = dpnp.get_usm_ndarray(x)
return dpnp_array._create_from_usm_ndarray(
dpt.any(dpt_array, axis=axis, keepdims=keepdims)
)

return call_origin(
numpy.any, x1, axis=axis, out=out, keepdims=keepdims, where=where
numpy.any, x, axis=axis, out=out, keepdims=keepdims, where=where
)


Expand Down
Loading