Skip to content

Commit 006cc54

Browse files
committed
Move dpnp.from_dlpack to array creation routine
1 parent 0bac735 commit 006cc54

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed

dpnp/dpnp_iface.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"check_limitations",
6363
"check_supported_arrays_type",
6464
"default_float_type",
65-
"from_dlpack",
6665
"get_dpnp_descriptor",
6766
"get_include",
6867
"get_normalized_queue_device",
@@ -443,60 +442,6 @@ def default_float_type(device=None, sycl_queue=None):
443442
return map_dtype_to_device(float64, _sycl_queue.sycl_device)
444443

445444

446-
def from_dlpack(obj, /, *, device=None, copy=None):
447-
"""
448-
Create a dpnp array from a Python object implementing the ``__dlpack__``
449-
protocol.
450-
451-
See https://dmlc.github.io/dlpack/latest/ for more details.
452-
453-
Parameters
454-
----------
455-
obj : object
456-
A Python object representing an array that implements the ``__dlpack__``
457-
and ``__dlpack_device__`` methods.
458-
device : {:class:`dpctl.SyclDevice`, :class:`dpctl.SyclQueue`,
459-
:class:`dpctl.tensor.Device`, tuple, None}, optional
460-
Array API concept of a device where the output array is to be placed.
461-
``device`` can be ``None``, an oneAPI filter selector string,
462-
an instance of :class:`dpctl.SyclDevice` corresponding to
463-
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
464-
a :class:`dpctl.tensor.Device` object returned by
465-
:attr:`dpctl.tensor.usm_ndarray.device`, or a 2-tuple matching
466-
the format of the output of the ``__dlpack_device__`` method,
467-
an integer enumerator representing the device type followed by
468-
an integer representing the index of the device.
469-
Default: ``None``.
470-
copy {bool, None}, optional
471-
Boolean indicating whether or not to copy the input.
472-
473-
* If `copy``is ``True``, the input will always be copied.
474-
* If ``False``, a ``BufferError`` will be raised if a copy is deemed
475-
necessary.
476-
* If ``None``, a copy will be made only if deemed necessary, otherwise,
477-
the existing memory buffer will be reused.
478-
479-
Default: ``None``.
480-
481-
Returns
482-
-------
483-
out : dpnp_array
484-
Returns a new dpnp array containing the data from another array
485-
(obj) with the ``__dlpack__`` method on the same device as object.
486-
487-
Raises
488-
------
489-
TypeError:
490-
if `obj` does not implement ``__dlpack__`` method
491-
ValueError:
492-
if the input array resides on an unsupported device
493-
494-
"""
495-
496-
usm_res = dpt.from_dlpack(obj, device=device, copy=copy)
497-
return dpnp_array._create_from_usm_ndarray(usm_res)
498-
499-
500445
def get_dpnp_descriptor(
501446
ext_obj,
502447
copy_when_strides=True,

dpnp/dpnp_iface_arraycreation.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"fromfunction",
7676
"fromiter",
7777
"fromstring",
78+
"from_dlpack",
7879
"full",
7980
"full_like",
8081
"geomspace",
@@ -2047,6 +2048,67 @@ def fromstring(
20472048
)
20482049

20492050

2051+
def from_dlpack(x, /, *, device=None, copy=None):
2052+
"""
2053+
Create a dpnp array from a Python object implementing the ``__dlpack__``
2054+
protocol.
2055+
2056+
For full documentation refer to :obj:`numpy.from_dlpack`.
2057+
2058+
Parameters
2059+
----------
2060+
x : object
2061+
A Python object representing an array that implements the ``__dlpack__``
2062+
and ``__dlpack_device__`` methods.
2063+
device : {None, tuple, SyclDevice, SyclQueue, Device}, optional
2064+
Array API concept of a device where the output array is to be placed.
2065+
``device`` can be ``None``, an oneAPI filter selector string,
2066+
an instance of :class:`dpctl.SyclDevice` corresponding to
2067+
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
2068+
a :class:`dpctl.tensor.Device` object returned by
2069+
:attr:`dpctl.tensor.usm_ndarray.device`, or a 2-tuple matching
2070+
the format of the output of the ``__dlpack_device__`` method,
2071+
an integer enumerator representing the device type followed by
2072+
an integer representing the index of the device.
2073+
Default: ``None``.
2074+
copy : {bool, None}, optional
2075+
Boolean indicating whether or not to copy the input.
2076+
2077+
* If `copy``is ``True``, the input will always be copied.
2078+
* If ``False``, a ``BufferError`` will be raised if a copy is deemed
2079+
necessary.
2080+
* If ``None``, a copy will be made only if deemed necessary, otherwise,
2081+
the existing memory buffer will be reused.
2082+
2083+
Default: ``None``.
2084+
2085+
Returns
2086+
-------
2087+
out : dpnp.ndarray
2088+
Returns a new dpnp array containing the data from another array `obj`
2089+
with the ``__dlpack__`` method on the same device as object.
2090+
2091+
Raises
2092+
------
2093+
TypeError:
2094+
if `obj` does not implement ``__dlpack__`` method
2095+
ValueError:
2096+
if the input array resides on an unsupported device
2097+
2098+
Examples
2099+
--------
2100+
>>> import dpnp as np
2101+
>>> import numpy
2102+
>>> x = numpy.arange(10)
2103+
>>> # create a view of the numpy array "x" in dpnp:
2104+
>>> y = np.from_dlpack(x)
2105+
2106+
"""
2107+
2108+
usm_res = dpt.from_dlpack(x, device=device, copy=copy)
2109+
return dpnp_array._create_from_usm_ndarray(usm_res)
2110+
2111+
20502112
def full(
20512113
shape,
20522114
fill_value,

0 commit comments

Comments
 (0)