onnxruntime icon indicating copy to clipboard operation
onnxruntime copied to clipboard

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

Open Copilot opened this issue 8 months ago • 0 comments

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.

Copilot avatar Jun 14 '25 15:06 Copilot