Skip to content
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

impelment dpnp.std, dpnp.var and dpnp.nanvar #1635

Merged
merged 15 commits into from
Dec 16, 2023
34 changes: 15 additions & 19 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ enum class DPNPFuncName : size_t
DPNP_FN_MULTIPLY_EXT, /**< Used in numpy.multiply() impl, requires extra
parameters */
DPNP_FN_NANVAR, /**< Used in numpy.nanvar() impl */
DPNP_FN_NANVAR_EXT, /**< Used in numpy.nanvar() impl, requires extra
parameters */
DPNP_FN_NEGATIVE, /**< Used in numpy.negative() impl */
DPNP_FN_NONZERO, /**< Used in numpy.nonzero() impl */
DPNP_FN_ONES, /**< Used in numpy.ones() impl */
Expand Down Expand Up @@ -374,8 +372,7 @@ enum class DPNPFuncName : size_t
*/
DPNP_FN_SQUARE, /**< Used in numpy.square() impl */
DPNP_FN_STD, /**< Used in numpy.std() impl */
DPNP_FN_STD_EXT, /**< Used in numpy.std() impl, requires extra parameters */
DPNP_FN_SUBTRACT, /**< Used in numpy.subtract() impl */
DPNP_FN_SUBTRACT, /**< Used in numpy.subtract() impl */
DPNP_FN_SUBTRACT_EXT, /**< Used in numpy.subtract() impl, requires extra
parameters */
DPNP_FN_SUM, /**< Used in numpy.sum() impl */
Expand All @@ -386,21 +383,20 @@ enum class DPNPFuncName : size_t
DPNP_FN_TAKE, /**< Used in numpy.take() impl */
DPNP_FN_TAN, /**< Used in numpy.tan() impl */
DPNP_FN_TANH, /**< Used in numpy.tanh() impl */
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */
DPNP_FN_TRACE, /**< Used in numpy.trace() impl */
DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra
parameters */
DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */
DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra
parameters */
DPNP_FN_TRI, /**< Used in numpy.tri() impl */
DPNP_FN_TRIL, /**< Used in numpy.tril() impl */
DPNP_FN_TRIU, /**< Used in numpy.triu() impl */
DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */
DPNP_FN_VANDER, /**< Used in numpy.vander() impl */
DPNP_FN_VAR, /**< Used in numpy.var() impl */
DPNP_FN_VAR_EXT, /**< Used in numpy.var() impl, requires extra parameters */
DPNP_FN_ZEROS, /**< Used in numpy.zeros() impl */
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */
DPNP_FN_TRACE, /**< Used in numpy.trace() impl */
DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra
parameters */
DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */
DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra
parameters */
DPNP_FN_TRI, /**< Used in numpy.tri() impl */
DPNP_FN_TRIL, /**< Used in numpy.tril() impl */
DPNP_FN_TRIU, /**< Used in numpy.triu() impl */
DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */
DPNP_FN_VANDER, /**< Used in numpy.vander() impl */
DPNP_FN_VAR, /**< Used in numpy.var() impl */
DPNP_FN_ZEROS, /**< Used in numpy.zeros() impl */
DPNP_FN_ZEROS_LIKE, /**< Used in numpy.zeros_like() impl */
DPNP_FN_LAST, /**< The latest element of the enumeration */
};
Expand Down
61 changes: 0 additions & 61 deletions dpnp/backend/kernels/dpnp_krnl_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,16 +939,6 @@ template <typename _DataType>
void (*dpnp_nanvar_default_c)(void *, void *, void *, const size_t, size_t) =
dpnp_nanvar_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_nanvar_ext_c)(DPCTLSyclQueueRef,
void *,
void *,
void *,
const size_t,
size_t,
const DPCTLEventVectorRef) =
dpnp_nanvar_c<_DataType>;

