Skip to content

Use new python dlpack interface, fixing warnings#1082

Merged
cliffburdick merged 1 commit intomainfrom
sbyrne/dlpack-update
Nov 1, 2025
Merged

Use new python dlpack interface, fixing warnings#1082
cliffburdick merged 1 commit intomainfrom
sbyrne/dlpack-update

Conversation

@simonbyrne
Copy link
Collaborator

@simonbyrne simonbyrne commented Oct 31, 2025

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.

/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()

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()
```
@copy-pr-bot
Copy link

copy-pr-bot bot commented Oct 31, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ptr parameter 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
Loading

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@cliffburdick
Copy link
Collaborator

/build

@cliffburdick cliffburdick merged commit bf9c4c0 into main Nov 1, 2025
1 check passed
@cliffburdick cliffburdick deleted the sbyrne/dlpack-update branch November 1, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants