Skip to content

Commit 142aa43

Browse files
authored
Merge branch 'master' into max_min
2 parents 2cdf0c3 + 38b0c4f commit 142aa43

18 files changed

+699
-446
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ enum class DPNPFuncName : size_t
8686
parameters */
8787
DPNP_FN_AROUND, /**< Used in numpy.around() impl */
8888
DPNP_FN_ASTYPE, /**< Used in numpy.astype() impl */
89-
DPNP_FN_ASTYPE_EXT, /**< Used in numpy.astype() impl, requires extra
90-
parameters */
9189
DPNP_FN_BITWISE_AND, /**< Used in numpy.bitwise_and() impl */
9290
DPNP_FN_BITWISE_OR, /**< Used in numpy.bitwise_or() impl */
9391
DPNP_FN_BITWISE_XOR, /**< Used in numpy.bitwise_xor() impl */

dpnp/backend/kernels/dpnp_krnl_common.cpp

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ template <typename _DataType, typename _ResultType>
101101
void (*dpnp_astype_default_c)(const void *, void *, const size_t) =
102102
dpnp_astype_c<_DataType, _ResultType>;
103103

104-
template <typename _DataType, typename _ResultType>
105-
DPCTLSyclEventRef (*dpnp_astype_ext_c)(DPCTLSyclQueueRef,
106-
const void *,
107-
void *,
108-
const size_t,
109-
const DPCTLEventVectorRef) =
110-
dpnp_astype_c<_DataType, _ResultType>;
111-
112104
template <typename _KernelNameSpecialization1,
113105
typename _KernelNameSpecialization2,
114106
typename _KernelNameSpecialization3>
@@ -1035,63 +1027,6 @@ void func_map_init_linalg(func_map_t &fmap)
10351027
(void *)
10361028
dpnp_astype_default_c<std::complex<double>, std::complex<double>>};
10371029