template <typename _DataType, typename _ResultType>
DPCTLSyclEventRef dpnp_std_c(DPCTLSyclQueueRef q_ref,
void *array1_in,
Expand Down Expand Up @@ -1039,18 +1029,6 @@ void (*dpnp_std_default_c)(void *,
size_t,
size_t) = dpnp_std_c<_DataType, _ResultType>;

template <typename _DataType, typename _ResultType>
DPCTLSyclEventRef (*dpnp_std_ext_c)(DPCTLSyclQueueRef,
void *,
void *,
const shape_elem_type *,
size_t,
const shape_elem_type *,
size_t,
size_t,
const DPCTLEventVectorRef) =
dpnp_std_c<_DataType, _ResultType>;

template <typename _DataType, typename _ResultType>
class dpnp_var_c_kernel;

Expand Down Expand Up @@ -1150,18 +1128,6 @@ void (*dpnp_var_default_c)(void *,
size_t,
size_t) = dpnp_var_c<_DataType, _ResultType>;

template <typename _DataType, typename _ResultType>
DPCTLSyclEventRef (*dpnp_var_ext_c)(DPCTLSyclQueueRef,
void *,
void *,
const shape_elem_type *,
size_t,
const shape_elem_type *,
size_t,
size_t,
const DPCTLEventVectorRef) =
dpnp_var_c<_DataType, _ResultType>;

void func_map_init_statistics(func_map_t &fmap)
{
fmap[DPNPFuncName::DPNP_FN_CORRELATE][eft_INT][eft_INT] = {
Expand Down Expand Up @@ -1316,15 +1282,6 @@ void func_map_init_statistics(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_NANVAR][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_nanvar_default_c<double>};

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

fmap[DPNPFuncName::DPNP_FN_STD][eft_INT][eft_INT] = {
eft_DBL, (void *)dpnp_std_default_c<int32_t, double>};
fmap[DPNPFuncName::DPNP_FN_STD][eft_LNG][eft_LNG] = {
Expand All @@ -1334,15 +1291,6 @@ void func_map_init_statistics(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_STD][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_std_default_c<double, double>};

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

fmap[DPNPFuncName::DPNP_FN_VAR][eft_INT][eft_INT] = {
eft_DBL, (void *)dpnp_var_default_c<int32_t, double>};
fmap[DPNPFuncName::DPNP_FN_VAR][eft_LNG][eft_LNG] = {
Expand All @@ -1352,14 +1300,5 @@ void func_map_init_statistics(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_VAR][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_var_default_c<double, double>};

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

return;
}
6 changes: 0 additions & 6 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_MINIMUM_EXT
DPNP_FN_MODF
DPNP_FN_MODF_EXT
DPNP_FN_NANVAR
DPNP_FN_NANVAR_EXT
DPNP_FN_NONZERO
DPNP_FN_ONES
DPNP_FN_ONES_LIKE
Expand Down Expand Up @@ -187,8 +185,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_SEARCHSORTED_EXT
DPNP_FN_SORT
DPNP_FN_SORT_EXT
DPNP_FN_STD
DPNP_FN_STD_EXT
DPNP_FN_SUM
DPNP_FN_SUM_EXT
DPNP_FN_SVD
Expand All @@ -202,8 +198,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_TRIL_EXT
DPNP_FN_TRIU
DPNP_FN_TRIU_EXT
DPNP_FN_VAR
DPNP_FN_VAR_EXT
DPNP_FN_ZEROS
DPNP_FN_ZEROS_LIKE

Expand Down
113 changes: 0 additions & 113 deletions dpnp/dpnp_algo/dpnp_algo_statistics.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -39,78 +39,15 @@ __all__ += [
"dpnp_average",
"dpnp_correlate",
"dpnp_median",
"dpnp_nanvar",
"dpnp_std",
"dpnp_var",
]


# C function pointer to the C library template functions
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_custom_cov_1in_1out_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , size_t, size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_custom_nanvar_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , void * , size_t, size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_custom_std_var_1in_1out_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , shape_elem_type * , size_t,
shape_elem_type * , size_t, size_t,
const c_dpctl.DPCTLEventVectorRef)

# C function pointer to the C library template functions
ctypedef c_dpctl.DPCTLSyclEventRef(*custom_statistic_1in_1out_func_ptr_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , shape_elem_type * , size_t,
shape_elem_type * , size_t,
const c_dpctl.DPCTLEventVectorRef)

cdef utils.dpnp_descriptor call_fptr_custom_std_var_1in_1out(DPNPFuncName fptr_name, utils.dpnp_descriptor x1, ddof):
cdef shape_type_c x1_shape = x1.shape

""" Convert string type names (array.dtype) to C enum DPNPFuncType """
cdef DPNPFuncType param_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)

""" get the FPTR data structure """
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(fptr_name, param_type, DPNP_FT_NONE)

x1_obj = x1.get_array()

# create result array with type given by FPTR data
cdef shape_type_c result_shape = (1,)
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
kernel_data.return_type,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_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 fptr_custom_std_var_1in_1out_t func = <fptr_custom_std_var_1in_1out_t > kernel_data.ptr

# stub for interface support
cdef shape_type_c axis
cdef Py_ssize_t axis_size = 0

""" Call FPTR function """
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
x1.get_data(),
result.get_data(),
x1_shape.data(),
x1.ndim,
axis.data(),
axis_size,
ddof,
NULL) # dep_events_ref

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


return result


cpdef dpnp_average(utils.dpnp_descriptor x1):
array_sum = dpnp_sum(x1).get_pyobj()
Expand Down Expand Up @@ -207,53 +144,3 @@ cpdef utils.dpnp_descriptor dpnp_median(utils.dpnp_descriptor array1):
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_nanvar(utils.dpnp_descriptor arr, ddof):
# dpnp_isnan does not support USM array as input in comparison to dpnp.isnan
cdef utils.dpnp_descriptor mask_arr = dpnp.get_dpnp_descriptor(dpnp.isnan(arr.get_pyobj()),
copy_when_nondefault_queue=False)
n = dpnp.count_nonzero(mask_arr.get_pyobj())
res_size = int(arr.size - n)
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(arr.dtype)

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

arr_obj = arr.get_array()

# create result array with type given by FPTR data
cdef shape_type_c result_shape = utils._object_to_tuple(res_size)
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
kernel_data.return_type,
None,
device=arr_obj.sycl_device,
usm_type=arr_obj.usm_type,
sycl_queue=arr_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 fptr_custom_nanvar_t func = <fptr_custom_nanvar_t > kernel_data.ptr

cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
arr.get_data(),
mask_arr.get_data(),
result.get_data(),
result.size,
arr.size,
NULL) # dep_events_ref

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

