|
75 | 75 | "fromfunction",
|
76 | 76 | "fromiter",
|
77 | 77 | "fromstring",
|
| 78 | + "from_dlpack", |
78 | 79 | "full",
|
79 | 80 | "full_like",
|
80 | 81 | "geomspace",
|
@@ -1956,6 +1957,93 @@ def fromstring(
|
1956 | 1957 | )
|
1957 | 1958 |
|
1958 | 1959 |
|
| 1960 | +def from_dlpack(x, /, *, device=None, copy=None): |
| 1961 | + """ |
| 1962 | + Constructs :class:`dpnp.ndarray` or :class:`numpy.ndarray` instance from |
| 1963 | + a Python object `x` that implements ``__dlpack__`` protocol. |
| 1964 | +
|
| 1965 | + For full documentation refer to :obj:`numpy.from_dlpack`. |
| 1966 | +
|
| 1967 | + Parameters |
| 1968 | + ---------- |
| 1969 | + x : object |
| 1970 | + A Python object representing an array that implements the ``__dlpack__`` |
| 1971 | + and ``__dlpack_device__`` methods. |
| 1972 | + device : {None, string, tuple, device}, optional |
| 1973 | + Device where the output array is to be placed. `device` keyword values |
| 1974 | + can be: |
| 1975 | +
|
| 1976 | + * ``None`` : The data remains on the same device. |
| 1977 | + * oneAPI filter selector string : SYCL device selected by filter |
| 1978 | + selector string. |
| 1979 | + * :class:`dpctl.SyclDevice` : Explicit SYCL device that must correspond |
| 1980 | + to a non-partitioned SYCL device. |
| 1981 | + * :class:`dpctl.SyclQueue` : Implies SYCL device targeted by the SYCL |
| 1982 | + queue. |
| 1983 | + * :class:`dpctl.tensor.Device` : Implies SYCL device |
| 1984 | + ``device.sycl_queue``. The `device` object is obtained via |
| 1985 | + :attr:`dpctl.tensor.usm_ndarray.device`. |
| 1986 | + * ``(device_type, device_id)`` : 2-tuple matching the format of the |
| 1987 | + output of the ``__dlpack_device__`` method: an integer enumerator |
| 1988 | + representing the device type followed by an integer representing |
| 1989 | + the index of the device. The only supported :class:`dpnp.DLDeviceType` |
| 1990 | + device types are ``"kDLCPU"`` and ``"kDLOneAPI"``. |
| 1991 | +
|
| 1992 | + Default: ``None``. |
| 1993 | + copy : {bool, None}, optional |
| 1994 | + Boolean indicating whether or not to copy the input. |
| 1995 | +
|
| 1996 | + * If `copy` is ``True``, the input will always be copied. |
| 1997 | + * If ``False``, a ``BufferError`` will be raised if a copy is deemed |
| 1998 | + necessary. |
| 1999 | + * If ``None``, a copy will be made only if deemed necessary, otherwise, |
| 2000 | + the existing memory buffer will be reused. |
| 2001 | +
|
| 2002 | + Default: ``None``. |
| 2003 | +
|
| 2004 | + Returns |
| 2005 | + ------- |
| 2006 | + out : {dpnp.ndarray, numpy.ndarray} |
| 2007 | + An array containing the data in `x`. When `copy` is ``None`` or |
| 2008 | + ``False``, this may be a view into the original memory. |
| 2009 | + The type of the returned object depends on where the data backing up |
| 2010 | + input object `x` resides. If it resides in a USM allocation on a SYCL |
| 2011 | + device, the type :class:`dpnp.ndarray` is returned, otherwise if it |
| 2012 | + resides on ``"kDLCPU"`` device the type is :class:`numpy.ndarray`, and |
| 2013 | + otherwise an exception is raised. |
| 2014 | +
|
| 2015 | + Raises |
| 2016 | + ------ |
| 2017 | + TypeError |
| 2018 | + if `obj` does not implement ``__dlpack__`` method |
| 2019 | + ValueError |
| 2020 | + if data of the input object resides on an unsupported device |
| 2021 | +
|
| 2022 | + Notes |
| 2023 | + ----- |
| 2024 | + If the return type is :class:`dpnp.ndarray`, the associated SYCL queue is |
| 2025 | + derived from the `device` keyword. When `device` keyword value has type |
| 2026 | + :class:`dpctl.SyclQueue`, the explicit queue instance is used, when `device` |
| 2027 | + keyword value has type :class:`dpctl.tensor.Device`, the |
| 2028 | + ``device.sycl_queue`` is used. In all other cases, the cached SYCL queue |
| 2029 | + corresponding to the implied SYCL device is used. |
| 2030 | +
|
| 2031 | + Examples |
| 2032 | + -------- |
| 2033 | + >>> import dpnp as np |
| 2034 | + >>> import numpy |
| 2035 | + >>> x = numpy.arange(10) |
| 2036 | + >>> # create a view of the numpy array "x" in dpnp: |
| 2037 | + >>> y = np.from_dlpack(x) |
| 2038 | +
|
| 2039 | + """ |
| 2040 | + |
| 2041 | + result = dpt.from_dlpack(x, device=device, copy=copy) |
| 2042 | + if isinstance(result, dpt.usm_ndarray): |
| 2043 | + return dpnp_array._create_from_usm_ndarray(result) |
| 2044 | + return result |
| 2045 | + |
| 2046 | + |
1959 | 2047 | def full(
|
1960 | 2048 | shape,
|
1961 | 2049 | fill_value,
|
|
0 commit comments