Skip to content

Commit

Permalink
Preserve previous result as context for next segment (#1335)
Browse files Browse the repository at this point in the history
Co-authored-by: vsd-vector <[email protected]>
  • Loading branch information
vsd-vector and vsd-vector authored Sep 11, 2024
1 parent ba7f1a7 commit fa20ae1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions sherpa-onnx/csrc/online-recognizer-transducer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,31 +360,40 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
}

void Reset(OnlineStream *s) const override {
int32_t context_size = model_->ContextSize();

{
// segment is incremented only when the last
// result is not empty
// result is not empty, contains non-blanks and longer than context_size)
const auto &r = s->GetResult();
if (!r.tokens.empty() && r.tokens.back() != 0) {
if (!r.tokens.empty() && r.tokens.back() != 0 && r.tokens.size() > context_size) {
s->GetCurrentSegment() += 1;
}
}

// reset encoder states
// s->SetStates(model_->GetEncoderInitStates());

// we keep the decoder_out
decoder_->UpdateDecoderOut(&s->GetResult());
Ort::Value decoder_out = std::move(s->GetResult().decoder_out);

auto r = decoder_->GetEmptyResult();
if (config_.decoding_method == "modified_beam_search" &&
nullptr != s->GetContextGraph()) {
for (auto it = r.hyps.begin(); it != r.hyps.end(); ++it) {
it->second.context_state = s->GetContextGraph()->Root();
}
}

auto last_result = s->GetResult();
// if last result is not empty, then
// preserve last tokens 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());

Hypotheses context_hyp({{context, 0}});
r.hyps = std::move(context_hyp);
r.tokens = std::move(context);
}

s->SetResult(r);
s->GetResult().decoder_out = std::move(decoder_out);

// Note: We only update counters. The underlying audio samples
// are not discarded.
Expand Down

0 comments on commit fa20ae1

Please sign in to comment.