-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathModelSession.cs
More file actions
277 lines (234 loc) · 9.29 KB
/
ModelSession.cs
File metadata and controls
277 lines (234 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using Microsoft.ML.OnnxRuntime;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using TensorStack.Common;
namespace TensorStack.Common
{
/// <summary>
/// ModelSession class to manage lifetime of an InferenceSession ans its SessionOptions.
/// </summary>
/// <typeparam name="T">ModelConfig implementation</typeparam>
/// <seealso cref="IDisposable" />
public class ModelSession<T> : IDisposable where T : ModelConfig
{
private readonly T _configuration;
private ModelMetadata _metadata;
private InferenceSession _session;
private SessionOptions _options;
private OrtAllocator _allocator;
private ModelOptimization _optimizations;
/// <summary>
/// Initializes a new instance of the <see cref="ModelSession"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public ModelSession(T configuration)
{
ArgumentNullException.ThrowIfNull(configuration.ExecutionProvider);
if (!File.Exists(configuration.Path))
throw new FileNotFoundException("Onnx model file not found, Path: {Path}", configuration.Path);
_configuration = configuration;
}
/// <summary>
/// Gets the metadata.
/// </summary>
public ModelMetadata Metadata => _metadata;
/// <summary>
/// Gets the SessionOptions.
/// </summary>
public SessionOptions Options => _options;
/// <summary>
/// Gets the InferenceSession.
/// </summary>
public T Configuration => _configuration;
/// <summary>
/// Gets the InferenceSession.
/// </summary>
public InferenceSession Session => _session;
/// <summary>
/// Gets the allocator.
/// </summary>
public OrtAllocator Allocator => _allocator;
/// <summary>
/// Loads the model session.
/// </summary>
/// <param name="optimizations">The optimizations.</param>
/// <returns>ModelMetadata.</returns>
public async Task<ModelMetadata> LoadAsync(ModelOptimization optimizations = default, CancellationToken cancellationToken = default)
{
try
{
if (_session is null)
return await CreateSession(optimizations, cancellationToken);
if (HasOptimizationsChanged(optimizations))
{
await UnloadAsync();
return await CreateSession(optimizations, cancellationToken);
}
return _metadata;
}
catch (OnnxRuntimeException ex)
{
if (ex.Message.Contains("ErrorCode:RequirementNotRegistered"))
throw new OperationCanceledException("Inference was canceled.", ex);
throw;
}
}
/// <summary>
/// Unloads the model session.
/// </summary>
/// <returns></returns>
public async Task UnloadAsync()
{
// TODO: deadlock on model dispose when no synchronization context exists(console app)
// Task.Yield seems to force a context switch resolving any issues, revist this
await Task.Yield();
if (_session is not null)
{
_session.Dispose();
_metadata = null;
_session = null;
}
}
/// <summary>
/// Runs inference on the model with the suppied parameters, use this method when you do not have a known output shape.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public virtual IDisposableReadOnlyCollection<OrtValue> RunInference(ModelParameters parameters)
{
try
{
return _session.Run(parameters.RunOptions, parameters.InputNameValues, parameters.OutputNames);
}
catch (OnnxRuntimeException ex)
{
if (ex.Message.Contains("Exiting due to terminate flag"))
throw new OperationCanceledException("Inference was canceled.", ex);
throw;
}
}
/// <summary>
/// Runs inference on the model with the suppied parameters, use this method when the output shape is known
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public virtual async Task<IDisposableReadOnlyCollection<OrtValue>> RunInferenceAsync(ModelParameters parameters)
{
try
{
return new DisposableList<OrtValue>(await _session.RunAsync(parameters.RunOptions, parameters.InputNames, parameters.InputValues, parameters.OutputNames, parameters.OutputValues));
}
catch (OnnxRuntimeException ex)
{
if (ex.Message.Contains("Exiting due to terminate flag"))
throw new OperationCanceledException("Inference was canceled.", ex);
throw;
}
}
/// <summary>
/// Creates the InferenceSession.
/// </summary>
/// <param name="optimizations">The optimizations.</param>
/// <returns>The Sessions ModelMetadata.</returns>
///
private async Task<ModelMetadata> CreateSession(ModelOptimization optimizations, CancellationToken cancellationToken)
{
_options?.Dispose();
_options = _configuration.ExecutionProvider.CreateSession(_configuration);
cancellationToken.Register(_options.CancelSession, true);
if (_configuration.IsOptimizationSupported)
ApplyOptimizations(optimizations);
_session = await Task.Run(() => new InferenceSession(_configuration.Path, _options), cancellationToken);
_allocator = new OrtAllocator(_session, _configuration.ExecutionProvider.MemoryInfo);
_metadata = new ModelMetadata(_session, _allocator);
return _metadata;
}
/// <summary>
/// Applies the optimizations.
/// </summary>
/// <param name="optimizations">The optimizations.</param>
protected virtual void ApplyOptimizations(ModelOptimization optimizations)
{
_optimizations = optimizations;
if (_optimizations != null)
{
_options.GraphOptimizationLevel = optimizations.OptimizationLevel.ToGraphOptimizationLevel();
if (_optimizations.OptimizationLevel == Optimization.None)
return;
foreach (var freeDimensionOverride in _optimizations.DimensionOverrides)
{
if (freeDimensionOverride.Key.StartsWith("dummy_"))
continue;
_options.AddFreeDimensionOverrideByName(freeDimensionOverride.Key, freeDimensionOverride.Value);
}
}
}
/// <summary>
/// Determines whether optimizations have changed
/// </summary>
/// <param name="optimizations">The optimizations.</param>
/// <returns><c>true</c> if changed; otherwise, <c>false</c>.</returns>
public virtual bool HasOptimizationsChanged(ModelOptimization optimizations)
{
if (_optimizations == null && optimizations == null)
return false; // No Optimizations set
if (_optimizations == optimizations)
return false; // Optimizations have not changed
return true;
}
#region IDisposable
private bool disposed = false;
/// <summary>
/// Disposes the managed and unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the managed and unmanaged resources.
/// </summary>
/// <param name="disposing">if set to <c>true</c> if disposing</param>
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
_session?.Dispose();
_options?.Dispose();
_allocator?.Dispose();
_session = null;
_options = null;
_allocator = null;
}
disposed = true;
}
/// <summary>
/// Finalizes an instance of the <see cref="ModelSession"/> class.
/// </summary>
~ModelSession()
{
Dispose(false);
}
#endregion
}
/// <summary>
/// Default ModelSession.
/// Implements the <see cref="IDisposable" />
/// </summary>
/// <seealso cref="IDisposable" />
public class ModelSession : ModelSession<ModelConfig>
{
/// <summary>
/// Initializes a new instance of the <see cref="ModelSession"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public ModelSession(ModelConfig configuration) : base(configuration) { }
}
}