Skip to content

Commit d6f4104

Browse files
authored
Add some algo module functions to descriptor (#843)
1 parent 321a466 commit d6f4104

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ cpdef utils.dpnp_descriptor dpnp_diag(utils.dpnp_descriptor v, int k):
9898

9999

100100
cpdef utils.dpnp_descriptor dpnp_full(result_shape, value_in, result_dtype):
101-
# Convert string type names (dparray.dtype) to C enum DPNPFuncType
101+
# Convert string type names (array.dtype) to C enum DPNPFuncType
102102
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)
103103

104104
# get the FPTR data structure
@@ -121,7 +121,7 @@ cpdef utils.dpnp_descriptor dpnp_full(result_shape, value_in, result_dtype):
121121

122122

123123
cpdef utils.dpnp_descriptor dpnp_full_like(result_shape, value_in, result_dtype):
124-
# Convert string type names (dparray.dtype) to C enum DPNPFuncType
124+
# Convert string type names (array.dtype) to C enum DPNPFuncType
125125
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)
126126

127127
# get the FPTR data structure
@@ -143,8 +143,9 @@ cpdef utils.dpnp_descriptor dpnp_full_like(result_shape, value_in, result_dtype)
143143
return result
144144

145145

146-
cpdef dparray dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
147-
cdef dparray result = dparray(num, dtype=dtype)
146+
cpdef utils.dpnp_descriptor dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
147+
cdef shape_type_c obj_shape = utils._object_to_tuple(num)
148+
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)
148149

149150
if endpoint:
150151
steps_count = num - 1
@@ -156,28 +157,27 @@ cpdef dparray dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
156157
step = dpnp.power(dpnp.float64(stop) / start, 1.0 / steps_count)
157158
mult = step
158159
for i in range(1, result.size):
159-
result[i] = start * mult
160+
result.get_pyobj()[i] = start * mult
160161
mult = mult * step
161162
else:
162163
step = dpnp.nan
163164

164165
# if result is not empty, then fiil first and last elements
165166
if num > 0:
166-
result[0] = start
167+
result.get_pyobj()[0] = start
167168
if endpoint and result.size > 1:
168-
result[result.size - 1] = stop
169+
result.get_pyobj()[result.size - 1] = stop
169170

170171
return result
171172

172173

173-
cpdef dparray dpnp_identity(n, result_dtype):
174+
cpdef utils.dpnp_descriptor dpnp_identity(n, result_dtype):
174175
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)
175176

176177
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_IDENTITY, dtype_in, DPNP_FT_NONE)
177178

178-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
179-
180-
cdef dparray result = dparray((n, n), dtype=result_type)
179+
cdef shape_type_c shape_in = (n, n)
180+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(shape_in, kernel_data.return_type, None)
181181

182182
cdef fptr_1out_t func = <fptr_1out_t > kernel_data.ptr
183183
func(result.get_data(), n)
@@ -187,7 +187,8 @@ cpdef dparray dpnp_identity(n, result_dtype):
187187

188188
# TODO this function should work through dpnp_arange_c
189189
cpdef tuple dpnp_linspace(start, stop, num, endpoint, retstep, dtype, axis):
190-
cdef dparray result = dparray(num, dtype=dtype)
190+
cdef shape_type_c obj_shape = utils._object_to_tuple(num)
191+
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)
191192

192193
if endpoint:
193194
steps_count = num - 1
@@ -198,20 +199,20 @@ cpdef tuple dpnp_linspace(start, stop, num, endpoint, retstep, dtype, axis):
198199
if steps_count > 0:
199200
step = (dpnp.float64(stop) - start) / steps_count
200201
for i in range(1, result.size):
201-
result[i] = start + step * i
202+
result.get_pyobj()[i] = start + step * i
202203
else:
203204
step = dpnp.nan
204205

205206
# if result is not empty, then fiil first and last elements
206207
if num > 0:
207-
result[0] = start
208+
result.get_pyobj()[0] = start
208209
if endpoint and result.size > 1:
209-
result[result.size - 1] = stop
210+
result.get_pyobj()[result.size - 1] = stop
210211

211-
return (result, step)
212+
return (result.get_pyobj(), step)
212213

213214

214-
cpdef dparray dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
215+
cpdef object dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
215216
temp = dpnp.linspace(start, stop, num=num, endpoint=endpoint)
216217
return dpnp.power(base, temp).astype(dtype)
217218

