Add DLPack and numpy array protocols to OrtValue Python wrapper - #27836
Closed
Rishi-Dave wants to merge 1 commit into
Closed
Add DLPack and numpy array protocols to OrtValue Python wrapper#27836Rishi-Dave wants to merge 1 commit into
Rishi-Dave wants to merge 1 commit into
Conversation
Rishi-Dave
force-pushed
the
rishidave/feat/ortvalue-dlpack-array-protocols
branch
from
March 26, 2026 12:22
6afdcc8 to
8311f75
Compare
…alue Expose DLPack and numpy array protocols on the Python OrtValue wrapper class, enabling zero-copy interop with frameworks like PyTorch, JAX, and numpy via standard Python protocols: - __dlpack__(stream) and __dlpack_device__() delegate to the existing C binding methods, allowing torch.from_dlpack(ort_value) and np.from_dlpack(ort_value) to work directly. - from_dlpack(data) classmethod accepts any __dlpack__-compatible object (PyTorch tensors, numpy arrays, JAX arrays, other OrtValues) or raw DLPack capsules, with automatic bool tensor detection. - __array__(dtype, copy) implements the numpy array protocol so that np.array(ort_value) and np.asarray(ort_value) work seamlessly. Previously users had to access the internal _ortvalue attribute to use these protocols. Fixes microsoft#24071
Rishi-Dave
force-pushed
the
rishidave/feat/ortvalue-dlpack-array-protocols
branch
from
April 6, 2026 12:30
8311f75 to
acc297b
Compare
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.
Summary
__dlpack__(),__dlpack_device__(), andfrom_dlpack()to the PythonOrtValueclass, enabling zero-copy tensor sharing with PyTorch, JAX, NumPy, and other DLPack-compatible frameworks.__array__()protocol sonp.array(ort_value)andnp.asarray(ort_value)work directly.C.OrtValue) already supported these methods — this PR exposes them through the public Python wrapper class.Motivation
Fixes #24071
The Python
OrtValuewrapper class did not expose the DLPack or numpy array protocols, even though the underlying C binding had full support (added in #23110). Users had to access the internal_ortvalueattribute to use__dlpack__orfrom_dlpack, which is fragile and undocumented.With this change, standard Python interop patterns work out of the box:
Changes
onnxruntime/python/onnxruntime_inference_collection.py: Added 4 methods toOrtValue:__dlpack__(*, stream=None)— returns a DLPack capsule (delegates toC.OrtValue.__dlpack__)__dlpack_device__()— returns(device_type, device_id)tuplefrom_dlpack(data)— classmethod accepting any__dlpack__-compatible object or raw capsule, with automatic bool tensor detection viais_dlpack_uint8_tensor__array__(dtype=None, copy=None)— numpy array protocol, compatible with NumPy 1.x and 2.xonnxruntime/test/python/onnxruntime_test_python.py: Added 9 test cases covering:__dlpack__and__dlpack_device__on the Python wrapperfrom_dlpackwith OrtValue→OrtValue, numpy→OrtValue, and raw capsule→OrtValuenp.from_dlpack(ort_value)interopnp.array(),np.asarray(), and dtype conversion via__array__Test Plan
test_ort_value_dlpackandtest_ort_value_dlpack_zero_sizetests unaffectedruff checkandruff format --checkpass on both modified files