Skip to content

Commit 4513339

Browse files
committed
update to reflect the discussions
1 parent c102bcb commit 4513339

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/array_api_stubs/_draft/array_object.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ def __dlpack__(
326326
- ``> 2``: stream number represented as a Python integer.
327327
- Using ``1`` and ``2`` is not supported.
328328
329+
.. note::
330+
When ``dl_device`` is provided explicitly, ``stream`` must be a valid
331+
construct for the specified device type. In particular, when ``kDLCPU``
332+
is in use, ``stream`` must be ``None`` and a synchronization must be
333+
performed to ensure data safety.
334+
329335
.. admonition:: Tip
330336
:class: important
331337
@@ -344,16 +350,22 @@ def __dlpack__(
344350
dl_device: Optional[Tuple[Enum, int]]
345351
the DLPack device type. Default is ``None``, meaning the exported capsule
346352
should be on the same device as ``self`` is. When specified, the format
347-
must follow that of the return value of :meth:`array.__dlpack_device__`.
353+
must be a 2-tuple, following that of the return value of :meth:`array.__dlpack_device__`.
348354
If the device type cannot be handled by the producer, this function must
349-
raise `BufferError`.
355+
raise ``BufferError``.
350356
copy: Optional[bool]
351357
boolean indicating whether or not to copy the input. If ``True``, the
352358
function must always copy (paerformed by the producer), potentially allowing
353359
data movement across the library (and/or device) boundary. If ``False``,
354360
the function must never copy. If ``None``, the function must reuse existing
355361
memory buffer if possible and copy otherwise. Default: ``None``.
356362
363+
When a copy happens, the ``DLPACK_FLAG_BITMASK_IS_COPIED`` flag must be set.
364+
365+
.. note::
366+
If a copy happens, and if the consumer-provided ``stream`` and ``dl_device``
367+
can be understood by the producer, the copy must be performed over ``stream``.
368+
357369
Returns
358370
-------
359371
capsule: PyCapsule
@@ -413,11 +425,14 @@ def __dlpack__(
413425
.. code:: python
414426
415427
try:
416-
x.__dlpack__(max_version=(1, 0))
428+
x.__dlpack__(max_version=(1, 0), ...)
417429
# if it succeeds, store info from the capsule named "dltensor_versioned",
418430
# and need to set the name to "used_dltensor_versioned" when we're done
419431
except TypeError:
420-
x.__dlpack__()
432+
x.__dlpack__(...)
433+
434+
This logic is also applicable to handling of the new ``dl_device`` and ``copy``
435+
keywords.
421436
422437
.. versionchanged:: 2022.12
423438
Added BufferError.

src/array_api_stubs/_draft/creation_functions.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,17 @@ def from_dlpack(
228228
x: object
229229
input (array) object.
230230
device: Optional[device]
231-
device on which to place the created array. If ``device`` is ``None`` and ``x`` supports DLPack, the output array device must be inferred from ``x``. Default: ``None``.
231+
device on which to place the created array. If ``device`` is ``None`` and ``x`` supports DLPack, the output array device must be inferred from ``x.device``. Default: ``None``.
232232
233233
The v2023.12 standard only mandates that a compliant library must offer a way for ``from_dlpack`` to create an array on CPU (using
234-
the library-chosen way to represent the CPU device - ``kDLCPU`` in DLPack - e.g. a ``"CPU"`` string or a ``Device("CPU")`` object).
235-
If the compliant library does not support the CPU device and needs to outsource to another (compliant) array library, it may do so
234+
a library-specifc way to represent the CPU device (``kDLCPU`` in DLPack) e.g. a ``"CPU"`` string or a ``Device("CPU")`` object).
235+
If the array library does not support the CPU device and needs to outsource to another (compliant) array library, it may do so
236236
with a clear user documentation and/or run-time warning. If a copy must be made to enable this, and ``copy`` is set to ``False``,
237237
the function must raise ``ValueError``.
238238
239-
Other kinds of devices will be considered for standardization in a future version.
239+
Other kinds of devices will be considered for standardization in a future version of this API standard.
240240
copy: Optional[bool]
241-
boolean indicating whether or not to copy the input. If ``True``, the function must always copy. If ``False``, the function must never copy and must raise a ``BufferError`` in case a copy would be necessary (e.g. the producer disallows views). If ``None``, the function must reuse existing memory buffer if possible and copy otherwise. Default: ``None``.
242-
243-
If a copy is needed, the stream over which the copy is performed must be taken from the consumer, following the DLPack protocol (see :meth:`array.__dlpack__`).
241+
boolean indicating whether or not to copy the input. If ``True``, the function must always copy. If ``False``, the function must never copy and must raise a ``BufferError`` in case a copy is deemed necessary (e.g. the producer disallows views). If ``None``, the function must reuse the existing memory buffer if possible and copy otherwise. Default: ``None``.
244242
245243
Returns
246244
-------

0 commit comments

Comments
 (0)