-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStatefulOnnxLabelScorer.cc
More file actions
487 lines (413 loc) · 19.4 KB
/
Copy pathStatefulOnnxLabelScorer.cc
File metadata and controls
487 lines (413 loc) · 19.4 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
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
/** Copyright 2025 RWTH Aachen University. All rights reserved.
*
* Licensed under the RWTH ASR License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.hltpr.rwth-aachen.de/rwth-asr/rwth-asr-license.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "StatefulOnnxLabelScorer.hh"
#include <algorithm>
#include <cstddef>
#include <utility>
#include <Core/Assertions.hh>
#include <Core/ReferenceCounting.hh>
#include <Flow/Timestamp.hh>
#include <Math/FastMatrix.hh>
#include <Mm/Module.hh>
#include <Speech/Types.hh>
#include "LabelScorer.hh"
#include "ScoringContext.hh"
namespace Nn {
/*
* =============================
* == StatefulOnnxLabelScorer ==
* =============================
*/
const Core::ParameterBool StatefulOnnxLabelScorer::paramBlankUpdatesHistory(
"blank-updates-history",
"Whether previously emitted blank labels should be used to update the history.",
false);
const Core::ParameterBool StatefulOnnxLabelScorer::paramLoopUpdatesHistory(
"loop-updates-history",
"Whether in the case of loop transitions every repeated emission should be used to update the history.",
false);
const Core::ParameterInt StatefulOnnxLabelScorer::paramMaxBatchSize(
"max-batch-size",
"Max number of hidden-states that can be fed into the scorer ONNX model at once.",
Core::Type<int>::max);
const Core::ParameterInt StatefulOnnxLabelScorer::paramMaxCachedScores(
"max-cached-score-vectors",
"Maximum size of cache that maps scoring contexts to scores. This prevents memory overflow in case of very long audio segments.",
1000);
// Scorer only takes hidden states as input which are not part of the IO spec
const std::vector<Onnx::IOSpecification> scorerModelIoSpec = {
Onnx::IOSpecification{
"scores",
Onnx::IODirection::OUTPUT,
false,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::FLOAT},
{{-1, -2}}}}; // [B, V]
const std::vector<Onnx::IOSpecification> stateInitializerModelIoSpec = {
Onnx::IOSpecification{
"encoder-states",
Onnx::IODirection::INPUT,
true,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::FLOAT},
{{1, -1, -2}, {-1, -1, -2}}}, // [1, T, E] or [B, T, E]
Onnx::IOSpecification{
"encoder-states-size",
Onnx::IODirection::INPUT,
true,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::INT32},
{{1}, {-1}}}}; // [1] or [B]
const std::vector<Onnx::IOSpecification> stateUpdaterModelIoSpec = {
Onnx::IOSpecification{
"encoder-states",
Onnx::IODirection::INPUT,
true,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::FLOAT},
{{1, -1, -2}, {-1, -1, -2}}}, // [1, T, E] or [B, T, E]
Onnx::IOSpecification{
"encoder-states-size",
Onnx::IODirection::INPUT,
true,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::INT32},
{{1}, {-1}}}, // [1] or [B]
Onnx::IOSpecification{
"token",
Onnx::IODirection::INPUT,
true,
{Onnx::ValueType::TENSOR},
{Onnx::ValueDataType::INT32},
{{1}, {-1}}}}; // [1] or [B]
StatefulOnnxLabelScorer::StatefulOnnxLabelScorer(Core::Configuration const& config)
: Core::Component(config),
Precursor(config),
blankUpdatesHistory_(paramBlankUpdatesHistory(config)),
loopUpdatesHistory_(paramLoopUpdatesHistory(config)),
maxBatchSize_(paramMaxBatchSize(config)),
scorerOnnxModel_(select("scorer-model"), scorerModelIoSpec),
stateInitializerOnnxModel_(select("state-initializer-model"), stateInitializerModelIoSpec),
stateUpdaterOnnxModel_(select("state-updater-model"), stateUpdaterModelIoSpec),
initialHiddenState_(),
initializerOutputToStateNameMap_(),
updaterInputToStateNameMap_(),
updaterOutputToStateNameMap_(),
scorerInputToStateNameMap_(),
scorerScoresName_(scorerOnnxModel_.mapping.getOnnxName("scores")),
initializerEncoderStatesName_(stateInitializerOnnxModel_.mapping.getOnnxName("encoder-states")),
initializerEncoderStatesSizeName_(stateInitializerOnnxModel_.mapping.getOnnxName("encoder-states-size")),
updaterEncoderStatesName_(stateUpdaterOnnxModel_.mapping.getOnnxName("encoder-states")),
updaterEncoderStatesSizeName_(stateUpdaterOnnxModel_.mapping.getOnnxName("encoder-states-size")),
updaterTokenName_(stateUpdaterOnnxModel_.mapping.getOnnxName("token")),
encoderStatesValue_(),
encoderStatesSizeValue_(),
scoreCache_(paramMaxCachedScores(config)) {
auto initializerMetadataKeys = stateInitializerOnnxModel_.session.getCustomMetadataKeys();
auto updaterMetadataKeys = stateUpdaterOnnxModel_.session.getCustomMetadataKeys();
auto scorerMetadataKeys = scorerOnnxModel_.session.getCustomMetadataKeys();
// Map state initializer outputs to states
std::unordered_set<std::string> initializerStateNames;
for (auto const& key : initializerMetadataKeys) {
if (stateInitializerOnnxModel_.session.hasOutput(key)) {
auto stateName = stateInitializerOnnxModel_.session.getCustomMetadata(key);
initializerOutputToStateNameMap_.emplace(key, stateName);
initializerStateNames.insert(stateName);
}
}
if (initializerStateNames.empty()) {
error() << "State initializer does not define any hidden states.";
}
// Map state updater inputs and outputs to states
std::unordered_set<std::string> updaterStateNames;
for (auto const& key : updaterMetadataKeys) {
if (stateUpdaterOnnxModel_.session.hasInput(key)) {
auto stateName = stateUpdaterOnnxModel_.session.getCustomMetadata(key);
if (initializerStateNames.find(stateName) == initializerStateNames.end()) {
error() << "State updater input " << key << " associated with state " << stateName << " is not present in state initializer";
}
updaterInputToStateNameMap_.emplace(key, stateName);
}
if (stateUpdaterOnnxModel_.session.hasOutput(key)) {
auto stateName = stateUpdaterOnnxModel_.session.getCustomMetadata(key);
if (initializerStateNames.find(stateName) == initializerStateNames.end()) {
error() << "State updater output " << key << " associated with state " << stateName << " is not present in state initializer";
}
updaterOutputToStateNameMap_.emplace(key, stateName);
updaterStateNames.insert(stateName);
}
}
if (updaterOutputToStateNameMap_.empty()) {
error() << "State updater does not produce any updated hidden states";
}
// In the loop we checked that the updater outputs are a subset of the initializer outputs.
// If they have the same size, they are equal. Otherwise, some initializer outputs
// are not updater outputs.
if (initializerStateNames.size() != updaterStateNames.size()) {
warning() << "State initializer has states that are not updated by the state updater";
}
// Map scorer inputs to states
for (auto const& key : scorerMetadataKeys) {
if (scorerOnnxModel_.session.hasInput(key)) {
auto stateName = scorerOnnxModel_.session.getCustomMetadata(key);
if (initializerStateNames.find(stateName) == initializerStateNames.end()) {
error() << "Scorer input " << key << " associated with state " << stateName << " is not present in state initializer";
}
scorerInputToStateNameMap_.emplace(key, stateName);
}
}
if (scorerInputToStateNameMap_.empty()) {
error() << "Scorer does not take any input";
}
}
void StatefulOnnxLabelScorer::reset() {
Precursor::reset();
scoreCache_.clear();
}
Core::Ref<const ScoringContext> StatefulOnnxLabelScorer::getInitialScoringContext() {
return Core::ref(new OnnxHiddenStateScoringContext()); // Sentinel empty Ref as initial hidden state
}
Core::Ref<const ScoringContext> StatefulOnnxLabelScorer::extendedScoringContext(LabelScorer::Request const& request) {
OnnxHiddenStateScoringContextRef scoringContext(dynamic_cast<const OnnxHiddenStateScoringContext*>(request.context.get()));
bool updateState = false;
switch (request.transitionType) {
case LabelScorer::TransitionType::BLANK_LOOP:
updateState = blankUpdatesHistory_ and loopUpdatesHistory_;
break;
case LabelScorer::TransitionType::LABEL_TO_BLANK:
case LabelScorer::TransitionType::INITIAL_BLANK:
updateState = blankUpdatesHistory_;
break;
case LabelScorer::TransitionType::LABEL_LOOP:
updateState = loopUpdatesHistory_;
break;
case LabelScorer::TransitionType::BLANK_TO_LABEL:
case LabelScorer::TransitionType::LABEL_TO_LABEL:
case LabelScorer::TransitionType::INITIAL_LABEL:
updateState = true;
break;
default:
error() << "Unknown transition type " << request.transitionType;
}
// If scoring context is not going to be modified, return the original one
if (not updateState) {
return request.context;
}
std::vector<LabelIndex> newLabelSeq(scoringContext->labelSeq);
newLabelSeq.push_back(request.nextToken);
// Re-use previous hidden-state but mark that finalization (i.e. hidden-state update) is required
auto newScoringContext = Core::ref(new OnnxHiddenStateScoringContext(std::move(newLabelSeq), scoringContext->hiddenState));
newScoringContext->requiresFinalize = true;
return newScoringContext;
}
void StatefulOnnxLabelScorer::addInput(DataView const& input) {
Precursor::addInput(input);
initialHiddenState_ = OnnxHiddenStateRef();
if (not encoderStatesValue_.empty()) { // Any previously computed hidden state values are outdated now so reset them
encoderStatesValue_ = Onnx::Value();
encoderStatesSizeValue_ = Onnx::Value();
}
}
std::optional<LabelScorer::ScoresWithTimes> StatefulOnnxLabelScorer::computeScoresWithTimes(std::vector<LabelScorer::Request> const& requests) {
if ((initializerEncoderStatesName_ != "" or initializerEncoderStatesSizeName_ != "" or updaterEncoderStatesName_ != "" or updaterEncoderStatesSizeName_ != "") and (expectMoreFeatures_ or bufferSize() == 0)) {
// Only allow scoring once all encoder states have been passed
return {};
}
ScoresWithTimes result;
result.scores.reserve(requests.size());
/*
* Identify unique scoring contexts that still need session runs
*/
std::unordered_set<OnnxHiddenStateScoringContextRef, ScoringContextHash, ScoringContextEq> uniqueUncachedScoringContexts;
for (auto& request : requests) {
// We need to finalize all scoring contexts before using them for scoring again.
OnnxHiddenStateScoringContextRef scoringContext(dynamic_cast<const OnnxHiddenStateScoringContext*>(request.context.get()));
finalizeScoringContext(scoringContext);
if (not scoreCache_.contains(scoringContext)) {
// Group by unique scoring context
uniqueUncachedScoringContexts.emplace(scoringContext);
}
}
std::vector<OnnxHiddenStateScoringContextRef> scoringContextBatch;
scoringContextBatch.reserve(std::min(uniqueUncachedScoringContexts.size(), maxBatchSize_));
for (auto scoringContext : uniqueUncachedScoringContexts) {
scoringContextBatch.push_back(scoringContext);
if (scoringContextBatch.size() == maxBatchSize_) { // Batch is full -> forward now
forwardBatch(scoringContextBatch);
scoringContextBatch.clear();
}
}
forwardBatch(scoringContextBatch); // Forward remaining scoring contexts
/*
* Assign from cache map to result vector
*/
for (const auto& request : requests) {
OnnxHiddenStateScoringContextRef scoringContext(dynamic_cast<const OnnxHiddenStateScoringContext*>(request.context.get()));
verify(scoreCache_.contains(scoringContext));
auto const& scores = scoreCache_.get(scoringContext)->get();
result.scores.push_back(scores.at(request.nextToken));
result.timeframes.push_back(scoringContext->labelSeq.size());
}
return result;
}
std::optional<LabelScorer::ScoreWithTime> StatefulOnnxLabelScorer::computeScoreWithTime(LabelScorer::Request const& request) {
auto result = computeScoresWithTimes({request});
if (not result) {
return {};
}
return ScoreWithTime{result->scores.front(), result->timeframes.front()};
}
size_t StatefulOnnxLabelScorer::getMinActiveInputIndex(Core::CollapsedVector<ScoringContextRef> const& activeContexts) const {
return 0u;
}
void StatefulOnnxLabelScorer::setupEncoderStatesValue() {
if (not encoderStatesValue_.empty()) {
return;
}
u32 T = bufferSize();
auto inputFeatureDataView = getInput(0);
encoderStatesValue_ = Onnx::Value::createEmpty<f32>({1l, static_cast<int64_t>(T), static_cast<int64_t>(inputFeatureDataView->size())});
for (size_t t = 0ul; t < T; ++t) {
inputFeatureDataView = getInput(t);
std::copy(inputFeatureDataView->data(), inputFeatureDataView->data() + inputFeatureDataView->size(), encoderStatesValue_.data<f32>(0, t));
}
}
void StatefulOnnxLabelScorer::setupEncoderStatesSizeValue() {
if (not encoderStatesSizeValue_.empty()) {
return;
}
u32 T = bufferSize();
encoderStatesSizeValue_ = Onnx::Value::create(std::vector<s32>{static_cast<s32>(T)});
}
OnnxHiddenStateRef StatefulOnnxLabelScorer::computeInitialHiddenState() {
verify(not expectMoreFeatures_);
if (not initialHiddenState_) { // initialHiddenState_ is still sentinel value -> compute it
/*
* Create session inputs
*/
std::vector<std::pair<std::string, Onnx::Value>> sessionInputs;
if (initializerEncoderStatesName_ != "") {
setupEncoderStatesValue();
sessionInputs.emplace_back(initializerEncoderStatesName_, std::move(encoderStatesValue_));
}
if (initializerEncoderStatesSizeName_ != "") {
setupEncoderStatesSizeValue();
sessionInputs.emplace_back(initializerEncoderStatesSizeName_, std::move(encoderStatesSizeValue_));
}
std::vector<std::string> sessionOutputNames;
std::vector<std::string> stateNames;
for (auto const& [outputName, stateName] : initializerOutputToStateNameMap_) {
sessionOutputNames.push_back(outputName);
stateNames.push_back(stateName);
}
/*
* Run session
*/
std::vector<Onnx::Value> sessionOutputs;
stateInitializerOnnxModel_.session.run(std::move(sessionInputs), sessionOutputNames, sessionOutputs);
/*
* Return resulting hidden state
*/
initialHiddenState_ = Core::ref(new OnnxHiddenState(std::move(stateNames), std::move(sessionOutputs)));
}
return initialHiddenState_;
}
OnnxHiddenStateRef StatefulOnnxLabelScorer::updatedHiddenState(OnnxHiddenStateRef const& hiddenState, LabelIndex nextToken) {
/*
* Create session inputs
*/
std::vector<std::pair<std::string, Onnx::Value>> sessionInputs;
if (updaterEncoderStatesName_ != "") {
setupEncoderStatesValue();
sessionInputs.emplace_back(updaterEncoderStatesName_, std::move(encoderStatesValue_));
}
if (updaterEncoderStatesSizeName_ != "") {
setupEncoderStatesSizeValue();
sessionInputs.emplace_back(updaterEncoderStatesSizeName_, std::move(encoderStatesSizeValue_));
}
if (updaterTokenName_ != "") {
sessionInputs.emplace_back(updaterTokenName_, Onnx::Value::create(std::vector<s32>{static_cast<s32>(nextToken)}));
}
for (auto const& [inputName, stateName] : updaterInputToStateNameMap_) {
sessionInputs.emplace_back(inputName, hiddenState->stateValueMap.at(stateName));
}
/*
* Run session
*/
std::vector<std::string> sessionOutputNames;
std::vector<std::string> stateNames;
for (auto const& [outputName, stateName] : updaterOutputToStateNameMap_) {
sessionOutputNames.push_back(outputName);
stateNames.push_back(stateName);
}
std::vector<Onnx::Value> sessionOutputs;
stateUpdaterOnnxModel_.session.run(std::move(sessionInputs), sessionOutputNames, sessionOutputs);
/*
* Return resulting hidden state
*/
auto newHiddenState = Core::ref(new OnnxHiddenState(std::move(stateNames), std::move(sessionOutputs)));
return newHiddenState;
}
void StatefulOnnxLabelScorer::finalizeScoringContext(OnnxHiddenStateScoringContextRef const& scoringContext) {
// If this scoring context does not need finalization, don't change it
if (not scoringContext->requiresFinalize) {
return;
}
auto hiddenState = scoringContext->hiddenState;
if (not hiddenState) { // Sentinel start-state
hiddenState = computeInitialHiddenState();
}
verify(not scoringContext->labelSeq.empty());
scoringContext->hiddenState = updatedHiddenState(hiddenState, scoringContext->labelSeq.back());
scoringContext->requiresFinalize = false;
}
void StatefulOnnxLabelScorer::forwardBatch(std::vector<OnnxHiddenStateScoringContextRef> const& scoringContextBatch) {
if (scoringContextBatch.empty()) {
return;
}
/*
* Create session inputs
*/
std::vector<std::pair<std::string, Onnx::Value>> sessionInputs;
for (auto const& [inputName, stateName] : scorerInputToStateNameMap_) {
// Collect a vector of individual state values of shape [1, *] and afterwards concatenate
// them to a batched state tensor of shape [B, *]
std::vector<Onnx::Value const*> stateValues;
stateValues.reserve(scoringContextBatch.size());
for (size_t b = 0ul; b < scoringContextBatch.size(); ++b) {
auto scoringContext = scoringContextBatch[b];
auto hiddenState = scoringContext->hiddenState;
if (not hiddenState) { // Sentinel hidden-state
hiddenState = computeInitialHiddenState();
}
stateValues.push_back(&hiddenState->stateValueMap.at(stateName));
}
sessionInputs.emplace_back(inputName, Onnx::Value::concat(stateValues, 0));
}
/*
* Run session
*/
std::vector<Onnx::Value> sessionOutputs;
scorerOnnxModel_.session.run(std::move(sessionInputs), {scorerScoresName_}, sessionOutputs);
/*
* Put resulting scores into cache map
*/
for (size_t b = 0ul; b < scoringContextBatch.size(); ++b) {
std::vector<f32> scoreVec;
sessionOutputs.front().get(b, scoreVec);
scoreCache_.put(scoringContextBatch[b], std::move(scoreVec));
}
}
} // namespace Nn