Skip to content
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

Update dpnp.astype function #1597

Merged
merged 6 commits into from
Oct 18, 2023
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
2 changes: 0 additions & 2 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ enum class DPNPFuncName : size_t
parameters */
DPNP_FN_AROUND, /**< Used in numpy.around() impl */
DPNP_FN_ASTYPE, /**< Used in numpy.astype() impl */
DPNP_FN_ASTYPE_EXT, /**< Used in numpy.astype() impl, requires extra
parameters */
DPNP_FN_BITWISE_AND, /**< Used in numpy.bitwise_and() impl */
DPNP_FN_BITWISE_OR, /**< Used in numpy.bitwise_or() impl */
DPNP_FN_BITWISE_XOR, /**< Used in numpy.bitwise_xor() impl */
Expand Down
65 changes: 0 additions & 65 deletions dpnp/backend/kernels/dpnp_krnl_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ template <typename _DataType, typename _ResultType>
void (*dpnp_astype_default_c)(const void *, void *, const size_t) =
dpnp_astype_c<_DataType, _ResultType>;

template <typename _DataType, typename _ResultType>
DPCTLSyclEventRef (*dpnp_astype_ext_c)(DPCTLSyclQueueRef,
const void *,
void *,
const size_t,
const DPCTLEventVectorRef) =
dpnp_astype_c<_DataType, _ResultType>;

template <typename _KernelNameSpecialization1,
typename _KernelNameSpecialization2,
typename _KernelNameSpecialization3>
Expand Down Expand Up @@ -1035,63 +1027,6 @@ void func_map_init_linalg(func_map_t &fmap)
(void *)
dpnp_astype_default_c<std::complex<double>, std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_BLN] = {
eft_BLN, (void *)dpnp_astype_ext_c<bool, bool>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_INT] = {
eft_INT, (void *)dpnp_astype_ext_c<bool, int32_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_LNG] = {
eft_LNG, (void *)dpnp_astype_ext_c<bool, int64_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_FLT] = {
eft_FLT, (void *)dpnp_astype_ext_c<bool, float>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_DBL] = {
eft_DBL, (void *)dpnp_astype_ext_c<bool, double>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_BLN] = {
eft_BLN, (void *)dpnp_astype_ext_c<int32_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_astype_ext_c<int32_t, int32_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_LNG] = {
eft_LNG, (void *)dpnp_astype_ext_c<int32_t, int64_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_FLT] = {
eft_FLT, (void *)dpnp_astype_ext_c<int32_t, float>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_DBL] = {
eft_DBL, (void *)dpnp_astype_ext_c<int32_t, double>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_BLN] = {
eft_BLN, (void *)dpnp_astype_ext_c<int64_t, bool>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_INT] = {
eft_INT, (void *)dpnp_astype_ext_c<int64_t, int32_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_astype_ext_c<int64_t, int64_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_FLT] = {
eft_FLT, (void *)dpnp_astype_ext_c<int64_t, float>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_DBL] = {
eft_DBL, (void *)dpnp_astype_ext_c<int64_t, double>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_BLN] = {
eft_BLN, (void *)dpnp_astype_ext_c<float, bool>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_INT] = {
eft_INT, (void *)dpnp_astype_ext_c<float, int32_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_LNG] = {
eft_LNG, (void *)dpnp_astype_ext_c<float, int64_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_astype_ext_c<float, float>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_DBL] = {
eft_DBL, (void *)dpnp_astype_ext_c<float, double>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_BLN] = {
eft_BLN, (void *)dpnp_astype_ext_c<double, bool>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_INT] = {
eft_INT, (void *)dpnp_astype_ext_c<double, int32_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_LNG] = {
eft_LNG, (void *)dpnp_astype_ext_c<double, int64_t>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_FLT] = {
eft_FLT, (void *)dpnp_astype_ext_c<double, float>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_astype_ext_c<double, double>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_C64][eft_C64] = {
eft_C64,
(void *)dpnp_astype_ext_c<std::complex<float>, std::complex<float>>};
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_C128][eft_C128] = {
eft_C128,
(void *)dpnp_astype_ext_c<std::complex<double>, std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_DOT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_dot_default_c<int32_t, int32_t, int32_t>};
fmap[DPNPFuncName::DPNP_FN_DOT][eft_INT][eft_LNG] = {
Expand Down
1 change: 0 additions & 1 deletion dpnp/dparray.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ cdef class dparray:
cdef void * get_data(self)

cpdef item(self, id=*)
cpdef dparray astype(self, dtype, order=*, casting=*, subok=*, copy=*)
58 changes: 23 additions & 35 deletions dpnp/dparray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ from libcpp cimport bool as cpp_bool
import numpy

from dpnp.dpnp_algo import (
dpnp_astype,
dpnp_flatten,
)

# to avoid interference with Python internal functions
from dpnp.dpnp_iface import asnumpy
from dpnp.dpnp_iface import asnumpy, astype
from dpnp.dpnp_iface import get_dpnp_descriptor as iface_get_dpnp_descriptor
from dpnp.dpnp_iface import prod as iface_prod
from dpnp.dpnp_iface import sum as iface_sum
Expand Down Expand Up @@ -870,47 +869,36 @@ cdef class dparray:
def __truediv__(self, other):
return divide(self, other)

cpdef dparray astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
"""Copy the array with data type casting.
def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
"""
Copy the array with data type casting.

Args:
dtype: Target type.
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy (bool): If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.
Parameters
----------
dtype : dtype
Target data type.
order : {'C', 'F', 'A', 'K'}
Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy : bool
If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.

Returns:
Returns
-------
out : dpnp.ndarray
If ``copy`` is False and no cast is required, then the array itself is returned.
Otherwise, it returns a (possibly casted) copy of the array.

.. note::
This method currently does not support `order``, `casting``, ``copy``, and ``subok`` arguments.

.. seealso:: :meth:`numpy.ndarray.astype`
Limitations
-----------
Parameter `subok` is supported with default value.
Otherwise ``NotImplementedError`` exception will be raised.

"""

if casting is not 'unsafe':
pass
elif subok is not True:
pass
elif copy is not True:
pass
elif order is not 'K':
pass
elif self.dtype == numpy.complex128 or dtype == numpy.complex128:
pass
elif self.dtype == numpy.complex64 or dtype == numpy.complex64:
pass
else:
self_desc = iface_get_dpnp_descriptor(self)
return dpnp_astype(self_desc, dtype).get_pyobj()

result = dp2nd_array(self).astype(dtype=dtype, order=order, casting=casting, subok=subok, copy=copy)

return nd2dp_array(result)
return astype(self, dtype, order=order, casting=casting, subok=subok, copy=copy)

def conj(self):
"""
Expand Down
3 changes: 0 additions & 3 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_ARGMIN_EXT
DPNP_FN_ARGSORT
DPNP_FN_ARGSORT_EXT
DPNP_FN_ASTYPE
DPNP_FN_ASTYPE_EXT
DPNP_FN_CBRT
DPNP_FN_CBRT_EXT
DPNP_FN_CHOLESKY
Expand Down Expand Up @@ -333,7 +331,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*dpnp_reduction_c_t)(c_dpctl.DPCTLSyclQueueRe
const long*,
const c_dpctl.DPCTLEventVectorRef)

