Skip to content

Commit c312c21

Browse files
authored
Linalg to desc 1 (#813)
1 parent 5bdd984 commit c312c21

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

dpnp/dpnp_algo/dpnp_algo_statistics.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ cpdef dparray dpnp_cov(dparray array1):
138138

139139

140140
cpdef dparray _dpnp_max(dparray input, _axis_, output_shape):
141+
cdef dparray_shape_type input_shape = input.shape
141142
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
142143

143144
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MAX, param1_type, param1_type)
@@ -157,7 +158,7 @@ cpdef dparray _dpnp_max(dparray input, _axis_, output_shape):
157158
axis_.push_back(shape_it)
158159
axis_size = len(axis)
159160

160-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
161+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
161162

162163
dpnp_array = dpnp.array(result, dtype=input.dtype)
163164
dpnp_result_array = dpnp_array.reshape(output_shape)
@@ -194,6 +195,7 @@ cpdef dparray dpnp_max(dparray input, axis):
194195

195196

196197
cpdef dparray _dpnp_mean(dparray input):
198+
cdef dparray_shape_type input_shape = input.shape
197199
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
198200

199201
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MEAN, param1_type, param1_type)
@@ -207,7 +209,7 @@ cpdef dparray _dpnp_mean(dparray input):
207209
cdef dparray_shape_type axis
208210
cdef Py_ssize_t axis_size = 0
209211

210-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis.data(), axis_size)
212+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis.data(), axis_size)
211213

212214
return result
213215

@@ -336,6 +338,7 @@ cpdef dparray dpnp_median(utils.dpnp_descriptor array1):
336338

337339

338340
cpdef dparray _dpnp_min(dparray input, _axis_, output_shape):
341+
cdef dparray_shape_type input_shape = input.shape
339342
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
340343

341344
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MIN, param1_type, param1_type)
@@ -357,7 +360,7 @@ cpdef dparray _dpnp_min(dparray input, _axis_, output_shape):
357360
axis_.push_back(shape_it)
358361
axis_size = len(axis)
359362

360-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
363+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
361364

362365
dpnp_array = dpnp.array(result, dtype=input.dtype)
363366
dpnp_result_array = dpnp_array.reshape(output_shape)

dpnp/linalg/dpnp_algo_linalg.pyx

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ and the rest of the library
3333
"""
3434

3535
import dpnp
36-
from dpnp.dpnp_utils cimport *
36+
cimport dpnp.dpnp_utils as utils
3737
from dpnp.dpnp_algo cimport *
3838
from dpnp.dparray cimport dparray, dparray_shape_type
3939
import numpy
@@ -63,24 +63,19 @@ ctypedef void(*custom_linalg_1in_3out_shape_t)(void * , void * , void * , void *
6363
ctypedef void(*custom_linalg_2in_1out_func_ptr_t)(void * , void * , void * , size_t )
6464

6565

66-
cpdef dparray dpnp_cholesky(dparray input):
67-
if input.dtype == dpnp.int32 or input.dtype == dpnp.int64:
68-
input_ = input.astype(dpnp.float64)
69-
else:
70-
input_ = input
71-
66+
cpdef utils.dpnp_descriptor dpnp_cholesky(utils.dpnp_descriptor input_):
7267
size_ = input_.shape[-1]
7368

7469
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input_.dtype)
7570

7671
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_CHOLESKY, param1_type, param1_type)
7772

78-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
79-
cdef dparray result = dparray(input_.shape, dtype=result_type)
73+
# ceate result array with type given by FPTR data
74+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(input_.shape, kernel_data.return_type, None)
8075

8176
cdef custom_linalg_1in_1out_with_2size_func_ptr_t_ func = <custom_linalg_1in_1out_with_2size_func_ptr_t_ > kernel_data.ptr
8277

83-
func(input_.get_data(), result.get_data(), input.size, size_)
78+
func(input_.get_data(), result.get_data(), input_.size, size_)
8479

8580
return result
8681

@@ -108,30 +103,31 @@ cpdef dparray dpnp_cond(dparray input, p):
108103
return ret
109104

110105

111-
cpdef dparray dpnp_det(dparray input):
106+
cpdef utils.dpnp_descriptor dpnp_det(utils.dpnp_descriptor input):
107+
cdef dparray_shape_type input_shape = input.shape
112108
cdef size_t n = input.shape[-1]
113109
cdef size_t size_out = 1
114110
if input.ndim != 2:
115111
output_shape = tuple((list(input.shape))[:-2])
116112
for i in range(len(output_shape)):
117113
size_out *= output_shape[i]
118114

115+
cdef dparray_shape_type result_shape = (size_out,)
116+
if size_out > 1:
117+
result_shape = output_shape
118+
119119
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
120120

121121
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_DET, param1_type, param1_type)
122122

123-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
124-
cdef dparray result = dparray(size_out, dtype=result_type)
123+
# ceate result array with type given by FPTR data
124+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
125125

126126
cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr
127127

128-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim)
128+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim)
129129

130-
if size_out > 1:
131-
dpnp_result = result.reshape(output_shape)
132-
return dpnp_result
133-
else:
134-
return result
130+
return result
135131

136132

137133
cpdef tuple dpnp_eig(dparray x1):
@@ -154,17 +150,16 @@ cpdef tuple dpnp_eig(dparray x1):
154150
return (res_val, res_vec)
155151

156152

157-
cpdef dparray dpnp_eigvals(dparray input):
153+
cpdef utils.dpnp_descriptor dpnp_eigvals(utils.dpnp_descriptor input):
158154
cdef dparray_shape_type input_shape = input.shape
159155

160156
cdef size_t size = 0 if input_shape.empty() else input_shape.front()
161157

162158
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
163159
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_EIGVALS, param1_type, param1_type)
164160

165-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
166-
167-
cdef dparray res_val = dparray((size,), dtype=result_type)
161+
# ceate result array with type given by FPTR data
162+
cdef utils.dpnp_descriptor res_val = utils.create_output_descriptor((size,), kernel_data.return_type, None)
168163

169164
cdef custom_linalg_1in_1out_with_size_func_ptr_t_ func = <custom_linalg_1in_1out_with_size_func_ptr_t_ > kernel_data.ptr
170165
# call FPTR function
@@ -175,6 +170,8 @@ cpdef dparray dpnp_eigvals(dparray input):
175170

176171
cpdef dparray dpnp_inv(dparray input_):
177172
cdef dparray input = input_.astype(dpnp.float64)
173+
cdef dparray_shape_type input_shape = input.shape
174+
178175
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
179176

180177
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_INV, param1_type, param1_type)
@@ -183,23 +180,24 @@ cpdef dparray dpnp_inv(dparray input_):
183180

184181
cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr
185182

186-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim)
183+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim)
187184

188185
dpnp_result = result.reshape(input.shape)
189186
return dpnp_result
190187

191188

192-
cpdef dparray dpnp_matrix_rank(dparray input):
189+
cpdef utils.dpnp_descriptor dpnp_matrix_rank(utils.dpnp_descriptor input):
190+
cdef dparray_shape_type input_shape = input.shape
193191
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
194192

195193
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MATRIX_RANK, param1_type, param1_type)
196194

197-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
198-
cdef dparray result = dparray((1,), dtype=result_type)
195+
# ceate result array with type given by FPTR data
196+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor((1,), kernel_data.return_type, None)
199197

200198
cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr
201199

202-
func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim)
200+
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim)
203201

204202
return result
205203

dpnp/linalg/dpnp_iface_linalg.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ def cholesky(input):
8888
matrix object if `input` is a matrix object.
8989
"""
9090

