Skip to content

Commit 25907d8

Browse files
author
Hang Lyu
committed
remove RecoverLastFrame()
1 parent b6abf43 commit 25907d8

File tree

4 files changed

+24
-148
lines changed

4 files changed

+24
-148
lines changed

src/decoder/lattice-faster-decoder-combine-bucketqueue.cc

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ bool LatticeFasterDecoderCombineTpl<FST, Token>::GetRawLattice(
218218
KALDI_ERR << "You cannot call FinalizeDecoding() and then call "
219219
<< "GetRawLattice() with use_final_probs == false";
220220

221-
std::unordered_map<Token*, BaseFloat> token_orig_cost;
222221
if (!decoding_finalized_) {
223222
// Process the non-emitting arcs for the unfinished last frame.
224-
ProcessNonemitting(&token_orig_cost);
223+
ProcessNonemitting();
225224
}
226225

227226

@@ -293,40 +292,9 @@ bool LatticeFasterDecoderCombineTpl<FST, Token>::GetRawLattice(
293292
}
294293
}
295294

296-
if (!decoding_finalized_) { // recover last token list
297-
RecoverLastTokenList(token_orig_cost);
298-
}
299295
return (ofst->NumStates() > 0);
300296
}
301297

302-
303-
// When GetRawLattice() is called during decoding, the
304-
// active_toks_[last_frame] is changed. To keep the consistency of function
305-
// ProcessForFrame(), recover it.
306-
// Notice: as new token will be added to the head of TokenList, tok->next
307-
// will not be affacted.
308-
template<typename FST, typename Token>
309-
void LatticeFasterDecoderCombineTpl<FST, Token>::RecoverLastTokenList(
310-
const std::unordered_map<Token*, BaseFloat> &token_orig_cost) {
311-
if (!token_orig_cost.empty()) {
312-
for (Token* tok = active_toks_[active_toks_.size() - 1].toks;
313-
tok != NULL;) {
314-
if (token_orig_cost.find(tok) != token_orig_cost.end()) {
315-
DeleteForwardLinks(tok);
316-
tok->tot_cost = token_orig_cost.find(tok)->second;
317-
tok->in_queue = false;
318-
tok = tok->next;
319-
} else {
320-
DeleteForwardLinks(tok);
321-
Token *next_tok = tok->next;
322-
delete tok;
323-
num_toks_--;
324-
tok = next_tok;
325-
}
326-
}
327-
}
328-
}
329-
330298
// This function is now deprecated, since now we do determinization from outside
331299
// the LatticeFasterDecoder class. Outputs an FST corresponding to the
332300
// lattice-determinized lattice (one path per word sequence).
@@ -756,7 +724,7 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::AdvanceDecoding(
756724
// tokens. This function used to be called PruneActiveTokensFinal().
757725
template <typename FST, typename Token>
758726
void LatticeFasterDecoderCombineTpl<FST, Token>::FinalizeDecoding() {
759-
ProcessNonemitting(NULL);
727+
ProcessNonemitting();
760728
int32 final_frame_plus_one = NumFramesDecoded();
761729
int32 num_toks_begin = num_toks_;
762730
// PruneForwardLinksFinal() prunes final frame (with final-probs), and
@@ -912,23 +880,8 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessForFrame(
912880

913881

914882
template <typename FST, typename Token>
915-
void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
916-
std::unordered_map<Token*, BaseFloat> *token_orig_cost) {
883+
void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting() {
917884
int32 frame = active_toks_.size() - 1;
918-
if (token_orig_cost) { // Build the elements which are used to recover
919-
for (Token *tok = active_toks_[frame].toks; tok != NULL; tok = tok->next) {
920-
(*token_orig_cost)[tok] = tok->tot_cost;
921-
}
922-
}
923-
924-
StateIdToTokenMap *tmp_toks;
925-
if (token_orig_cost) { // "token_orig_cost" isn't NULL. It means we need to
926-
// recover active_toks_[last_frame] and "cur_toks_"
927-
// will be used in the future.
928-
tmp_toks = new StateIdToTokenMap(cur_toks_);
929-
} else {
930-
tmp_toks = &cur_toks_;
931-
}
932885

933886
cur_queue_.Clear();
934887
for (Token* tok = active_toks_[frame].toks; tok != NULL; tok = tok->next) {
@@ -971,7 +924,7 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
971924
BaseFloat tot_cost = cur_cost + graph_cost;
972925
if (tot_cost < cur_cutoff) {
973926
Token *new_tok = FindOrAddToken(arc.nextstate, frame, tot_cost,
974-
tok, tmp_toks, &changed);
927+
tok, &cur_toks_, &changed);
975928

976929
// Add ForwardLink from tok to new_tok. Put it on the head of
977930
// tok->link list
@@ -987,7 +940,12 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
987940
}
988941
} // end of for loop
989942
} // end of while loop
990-
if (token_orig_cost) delete tmp_toks;
943+
if (!decoding_finalized_) {
944+
// Update cost_offsets_, it equals "- best_cost".
945+
cost_offsets_[frame] = adaptive_beam - cur_cutoff;
946+
// Needn't to update adaptive_beam_, since we still process this frame in
947+
// ProcessForFrame.
948+
}
991949
}
992950

993951

src/decoder/lattice-faster-decoder-combine-bucketqueue.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -540,28 +540,8 @@ class LatticeFasterDecoderCombineTpl {
540540
/// Processes nonemitting (epsilon) arcs for one frame.
541541
/// This function is called from FinalizeDecoding(), and also from
542542
/// GetRawLattice() if GetRawLattice() is called before FinalizeDecoding() is
543-
/// called. In the latter case, RecoverLastTokenList() is called later by
544-
/// GetRawLattice() to restore the state prior to ProcessNonemitting() being
545-
/// called, since ProcessForFrame() does not expect nonemitting arcs to
546-
/// already have been propagagted. ["token_orig_cost" isn't NULL in the
547-
/// latter case, we build the map which will be used to recover
548-
/// "active_toks_[last_frame]" token list for the last frame.]
549-
void ProcessNonemitting(
550-
std::unordered_map<Token*, BaseFloat> *token_orig_cost);
551-
552-
/// When GetRawLattice() is called during decoding, the
553-
/// active_toks_[last_frame] is changed. To keep the consistency of function
554-
/// ProcessForFrame(), recover it.
555-
/// Notice: as new token will be added to the head of TokenList, tok->next
556-
/// will not be affacted.
557-
/// "token_orig_cost" is a mapping from token pointer to the tot_cost of the
558-
/// token before propagating non-emitting arcs. It is used to recover the
559-
/// change of original tokens in the last frame and remove the new tokens
560-
/// which come from propagating non-emitting arcs, so that we can guarantee
561-
/// the consistency of function ProcessForFrame().
562-
void RecoverLastTokenList(
563-
const std::unordered_map<Token*, BaseFloat> &token_orig_cost);
564-
543+
/// called.
544+
void ProcessNonemitting();
565545

566546
/// The "prev_toks_" and "cur_toks_" actually allow us to maintain current
567547
/// and next frames. They are indexed by StateId. It is indexed by frame-index

src/decoder/lattice-faster-decoder-combine.cc

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ bool LatticeFasterDecoderCombineTpl<FST, Token>::GetRawLattice(
218218
KALDI_ERR << "You cannot call FinalizeDecoding() and then call "
219219
<< "GetRawLattice() with use_final_probs == false";
220220

221-
std::unordered_map<Token*, BaseFloat> token_orig_cost;
222221
if (!decoding_finalized_) {
223222
// Process the non-emitting arcs for the unfinished last frame.
224-
ProcessNonemitting(&token_orig_cost);
223+
ProcessNonemitting();
225224
}
226225

227226

@@ -293,40 +292,9 @@ bool LatticeFasterDecoderCombineTpl<FST, Token>::GetRawLattice(
293292
}
294293
}
295294

296-
if (!decoding_finalized_) { // recover last token list
297-
RecoverLastTokenList(token_orig_cost);
298-
}
299295
return (ofst->NumStates() > 0);
300296
}
301297

302-
303-
// When GetRawLattice() is called during decoding, the
304-
// active_toks_[last_frame] is changed. To keep the consistency of function
305-
// ProcessForFrame(), recover it.
306-
// Notice: as new token will be added to the head of TokenList, tok->next
307-
// will not be affacted.
308-
template<typename FST, typename Token>
309-
void LatticeFasterDecoderCombineTpl<FST, Token>::RecoverLastTokenList(
310-
const std::unordered_map<Token*, BaseFloat> &token_orig_cost) {
311-
if (!token_orig_cost.empty()) {
312-
for (Token* tok = active_toks_[active_toks_.size() - 1].toks;
313-
tok != NULL;) {
314-
if (token_orig_cost.find(tok) != token_orig_cost.end()) {
315-
DeleteForwardLinks(tok);
316-
tok->tot_cost = token_orig_cost.find(tok)->second;
317-
tok->in_queue = false;
318-
tok = tok->next;
319-
} else {
320-
DeleteForwardLinks(tok);
321-
Token *next_tok = tok->next;
322-
delete tok;
323-
num_toks_--;
324-
tok = next_tok;
325-
}
326-
}
327-
}
328-
}
329-
330298
// This function is now deprecated, since now we do determinization from outside
331299
// the LatticeFasterDecoder class. Outputs an FST corresponding to the
332300
// lattice-determinized lattice (one path per word sequence).
@@ -756,7 +724,7 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::AdvanceDecoding(
756724
// tokens. This function used to be called PruneActiveTokensFinal().
757725
template <typename FST, typename Token>
758726
void LatticeFasterDecoderCombineTpl<FST, Token>::FinalizeDecoding() {
759-
ProcessNonemitting(NULL);
727+
ProcessNonemitting();
760728
int32 final_frame_plus_one = NumFramesDecoded();
761729
int32 num_toks_begin = num_toks_;
762730
// PruneForwardLinksFinal() prunes final frame (with final-probs), and
@@ -912,23 +880,8 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessForFrame(
912880

913881

914882
template <typename FST, typename Token>
915-
void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
916-
std::unordered_map<Token*, BaseFloat> *token_orig_cost) {
883+
void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting() {
917884
int32 frame = active_toks_.size() - 1;
918-
if (token_orig_cost) { // Build the elements which are used to recover
919-
for (Token *tok = active_toks_[frame].toks; tok != NULL; tok = tok->next) {
920-
(*token_orig_cost)[tok] = tok->tot_cost;
921-
}
922-
}
923-
924-
StateIdToTokenMap *tmp_toks;
925-
if (token_orig_cost) { // "token_orig_cost" isn't NULL. It means we need to
926-
// recover active_toks_[last_frame] and "cur_toks_"
927-
// will be used in the future.
928-
tmp_toks = new StateIdToTokenMap(cur_toks_);
929-
} else {
930-
tmp_toks = &cur_toks_;
931-
}
932885

933886
cur_queue_.Clear();
934887
for (Token* tok = active_toks_[frame].toks; tok != NULL; tok = tok->next) {
@@ -971,7 +924,7 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
971924
BaseFloat tot_cost = cur_cost + graph_cost;
972925
if (tot_cost < cur_cutoff) {
973926
Token *new_tok = FindOrAddToken(arc.nextstate, frame, tot_cost,
974-
tok, tmp_toks, &changed);
927+
tok, &cur_toks_, &changed);
975928

976929
// Add ForwardLink from tok to new_tok. Put it on the head of
977930
// tok->link list
@@ -987,7 +940,12 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessNonemitting(
987940
}
988941
} // end of for loop
989942
} // end of while loop
990-
if (token_orig_cost) delete tmp_toks;
943+
if (!decoding_finalized_) {
944+
// Update cost_offsets_, it equals "- best_cost".
945+
cost_offsets_[frame] = adaptive_beam - cur_cutoff;
946+
// Needn't to update adaptive_beam_, since we still process this frame in
947+
// ProcessForFrame.
948+
}
991949
}
992950

993951

src/decoder/lattice-faster-decoder-combine.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -540,28 +540,8 @@ class LatticeFasterDecoderCombineTpl {
540540
/// Processes nonemitting (epsilon) arcs for one frame.
541541
/// This function is called from FinalizeDecoding(), and also from
542542
/// GetRawLattice() if GetRawLattice() is called before FinalizeDecoding() is
543-
/// called. In the latter case, RecoverLastTokenList() is called later by
544-
/// GetRawLattice() to restore the state prior to ProcessNonemitting() being
545-
/// called, since ProcessForFrame() does not expect nonemitting arcs to
546-
/// already have been propagagted. ["token_orig_cost" isn't NULL in the
547-
/// latter case, we build the map which will be used to recover
548-
/// "active_toks_[last_frame]" token list for the last frame.]
549-
void ProcessNonemitting(
550-
std::unordered_map<Token*, BaseFloat> *token_orig_cost);
551-
552-
/// When GetRawLattice() is called during decoding, the
553-
/// active_toks_[last_frame] is changed. To keep the consistency of function
554-
/// ProcessForFrame(), recover it.
555-
/// Notice: as new token will be added to the head of TokenList, tok->next
556-
/// will not be affacted.
557-
/// "token_orig_cost" is a mapping from token pointer to the tot_cost of the
558-
/// token before propagating non-emitting arcs. It is used to recover the
559-
/// change of original tokens in the last frame and remove the new tokens
560-
/// which come from propagating non-emitting arcs, so that we can guarantee
561-
/// the consistency of function ProcessForFrame().
562-
void RecoverLastTokenList(
563-
const std::unordered_map<Token*, BaseFloat> &token_orig_cost);
564-
543+
/// called.
544+
void ProcessNonemitting();
565545

566546
/// The "prev_toks_" and "cur_toks_" actually allow us to maintain current
567547
/// and next frames. They are indexed by StateId. It is indexed by frame-index

0 commit comments

Comments
 (0)