Use new python dlpack interface, fixing warnings#1082
Merged
cliffburdick merged 1 commit intomainfrom Nov 1, 2025
Merged
Conversation
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() ```
Contributor
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
Updates Python DLPack interface from deprecated .toDlpack() to modern .__dlpack__() method, fixing deprecation warnings in CuPy.
Key changes:
- Replaced 5 calls to
.toDlpack()with.__dlpack__() - Added
stream=stream.ptrparameter to 3 calls for proper CUDA stream synchronization - Two simple usage examples (lines 15, 25) use
.__dlpack__()without stream parameter for default stream
Technical correctness:
The changes align with the new DLPack Python interface standard (https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html). The stream parameter properly communicates CUDA stream context to the consumer (MatX C++ code), which is critical for correct async GPU operations.
Confidence Score: 5/5
- This PR is safe to merge - it's a straightforward API migration that fixes deprecation warnings
- The changes are minimal, well-understood, and follow the official DLPack migration path. The new interface is more explicit about stream synchronization semantics, which actually improves correctness. All deprecated calls are properly updated with correct stream parameters where needed.
- No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| examples/python_integration_sample/example_matxutil.py | 5/5 | Updates deprecated .toDlpack() to new .__dlpack__() interface with stream parameter for proper synchronization |
Sequence Diagram
sequenceDiagram
participant PY as Python (CuPy)
participant DLP as DLPack Capsule
participant CPP as C++ (MatX)
participant GPU as CUDA Stream
Note over PY,GPU: Tensor Addition Flow (lines 70-73)
PY->>GPU: Create non-blocking stream
PY->>PY: Create CuPy arrays a, b, c
PY->>DLP: a.__dlpack__(stream=stream.ptr)
Note right of DLP: New interface with stream param
DLP-->>PY: Returns dlpack capsule (a_dlp)
PY->>DLP: b.__dlpack__(stream=stream.ptr)
DLP-->>PY: Returns dlpack capsule (b_dlp)
PY->>DLP: c.__dlpack__(stream=stream.ptr)
DLP-->>PY: Returns dlpack capsule (c_dlp)
PY->>CPP: matxutil.add_float_2D(c_dlp, a_dlp, b_dlp, stream.ptr)
CPP->>CPP: Unpack dlpack capsules
CPP->>CPP: Create MatX tensors from dlpack
CPP->>GPU: Execute (c = a + b) on stream
CPP-->>PY: Return control
PY->>GPU: stream.synchronize()
Note right of GPU: Wait for GPU operation
GPU-->>PY: Operation complete
Note over PY,GPU: Simple Usage (lines 15, 25)
PY->>PY: Create CuPy array
PY->>DLP: a.__dlpack__()
Note right of DLP: No stream param for default stream
DLP-->>PY: Returns dlpack capsule
PY->>CPP: Pass to C++ for processing
1 file reviewed, no comments
cliffburdick
approved these changes
Oct 31, 2025
Collaborator
|
/build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).Although I think this usage is correct, I'm not sure it is quite intended, as it doesn't formally "consume" the array (instead, it maintains a reference to the "unconsumed" object while operating on it, then lets it go out of scope, allowing it to be cleaned up). Part of the issue is that the Python dlpack interface is intended for python package interop, not Python <-> C++ interop.
This fixes some warnings when running the examples, e.g.