Skip to content

Commit

Permalink
implement dpnp.nanmean, dpnp.nanstd, dpnp.nansum, and `dpnp.ave…
Browse files Browse the repository at this point in the history
…rage` (#1654)

* implement dpnp.nanmean, dpnp.nanstd, dpnp.nansum and dpnp.average

* change size huge test case

* fix TestArithmetic test

* fix test nanmean

* address comments

* using assert_dtype_allclose

* add rtol to comparison

* Update tests/test_mathematical.py

---------

Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com>
  • Loading branch information
vtavana and antonwolfy committed Jan 12, 2024
1 parent 5b25163 commit 3fdb921
Show file tree
Hide file tree
Showing 17 changed files with 1,443 additions and 728 deletions.
45 changes: 22 additions & 23 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,29 +376,28 @@ enum class DPNPFuncName : size_t
DPNP_FN_SUBTRACT_EXT, /**< Used in numpy.subtract() impl, requires extra
parameters */
DPNP_FN_SUM, /**< Used in numpy.sum() impl */
DPNP_FN_SUM_EXT, /**< Used in numpy.sum() impl, requires extra parameters */
DPNP_FN_SVD, /**< Used in numpy.linalg.svd() impl */
DPNP_FN_SVD_EXT, /**< Used in numpy.linalg.svd() impl, requires extra
parameters */
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_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 */
DPNP_FN_SVD, /**< Used in numpy.linalg.svd() impl */
DPNP_FN_SVD_EXT, /**< Used in numpy.linalg.svd() impl, requires extra
parameters */
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_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
49 changes: 0 additions & 49 deletions dpnp/backend/kernels/dpnp_krnl_reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,6 @@ void (*dpnp_sum_default_c)(void *,
const long *) =
dpnp_sum_c<_DataType_output, _DataType_input>;

template <typename _DataType_output, typename _DataType_input>
DPCTLSyclEventRef (*dpnp_sum_ext_c)(DPCTLSyclQueueRef,
void *,
const void *,
const shape_elem_type *,
const size_t,
const shape_elem_type *,
const size_t,
const void *,
const long *,
const DPCTLEventVectorRef) =
dpnp_sum_c<_DataType_output, _DataType_input>;

template <typename _KernelNameSpecialization1,
typename _KernelNameSpecialization2>
class dpnp_prod_c_kernel;
Expand Down Expand Up @@ -372,41 +359,5 @@ void func_map_init_reduction(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_SUM][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_sum_default_c<double, double>};

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

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

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

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

return;
}
12 changes: 0 additions & 12 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -185,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_SUM
DPNP_FN_SUM_EXT
DPNP_FN_SVD
DPNP_FN_SVD_EXT
DPNP_FN_TRACE
Expand Down Expand Up @@ -284,16 +282,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_2in_1out_strides_t)(c_dpctl.DPCTLSyclQu
const long * ,
const c_dpctl.DPCTLEventVectorRef) except +
ctypedef void(*fptr_blas_gemm_2in_1out_t)(void *, void * , void * , size_t, size_t, size_t)
ctypedef c_dpctl.DPCTLSyclEventRef(*dpnp_reduction_c_t)(c_dpctl.DPCTLSyclQueueRef,
void *,
const void * ,
const shape_elem_type*,
const size_t,
const shape_elem_type*,
const size_t,
const void * ,
const long*,
const c_dpctl.DPCTLEventVectorRef)


"""
Expand Down
78 changes: 0 additions & 78 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ __all__ += [
"dpnp_modf",
"dpnp_nancumprod",
"dpnp_nancumsum",
"dpnp_nansum",
"dpnp_sum",
"dpnp_trapz",
]

Expand Down Expand Up @@ -278,82 +276,6 @@ cpdef utils.dpnp_descriptor dpnp_nancumsum(utils.dpnp_descriptor x1):
return dpnp_cumsum(x1_desc)


cpdef utils.dpnp_descriptor dpnp_nansum(utils.dpnp_descriptor x1):
x1_obj = x1.get_array()
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(x1.shape,
x1.dtype,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

for i in range(result.size):
input_elem = x1.get_pyobj().flat[i]

if dpnp.isnan(input_elem):
result.get_pyobj().flat[i] = 0
else:
result.get_pyobj().flat[i] = input_elem

return dpnp_sum(result)


cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor x1,
object axis=None,
object dtype=None,
utils.dpnp_descriptor out=None,
cpp_bool keepdims=False,
object initial=None,
object where=True):

cdef shape_type_c x1_shape = x1.shape
cdef DPNPFuncType x1_c_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)

cdef shape_type_c axis_shape = utils._object_to_tuple(axis)

cdef shape_type_c result_shape = utils.get_reduction_output_shape(x1_shape, axis, keepdims)
cdef DPNPFuncType result_c_type = utils.get_output_c_type(DPNP_FN_SUM_EXT, x1_c_type, out, dtype)

""" select kernel """
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_SUM_EXT, x1_c_type, result_c_type)

x1_obj = x1.get_array()

""" Create result array """
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
result_c_type,
out,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

if x1.size == 0 and axis is None:
return result

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()

""" Call FPTR interface function """
cdef dpnp_reduction_c_t func = <dpnp_reduction_c_t > kernel_data.ptr
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
result.get_data(),
x1.get_data(),
x1_shape.data(),
x1_shape.size(),
axis_shape.data(),
axis_shape.size(),
NULL,
NULL,
NULL) # dep_events_ref

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

return result


cpdef utils.dpnp_descriptor dpnp_trapz(utils.dpnp_descriptor y1, utils.dpnp_descriptor x1, double dx):

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(y1.dtype)
Expand Down
10 changes: 0 additions & 10 deletions dpnp/dpnp_algo/dpnp_algo_statistics.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ and the rest of the library
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file

__all__ += [
"dpnp_average",
"dpnp_correlate",
"dpnp_median",
]
Expand All @@ -49,15 +48,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_statistic_1in_1out_func_ptr_t)(c_dpct
const c_dpctl.DPCTLEventVectorRef)


cpdef dpnp_average(utils.dpnp_descriptor x1):
array_sum = dpnp_sum(x1).get_pyobj()

""" Numpy interface inconsistency """
return_type = dpnp.float32 if (x1.dtype == dpnp.float32) else dpnp.float64

return (return_type(array_sum / x1.size))


cpdef utils.dpnp_descriptor dpnp_correlate(utils.dpnp_descriptor x1, utils.dpnp_descriptor x2):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(x2.dtype)
Expand Down
Loading

0 comments on commit 3fdb921

Please sign in to comment.