-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Compile API: output model and initializer stream write functions #25455
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
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
033a887
Add func typedefs
adrianlizarraga fcdb5cf
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 03eb5fa
stub apis
adrianlizarraga 3310968
merge main
adrianlizarraga c3693de
new branch. add 2 streams first
adrianlizarraga a69d5f9
Move away from using Graph's graph_proto_ member
adrianlizarraga 5743dcd
fix deref assignment
adrianlizarraga fd87e0c
Clean up
adrianlizarraga a40f463
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 0dadf4d
Use std::filesystem::path in ModelCompilationOptions; fix memleak in …
adrianlizarraga d94cf44
fix unused variable warning (as error)
adrianlizarraga 5bfbddb
Merge main and fix conflicts
adrianlizarraga 69a4338
Update handler function signature to take in the ExternalDataInfo for…
adrianlizarraga 90ade82
Add test that reuses external initializers from original model
adrianlizarraga c36afe5
Define new ExternalDataInfo constructor only for non-minimal builds
adrianlizarraga c07dc11
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 4b83a2b
Fix unused variable warning (as error)
adrianlizarraga 91acc8f
another unused variable
adrianlizarraga 6e5629a
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 9b092bf
clean up
adrianlizarraga 049b9ad
Start adding csharp api funcs
adrianlizarraga 8e00a06
Remove qnn_factory memleak fix (address in different PR)
adrianlizarraga 11a6c74
Add ExternalInitializerInfo to C++ api
adrianlizarraga 9ca882f
Add compile_to_stream py api
adrianlizarraga 6d522d8
Python bindings and tests
adrianlizarraga af996bb
C# API for WriteBuffer delegate
adrianlizarraga 9b27b31
c# api handle initializers
adrianlizarraga 9607193
missing documentation in c#
adrianlizarraga e65710a
Add ExternalInitializerInfo C# class
adrianlizarraga c16b327
Full C# API for delegate that handles initializers
adrianlizarraga 0b2f0e6
Update comment
adrianlizarraga 83758d1
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga c62ed23
Address review comments
adrianlizarraga a35e7b6
Address review comments
adrianlizarraga d906855
Remove unused variable
adrianlizarraga 255c2df
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 3db3117
Merge main conflicts
adrianlizarraga c7f98de
Merge main again
adrianlizarraga 9031635
Address review comments for C#
adrianlizarraga abd0297
Rename functions in C and python
adrianlizarraga d5012fb
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga 0e0497a
Address comments
adrianlizarraga 0a61f1f
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
372 changes: 349 additions & 23 deletions
372
csharp/src/Microsoft.ML.OnnxRuntime/CompileModel.shared.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
csharp/src/Microsoft.ML.OnnxRuntime/OrtExternalInitializerInfo.shared.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
|
|
||
| namespace Microsoft.ML.OnnxRuntime | ||
| { | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| /// <summary> | ||
| /// Class to that stores information about the file location where an "external" initializer is stored. | ||
| /// </summary> | ||
| /// <see cref="OrtModelCompilationOptions.HandleInitializerDelegate"/> | ||
| public class OrtExternalInitializerInfo : SafeHandle, IReadOnlyExternalInitializerInfo | ||
| { | ||
| // Set to false when constructed with an externally managed constant handle owned by ORT. | ||
| private readonly bool _ownsHandle = true; | ||
|
|
||
| /// <summary> | ||
| /// Create a new OrtExternalInitializerInfo instance. | ||
| /// </summary> | ||
| /// <param name="filePath">The path to the file that stores the initializer data.</param> | ||
| /// <param name="fileOffset">The byte offset in the file where the data is stored.</param> | ||
| /// <param name="byteSize">The size of the data (in bytes) within the file.</param> | ||
| public OrtExternalInitializerInfo(string filePath, long fileOffset, long byteSize) | ||
| : base(IntPtr.Zero, ownsHandle: true) | ||
| { | ||
| var platformFilePath = NativeOnnxValueHelper.GetPlatformSerializedString(filePath); | ||
| NativeApiStatus.VerifySuccess( | ||
| NativeMethods.OrtCreateExternalInitializerInfo(platformFilePath, fileOffset, (UIntPtr)byteSize, out handle)); | ||
| _ownsHandle = true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a new OrtExternalInitializerInfo instance from an existing native OrtExternalInitializerInfo handle. | ||
| /// </summary> | ||
| /// <param name="constHandle">Native OrtExternalInitializerInfo handle.</param> | ||
| /// <param name="ownsHandle">True if the OrtExternalInitializerInfo instance owns the native handle. | ||
| /// Defaults to false.</param> | ||
| internal OrtExternalInitializerInfo(IntPtr constHandle, bool ownsHandle = false) | ||
| : base(IntPtr.Zero, ownsHandle) | ||
| { | ||
| Debug.Assert(constHandle != IntPtr.Zero); | ||
| SetHandle(constHandle); | ||
| _ownsHandle = ownsHandle; | ||
| } | ||
|
|
||
| /// <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> | ||
| public string GetFilePath() | ||
| { | ||
| IntPtr filePathPtr = NativeMethods.OrtExternalInitializerInfo_GetFilePath(handle); | ||
| if (filePathPtr == IntPtr.Zero) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| return NativeOnnxValueHelper.StringFromNativePathString(filePathPtr); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the byte offset within the file where the initializer's data is stored. | ||
| /// </summary> | ||
| /// <returns>The file offset location.</returns> | ||
| public long GetFileOffset() | ||
| { | ||
| return NativeMethods.OrtExternalInitializerInfo_GetFileOffset(handle); | ||
| } | ||
|
|
||
| /// <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> | ||
| public long GetByteSize() | ||
| { | ||
| UIntPtr byteSize = NativeMethods.OrtExternalInitializerInfo_GetByteSize(handle); | ||
| return checked((long)byteSize); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether the native handle is invalid. | ||
| /// </summary> | ||
| public override bool IsInvalid { get { return handle == IntPtr.Zero; } } | ||
|
|
||
| /// <summary> | ||
| /// Release the native instance of OrtExternalInitializerInfo if we own it. | ||
| /// </summary> | ||
| /// <returns>true on success and false on error.</returns> | ||
| protected override bool ReleaseHandle() | ||
| { | ||
| if (!_ownsHandle) | ||
| { | ||
| // Return false to indicate an error. | ||
| // ReleaseHandle() should not be called on a const handle that this class does not own. | ||
| return false; | ||
| } | ||
|
|
||
| NativeMethods.OrtReleaseExternalInitializerInfo(handle); | ||
| handle = IntPtr.Zero; | ||
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.