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-decoder.h

112 lines
3.5 KiB
C
Raw Normal View History

2023-02-19 19:36:03 +08:00
// sherpa-onnx/csrc/online-transducer-decoder.h
2023-02-19 10:39:07 +08:00
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_ONLINE_TRANSDUCER_DECODER_H_
#define SHERPA_ONNX_CSRC_ONLINE_TRANSDUCER_DECODER_H_
#include <vector>
#include "onnxruntime_cxx_api.h" // NOLINT
2023-03-01 15:32:54 +08:00
#include "sherpa-onnx/csrc/hypothesis.h"
#include "sherpa-onnx/csrc/macros.h"
2023-02-19 10:39:07 +08:00
namespace sherpa_onnx {
struct OnlineTransducerDecoderResult {
/// Number of frames after subsampling we have decoded so far
int32_t frame_offset = 0;
2023-02-19 10:39:07 +08:00
/// The decoded token IDs so far
std::vector<int64_t> tokens;
2023-02-22 15:35:55 +08:00
/// number of trailing blank frames decoded so far
int32_t num_trailing_blanks = 0;
2023-03-01 15:32:54 +08:00
/// timestamps[i] contains the output frame index where tokens[i] is decoded.
std::vector<int32_t> timestamps;
std::vector<float> ys_probs;
std::vector<float> lm_probs;
std::vector<float> context_scores;
// Cache decoder_out for endpointing
Ort::Value decoder_out;
2023-03-01 15:32:54 +08:00
// used only in modified beam_search
Hypotheses hyps;
OnlineTransducerDecoderResult()
: tokens{}, num_trailing_blanks(0), decoder_out{nullptr}, hyps{} {}
OnlineTransducerDecoderResult(const OnlineTransducerDecoderResult &other);
OnlineTransducerDecoderResult &operator=(
const OnlineTransducerDecoderResult &other);
2024-06-19 20:51:57 +08:00
OnlineTransducerDecoderResult(OnlineTransducerDecoderResult &&other) noexcept;
OnlineTransducerDecoderResult &operator=(
2024-06-19 20:51:57 +08:00
OnlineTransducerDecoderResult &&other) noexcept;
2023-02-19 10:39:07 +08:00
};
class OnlineStream;
2023-02-19 10:39:07 +08:00
class OnlineTransducerDecoder {
public:
virtual ~OnlineTransducerDecoder() = default;
/* Return an empty result.
*
* To simplify the decoding code, we add `context_size` blanks
* to the beginning of the decoding result, which will be
* stripped by calling `StripPrecedingBlanks()`.
*/
2023-02-19 12:45:38 +08:00
virtual OnlineTransducerDecoderResult GetEmptyResult() const = 0;
2023-02-19 10:39:07 +08:00
/** Strip blanks added by `GetEmptyResult()`.
*
* @param r It is changed in-place.
*/
2023-02-19 12:45:38 +08:00
virtual void StripLeadingBlanks(OnlineTransducerDecoderResult * /*r*/) const {
}
2023-02-19 10:39:07 +08:00
/** Run transducer beam search given the output from the encoder model.
*
* @param encoder_out A 3-D tensor of shape (N, T, joiner_dim)
* @param result It is modified in-place.
*
* @note There is no need to pass encoder_out_length here since for the
* online decoding case, each utterance has the same number of frames
* and there are no paddings.
*/
virtual void Decode(Ort::Value encoder_out,
std::vector<OnlineTransducerDecoderResult> *result) = 0;
/** Run transducer beam search given the output from the encoder model.
*
* Note: Currently this interface is for contextual-biasing feature which
* needs a ContextGraph owned by the OnlineStream.
*
* @param encoder_out A 3-D tensor of shape (N, T, joiner_dim)
* @param ss A list of OnlineStreams.
* @param result It is modified in-place.
*
* @note There is no need to pass encoder_out_length here since for the
* online decoding case, each utterance has the same number of frames
* and there are no paddings.
*/
virtual void Decode(Ort::Value /*encoder_out*/, OnlineStream ** /*ss*/,
std::vector<OnlineTransducerDecoderResult> * /*result*/) {
SHERPA_ONNX_LOGE(
"This interface is for OnlineTransducerModifiedBeamSearchDecoder.");
exit(-1);
}
// used for endpointing. We need to keep decoder_out after reset
virtual void UpdateDecoderOut(OnlineTransducerDecoderResult * /*result*/) {}
2023-02-19 10:39:07 +08:00
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_ONLINE_TRANSDUCER_DECODER_H_