// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; namespace Microsoft.ML.OnnxRuntime { /// /// A non-owning view of named EPContext data passed to a write delegate. /// The view is valid only for the duration of the delegate invocation. /// public sealed class OrtEpContextData { internal OrtEpContextData(IntPtr pointer, UIntPtr size) { _pointer = pointer; Size = size.ToUInt64(); _isValid = true; } /// /// Size of the data in bytes. /// public ulong Size { get; } /// /// Returns all data as a read-only span. For data larger than , /// use to process it in chunks. /// /// A read-only span over the data. public ReadOnlySpan GetSpan() { ThrowIfInvalid(); if (Size > int.MaxValue) { throw new InvalidOperationException("EPContext data is too large for a single Span. Read it in chunks."); } return GetSpan(0, checked((int)Size)); } /// /// Returns a read-only span for a range of the data. /// /// Byte offset into the data. /// Number of bytes in the returned span. /// A read-only span over the requested range. public unsafe ReadOnlySpan GetSpan(ulong offset, int length) { ThrowIfInvalid(); ValidateRange(offset, length); if (length == 0) { return ReadOnlySpan.Empty; } return new ReadOnlySpan(AddOffset(_pointer, offset).ToPointer(), length); } internal void Invalidate() { _isValid = false; _pointer = IntPtr.Zero; } internal static IntPtr AddOffset(IntPtr pointer, ulong offset) { if (UIntPtr.Size == 4) { ulong address = unchecked((uint)pointer.ToInt32()); if (offset > uint.MaxValue || address + offset > uint.MaxValue) { throw new ArgumentOutOfRangeException(nameof(offset)); } return new IntPtr(unchecked((int)(uint)(address + offset))); } ulong address64 = unchecked((ulong)pointer.ToInt64()); if (offset > ulong.MaxValue - address64) { throw new ArgumentOutOfRangeException(nameof(offset)); } return new IntPtr(unchecked((long)(address64 + offset))); } private void ThrowIfInvalid() { if (!_isValid) { throw new ObjectDisposedException(nameof(OrtEpContextData), "EPContext data is valid only during the write delegate invocation."); } } private void ValidateRange(ulong offset, int length) { if (length < 0 || offset > Size || (ulong)length > Size - offset) { throw new ArgumentOutOfRangeException(nameof(length)); } if (length != 0 && _pointer == IntPtr.Zero) { throw new InvalidOperationException("EPContext data has a null buffer for a non-empty range."); } } private IntPtr _pointer; private bool _isValid; } /// /// Allocator-backed output supplied to an EPContext read delegate. /// Allocate and fill the buffer during the delegate invocation. On success, ownership is transferred to ORT. /// public sealed class OrtEpContextDataBuffer : IDisposable { internal OrtEpContextDataBuffer(IntPtr allocator, ulong maxDataSize) { _allocator = new OrtAllocator(allocator, false); _maxDataSize = maxDataSize; try { using var memoryInfo = _allocator.Info; EnsureCpuAccessibleAllocator(memoryInfo); } catch { _allocator.Dispose(); throw; } } internal static void EnsureCpuAccessibleAllocator(OrtMemoryInfo memoryInfo) { if (memoryInfo.GetDeviceType() != OrtMemoryInfoDeviceType.CPU && memoryInfo.GetDeviceMemoryType() != OrtDeviceMemoryType.HOST_ACCESSIBLE) { throw new InvalidOperationException( "EPContext read callbacks require a CPU or host-accessible allocator."); } } /// /// Size of the allocated buffer in bytes. /// public ulong Size { get; private set; } /// /// Whether the delegate has allocated an output buffer. A zero-byte allocation is valid. /// public bool IsAllocated { get; private set; } /// /// Allocates an output buffer and returns it as a writable span. /// For buffers larger than , use /// followed by chunked calls to . /// /// Number of bytes to allocate. /// A writable span over the allocated buffer. public Span Allocate(int size) { if (size < 0) { throw new ArgumentOutOfRangeException(nameof(size)); } Allocate((ulong)size); return GetSpan(); } /// /// Allocates an output buffer of the requested size. /// /// Number of bytes to allocate. public void Allocate(ulong size) { ThrowIfDisposed(); if (IsAllocated) { throw new InvalidOperationException("The EPContext data buffer has already been allocated."); } if (size > _maxDataSize) { throw new ArgumentOutOfRangeException(nameof(size), "EPContext data exceeds the configured maximum size."); } UIntPtr nativeSize = ToUIntPtr(size); IntPtr pointer = IntPtr.Zero; if (size != 0) { NativeApiStatus.VerifySuccess(NativeMethods.OrtAllocatorAlloc(_allocator.Pointer, nativeSize, out pointer)); if (pointer == IntPtr.Zero) { throw new InvalidOperationException("The ORT allocator returned a null pointer for a non-empty allocation."); } } _pointer = pointer; Size = size; IsAllocated = true; } /// /// Returns the entire allocated buffer as a writable span. /// /// A writable span over the allocated buffer. public Span GetSpan() { if (Size > int.MaxValue) { throw new InvalidOperationException("EPContext data is too large for a single Span. Fill it in chunks."); } return GetSpan(0, checked((int)Size)); } /// /// Returns a writable span for a range of the allocated buffer. /// /// Byte offset into the allocated buffer. /// Number of bytes in the returned span. /// A writable span over the requested range. public unsafe Span GetSpan(ulong offset, int length) { ThrowIfDisposed(); if (!IsAllocated) { throw new InvalidOperationException("Allocate must be called before accessing the EPContext data buffer."); } if (length < 0 || offset > Size || (ulong)length > Size - offset) { throw new ArgumentOutOfRangeException(nameof(length)); } if (length == 0) { return Span.Empty; } return new Span(OrtEpContextData.AddOffset(_pointer, offset).ToPointer(), length); } internal void Detach(out IntPtr pointer, out UIntPtr size) { ThrowIfDisposed(); if (!IsAllocated) { throw new InvalidOperationException("The EPContext data read delegate must allocate its output buffer."); } pointer = _pointer; size = ToUIntPtr(Size); _pointer = IntPtr.Zero; _disposed = true; _allocator.Dispose(); _allocator = null; } /// /// Releases an allocation that was not transferred to ORT because the delegate failed. /// public void Dispose() { if (_disposed) { return; } if (_pointer != IntPtr.Zero) { _allocator.FreeMemory(_pointer); _pointer = IntPtr.Zero; } _allocator.Dispose(); _allocator = null; _disposed = true; GC.SuppressFinalize(this); } private static UIntPtr ToUIntPtr(ulong value) { if (UIntPtr.Size == 4 && value > uint.MaxValue) { throw new ArgumentOutOfRangeException(nameof(value)); } return new UIntPtr(value); } private void ThrowIfDisposed() { if (_disposed) { throw new ObjectDisposedException(nameof(OrtEpContextDataBuffer)); } } private OrtAllocator _allocator; private readonly ulong _maxDataSize; private IntPtr _pointer; private bool _disposed; } }