Skip to content

Commit 89668ff

Browse files
committed
Move astype function to arraycreation namespace
1 parent 9921320 commit 89668ff

File tree

2 files changed

+91
-91
lines changed

2 files changed

+91
-91
lines changed

dpnp/dpnp_iface.py

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
3838
"""
3939
# pylint: disable=protected-access
40+
# pylint: disable=redefined-outer-name
4041

4142
import os
4243

@@ -57,7 +58,6 @@
5758
__all__ = [
5859
"are_same_logical_tensors",
5960
"asnumpy",
60-
"astype",
6161
"as_usm_ndarray",
6262
"check_limitations",
6363
"check_supported_arrays_type",
@@ -209,96 +209,6 @@ def asnumpy(a, order="C"):
209209
return numpy.asarray(a, order=order)
210210

211211

212-
# pylint: disable=redefined-outer-name
213-
def astype(x, dtype, /, *, order="K", casting="unsafe", copy=True, device=None):
214-
"""
215-
Copy the array with data type casting.
216-
217-
Parameters
218-
----------
219-
x : {dpnp.ndarray, usm_ndarray}
220-
Array data type casting.
221-
dtype : {None, str, dtype object}
222-
Target data type.
223-
order : {None, "C", "F", "A", "K"}, optional
224-
Row-major (C-style) or column-major (Fortran-style) order.
225-
When `order` is ``"A"``, it uses ``"F"`` if `a` is column-major and
226-
uses ``"C"`` otherwise. And when `order` is ``"K"``, it keeps strides
227-
as closely as possible.
228-
229-
Default: ``"K"``.
230-
casting : {"no", "equiv", "safe", "same_kind", "unsafe"}, optional
231-
Controls what kind of data casting may occur. Defaults to ``"unsafe"``
232-
for backwards compatibility.
233-
234-
- "no" means the data types should not be cast at all.
235-
- "equiv" means only byte-order changes are allowed.
236-
- "safe" means only casts which can preserve values are allowed.
237-
- "same_kind" means only safe casts or casts within a kind, like
238-
float64 to float32, are allowed.
239-
- "unsafe" means any data conversions may be done.
240-
241-
Default: ``"unsafe"``.
242-
copy : bool, optional
243-
Specifies whether to copy an array when the specified dtype matches the
244-
data type of the input array ``x``. If ``True``, a newly allocated
245-
array must always be returned. If ``False`` and the specified dtype
246-
matches the data type of the input array, the input array must be
247-
returned; otherwise, a newly allocated array must be returned.
248-
249-
Default: ``True``.
250-
device : {None, string, SyclDevice, SyclQueue, Device}, optional
251-
An array API concept of device where the output array is created.
252-
`device` can be ``None``, a oneAPI filter selector string, an instance
253-
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
254-
device, an instance of :class:`dpctl.SyclQueue`, or a
255-
:class:`dpctl.tensor.Device` object returned by
256-
:attr:`dpnp.ndarray.device`.
257-
If the value is ``None``, returned array is created on the same device
258-
as `x`.
259-
260-
Default: ``None``.
261-
262-
Returns
263-
-------
264-
out : dpnp.ndarray
265-
An array having the specified data type.
266-
267-
See Also
268-
--------
269-
:obj:`dpnp.ndarray.astype` : Equivalent method.
270-
271-
Examples
272-
--------
273-
>>> import dpnp as np
274-
>>> x = np.array([1, 2, 3]); x
275-
array([1, 2, 3])
276-
>>> np.astype(x, np.float32)
277-
array([1., 2., 3.], dtype=float32)
278-
279-
Non-copy case:
280-
281-
>>> x = np.array([1, 2, 3])
282-
>>> result = np.astype(x, x.dtype, copy=False)
283-
>>> result is x
284-
True
285-
286-
"""
287-
288-
if order is None:
289-
order = "K"
290-
291-
usm_x = dpnp.get_usm_ndarray(x)
292-
usm_res = dpt.astype(
293-
usm_x, dtype, order=order, casting=casting, copy=copy, device=device
294-
)
295-
296-
if usm_res is usm_x and isinstance(x, dpnp_array):
297-
# return x if dpctl returns a zero copy of usm_x
298-
return x
299-
return dpnp_array._create_from_usm_ndarray(usm_res)
300-
301-
302212
def as_usm_ndarray(a, dtype=None, device=None, usm_type=None, sycl_queue=None):
303213
"""
304214
Return :class:`dpctl.tensor.usm_ndarray` from input object `a`.

