Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
a288310
Initial commit
hariharans29 Apr 20, 2021
d2c27d3
More changes
hariharans29 Apr 22, 2021
d22c8da
a
hariharans29 Apr 23, 2021
36aac2b
b
hariharans29 Apr 23, 2021
b13c7c2
c
hariharans29 Apr 23, 2021
b94ba94
fixes
hariharans29 Apr 23, 2021
1762e39
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 Apr 23, 2021
d698990
More fixes
hariharans29 Apr 23, 2021
00bd195
Fix build
hariharans29 Apr 23, 2021
a1570f3
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 Apr 26, 2021
10bece6
More changes
hariharans29 Apr 26, 2021
b8e0fbc
more changes
hariharans29 Apr 26, 2021
247a70e
fix builds
hariharans29 Apr 26, 2021
29f6d13
misc
hariharans29 Apr 26, 2021
8330062
misc
hariharans29 Apr 26, 2021
a6330b9
fix training build
hariharans29 Apr 26, 2021
3dac16f
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 Apr 27, 2021
36d6b70
PR feedback
hariharans29 Apr 27, 2021
59d3603
Add C# layer
hariharans29 Apr 27, 2021
5dbb82b
Add UT and logging
hariharans29 Apr 27, 2021
065ebab
Fix
hariharans29 Apr 27, 2021
5ff4f32
Refine
hariharans29 Apr 27, 2021
0f4c076
PR feedback
hariharans29 Apr 28, 2021
bcc64ca
Refine
hariharans29 Apr 28, 2021
9eb34ab
Refine
hariharans29 Apr 28, 2021
25eac96
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 Apr 28, 2021
a74151e
Add COnvTranspose
hariharans29 Apr 28, 2021
07aa1fa
PR feedback
hariharans29 Apr 28, 2021
0af41c3
More changes
hariharans29 Apr 28, 2021
7de7d2f
Fix training builds
hariharans29 Apr 28, 2021
3cce698
PR feedback
hariharans29 Apr 30, 2021
7dc54cd
Merge master
hariharans29 Apr 30, 2021
cfb04bd
refine
hariharans29 Apr 30, 2021
093b963
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 Apr 30, 2021
875a30d
refine
hariharans29 Apr 30, 2021
9b1a049
refine
hariharans29 Apr 30, 2021
1b6d396
Fix warning
hariharans29 Apr 30, 2021
6f0d646
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 May 6, 2021
2934c53
More changes
hariharans29 May 7, 2021
cd68f59
Fix comment
hariharans29 May 7, 2021
639214d
Refine
hariharans29 May 7, 2021
0803537
Fix build
hariharans29 May 7, 2021
98af700
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 May 7, 2021
8bc4ced
More tests
hariharans29 May 10, 2021
85e5713
Merge master and resolve conflicts
hariharans29 May 10, 2021
908fb3f
Fixes
hariharans29 May 10, 2021
6d9bb17
More changes
hariharans29 May 10, 2021
411ebab
Fix test
hariharans29 May 10, 2021
2a15490
More changes
hariharans29 May 10, 2021
839e6b5
Trial
hariharans29 May 10, 2021
2ea9008
Fixes
hariharans29 May 10, 2021
98c540e
More fixes
hariharans29 May 11, 2021
c9a733e
PR feedback
hariharans29 May 13, 2021
5ca1d2d
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 May 13, 2021
68867d8
Fix warning
hariharans29 May 13, 2021
6460aeb
Fix typos
hariharans29 May 13, 2021
42e7aee
More fixes
hariharans29 May 13, 2021
0f8c73c
PR feddback
hariharans29 May 14, 2021
146a8b9
Merge master and resolve conflicts
hariharans29 May 14, 2021
8cae9b5
Nit
hariharans29 May 14, 2021
c4257f7
PR feedback
hariharans29 May 14, 2021
de79b57
Merge remote-tracking branch 'origin/master' into hari/prepacking_shared
hariharans29 May 14, 2021
fff1236
Resolve conflict
hariharans29 May 15, 2021
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
135 changes: 112 additions & 23 deletions csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public InferenceSession(string modelPath)
Init(modelPath, _builtInSessionOptions);
}

/// <summary>
/// Constructs an InferenceSession from a model file and it will use
/// the provided pre-packed weights container to store and share pre-packed buffers
/// of shared initializers across sessions if any.
/// </summary>
/// <param name="modelPath">Model path</param>
/// <param name="prepackedWeightsContainer">Instance of PrepackedWeightsContainer.
/// Lifetime of 'prepackedWeightsContainer' must be
/// managed by the user and it must outlive any sessions reliant on it</param>
public InferenceSession(string modelPath, PrePackedWeightsContainer prepackedWeightsContainer)
{
_builtInSessionOptions = new SessionOptions(); // need to be disposed
Init(modelPath, _builtInSessionOptions, prepackedWeightsContainer);
}


