Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
033a887
Add func typedefs
adrianlizarraga Jul 9, 2025
fcdb5cf
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Jul 9, 2025
03eb5fa
stub apis
adrianlizarraga Jul 15, 2025
3310968
merge main
adrianlizarraga Jul 17, 2025
c3693de
new branch. add 2 streams first
adrianlizarraga Jul 19, 2025
a69d5f9
Move away from using Graph's graph_proto_ member
adrianlizarraga Jul 19, 2025
5743dcd
fix deref assignment
adrianlizarraga Jul 20, 2025
fd87e0c
Clean up
adrianlizarraga Jul 21, 2025
a40f463
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Jul 21, 2025
0dadf4d
Use std::filesystem::path in ModelCompilationOptions; fix memleak in …
adrianlizarraga Jul 21, 2025
d94cf44
fix unused variable warning (as error)
adrianlizarraga Jul 21, 2025
5bfbddb
Merge main and fix conflicts
adrianlizarraga Aug 28, 2025
69a4338
Update handler function signature to take in the ExternalDataInfo for…
adrianlizarraga Aug 28, 2025
90ade82
Add test that reuses external initializers from original model
adrianlizarraga Aug 29, 2025
c36afe5
Define new ExternalDataInfo constructor only for non-minimal builds
adrianlizarraga Aug 29, 2025
c07dc11
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Aug 29, 2025
4b83a2b
Fix unused variable warning (as error)
adrianlizarraga Aug 29, 2025
91acc8f
another unused variable
adrianlizarraga Aug 29, 2025
6e5629a
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Aug 29, 2025
9b092bf
clean up
adrianlizarraga Aug 29, 2025
049b9ad
Start adding csharp api funcs
adrianlizarraga Aug 29, 2025
8e00a06
Remove qnn_factory memleak fix (address in different PR)
adrianlizarraga Aug 29, 2025
11a6c74
Add ExternalInitializerInfo to C++ api
adrianlizarraga Aug 29, 2025
9ca882f
Add compile_to_stream py api
adrianlizarraga Aug 29, 2025
6d522d8
Python bindings and tests
adrianlizarraga Aug 30, 2025
af996bb
C# API for WriteBuffer delegate
adrianlizarraga Aug 31, 2025
9b27b31
c# api handle initializers
adrianlizarraga Aug 31, 2025
9607193
missing documentation in c#
adrianlizarraga Aug 31, 2025
e65710a
Add ExternalInitializerInfo C# class
adrianlizarraga Aug 31, 2025
c16b327
Full C# API for delegate that handles initializers
adrianlizarraga Sep 1, 2025
0b2f0e6
Update comment
adrianlizarraga Sep 2, 2025
83758d1
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 2, 2025
c62ed23
Address review comments
adrianlizarraga Sep 2, 2025
a35e7b6
Address review comments
adrianlizarraga Sep 3, 2025
d906855
Remove unused variable
adrianlizarraga Sep 3, 2025
255c2df
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 3, 2025
3db3117
Merge main conflicts
adrianlizarraga Sep 3, 2025
c7f98de
Merge main again
adrianlizarraga Sep 3, 2025
9031635
Address review comments for C#
adrianlizarraga Sep 3, 2025
abd0297
Rename functions in C and python
adrianlizarraga Sep 3, 2025
d5012fb
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 3, 2025
0e0497a
Address comments
adrianlizarraga Sep 4, 2025
0a61f1f
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Full C# API for delegate that handles initializers
  • Loading branch information
adrianlizarraga committed Sep 1, 2025
commit c16b3279dc38228fe2079b624ece2db2de80206a
54 changes: 50 additions & 4 deletions csharp/src/Microsoft.ML.OnnxRuntime/CompileModel.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,24 @@ public void SetOutputModelWriteBufferDelegate(WriteBufferDelegate writeBufferDel
_writeBufferDelegateState.GetConnectorHandleAsPointer()));
}

