-
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# #7179
Closed
Closed
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b882a42
Enable TensorRT EP for C#
chilo-ms fb79c1d
Add comment
chilo-ms 5328c03
Fix bug due to build fail
chilo-ms 5e6e233
Remove unnecessary code
chilo-ms 2074557
Fix bug for documentation check
chilo-ms f016bc4
Add test cases
chilo-ms d274fe3
restore some changes
chilo-ms 767083f
fix CI build bug
chilo-ms 7f3a544
expose all tensorrt env provider options
chilo-ms fb5b4a3
Merge branch 'master' into c_sharp_tensorrt
chilo-ms 9af54d0
fix typos
chilo-ms ee998f8
Fix bug
chilo-ms 58f2b2f
fix minor define issue
chilo-ms ba8b20e
modify trt ep constructor to take additional trt provider options
chilo-ms 478a81c
minor refine
chilo-ms 1cb12c5
refactor
chilo-ms 532c899
add documentation
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ public class SessionOptions : SafeHandle | |
{ | ||
// Delay-loaded CUDA or cuDNN DLLs. Currently, delayload is disabled. See cmake/CMakeLists.txt for more information. | ||
private static string[] cudaDelayLoadedLibs = { }; | ||
private static string[] trtDelayLoadedLibs = { }; | ||
|
||
#region Constructor and Factory methods | ||
|
||
|
@@ -75,6 +76,63 @@ public static SessionOptions MakeSessionOptionWithCudaProvider(int deviceId = 0) | |
return options; | ||
} | ||
|
||
/// <summary> | ||
/// A helper method to construct a SessionOptions object for TensorRT execution. | ||
/// Use only if CUDA/TensorRT are installed and you have the onnxruntime package specific to this Execution Provider. | ||
/// </summary> | ||
/// <param name="deviceId"></param> | ||
/// <returns>A SessionsOptions() object configured for execution on deviceId</returns> | ||
public static SessionOptions MakeSessionOptionWithTensorrtProvider(int deviceId = 0) | ||
{ | ||
CheckTensorrtExecutionProviderDLLs(); | ||
SessionOptions options = new SessionOptions(); | ||
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Tensorrt(options.Handle, deviceId)); | ||
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, deviceId)); | ||
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1)); | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// A helper method to construct a SessionOptions object for TensorRT execution. | ||
/// Use only if CUDA/TensorRT are installed and you have the onnxruntime package specific to this Execution Provider. | ||
/// </summary> | ||
/// <param name="trt_options">Provider Options for TensorRT EP.</param> | ||
/// <returns>A SessionsOptions() object configured for execution on deviceId</returns> | ||
/// | ||
public static SessionOptions MakeSessionOptionWithTensorrtProvider(OrtTensorRTProviderOptions trt_options) | ||
{ | ||
CheckTensorrtExecutionProviderDLLs(); | ||
SessionOptions options = new SessionOptions(); | ||
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. |
||
|
||
OrtTensorRTProviderOptionsNative trt_options_native; | ||
trt_options_native.device_id = trt_options.device_id; | ||
trt_options_native.has_user_compute_stream = 0; | ||
trt_options_native.user_compute_stream = IntPtr.Zero; | ||
trt_options_native.has_trt_options = trt_options.has_trt_options; | ||
if ((ulong)trt_options.trt_max_workspace_size > (1 << 30)) | ||
{ | ||
trt_options_native.trt_max_workspace_size = (UIntPtr)(1 << 30); | ||
} | ||
else | ||
{ | ||
trt_options_native.trt_max_workspace_size = trt_options.trt_max_workspace_size; | ||
} | ||
trt_options_native.trt_fp16_enable = trt_options.trt_fp16_enable; | ||
trt_options_native.trt_int8_enable = trt_options.trt_int8_enable; | ||
var tableNamePinned = GCHandle.Alloc(NativeOnnxValueHelper.StringToZeroTerminatedUtf8(trt_options.trt_int8_calibration_table_name), GCHandleType.Pinned); | ||
using (var pinnedSettingsName = new PinnedGCHandle(tableNamePinned)) | ||
{ | ||
trt_options_native.trt_int8_calibration_table_name = pinnedSettingsName.Pointer; | ||
} | ||
trt_options_native.trt_int8_use_native_calibration_table = trt_options.trt_int8_use_native_calibration_table; | ||
|
||
|
||
NativeApiStatus.VerifySuccess(NativeMethods.SessionOptionsAppendExecutionProvider_TensorRT(options.Handle, ref trt_options_native)); | ||
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, trt_options.device_id)); | ||
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1)); | ||
return options; | ||
} | ||
|
||
/// <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. | ||
|
@@ -592,6 +650,31 @@ public ExecutionMode ExecutionMode | |
} | ||
private ExecutionMode _executionMode = ExecutionMode.ORT_SEQUENTIAL; | ||
|
||
|
||
/// <summary> | ||
/// Provider options for TensorRT. | ||
/// </summary> | ||
/// | ||
// Example for setting: | ||
// SessionOptions.OrtTensorRTProviderOptions trt_options; | ||
// trt_options.device_id = 0; | ||
// trt_options.has_trt_options = 1; | ||
// trt_options.trt_max_workspace_size = (UIntPtr) (1<<30); | ||
// trt_options.trt_fp16_enable = 1; | ||
// trt_options.trt_int8_enable = 1; | ||
// trt_options.trt_int8_calibration_table_name = "C:\calibration.flatbuffers"; | ||
// trt_options.trt_int8_use_native_calibration_table = 0; | ||
public struct OrtTensorRTProviderOptions | ||
jywu-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public int device_id; // cuda device id. Default is 0. | ||
public int has_trt_options; // override environment variables with following TensorRT settings at runtime. Default 0 = false, nonzero = true. | ||
public UIntPtr trt_max_workspace_size; // maximum workspace size for TensorRT. ORT C++ DLL has this field to be the type of size_t, hence using UIntPtr for conversion. | ||
public int trt_fp16_enable; // enable TensorRT FP16 precision. Default 0 = false, nonzero = true. | ||
public int trt_int8_enable; // enable TensorRT INT8 precision. Default 0 = false, nonzero = true. | ||
public String trt_int8_calibration_table_name; // TensorRT INT8 calibration table name. | ||
public int trt_int8_use_native_calibration_table; // use native TensorRT generated calibration table. Default 0 = false, nonzero = true | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
|
@@ -624,6 +707,27 @@ private static bool CheckCudaExecutionProviderDLLs() | |
return true; | ||
} | ||
|
||
private static bool CheckTensorrtExecutionProviderDLLs() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
foreach (var dll in trtDelayLoadedLibs) | ||
{ | ||
IntPtr handle = LoadLibrary(dll); | ||
if (handle != IntPtr.Zero) | ||
continue; | ||
var sysdir = new StringBuilder(String.Empty, 2048); | ||
GetSystemDirectory(sysdir, (uint)sysdir.Capacity); | ||
throw new OnnxRuntimeException( | ||
ErrorCode.NoSuchFile, | ||
$"kernel32.LoadLibrary():'{dll}' not found. TensorRT/CUDA are required for GPU execution. " + | ||
$". Verify it is available in the system directory={sysdir}. Else copy it to the output folder." | ||
); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
|
||
#endregion | ||
#region SafeHandle | ||
|
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
OrtSessionOptionsAppendExecutionProvider_Tensorrt | ||
SessionOptionsAppendExecutionProvider_TensorRT |
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.
Same comment on handling leaking options when the below throws.