|
75 | 75 | "fromfunction",
|
76 | 76 | "fromiter",
|
77 | 77 | "fromstring",
|
| 78 | + "from_dlpack", |
78 | 79 | "full",
|
79 | 80 | "full_like",
|
80 | 81 | "geomspace",
|
@@ -2047,6 +2048,94 @@ def fromstring(
|
2047 | 2048 | )
|
2048 | 2049 |
|
2049 | 2050 |
|
| 2051 | +def from_dlpack(x, /, *, device=None, copy=None): |
| 2052 | + """ |
| 2053 | + Constructs :class:`dpnp.ndarray` or :class:`numpy.ndarray` instance from |
| 2054 | + a Python object `x` that implements ``__dlpack__`` 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, string, tuple, device}, optional |
| 2064 | + Device where the output array is to be placed. `device` keyword values |
| 2065 | + can be: |
| 2066 | +
|
| 2067 | + * ``None`` : The data remains on the same device. |
| 2068 | + * oneAPI filter selector string : SYCL device selected by filter |
| 2069 | + selector string. |
| 2070 | + * :class:`dpctl.SyclDevice` : Explicit SYCL device that must correspond |
| 2071 | + to a non-partitioned SYCL device. |
| 2072 | + * :class:`dpctl.SyclQueue` : Implies SYCL device targeted by the SYCL |
| 2073 | + queue. |
| 2074 | + * :class:`dpctl.tensor.Device` : Implies SYCL device |
| 2075 | + ``device.sycl_queue``. The `device` object is obtained via |
| 2076 | + :attr:`dpctl.tensor.usm_ndarray.device`. |
| 2077 | + * ``(device_type, device_id)`` : 2-tuple matching the format of the |
| 2078 | + output of the ``__dlpack_device__`` method: an integer enumerator |
| 2079 | + representing the device type followed by an integer representing |
| 2080 | + the index of the device. The only supported |
| 2081 | + :class:`dpctl.tensor.DLDeviceType` device types are ``"kDLCPU"`` |
| 2082 | + and ``"kDLOneAPI"``. |
| 2083 | +
|
| 2084 | + Default: ``None``. |
| 2085 | + copy : {bool, None}, optional |
| 2086 | + Boolean indicating whether or not to copy the input. |
| 2087 | +
|
| 2088 | + * If `copy``is ``True``, the input will always be copied. |
| 2089 | + * If ``False``, a ``BufferError`` will be raised if a copy is deemed |
| 2090 | + necessary. |
| 2091 | + * If ``None``, a copy will be made only if deemed necessary, otherwise, |
| 2092 | + the existing memory buffer will be reused. |
| 2093 | +
|
| 2094 | + Default: ``None``. |
| 2095 | +
|
| 2096 | + Returns |
| 2097 | + ------- |
| 2098 | + out : {dpnp.ndarray, numpy.ndarray} |
| 2099 | + An array containing the data in `x`. When `copy` is ``None`` or |
| 2100 | + ``False``, this may be a view into the original memory. |
| 2101 | + The type of the returned object depends on where the data backing up |
| 2102 | + input object `x` resides. If it resides in a USM allocation on a SYCL |
| 2103 | + device, the type :class:`dpnp.ndarray` is returned, otherwise if it |
| 2104 | + resides on ``"kDLCPU"`` device the type is :class:`numpy.ndarray`, and |
| 2105 | + otherwise an exception is raised. |
| 2106 | +
|
| 2107 | + Raises |
| 2108 | + ------ |
| 2109 | + TypeError |
| 2110 | + if `obj` does not implement ``__dlpack__`` method |
| 2111 | + ValueError |
| 2112 | + if data of the input object resides on an unsupported device |
| 2113 | +
|
| 2114 | + Notes |
| 2115 | + ----- |
| 2116 | + If the return type is :class:`dpnp.ndarray`, the associated SYCL queue is |
| 2117 | + derived from the `device` keyword. When `device` keyword value has type |
| 2118 | + :class:`dpctl.SyclQueue`, the explicit queue instance is used, when `device` |
| 2119 | + keyword value has type :class:`dpctl.tensor.Device`, the |
| 2120 | + ``device.sycl_queue`` is used. In all other cases, the cached SYCL queue |
| 2121 | + corresponding to the implied SYCL device is used. |
| 2122 | +
|
| 2123 | + Examples |
| 2124 | + -------- |
| 2125 | + >>> import dpnp as np |
| 2126 | + >>> import numpy |
| 2127 | + >>> x = numpy.arange(10) |
| 2128 | + >>> # create a view of the numpy array "x" in dpnp: |
| 2129 | + >>> y = np.from_dlpack(x) |
| 2130 | +
|
| 2131 | + """ |
| 2132 | + |
| 2133 | + result = dpt.from_dlpack(x, device=device, copy=copy) |
| 2134 | + if isinstance(result, dpt.usm_ndarray): |
| 2135 | + return dpnp_array._create_from_usm_ndarray(result) |
| 2136 | + return result |
| 2137 | + |
| 2138 | + |
2050 | 2139 | def full(
|
2051 | 2140 | shape,
|
2052 | 2141 | fill_value,
|
|
0 commit comments