/// <summary>
/// Constructs an InferenceSession from a model file, with some additional session options
Expand All @@ -58,6 +73,23 @@ public InferenceSession(string modelPath, SessionOptions options)
Init(modelPath, options);
}


/// <summary>
/// Constructs an InferenceSession from a model file, with some additional session options
/// and it will use the provided pre-packed weights container to store and share pre-packed buffers
/// of shared initializers across sessions if any.
/// </summary>
/// <param name="modelPath">Model path</param>
/// <param name="options">Session options</param>
/// <param name="prepackedWeightsContainer">Instance of PrepackedWeightsContainer.
/// Lifetime of 'prepackedWeightsContainer' must be
/// managed by the user and it must outlive any sessions reliant on it</param>
public InferenceSession(string modelPath, SessionOptions options,
PrePackedWeightsContainer prepackedWeightsContainer)
{
Init(modelPath, options, prepackedWeightsContainer);
}

/// <summary>
/// Constructs an InferenceSession from a model data in byte array
/// </summary>
Expand All @@ -68,6 +100,21 @@ public InferenceSession(byte[] model)
Init(model, _builtInSessionOptions);
}

/// <summary>
/// Constructs an InferenceSession from a model data (in byte array) and it will use
/// the provided pre-packed weights container to store and share pre-packed buffers
/// of shared initializers across sessions if any.
/// </summary>
/// <param name="model">Model as byte array</param>
/// <param name="prepackedWeightsContainer">Instance of PrepackedWeightsContainer.
/// Lifetime of 'prepackedWeightsContainer' must be
/// managed by the user and it must outlive any sessions reliant on it</param>
public InferenceSession(byte[] model, PrePackedWeightsContainer prepackedWeightsContainer)
{
_builtInSessionOptions = new SessionOptions(); // need to be disposed
Init(model, _builtInSessionOptions, prepackedWeightsContainer);
}

/// <summary>
/// Constructs an InferenceSession from a model data in byte array, with some additional session options
/// </summary>
Expand All @@ -78,6 +125,21 @@ public InferenceSession(byte[] model, SessionOptions options)
Init(model, options);
}

/// <summary>
/// Constructs an InferenceSession from a model data (in byte array) with some additional
/// session options and it will use the provided pre-packed weights container to store
/// and share pre-packed buffers of shared initializers across sessions if any.
/// </summary>
/// <param name="model">Model as byte array</param>
/// <param name="options">Session Options</param>
/// <param name="prepackedWeightsContainer">Instance of PrepackedWeightsContainer.
/// Lifetime of 'prepackedWeightsContainer' must be
/// managed by the user and it must outlive any sessions reliant on it</param>
public InferenceSession(byte[] model, SessionOptions options,
PrePackedWeightsContainer prepackedWeightsContainer)
{
Init(model, options, prepackedWeightsContainer);
}
/// <summary>
/// Meta data regarding the input nodes, keyed by input names
/// </summary>
Expand Down Expand Up @@ -310,7 +372,7 @@ public void Run(
IReadOnlyCollection<NamedOnnxValue> outputs,
RunOptions options)
{
using(var cleanupList = new DisposableList<IDisposable>())
using (var cleanupList = new DisposableList<IDisposable>())
{
var inputNamesArray = ConvertNamesToUtf8(inputs, i => i.Name, cleanupList);
var inputValuesArray = GetOrtValuesHandles(inputs, cleanupList);
Expand Down Expand Up @@ -367,7 +429,7 @@ public void Run(
throw new ArgumentException($"Length of {nameof(outputNames)} ({outputNames.Count}) must match that of {nameof(outputValues)} ({outputValues.Count}).");
}

using(var cleanupList = new DisposableList<IDisposable>())
using (var cleanupList = new DisposableList<IDisposable>())
{
// prepare inputs
var inputNamesArray = ConvertNamesToUtf8(inputs, i => i.Name, cleanupList);
Expand Down Expand Up @@ -428,7 +490,7 @@ public void Run(
throw new ArgumentException($"Length of {nameof(inputNames)} ({inputNames.Count}) must match that of {nameof(inputValues)} ({inputValues.Count}).");
}

using(var cleanupList = new DisposableList<IDisposable>())
using (var cleanupList = new DisposableList<IDisposable>())
{
// prepare inputs
var inputNamesArray = ConvertNamesToUtf8(inputNames, n => n, cleanupList);
Expand Down Expand Up @@ -515,7 +577,8 @@ public IDisposableReadOnlyCollection<DisposableNamedOnnxValue> RunWithBindingAnd
var ortValue = ortValues.ElementAt(i);
result.Add(DisposableNamedOnnxValue.CreateFromOrtValue(outputNames[i], ortValue));
}
} catch(Exception e)
}
catch (Exception e)
{
result.Dispose();
throw e;
Expand All @@ -535,7 +598,7 @@ public string EndProfiling()
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionEndProfiling(_nativeHandle,
allocator.Pointer,
out nameHandle));
using(var allocation = new OrtMemoryAllocation(allocator, nameHandle, 0))
using (var allocation = new OrtMemoryAllocation(allocator, nameHandle, 0))
{
return NativeOnnxValueHelper.StringFromNativeUtf8(nameHandle);
}
Expand Down Expand Up @@ -609,8 +672,8 @@ private IntPtr[] GetOrtValuesHandles(IReadOnlyCollection<FixedBufferOnnxValue> v
}


private DisposableList<OrtValue> RunImpl(RunOptions options, IntPtr[] inputNames, IntPtr[] inputValues, IntPtr[] outputNames,
DisposableList<IDisposable> cleanupList)
private DisposableList<OrtValue> RunImpl(RunOptions options, IntPtr[] inputNames, IntPtr[] inputValues, IntPtr[] outputNames,
DisposableList<IDisposable> cleanupList)
{
var ortValues = new DisposableList<OrtValue>(outputNames.Length);
cleanupList.Add(ortValues);
Expand Down Expand Up @@ -680,31 +743,57 @@ public ModelMetadata ModelMetadata
/// </summary>
public ulong ProfilingStartTimeNs
{
get
{
return _profilingStartTimeNs;
}
}
get
{
return _profilingStartTimeNs;
}
}

#endregion

#region private methods

private void Init(string modelPath, SessionOptions options)
private void Init(string modelPath, SessionOptions options,
PrePackedWeightsContainer prepackedWeightsContainer = null)
{
var envHandle = OrtEnv.Handle;
var session = IntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSession(envHandle, NativeMethods.GetPlatformSerializedString(modelPath), options.Handle, out session));

if (prepackedWeightsContainer == null)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSession(envHandle, NativeMethods.GetPlatformSerializedString(modelPath),
options.Handle, out session));
}

