offline-moonshine-model.cc
9.2 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
// sherpa-onnx/csrc/offline-moonshine-model.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/offline-moonshine-model.h"
#include <string>
#include <utility>
#include <vector>
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/session.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
class OfflineMoonshineModel::Impl {
public:
explicit Impl(const OfflineModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
{
auto buf = ReadFile(config.moonshine.preprocessor);
InitPreprocessor(buf.data(), buf.size());
}
{
auto buf = ReadFile(config.moonshine.encoder);
InitEncoder(buf.data(), buf.size());
}
{
auto buf = ReadFile(config.moonshine.uncached_decoder);
InitUnCachedDecoder(buf.data(), buf.size());
}
{
auto buf = ReadFile(config.moonshine.cached_decoder);
InitCachedDecoder(buf.data(), buf.size());
}
}
#if __ANDROID_API__ >= 9
Impl(AAssetManager *mgr, const OfflineModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
{
auto buf = ReadFile(mgr, config.moonshine.preprocessor);
InitPreprocessor(buf.data(), buf.size());
}
{
auto buf = ReadFile(mgr, config.moonshine.encoder);
InitEncoder(buf.data(), buf.size());
}
{
auto buf = ReadFile(mgr, config.moonshine.uncached_decoder);
InitUnCachedDecoder(buf.data(), buf.size());
}
{
auto buf = ReadFile(mgr, config.moonshine.cached_decoder);
InitCachedDecoder(buf.data(), buf.size());
}
}
#endif
Ort::Value ForwardPreprocessor(Ort::Value audio) {
auto features = preprocessor_sess_->Run(
{}, preprocessor_input_names_ptr_.data(), &audio, 1,
preprocessor_output_names_ptr_.data(),
preprocessor_output_names_ptr_.size());
return std::move(features[0]);
}
Ort::Value ForwardEncoder(Ort::Value features, Ort::Value features_len) {
std::array<Ort::Value, 2> encoder_inputs{std::move(features),
std::move(features_len)};
auto encoder_out = encoder_sess_->Run(
{}, encoder_input_names_ptr_.data(), encoder_inputs.data(),
encoder_inputs.size(), encoder_output_names_ptr_.data(),
encoder_output_names_ptr_.size());
return std::move(encoder_out[0]);
}
std::pair<Ort::Value, std::vector<Ort::Value>> ForwardUnCachedDecoder(
Ort::Value tokens, Ort::Value seq_len, Ort::Value encoder_out) {
std::array<Ort::Value, 3> uncached_decoder_input = {
std::move(tokens),
std::move(encoder_out),
std::move(seq_len),
};
auto uncached_decoder_out = uncached_decoder_sess_->Run(
{}, uncached_decoder_input_names_ptr_.data(),
uncached_decoder_input.data(), uncached_decoder_input.size(),
uncached_decoder_output_names_ptr_.data(),
uncached_decoder_output_names_ptr_.size());
std::vector<Ort::Value> states;
states.reserve(uncached_decoder_out.size() - 1);
int32_t i = -1;
for (auto &s : uncached_decoder_out) {
++i;
if (i == 0) {
continue;
}
states.push_back(std::move(s));
}
return {std::move(uncached_decoder_out[0]), std::move(states)};
}
std::pair<Ort::Value, std::vector<Ort::Value>> ForwardCachedDecoder(
Ort::Value tokens, Ort::Value seq_len, Ort::Value encoder_out,
std::vector<Ort::Value> states) {
std::vector<Ort::Value> cached_decoder_input;
cached_decoder_input.reserve(3 + states.size());
cached_decoder_input.push_back(std::move(tokens));
cached_decoder_input.push_back(std::move(encoder_out));
cached_decoder_input.push_back(std::move(seq_len));
for (auto &s : states) {
cached_decoder_input.push_back(std::move(s));
}
auto cached_decoder_out = cached_decoder_sess_->Run(
{}, cached_decoder_input_names_ptr_.data(), cached_decoder_input.data(),
cached_decoder_input.size(), cached_decoder_output_names_ptr_.data(),
cached_decoder_output_names_ptr_.size());
std::vector<Ort::Value> next_states;
next_states.reserve(cached_decoder_out.size() - 1);
int32_t i = -1;
for (auto &s : cached_decoder_out) {
++i;
if (i == 0) {
continue;
}
next_states.push_back(std::move(s));
}
return {std::move(cached_decoder_out[0]), std::move(next_states)};
}
OrtAllocator *Allocator() { return allocator_; }
private:
void InitPreprocessor(void *model_data, size_t model_data_length) {
preprocessor_sess_ = std::make_unique<Ort::Session>(
env_, model_data, model_data_length, sess_opts_);
GetInputNames(preprocessor_sess_.get(), &preprocessor_input_names_,
&preprocessor_input_names_ptr_);
GetOutputNames(preprocessor_sess_.get(), &preprocessor_output_names_,
&preprocessor_output_names_ptr_);
}
void InitEncoder(void *model_data, size_t model_data_length) {
encoder_sess_ = std::make_unique<Ort::Session>(
env_, model_data, model_data_length, sess_opts_);
GetInputNames(encoder_sess_.get(), &encoder_input_names_,
&encoder_input_names_ptr_);
GetOutputNames(encoder_sess_.get(), &encoder_output_names_,
&encoder_output_names_ptr_);
}
void InitUnCachedDecoder(void *model_data, size_t model_data_length) {
uncached_decoder_sess_ = std::make_unique<Ort::Session>(
env_, model_data, model_data_length, sess_opts_);
GetInputNames(uncached_decoder_sess_.get(), &uncached_decoder_input_names_,
&uncached_decoder_input_names_ptr_);
GetOutputNames(uncached_decoder_sess_.get(),
&uncached_decoder_output_names_,
&uncached_decoder_output_names_ptr_);
}
void InitCachedDecoder(void *model_data, size_t model_data_length) {
cached_decoder_sess_ = std::make_unique<Ort::Session>(
env_, model_data, model_data_length, sess_opts_);
GetInputNames(cached_decoder_sess_.get(), &cached_decoder_input_names_,
&cached_decoder_input_names_ptr_);
GetOutputNames(cached_decoder_sess_.get(), &cached_decoder_output_names_,
&cached_decoder_output_names_ptr_);
}
private:
OfflineModelConfig config_;
Ort::Env env_;
Ort::SessionOptions sess_opts_;
Ort::AllocatorWithDefaultOptions allocator_;
std::unique_ptr<Ort::Session> preprocessor_sess_;
std::unique_ptr<Ort::Session> encoder_sess_;
std::unique_ptr<Ort::Session> uncached_decoder_sess_;
std::unique_ptr<Ort::Session> cached_decoder_sess_;
std::vector<std::string> preprocessor_input_names_;
std::vector<const char *> preprocessor_input_names_ptr_;
std::vector<std::string> preprocessor_output_names_;
std::vector<const char *> preprocessor_output_names_ptr_;
std::vector<std::string> encoder_input_names_;
std::vector<const char *> encoder_input_names_ptr_;
std::vector<std::string> encoder_output_names_;
std::vector<const char *> encoder_output_names_ptr_;
std::vector<std::string> uncached_decoder_input_names_;
std::vector<const char *> uncached_decoder_input_names_ptr_;
std::vector<std::string> uncached_decoder_output_names_;
std::vector<const char *> uncached_decoder_output_names_ptr_;
std::vector<std::string> cached_decoder_input_names_;
std::vector<const char *> cached_decoder_input_names_ptr_;
std::vector<std::string> cached_decoder_output_names_;
std::vector<const char *> cached_decoder_output_names_ptr_;
};
OfflineMoonshineModel::OfflineMoonshineModel(const OfflineModelConfig &config)
: impl_(std::make_unique<Impl>(config)) {}
#if __ANDROID_API__ >= 9
OfflineMoonshineModel::OfflineMoonshineModel(AAssetManager *mgr,
const OfflineModelConfig &config)
: impl_(std::make_unique<Impl>(mgr, config)) {}
#endif
OfflineMoonshineModel::~OfflineMoonshineModel() = default;
Ort::Value OfflineMoonshineModel::ForwardPreprocessor(Ort::Value audio) const {
return impl_->ForwardPreprocessor(std::move(audio));
}
Ort::Value OfflineMoonshineModel::ForwardEncoder(
Ort::Value features, Ort::Value features_len) const {
return impl_->ForwardEncoder(std::move(features), std::move(features_len));
}
std::pair<Ort::Value, std::vector<Ort::Value>>
OfflineMoonshineModel::ForwardUnCachedDecoder(Ort::Value token,
Ort::Value seq_len,
Ort::Value encoder_out) const {
return impl_->ForwardUnCachedDecoder(std::move(token), std::move(seq_len),
std::move(encoder_out));
}
std::pair<Ort::Value, std::vector<Ort::Value>>
OfflineMoonshineModel::ForwardCachedDecoder(
Ort::Value token, Ort::Value seq_len, Ort::Value encoder_out,
std::vector<Ort::Value> states) const {
return impl_->ForwardCachedDecoder(std::move(token), std::move(seq_len),
std::move(encoder_out), std::move(states));
}
OrtAllocator *OfflineMoonshineModel::Allocator() const {
return impl_->Allocator();
}
} // namespace sherpa_onnx