This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex_bi_series-sherpa-onnx/sherpa-onnx/csrc/online-transducer-greedy-search-decoder.cc

121 lines
3.7 KiB
C++
Raw Normal View History

2023-02-19 19:36:03 +08:00
// sherpa-onnx/csrc/online-transducer-greedy-search-decoder.cc
2023-02-19 10:39:07 +08:00
//
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/online-transducer-greedy-search-decoder.h"
#include <assert.h>
#include <algorithm>
#include <utility>
#include <vector>
#include "sherpa-onnx/csrc/macros.h"
2023-02-19 10:39:07 +08:00
#include "sherpa-onnx/csrc/onnx-utils.h"
namespace sherpa_onnx {
static Ort::Value GetFrame(OrtAllocator *allocator, Ort::Value *encoder_out,
int32_t t) {
2023-02-19 10:39:07 +08:00
std::vector<int64_t> encoder_out_shape =
encoder_out->GetTensorTypeAndShapeInfo().GetShape();
auto batch_size = encoder_out_shape[0];
auto num_frames = encoder_out_shape[1];
assert(t < num_frames);
2023-02-19 10:39:07 +08:00
auto encoder_out_dim = encoder_out_shape[2];
2023-02-19 10:39:07 +08:00
auto offset = num_frames * encoder_out_dim;
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
std::array<int64_t, 2> shape{batch_size, encoder_out_dim};
Ort::Value ans =
Ort::Value::CreateTensor<float>(allocator, shape.data(), shape.size());
float *dst = ans.GetTensorMutableData<float>();
const float *src = encoder_out->GetTensorData<float>();
for (int32_t i = 0; i != batch_size; ++i) {
std::copy(src + t * encoder_out_dim, src + (t + 1) * encoder_out_dim, dst);
src += offset;
dst += encoder_out_dim;
}
return ans;
}
2023-02-19 10:39:07 +08:00
OnlineTransducerDecoderResult
2023-02-19 12:45:38 +08:00
OnlineTransducerGreedySearchDecoder::GetEmptyResult() const {
2023-02-19 10:39:07 +08:00
int32_t context_size = model_->ContextSize();
int32_t blank_id = 0; // always 0
OnlineTransducerDecoderResult r;
r.tokens.resize(context_size, blank_id);
return r;
}
void OnlineTransducerGreedySearchDecoder::StripLeadingBlanks(
2023-02-19 12:45:38 +08:00
OnlineTransducerDecoderResult *r) const {
2023-02-19 10:39:07 +08:00
int32_t context_size = model_->ContextSize();
auto start = r->tokens.begin() + context_size;
auto end = r->tokens.end();
r->tokens = std::vector<int64_t>(start, end);
}
void OnlineTransducerGreedySearchDecoder::Decode(
Ort::Value encoder_out,
std::vector<OnlineTransducerDecoderResult> *result) {
std::vector<int64_t> encoder_out_shape =
encoder_out.GetTensorTypeAndShapeInfo().GetShape();
if (encoder_out_shape[0] != result->size()) {
SHERPA_ONNX_LOGE(
"Size mismatch! encoder_out.size(0) %d, result.size(0): %d",
static_cast<int32_t>(encoder_out_shape[0]),
static_cast<int32_t>(result->size()));
2023-02-19 10:39:07 +08:00
exit(-1);
}
int32_t batch_size = static_cast<int32_t>(encoder_out_shape[0]);
int32_t num_frames = static_cast<int32_t>(encoder_out_shape[1]);
2023-02-19 10:39:07 +08:00
int32_t vocab_size = model_->VocabSize();
Ort::Value decoder_input = model_->BuildDecoderInput(*result);
2023-02-19 10:39:07 +08:00
Ort::Value decoder_out = model_->RunDecoder(std::move(decoder_input));
for (int32_t t = 0; t != num_frames; ++t) {
Ort::Value cur_encoder_out = GetFrame(model_->Allocator(), &encoder_out, t);
2023-02-26 14:54:01 +08:00
Ort::Value logit = model_->RunJoiner(
std::move(cur_encoder_out), Clone(model_->Allocator(), &decoder_out));
2023-02-19 10:39:07 +08:00
const float *p_logit = logit.GetTensorData<float>();
bool emitted = false;
for (int32_t i = 0; i < batch_size; ++i, p_logit += vocab_size) {
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 != 0) {
emitted = true;
(*result)[i].tokens.push_back(y);
2023-02-22 15:35:55 +08:00
(*result)[i].num_trailing_blanks = 0;
} else {
++(*result)[i].num_trailing_blanks;
}
}
if (emitted) {
decoder_input = model_->BuildDecoderInput(*result);
2023-02-19 10:39:07 +08:00
decoder_out = model_->RunDecoder(std::move(decoder_input));
}
}
}
} // namespace sherpa_onnx