-
Notifications
You must be signed in to change notification settings - Fork 157
Update exchange API to be capsule following new convention #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tqchen
wants to merge
6
commits into
dmlc:main
Choose a base branch
from
tqchen:c-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5eeaaae
Update exchange API to be capsule following new convention
tqchen 1f18a9e
[DOCS] Update the exchange api spec
tqchen bd96c5f
Update docs/source/python_spec.rst
tqchen 4d25c02
Update docs/source/python_spec.rst
tqchen 68cb293
update per lint
tqchen 29c2602
use commit
tqchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,6 +179,42 @@ refer to `github.com/dmlc/dlpack <https://github.com/dmlc/dlpack>`_. | |
| guaranteed to be in a certain order or not. | ||
|
|
||
|
|
||
| DLPack C Exchange API | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Starting with DLPack 1.3, a new C Exchange API is introduced to enable faster | ||
| data exchange than the Python ``__dlpack__`` API at the C extension level. | ||
| Producer array frameworks may provide a ``__dlpack_c_exchange_api__`` | ||
| attribute on the array type. | ||
| The attribute should be a ``PyCapsule`` with name ``"dlpack_exchange_api"``. | ||
| The consumer can query whether this attribute exists and use it at the C extension level. | ||
| Notably, consumer frameworks can always start implementing by only using the Python ``__dlpack__`` API, | ||
| and then upgrade to the C Exchange API later when faster data exchange is needed. | ||
|
|
||
| .. code-block:: C | ||
|
|
||
| PyObject *api_obj = type(tensor_obj).__dlpack_c_exchange_api__; // as C code. | ||
| MyDLPackExchangeAPI *api = PyCapsule_GetPointer(api_obj, "dlpack_exchange_api"); | ||
| if (api == NULL && PyErr_Occurred()) { goto handle_error; } | ||
|
|
||
|
|
||
| .. note:: Implementation of the C Exchange API | ||
|
|
||
| Producer framework should implement the C Exchange API in a static way either | ||
| through Cython, Python C extensions, or Python binding mechanism. Importantly, | ||
| because the DLPack C exchange API operates at the C extension level, we need | ||
| direct interaction between the array framework ``PyObject*`` and DLPack, | ||
| as a result it is harder to implement the C Exchange API through ctypes (because | ||
| ctypes releases GIL by default and sometimes in non-free-threading environment, | ||
| GIL is needed to interact with the Python C API). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| A reference implementations of the C Exchange API in frameworks: | ||
|
|
||
|
|
||
| * PyTorch: `C++ <https://github.com/pytorch/pytorch/blob/main/torch/csrc/Module.cpp#L692>`__ | ||
tqchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Paddle: `C++ <https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/pybind/pybind.cc#L856>`__ | ||
tqchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| Reference Implementations | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
|
|
@@ -198,3 +234,4 @@ ctypes, cffi, etc: | |
| * mpi4py: `Cython <https://github.com/mpi4py/mpi4py/blob/master/src/mpi4py/MPI.src/asdlpack.pxi>`_ | ||
| * Paddle: `C++ <https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/framework/tensor_util.cc#L901-L951>`__, `Python wrapper using Python C API <https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/pybind/pybind.cc#L1263-L1280>`__ | ||
| * Hidet: `ctypes <https://github.com/hidet-org/hidet/blob/main/python/hidet/graph/impl/dlpack.py>`__ | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.