-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Enable TRT provider option configuration for C# (updated version) #7808
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
Changes from 20 commits
7c2d4c6
1921ec4
9ec0f35
6ac01ca
de6cb01
c80a02a
a2b8984
681c319
a67fcf3
ace27d2
12b7cdc
a763a64
557724b
b628bf5
9677dfc
3e9d013
d8c18aa
69d37e8
d40122b
b456e6c
2a87b40
99774ae
9a0b07c
96851de
30cc55c
2576645
497550d
748cb95
1f815e4
0a173c4
ae45fe8
1126559
e6953e8
5fff868
7a5f903
5e3f600
3ee5b20
b312090
1f6280e
c48c9ba
41a8dee
3649fa9
2ca2a19
a2456af
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 |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Microsoft.ML.OnnxRuntime | ||
| { | ||
| /// <summary> | ||
| /// Holds the options for configuring a TensorRT Execution Provider instance | ||
| /// </summary> | ||
| public class OrtTensorRTProviderOptions : SafeHandle | ||
| { | ||
| internal IntPtr Handle | ||
| { | ||
| get | ||
| { | ||
| return handle; | ||
| } | ||
| } | ||
|
|
||
| //private string _options; | ||
|
|
||
| #region Constructor | ||
|
|
||
| /// <summary> | ||
| /// Constructs an empty OrtTensorRTProviderOptions instance | ||
| /// </summary> | ||
| public OrtTensorRTProviderOptions() : base(IntPtr.Zero, true) | ||
| { | ||
| NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateTensorRTProviderOptions(out handle)); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Public Methods | ||
|
|
||
| /// <summary> | ||
| /// Get TensorRT EP provider options | ||
| /// </summary> | ||
| /// <returns> return C# UTF-16 encoded string </returns> | ||
| public string GetOptions() | ||
| { | ||
| var allocator = OrtAllocator.DefaultInstance; | ||
|
|
||
| // Process provider options string | ||
| IntPtr providerOptions = IntPtr.Zero; | ||
| NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorRTProviderOptions(allocator.Pointer, out providerOptions)); | ||
| using (var ortAllocation = new OrtMemoryAllocation(allocator, providerOptions, 0)) | ||
| { | ||
| return NativeOnnxValueHelper.StringFromNativeUtf8(providerOptions); | ||
| } | ||
|
|
||
| //return _options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates the configuration knobs of OrtTensorRTProviderOptions that will eventually be used to configure a TensorRT EP | ||
| /// Please refer to the following on different key/value pairs to configure a TensorRT EP and their meaning: | ||
| /// https://www.onnxruntime.ai/docs/reference/execution-providers/TensorRT-ExecutionProvider.html | ||
| /// </summary> | ||
| /// <param name="providerOptions">key/value pairs used to configure a TensorRT Execution Provider</param> | ||
| public void UpdateOptions(Dictionary<string, string> providerOptions) | ||
| { | ||
|
|
||
| using (var cleanupList = new DisposableList<IDisposable>()) | ||
| { | ||
| var keysArray = NativeOnnxValueHelper.ConvertNamesToUtf8(providerOptions.Keys.ToArray(), n => n, cleanupList); | ||
| var valuesArray = NativeOnnxValueHelper.ConvertNamesToUtf8(providerOptions.Values.ToArray(), n => n, cleanupList); | ||
|
|
||
| NativeApiStatus.VerifySuccess(NativeMethods.OrtUpdateTensorRTProviderOptions(handle, keysArray, valuesArray, (UIntPtr)providerOptions.Count)); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Public Properties | ||
|
|
||
| /// <summary> | ||
| /// Overrides SafeHandle.IsInvalid | ||
| /// </summary> | ||
| /// <value>returns true if handle is equal to Zero</value> | ||
| public override bool IsInvalid { get { return handle == IntPtr.Zero; } } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Private Methods | ||
|
|
||
|
|
||
| #endregion | ||
|
|
||
| #region SafeHandle | ||
| /// <summary> | ||
| /// Overrides SafeHandle.ReleaseHandle() to properly dispose of | ||
| /// the native instance of OrtTensorRTProviderOptions | ||
| /// </summary> | ||
| /// <returns>always returns true</returns> | ||
| protected override bool ReleaseHandle() | ||
| { | ||
| NativeMethods.OrtReleaseTensorRTProviderOptions(handle); | ||
| handle = IntPtr.Zero; | ||
| return true; | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This helper class contains methods to handle values of provider options | ||
| /// </summary> | ||
| public class ProviderOptionsValueHelper | ||
| { | ||
| /// <summary> | ||
| /// Parse from string and save to dictionary | ||
| /// </summary> | ||
| public static void StringToDict(string s, Dictionary<string, string> dict) | ||
| { | ||
| string[] paris = s.Split(';'); | ||
|
|
||
| foreach (var p in paris) | ||
| { | ||
| string[] keyValue = p.Split('='); | ||
| dict.Add(keyValue[0], keyValue[1]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.ML.OnnxRuntime | ||
| { | ||
|
|
@@ -100,6 +101,38 @@ public static SessionOptions MakeSessionOptionWithTensorrtProvider(int deviceId | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A helper method to construct a SessionOptions object for TensorRT execution provider. | ||
| /// Use only if CUDA/TensorRT are installed and you have the onnxruntime package specific to this Execution Provider. | ||
| /// </summary> | ||
| /// <param name="trtProviderOptions">TensorRT EP provider options</param> | ||
| /// <returns>A SessionsOptions() object configured for execution on provider options</returns> | ||
| public static SessionOptions MakeSessionOptionWithTensorrtProvider(OrtTensorRTProviderOptions trtProviderOptions) | ||
| { | ||
| CheckTensorrtExecutionProviderDLLs(); | ||
| SessionOptions options = new SessionOptions(); | ||
| try | ||
| { | ||
| // get device id for configuring CUDA EP | ||
| int deviceId; | ||
| string optionsStr; | ||
| var dict = new Dictionary<string, string>(); | ||
| optionsStr = trtProviderOptions.GetOptions(); | ||
| ProviderOptionsValueHelper.StringToDict(optionsStr, dict); | ||
| deviceId = Int32.Parse(dict["device_id"]); | ||
|
Contributor
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. The usage seems to be a little convoluted.
Contributor
Author
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. fixed.
Contributor
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.
Contributor
Author
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. After code refactor, this part is removed. |
||
|
|
||
| NativeApiStatus.VerifySuccess(NativeMethods.SessionOptionsAppendExecutionProvider_TensorRT(options.Handle, trtProviderOptions.Handle)); | ||
| NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, deviceId)); | ||
| NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1)); | ||
| return options; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| options.Dispose(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A helper method to construct a SessionOptions object for Nuphar execution. | ||
| /// Use only if you have the onnxruntime package specific to this Execution Provider. | ||
|
|
@@ -205,6 +238,16 @@ public void AppendExecutionProvider_Tensorrt(int deviceId) | |
| NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Tensorrt(handle, deviceId)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Append a TensorRT EP instance (based on specified configuration) to the SessionOptions instance. | ||
| /// Use only if you have the onnxruntime package specific to this Execution Provider. | ||
| /// </summary> | ||
| /// <param name="trtProviderOptions">TensorRT EP provider options</param> | ||
| public void AppendExecutionProvider_Tensorrt(OrtTensorRTProviderOptions trtProviderOptions) | ||
| { | ||
| NativeApiStatus.VerifySuccess(NativeMethods.SessionOptionsAppendExecutionProvider_TensorRT(handle, trtProviderOptions.Handle)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Use only if you have the onnxruntime package specific to this Execution Provider. | ||
| /// </summary> | ||
|
|
||
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.
Need to check that the array after split contains 2 elements, throw with a meaningful message, otherwise it would be some generic OutOfBounds().