@@ -13,11 +13,11 @@ Syntax for data interchange with DLPack
13
13
14
14
The array API will offer the following syntax for data interchange:
15
15
16
- 1. A `` from_dlpack(x, ...) ` ` function, which accepts any (array) object with
16
+ 1. A :func: ` ~array_api.from_dlpack ` function, which accepts any (array) object with
17
17
the two DLPack methods implemented (see below) and uses them to construct
18
- a new array containing the data from `` x `` .
19
- 2. `` __dlpack__ `` and `` __dlpack_device__ ` ` methods on the
20
- array object, which will be called from within `` from_dlpack ` `, to query
18
+ a new array containing the data from the input array .
19
+ 2. :meth: ` ~array_api.array. __dlpack__ ` and :meth: ` ~array_api.array. __dlpack_device__ ` methods on the
20
+ array object, which will be called from within :func: ` ~array_api. from_dlpack `, to query
21
21
what device the array is on (may be needed to pass in the correct
22
22
stream, e.g. in the case of multiple GPUs) and to access the data.
23
23
@@ -39,7 +39,7 @@ may flag this and make a copy of the data. In both cases:
39
39
40
40
If an array that is accessed via the interchange protocol lives on a device that
41
41
the requesting (consumer) library does not support, it is recommended to raise a
42
- `` BufferError ` `, unless an explicit copy is requested (see below) and the producer
42
+ :obj: ` BufferError `, unless an explicit copy is requested (see below) and the producer
43
43
can support the request.
44
44
45
45
Stream handling through the ``stream `` keyword applies to CUDA and ROCm (perhaps
@@ -51,9 +51,9 @@ unnecessary so asynchronous execution is enabled.
51
51
52
52
Starting Python array API standard v2023, a copy can be explicitly requested (or
53
53
disabled) through the new ``copy `` argument of ``from_dlpack() ``. When a copy is
54
- made, the producer must set the `` DLPACK_FLAG_BITMASK_IS_COPIED ` ` bit flag.
54
+ made, the producer must set the :c:macro: ` DLPACK_FLAG_BITMASK_IS_COPIED ` bit flag.
55
55
It is also possible to request cross-device copies through the new ``device ``
56
- argument, though the v2023 standard only mandates the support of `` kDLCPU ` `.
56
+ argument, though the v2023 standard only mandates the support of :c:enumerator: ` kDLCPU `.
57
57
58
58
Implementation
59
59
~~~~~~~~~~~~~~
@@ -72,24 +72,25 @@ types.*
72
72
73
73
Starting Python array API standard v2023, a new ``max_version `` argument
74
74
is added to ``__dlpack__ `` for the consumer to signal the producer the
75
- maximal supported DLPack version. Starting DLPack 1.0, the `` DLManagedTensorVersioned ` `
76
- struct should be used and the existing `` DLManagedTensor ` ` struct is considered
75
+ maximal supported DLPack version. Starting DLPack 1.0, the :c:struct: ` DLManagedTensorVersioned `
76
+ struct should be used and the existing :c:struct: ` DLManagedTensor ` struct is considered
77
77
deprecated, though a library should try to support both during the transition
78
78
period if possible.
79
79
80
- In the rest of this document, ``DLManagedTensorVersioned `` and ``DLManagedTensor ``
81
- are treated as synonyms, assuming a proper handling of ``max_version `` has been
82
- done to choose the right struct. As far as the capsule name is concerned,
83
- when ``DLManagedTensorVersioned `` is in use the capsule names ``dltensor ``
84
- and ``used_dltensor `` will need a ``_versioned `` suffix.
80
+ .. note ::
81
+ In the rest of this document, ``DLManagedTensorVersioned `` and ``DLManagedTensor ``
82
+ are treated as synonyms, assuming a proper handling of ``max_version `` has been
83
+ done to choose the right struct. As far as the capsule name is concerned,
84
+ when ``DLManagedTensorVersioned `` is in use the capsule names ``dltensor ``
85
+ and ``used_dltensor `` will need a ``_versioned `` suffix.
85
86
86
- The ``__dlpack__ `` method will produce a `` PyCapsule ` ` containing a
87
+ The ``__dlpack__ `` method will produce a :c:type: ` PyCapsule ` containing a
87
88
``DLManagedTensor ``, which will be consumed immediately within
88
89
``from_dlpack `` - therefore it is consumed exactly once, and it will not be
89
90
visible to users of the Python API.
90
91
91
92
The producer must set the ``PyCapsule `` name to ``"dltensor" `` so that
92
- it can be inspected by name, and set `` PyCapsule_Destructor ` ` that calls
93
+ it can be inspected by name, and set :c:type: ` PyCapsule_Destructor ` that calls
93
94
the ``deleter `` of the ``DLManagedTensor `` when the ``"dltensor" ``-named
94
95
capsule is no longer needed.
95
96
@@ -128,7 +129,7 @@ statically allocated.
128
129
The ``DLManagedTensor `` deleter must ensure that sharing beyond Python
129
130
boundaries is possible, this means that the GIL must be acquired explicitly
130
131
if it uses Python objects or API.
131
- In Python, the deleter usually needs to `` Py_DECREF() ` ` the original owner
132
+ In Python, the deleter usually needs to :c:func: ` Py_DECREF ` the original owner
132
133
and free the ``DLManagedTensor `` allocation.
133
134
For example, NumPy uses the following code to ensure sharing with arbitrary
134
135
non-Python code is safe:
@@ -157,15 +158,15 @@ non-Python code is safe:
157
158
PyGILState_Release(state);
158
159
}
159
160
160
- When the `` strides `` field in the `` DLTensor ` ` struct is ``NULL ``, it indicates a
161
+ When the :c:member: ` ~DLTensor. strides ` field in the :c:struct: ` DLTensor ` struct is ``NULL ``, it indicates a
161
162
row-major compact array. If the array is of size zero, the data pointer in
162
163
``DLTensor `` should be set to either ``NULL `` or ``0 ``.
163
164
164
165
For further details on DLPack design and how to implement support for it,
165
166
refer to `github.com/dmlc/dlpack <https://github.com/dmlc/dlpack >`_.
166
167
167
168
.. warning ::
168
- DLPack contains a `` device_id ` `, which will be the device
169
+ DLPack contains a :c:member: ` ~DLDevice. device_id `, which will be the device
169
170
ID (an integer, ``0, 1, ... ``) which the producer library uses. In
170
171
practice this will likely be the same numbering as that of the
171
172
consumer, however that is not guaranteed. Depending on the hardware
@@ -174,7 +175,7 @@ refer to `github.com/dmlc/dlpack <https://github.com/dmlc/dlpack>`_.
174
175
possible for example for CUDA device pointers.
175
176
176
177
It is recommended that implementers of this array API consider and document
177
- whether the `` . device` ` attribute of the array returned from ``from_dlpack `` is
178
+ whether the :attr: ` ~array_api.array. device ` attribute of the array returned from ``from_dlpack `` is
178
179
guaranteed to be in a certain order or not.
179
180
180
181
0 commit comments