Skip to content

Commit 3baa476

Browse files
shssfsamir-nasibli
andauthored
replace dparray with descriptor step1 (#523)
* replace dparray with descriptor step1 * get_descriptor() function added Co-authored-by: Samir Nasibli <[email protected]>
1 parent 3ba34d4 commit 3baa476

File tree

5 files changed

+120
-6
lines changed

5 files changed

+120
-6
lines changed

dpnp/dpnp_algo/dpnp_algo_mathematical.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ cpdef dparray dpnp_minimum(object x1_obj, object x2_obj, object dtype=None, dpar
257257
return call_fptr_2in_1out(DPNP_FN_MINIMUM, x1_obj, x2_obj, dtype=dtype, out=out, where=where)
258258

259259

260-
cpdef tuple dpnp_modf(dparray x1):
260+
cpdef tuple dpnp_modf(dpnp_descriptor x1):
261261
""" Convert string type names (dparray.dtype) to C enum DPNPFuncType """
262262
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
263263

dpnp/dpnp_iface.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"asnumpy",
5757
"dpnp_queue_initialize",
5858
"dpnp_queue_is_cpu",
59+
"get_dpnp_descriptor",
5960
"get_include",
6061
"matmul"
6162
]
@@ -134,6 +135,32 @@ def asnumpy(input, order='C'):
134135
return numpy.asarray(input, order=order)
135136

136137

138+
def get_dpnp_descriptor(ext_obj):
139+
"""
140+
Return True:
141+
never
142+
Return DPNP internal data discriptor object if:
143+
1. We can proceed with input data object with DPNP
144+
2. We want to handle input data object
145+
Return False if:
146+
1. We do not want to work with input data object
147+
2. We can not handle with input data object
148+
"""
149+
150+
# TODO need to allow "import dpnp" with no build procedure
151+
# if no_modules_load_doc_build();
152+
# return False
153+
154+
if use_origin_backend():
155+
return False
156+
157+
dpnp_desc = dpnp_descriptor(ext_obj)
158+
if dpnp_desc.is_valid:
159+
return dpnp_desc
160+
161+
return False
162+
163+
137164
def get_include():
138165
"""
139166
Return the directory that contains the DPNP C++ backend \\*.h header files.

dpnp/dpnp_iface_mathematical.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,10 @@ def modf(x, **kwargs):
10401040
10411041
10421042
"""
1043-
if not use_origin_backend(x) and not kwargs:
1044-
if not isinstance(x, dparray):
1045-
pass
1046-
else:
1047-
return dpnp_modf(x)
1043+
1044+
dpnp_desc = dpnp.get_dpnp_descriptor(x)
1045+
if dpnp_desc and not kwargs:
1046+
return dpnp_modf(dpnp_desc)
10481047

10491048
return call_origin(numpy.modf, x, **kwargs)
10501049

dpnp/dpnp_utils/dpnp_algo_utils.pxd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ cpdef nd2dp_array(arr)
130130
Convert ndarray to dparray
131131
"""
132132

133+
cdef class dpnp_descriptor:
134+
"""array DPNP descriptor"""
135+
136+
cdef public: # TODO remove "public" as python accessible attribute
137+
dict descriptor
138+
Py_ssize_t dpnp_descriptor_data_size
139+
cpp_bool dpnp_descriptor_is_scalar
140+
141+
cdef void * get_data(self)
142+
143+
133144
cdef dparray_shape_type get_common_shape(dparray_shape_type input1_shape, dparray_shape_type input2_shape)
134145
"""
135146
Calculate common shape from input shapes

dpnp/dpnp_utils/dpnp_algo_utils.pyx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ __all__ = [
5454
"checker_throw_type_error",
5555
"checker_throw_value_error",
5656
"dp2nd_array",
57+
"dpnp_descriptor",
5758
"get_axis_indeces",
5859
"get_axis_offsets",
5960
"_get_linear_index",
@@ -71,6 +72,8 @@ def call_origin(function, *args, **kwargs):
7172
Call fallback function for unsupported cases
7273
"""
7374

75+
# print(f"DPNP call_origin(): Fallback called")
76+
7477
kwargs_out = kwargs.get("out", None)
7578
if (kwargs_out is not None):
7679
kwargs["out"] = dpnp.asnumpy(kwargs_out) if isinstance(kwargs_out, dparray) else kwargs_out
@@ -431,3 +434,77 @@ cpdef cpp_bool use_origin_backend(input1=None, size_t compute_size=0):
431434
return True
432435

433436
return False
437+
438+
439+
cdef class dpnp_descriptor:
440+
def __init__(self, obj):
441+
""" Initialze variables """
442+
self.descriptor = None
443+
self.dpnp_descriptor_data_size = 0
444+
self.dpnp_descriptor_is_scalar = True
445+
446+
""" Accure main data storage """
447+
self.descriptor = getattr(obj, "__array_interface__", None)
448+
if self.descriptor is None:
449+
return
450+
451+
if self.descriptor["version"] != 3:
452+
return
453+
454+
""" array size calculation """
455+
cdef Py_ssize_t shape_it = 0
456+
self.dpnp_descriptor_data_size = 1
457+
for shape_it in self.shape:
458+
# TODO need to use common procedure from utils to calculate array size by shape
459+
if shape_it < 0:
460+
raise ValueError(f"{ERROR_PREFIX} dpnp_descriptor::__init__() invalid value {shape_it} in 'shape'")
461+
self.dpnp_descriptor_data_size *= shape_it
462+
463+
""" set scalar propery """
464+
self.dpnp_descriptor_is_scalar = False
465+
466+
@property
467+
def is_valid(self):
468+
if self.descriptor is None:
469+
return False
470+
return True
471+
472+
@property
473+
def shape(self):
474+
if self.is_valid:
475+
return self.descriptor["shape"]
476+
return None
477+
478+
@property
479+
def size(self):
480+
if self.is_valid:
481+
return self.dpnp_descriptor_data_size
482+
return 0
483+
484+
@property
485+
def dtype(self):
486+
if self.is_valid:
487+
type_str = self.descriptor["typestr"]
488+
return dpnp.dtype(type_str)
489+
return None
490+
491+
@property
492+
def is_scalar(self):
493+
return self.dpnp_descriptor_is_scalar
494+
495+
@property
496+
def data(self):
497+
if self.is_valid:
498+
data_tuple = self.descriptor["data"]
499+
return data_tuple[0]
500+
return None
501+
502+
cdef void * get_data(self):
503+
cdef long val = self.data
504+
return < void * > val
505+
506+
def __bool__(self):
507+
return self.is_valid
508+
509+
def __str__(self):
510+
return str(self.descriptor)

0 commit comments

Comments
 (0)