// TODO: Add return value and other parameters
/// <summary>
/// Delegate called by ORT for every initializer when generating the compiled model.
/// The delegate allows the user to determine whether the initializer should be stored within the compiled
/// model or externally in a file. If the delegate chooses to store an initializer externally, the delegate
/// implementation is responsible for writing the initializer data to a file.
/// </summary>
/// <param name="initializerName">The initializer's name.</param>
public delegate void HandleInitializerDelegate(string initializerName);
/// <param name="initializerValue">The readonly OrtValue instance containing the data, type, and
/// shape of the initializer.</param>
/// <param name="optionalExternalInfo">May be null. If the initializer is originally stored externally,
/// this contains the file path, file offset, and data size. Otherwise, this is null.</param>
/// <returns>A new OrtExternalInitializerInfo indicating the new location of the initializer.
/// Returns null if the initializer should be stored within the generated compiled model.</returns>
/// <remarks>The return value may be null.</remarks>
public delegate OrtExternalInitializerInfo HandleInitializerDelegate(
string initializerName,
IReadOnlyOrtValue initializerValue,
IReadOnlyExternalInitializerInfo optionalExternalInfo);

/// <summary>
/// Sets a delegate that is called by ORT for every initializer when generating the compiled model.
Expand Down Expand Up @@ -280,8 +289,45 @@ public static IntPtr HandleInitializerDelegateWrapper(
{

HandleInitializerConnector connector = (HandleInitializerConnector)GCHandle.FromIntPtr(state).Target;
var utf8InitializerName = NativeOnnxValueHelper.StringFromNativeUtf8(initializerName);
connector._csharpDelegate(utf8InitializerName);
string utf8InitializerName = NativeOnnxValueHelper.StringFromNativeUtf8(initializerName);
IReadOnlyOrtValue readOnlyInitializerValue = new OrtValue(initializerValue, owned: false);
IReadOnlyExternalInitializerInfo readOnlyExternalInfo = null;

if (externalInfo != IntPtr.Zero)
{
readOnlyExternalInfo = new OrtExternalInitializerInfo(externalInfo, ownsHandle: false);
}

// Call user's delegate, which may return the new location of the initializer.
OrtExternalInitializerInfo optionalNewExternalInfo = connector._csharpDelegate(
utf8InitializerName, readOnlyInitializerValue, readOnlyExternalInfo);

if (optionalNewExternalInfo != null)
{
// Delegate returned info about a new location for the initializer.
// Can't guarantee that the new external info returned by user's delegate is not referenced
// by other C# code. ORT expects to own the new external info, so create a copy here and
// give it to ORT.
string newFilePath = optionalNewExternalInfo.GetFilePath();
byte[] newFilePathBytes = NativeOnnxValueHelper.GetPlatformSerializedString(newFilePath);

IntPtr status = NativeMethods.OrtCreateExternalInitializerInfo(
newFilePathBytes,
optionalNewExternalInfo.GetFileOffset(),
(UIntPtr)optionalNewExternalInfo.GetByteSize(),
out newExternalInfo);

if (status != IntPtr.Zero)
{
return status;
}
}
else
{
// User's delegate did not return a new location for the initializer. ORT will store initializer
// within the generated compiled model.
newExternalInfo = IntPtr.Zero;
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,45 @@ internal static byte[] GetPlatformSerializedString(string str)
else
return StringToZeroTerminatedUtf8(str);
}

/// <summary>
/// Converts a null-terminated path string that is pointed to by the given IntPtr handle into
/// a C# UTF-16 string.
/// </summary>
/// <remarks>A path string on Windows is utf-16, but utf-8 on other operating systems.</remarks>
/// <param name="strPtr"></param>
/// <returns></returns>
internal static string StringFromNativePathString(IntPtr strPtr)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (strPtr == IntPtr.Zero)
{
return string.Empty;
}

// Get length of utf16 string by checking for two 0 bytes in a row.
int length = 0;
while (Marshal.ReadInt16(strPtr, length * 2) != 0)
{
length += 1;
}

if (length == 0)
{
return string.Empty;
}

unsafe
{
return System.Text.Encoding.Unicode.GetString((byte*)strPtr, length * 2);
}
}
else
{
return StringFromNativeUtf8(strPtr);
}
}
}