dpnp/dpnp_iface_arraycreation.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"asarray",
6565
"ascontiguousarray",
6666
"asfortranarray",
67+
"astype",
6768
"copy",
6869
"diag",
6970
"diagflat",
@@ -896,6 +897,95 @@ def asfortranarray(
896897
)
897898

898899

900+
def astype(x, dtype, /, *, order="K", casting="unsafe", copy=True, device=None):
901+
"""
902+
Copy the array with data type casting.
903+
904+
Parameters
905+
----------
906+
x : {dpnp.ndarray, usm_ndarray}
907+
Array data type casting.
908+
dtype : {None, str, dtype object}
909+
Target data type.
910+
order : {None, "C", "F", "A", "K"}, optional
911+
Row-major (C-style) or column-major (Fortran-style) order.
912+
When `order` is ``"A"``, it uses ``"F"`` if `a` is column-major and
913+
uses ``"C"`` otherwise. And when `order` is ``"K"``, it keeps strides
914+
as closely as possible.
915+
916+
Default: ``"K"``.
917+
casting : {"no", "equiv", "safe", "same_kind", "unsafe"}, optional
918+
Controls what kind of data casting may occur. Defaults to ``"unsafe"``
919+
for backwards compatibility.
920+
921+
- "no" means the data types should not be cast at all.
922+
- "equiv" means only byte-order changes are allowed.
923+
- "safe" means only casts which can preserve values are allowed.
924+
- "same_kind" means only safe casts or casts within a kind, like
925+
float64 to float32, are allowed.
926+
- "unsafe" means any data conversions may be done.
927+
928+
Default: ``"unsafe"``.
929+
copy : bool, optional
930+
Specifies whether to copy an array when the specified dtype matches the
931+
data type of the input array ``x``. If ``True``, a newly allocated
932+
array must always be returned. If ``False`` and the specified dtype
933+
matches the data type of the input array, the input array must be
934+
returned; otherwise, a newly allocated array must be returned.
935+
936+
Default: ``True``.
937+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
938+
An array API concept of device where the output array is created.
939+
`device` can be ``None``, a oneAPI filter selector string, an instance
940+
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
941+
device, an instance of :class:`dpctl.SyclQueue`, or a
942+
:class:`dpctl.tensor.Device` object returned by
943+
:attr:`dpnp.ndarray.device`.
944+
If the value is ``None``, returned array is created on the same device
945+
as `x`.
946+
947+
Default: ``None``.
948+
949+
Returns
950+
-------
951+
out : dpnp.ndarray
952+
An array having the specified data type.
953+
954+
See Also
955+
--------
956+
:obj:`dpnp.ndarray.astype` : Equivalent method.
957+
958+
Examples
959+
--------
960+
>>> import dpnp as np
961+
>>> x = np.array([1, 2, 3]); x
962+
array([1, 2, 3])
963+
>>> np.astype(x, np.float32)
964+
array([1., 2., 3.], dtype=float32)
965+
966+
Non-copy case:
967+
968+
>>> x = np.array([1, 2, 3])
969+
>>> result = np.astype(x, x.dtype, copy=False)
970+
>>> result is x
971+
True
972+
973+
"""
974+
975+
if order is None:
976+
order = "K"
977+
978+
usm_x = dpnp.get_usm_ndarray(x)
979+
usm_res = dpt.astype(
980+
usm_x, dtype, order=order, casting=casting, copy=copy, device=device
981+
)
982+
983+
if usm_res is usm_x and isinstance(x, dpnp_array):
984+
# return x if dpctl returns a zero copy of usm_x
985+
return x
986+
return dpnp_array._create_from_usm_ndarray(usm_res)
987+
988+
899989
def copy(
900990
a, order="K", subok=False, device=None, usm_type=None, sycl_queue=None
901991
):

0 commit comments

Comments
 (0)