-
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
Enable TRT provider option configuration for C# (updated version) #7808
Merged
Merged
Changes from 42 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
7c2d4c6
prepare for C# to configure provider options
chilo-ms 1921ec4
add c# code
chilo-ms 9ec0f35
revert modification
chilo-ms 6ac01ca
Add update provider info configuration in trt ep side
chilo-ms de6cb01
fix bugs
chilo-ms c80a02a
fix bug for compiler error C2259
chilo-ms a2b8984
Add c# test
chilo-ms 681c319
fix bug
chilo-ms a67fcf3
fix bug
chilo-ms ace27d2
Properly deal with string
chilo-ms 12b7cdc
Add c# api for accepting trt provider options
chilo-ms a763a64
fix bug
chilo-ms 557724b
Merge branch 'c_sharp_trt_provider_options' of https://github.com/mic…
chilo-ms b628bf5
Modify C# test
chilo-ms 9677dfc
add shared lib test
chilo-ms 3e9d013
Add get provider options functionality
chilo-ms d8c18aa
clean up
chilo-ms 69d37e8
clean up
chilo-ms d40122b
fix bug
chilo-ms b456e6c
Merge branch 'master' into c_sharp_trt_provider_options
chilo-ms 2a87b40
fix bugs for CI
chilo-ms 99774ae
Fix bugs for CI and documentation
chilo-ms 9a0b07c
Move TRT EP provider options related functions out of C API
chilo-ms 96851de
revert
chilo-ms 30cc55c
fix bug
chilo-ms 2576645
refactor
chilo-ms 497550d
add check for provider options string
chilo-ms 748cb95
Merge branch 'master' into c_sharp_trt_provider_options
chilo-ms 1f815e4
code refactor
chilo-ms 0a173c4
fix CI bug
chilo-ms ae45fe8
Fix CI bugs
chilo-ms 1126559
clean up
chilo-ms e6953e8
fix bug
chilo-ms 5fff868
Fix bug for Post Analysis
chilo-ms 7a5f903
fix accidental bug
chilo-ms 5e3f600
Add API_IMPL_BEGIN/API_IMPL_END
chilo-ms 3ee5b20
clean up
chilo-ms b312090
code refactor
chilo-ms 1f6280e
code refactor
chilo-ms c48c9ba
Merge branch 'master' into c_sharp_trt_provider_options
chilo-ms 41a8dee
fix CI fail
chilo-ms 3649fa9
fix bug
chilo-ms 2ca2a19
use string append
chilo-ms a2456af
Change the code to better handle strncpy and string append
chilo-ms 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
This file contains 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 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 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,149 @@ | ||
// 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 int _deviceId = 0; | ||
private string _deviceIdStr = "device_id"; | ||
|
||
#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.OrtGetTensorRTProviderOptionsAsString(handle, allocator.Pointer, out providerOptions)); | ||
using (var ortAllocation = new OrtMemoryAllocation(allocator, providerOptions, 0)) | ||
{ | ||
return NativeOnnxValueHelper.StringFromNativeUtf8(providerOptions); | ||
} | ||
} | ||
|
||
/// <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)); | ||
|
||
if (providerOptions.ContainsKey(_deviceIdStr)) | ||
{ | ||
_deviceId = Int32.Parse(providerOptions[_deviceIdStr]); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get device id of TensorRT EP. | ||
/// </summary> | ||
/// <returns> device id </returns> | ||
public int GetDeviceId() | ||
{ | ||
return _deviceId; | ||
} | ||
|
||
#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> | ||
/// <param name="s">C# string</param> | ||
/// <param name="dict">Dictionary instance to store the parsing result of s</param> | ||
public static void StringToDict(string s, Dictionary<string, string> dict) | ||
{ | ||
string[] paris = s.Split(';'); | ||
|
||
foreach (var p in paris) | ||
{ | ||
string[] keyValue = p.Split('='); | ||
if (keyValue.Length != 2) | ||
{ | ||
throw new ArgumentException("Make sure input string contains key-value paris, e.g. key1=value1;key2=value2...", "s"); | ||
} | ||
dict.Add(keyValue[0], keyValue[1]); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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
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.
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().