// Guards an array of disposable objects on stack and disposes them in reverse order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Microsoft.ML.OnnxRuntime
/// Class to that stores information about the file location where an "external" initializer is stored.
/// </summary>
/// <see cref="OrtModelCompilationOptions.HandleInitializerDelegate"/>
public class OrtExternalInitializerInfo : SafeHandle
public class OrtExternalInitializerInfo : SafeHandle, IReadOnlyExternalInitializerInfo
{
// Set to true when constructed with an externally managed constant handle owned by ORT.
private readonly bool _isConst;
private readonly bool _ownsHandle = true;

/// <summary>
/// Create a new OrtExternalInitializerInfo instance.
Expand All @@ -28,26 +28,25 @@ public OrtExternalInitializerInfo(string filePath, long fileOffset, long byteSiz
var platformFilePath = NativeOnnxValueHelper.GetPlatformSerializedString(filePath);
NativeApiStatus.VerifySuccess(
NativeMethods.OrtCreateExternalInitializerInfo(platformFilePath, fileOffset, (UIntPtr)byteSize, out handle));
_isConst = false;
_ownsHandle = true;
}

/// <summary>
/// Create a new OrtExternalInitializerInfo instance from an existing native OrtExternalInitializerInfo handle.
/// </summary>
/// <param name="constHandle">Native OrtExternalInitializerInfo handle.</param>
/// <remarks>
/// The instance is read-only.
/// </remarks>
/// <param name="ownsHandle">True if the OrtExternalInitializerInfo instance owns the native handle.
/// Defaults to false.</param>
/// <exception cref="InvalidOperationException"></exception>
internal OrtExternalInitializerInfo(IntPtr constHandle)
: base(IntPtr.Zero, ownsHandle: false)
internal OrtExternalInitializerInfo(IntPtr constHandle, bool ownsHandle = false)
: base(IntPtr.Zero, ownsHandle)
{
if (constHandle == IntPtr.Zero)
{
throw new InvalidOperationException($"{nameof(OrtExternalInitializerInfo)}: Invalid instance.");
}
SetHandle(constHandle);
_isConst = true;
_ownsHandle = ownsHandle;
}

/// <summary>
Expand All @@ -65,7 +64,7 @@ public string GetFilePath()
return string.Empty;
}

return NativeOnnxValueHelper.StringFromNativeUtf8(filePathPtr);
return NativeOnnxValueHelper.StringFromNativePathString(filePathPtr);
}

/// <summary>
Expand Down Expand Up @@ -98,7 +97,7 @@ public long GetByteSize()
/// <returns>true on success and false on error.</returns>
protected override bool ReleaseHandle()
{
if (_isConst)
if (!_ownsHandle)
{
// Return false to indicate an error.
// ReleaseHandle() should not be called on a const handle that this class does not own.
Expand All @@ -110,4 +109,31 @@ protected override bool ReleaseHandle()
return true;
}
}

/// <summary>
/// Interface for all readonly methods implemented by OrtExternalInitializerInfo.
/// </summary>
public interface IReadOnlyExternalInitializerInfo
{
/// <summary>
/// Get the file path to the file that store's the initializer's data.
/// </summary>
/// <remarks>
/// The path is relative to the filesystem directory where the ONNX model was stored.
/// </remarks>
/// <returns>The file path.</returns>
string GetFilePath();

/// <summary>
/// Get the byte offset within the file where the initializer's data is stored.
/// </summary>
/// <returns>The file offset location.</returns>
long GetFileOffset();

/// <summary>
/// Get the size in bytes of the initializer's data within the file.
/// </summary>
/// <returns>The size in bytes of the initializer data.</returns>
long GetByteSize();
}
}
Loading
Loading