else
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSessionWithPrepackedWeightsContainer(
envHandle, NativeMethods.GetPlatformSerializedString(modelPath),
options.Handle, prepackedWeightsContainer.Pointer, out session));
}

InitWithSessionHandle(session, options);
}

private void Init(byte[] modelData, SessionOptions options)
private void Init(byte[] modelData, SessionOptions options,
PrePackedWeightsContainer prepackedWeightsContainer = null)
{
var envHandle = OrtEnv.Handle;
var session = IntPtr.Zero;

NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSessionFromArray(envHandle, modelData, (UIntPtr)modelData.Length, options.Handle, out session));
if (prepackedWeightsContainer == null)
{

NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSessionFromArray(envHandle, modelData, (UIntPtr)modelData.Length, options.Handle, out session));
}

else
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSessionFromArrayWithPrepackedWeightsContainer(
envHandle, modelData, (UIntPtr)modelData.Length, options.Handle, prepackedWeightsContainer.Pointer,
out session));

}

InitWithSessionHandle(session, options);
}
Expand Down Expand Up @@ -757,8 +846,8 @@ private void InitWithSessionHandle(IntPtr session, SessionOptions options)
// set profiling's start time
UIntPtr startTime = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionGetProfilingStartTimeNs(_nativeHandle,
out startTime));
_profilingStartTimeNs = (ulong) startTime;
out startTime));
_profilingStartTimeNs = (ulong)startTime;
}
catch (OnnxRuntimeException e)
{
Expand Down Expand Up @@ -821,7 +910,7 @@ private string GetOverridableInitializerName(ulong index)
(UIntPtr)index,
allocator.Pointer,
out nameHandle));
using(var ortAllocation = new OrtMemoryAllocation(allocator, nameHandle, 0))
using (var ortAllocation = new OrtMemoryAllocation(allocator, nameHandle, 0))
{
str = NativeOnnxValueHelper.StringFromNativeUtf8(nameHandle);
}
Expand Down Expand Up @@ -963,7 +1052,7 @@ public void Dispose()
/// <param name="disposing">true if invoked from Dispose() method</param>
protected virtual void Dispose(bool disposing)
{
if(_disposed)
if (_disposed)
{
return;
}
Expand Down Expand Up @@ -1137,7 +1226,7 @@ internal ModelMetadata(InferenceSession session)
}

// Process each key via the stored key handles
foreach(var allocation in ortAllocationKeys)
foreach (var allocation in ortAllocationKeys)
{
IntPtr keyHandle = allocation.Pointer;
IntPtr valueHandle = IntPtr.Zero;
Expand All @@ -1160,9 +1249,9 @@ internal ModelMetadata(InferenceSession session)
{

// Free ModelMetadata handle
NativeMethods.OrtReleaseModelMetadata(modelMetadataHandle);
NativeMethods.OrtReleaseModelMetadata(modelMetadataHandle);

}
}

}

Expand Down
Loading