Skip to content

Conversation

yuslepukhin
Copy link
Member

This pull request adds new APIs and updates existing ones to improve memory and device information handling in the ONNX Runtime C# bindings. The most significant changes introduce methods for fetching memory info and device info for session inputs/outputs, and add support for shared allocators and synchronization streams. There are also several updates and renamings for LoraAdapter delegates and related APIs.

Memory and Device Info APIs

  • Added GetMemoryInfosForInputs, GetMemoryInfosForOutputs, and GetEpDeviceForInputs methods to InferenceSession.shared.cs to fetch memory info and device info for session inputs/outputs. These methods utilize new native delegates for retrieving memory and device information.
  • Introduced native delegates in NativeMethods.shared.cs for OrtSessionGetMemoryInfoForInputs, OrtSessionGetMemoryInfoForOutputs, and OrtSessionGetEpDeviceForInputs, and wired them up in the static constructor. [1] [2]

Shared Allocator and Synchronization Stream Support

  • Added delegates and static fields for creating, getting, and releasing shared allocators, as well as for creating and managing synchronization streams (OrtCreateSharedAllocator, OrtGetSharedAllocator, OrtReleaseSharedAllocator, OrtCreateSyncStreamForEpDevice, OrtSyncStream_GetHandle, OrtReleaseSyncStream).
  • Added delegate for copying tensors (OrtCopyTensors).

LoraAdapter API Updates

  • Renamed LoraAdapter-related delegates to use the Ort prefix (OrtCreateLoraAdapter, OrtCreateLoraAdapterFromArray, OrtReleaseLoraAdapter) and updated their usage throughout the codebase. [1] [2] [3]

MemoryInfo Enhancements

  • Added new delegates for creating memory info with more parameters (OrtCreateMemoryInfoV2), and for querying device memory type and vendor ID (OrtMemoryInfoGetDeviceMemType, OrtMemoryInfoGetVendorId). [1] [2] [3]

Minor API Documentation Update

  • Clarified the lifetime of allocators in the documentation, noting they can be explicitly unregistered.### Description

Motivation and Context

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements missing ONNX Runtime C# APIs to enhance memory management, device information handling, and synchronization capabilities. The changes focus on providing developers with better control over memory allocation and device-specific operations in ONNX Runtime C# bindings.

Key changes include:

  • Added memory and device info retrieval methods for session inputs/outputs
  • Introduced shared allocator management APIs with creation, retrieval, and release capabilities
  • Implemented synchronization stream support for async operations and tensor copying
  • Updated LoraAdapter APIs with consistent naming conventions

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs Added native delegates for memory info, shared allocators, sync streams, and tensor copying APIs
csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.shared.cs Implemented session methods to retrieve memory info and device info for inputs/outputs
csharp/src/Microsoft.ML.OnnxRuntime/OrtEnv.shared.cs Added shared allocator management and tensor copying functionality
csharp/src/Microsoft.ML.OnnxRuntime/OrtEpDevice.shared.cs Added OrtSyncStream class and device memory info retrieval methods
csharp/src/Microsoft.ML.OnnxRuntime/OrtAllocator.shared.cs Enhanced OrtMemoryInfo with new device memory types and V2 constructor
csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/OrtEnvTests.cs Added comprehensive tests for shared allocator APIs and tensor copying
csharp/test/Microsoft.ML.OnnxRuntime.Tests.NetCoreApp/InferenceTest.netcore.cs Added tests for new session memory info retrieval methods
csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs Updated memory info tests and moved shared allocator tests to OrtEnvTests

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@yuslepukhin yuslepukhin merged commit 35dcab5 into main Sep 24, 2025
92 checks passed
@yuslepukhin yuslepukhin deleted the yuslepukhin/cs_api_additions branch September 24, 2025 17:50
adrianlizarraga pushed a commit that referenced this pull request Sep 26, 2025
This pull request adds new APIs and updates existing ones to improve
memory and device information handling in the ONNX Runtime C# bindings.
The most significant changes introduce methods for fetching memory info
and device info for session inputs/outputs, and add support for shared
allocators and synchronization streams. There are also several updates
and renamings for LoraAdapter delegates and related APIs.

### Memory and Device Info APIs