cpdef dpnp_descriptor dpnp_astype(dpnp_descriptor x1, dtype)
cpdef dpnp_descriptor dpnp_flatten(dpnp_descriptor x1)


Expand Down
35 changes: 0 additions & 35 deletions dpnp/dpnp_algo/dpnp_algo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import operator
import numpy

__all__ = [
"dpnp_astype",
"dpnp_flatten",
"dpnp_queue_initialize",
]
Expand All @@ -74,9 +73,6 @@ include "dpnp_algo_statistics.pxi"
include "dpnp_algo_trigonometric.pxi"


ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_astype_t)(c_dpctl.DPCTLSyclQueueRef,
const void *, void * , const size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_flatten_t)(c_dpctl.DPCTLSyclQueueRef,
void *, const size_t, const size_t,
const shape_elem_type * , const shape_elem_type * ,
Expand All @@ -86,37 +82,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_flatten_t)(c_dpctl.DPCTLSyclQueueR
const c_dpctl.DPCTLEventVectorRef)


cpdef utils.dpnp_descriptor dpnp_astype(utils.dpnp_descriptor x1, dtype):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ASTYPE_EXT, param1_type, param2_type)

x1_obj = x1.get_array()

# create result array with type given by FPTR data
cdef shape_type_c result_shape = x1.shape
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
kernel_data.return_type,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef fptr_dpnp_astype_t func = <fptr_dpnp_astype_t > kernel_data.ptr
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, x1.get_data(), result.get_data(), x1.size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_flatten(utils.dpnp_descriptor x1):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)

Expand Down
70 changes: 51 additions & 19 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,32 +597,64 @@ def asnumpy(self):
return dpt.asnumpy(self._array_obj)

def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True):
"""Copy the array with data type casting.
"""
Copy the array with data type casting.
npolina4 marked this conversation as resolved.
Show resolved Hide resolved

Args:
dtype: Target type.
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy (bool): If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.
For full documentation refer to :obj:`numpy.ndarray.astype`.

Returns:
If ``copy`` is False and no cast is required, then the array itself is returned.
Otherwise, it returns a (possibly casted) copy of the array.
Parameters
----------
x1 : {dpnp.ndarray, usm_ndarray}
Array data type casting.
dtype : dtype
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
Target data type.
order : {'C', 'F', 'A', 'K'}
Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy : bool
If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
Controls what kind of data casting may occur. Defaults to 'unsafe' for backwards compatibility.
'no' means the data types should not be cast at all.
'equiv' means only byte-order changes are allowed.
'safe' means only casts which can preserve values are allowed.
'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed.
'unsafe' means any data conversions may be done.
copy : bool, optional
By default, astype always returns a newly allocated array. If this is set to false, and the dtype,
order, and subok requirements are satisfied, the input array is returned instead of a copy.

.. note::
This method currently does not support `order``, `casting``, ``copy``, and ``subok`` arguments.
Returns
-------
arr_t : dpnp.ndarray
Unless `copy` is ``False`` and the other conditions for returning the input array
are satisfied, `arr_t` is a new array of the same shape as the input array,
with dtype, order given by dtype, order.

.. seealso:: :meth:`numpy.ndarray.astype`
Limitations
-----------
Parameter `subok` is supported with default value.
Otherwise ``NotImplementedError`` exception will be raised.

Examples
--------
>>> import dpnp as np
>>> x = np.array([1, 2, 2.5])
>>> x
array([1. , 2. , 2.5])
>>> x.astype(int)
array([1, 2, 2])

"""

new_array = self.__new__(dpnp_array)
new_array._array_obj = dpt.astype(
self._array_obj, dtype, order=order, casting=casting, copy=copy
)
return new_array
if subok is not True:
raise NotImplementedError(
f"subok={subok} is currently not supported"
)

return dpnp.astype(self, dtype, order=order, casting=casting, copy=copy)

# 'base',
# 'byteswap',
Expand Down
Loading