Skip to content

Commit 2cdf0c3

Browse files
committed
implement dpnp.max and dpnp.max using dpctl.tensor functions
1 parent 2aeb333 commit 2cdf0c3

13 files changed

+132
-342
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,18 @@ enum class DPNPFuncName : size_t
216216
DPNP_FN_MATRIX_RANK_EXT, /**< Used in numpy.linalg.matrix_rank() impl,
217217
requires extra parameters */
218218
DPNP_FN_MAX, /**< Used in numpy.max() impl */
219-
DPNP_FN_MAX_EXT, /**< Used in numpy.max() impl, requires extra parameters */
220-
DPNP_FN_MAXIMUM, /**< Used in numpy.fmax() impl */
221-
DPNP_FN_MAXIMUM_EXT, /**< Used in numpy.fmax() impl , requires extra
222-
parameters */
223-
DPNP_FN_MEAN, /**< Used in numpy.mean() impl */
224-
DPNP_FN_MEDIAN, /**< Used in numpy.median() impl */
225-
DPNP_FN_MEDIAN_EXT, /**< Used in numpy.median() impl, requires extra
226-
parameters */
227-
DPNP_FN_MIN, /**< Used in numpy.min() impl */
228-
DPNP_FN_MIN_EXT, /**< Used in numpy.min() impl, requires extra parameters */
229-
DPNP_FN_MINIMUM, /**< Used in numpy.fmin() impl */
230-
DPNP_FN_MINIMUM_EXT, /**< Used in numpy.fmax() impl, requires extra
231-
parameters */
232-
DPNP_FN_MODF, /**< Used in numpy.modf() impl */
219+
DPNP_FN_MAXIMUM, /**< Used in numpy.fmax() impl */
220+
DPNP_FN_MAXIMUM_EXT, /**< Used in numpy.fmax() impl , requires extra
221+
parameters */
222+
DPNP_FN_MEAN, /**< Used in numpy.mean() impl */
223+
DPNP_FN_MEDIAN, /**< Used in numpy.median() impl */
224+
DPNP_FN_MEDIAN_EXT, /**< Used in numpy.median() impl, requires extra
225+
parameters */
226+
DPNP_FN_MIN, /**< Used in numpy.min() impl */
227+
DPNP_FN_MINIMUM, /**< Used in numpy.fmin() impl */
228+
DPNP_FN_MINIMUM_EXT, /**< Used in numpy.fmax() impl, requires extra
229+
parameters */
230+
DPNP_FN_MODF, /**< Used in numpy.modf() impl */
233231
DPNP_FN_MODF_EXT, /**< Used in numpy.modf() impl, requires extra parameters
234232
*/
235233
DPNP_FN_MULTIPLY, /**< Used in numpy.multiply() impl */

dpnp/backend/kernels/dpnp_krnl_statistics.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -511,18 +511,6 @@ void (*dpnp_max_default_c)(void *,
511511
const shape_elem_type *,
512512
size_t) = dpnp_max_c<_DataType>;
513513

