@@ -90,6 +90,10 @@ def get_build_dlpack_version():
90
90
"""
91
91
Returns the string value of DLPACK_VERSION from dlpack.h
92
92
`dpctl.tensor` was built with.
93
+
94
+ Returns:
95
+ A string value of the version of DLPack used to build
96
+ `dpctl`.
93
97
"""
94
98
return str (DLPACK_VERSION)
95
99
@@ -112,9 +116,26 @@ cdef void _managed_tensor_deleter(DLManagedTensor *dlm_tensor) with gil:
112
116
113
117
cpdef to_dlpack_capsule(usm_ndarray usm_ary) except + :
114
118
"""
119
+ to_dlpack_capsule(usm_ary)
120
+
115
121
Constructs named Python capsule object referencing
116
122
instance of `DLManagerTensor` from
117
123
:class:`dpctl.tensor.usm_ndarray` instance.
124
+
125
+ Args:
126
+ usm_ary: An instance of :class:`dpctl.tensor.usm_ndarray`
127
+ Returns:
128
+ Python a new capsule with name "dltensor" that contains
129
+ a pointer to `DLManagedTensor` struct.
130
+ Raises:
131
+ DLPackCreationError: when array can be represented as
132
+ DLPack tensor. This may happen when array was allocated
133
+ on a partitioned sycl device, or its USM allocation is
134
+ not bound to the platform default SYCL context.
135
+ MemoryError: when host allocation to needed for `DLManagedTensor`
136
+ did not succeed.
137
+ ValueError: when array elements data type could not be represented
138
+ in `DLManagedTensor`.
118
139
"""
119
140
cdef c_dpctl.SyclQueue ary_sycl_queue
120
141
cdef c_dpctl.SyclDevice ary_sycl_device
@@ -144,15 +165,13 @@ cpdef to_dlpack_capsule(usm_ndarray usm_ary) except+:
144
165
" to_dlpack_capsule: DLPack can only export arrays allocated on "
145
166
" non-partitioned SYCL devices."
146
167
)
147
- # TODO: check that ary_sycl_context is the default context
148
168
default_context = dpctl.SyclQueue(ary_sycl_device).sycl_context
149
169
if not usm_ary.sycl_context == default_context:
150
170
raise DLPackCreationError(
151
171
" to_dlpack_capsule: DLPack can only export arrays based on USM "
152
172
" allocations bound to a default platform SYCL context"
153
173
)
154
174
155
-
156
175
dlm_tensor = < DLManagedTensor * > stdlib.malloc(
157
176
sizeof(DLManagedTensor))
158
177
if dlm_tensor is NULL :
@@ -244,9 +263,25 @@ cdef class _DLManagedTensorOwner:
244
263
245
264
cpdef usm_ndarray from_dlpack_capsule(object py_caps) except + :
246
265
"""
266
+ from_dlpack_capsule(caps)
267
+
247
268
Reconstructs instance of :class:`dpctl.tensor.usm_ndarray` from
248
269
named Python capsule object referencing instance of `DLManagedTensor`
249
270
without copy. The instance forms a view in the memory of the tensor.
271
+
272
+ Args:
273
+ caps: Python capsule with name "dltensor" expected to reference
274
+ an instance of `DLManagedTensor` struct.
275
+ Returns:
276
+ Instance of :class:`dpctl.tensor.usm_ndarray` with a view into
277
+ memory of the tensor. Capsule is renamed to "used_dltensor" upon
278
+ success.
279
+ Raises:
280
+ TypeError: if argument is not a "dltensor" capsule.
281
+ ValueError: if argument is "used_dltensor" capsule,
282
+ if the USM pointer is not bound to the reconstructed
283
+ sycl context, or the DLPack's device_type is not supported
284
+ by dpctl.
250
285
"""
251
286
cdef DLManagedTensor * dlm_tensor = NULL
252
287
cdef bytes usm_type
@@ -355,7 +390,7 @@ cpdef usm_ndarray from_dlpack_capsule(object py_caps) except +:
355
390
raise ValueError (
356
391
" Can not import DLPack tensor with type code {}." .format(
357
392
< object > dlm_tensor.dl_tensor.dtype.code
358
- )
393
+ )
359
394
)
360
395
res_ary = usm_ndarray(
361
396
py_shape,
@@ -379,6 +414,9 @@ cpdef from_dlpack(array):
379
414
object `obj` that implements `__dlpack__` protocol. The output
380
415
array is always a zero-copy view of the input.
381
416
417
+ Args:
418
+ A Python object representing an array that supports `__dlpack__`
419
+ protocol.
382
420
Raises:
383
421
TypeError: if `obj` does not implement `__dlpack__` method.
384
422
ValueError: if zero copy view can not be constructed because
0 commit comments