Skip to content

Commit 3fdb921

Browse files
vtavanaantonwolfy
andauthored
implement dpnp.nanmean, dpnp.nanstd, dpnp.nansum, and dpnp.average (#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>
1 parent 5b25163 commit 3fdb921

17 files changed

+1443
-728
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -376,29 +376,28 @@ enum class DPNPFuncName : size_t
376376
DPNP_FN_SUBTRACT_EXT, /**< Used in numpy.subtract() impl, requires extra
377377
parameters */
378378
DPNP_FN_SUM, /**< Used in numpy.sum() impl */
379-
DPNP_FN_SUM_EXT, /**< Used in numpy.sum() impl, requires extra parameters */
380-
DPNP_FN_SVD, /**< Used in numpy.linalg.svd() impl */
381-
DPNP_FN_SVD_EXT, /**< Used in numpy.linalg.svd() impl, requires extra
382-
parameters */
383-
DPNP_FN_TAKE, /**< Used in numpy.take() impl */
384-
DPNP_FN_TAN, /**< Used in numpy.tan() impl */
385-
DPNP_FN_TANH, /**< Used in numpy.tanh() impl */
386-
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */
387-
DPNP_FN_TRACE, /**< Used in numpy.trace() impl */
388-
DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra
389-
parameters */
390-
DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */
391-
DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra
392-
parameters */
393-
DPNP_FN_TRI, /**< Used in numpy.tri() impl */
394-
DPNP_FN_TRIL, /**< Used in numpy.tril() impl */
395-
DPNP_FN_TRIU, /**< Used in numpy.triu() impl */
396-
DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */
397-
DPNP_FN_VANDER, /**< Used in numpy.vander() impl */
398-
DPNP_FN_VAR, /**< Used in numpy.var() impl */
399-
DPNP_FN_ZEROS, /**< Used in numpy.zeros() impl */
400-
DPNP_FN_ZEROS_LIKE, /**< Used in numpy.zeros_like() impl */
401-
DPNP_FN_LAST, /**< The latest element of the enumeration */
379+
DPNP_FN_SVD, /**< Used in numpy.linalg.svd() impl */
380+
DPNP_FN_SVD_EXT, /**< Used in numpy.linalg.svd() impl, requires extra
381+
parameters */
382+
DPNP_FN_TAKE, /**< Used in numpy.take() impl */
383+
DPNP_FN_TAN, /**< Used in numpy.tan() impl */
384+
DPNP_FN_TANH, /**< Used in numpy.tanh() impl */
385+
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */
386+
DPNP_FN_TRACE, /**< Used in numpy.trace() impl */
387+
DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra
388+
parameters */
389+
DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */
390+
DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra
391+
parameters */
392+
DPNP_FN_TRI, /**< Used in numpy.tri() impl */
393+
DPNP_FN_TRIL, /**< Used in numpy.tril() impl */
394+
DPNP_FN_TRIU, /**< Used in numpy.triu() impl */
395+
DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */
396+
DPNP_FN_VANDER, /**< Used in numpy.vander() impl */
397+
DPNP_FN_VAR, /**< Used in numpy.var() impl */
398+
DPNP_FN_ZEROS, /**< Used in numpy.zeros() impl */
399+
DPNP_FN_ZEROS_LIKE, /**< Used in numpy.zeros_like() impl */
400+
DPNP_FN_LAST, /**< The latest element of the enumeration */
402401
};
403402