@@ -278,7 +279,7 @@ cpdef utils.dpnp_descriptor dpnp_trace(utils.dpnp_descriptor arr, offset=0, axis
278279
else:
279280
dtype_ = dtype
280281

281-
cdef dparray diagonal_arr = dpnp.diagonal(arr, offset, axis1, axis2)
282+
cdef utils.dpnp_descriptor diagonal_arr = dpnp_diagonal(arr, offset)
282283
cdef size_t diagonal_ndim = diagonal_arr.ndim
283284
cdef shape_type_c diagonal_shape = diagonal_arr.shape
284285

@@ -298,22 +299,19 @@ cpdef utils.dpnp_descriptor dpnp_trace(utils.dpnp_descriptor arr, offset=0, axis
298299
return result
299300

300301

301-
cpdef dparray dpnp_tri(N, M=None, k=0, dtype=numpy.float):
302+
cpdef utils.dpnp_descriptor dpnp_tri(N, M=None, k=0, dtype=numpy.float):
302303
if M is None:
303304
M = N
304305

305306
if dtype == numpy.float:
306307
dtype = numpy.float64
307308

308-
cdef dparray result
309-
310309
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
311310

312311
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRI, param1_type, param1_type)
313312

314-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
315-
316-
result = dparray(shape=(N, M), dtype=result_type)
313+
cdef shape_type_c shape_in = (N, M)
314+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(shape_in, kernel_data.return_type, None)
317315

318316
cdef custom_indexing_1out_func_ptr_t func = <custom_indexing_1out_func_ptr_t > kernel_data.ptr
319317

dpnp/dpnp_algo/dpnp_algo_indexing.pyx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ cpdef tuple dpnp_diag_indices(n, ndim):
8383
return tuple(result)
8484

8585

86-
cpdef dparray dpnp_diagonal(dpnp_descriptor input, offset=0):
86+
cpdef utils.dpnp_descriptor dpnp_diagonal(dpnp_descriptor input, offset=0):
8787
cdef shape_type_c input_shape = input.shape
8888

8989
n = min(input.shape[0], input.shape[1])
@@ -100,18 +100,23 @@ cpdef dparray dpnp_diagonal(dpnp_descriptor input, offset=0):
100100
else:
101101
res_shape[-1] = n + offset
102102

103+
cdef shape_type_c result_shape = res_shape
103104
res_ndim = len(res_shape)
104105

105106
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
106107

107108
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_DIAGONAL, param1_type, param1_type)
108109

109-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
110-
cdef dparray result = dparray(res_shape, dtype=result_type)
110+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
111111

112112
cdef custom_indexing_2in_1out_func_ptr_t_ func = <custom_indexing_2in_1out_func_ptr_t_ > kernel_data.ptr
113113

114-
func(input.get_data(), result.get_data(), offset, < size_t * > input_shape.data(), < size_t * > result._dparray_shape.data(), res_ndim)
114+
func(input.get_data(),
115+
result.get_data(),
116+
offset,
117+
< size_t * > input_shape.data(),
118+
< size_t * > result_shape.data(),
119+
res_ndim)
115120

116121
return result
117122

dpnp/dpnp_iface_arraycreation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,9 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
742742

743743
if not use_origin_backend():
744744
if axis != 0:
745-
checker_throw_value_error("linspace", "axis", axis, 0)
746-
747-
return dpnp_geomspace(start, stop, num, endpoint, dtype, axis)
745+
pass
746+
else:
747+
return dpnp_geomspace(start, stop, num, endpoint, dtype, axis).get_pyobj()
748748

749749
return call_origin(numpy.geomspace, start, stop, num, endpoint, dtype, axis)
750750

@@ -778,7 +778,7 @@ def identity(n, dtype=None, *, like=None):
778778
else:
779779
if dtype is None:
780780
dtype = dpnp.float64
781-
return dpnp_identity(n, dtype)
781+
return dpnp_identity(n, dtype).get_pyobj()
782782

783783
return call_origin(numpy.identity, n, dtype=dtype, like=like)
784784

@@ -1175,7 +1175,7 @@ def tri(N, M=None, k=0, dtype=numpy.float, **kwargs):
11751175
elif not isinstance(k, int):
11761176
pass
11771177
else:
1178-
return dpnp_tri(N, M, k, dtype)
1178+
return dpnp_tri(N, M, k, dtype).get_pyobj()
11791179

11801180
return call_origin(numpy.tri, N, M, k, dtype, **kwargs)
11811181

dpnp/dpnp_iface_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def diagonal(x1, offset=0, axis1=0, axis2=1):
241241
elif axis2 != 1:
242242
pass
243243
else:
244-
return dpnp_diagonal(x1_desc, offset)
244+
return dpnp_diagonal(x1_desc, offset).get_pyobj()
245245

246246
return call_origin(numpy.diagonal, x1, offset, axis1, axis2)
247247

dpnp/dpnp_utils/dpnp_algo_utils.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ cpdef checker_throw_value_error(function_name, param_name, param, expected):
163163

164164

165165
cpdef dpnp_descriptor create_output_descriptor_py(shape_type_c output_shape, object d_type, object requested_out):
166-
cdef DPNPFuncType c_type = dpnp_dtype_to_DPNPFuncType(d_type)
166+
py_type = dpnp.default_float_type() if d_type is None else d_type
167+
168+
cdef DPNPFuncType c_type = dpnp_dtype_to_DPNPFuncType(py_type)
167169

168170
return create_output_descriptor(output_shape, c_type, requested_out)
169171

0 commit comments

Comments
 (0)