514-
template <typename _DataType>
515-
DPCTLSyclEventRef (*dpnp_max_ext_c)(DPCTLSyclQueueRef,
516-
void *,
517-
void *,
518-
const size_t,
519-
const shape_elem_type *,
520-
size_t,
521-
const shape_elem_type *,
522-
size_t,
523-
const DPCTLEventVectorRef) =
524-
dpnp_max_c<_DataType>;
525-
526514
template <typename _DataType, typename _ResultType>
527515
DPCTLSyclEventRef dpnp_mean_c(DPCTLSyclQueueRef q_ref,
528516
void *array1_in,
@@ -895,18 +883,6 @@ void (*dpnp_min_default_c)(void *,
895883
const shape_elem_type *,
896884
size_t) = dpnp_min_c<_DataType>;
897885

898-
template <typename _DataType>
899-
DPCTLSyclEventRef (*dpnp_min_ext_c)(DPCTLSyclQueueRef,
900-
void *,
901-
void *,
902-
const size_t,
903-
const shape_elem_type *,
904-
size_t,
905-
const shape_elem_type *,
906-
size_t,
907-
const DPCTLEventVectorRef) =
908-
dpnp_min_c<_DataType>;
909-
910886
template <typename _DataType>
911887
DPCTLSyclEventRef dpnp_nanvar_c(DPCTLSyclQueueRef q_ref,
912888
void *array1_in,
@@ -1302,15 +1278,6 @@ void func_map_init_statistics(func_map_t &fmap)
13021278
fmap[DPNPFuncName::DPNP_FN_MAX][eft_DBL][eft_DBL] = {
13031279
eft_DBL, (void *)dpnp_max_default_c<double>};
13041280

1305-
fmap[DPNPFuncName::DPNP_FN_MAX_EXT][eft_INT][eft_INT] = {
1306-
eft_INT, (void *)dpnp_max_ext_c<int32_t>};
1307-
fmap[DPNPFuncName::DPNP_FN_MAX_EXT][eft_LNG][eft_LNG] = {
1308-
eft_LNG, (void *)dpnp_max_ext_c<int64_t>};
1309-
fmap[DPNPFuncName::DPNP_FN_MAX_EXT][eft_FLT][eft_FLT] = {
1310-
eft_FLT, (void *)dpnp_max_ext_c<float>};
1311-
fmap[DPNPFuncName::DPNP_FN_MAX_EXT][eft_DBL][eft_DBL] = {
1312-
eft_DBL, (void *)dpnp_max_ext_c<double>};
1313-
13141281
fmap[DPNPFuncName::DPNP_FN_MEAN][eft_INT][eft_INT] = {
13151282
eft_DBL, (void *)dpnp_mean_default_c<int32_t, double>};
13161283
fmap[DPNPFuncName::DPNP_FN_MEAN][eft_LNG][eft_LNG] = {
@@ -1359,15 +1326,6 @@ void func_map_init_statistics(func_map_t &fmap)
13591326
fmap[DPNPFuncName::DPNP_FN_MIN][eft_DBL][eft_DBL] = {
13601327
eft_DBL, (void *)dpnp_min_default_c<double>};
13611328

1362-
fmap[DPNPFuncName::DPNP_FN_MIN_EXT][eft_INT][eft_INT] = {
1363-
eft_INT, (void *)dpnp_min_ext_c<int32_t>};
1364-
fmap[DPNPFuncName::DPNP_FN_MIN_EXT][eft_LNG][eft_LNG] = {
1365-
eft_LNG, (void *)dpnp_min_ext_c<int64_t>};
1366-
fmap[DPNPFuncName::DPNP_FN_MIN_EXT][eft_FLT][eft_FLT] = {
1367-
eft_FLT, (void *)dpnp_min_ext_c<float>};
1368-
fmap[DPNPFuncName::DPNP_FN_MIN_EXT][eft_DBL][eft_DBL] = {
1369-
eft_DBL, (void *)dpnp_min_ext_c<double>};
1370-
13711329
fmap[DPNPFuncName::DPNP_FN_NANVAR][eft_INT][eft_INT] = {
13721330
eft_INT, (void *)dpnp_nanvar_default_c<int32_t>};
13731331
fmap[DPNPFuncName::DPNP_FN_NANVAR][eft_LNG][eft_LNG] = {

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,10 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
110110
DPNP_FN_MATMUL_EXT
111111
DPNP_FN_MATRIX_RANK
112112
DPNP_FN_MATRIX_RANK_EXT
113-
DPNP_FN_MAX
114-
DPNP_FN_MAX_EXT
115113
DPNP_FN_MAXIMUM
116114
DPNP_FN_MAXIMUM_EXT
117115
DPNP_FN_MEDIAN
118116
DPNP_FN_MEDIAN_EXT
119-
DPNP_FN_MIN
120-
DPNP_FN_MIN_EXT
121117
DPNP_FN_MINIMUM
122118
DPNP_FN_MINIMUM_EXT
123119
DPNP_FN_MODF
@@ -376,12 +372,6 @@ Array manipulation routines
376372
cpdef dpnp_descriptor dpnp_repeat(dpnp_descriptor array1, repeats, axes=*)
377373

378374

379-
"""
380-
Statistics functions
381-
"""
382-
cpdef dpnp_descriptor dpnp_min(dpnp_descriptor a, axis)
383-
384-
385375
"""
386376
Sorting functions
387377
"""

dpnp/dpnp_algo/dpnp_algo_statistics.pxi

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ and the rest of the library
3838
__all__ += [
3939
"dpnp_average",
4040
"dpnp_correlate",
41-
"dpnp_max",
4241
"dpnp_median",
43-
"dpnp_min",
4442
"dpnp_nanvar",
4543
"dpnp_std",
4644
"dpnp_var",
@@ -64,16 +62,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_statistic_1in_1out_func_ptr_t)(c_dpct
6462
void *, void * , shape_elem_type * , size_t,
6563
shape_elem_type * , size_t,
6664
const c_dpctl.DPCTLEventVectorRef)
67-
ctypedef c_dpctl.DPCTLSyclEventRef(*custom_statistic_1in_1out_func_ptr_t_max)(c_dpctl.DPCTLSyclQueueRef,
68-
void *,
69-
void * ,
70-
const size_t,
71-
shape_elem_type * ,
72-
size_t,
73-
shape_elem_type * ,
74-
size_t,
75-
const c_dpctl.DPCTLEventVectorRef)
76-
7765

7866
cdef utils.dpnp_descriptor call_fptr_custom_std_var_1in_1out(DPNPFuncName fptr_name, utils.dpnp_descriptor x1, ddof):
7967
cdef shape_type_c x1_shape = x1.shape
@@ -177,86 +165,6 @@ cpdef utils.dpnp_descriptor dpnp_correlate(utils.dpnp_descriptor x1, utils.dpnp_
177165
return result
178166

179167

180-
cdef utils.dpnp_descriptor _dpnp_max(utils.dpnp_descriptor x1, _axis_, shape_type_c result_shape):
181-
cdef shape_type_c x1_shape = x1.shape
182-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
183-
184-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MAX_EXT, param1_type, param1_type)
185-
186-
x1_obj = x1.get_array()
187-
188-
# create result array with type given by FPTR data
189-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
190-
kernel_data.return_type,
191-
None,
192-
device=x1_obj.sycl_device,
193-
usm_type=x1_obj.usm_type,
194-
sycl_queue=x1_obj.sycl_queue)
195-
196-
result_sycl_queue = result.get_array().sycl_queue
197-
198-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
199-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
200-
201-
cdef custom_statistic_1in_1out_func_ptr_t_max func = <custom_statistic_1in_1out_func_ptr_t_max > kernel_data.ptr
202-
cdef shape_type_c axis
203-
cdef Py_ssize_t axis_size = 0
204-
cdef shape_type_c axis_ = axis
205-
206-
if _axis_ is not None:
207-
axis = _axis_
208-
axis_.reserve(len(axis))
209-
for shape_it in axis:
210-
axis_.push_back(shape_it)
211-
axis_size = len(axis)
212-
213-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
214-
x1.get_data(),
215-
result.get_data(),
216-
result.size,
217-
x1_shape.data(),
218-
x1.ndim,
219-
axis_.data(),
220-
axis_size,
221-
NULL) # dep_events_ref
222-
223-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
224-
c_dpctl.DPCTLEvent_Delete(event_ref)
225-
226-
return result
227-
228-
229-
cpdef utils.dpnp_descriptor dpnp_max(utils.dpnp_descriptor x1, axis):
230-
cdef shape_type_c x1_shape = x1.shape
231-
cdef shape_type_c output_shape
232-
233-
if axis is None:
234-
axis_ = axis
235-
output_shape.push_back(1)
236-
else:
237-
if isinstance(axis, int):
238-
if axis < 0:
239-
axis_ = tuple([x1.ndim - axis])
240-
else:
241-
axis_ = tuple([axis])
242-
else:
243-
_axis_ = []
244-
for i in range(len(axis)):
245-
if axis[i] < 0:
246-
_axis_.append(x1.ndim - axis[i])
247-
else:
248-
_axis_.append(axis[i])
249-
axis_ = tuple(_axis_)
250-
251-
output_shape.resize(len(x1_shape) - len(axis_), 0)
252-
ind = 0
253-
for id, shape_axis in enumerate(x1_shape):
254-
if id not in axis_:
255-
output_shape[ind] = shape_axis
256-
ind += 1
257-
258-
return _dpnp_max(x1, axis_, output_shape)
259-
260168
cpdef utils.dpnp_descriptor dpnp_median(utils.dpnp_descriptor array1):
261169
cdef shape_type_c x1_shape = array1.shape
262170
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(array1.dtype)
@@ -301,85 +209,6 @@ cpdef utils.dpnp_descriptor dpnp_median(utils.dpnp_descriptor array1):
301209
return result
302210

303211

304-
cpdef utils.dpnp_descriptor _dpnp_min(utils.dpnp_descriptor x1, _axis_, shape_type_c shape_output):
305-
cdef shape_type_c x1_shape = x1.shape
306-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
307-
308-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MIN_EXT, param1_type, param1_type)
309-
310-
x1_obj = x1.get_array()
311-
312-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(shape_output,
313-
kernel_data.return_type,
314-
None,
315-
device=x1_obj.sycl_device,
316-
usm_type=x1_obj.usm_type,
317-
sycl_queue=x1_obj.sycl_queue)
318-
319-
result_sycl_queue = result.get_array().sycl_queue
320-
321-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
322-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
323-
324-
cdef custom_statistic_1in_1out_func_ptr_t_max func = <custom_statistic_1in_1out_func_ptr_t_max > kernel_data.ptr
325-
cdef shape_type_c axis
326-
cdef Py_ssize_t axis_size = 0
327-
cdef shape_type_c axis_ = axis
328-
329-
if _axis_ is not None:
330-
axis = _axis_
331-
axis_.reserve(len(axis))
332-
for shape_it in axis:
333-
if shape_it < 0:
334-
raise ValueError("DPNP algo::_dpnp_min(): Negative values in 'shape' are not allowed")
335-
axis_.push_back(shape_it)
336-
axis_size = len(axis)
337-
338-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
339-
x1.get_data(),
340-
result.get_data(),
341-
result.size,
342-
x1_shape.data(),
343-
x1.ndim,
344-
axis_.data(),
345-
axis_size,
346-
NULL) # dep_events_ref
347-
348-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
349-
c_dpctl.DPCTLEvent_Delete(event_ref)
350-
351-
return result
352-
353-
354-
cpdef utils.dpnp_descriptor dpnp_min(utils.dpnp_descriptor x1, axis):
355-
cdef shape_type_c x1_shape = x1.shape
356-
cdef shape_type_c shape_output
357-
358-
if axis is None:
359-
axis_ = axis
360-
shape_output = (1,)
361-
else:
362-
if isinstance(axis, int):
363-
if axis < 0:
364-
axis_ = tuple([x1.ndim - axis])
365-
else:
366-
axis_ = tuple([axis])
367-
else:
368-
_axis_ = []
369-
for i in range(len(axis)):
370-
if axis[i] < 0:
371-
_axis_.append(x1.ndim - axis[i])
372-
else:
373-
_axis_.append(axis[i])
374-
axis_ = tuple(_axis_)
375-
376-
for id, shape_axis in enumerate(x1_shape):
377-
if id not in axis_:
378-
shape_output.push_back(shape_axis)
379-
380-
return _dpnp_min(x1, axis_, shape_output)
381-
382-
383212
cpdef utils.dpnp_descriptor dpnp_nanvar(utils.dpnp_descriptor arr, ddof):
384213
# dpnp_isnan does not support USM array as input in comparison to dpnp.isnan
385214
cdef utils.dpnp_descriptor mask_arr = dpnp.get_dpnp_descriptor(dpnp.isnan(arr.get_pyobj()),

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: language_level=3
2-
# distutils: language = c++
31
# -*- coding: utf-8 -*-
42
# *****************************************************************************
53
# Copyright (c) 2023, Intel Corporation

dpnp/dpnp_array.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
# *****************************************************************************
2626

2727
import dpctl.tensor as dpt
28-
import numpy
2928

3029
import dpnp
3130

@@ -907,9 +906,9 @@ def max(
907906
self,
908907
axis=None,
909908
out=None,
910-
keepdims=numpy._NoValue,
911-
initial=numpy._NoValue,
912-
where=numpy._NoValue,
909+
keepdims=False,
910+
initial=None,
911+
where=True,
913912
):
914913
"""Return the maximum along an axis."""
915914

@@ -924,9 +923,9 @@ def min(
924923
self,
925924
axis=None,
926925
out=None,
927-
keepdims=numpy._NoValue,
928-
initial=numpy._NoValue,
929-
where=numpy._NoValue,
926+
keepdims=False,
927+
initial=None,
928+
where=True,
930929
):
931930
"""Return the minimum along a given axis."""
932931

dpnp/dpnp_iface_mathematical.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,9 +1400,9 @@ def maximum(
14001400
:obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
14011401
:obj:`dpnp.amax` : The maximum value of an array along a given axis, propagates NaNs.
14021402
:obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
1403-
:obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
1404-
:obj:`dpnp.amix` : The minimum value of an array along a given axis, propagates NaNs.
1405-
:obj:`dpnp.nanmix` : The minimum value of an array along a given axis, ignores NaNs.
1403+
:obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
1404+
:obj:`dpnp.amax` : The maximum value of an array along a given axis, propagates NaNs.
1405+
:obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
14061406
14071407
Examples
14081408
--------
@@ -1479,9 +1479,9 @@ def minimum(
14791479
:obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
14801480
:obj:`dpnp.amin` : The minimum value of an array along a given axis, propagates NaNs.
14811481
:obj:`dpnp.nanmin` : The minimum value of an array along a given axis, ignores NaNs.
1482-
:obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
1483-
:obj:`dpnp.amax` : The maximum value of an array along a given axis, propagates NaNs.
1484-
:obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
1482+
:obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
1483+
:obj:`dpnp.amin` : The minimum value of an array along a given axis, propagates NaNs.
1484+
:obj:`dpnp.nanmin` : The minimum value of an array along a given axis, ignores NaNs.
14851485
14861486
Examples
14871487
--------

0 commit comments

Comments
 (0)