Skip to content

Linalg to desc 1 #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions dpnp/dpnp_algo/dpnp_algo_statistics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ cpdef dparray dpnp_cov(dparray array1):


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

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

func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)

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


cpdef dparray _dpnp_mean(dparray input):
cdef dparray_shape_type input_shape = input.shape
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)

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

func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis.data(), axis_size)
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis.data(), axis_size)

return result

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


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

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

func(input.get_data(), result.get_data(), < size_t * > input._dparray_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)
func(input.get_data(), result.get_data(), < size_t * > input_shape.data(), input.ndim, < size_t * > axis_.data(), axis_size)

dpnp_array = dpnp.array(result, dtype=input.dtype)
dpnp_result_array = dpnp_array.reshape(output_shape)
Expand Down
54 changes: 26 additions & 28 deletions dpnp/linalg/dpnp_algo_linalg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ and the rest of the library
"""

import dpnp
from dpnp.dpnp_utils cimport *
cimport dpnp.dpnp_utils as utils
from dpnp.dpnp_algo cimport *
from dpnp.dparray cimport dparray, dparray_shape_type
import numpy
Expand Down Expand Up @@ -63,24 +63,19 @@ ctypedef void(*custom_linalg_1in_3out_shape_t)(void * , void * , void * , void *
ctypedef void(*custom_linalg_2in_1out_func_ptr_t)(void * , void * , void * , size_t )


cpdef dparray dpnp_cholesky(dparray input):
if input.dtype == dpnp.int32 or input.dtype == dpnp.int64:
input_ = input.astype(dpnp.float64)
else:
input_ = input

cpdef utils.dpnp_descriptor dpnp_cholesky(utils.dpnp_descriptor input_):
size_ = input_.shape[-1]

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input_.dtype)

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

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

cdef custom_linalg_1in_1out_with_2size_func_ptr_t_ func = <custom_linalg_1in_1out_with_2size_func_ptr_t_ > kernel_data.ptr

func(input_.get_data(), result.get_data(), input.size, size_)
func(input_.get_data(), result.get_data(), input_.size, size_)

return result

Expand Down Expand Up @@ -108,30 +103,31 @@ cpdef dparray dpnp_cond(dparray input, p):
return ret


cpdef dparray dpnp_det(dparray input):
cpdef utils.dpnp_descriptor dpnp_det(utils.dpnp_descriptor input):
cdef dparray_shape_type input_shape = input.shape
cdef size_t n = input.shape[-1]
cdef size_t size_out = 1
if input.ndim != 2:
output_shape = tuple((list(input.shape))[:-2])
for i in range(len(output_shape)):
size_out *= output_shape[i]

cdef dparray_shape_type result_shape = (size_out,)
if size_out > 1:
result_shape = output_shape

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)

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

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

cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr

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

if size_out > 1:
dpnp_result = result.reshape(output_shape)
return dpnp_result
else:
return result
return result


cpdef tuple dpnp_eig(dparray x1):
Expand All @@ -154,17 +150,16 @@ cpdef tuple dpnp_eig(dparray x1):
return (res_val, res_vec)


cpdef dparray dpnp_eigvals(dparray input):
cpdef utils.dpnp_descriptor dpnp_eigvals(utils.dpnp_descriptor input):
cdef dparray_shape_type input_shape = input.shape

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

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

result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)

cdef dparray res_val = dparray((size,), dtype=result_type)
# ceate result array with type given by FPTR data
cdef utils.dpnp_descriptor res_val = utils.create_output_descriptor((size,), kernel_data.return_type, None)

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

cpdef dparray dpnp_inv(dparray input_):
cdef dparray input = input_.astype(dpnp.float64)
cdef dparray_shape_type input_shape = input.shape

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)

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

cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr

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

dpnp_result = result.reshape(input.shape)
return dpnp_result


cpdef dparray dpnp_matrix_rank(dparray input):
cpdef utils.dpnp_descriptor dpnp_matrix_rank(utils.dpnp_descriptor input):
cdef dparray_shape_type input_shape = input.shape
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)

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

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

cdef custom_linalg_1in_1out_func_ptr_t func = <custom_linalg_1in_1out_func_ptr_t > kernel_data.ptr

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

return result

Expand Down
52 changes: 27 additions & 25 deletions dpnp/linalg/dpnp_iface_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ def cholesky(input):
matrix object if `input` is a matrix object.
"""

if not use_origin_backend(input):
if not isinstance(input, dparray):
pass
elif input.shape[-1] != input.shape[-2]:
x1_desc = dpnp.get_dpnp_descriptor(input)
if x1_desc:
if x1_desc.shape[-1] != x1_desc.shape[-2]:
pass
else:
return dpnp_cholesky(input)
if input.dtype == dpnp.int32 or input.dtype == dpnp.int64:
# TODO memory copy. needs to move into DPNPC
input_ = dpnp.get_dpnp_descriptor(input.astype(dpnp.float64))
else:
input_ = x1_desc
return dpnp_cholesky(input_).get_pyobj()

return call_origin(numpy.linalg.cholesky, input)

Expand Down Expand Up @@ -140,11 +144,11 @@ def det(input):
det : (...) array_like
Determinant of `input`.
"""
is_input_dparray = isinstance(input, dparray)

if not use_origin_backend(input) and is_input_dparray:
if input.shape[-1] == input.shape[-2]:
result_obj = dpnp_det(input)
x1_desc = dpnp.get_dpnp_descriptor(input)
if x1_desc:
if x1_desc.shape[-1] == x1_desc.shape[-2]:
result_obj = dpnp_det(x1_desc).get_pyobj()
result = dpnp.convert_single_elem_array_to_scalar(result_obj)

return result
Expand Down Expand Up @@ -188,11 +192,10 @@ def eigvals(input):
real for real matrices.
"""

is_input_dparray = isinstance(input, dparray)

if (not use_origin_backend(input) and is_input_dparray):
if (input.size > 0):
return dpnp_eigvals(input)
x1_desc = dpnp.get_dpnp_descriptor(input)
if x1_desc:
if x1_desc.size > 0:
return dpnp_eigvals(x1_desc).get_pyobj()

return call_origin(numpy.linalg.eigvals, input)

Expand Down Expand Up @@ -276,18 +279,17 @@ def matrix_rank(input, tol=None, hermitian=False):

"""

is_input_dparray = isinstance(input, dparray)

if not use_origin_backend(input) and is_input_dparray:
x1_desc = dpnp.get_dpnp_descriptor(input)
if x1_desc:
if tol is not None:
checker_throw_value_error("matrix_rank", "tol", type(tol), None)
if hermitian is not False:
checker_throw_value_error("matrix_rank", "hermitian", hermitian, False)

result_obj = dpnp_matrix_rank(input)
result = dpnp.convert_single_elem_array_to_scalar(result_obj)

return result
pass
elif hermitian:
pass
else:
result_obj = dpnp_matrix_rank(x1_desc).get_pyobj()
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
return result

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

Expand Down