Skip to content

[Feature] Add numpy array protocol and enhanced dlpack support to OrtValue #25062

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 14, 2025

This PR implements numpy array protocol compatibility and enhanced dlpack support for onnxruntime.OrtValue, enabling seamless integration with the Python tensor ecosystem.

Features Added

1. Numpy Array Protocol Support (__array__)

Enables OrtValue to work seamlessly with numpy functions:

import onnxruntime as ort
import numpy as np

ort_value = ort.OrtValue.ortvalue_from_numpy(np.array([1, 2, 3]))

# Now these work automatically:
numpy_array = np.array(ort_value)
asarray_result = np.asarray(ort_value)

2. Enhanced DLPack Protocol Support

Exposes dlpack methods directly on the Python OrtValue class:

# Direct dlpack protocol access
dlpack_capsule = ort_value.__dlpack__()
device_info = ort_value.__dlpack_device__()
dlpack_capsule2 = ort_value.to_dlpack()  # Convenience method

3. Intelligent from_dlpack() Method

Automatically detects and calls __dlpack__() on source objects:

import torch

# Before: Required manual __dlpack__ call
torch_tensor = torch.tensor([1.0, 2.0])
ort_value = ort.OrtValue.from_dlpack(torch_tensor.__dlpack__())

# After: Automatic detection and calling
ort_value = ort.OrtValue.from_dlpack(torch_tensor)  # Auto-calls __dlpack__!

# Works with any object that has __dlpack__ method
ort_value1 = ort.OrtValue.ortvalue_from_numpy(array)
ort_value2 = ort.OrtValue.from_dlpack(ort_value1)  # Auto-calls __dlpack__

Implementation Details

  • Minimal Changes: Only 56 lines added to core implementation
  • Full Backward Compatibility: All existing APIs continue to work unchanged
  • Proper Delegation: Python methods delegate to existing C++ implementations
  • Type Safety: Complete type annotations for better developer experience
  • Comprehensive Testing: Added extensive test coverage for all new functionality

Backward Compatibility

All existing functionality is preserved:

  • ortvalue._ortvalue.to_dlpack() still works
  • OrtValue.from_dlpack(dlpack_capsule, is_bool_tensor=False) still works
  • No breaking changes to any existing APIs

Benefits

  1. Pythonic Integration: OrtValue now works seamlessly with numpy ecosystem
  2. Cross-Library Compatibility: Easy tensor sharing between PyTorch, CuPy, JAX, etc.
  3. Simplified API: No need to manually call __dlpack__() methods
  4. Modern Standards: Implements current Python tensor protocols

Fixes #24071.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Feature Request] Make OrtValue compatible with numpy __array__ and dlpack protocols [Feature] Add numpy array protocol and enhanced dlpack support to OrtValue Jun 14, 2025
@Copilot Copilot AI requested a review from justinchuby June 14, 2025 16:11
Copilot finished work on behalf of justinchuby June 14, 2025 16:11
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.

[Feature Request] Make OrtValue compatible with numpy __array__ and dlpack protocols
2 participants