Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#import "ort_training_session_internal.h"
 
#import <vector>
#import <optional>
#import <string>
 
#import "cxx_api.h"
#import "cxx_utils.h"
#import "error_utils.h"
#import "ort_checkpoint_internal.h"
#import "ort_session_internal.h"
#import "ort_enums_internal.h"
#import "ort_env_internal.h"
#import "ort_value_internal.h"
 
NS_ASSUME_NONNULL_BEGIN
 
@implementation ORTTrainingSession {
  ORTEnv* _env;  // keep a strong reference so the ORTEnv doesn't get destroyed before this does
  ORTCheckpoint* _checkpoint;
  id _epContextDataReadRegistration;
  std::optional<Ort::TrainingSession> _session;
}
 
- (Ort::TrainingSession&)CXXAPIOrtTrainingSession {
  return *_session;
}
 
- (nullable instancetype)initWithEnv:(ORTEnv*)env
                      sessionOptions:(nullable ORTSessionOptions*)sessionOptions
                          checkpoint:(ORTCheckpoint*)checkpoint
                      trainModelPath:(NSString*)trainModelPath
                       evalModelPath:(nullable NSString*)evalModelPath
                  optimizerModelPath:(nullable NSString*)optimizerModelPath
                               error:(NSError**)error {
  if ((self = [super init]) == nil) {
    return nil;
  }
 
  try {
    if (!sessionOptions) {
      sessionOptions = [[ORTSessionOptions alloc] initWithError:error];
      if (!sessionOptions) {
        return nil;
      }
    }
 
    std::optional<std::string> evalPath = utils::toStdOptionalString(evalModelPath);
    std::optional<std::string> optimizerPath = utils::toStdOptionalString(optimizerModelPath);
 
    id registrationSnapshot;
    Ort::SessionOptions sessionOptionsSnapshot{nullptr};
    @synchronized(sessionOptions) {
      registrationSnapshot = [sessionOptions epContextDataReadRegistrationSnapshot];
      sessionOptionsSnapshot = [sessionOptions CXXAPIOrtSessionOptions].Clone();
    }
 
    std::optional<Ort::TrainingSession> session;
    session.emplace(
        [env CXXAPIOrtEnv],
        sessionOptionsSnapshot,
        [checkpoint CXXAPIOrtCheckpoint],
        trainModelPath.UTF8String,
        evalPath,
        optimizerPath);
 
    _env = env;
    _checkpoint = checkpoint;
    _epContextDataReadRegistration = registrationSnapshot;
    _session = std::move(session);
    return self;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (void)dealloc {
  // Execution providers may retain the raw callback state, so release the native session first.
  _session.reset();
  _epContextDataReadRegistration = nil;
}
 
- (nullable NSArray<ORTValue*>*)trainStepWithInputValues:(NSArray<ORTValue*>*)inputs
                                                   error:(NSError**)error {
  try {
    std::vector<const OrtValue*> inputValues = utils::getWrappedCAPIOrtValues(inputs);
 
    size_t outputCount;
    Ort::ThrowOnError(Ort::GetTrainingApi().TrainingSessionGetTrainingModelOutputCount(*_session, &outputCount));
    std::vector<OrtValue*> outputValues(outputCount, nullptr);
 
    Ort::RunOptions runOptions;
    Ort::ThrowOnError(Ort::GetTrainingApi().TrainStep(
        *_session,
        runOptions,
        inputValues.size(),
        inputValues.data(),
        outputValues.size(),
        outputValues.data()));
 
    return utils::wrapUnownedCAPIOrtValues(outputValues, error);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
- (nullable NSArray<ORTValue*>*)evalStepWithInputValues:(NSArray<ORTValue*>*)inputs
                                                  error:(NSError**)error {
  try {
    // create vector of OrtValue from NSArray<ORTValue*> with same size as inputValues
    std::vector<const OrtValue*> inputValues = utils::getWrappedCAPIOrtValues(inputs);
 
    size_t outputCount;
    Ort::ThrowOnError(Ort::GetTrainingApi().TrainingSessionGetEvalModelOutputCount(*_session, &outputCount));
    std::vector<OrtValue*> outputValues(outputCount, nullptr);
 
    Ort::RunOptions runOptions;
    Ort::ThrowOnError(Ort::GetTrainingApi().EvalStep(
        *_session,
        runOptions,
        inputValues.size(),
        inputValues.data(),
        outputValues.size(),
        outputValues.data()));
 
    return utils::wrapUnownedCAPIOrtValues(outputValues, error);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (BOOL)lazyResetGradWithError:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].LazyResetGrad();
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (BOOL)optimizerStepWithError:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].OptimizerStep();
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (nullable NSArray<NSString*>*)getTrainInputNamesWithError:(NSError**)error {
  try {
    std::vector<std::string> inputNames = [self CXXAPIOrtTrainingSession].InputNames(true);
    return utils::toNSStringNSArray(inputNames);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (nullable NSArray<NSString*>*)getTrainOutputNamesWithError:(NSError**)error {
  try {
    std::vector<std::string> outputNames = [self CXXAPIOrtTrainingSession].OutputNames(true);
    return utils::toNSStringNSArray(outputNames);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (nullable NSArray<NSString*>*)getEvalInputNamesWithError:(NSError**)error {
  try {
    std::vector<std::string> inputNames = [self CXXAPIOrtTrainingSession].InputNames(false);
    return utils::toNSStringNSArray(inputNames);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (nullable NSArray<NSString*>*)getEvalOutputNamesWithError:(NSError**)error {
  try {
    std::vector<std::string> outputNames = [self CXXAPIOrtTrainingSession].OutputNames(false);
    return utils::toNSStringNSArray(outputNames);
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (BOOL)registerLinearLRSchedulerWithWarmupStepCount:(int64_t)warmupStepCount
                                      totalStepCount:(int64_t)totalStepCount
                                           initialLr:(float)initialLr
                                               error:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].RegisterLinearLRScheduler(warmupStepCount, totalStepCount, initialLr);
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (BOOL)schedulerStepWithError:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].SchedulerStep();
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (float)getLearningRateWithError:(NSError**)error {
  try {
    return [self CXXAPIOrtTrainingSession].GetLearningRate();
  }
  ORT_OBJC_API_IMPL_CATCH(error, 0.0f);
}
 
- (BOOL)setLearningRate:(float)lr
                  error:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].SetLearningRate(lr);
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (BOOL)fromBufferWithValue:(ORTValue*)buffer
                      error:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].FromBuffer([buffer CXXAPIOrtValue]);
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
- (nullable ORTValue*)toBufferWithTrainable:(BOOL)onlyTrainable
                                      error:(NSError**)error {
  try {
    Ort::Value val = [self CXXAPIOrtTrainingSession].ToBuffer(onlyTrainable);
    return [[ORTValue alloc] initWithCXXAPIOrtValue:std::move(val)
                                 externalTensorData:nil
                                              error:error];
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
 
- (BOOL)exportModelForInferenceWithOutputPath:(NSString*)inferenceModelPath
                             graphOutputNames:(NSArray<NSString*>*)graphOutputNames
                                        error:(NSError**)error {
  try {
    [self CXXAPIOrtTrainingSession].ExportModelForInferencing(utils::toStdString(inferenceModelPath),
                                                              utils::toStdStringVector(graphOutputNames));
    return YES;
  }
  ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
}
 
@end
 
void ORTSetSeed(int64_t seed) {
  Ort::SetSeed(seed);
}
 
NS_ASSUME_NONNULL_END