Skip to content

Commit bf9c4c0

Browse files
authored
Use new python dlpack interface, fixing warnings (#1082)
dlpack changed their Python interface to make the stream synchronization semantics clearer (see dmlc/dlpack#57). The interface now involves the consumer calling the __dlpack__() method on the producer array, with an optional stream argument (see https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html). I'm not sure our example is strictly correct, since we don't formally "consume" the array (instead, we just maintain a reference while operating on it, then let it go out of scope). This fixes some warnings when running the examples, e.g. ``` /workspaces/MatX/build/../examples/python_integration_sample/example_matxutil.py:70: VisibleDeprecationWarning: This function is deprecated and will be removed in a future release. Use the cupy.from_dlpack() array constructor instead. c_dlp = c.toDlpack() /workspaces/MatX/build/../examples/python_integration_sample/example_matxutil.py:71: VisibleDeprecationWarning: This function is deprecated and will be removed in a future release. Use the cupy.from_dlpack() array constructor instead. a_dlp = a.toDlpack() /workspaces/MatX/build/../examples/python_integration_sample/example_matxutil.py:72: VisibleDeprecationWarning: This function is deprecated and will be removed in a future release. Use the cupy.from_dlpack() array constructor instead. b_dlp = b.toDlpack() ```
1 parent 0f53ca5 commit bf9c4c0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

examples/python_integration_sample/example_matxutil.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Demonstrate dlpack consumption invalidates it for future use
1313
def dlp_usage_error():
1414
a = cp.empty((3,3), dtype=cp.float32)
15-
dlp = a.toDlpack()
15+
dlp = a.__dlpack__()
1616
assert(matxutil.check_dlpack_status(dlp) == 0)
1717
a2 = cp.from_dlpack(dlp) # causes dlp to become unused
1818
assert(matxutil.check_dlpack_status(dlp) != 0)
@@ -22,7 +22,7 @@ def dlp_usage_error():
2222
def scope_okay():
2323
a = cp.empty((3,3), dtype=cp.float32)
2424
a[1,1] = 2
25-
dlp = a.toDlpack()
25+
dlp = a.__dlpack__()
2626
assert(matxutil.check_dlpack_status(dlp) == 0)
2727
return dlp
2828

@@ -67,9 +67,9 @@ def scope_okay():
6767
b = cp.array([[1,2,3],[4,5,6],[7,8,9]], dtype=cp.float32)
6868
c = cp.empty(b.shape, dtype=b.dtype)
6969

70-
c_dlp = c.toDlpack()
71-
a_dlp = a.toDlpack()
72-
b_dlp = b.toDlpack()
70+
c_dlp = c.__dlpack__(stream=stream.ptr)
71+
a_dlp = a.__dlpack__(stream=stream.ptr)
72+
b_dlp = b.__dlpack__(stream=stream.ptr)
7373
matxutil.add_float_2D(c_dlp, a_dlp, b_dlp, stream.ptr)
7474
stream.synchronize()
7575
print(f"Tensor a {a}")

0 commit comments

Comments
 (0)