/
TrainingSession.shared.cs
904 lines (826 loc) · 44 KB
/
TrainingSession.shared.cs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
namespace Microsoft.ML.OnnxRuntime
{
#if __ENABLE_TRAINING_APIS__
/// <summary>
/// This class defines utility methods for training.
/// </summary>
public class TrainingUtils
{
/// <summary>
/// Use this function to generate reproducible results. It should be noted that completely
/// reproducible results are not guaranteed.
/// </summary>
/// <param name="seed">Manual seed to use for random number generation.</param>
public static void SetSeed(long seed)
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtSetSeed(seed));
}
}
enum LRScheduler
{
None = 0,
Constant = 1,
Linear = 2
}
/// <summary>
/// Trainer class that provides training, evaluation and optimizer methods for training an ONNX model.
///
/// The training session requires four training artifacts
/// - The training onnx model
/// - The evaluation onnx model (optional)
/// - The optimizer onnx model
/// - The checkpoint directory
///
/// These artifacts can be generated using the `onnxruntime-training` python [utility](https://github.com/microsoft/onnxruntime/blob/main/orttraining/orttraining/python/training/onnxblock/README.md).
///
/// This is an IDisposable class and it must be disposed of
/// using either an explicit call to Dispose() method or
/// a pattern of using() block. If this is a member of another
/// class that class must also become IDisposable and it must
/// dispose of TrainingSession in its Dispose() method.
/// </summary>
public class TrainingSession : IDisposable
{
/// <summary>
/// A pointer to an underlying native instance of OrtTrainingSession
/// </summary>
private IntPtr _nativeHandle;
private ulong _trainOutputCount;
private ulong _evalOutputCount;
private List<string> _trainOutputNames;
private List<string> _evalOutputNames;
private List<string> _trainInputNames;
private List<string> _evalInputNames;
private SessionOptions _builtInSessionOptions = null;
private RunOptions _builtInRunOptions = null;
private LRScheduler _scheduler = LRScheduler.None;
private bool _disposed = false;
#region Public API
/// <summary>
/// Create a training session that can be used to begin or resume training.
///
/// This constructor instantiates the training session based on the env and session options provided that can
/// begin or resume training from a given checkpoint state for the given onnx models.
/// The checkpoint state represents the parameters of the training session which will be moved
/// to the device specified by the user through the session options (if necessary).
/// </summary>
/// <param name="state">Training states that the training session uses as a starting point for training.</param>
/// <param name="trainModelPath">Model to be used to perform training.</param>
/// <param name="evalModelPath">Model to be used to perform evaluation.</param>
/// <param name="optimizerModelPath">Model to be used to perform weight update.</param>
public TrainingSession(CheckpointState state, string trainModelPath, string evalModelPath, string optimizerModelPath)
{
Init(null, state, NativeOnnxValueHelper.GetPlatformSerializedString(trainModelPath), NativeOnnxValueHelper.GetPlatformSerializedString(evalModelPath), NativeOnnxValueHelper.GetPlatformSerializedString(optimizerModelPath));
}
/// <summary>
/// Create a training session that can be used to begin or resume training.
///
/// This constructor instantiates the training session based on the env and session options provided that can
/// begin or resume training from a given checkpoint state for the given onnx models.
/// The checkpoint state represents the parameters of the training session which will be moved
/// to the device specified by the user through the session options (if necessary).
/// </summary>
/// <param name="state">Training states that the training session uses as a starting point for training.</param>
/// <param name="trainModelPath">Model to be used to perform training.</param>
/// <param name="optimizerModelPath">Model to be used to perform weight update.</param>
public TrainingSession(CheckpointState state, string trainModelPath, string optimizerModelPath)
{
Init(null, state, NativeOnnxValueHelper.GetPlatformSerializedString(trainModelPath), null, NativeOnnxValueHelper.GetPlatformSerializedString(optimizerModelPath));
}
/// <summary>
/// Create a training session that can be used to begin or resume training.
///
/// This constructor instantiates the training session based on the env and session options provided that can
/// begin or resume training from a given checkpoint state for the given onnx models.
/// The checkpoint state represents the parameters of the training session which will be moved
/// to the device specified by the user through the session options (if necessary).
/// </summary>
/// <param name="options">SessionOptions that the user can customize for this training session.</param>
/// <param name="state">Training states that the training session uses as a starting point for training.</param>
/// <param name="trainModelPath">Model to be used to perform training.</param>
/// <param name="evalModelPath">Model to be used to perform evaluation.</param>
/// <param name="optimizerModelPath">Model to be used to perform weight update.</param>
public TrainingSession(SessionOptions options, CheckpointState state, string trainModelPath, string evalModelPath, string optimizerModelPath)
{
Init(options, state, NativeOnnxValueHelper.GetPlatformSerializedString(trainModelPath), NativeOnnxValueHelper.GetPlatformSerializedString(evalModelPath), NativeOnnxValueHelper.GetPlatformSerializedString(optimizerModelPath));
}
/// <summary>
/// Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
///
/// This function performs a training step that computes the outputs of the training model and the gradients
/// of the trainable parameters for the given inputs. The train step is performed based on the training model
/// that was provided to the training session.
/// The TrainStep method is equivalent of running forward propagation and backward propagation in a single
/// step.
/// The gradients computed are stored inside the training session state so they can be later consumed
/// by the OptimizerStep function.
/// The gradients can be lazily reset by invoking the LazyResetGrad function.
/// </summary>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the training model.</param>
/// <param name="outputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the output values of the training model.</param>
public void TrainStep(
IReadOnlyCollection<FixedBufferOnnxValue> inputValues,
IReadOnlyCollection<FixedBufferOnnxValue> outputValues)
{
TrainStep(_builtInRunOptions, inputValues, outputValues);
}
/// <summary>
/// Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
///
/// This function performs a training step that computes the outputs of the training model and the gradients
/// of the trainable parameters for the given inputs. The train step is performed based on the training model
/// that was provided to the training session.
/// The TrainStep method is equivalent of running forward propagation and backward propagation in a single
/// step.
/// The gradients computed are stored inside the training session state so they can be later consumed
/// by the OptimizerStep function.
/// The gradients can be lazily reset by invoking the LazyResetGrad function.
/// </summary>
/// <param name="options">Specify <see cref="RunOptions"/> for step.</param>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the training model.</param>
/// <param name="outputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the output values of the training model.</param>
public void TrainStep(
RunOptions options,
IReadOnlyCollection<FixedBufferOnnxValue> inputValues,
IReadOnlyCollection<FixedBufferOnnxValue> outputValues)
{
if (_trainOutputCount != (ulong)outputValues.Count())
{
throw new ArgumentException($"Length of {nameof(outputValues)} ({outputValues.Count}) must match that of train model ({_trainOutputCount}).");
}
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues, true);
IntPtr[] outputValuesArray = GetOrtValuesHandles(outputValues, false); /* pointers to Pre-allocated OrtValue instances */
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtTrainStep(_nativeHandle, options.Handle, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)outputValues.Count, outputValuesArray));
}
/// <summary>
/// Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
///
/// This function performs a training step that computes the outputs of the training model and the gradients
/// of the trainable parameters for the given inputs. The train step is performed based on the training model
/// that was provided to the training session.
/// The TrainStep method is equivalent of running forward propagation and backward propagation in a single
/// step.
/// The gradients computed are stored inside the training session state so they can be later consumed
/// by the OptimizerStep function.
/// The gradients can be lazily reset by invoking the LazyResetGrad function.
/// </summary>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the training model.</param>
/// <returns>Output Tensors in a Collection of NamedOnnxValue. User must dispose the output.</returns>
public IDisposableReadOnlyCollection<DisposableNamedOnnxValue> TrainStep(
IReadOnlyCollection<FixedBufferOnnxValue> inputValues)
{
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues, true);
IntPtr[] outputValuesArray = new IntPtr[(int)_trainOutputCount];
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtTrainStep(_nativeHandle, _builtInRunOptions.Handle, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)_trainOutputCount, outputValuesArray));
// On success ortValues would contain nulls that will be
// ignored. On failure, ortValues would contain at least
// some valid OrtValue instances that need to be disposed.
// It would be nice to use using() clause, but we need to upgrade to C# 8.0 for that.
var ortValueDisposer = ConvertNativeHandlesToOrtValues(outputValuesArray);
try
{
var result = new DisposableList<DisposableNamedOnnxValue>(_trainOutputNames.Count);
try
{
for (int i = 0; i < ortValueDisposer.Span.Length; i++)
{
result.Add(DisposableNamedOnnxValue.CreateFromOrtValue(_trainOutputNames[i], ref ortValueDisposer.Span[i]));
}
}
catch (OnnxRuntimeException)
{
result.Dispose();
throw;
}
return result;
}
finally
{
// On success ortValues would contain nulls that will be
// ignored. On failure, ortValues would contain at least
// some valid OrtValue instances that need to be disposed.
ortValueDisposer.Dispose();
}
}
/// <summary>
/// Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
///
/// This function performs a training step that computes the outputs of the training model and the gradients
/// of the trainable parameters for the given inputs. The train step is performed based on the training model
/// that was provided to the training session.
/// The TrainStep method is equivalent of running forward propagation and backward propagation in a single
/// step.
/// The gradients computed are stored inside the training session state so they can be later consumed
/// by the OptimizerStep function.
/// The gradients can be lazily reset by invoking the LazyResetGrad function.
/// </summary>
/// <param name="options">Specify <see cref="RunOptions"/> for step.</param>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the training model.</param>
/// <returns>Output Tensors in a Collection of NamedOnnxValue. User must dispose the output.</returns>
public IDisposableReadOnlyCollection<DisposableNamedOnnxValue> TrainStep(
RunOptions options,
IReadOnlyCollection<FixedBufferOnnxValue> inputValues)
{
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues, true);
IntPtr[] outputValuesArray = new IntPtr[(int)_trainOutputCount];
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtTrainStep(_nativeHandle, options.Handle, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)_trainOutputCount, outputValuesArray));
// On success ortValues would contain nulls that will be
// ignored. On failure, ortValues would contain at least
// some valid OrtValue instances that need to be disposed.
// It would be nice to use using() clause, but we need to upgrade to C# 8.0 for that.
var ortValueDisposer = ConvertNativeHandlesToOrtValues(outputValuesArray);
try
{
var result = new DisposableList<DisposableNamedOnnxValue>(_trainOutputNames.Count);
try
{
for (int i = 0; i < ortValueDisposer.Span.Length; i++)
{
result.Add(DisposableNamedOnnxValue.CreateFromOrtValue(_trainOutputNames[i], ref ortValueDisposer.Span[i]));
}
}
catch (OnnxRuntimeException)
{
result.Dispose();
throw;
}
return result;
}
finally
{
ortValueDisposer.Dispose();
}
}
/// <summary>
/// This function performs a training step that computes the outputs of the training model and the gradients
/// of the trainable parameters for the given OrtValue inputs. The train step is performed based on the training model
/// that was provided to the training session.
/// The TrainStep method is equivalent of running forward propagation and backward propagation in a single
/// step.
/// The gradients computed are stored inside the training session state so they can be later consumed
/// by the OptimizerStep function.
/// The gradients can be lazily reset by invoking the LazyResetGrad function.
/// Example usage:
/// <code>
/// using OrtValue x = OrtValue.CreateTensorValueFromMemory(...);
/// using OrtValue label = OrtValue.CreateTensorValueFromMemory(...);
/// List<OrtValue> inputValues = new List<OrtValue> { x, label };
/// using (var loss = trainingSession.TrainStep(inputValues))
/// {
/// // process output values
/// }
/// </code>
/// </summary>
/// <param name="inputValues">Specify a collection of <see cref="OrtValue"/> that indicates the input values to the training model.</param>
/// <returns>Output Tensors in a Collection of NamedOnnxValue. User must dispose the output.</returns>
public IDisposableReadOnlyCollection<OrtValue> TrainStep(IReadOnlyCollection<OrtValue> inputValues)
{
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues);
IntPtr[] outputValuesArray = new IntPtr[(int)_trainOutputCount];
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtTrainStep(_nativeHandle, IntPtr.Zero, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)_trainOutputCount, outputValuesArray));
var disposableHandles = new DisposableOrtValueHandleArray(outputValuesArray);
try
{
return CreateDisposableResult(disposableHandles);
}
finally
{
disposableHandles.Dispose();
}
}
/// <summary>
/// Convert native OrtValue handles to OrtValue instances
/// in an exceptions safe manner.
/// </summary>
/// <param name="nativeHandles"></param>
/// <returns></returns>
private DisposableArray<OrtValue> ConvertNativeHandlesToOrtValues(IntPtr[] nativeHandles)
{
var diposableArray = new DisposableOrtValueHandleArray(nativeHandles);
try
{
var ortValues = new OrtValue[nativeHandles.Length];
var ortValueDisposer = new DisposableArray<OrtValue>(ortValues);
try
{
for (int i = 0; i < nativeHandles.Length; i++)
{
ortValues[i] = new OrtValue(nativeHandles[i]);
nativeHandles[i] = IntPtr.Zero;
}
return ortValueDisposer;
}
catch (Exception)
{
// ortValues is the result, dispose only on exception
ortValueDisposer.Dispose();
throw;
}
}
catch (Exception)
{
// No need to dispose on exception since the ownership is transferred to ortValues
diposableArray.Dispose();
throw;
}
}
/// <summary>
/// Reset the gradients of all trainable parameters to zero lazily.
///
/// This function sets the internal state of the training session such that the gradients of the trainable
/// parameters in the OrtCheckpointState will be scheduled to be reset just before the new gradients are
/// computed on the next invocation of the next TrainStep.
/// </summary>
public void LazyResetGrad()
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtLazyResetGrad(_nativeHandle));
}
/// <summary>
/// Computes the outputs for the eval model for the given inputs
/// This function performs an eval step that computes the outputs of the eval model for the given inputs.
/// The eval step is performed based on the eval model that was provided to the training session.
/// </summary>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the eval model.</param>
/// <param name="outputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the output values of the eval model.</param>
public void EvalStep(
IReadOnlyCollection<FixedBufferOnnxValue> inputValues,
IReadOnlyCollection<FixedBufferOnnxValue> outputValues)
{
EvalStep(_builtInRunOptions, inputValues, outputValues);
}
/// <summary>
/// Computes the outputs for the eval model for the given inputs
/// This function performs an eval step that computes the outputs of the eval model for the given inputs.
/// The eval step is performed based on the eval model that was provided to the training session.
/// </summary>
/// <param name="options">Specify <see cref="RunOptions"/> for step.</param>
/// <param name="inputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the input values to the eval model.</param>
/// <param name="outputValues">Specify a collection of <see cref="FixedBufferOnnxValue"/> that indicates the output values of the eval model.</param>
public void EvalStep(
RunOptions options,
IReadOnlyCollection<FixedBufferOnnxValue> inputValues,
IReadOnlyCollection<FixedBufferOnnxValue> outputValues)
{
if (_evalOutputCount != (ulong)outputValues.Count())
{
throw new ArgumentException($"Length of {nameof(outputValues)} ({outputValues.Count}) must match that of eval model ({_evalOutputCount}).");
}
const bool isInput = true;
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues, isInput);
IntPtr[] outputValuesArray = GetOrtValuesHandles(outputValues, !isInput); /* pointers to Pre-allocated OrtValue instances */
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtEvalStep(_nativeHandle, options.Handle, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)outputValues.Count, outputValuesArray));
}
/// <summary>
/// This function performs an eval step that computes the outputs of the eval model for the given inputs.
/// Inputs are expected to be of type OrtValue. The eval step is performed based on the eval model that was
/// provided to the training session.
/// Example usage:
/// <code>
/// using OrtValue x = OrtValue.CreateTensorValueFromMemory(...);
/// using OrtValue label = OrtValue.CreateTensorValueFromMemory(...);
/// List<OrtValue> inputValues = new List<OrtValue> { x, label };
/// using (var loss = trainingSession.EvalSteps(inputValues))
/// {
/// // process output values
/// }
/// </code>
/// </summary>
/// <param name="inputValues">Specify a collection of <see cref="OrtValue"/> that indicates the input values to the eval model.</param>
public IDisposableReadOnlyCollection<OrtValue> EvalStep(IReadOnlyCollection<OrtValue> inputValues)
{
IntPtr[] inputValuesArray = GetOrtValuesHandles(inputValues);
IntPtr[] outputValuesArray = new IntPtr[(int)_evalOutputCount];
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtEvalStep(_nativeHandle, IntPtr.Zero, (UIntPtr)inputValues.Count,
inputValuesArray, (UIntPtr)_evalOutputCount, outputValuesArray));
var disposableHandles = new DisposableOrtValueHandleArray(outputValuesArray);
try
{
return CreateDisposableResult(disposableHandles);
}
finally
{
disposableHandles.Dispose();
}
}
/// <summary>
/// Sets the learning rate for this training session.
///
/// This function allows users to set the learning rate for the training session. The current
/// learning rate is maintained by the training session and can be overwritten by invoking
/// this function with the desired learning rate. This function should not be used when a valid
/// learning rate scheduler is registered. It should be used either to set the learning rate
/// derived from a custom learning rate scheduler or to set a constant learning rate to be used
/// throughout the training session.
/// <note type="note">
/// Please note that this function does not set the initial learning rate that may be needed
/// by the predefined learning rate schedulers. To set the initial learning rate for learning
/// rate schedulers, please look at the function RegisterLinearLRScheduler.
/// </note>
/// </summary>
/// <param name="learningRate">Desired learning rate to be set.</param>
public void SetLearningRate(float learningRate)
{
if (_scheduler != LRScheduler.None && _scheduler != LRScheduler.Constant)
{
throw new InvalidOperationException("Cannot set constant LR while using LR scheduler.");
}
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtSetLearningRate(_nativeHandle, learningRate));
_scheduler = LRScheduler.Constant;
}
/// <summary>
/// Gets the current learning rate for this training session.
///
/// This function allows users to get the learning rate for the training session. The current
/// learning rate is maintained by the training session, and users can query it for the purpose
/// of implementing their own learning rate schedulers.
/// </summary>
/// <returns>float representing the current learning rate.</returns>
public float GetLearningRate()
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetLearningRate(_nativeHandle, out float lr));
return lr;
}
/// <summary>
/// Registers a linear learning rate scheduler for the training session.
///
/// Register a linear learning rate scheduler that decays the learning rate by linearly updated
/// multiplicative factor from the initial learning rate set on the training session to 0. The decay
/// is performed after the initial warm up phase where the learning rate is linearly incremented
/// from 0 to the initial learning rate provided.
/// </summary>
/// <param name="warmupStepCount"> Number of warmup steps</param>
/// <param name="totalStepCount"> Number of total steps</param>
/// <param name="initialLearningRate"> Initial learning rate</param>
public void RegisterLinearLRScheduler(long warmupStepCount,
long totalStepCount,
float initialLearningRate)
{
if (_scheduler != LRScheduler.None && _scheduler != LRScheduler.Constant)
{
throw new InvalidOperationException("Cannot set LR scheduler while using constant LR.");
}
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtRegisterLinearLRScheduler(_nativeHandle, warmupStepCount, totalStepCount, initialLearningRate));
_scheduler = LRScheduler.Linear;
}
/// <summary>
/// Update the learning rate based on the registered learning rate scheduler.
///
/// Takes a scheduler step that updates the learning rate that is being used by the training session.
/// This function should typically be called before invoking the optimizer step for each round,
/// or as determined necessary to update the learning rate being used by the training session.
/// <note type="note">
/// Please note that a valid predefined learning rate scheduler must be first registered to invoke this function.
/// </note>
/// </summary>
public void SchedulerStep()
{
if (_scheduler == LRScheduler.Constant || _scheduler == LRScheduler.None)
{
throw new InvalidOperationException("Cannot take scheduler step without registering a valid LR scheduler.");
}
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtSchedulerStep(_nativeHandle));
}
/// <summary>
/// Performs the weight updates for the trainable parameters using the optimizer model.
///
/// This function performs the weight update step that updates the trainable parameters such that they
/// take a step in the direction of their gradients (gradient descent). The optimizer step is performed
/// based on the optimizer model that was provided to the training session.
/// The updated parameters are stored inside the training state so that they can be used by the next
/// TrainStep function call.
/// </summary>
public void OptimizerStep()
{
OptimizerStep(_builtInRunOptions);
}
/// <summary>
/// Performs the weight updates for the trainable parameters using the optimizer model.
///
/// This function performs the weight update step that updates the trainable parameters such that they
/// take a step in the direction of their gradients (gradient descent). The optimizer step is performed
/// based on the optimizer model that was provided to the training session.
/// The updated parameters are stored inside the training state so that they can be used by the next
/// TrainStep function call.
/// </summary>
/// <param name="options">Specify <see cref="RunOptions"/> for step.</param>
public void OptimizerStep(RunOptions options)
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtOptimizerStep(_nativeHandle, options.Handle));
}
/// <summary>
/// Export a model that can be used for inferencing.
/// If the training session was provided with an eval model, the training session can generate
/// an inference model if it knows the inference graph outputs. The input inference graph outputs
/// are used to prune the eval model so that the inference model's outputs align with the provided outputs.
/// The exported model is saved at the path provided and can be used for inferencing with InferenceSession.
/// Note that the function re-loads the eval model from the path provided to TrainingSession
/// and expects that this path still be valid.
/// </summary>
/// <param name="inferenceModelPath">Path where the inference model should be serialized to.</param>
/// <param name="graphOutputNames">Names of the outputs that are needed in the inference model.</param>
public void ExportModelForInferencing(string inferenceModelPath, IReadOnlyCollection<string> graphOutputNames)
{
using (var cleanupList = new DisposableList<IDisposable>())
{
var outputNamesArray = ConvertNamesToUtf8(graphOutputNames, cleanupList);
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtExportModelForInferencing(
_nativeHandle, NativeOnnxValueHelper.GetPlatformSerializedString(inferenceModelPath),
(UIntPtr)graphOutputNames.Count, outputNamesArray));
}
}
/// <summary>
/// Returns a contiguous buffer that holds a copy of all training state parameters
/// </summary>
/// <param name="onlyTrainable">Whether to only copy trainable parameters or to copy all parameters.</param>
public OrtValue ToBuffer(bool onlyTrainable)
{
UIntPtr bufferSize = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetParametersSize(_nativeHandle, out bufferSize, onlyTrainable));
float[] bufferMemory = new float[bufferSize.ToUInt64()];
var shape = new long[] { (long)bufferSize };
var buffer = OrtValue.CreateAllocatedTensorValue(OrtAllocator.DefaultInstance, Tensors.TensorElementType.Float, shape);
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtCopyParametersToBuffer(_nativeHandle, buffer.Handle, onlyTrainable));
return buffer;
}
/// <summary>
/// Loads the training session model parameters from a contiguous buffer
/// </summary>
/// <param name="ortValue">Contiguous buffer to load the parameters from.</param>
/// <param name="onlyTrainable">Whether to only load trainable parameters or to load all parameters.</param>
public void FromBuffer(OrtValue ortValue, bool onlyTrainable)
{
if (ortValue.OnnxType != OnnxValueType.ONNX_TYPE_TENSOR)
{
throw new ArgumentException("Incorrect buffer received. Expected a tensor buffer.");
}
var tensorInfo = ortValue.GetTensorTypeAndShape();
if (tensorInfo.ElementDataType != Tensors.TensorElementType.Float)
{
throw new ArgumentException("Incorrect buffer received. Expected a tensor buffer of type float.");
}
UIntPtr numElements = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetParametersSize(_nativeHandle, out numElements, onlyTrainable));
if ((ulong)tensorInfo.ElementCount != (ulong)numElements)
{
string errorMessage = "Incorrect buffer size received. Expected size to be " + numElements.ToString() + ". Actual size: " + tensorInfo.ElementCount.ToString();
throw new ArgumentException(errorMessage);
}
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtCopyBufferToParameters(_nativeHandle, ortValue.Handle, onlyTrainable));
}
/// <summary>
/// Retrieves the names of the user outputs for the training and eval models.
/// </summary>
/// <param name="training">Whether the training model output names are requested or eval model output names.</param>
public List<string> OutputNames(bool training)
{
return training ? _trainOutputNames : _evalOutputNames;
}
/// <summary>
/// Retrieves the names of the user inputs for the training and eval models.
/// </summary>
/// <param name="training">Whether the training model input names are requested or eval model input names.</param>
public List<string> InputNames(bool training)
{
return training ? _trainInputNames : _evalInputNames;
}
#endregion
#region private methods
private void Init(SessionOptions sessOptions, CheckpointState state, byte[] trainModelPath, byte[] evalModelPath, byte[] optimizerModelPath)
{
if (!NativeTrainingMethods.TrainingEnabled())
{
throw new InvalidOperationException("This package does not contain the training API. Please install the Microsoft.ML.OnnxRuntime.Training NuGet package.\n");
}
var options = sessOptions;
if (sessOptions == null)
{
_builtInSessionOptions = new SessionOptions();
options = _builtInSessionOptions;
}
var envHandle = OrtEnv.Instance().Handle;
try
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtCreateTrainingSession(envHandle, options.Handle, state.Handle, trainModelPath,
evalModelPath, optimizerModelPath, out _nativeHandle));
UIntPtr outputCount = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetTrainingModelOutputCount(_nativeHandle, out outputCount));
_trainOutputCount = outputCount.ToUInt64();
// get all the output names and metadata
_trainOutputNames = new List<string>();
for (ulong i = 0; i < _trainOutputCount; i++)
{
_trainOutputNames.Add(GetOutputName(i, true));
}
_trainInputNames = new List<string>();
UIntPtr inputCount = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetTrainingModelInputCount(_nativeHandle, out inputCount));
for (ulong i = 0; i < inputCount.ToUInt64(); i++)
{
_trainInputNames.Add(GetInputName(i, true));
}
if (evalModelPath != null)
{
outputCount = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetEvalModelOutputCount(_nativeHandle, out outputCount));
_evalOutputCount = outputCount.ToUInt64();
_evalOutputNames = new List<string>();
for (ulong i = 0; i < _evalOutputCount; i++)
{
_evalOutputNames.Add(GetOutputName(i, false));
}
_evalInputNames = new List<string>();
inputCount = UIntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetEvalModelInputCount(_nativeHandle, out inputCount));
for (ulong i = 0; i < inputCount.ToUInt64(); i++)
{
_evalInputNames.Add(GetInputName(i, false));
}
}
_builtInRunOptions = new RunOptions(); // create a default built-in run option, and avoid creating a new one every run() call
}
catch (Exception)
{
CleanupHelper(true);
throw;
}
}
private string GetOutputName(ulong index, bool training)
{
var allocator = OrtAllocator.DefaultInstance;
IntPtr nameHandle;
if (training)
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetTrainingModelOutputName(
_nativeHandle,
(UIntPtr)index,
allocator.Pointer,
out nameHandle));
}
else
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetEvalModelOutputName(
_nativeHandle,
(UIntPtr)index,
allocator.Pointer,
out nameHandle));
}
return NativeOnnxValueHelper.StringFromNativeUtf8(nameHandle, allocator);
}
private string GetInputName(ulong index, bool training)
{
var allocator = OrtAllocator.DefaultInstance;
IntPtr nameHandle;
if (training)
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetTrainingModelInputName(
_nativeHandle,
(UIntPtr)index,
allocator.Pointer,
out nameHandle));
}
else
{
NativeApiStatus.VerifySuccess(NativeTrainingMethods.OrtGetEvalModelInputName(
_nativeHandle,
(UIntPtr)index,
allocator.Pointer,
out nameHandle));
}
return NativeOnnxValueHelper.StringFromNativeUtf8(nameHandle, allocator);
}
private IntPtr[] GetOrtValuesHandles(IReadOnlyCollection<FixedBufferOnnxValue> values, bool input)
{
var valuesArray = new IntPtr[values.Count];
for (int index = 0; index < values.Count; ++index)
{
var v = values.ElementAt(index);
if (!input && v.ElementType == Tensors.TensorElementType.String)
{
throw new NotSupportedException("Using string type FixedBufferOnnxValue in outputs is not supported.");
}
valuesArray[index] = v.Value.Handle;
}
return valuesArray;
}
private IntPtr[] GetOrtValuesHandles(IReadOnlyCollection<OrtValue> inputValues)
{
var valuesArray = new IntPtr[inputValues.Count];
for (int index = 0; index < inputValues.Count; ++index)
{
valuesArray[index] = inputValues.ElementAt(index).Handle;
}
return valuesArray;
}
private static IDisposableReadOnlyCollection<OrtValue> CreateDisposableResult(DisposableOrtValueHandleArray disposableHandles)
{
var outputValues = new DisposableList<OrtValue>(disposableHandles.Span.Length);
try
{
for (int i = 0; i < disposableHandles.Span.Length; i++)
{
outputValues.Add(new OrtValue(disposableHandles.Span[i]));
disposableHandles.Span[i] = IntPtr.Zero;
}
return outputValues;
}
catch (Exception)
{
outputValues.Dispose();
throw;
}
}
private IntPtr[] ConvertNamesToUtf8(IReadOnlyCollection<string> names, DisposableList<IDisposable> cleanupList)
{
cleanupList.Capacity += names.Count;
var result = new IntPtr[names.Count];
for (int i = 0; i < names.Count; ++i)
{
var name = names.ElementAt(i);
var utf8Name = NativeOnnxValueHelper.StringToZeroTerminatedUtf8(name);
var pinnedHandle = new Memory<byte>(utf8Name).Pin();
unsafe
{
result[i] = (IntPtr)pinnedHandle.Pointer;
}
cleanupList.Add(pinnedHandle);
}
return result;
}
/// <summary>
/// Other classes access
/// </summary>
internal IntPtr Handle
{
get
{
return _nativeHandle;
}
}
#endregion
#region IDisposable
/// <summary>
/// Finalizer.
/// </summary>
~TrainingSession()
{
Dispose(false);
}
/// <summary>
/// IDisposable implementation
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// IDisposable implementation
/// </summary>
/// <param name="disposing">true if invoked from Dispose() method</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
CleanupHelper(disposing);
_disposed = true;
}
private void CleanupHelper(bool disposing)
{
if (disposing)
{
if (_builtInRunOptions != null)
{
_builtInRunOptions.Dispose();
_builtInRunOptions = null;
}
if (_builtInSessionOptions != null)
{
_builtInSessionOptions.Dispose();
_builtInSessionOptions = null;
}
}
// cleanup unmanaged resources
if (_nativeHandle != IntPtr.Zero)
{
NativeTrainingMethods.OrtReleaseTrainingSession(_nativeHandle);
_nativeHandle = IntPtr.Zero;
}
}
#endregion
}
#endif
}