offline-transducer-greedy-search-nemo-decoder.cc
6.9 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
// sherpa-onnx/csrc/offline-transducer-greedy-search-nemo-decoder.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/offline-transducer-greedy-search-nemo-decoder.h"
#include <algorithm>
#include <iterator>
#include <utility>
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
namespace sherpa_onnx {
static std::pair<Ort::Value, Ort::Value> BuildDecoderInput(
int32_t token, OrtAllocator *allocator) {
std::array<int64_t, 2> shape{1, 1};
Ort::Value decoder_input =
Ort::Value::CreateTensor<int32_t>(allocator, shape.data(), shape.size());
std::array<int64_t, 1> length_shape{1};
Ort::Value decoder_input_length = Ort::Value::CreateTensor<int32_t>(
allocator, length_shape.data(), length_shape.size());
int32_t *p = decoder_input.GetTensorMutableData<int32_t>();
int32_t *p_length = decoder_input_length.GetTensorMutableData<int32_t>();
p[0] = token;
p_length[0] = 1;
return {std::move(decoder_input), std::move(decoder_input_length)};
}
static OfflineTransducerDecoderResult DecodeOne(
const float *p, int32_t num_rows, int32_t num_cols,
OfflineTransducerNeMoModel *model, float blank_penalty) {
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
OfflineTransducerDecoderResult ans;
int32_t vocab_size = model->VocabSize();
int32_t blank_id = vocab_size - 1;
int32_t max_symbols_per_frame = 10;
auto decoder_input_pair = BuildDecoderInput(blank_id, model->Allocator());
std::pair<Ort::Value, std::vector<Ort::Value>> decoder_output_pair =
model->RunDecoder(std::move(decoder_input_pair.first),
std::move(decoder_input_pair.second),
model->GetDecoderInitStates(1));
std::array<int64_t, 3> encoder_shape{1, num_cols, 1};
for (int32_t t = 0; t != num_rows; ++t) {
Ort::Value cur_encoder_out = Ort::Value::CreateTensor(
memory_info, const_cast<float *>(p) + t * num_cols, num_cols,
encoder_shape.data(), encoder_shape.size());
for (int32_t q = 0; q != max_symbols_per_frame; ++q) {
Ort::Value logit = model->RunJoiner(View(&cur_encoder_out),
View(&decoder_output_pair.first));
float *p_logit = logit.GetTensorMutableData<float>();
if (blank_penalty > 0) {
p_logit[blank_id] -= blank_penalty;
}
auto y = static_cast<int32_t>(std::distance(
static_cast<const float *>(p_logit),
std::max_element(static_cast<const float *>(p_logit),
static_cast<const float *>(p_logit) + vocab_size)));
if (y != blank_id) {
ans.tokens.push_back(y);
ans.timestamps.push_back(t);
decoder_input_pair = BuildDecoderInput(y, model->Allocator());
decoder_output_pair =
model->RunDecoder(std::move(decoder_input_pair.first),
std::move(decoder_input_pair.second),
std::move(decoder_output_pair.second));
} else {
break;
} // if (y != blank_id)
}
} // for (int32_t i = 0; i != num_rows; ++i)
return ans;
}
static OfflineTransducerDecoderResult DecodeOneTDT(
const float *p, int32_t num_rows, int32_t num_cols,
OfflineTransducerNeMoModel *model, float blank_penalty) {
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
OfflineTransducerDecoderResult ans;
int32_t vocab_size = model->VocabSize();
int32_t blank_id = vocab_size - 1;
auto decoder_input_pair = BuildDecoderInput(blank_id, model->Allocator());
std::pair<Ort::Value, std::vector<Ort::Value>> decoder_output_pair =
model->RunDecoder(std::move(decoder_input_pair.first),
std::move(decoder_input_pair.second),
model->GetDecoderInitStates(1));
std::array<int64_t, 3> encoder_shape{1, num_cols, 1};
int32_t skip = 0;
for (int32_t t = 0; t < num_rows; t += skip) {
Ort::Value cur_encoder_out = Ort::Value::CreateTensor(
memory_info, const_cast<float *>(p) + t * num_cols, num_cols,
encoder_shape.data(), encoder_shape.size());
Ort::Value logit = model->RunJoiner(View(&cur_encoder_out),
View(&decoder_output_pair.first));
auto shape = logit.GetTensorTypeAndShapeInfo().GetShape();
float *p_logit = logit.GetTensorMutableData<float>();
if (blank_penalty > 0) {
p_logit[blank_id] -= blank_penalty;
}
auto y = static_cast<int32_t>(std::distance(
static_cast<const float *>(p_logit),
std::max_element(static_cast<const float *>(p_logit),
static_cast<const float *>(p_logit) + vocab_size)));
skip = static_cast<int32_t>(std::distance(
static_cast<const float *>(p_logit) + vocab_size,
std::max_element(static_cast<const float *>(p_logit) + vocab_size,
static_cast<const float *>(p_logit) + shape.back())));
if (skip == 0) {
skip = 1;
}
if (y != blank_id) {
ans.tokens.push_back(y);
ans.timestamps.push_back(t);
decoder_input_pair = BuildDecoderInput(y, model->Allocator());
decoder_output_pair =
model->RunDecoder(std::move(decoder_input_pair.first),
std::move(decoder_input_pair.second),
std::move(decoder_output_pair.second));
}
} // for (int32_t t = 0; t < num_rows; ++t) {
return ans;
}
std::vector<OfflineTransducerDecoderResult>
OfflineTransducerGreedySearchNeMoDecoder::Decode(
Ort::Value encoder_out, Ort::Value encoder_out_length,
OfflineStream ** /*ss = nullptr*/, int32_t /*n= 0*/) {
auto shape = encoder_out.GetTensorTypeAndShapeInfo().GetShape();
int32_t batch_size = static_cast<int32_t>(shape[0]);
int32_t dim1 = static_cast<int32_t>(shape[1]);
int32_t dim2 = static_cast<int32_t>(shape[2]);
auto length_type =
encoder_out_length.GetTensorTypeAndShapeInfo().GetElementType();
if ((length_type != ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32) &&
(length_type != ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64)) {
SHERPA_ONNX_LOGE("Unsupported encoder_out_length data type: %d",
static_cast<int32_t>(length_type));
SHERPA_ONNX_EXIT(-1);
}
const float *p = encoder_out.GetTensorData<float>();
std::vector<OfflineTransducerDecoderResult> ans(batch_size);
for (int32_t i = 0; i != batch_size; ++i) {
const float *this_p = p + dim1 * dim2 * i;
int32_t this_len = length_type == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32
? encoder_out_length.GetTensorData<int32_t>()[i]
: encoder_out_length.GetTensorData<int64_t>()[i];
if (is_tdt_) {
ans[i] = DecodeOneTDT(this_p, this_len, dim2, model_, blank_penalty_);
} else {
ans[i] = DecodeOne(this_p, this_len, dim2, model_, blank_penalty_);
}
}
return ans;
}
} // namespace sherpa_onnx