1038-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_BLN] = {
1039-
eft_BLN, (void *)dpnp_astype_ext_c<bool, bool>};
1040-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_INT] = {
1041-
eft_INT, (void *)dpnp_astype_ext_c<bool, int32_t>};
1042-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_LNG] = {
1043-
eft_LNG, (void *)dpnp_astype_ext_c<bool, int64_t>};
1044-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_FLT] = {
1045-
eft_FLT, (void *)dpnp_astype_ext_c<bool, float>};
1046-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_BLN][eft_DBL] = {
1047-
eft_DBL, (void *)dpnp_astype_ext_c<bool, double>};
1048-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_BLN] = {
1049-
eft_BLN, (void *)dpnp_astype_ext_c<int32_t, bool>};
1050-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_INT] = {
1051-
eft_INT, (void *)dpnp_astype_ext_c<int32_t, int32_t>};
1052-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_LNG] = {
1053-
eft_LNG, (void *)dpnp_astype_ext_c<int32_t, int64_t>};
1054-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_FLT] = {
1055-
eft_FLT, (void *)dpnp_astype_ext_c<int32_t, float>};
1056-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_INT][eft_DBL] = {
1057-
eft_DBL, (void *)dpnp_astype_ext_c<int32_t, double>};
1058-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_BLN] = {
1059-
eft_BLN, (void *)dpnp_astype_ext_c<int64_t, bool>};
1060-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_INT] = {
1061-
eft_INT, (void *)dpnp_astype_ext_c<int64_t, int32_t>};
1062-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_LNG] = {
1063-
eft_LNG, (void *)dpnp_astype_ext_c<int64_t, int64_t>};
1064-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_FLT] = {
1065-
eft_FLT, (void *)dpnp_astype_ext_c<int64_t, float>};
1066-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_LNG][eft_DBL] = {
1067-
eft_DBL, (void *)dpnp_astype_ext_c<int64_t, double>};
1068-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_BLN] = {
1069-
eft_BLN, (void *)dpnp_astype_ext_c<float, bool>};
1070-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_INT] = {
1071-
eft_INT, (void *)dpnp_astype_ext_c<float, int32_t>};
1072-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_LNG] = {
1073-
eft_LNG, (void *)dpnp_astype_ext_c<float, int64_t>};
1074-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_FLT] = {
1075-
eft_FLT, (void *)dpnp_astype_ext_c<float, float>};
1076-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_FLT][eft_DBL] = {
1077-
eft_DBL, (void *)dpnp_astype_ext_c<float, double>};
1078-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_BLN] = {
1079-
eft_BLN, (void *)dpnp_astype_ext_c<double, bool>};
1080-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_INT] = {
1081-
eft_INT, (void *)dpnp_astype_ext_c<double, int32_t>};
1082-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_LNG] = {
1083-
eft_LNG, (void *)dpnp_astype_ext_c<double, int64_t>};
1084-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_FLT] = {
1085-
eft_FLT, (void *)dpnp_astype_ext_c<double, float>};
1086-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_DBL][eft_DBL] = {
1087-
eft_DBL, (void *)dpnp_astype_ext_c<double, double>};
1088-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_C64][eft_C64] = {
1089-
eft_C64,
1090-
(void *)dpnp_astype_ext_c<std::complex<float>, std::complex<float>>};
1091-
fmap[DPNPFuncName::DPNP_FN_ASTYPE_EXT][eft_C128][eft_C128] = {
1092-
eft_C128,
1093-
(void *)dpnp_astype_ext_c<std::complex<double>, std::complex<double>>};
1094-
10951030
fmap[DPNPFuncName::DPNP_FN_DOT][eft_INT][eft_INT] = {
10961031
eft_INT, (void *)dpnp_dot_default_c<int32_t, int32_t, int32_t>};
10971032
fmap[DPNPFuncName::DPNP_FN_DOT][eft_INT][eft_LNG] = {

dpnp/dparray.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,3 @@ cdef class dparray:
5050
cdef void * get_data(self)
5151

5252
cpdef item(self, id=*)
53-
cpdef dparray astype(self, dtype, order=*, casting=*, subok=*, copy=*)

dpnp/dparray.pyx

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ from libcpp cimport bool as cpp_bool
4141
import numpy
4242

4343
from dpnp.dpnp_algo import (
44-
dpnp_astype,
4544
dpnp_flatten,
4645
)
4746

4847
# to avoid interference with Python internal functions
49-
from dpnp.dpnp_iface import asnumpy
48+
from dpnp.dpnp_iface import asnumpy, astype
5049
from dpnp.dpnp_iface import get_dpnp_descriptor as iface_get_dpnp_descriptor
5150
from dpnp.dpnp_iface import prod as iface_prod
5251
from dpnp.dpnp_iface import sum as iface_sum
@@ -870,47 +869,36 @@ cdef class dparray:
870869
def __truediv__(self, other):
871870
return divide(self, other)
872871
873-
cpdef dparray astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
874-
"""Copy the array with data type casting.
872+
def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
873+
"""
874+
Copy the array with data type casting.
875875

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

884-
Returns:
888+
Returns
889+
-------
890+
out : dpnp.ndarray
885891
If ``copy`` is False and no cast is required, then the array itself is returned.
886892
Otherwise, it returns a (possibly casted) copy of the array.
887893

888-
.. note::
889-
This method currently does not support `order``, `casting``, ``copy``, and ``subok`` arguments.
890-
891-
.. seealso:: :meth:`numpy.ndarray.astype`
894+
Limitations
895+
-----------
896+
Parameter `subok` is supported with default value.
897+
Otherwise ``NotImplementedError`` exception will be raised.
892898

893899
"""
894900
895-
if casting is not 'unsafe':
896-
pass
897-
elif subok is not True:
898-
pass
899-
elif copy is not True:
900-
pass
901-
elif order is not 'K':
902-
pass
903-
elif self.dtype == numpy.complex128 or dtype == numpy.complex128:
904-
pass
905-
elif self.dtype == numpy.complex64 or dtype == numpy.complex64:
906-
pass
907-
else:
908-
self_desc = iface_get_dpnp_descriptor(self)
909-
return dpnp_astype(self_desc, dtype).get_pyobj()
910-
911-
result = dp2nd_array(self).astype(dtype=dtype, order=order, casting=casting, subok=subok, copy=copy)
912-
913-
return nd2dp_array(result)
901+
return astype(self, dtype, order=order, casting=casting, subok=subok, copy=copy)
914902
915903
def conj(self):
916904
"""

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
4242
DPNP_FN_ARGMIN_EXT
4343
DPNP_FN_ARGSORT
4444
DPNP_FN_ARGSORT_EXT
45-
DPNP_FN_ASTYPE
46-
DPNP_FN_ASTYPE_EXT
4745
DPNP_FN_CBRT
4846
DPNP_FN_CBRT_EXT
4947
DPNP_FN_CHOLESKY
@@ -329,7 +327,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*dpnp_reduction_c_t)(c_dpctl.DPCTLSyclQueueRe
329327
const long*,
330328
const c_dpctl.DPCTLEventVectorRef)
331329

332-
cpdef dpnp_descriptor dpnp_astype(dpnp_descriptor x1, dtype)
333330
cpdef dpnp_descriptor dpnp_flatten(dpnp_descriptor x1)
334331

335332

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import operator
5454
import numpy
5555

5656
__all__ = [
57-
"dpnp_astype",
5857
"dpnp_flatten",
5958
"dpnp_queue_initialize",
6059
]
@@ -74,9 +73,6 @@ include "dpnp_algo_statistics.pxi"
7473
include "dpnp_algo_trigonometric.pxi"
7574

7675

