-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Support DisposableNamedOnnxValue inputs in c# Run() #3175
Changes from 9 commits
50704e4
bcd97a5
4f7f68f
d74e62a
de81b24
6f461d4
16a4d99
c0455ee
3b1bbc4
cd6ef29
6f2cc17
c7a7022
aba55c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using Microsoft.ML.OnnxRuntime.Tensors; | ||
using System.Runtime.InteropServices; | ||
|
@@ -60,13 +61,48 @@ public void Dispose() | |
|
||
public class DisposableNamedOnnxValue : NamedOnnxValue, IDisposable | ||
{ | ||
protected IDisposable _nativeMemoryManager; | ||
protected DisposableNamedOnnxValue(string name, Object value, IDisposable nativeMemoryManager) | ||
private NativeMemoryHandler _nativeMemoryManager; | ||
private DisposableNamedOnnxValue(string name, Object value, NativeMemoryHandler nativeMemoryManager) | ||
: base(name, value) | ||
{ | ||
_nativeMemoryManager = nativeMemoryManager; | ||
} | ||
|
||
/// <summary> | ||
/// Overrides the base class method. Since the instance already has access to the | ||
/// underlying OrtValue handle (if this instance hasn't been disposed), it just assigns | ||
/// that to the output onnxValue. With respect to pinnedMemoryHandle, it has no operation | ||
/// to do, as this class doesn't maintain a managed buffer. It doesn't have to maintain it | ||
/// as it already is associated with the object of interest (native OrtValue) | ||
/// </summary> | ||
/// <param name="onnxValue"></param> | ||
/// <param name="pinnedMemoryHandle"></param> | ||
/// <param name="disposeOnnxValueAfterUse"></param> | ||
internal override void ToNativeOnnxValue(out IntPtr onnxValue, | ||
out MemoryHandle pinnedMemoryHandle, | ||
out bool disposeOnnxValueAfterUse) | ||
{ | ||
// Make sure that this instance hasn't been disposed yet | ||
if (disposedValue) | ||
{ | ||
throw new Exception("This instance of DisposableNamedOnnxValue has already been disposed"); | ||
fs-eire marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should be a bug? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug in the user code ? Probably. But they get a user friendly error message this way... |
||
} | ||
|
||
// If not already disposed, _nativeMemoryManager can only be null | ||
// for Maps and SequenceTensors | ||
if (_nativeMemoryManager == null) | ||
{ | ||
throw new NotSupportedException("Use of Maps and SequenceTensors is not yet supported"); | ||
} | ||
|
||
// 'disposeOnnxValueAfterUse' is always 'false' for DisposableNamedOnnxValue as the onus | ||
// to dispose the onnxValue after use is on the user, not the internal caller | ||
disposeOnnxValueAfterUse = false; | ||
|
||
// Assign the onnxValue by querying this instance's NativeOnnxTensorMemory instance | ||
onnxValue = _nativeMemoryManager.GetOnnxValue(); | ||
} | ||
|
||
internal static DisposableNamedOnnxValue CreateTensorFromOnnxValue(string name, IntPtr nativeOnnxValue) | ||
{ | ||
DisposableNamedOnnxValue result = null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctor is private now as per @fs-eire 's suggestion