online-t-one-ctc-model.cc
7.6 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
// sherpa-onnx/csrc/online-t-one-ctc-model.cc
//
// Copyright (c) 2025 Xiaomi Corporation
#include "sherpa-onnx/csrc/online-t-one-ctc-model.h"
#include <algorithm>
#include <cmath>
#include <string>
#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#if __OHOS__
#include "rawfile/raw_file_manager.h"
#endif
#include "sherpa-onnx/csrc/cat.h"
#include "sherpa-onnx/csrc/file-utils.h"
#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"
#include "sherpa-onnx/csrc/unbind.h"
namespace sherpa_onnx {
class OnlineToneCtcModel::Impl {
public:
explicit Impl(const OnlineModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
{
auto buf = ReadFile(config.t_one_ctc.model);
Init(buf.data(), buf.size());
}
}
template <typename Manager>
Impl(Manager *mgr, const OnlineModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
{
auto buf = ReadFile(mgr, config.t_one_ctc.model);
Init(buf.data(), buf.size());
}
}
std::vector<Ort::Value> Forward(Ort::Value x,
std::vector<Ort::Value> states) {
// shape0 is (batch_size, 1, num_samples)
auto shape0 = x.GetTensorTypeAndShapeInfo().GetShape();
std::array<int64_t, 3> shape = {shape0[0], shape0[2], shape0[1]};
std::vector<int32_t> samples(shape[0] * shape[1] * shape[2]);
const float *px = x.GetTensorData<float>();
for (int32_t i = 0; i < samples.size(); ++i) {
float f = px[i];
f = f > 1 ? 1 : f;
f = f < -1 ? -1 : f;
samples[i] = static_cast<int32_t>(f * 32767);
}
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
Ort::Value xx =
Ort::Value::CreateTensor(memory_info, samples.data(), samples.size(),
shape.data(), shape.size());
std::array<Ort::Value, 2> inputs = {std::move(xx), std::move(states[0])};
auto out =
sess_->Run({}, input_names_ptr_.data(), inputs.data(), inputs.size(),
output_names_ptr_.data(), output_names_ptr_.size());
// out[0]: log_probs
// out[1] next_states
return out;
}
int32_t VocabSize() const { return vocab_size_; }
int32_t ChunkLength() const { return 1; }
int32_t ChunkShift() const { return 1; }
OrtAllocator *Allocator() { return allocator_; }
// Return a vector containing 1 tensor
// - state_
std::vector<Ort::Value> GetInitStates() {
std::vector<Ort::Value> ans;
ans.push_back(View(&state_));
return ans;
}
std::vector<Ort::Value> StackStates(
std::vector<std::vector<Ort::Value>> states) {
int32_t batch_size = static_cast<int32_t>(states.size());
if (batch_size == 1) {
return std::move(states[0]);
}
std::vector<Ort::Value> ans;
ans.reserve(1);
std::vector<const Ort::Value *> buf;
buf.reserve(batch_size);
for (int32_t b = 0; b != batch_size; ++b) {
buf.push_back(&states[b][0]);
}
Ort::Value c{nullptr};
c = CatFloat16(allocator_, buf, 0);
ans.push_back(std::move(c));
return ans;
}
std::vector<std::vector<Ort::Value>> UnStackStates(
std::vector<Ort::Value> states) const {
auto allocator = const_cast<Impl *>(this)->allocator_;
std::vector<std::vector<Ort::Value>> ans;
auto shape = states[0].GetTensorTypeAndShapeInfo().GetShape();
int32_t batch_size = shape[0];
ans.resize(batch_size);
if (batch_size == 1) {
ans[0] = std::move(states);
return ans;
}
std::vector<Ort::Value> v;
v = UnbindFloat16(allocator, &states[0], 0);
for (int32_t b = 0; b != batch_size; ++b) {
ans[b].push_back(std::move(v[b]));
}
return ans;
}
private:
void Init(void *model_data, size_t model_data_length) {
sess_ = std::make_unique<Ort::Session>(env_, model_data, model_data_length,
sess_opts_);
GetInputNames(sess_.get(), &input_names_, &input_names_ptr_);
GetOutputNames(sess_.get(), &output_names_, &output_names_ptr_);
// get meta data
Ort::ModelMetadata meta_data = sess_->GetModelMetadata();
if (config_.debug) {
std::ostringstream os;
PrintModelMetadata(os, meta_data);
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
#else
SHERPA_ONNX_LOGE("%s", os.str().c_str());
#endif
}
Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
SHERPA_ONNX_READ_META_DATA(frame_length_ms_, "frame_length_ms");
SHERPA_ONNX_READ_META_DATA(state_dim_, "state_dim");
SHERPA_ONNX_READ_META_DATA(sample_rate_, "sample_rate");
InitStates();
vocab_size_ = sess_->GetOutputTypeInfo(0)
.GetTensorTypeAndShapeInfo()
.GetShape()
.back();
}
void InitStates() {
std::array<int64_t, 2> state_shape{1, state_dim_};
state_ = Ort::Value::CreateTensor(allocator_, state_shape.data(),
state_shape.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16);
auto p = state_.GetTensorMutableData<uint16_t>();
std::fill(p, p + state_dim_, 0);
}
private:
OnlineModelConfig config_;
Ort::Env env_;
Ort::SessionOptions sess_opts_;
Ort::AllocatorWithDefaultOptions allocator_;
std::unique_ptr<Ort::Session> sess_;
std::vector<std::string> input_names_;
std::vector<const char *> input_names_ptr_;
std::vector<std::string> output_names_;
std::vector<const char *> output_names_ptr_;
// One input frame is of length is 300ms
// For each input frame, there are 10 output frames,
// so each output frame is 30ms
int32_t frame_length_ms_ = 0;
int32_t state_dim_ = 0;
int32_t sample_rate_ = 0;
int32_t vocab_size_ = 0;
Ort::Value state_{nullptr};
};
OnlineToneCtcModel::OnlineToneCtcModel(const OnlineModelConfig &config)
: impl_(std::make_unique<Impl>(config)) {}
template <typename Manager>
OnlineToneCtcModel::OnlineToneCtcModel(Manager *mgr,
const OnlineModelConfig &config)
: impl_(std::make_unique<Impl>(mgr, config)) {}
OnlineToneCtcModel::~OnlineToneCtcModel() = default;
std::vector<Ort::Value> OnlineToneCtcModel::Forward(
Ort::Value x, std::vector<Ort::Value> states) const {
return impl_->Forward(std::move(x), std::move(states));
}
int32_t OnlineToneCtcModel::VocabSize() const { return impl_->VocabSize(); }
int32_t OnlineToneCtcModel::ChunkLength() const { return impl_->ChunkLength(); }
int32_t OnlineToneCtcModel::ChunkShift() const { return impl_->ChunkShift(); }
OrtAllocator *OnlineToneCtcModel::Allocator() const {
return impl_->Allocator();
}
std::vector<Ort::Value> OnlineToneCtcModel::GetInitStates() const {
return impl_->GetInitStates();
}
std::vector<Ort::Value> OnlineToneCtcModel::StackStates(
std::vector<std::vector<Ort::Value>> states) const {
return impl_->StackStates(std::move(states));
}
std::vector<std::vector<Ort::Value>> OnlineToneCtcModel::UnStackStates(
std::vector<Ort::Value> states) const {
return impl_->UnStackStates(std::move(states));
}
#if __ANDROID_API__ >= 9
template OnlineToneCtcModel::OnlineToneCtcModel(
AAssetManager *mgr, const OnlineModelConfig &config);
#endif
#if __OHOS__
template OnlineToneCtcModel::OnlineToneCtcModel(
NativeResourceManager *mgr, const OnlineModelConfig &config);
#endif
} // namespace sherpa_onnx