77-
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_astype_t)(c_dpctl.DPCTLSyclQueueRef,
78-
const void *, void * , const size_t,
79-
const c_dpctl.DPCTLEventVectorRef)
8076
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_flatten_t)(c_dpctl.DPCTLSyclQueueRef,
8177
void *, const size_t, const size_t,
8278
const shape_elem_type * , const shape_elem_type * ,
@@ -86,37 +82,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_flatten_t)(c_dpctl.DPCTLSyclQueueR
8682
const c_dpctl.DPCTLEventVectorRef)
8783

8884

89-
cpdef utils.dpnp_descriptor dpnp_astype(utils.dpnp_descriptor x1, dtype):
90-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
91-
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(dtype)
92-
93-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ASTYPE_EXT, param1_type, param2_type)
94-
95-
x1_obj = x1.get_array()
96-
97-
# create result array with type given by FPTR data
98-
cdef shape_type_c result_shape = x1.shape
99-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
100-
kernel_data.return_type,
101-
None,
102-
device=x1_obj.sycl_device,
103-
usm_type=x1_obj.usm_type,
104-
sycl_queue=x1_obj.sycl_queue)
105-
106-
result_sycl_queue = result.get_array().sycl_queue
107-
108-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
109-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
110-
111-
cdef fptr_dpnp_astype_t func = <fptr_dpnp_astype_t > kernel_data.ptr
112-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, x1.get_data(), result.get_data(), x1.size, NULL)
113-
114-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
115-
c_dpctl.DPCTLEvent_Delete(event_ref)
116-
117-
return result
118-
119-
12085
cpdef utils.dpnp_descriptor dpnp_flatten(utils.dpnp_descriptor x1):
12186
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
12287

dpnp/dpnp_array.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -596,32 +596,64 @@ def asnumpy(self):
596596
return dpt.asnumpy(self._array_obj)
597597

598598
def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True):
599-
"""Copy the array with data type casting.
599+
"""
600+
Copy the array with data type casting.
600601
601-
Args:
602-
dtype: Target type.
603-
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major (Fortran-style) order.
604-
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
605-
And when ``order`` is 'K', it keeps strides as closely as possible.
606-
copy (bool): If it is False and no cast happens, then this method returns the array itself.
607-
Otherwise, a copy is returned.
602+
For full documentation refer to :obj:`numpy.ndarray.astype`.
608603
609-
Returns:
610-
If ``copy`` is False and no cast is required, then the array itself is returned.
611-
Otherwise, it returns a (possibly casted) copy of the array.
604+
Parameters
605+
----------
606+
x1 : {dpnp.ndarray, usm_ndarray}
607+
Array data type casting.
608+
dtype : dtype
609+
Target data type.
610+
order : {'C', 'F', 'A', 'K'}
611+
Row-major (C-style) or column-major (Fortran-style) order.
612+
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
613+
And when ``order`` is 'K', it keeps strides as closely as possible.
614+
copy : bool
615+
If it is False and no cast happens, then this method returns the array itself.
616+
Otherwise, a copy is returned.
617+
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
618+
Controls what kind of data casting may occur. Defaults to 'unsafe' for backwards compatibility.
619+
'no' means the data types should not be cast at all.
620+
'equiv' means only byte-order changes are allowed.
621+
'safe' means only casts which can preserve values are allowed.
622+
'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed.
623+
'unsafe' means any data conversions may be done.
624+
copy : bool, optional
625+
By default, astype always returns a newly allocated array. If this is set to false, and the dtype,
626+
order, and subok requirements are satisfied, the input array is returned instead of a copy.
612627
613-
.. note::
614-
This method currently does not support `order``, `casting``, ``copy``, and ``subok`` arguments.
628+
Returns
629+
-------
630+
arr_t : dpnp.ndarray
631+
Unless `copy` is ``False`` and the other conditions for returning the input array
632+
are satisfied, `arr_t` is a new array of the same shape as the input array,
633+
with dtype, order given by dtype, order.
615634
616-
.. seealso:: :meth:`numpy.ndarray.astype`
635+
Limitations
636+
-----------
637+
Parameter `subok` is supported with default value.
638+
Otherwise ``NotImplementedError`` exception will be raised.
639+
640+
Examples
641+
--------
642+
>>> import dpnp as np
643+
>>> x = np.array([1, 2, 2.5])
644+
>>> x
645+
array([1. , 2. , 2.5])
646+
>>> x.astype(int)
647+
array([1, 2, 2])
617648
618649
"""
619650

620-
new_array = self.__new__(dpnp_array)
621-
new_array._array_obj = dpt.astype(
622-
self._array_obj, dtype, order=order, casting=casting, copy=copy
623-
)
624-
return new_array
651+
if subok is not True:
652+
raise NotImplementedError(
653+
f"subok={subok} is currently not supported"
654+
)
655+
656+
return dpnp.astype(self, dtype, order=order, casting=casting, copy=copy)
625657

626658
# 'base',
627659
# 'byteswap',

0 commit comments

Comments
 (0)