91-
if not use_origin_backend(input):
92-
if not isinstance(input, dparray):
93-
pass
94-
elif input.shape[-1] != input.shape[-2]:
91+
x1_desc = dpnp.get_dpnp_descriptor(input)
92+
if x1_desc:
93+
if x1_desc.shape[-1] != x1_desc.shape[-2]:
9594
pass
9695
else:
97-
return dpnp_cholesky(input)
96+
if input.dtype == dpnp.int32 or input.dtype == dpnp.int64:
97+
# TODO memory copy. needs to move into DPNPC
98+
input_ = dpnp.get_dpnp_descriptor(input.astype(dpnp.float64))
99+
else:
100+
input_ = x1_desc
101+
return dpnp_cholesky(input_).get_pyobj()
98102

99103
return call_origin(numpy.linalg.cholesky, input)
100104

@@ -140,11 +144,11 @@ def det(input):
140144
det : (...) array_like
141145
Determinant of `input`.
142146
"""
143-
is_input_dparray = isinstance(input, dparray)
144147

145-
if not use_origin_backend(input) and is_input_dparray:
146-
if input.shape[-1] == input.shape[-2]:
147-
result_obj = dpnp_det(input)
148+
x1_desc = dpnp.get_dpnp_descriptor(input)
149+
if x1_desc:
150+
if x1_desc.shape[-1] == x1_desc.shape[-2]:
151+
result_obj = dpnp_det(x1_desc).get_pyobj()
148152
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
149153

150154
return result
@@ -188,11 +192,10 @@ def eigvals(input):
188192
real for real matrices.
189193
"""
190194

191-
is_input_dparray = isinstance(input, dparray)
192-
193-
if (not use_origin_backend(input) and is_input_dparray):
194-
if (input.size > 0):
195-
return dpnp_eigvals(input)
195+
x1_desc = dpnp.get_dpnp_descriptor(input)
196+
if x1_desc:
197+
if x1_desc.size > 0:
198+
return dpnp_eigvals(x1_desc).get_pyobj()
196199

197200
return call_origin(numpy.linalg.eigvals, input)
198201

@@ -276,18 +279,17 @@ def matrix_rank(input, tol=None, hermitian=False):
276279
277280
"""
278281

279-
is_input_dparray = isinstance(input, dparray)
280-
281-
if not use_origin_backend(input) and is_input_dparray:
282+
x1_desc = dpnp.get_dpnp_descriptor(input)
283+
if x1_desc:
282284
if tol is not None:
283-
checker_throw_value_error("matrix_rank", "tol", type(tol), None)
284-
if hermitian is not False:
285-
checker_throw_value_error("matrix_rank", "hermitian", hermitian, False)
286-
287-
result_obj = dpnp_matrix_rank(input)
288-
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
289-
290-
return result
285+
pass
286+
elif hermitian:
287+
pass
288+
else:
289+
result_obj = dpnp_matrix_rank(x1_desc).get_pyobj()
290+
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
291+
292+
return result
291293

292294
return call_origin(numpy.linalg.matrix_rank, input, tol, hermitian)
293295

0 commit comments

Comments
 (0)