11using Microsoft . ML . OnnxRuntime ;
22using OnnxStack . Core . Config ;
33using System ;
4+ using System . Collections . Concurrent ;
45using System . Collections . Generic ;
56using System . Linq ;
7+ using System . Xml . Linq ;
68
79namespace OnnxStack . Core
810{
@@ -12,16 +14,16 @@ public static SessionOptions GetSessionOptions(this OnnxModelSessionConfig confi
1214 {
1315 var sessionOptions = new SessionOptions
1416 {
15- ExecutionMode = configuration . ExecutionMode ,
16- InterOpNumThreads = configuration . InterOpNumThreads ,
17- IntraOpNumThreads = configuration . InterOpNumThreads
17+ ExecutionMode = configuration . ExecutionMode . Value ,
18+ InterOpNumThreads = configuration . InterOpNumThreads . Value ,
19+ IntraOpNumThreads = configuration . IntraOpNumThreads . Value
1820 } ;
1921 switch ( configuration . ExecutionProvider )
2022 {
2123 case ExecutionProvider . DirectML :
2224 sessionOptions . GraphOptimizationLevel = GraphOptimizationLevel . ORT_ENABLE_ALL ;
2325 sessionOptions . EnableMemoryPattern = false ;
24- sessionOptions . AppendExecutionProvider_DML ( configuration . DeviceId ) ;
26+ sessionOptions . AppendExecutionProvider_DML ( configuration . DeviceId . Value ) ;
2527 sessionOptions . AppendExecutionProvider_CPU ( ) ;
2628 return sessionOptions ;
2729 case ExecutionProvider . Cpu :
@@ -30,7 +32,7 @@ public static SessionOptions GetSessionOptions(this OnnxModelSessionConfig confi
3032 default :
3133 case ExecutionProvider . Cuda :
3234 sessionOptions . GraphOptimizationLevel = GraphOptimizationLevel . ORT_ENABLE_ALL ;
33- sessionOptions . AppendExecutionProvider_CUDA ( configuration . DeviceId ) ;
35+ sessionOptions . AppendExecutionProvider_CUDA ( configuration . DeviceId . Value ) ;
3436 sessionOptions . AppendExecutionProvider_CPU ( ) ;
3537 return sessionOptions ;
3638 case ExecutionProvider . CoreML :
@@ -41,6 +43,25 @@ public static SessionOptions GetSessionOptions(this OnnxModelSessionConfig confi
4143 }
4244 }
4345
46+ /// <summary>
47+ /// Applies the configuration overrides.
48+ /// </summary>
49+ public static void ApplyConfigurationOverrides ( this IOnnxModelSetConfig innxModelSetConfig )
50+ {
51+ if ( innxModelSetConfig . ModelConfigurations . IsNullOrEmpty ( ) )
52+ return ;
53+
54+ foreach ( var modelConfig in innxModelSetConfig . ModelConfigurations )
55+ {
56+ modelConfig . IsEnabled = modelConfig . IsEnabled != false ;
57+ modelConfig . DeviceId ??= innxModelSetConfig . DeviceId ;
58+ modelConfig . ExecutionMode ??= innxModelSetConfig . ExecutionMode ;
59+ modelConfig . InterOpNumThreads ??= innxModelSetConfig . InterOpNumThreads ;
60+ modelConfig . IntraOpNumThreads ??= innxModelSetConfig . IntraOpNumThreads ;
61+ modelConfig . ExecutionProvider ??= innxModelSetConfig . ExecutionProvider ;
62+ }
63+ }
64+
4465 /// <summary>
4566 /// Determines whether the the source sequence is null or empty
4667 /// </summary>
@@ -139,5 +160,16 @@ public static int IndexOf<T>(this IReadOnlyList<T> list, T item) where T : IEqua
139160 }
140161
141162
163+ /// <summary>
164+ /// Converts to source IEnumerable to a ConcurrentDictionary.
165+ /// </summary>
166+ /// <param name="source">The source.</param>
167+ /// <param name="keySelector">The key selector.</param>
168+ /// <param name="elementSelector">The element selector.</param>
169+ /// <returns></returns>
170+ public static ConcurrentDictionary < T , U > ToConcurrentDictionary < S , T , U > ( this IEnumerable < S > source , Func < S , T > keySelector , Func < S , U > elementSelector ) where T : notnull
171+ {
172+ return new ConcurrentDictionary < T , U > ( source . ToDictionary ( keySelector , elementSelector ) ) ;
173+ }
142174 }
143175}
0 commit comments