Skip to content

Preserve more context after endpointing in transducer #2061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 2, 2025
Merged
16 changes: 10 additions & 6 deletions sherpa-onnx/csrc/online-recognizer-transducer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,20 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
auto r = decoder_->GetEmptyResult();
auto last_result = s->GetResult();
// if last result is not empty, then
// preserve last tokens as the context for next result
// truncate all last hyps and save as the context for next result
if (static_cast<int32_t>(last_result.tokens.size()) > context_size) {
std::vector<int64_t> context(last_result.tokens.end() - context_size,
last_result.tokens.end());
for (const auto &it : last_result.hyps) {
auto h = it.second;
r.hyps.Add({std::vector<int64_t>(h.ys.end() - context_size,
h.ys.end()),
h.log_prob});
}

Hypotheses context_hyp({{context, 0}});
r.hyps = std::move(context_hyp);
r.tokens = std::move(context);
r.tokens = std::vector<int64_t> (last_result.tokens.end() - context_size,
last_result.tokens.end());
}

// but reset all contextual biasing graph states to root
if (config_.decoding_method == "modified_beam_search" &&
nullptr != s->GetContextGraph()) {
for (auto it = r.hyps.begin(); it != r.hyps.end(); ++it) {
Expand Down
Loading