|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.ML.Data; |
| 9 | +using Microsoft.ML.OnnxRuntime; |
| 10 | +using Microsoft.ML.Runtime; |
| 11 | +using Microsoft.ML.Transforms.Onnx; |
| 12 | + |
| 13 | +namespace Microsoft.ML.Transforms.Onnx |
| 14 | +{ |
| 15 | + public static class OnnxSessionOptionsExtensions |
| 16 | + { |
| 17 | + private const string OnnxSessionOptionsName = "OnnxSessionOptions"; |
| 18 | + |
| 19 | + public static OnnxSessionOptions GetOnnxSessionOption(this IHostEnvironment env) |
| 20 | + { |
| 21 | + if (env is IHostEnvironmentInternal localEnvironment) |
| 22 | + { |
| 23 | + return localEnvironment.GetOptionOrDefault<OnnxSessionOptions>(OnnxSessionOptionsName); |
| 24 | + } |
| 25 | + |
| 26 | + throw new ArgumentException("No Onnx Session Options"); |
| 27 | + } |
| 28 | + |
| 29 | + public static void SetOnnxSessionOption(this IHostEnvironment env, OnnxSessionOptions onnxSessionOptions) |
| 30 | + { |
| 31 | + if (env is IHostEnvironmentInternal localEnvironment) |
| 32 | + { |
| 33 | + localEnvironment.SetOption(OnnxSessionOptionsName, onnxSessionOptions); |
| 34 | + } |
| 35 | + else |
| 36 | + throw new ArgumentException("No Onnx Session Options"); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public sealed class OnnxSessionOptions |
| 41 | + { |
| 42 | + internal void CopyTo(SessionOptions sessionOptions) |
| 43 | + { |
| 44 | + sessionOptions.EnableMemoryPattern = EnableMemoryPattern; |
| 45 | + sessionOptions.ProfileOutputPathPrefix = ProfileOutputPathPrefix; |
| 46 | + sessionOptions.EnableProfiling = EnableProfiling; |
| 47 | + sessionOptions.OptimizedModelFilePath = OptimizedModelFilePath; |
| 48 | + sessionOptions.EnableCpuMemArena = EnableCpuMemArena; |
| 49 | + if (!PerSessionThreads) |
| 50 | + sessionOptions.DisablePerSessionThreads(); |
| 51 | + sessionOptions.LogId = LogId; |
| 52 | + sessionOptions.LogSeverityLevel = LogSeverityLevel; |
| 53 | + sessionOptions.LogVerbosityLevel = LogVerbosityLevel; |
| 54 | + sessionOptions.InterOpNumThreads = InterOpNumThreads; |
| 55 | + sessionOptions.IntraOpNumThreads = IntraOpNumThreads; |
| 56 | + sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel; |
| 57 | + sessionOptions.ExecutionMode = ExecutionMode; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Enables the use of the memory allocation patterns in the first Run() call for subsequent runs. Default = true. |
| 62 | + /// </summary> |
| 63 | +#pragma warning disable MSML_NoInstanceInitializers // No initializers on instance fields or properties |
| 64 | + public bool EnableMemoryPattern { get; set; } = true; |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Path prefix to use for output of profiling data |
| 68 | + /// </summary> |
| 69 | + public string ProfileOutputPathPrefix { get; set; } = "onnxruntime_profile_"; // this is the same default in C++ implementation |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Enables profiling of InferenceSession.Run() calls. Default is false |
| 73 | + /// </summary> |
| 74 | + public bool EnableProfiling { get; set; } = false; |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Set filepath to save optimized model after graph level transformations. Default is empty, which implies saving is disabled. |
| 78 | + /// </summary> |
| 79 | + public string OptimizedModelFilePath { get; set; } = string.Empty; |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Enables Arena allocator for the CPU memory allocations. Default is true. |
| 83 | + /// </summary> |
| 84 | + public bool EnableCpuMemArena { get; set; } = true; |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Per session threads. Default is true. |
| 88 | + /// If false this makes all sessions in the process use a global TP. |
| 89 | + /// </summary> |
| 90 | + public bool PerSessionThreads { get; set; } = true; |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Sets the number of threads used to parallelize the execution within nodes |
| 94 | + /// A value of 0 means ORT will pick a default. Only used when <see cref="PerSessionThreads"/> is false. |
| 95 | + /// </summary> |
| 96 | + public int GlobalIntraOpNumThreads { get; set; } = 0; |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Sets the number of threads used to parallelize the execution of the graph (across nodes) |
| 100 | + /// If sequential execution is enabled this value is ignored |
| 101 | + /// A value of 0 means ORT will pick a default. Only used when <see cref="PerSessionThreads"/> is false. |
| 102 | + /// </summary> |
| 103 | + public int GlobalInterOpNumThreads { get; set; } = 0; |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Log Id to be used for the session. Default is empty string. |
| 107 | + /// </summary> |
| 108 | + public string LogId { get; set; } = string.Empty; |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Log Severity Level for the session logs. Default = ORT_LOGGING_LEVEL_WARNING |
| 112 | + /// </summary> |
| 113 | + public OrtLoggingLevel LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING; |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Log Verbosity Level for the session logs. Default = 0. Valid values are >=0. |
| 117 | + /// This takes into effect only when the LogSeverityLevel is set to ORT_LOGGING_LEVEL_VERBOSE. |
| 118 | + /// </summary> |
| 119 | + public int LogVerbosityLevel { get; set; } = 0; |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Sets the number of threads used to parallelize the execution within nodes |
| 123 | + /// A value of 0 means ORT will pick a default |
| 124 | + /// </summary> |
| 125 | + public int IntraOpNumThreads { get; set; } = 0; |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Sets the number of threads used to parallelize the execution of the graph (across nodes) |
| 129 | + /// If sequential execution is enabled this value is ignored |
| 130 | + /// A value of 0 means ORT will pick a default |
| 131 | + /// </summary> |
| 132 | + public int InterOpNumThreads { get; set; } = 0; |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Sets the graph optimization level for the session. Default is set to ORT_ENABLE_ALL. |
| 136 | + /// </summary> |
| 137 | + public GraphOptimizationLevel GraphOptimizationLevel { get; set; } = GraphOptimizationLevel.ORT_ENABLE_ALL; |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Sets the execution mode for the session. Default is set to ORT_SEQUENTIAL. |
| 141 | + /// See [ONNX_Runtime_Perf_Tuning.md] for more details. |
| 142 | + /// </summary> |
| 143 | + public ExecutionMode ExecutionMode { get; set; } = ExecutionMode.ORT_SEQUENTIAL; |
| 144 | +#pragma warning restore MSML_NoInstanceInitializers // No initializers on instance fields or properties |
| 145 | + |
| 146 | + public delegate SessionOptions CreateOnnxSessionOptions(); |
| 147 | + |
| 148 | + public CreateOnnxSessionOptions CreateSessionOptions { get; set; } |
| 149 | + } |
| 150 | +} |
0 commit comments