return call_fptr_custom_std_var_1in_1out(DPNP_FN_VAR_EXT, result, ddof)


cpdef utils.dpnp_descriptor dpnp_std(utils.dpnp_descriptor a, size_t ddof):
return call_fptr_custom_std_var_1in_1out(DPNP_FN_STD_EXT, a, ddof)


cpdef utils.dpnp_descriptor dpnp_var(utils.dpnp_descriptor a, size_t ddof):
return call_fptr_custom_std_var_1in_1out(DPNP_FN_VAR_EXT, a, ddof)
52 changes: 28 additions & 24 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,15 +1167,23 @@ def squeeze(self, axis=None):

return dpnp.squeeze(self, axis)

def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""Returns the variance of the array elements, along given axis.

.. seealso::
:obj:`dpnp.var` for full documentation,
def std(
self,
axis=None,
dtype=None,
out=None,
ddof=0,
keepdims=False,
*,
where=True,
):
"""
Returns the standard deviation of the array elements, along given axis.

Refer to :obj:`dpnp.std` for full documentation.
"""

return dpnp.std(self, axis, dtype, out, ddof, keepdims)
return dpnp.std(self, axis, dtype, out, ddof, keepdims, where=where)

@property
def strides(self):
Expand Down Expand Up @@ -1207,10 +1215,7 @@ def sum(
"""
Returns the sum along a given axis.

.. seealso::
:obj:`dpnp.sum` for full documentation,
:meth:`dpnp.dparray.sum`

For full documentation refer to :obj:`dpnp.sum`.
"""

return dpnp.sum(
Expand Down Expand Up @@ -1307,23 +1312,22 @@ def transpose(self, *axes):
res._array_obj = dpt.permute_dims(self._array_obj, axes)
return res

def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
def var(
self,
axis=None,
dtype=None,
out=None,
ddof=0,
keepdims=False,
*,
where=True,
):
"""
Returns the variance of the array elements along given axis.

Masked entries are ignored, and result elements which are not
finite will be masked.

Refer to `numpy.var` for full documentation.

See Also
--------
:obj:`numpy.ndarray.var` : corresponding function for ndarrays
:obj:`numpy.var` : Equivalent function
Returns the variance of the array elements, along given axis.

Refer to :obj:`dpnp.var` for full documentation.
"""

return dpnp.var(self, axis, dtype, out, ddof, keepdims)
return dpnp.var(self, axis, dtype, out, ddof, keepdims, where=where)


# 'view'
Loading
Loading