Skip to content

Commit af0fc93

Browse files
authored
Align signature of dpnp.astype with Python array API (#2318)
The PR is aimed to align signature of `dpnp.astype` function with Python array API where only `x` and `dtype` arguments are positional-only and other arguments are keyword-only. Also docstrings of `dpnp.astype` function and `dpnp.ndarray,astype` method are aligned and properly updated. And `dpnp.astype` function is moved to array creation namespace like NumPy has it. Additionally, docstring of `device` keyword is updated and aligned across all the functions description to have it described in a common way.
1 parent f56a43f commit af0fc93

16 files changed

+679
-485
lines changed

dpnp/dpnp_array.py

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -789,47 +789,51 @@ def astype(
789789
790790
Parameters
791791
----------
792-
x1 : {dpnp.ndarray, usm_ndarray}
793-
Array data type casting.
794792
dtype : {None, str, dtype object}
795793
Target data type.
796-
order : {"C", "F", "A", "K"}, optional
794+
order : {None, "C", "F", "A", "K"}, optional
797795
Row-major (C-style) or column-major (Fortran-style) order.
798-
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
799-
And when ``order`` is 'K', it keeps strides as closely as possible.
800-
copy : bool
801-
If it is False and no cast happens, then this method returns the array itself.
802-
Otherwise, a copy is returned.
803-
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
804-
Controls what kind of data casting may occur.
805-
Defaults to ``'unsafe'`` for backwards compatibility.
806-
807-
- 'no' means the data types should not be cast at all.
808-
- 'equiv' means only byte-order changes are allowed.
809-
- 'safe' means only casts which can preserve values are allowed.
810-
- 'same_kind' means only safe casts or casts within a kind, like
811-
float64 to float32, are allowed.
812-
- 'unsafe' means any data conversions may be done.
813-
814-
copy : {bool}, optional
815-
By default, ``astype`` always returns a newly allocated array. If
816-
this is set to ``False``, and the `dtype`, `order`, and `subok`
817-
requirements are satisfied, the input array is returned instead of
818-
a copy.
819-
device : {None, string, SyclDevice, SyclQueue}, optional
796+
When `order` is ``"A"``, it uses ``"F"`` if `a` is column-major and
797+
uses ``"C"`` otherwise. And when `order` is ``"K"``, it keeps
798+
strides as closely as possible.
799+
800+
Default: ``"K"``.
801+
casting : {"no", "equiv", "safe", "same_kind", "unsafe"}, optional
802+
Controls what kind of data casting may occur. Defaults to
803+
``"unsafe"`` for backwards compatibility.
804+
805+
- "no" means the data types should not be cast at all.
806+
- "equiv" means only byte-order changes are allowed.
807+
- "safe" means only casts which can preserve values are allowed.
808+
- "same_kind" means only safe casts or casts within a kind,
809+
like float64 to float32, are allowed.
810+
- "unsafe" means any data conversions may be done.
811+
812+
Default: ``"unsafe"``.
813+
copy : bool, optional
814+
Specifies whether to copy an array when the specified dtype matches
815+
the data type of that array. If ``True``, a newly allocated array
816+
must always be returned. If ``False`` and the specified dtype
817+
matches the data type of that array, the self array must be returned;
818+
otherwise, a newly allocated array must be returned.
819+
820+
Default: ``True``.
821+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
820822
An array API concept of device where the output array is created.
821-
The `device` can be ``None`` (the default), an OneAPI filter selector
822-
string, an instance of :class:`dpctl.SyclDevice` corresponding to
823-
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
824-
or a `Device` object returned by
825-
:obj:`dpnp.dpnp_array.dpnp_array.device` property. Default: ``None``.
823+
`device` can be ``None``, a oneAPI filter selector string,
824+
an instance of :class:`dpctl.SyclDevice` corresponding to
825+
a non-partitioned SYCL device, an instance of
826+
:class:`dpctl.SyclQueue`, or a :class:`dpctl.tensor.Device` object
827+
returned by :attr:`dpnp.ndarray.device`.
828+
If the value is ``None``, returned array is created on the same
829+
device as that array.
830+
831+
Default: ``None``.
826832
827833
Returns
828834
-------
829-
arr_t : dpnp.ndarray
830-
Unless `copy` is ``False`` and the other conditions for returning the input array
831-
are satisfied, `arr_t` is a new array of the same shape as the input array,
832-
with dtype, order given by dtype, order.
835+
out : dpnp.ndarray
836+
An array having the specified data type.
833837
834838
Limitations
835839
-----------
@@ -839,9 +843,9 @@ def astype(
839843
Examples
840844
--------
841845
>>> import dpnp as np
842-
>>> x = np.array([1, 2, 2.5])
843-
>>> x
846+
>>> x = np.array([1, 2, 2.5]); x
844847
array([1. , 2. , 2.5])
848+
845849
>>> x.astype(int)
846850
array([1, 2, 2])
847851
@@ -925,13 +929,14 @@ def copy(self, order="C", device=None, usm_type=None, sycl_queue=None):
925929
order : {"C", "F", "A", "K"}, optional
926930
Memory layout of the newly output array.
927931
Default: ``"C"``.
928-
device : {None, string, SyclDevice, SyclQueue}, optional
932+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
929933
An array API concept of device where the output array is created.
930-
The `device` can be ``None`` (the default), an OneAPI filter
931-
selector string, an instance of :class:`dpctl.SyclDevice`
932-
corresponding to a non-partitioned SYCL device, an instance of
933-
:class:`dpctl.SyclQueue`, or a `Device` object returned by
934-
:obj:`dpnp.dpnp_array.dpnp_array.device` property.
934+
`device` can be ``None``, a oneAPI filter selector string,
935+
an instance of :class:`dpctl.SyclDevice` corresponding to
936+
a non-partitioned SYCL device, an instance of
937+
:class:`dpctl.SyclQueue`, or a :class:`dpctl.tensor.Device` object
938+
returned by :attr:`dpnp.ndarray.device`.
939+
935940
Default: ``None``.
936941
usm_type : {None, "device", "shared", "host"}, optional
937942
The type of SYCL USM allocation for the output array.
@@ -1821,12 +1826,13 @@ def to_device(self, device, /, *, stream=None):
18211826
18221827
Parameters
18231828
----------
1824-
device : {string, SyclDevice, SyclQueue}
1825-
Array API concept of target device. It can be an OneAPI filter
1826-
selector string, an instance of :class:`dpctl.SyclDevice`
1827-
corresponding to a non-partitioned SYCL device, an instance of
1829+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
1830+
An array API concept of device where the output array is created.
1831+
`device` can be ``None``, a oneAPI filter selector string,
1832+
an instance of :class:`dpctl.SyclDevice` corresponding to
1833+
a non-partitioned SYCL device, an instance of
18281834
:class:`dpctl.SyclQueue`, or a :class:`dpctl.tensor.Device` object
1829-
returned by :obj:`dpnp.dpnp_array.dpnp_array.device` property.
1835+
returned by :attr:`dpnp.ndarray.device`.
18301836
stream : {SyclQueue, None}, optional
18311837
Execution queue to synchronize with. If ``None``, synchronization
18321838
is not performed.

dpnp/dpnp_iface.py

Lines changed: 56 additions & 94 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",
@@ -74,7 +74,6 @@
7474
"synchronize_array_data",
7575
]
7676

77-
from dpnp import float64
7877
from dpnp.dpnp_iface_arraycreation import *
7978
from dpnp.dpnp_iface_arraycreation import __all__ as __all__arraycreation
8079
from dpnp.dpnp_iface_bitwise import *
@@ -182,11 +181,13 @@ def asnumpy(a, order="C"):
182181
----------
183182
a : {array_like}
184183
Arbitrary object that can be converted to :obj:`numpy.ndarray`.
185-
order : {'C', 'F', 'A', 'K'}
184+
order : {None, 'C', 'F', 'A', 'K'}, optional
186185
The desired memory layout of the converted array.
187-
When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
188-
``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
189-
as possible.
186+
When `order` is ``'A'``, it uses ``'F'`` if `a` is column-major and
187+
uses ``'C'`` otherwise. And when `order` is ``'K'``, it keeps strides
188+
as closely as possible.
189+
190+
Default: ``'C'``.
190191
191192
Returns
192193
-------
@@ -208,71 +209,6 @@ def asnumpy(a, order="C"):
208209
return numpy.asarray(a, order=order)
209210

210211

211-
# pylint: disable=redefined-outer-name
212-
def astype(x1, dtype, order="K", casting="unsafe", copy=True, device=None):
213-
"""
214-
Copy the array with data type casting.
215-
216-
Parameters
217-
----------
218-
x1 : {dpnp.ndarray, usm_ndarray}
219-
Array data type casting.
220-
dtype : {None, str, dtype object}
221-
Target data type.
222-
order : {'C', 'F', 'A', 'K'}
223-
Row-major (C-style) or column-major (Fortran-style) order.
224-
When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
225-
``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
226-
as possible.
227-
copy : bool
228-
If it is ``False`` and no cast happens, then this method returns
229-
the array itself. Otherwise, a copy is returned.
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-
copy : {bool}, optional
242-
By default, ``astype`` always returns a newly allocated array. If this
243-
is set to ``False``, and the `dtype`, `order`, and `subok` requirements
244-
are satisfied, the input array is returned instead of a copy.
245-
device : {None, string, SyclDevice, SyclQueue}, optional
246-
An array API concept of device where the output array is created.
247-
The `device` can be ``None`` (the default), an OneAPI filter selector
248-
string, an instance of :class:`dpctl.SyclDevice` corresponding to
249-
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
250-
or a `Device` object returned by
251-
:obj:`dpnp.dpnp_array.dpnp_array.device` property. Default: ``None``.
252-
253-
Returns
254-
-------
255-
arr_t : dpnp.ndarray
256-
Unless `copy` is ``False`` and the other conditions for returning
257-
the input array are satisfied, `arr_t` is a new array of the same shape
258-
as the input array, with dtype, order given by dtype, order.
259-
260-
"""
261-
262-
if order is None:
263-
order = "K"
264-
265-
x1_obj = dpnp.get_usm_ndarray(x1)
266-
array_obj = dpt.astype(
267-
x1_obj, dtype, order=order, casting=casting, copy=copy, device=device
268-
)
269-
270-
if array_obj is x1_obj and isinstance(x1, dpnp_array):
271-
# return x1 if dpctl returns a zero copy of x1_obj
272-
return x1
273-
return dpnp_array._create_from_usm_ndarray(array_obj)
274-
275-
276212
def as_usm_ndarray(a, dtype=None, device=None, usm_type=None, sycl_queue=None):
277213
"""
278214
Return :class:`dpctl.tensor.usm_ndarray` from input object `a`.
@@ -285,22 +221,27 @@ def as_usm_ndarray(a, dtype=None, device=None, usm_type=None, sycl_queue=None):
285221
The desired dtype for the result array if new array is creating. If not
286222
given, a default dtype will be used that can represent the values (by
287223
considering Promotion Type Rule and device capabilities when necessary).
224+
288225
Default: ``None``.
289-
device : {None, string, SyclDevice, SyclQueue}, optional
290-
An array API concept of device where the result array is created if
291-
required.
292-
The `device` can be ``None`` (the default), an OneAPI filter selector
293-
string, an instance of :class:`dpctl.SyclDevice` corresponding to
294-
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
295-
or a `Device` object returned by
296-
:obj:`dpnp.dpnp_array.dpnp_array.device` property.
226+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
227+
An array API concept of device where the output array is created.
228+
`device` can be ``None``, a oneAPI filter selector string, an instance
229+
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
230+
device, an instance of :class:`dpctl.SyclQueue`, or a
231+
:class:`dpctl.tensor.Device` object returned by
232+
:attr:`dpnp.ndarray.device`.
233+
If the value is ``None``, returned array is created on the same device
234+
as `a`.
235+
297236
Default: ``None``.
298237
usm_type : {None, "device", "shared", "host"}, optional
299238
The type of SYCL USM allocation for the result array if new array
300239
is created.
240+
301241
Default: ``None``.
302242
sycl_queue : {None, SyclQueue}, optional
303243
A SYCL queue to use for result array allocation if required.
244+
304245
Default: ``None``.
305246
306247
Returns
@@ -391,11 +332,15 @@ def check_supported_arrays_type(*arrays, scalar_type=False, all_scalars=False):
391332
----------
392333
arrays : {dpnp.ndarray, usm_ndarray}
393334
Input arrays to check for supported types.
394-
scalar_type : {bool}, optional
335+
scalar_type : bool, optional
395336
A scalar type is also considered as supported if flag is ``True``.
396-
all_scalars : {bool}, optional
337+
338+
Default: ``False``.
339+
all_scalars : bool, optional
397340
All the input arrays can be scalar if flag is ``True``.
398341
342+
Default: ``False``.
343+
399344
Returns
400345
-------
401346
out : bool
@@ -437,20 +382,24 @@ def default_float_type(device=None, sycl_queue=None):
437382
438383
Parameters
439384
----------
440-
device : {None, string, SyclDevice, SyclQueue}, optional
441-
An array API concept of device where an array of default floating type
442-
might be created. The `device` can be ``None`` (the default), an OneAPI
443-
filter selector string, an instance of :class:`dpctl.SyclDevice`
444-
corresponding to a non-partitioned SYCL device, an instance of
445-
:class:`dpctl.SyclQueue`, or a `Device` object returned by
446-
:obj:`dpnp.dpnp_array.dpnp_array.device` property.
385+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
386+
An array API concept of device where the output array is created.
387+
`device` can be ``None``, a oneAPI filter selector string, an instance
388+
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
389+
device, an instance of :class:`dpctl.SyclQueue`, or a
390+
:class:`dpctl.tensor.Device` object returned by
391+
:attr:`dpnp.ndarray.device`.
447392
The value ``None`` is interpreted as to use a default device.
393+
394+
Default: ``None``.
448395
sycl_queue : {None, SyclQueue}, optional
449396
A SYCL queue which might be used to create an array of default floating
450397
type. The `sycl_queue` can be ``None`` (the default), which is
451398
interpreted as to get the SYCL queue from `device` keyword if present
452399
or to use a default queue.
453400
401+
Default: ``None``.
402+
454403
Returns
455404
-------
456405
dt : dtype
@@ -461,7 +410,7 @@ def default_float_type(device=None, sycl_queue=None):
461410
_sycl_queue = get_normalized_queue_device(
462411
device=device, sycl_queue=sycl_queue
463412
)
464-
return map_dtype_to_device(float64, _sycl_queue.sycl_device)
413+
return map_dtype_to_device(dpnp.float64, _sycl_queue.sycl_device)
465414

466415

467416
def get_dpnp_descriptor(
@@ -569,16 +518,24 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
569518
and implementing `__sycl_usm_array_interface__` protocol, an instance
570519
of `numpy.ndarray`, an object supporting Python buffer protocol,
571520
a Python scalar, or a (possibly nested) sequence of Python scalars.
572-
sycl_queue : class:`dpctl.SyclQueue`, optional
521+
sycl_queue : {None, class:`dpctl.SyclQueue`}, optional
573522
A queue which explicitly indicates where USM allocation is done
574523
and the population code (if any) is executed.
575524
Value ``None`` is interpreted as to get the SYCL queue from either
576525
`obj` parameter if not ``None`` or from `device` keyword,
577526
or to use default queue.
578-
device : {string, :class:`dpctl.SyclDevice`, :class:`dpctl.SyclQueue,
579-
:class:`dpctl.tensor.Device`}, optional
580-
An array-API keyword indicating non-partitioned SYCL device
581-
where array is allocated.
527+
528+
Default: ``None``.
529+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
530+
An array API concept of device where the output array is created.
531+
`device` can be ``None``, a oneAPI filter selector string, an instance
532+
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
533+
device, an instance of :class:`dpctl.SyclQueue`, or a
534+
:class:`dpctl.tensor.Device` object returned by
535+
:attr:`dpnp.ndarray.device`.
536+
The value ``None`` is interpreted as to use the same device as `obj`.
537+
538+
Default: ``None``.
582539
583540
Returns
584541
-------
@@ -616,9 +573,13 @@ def get_result_array(a, out=None, casting="safe"):
616573
If provided, value of `a` array will be copied into it
617574
according to ``safe`` casting rule.
618575
It should be of the appropriate shape.
576+
577+
Default: ``None``.
619578
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
620579
Controls what kind of data casting may occur.
621580
581+
Default: ``'safe'``.
582+
622583
Returns
623584
-------
624585
out : {dpnp_array}
@@ -714,6 +675,7 @@ def is_cuda_backend(obj=None):
714675
An input object with sycl_device property to check device backend.
715676
If `obj` is ``None``, device backend will be checked for the default
716677
queue.
678+
717679
Default: ``None``.
718680
719681
Returns

0 commit comments

Comments
 (0)