404403
/**

dpnp/backend/kernels/dpnp_krnl_reduction.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,6 @@ void (*dpnp_sum_default_c)(void *,
177177
const long *) =
178178
dpnp_sum_c<_DataType_output, _DataType_input>;
179179

180-
template <typename _DataType_output, typename _DataType_input>
181-
DPCTLSyclEventRef (*dpnp_sum_ext_c)(DPCTLSyclQueueRef,
182-
void *,
183-
const void *,
184-
const shape_elem_type *,
185-
const size_t,
186-
const shape_elem_type *,
187-
const size_t,
188-
const void *,
189-
const long *,
190-
const DPCTLEventVectorRef) =
191-
dpnp_sum_c<_DataType_output, _DataType_input>;
192-
193180
template <typename _KernelNameSpecialization1,
194181
typename _KernelNameSpecialization2>
195182
class dpnp_prod_c_kernel;
@@ -372,41 +359,5 @@ void func_map_init_reduction(func_map_t &fmap)
372359
fmap[DPNPFuncName::DPNP_FN_SUM][eft_DBL][eft_DBL] = {
373360
eft_DBL, (void *)dpnp_sum_default_c<double, double>};
374361

375-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_INT][eft_INT] = {
376-
eft_LNG, (void *)dpnp_sum_ext_c<int32_t, int32_t>};
377-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_INT][eft_LNG] = {
378-
eft_LNG, (void *)dpnp_sum_ext_c<int64_t, int32_t>};
379-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_INT][eft_FLT] = {
380-
eft_FLT, (void *)dpnp_sum_ext_c<float, int32_t>};
381-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_INT][eft_DBL] = {
382-
eft_DBL, (void *)dpnp_sum_ext_c<double, int32_t>};
383-
384-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_LNG][eft_INT] = {
385-
eft_INT, (void *)dpnp_sum_ext_c<int32_t, int64_t>};
386-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_LNG][eft_LNG] = {
387-
eft_LNG, (void *)dpnp_sum_ext_c<int64_t, int64_t>};
388-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_LNG][eft_FLT] = {
389-
eft_FLT, (void *)dpnp_sum_ext_c<float, int64_t>};
390-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_LNG][eft_DBL] = {
391-
eft_DBL, (void *)dpnp_sum_ext_c<double, int64_t>};
392-
393-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_FLT][eft_INT] = {
394-
eft_INT, (void *)dpnp_sum_ext_c<int32_t, float>};
395-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_FLT][eft_LNG] = {
396-
eft_LNG, (void *)dpnp_sum_ext_c<int64_t, float>};
397-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_FLT][eft_FLT] = {
398-
eft_FLT, (void *)dpnp_sum_ext_c<float, float>};
399-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_FLT][eft_DBL] = {
400-
eft_DBL, (void *)dpnp_sum_ext_c<double, float>};
401-
402-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_DBL][eft_INT] = {
403-
eft_INT, (void *)dpnp_sum_ext_c<int32_t, double>};
404-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_DBL][eft_LNG] = {
405-
eft_LNG, (void *)dpnp_sum_ext_c<int64_t, double>};
406-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_DBL][eft_FLT] = {
407-
eft_FLT, (void *)dpnp_sum_ext_c<float, double>};
408-
fmap[DPNPFuncName::DPNP_FN_SUM_EXT][eft_DBL][eft_DBL] = {
409-
eft_DBL, (void *)dpnp_sum_ext_c<double, double>};
410-
411362
return;
412363
}

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
185185
DPNP_FN_SEARCHSORTED_EXT
186186
DPNP_FN_SORT
187187
DPNP_FN_SORT_EXT
188-
DPNP_FN_SUM
189-
DPNP_FN_SUM_EXT
190188
DPNP_FN_SVD
191189
DPNP_FN_SVD_EXT
192190
DPNP_FN_TRACE
@@ -284,16 +282,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_2in_1out_strides_t)(c_dpctl.DPCTLSyclQu
284282
const long * ,
285283
const c_dpctl.DPCTLEventVectorRef) except +
286284
ctypedef void(*fptr_blas_gemm_2in_1out_t)(void *, void * , void * , size_t, size_t, size_t)
287-
ctypedef c_dpctl.DPCTLSyclEventRef(*dpnp_reduction_c_t)(c_dpctl.DPCTLSyclQueueRef,
288-
void *,
289-
const void * ,
290-
const shape_elem_type*,
291-
const size_t,
292-
const shape_elem_type*,
293-
const size_t,
294-
const void * ,
295-
const long*,
296-
const c_dpctl.DPCTLEventVectorRef)
297285

298286

299287
"""

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ __all__ += [
4848
"dpnp_modf",
4949
"dpnp_nancumprod",
5050
"dpnp_nancumsum",
51-
"dpnp_nansum",
52-
"dpnp_sum",
5351
"dpnp_trapz",
5452
]
5553

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

280278

281-
cpdef utils.dpnp_descriptor dpnp_nansum(utils.dpnp_descriptor x1):
282-
x1_obj = x1.get_array()
283-
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(x1.shape,
284-
x1.dtype,
285-
None,
286-
device=x1_obj.sycl_device,
287-
usm_type=x1_obj.usm_type,
288-
sycl_queue=x1_obj.sycl_queue)
289-
290-
for i in range(result.size):
291-
input_elem = x1.get_pyobj().flat[i]
292-
293-
if dpnp.isnan(input_elem):
294-
result.get_pyobj().flat[i] = 0
295-
else:
296-
result.get_pyobj().flat[i] = input_elem
297-
298-
return dpnp_sum(result)
299-
300-
301-
cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor x1,
302-
object axis=None,
303-
object dtype=None,
304-
utils.dpnp_descriptor out=None,
305-
cpp_bool keepdims=False,
306-
object initial=None,
307-
object where=True):
308-
309-
cdef shape_type_c x1_shape = x1.shape
310-
cdef DPNPFuncType x1_c_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
311-
312-
cdef shape_type_c axis_shape = utils._object_to_tuple(axis)
313-
314-
cdef shape_type_c result_shape = utils.get_reduction_output_shape(x1_shape, axis, keepdims)
315-
cdef DPNPFuncType result_c_type = utils.get_output_c_type(DPNP_FN_SUM_EXT, x1_c_type, out, dtype)
316-
317-
""" select kernel """
318-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_SUM_EXT, x1_c_type, result_c_type)
319-
320-
x1_obj = x1.get_array()
321-
322-
""" Create result array """
323-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
324-
result_c_type,
325-
out,
326-
device=x1_obj.sycl_device,
327-
usm_type=x1_obj.usm_type,
328-
sycl_queue=x1_obj.sycl_queue)
329-
330-
if x1.size == 0 and axis is None:
331-
return result
332-
333-
result_sycl_queue = result.get_array().sycl_queue
334-
335-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
336-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
337-
338-
""" Call FPTR interface function """
339-
cdef dpnp_reduction_c_t func = <dpnp_reduction_c_t > kernel_data.ptr
340-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
341-
result.get_data(),
342-
x1.get_data(),
343-
x1_shape.data(),
344-
x1_shape.size(),
345-
axis_shape.data(),
346-
axis_shape.size(),
347-
NULL,
348-
NULL,
349-
NULL) # dep_events_ref
350-
351-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
352-
c_dpctl.DPCTLEvent_Delete(event_ref)
353-
354-
return result
355-
356-
357279
cpdef utils.dpnp_descriptor dpnp_trapz(utils.dpnp_descriptor y1, utils.dpnp_descriptor x1, double dx):
358280

359281
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(y1.dtype)

dpnp/dpnp_algo/dpnp_algo_statistics.pxi

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ and the rest of the library
3636
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3737

3838
__all__ += [
39-
"dpnp_average",
4039
"dpnp_correlate",
4140
"dpnp_median",
4241
]
@@ -49,15 +48,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_statistic_1in_1out_func_ptr_t)(c_dpct
4948
const c_dpctl.DPCTLEventVectorRef)
5049

5150

52-
cpdef dpnp_average(utils.dpnp_descriptor x1):
53-
array_sum = dpnp_sum(x1).get_pyobj()
54-
55-
""" Numpy interface inconsistency """
56-
return_type = dpnp.float32 if (x1.dtype == dpnp.float32) else dpnp.float64
57-
58-
return (return_type(array_sum / x1.size))
59-
60-
6151
cpdef utils.dpnp_descriptor dpnp_correlate(utils.dpnp_descriptor x1, utils.dpnp_descriptor x2):
6252
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
6353
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(x2.dtype)

0 commit comments

Comments
 (0)