Skip to content

feat(pyo3): consumer + producer DLPackExchangeAPI#30

Open
HaoZeke wants to merge 2 commits into
metatensor:mainfrom
HaoZeke:feat/dlpack-exchange-api
Open

feat(pyo3): consumer + producer DLPackExchangeAPI#30
HaoZeke wants to merge 2 commits into
metatensor:mainfrom
HaoZeke:feat/dlpack-exchange-api

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Apr 17, 2026

Copy link
Copy Markdown
Member

Consumer side: adds ExchangeAPI, a Copy wrapper around the producer's function table exposed via type(obj).__dlpack_c_exchange_api__, with safe methods for all four callback slots:
current_work_stream, managed_tensor_from_pyobject, managed_tensor_to_pyobject, and with_dltensor_from (closure-style so the producer-owned shape/stride borrow stays scoped).

Producer side: the PyDLPack pyclass exposes a static DLPackExchangeAPI via a __dlpack_c_exchange_api__ classattr. Three callbacks are wired up to the existing capsule plumbing: managed_tensor_from_py_object_no_sync transfers ownership out by renaming the capsule to used_dltensor_versioned (same mechanism as the legacy __dlpack__ path, so a second consume fails cleanly); dltensor_from_py_object_no_sync fills a stack DLTensor borrowing the capsule's storage; managed_tensor_to_py_object_no_sync rewraps an owned DLPackTensor as a fresh PyDLPack. current_work_stream returns null for all devices -- dlpk does not own an accelerator stream, and the upstream producer (if any) is expected to sync before handing the tensor off. Each C callback is panic-caught so a Rust panic cannot unwind across the extern "C" boundary.

The capsule name constant moves to sys::DLPACK_EXCHANGE_API_CAPSULE_NAME.

Tests: a pure-Rust stub producer roundtrips a 1D f32 tensor through the consumer wrapper, and the PyDLPack side roundtrips both through the fast borrow (with_dltensor_from) and the fast consume (managed_tensor_from_pyobject), with a second consume asserted to fail. managed_tensor_to_pyobject is covered by re-exporting the returned PyDLPack through __dlpack__().

Closes #18.

Consumer side: adds `ExchangeAPI`, a Copy wrapper around the producer's
function table exposed via `type(obj).__dlpack_c_exchange_api__`, with
safe methods for all four callback slots:
`current_work_stream`, `managed_tensor_from_pyobject`,
`managed_tensor_to_pyobject`, and `with_dltensor_from` (closure-style
so the producer-owned shape/stride borrow stays scoped).

Producer side: the `PyDLPack` pyclass exposes a static
`DLPackExchangeAPI` via a `__dlpack_c_exchange_api__` classattr. Three
callbacks are wired up to the existing capsule plumbing:
`managed_tensor_from_py_object_no_sync` transfers ownership out by
renaming the capsule to `used_dltensor_versioned` (same mechanism as
the legacy `__dlpack__` path, so a second consume fails cleanly);
`dltensor_from_py_object_no_sync` fills a stack DLTensor borrowing the
capsule's storage; `managed_tensor_to_py_object_no_sync` rewraps an
owned DLPackTensor as a fresh PyDLPack. `current_work_stream` returns
null for all devices -- dlpk does not own an accelerator stream, and
the upstream producer (if any) is expected to sync before handing the
tensor off. Each C callback is panic-caught so a Rust panic cannot
unwind across the extern "C" boundary.

The capsule name constant moves to `sys::DLPACK_EXCHANGE_API_CAPSULE_NAME`.

Tests: a pure-Rust stub producer roundtrips a 1D f32 tensor through
the consumer wrapper, and the PyDLPack side roundtrips both through
the fast borrow (`with_dltensor_from`) and the fast consume
(`managed_tensor_from_pyobject`), with a second consume asserted to
fail. `managed_tensor_to_pyobject` is covered by re-exporting the
returned PyDLPack through `__dlpack__()`.

Closes metatensor#18.
@HaoZeke HaoZeke requested a review from Luthaf April 17, 2026 08:59
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.

feat(v1.3): Support the __dlpack_c_exchange_api__

1 participant