* Added `GetMemoryInfosForInputs`, `GetMemoryInfosForOutputs`, and
`GetEpDeviceForInputs` methods to `InferenceSession.shared.cs` to fetch
memory info and device info for session inputs/outputs. These methods
utilize new native delegates for retrieving memory and device
information.
* Introduced native delegates in `NativeMethods.shared.cs` for
`OrtSessionGetMemoryInfoForInputs`, `OrtSessionGetMemoryInfoForOutputs`,
and `OrtSessionGetEpDeviceForInputs`, and wired them up in the static
constructor.
[[1]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73R530-R532)
[[2]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73R1312-R1335)

### Shared Allocator and Synchronization Stream Support

* Added delegates and static fields for creating, getting, and releasing
shared allocators, as well as for creating and managing synchronization
streams (`OrtCreateSharedAllocator`, `OrtGetSharedAllocator`,
`OrtReleaseSharedAllocator`, `OrtCreateSyncStreamForEpDevice`,
`OrtSyncStream_GetHandle`, `OrtReleaseSyncStream`).
* Added delegate for copying tensors (`OrtCopyTensors`).

### LoraAdapter API Updates

* Renamed LoraAdapter-related delegates to use the `Ort` prefix
(`OrtCreateLoraAdapter`, `OrtCreateLoraAdapterFromArray`,
`OrtReleaseLoraAdapter`) and updated their usage throughout the
codebase.
[[1]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73L699-R710)
[[2]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73L1561-R1672)
[[3]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73L1578-R1695)

### MemoryInfo Enhancements

* Added new delegates for creating memory info with more parameters
(`OrtCreateMemoryInfoV2`), and for querying device memory type and
vendor ID (`OrtMemoryInfoGetDeviceMemType`, `OrtMemoryInfoGetVendorId`).
[[1]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73R594-R596)
[[2]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73R1804-R1817)
[[3]](diffhunk://#diff-f9f2aaafc076365917de8ab96628da427d9dd0fd6a214fb9c266733f90d6fc73R1866-R1877)

### Minor API Documentation Update

* Clarified the lifetime of allocators in the documentation, noting they
can be explicitly unregistered.### Description
<!-- Describe your changes. -->



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
snnn pushed a commit that referenced this pull request Sep 27, 2025
### Description
Adds the following commits to the `rel-1.23.1` branch for ORT 1.23.1:


- add session_id_ to LogEvaluationStart/Stop, LogSessionCreationStart
  - main merge date: July 31, 1:05am
  - pr: #25590
  - commit: e753643
- [build] fix WebAssembly build on macOS/arm64
  - main merge date: Aug 5, 8:07am
  - pr: #25653
  - commit: 53f152b
- [CPU] MoE Kernel (#25958)
  - main merge date: Sept 10, 4:54pm
  - pr: #25958
  - commit: 930e640
- [CPU] Block-wise QMoE kernel for CPU
  - main merge date: Sept 15, 8:32am
  - pr: #26009
  - commit: 5d17734
- [C#] Implement missing APIs
  - main merge date: Sept 24, 10:50am
  - pr: #26101
  - commit: 35dcab5
- Regenerate test model with ONNX IR < 12
  - main merge date: Sept 24, 2:50pm
  - pr: #26149
  - commit: 88f2652
- [CPU] Fix compilation errors because of unused variables
  - main merge date: Sept 25, 1:21pm
  - pr: #26147
  - commit: 42fcd71
- [EP ABI] Check if nodes specified in GetCapability() have already been
assigned
  - main merge date: Sept 26, 1:24am
  - pr: #26156
  - commit: 67d3ba0
- [QNN EP] Add dynamic option to set HTP performance mode
  - main merge date: Sept 26, 11:55am
  - pr: #26135
  - commit: 6cc40fd

---------

Co-authored-by: xieofxie <[email protected]>
Co-authored-by: hualxie <[email protected]>
Co-authored-by: Yulong Wang <[email protected]>
Co-authored-by: Akshay Sonawane <[email protected]>
Co-authored-by: Dmitri Smirnov <[email protected]>
Co-authored-by: Edward Chen <[email protected]>
Co-authored-by: quic-tirupath <[email protected]>
Co-authored-by: quic-ashwshan <[email protected]>
@snnn
Copy link
Member

snnn commented Sep 27, 2025

This PR has been cherry-picked into the rel-1.23.1 branch in PR #26182. Removing the release:1.23.1 label.

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.

3 participants