LatticeIncrementalOnlineDecoderTpl< FST > Class Template Reference

LatticeIncrementalOnlineDecoderTpl is as LatticeIncrementalDecoderTpl but also supports an efficient way to get the best path (see the function BestPathEnd()), which is useful in endpointing and in situations where you might want to frequently access the best path. More...

#include <lattice-incremental-online-decoder.h>

Inheritance diagram for LatticeIncrementalOnlineDecoderTpl< FST >:
Collaboration diagram for LatticeIncrementalOnlineDecoderTpl< FST >:

Classes

struct  BestPathIterator
 

Public Types

using Arc = typename FST::Arc
 
using Label = typename Arc::Label
 
using StateId = typename Arc::StateId
 
using Weight = typename Arc::Weight
 
using Token = decoder::BackpointerToken
 
using ForwardLinkT = decoder::ForwardLink< Token >
 
- Public Types inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
using Arc = typename FST::Arc
 
using Label = typename Arc::Label
 
using StateId = typename Arc::StateId
 
using Weight = typename Arc::Weight
 
using ForwardLinkT = decoder::ForwardLink< decoder::BackpointerToken >
 

Public Member Functions

 LatticeIncrementalOnlineDecoderTpl (const FST &fst, const TransitionModel &trans_model, const LatticeIncrementalDecoderConfig &config)
 
 LatticeIncrementalOnlineDecoderTpl (const LatticeIncrementalDecoderConfig &config, FST *fst, const TransitionModel &trans_model)
 
bool GetBestPath (Lattice *ofst, bool use_final_probs=true) const
 Outputs an FST corresponding to the single best path through the lattice. More...
 
BestPathIterator BestPathEnd (bool use_final_probs, BaseFloat *final_cost=NULL) const
 This function returns an iterator that can be used to trace back the best path. More...
 
BestPathIterator TraceBackBestPath (BestPathIterator iter, LatticeArc *arc) const
 This function can be used in conjunction with BestPathEnd() to trace back the best path one link at a time (e.g. More...
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (LatticeIncrementalOnlineDecoderTpl)
 
- Public Member Functions inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
 LatticeIncrementalDecoderTpl (const FST &fst, const TransitionModel &trans_model, const LatticeIncrementalDecoderConfig &config)
 
 LatticeIncrementalDecoderTpl (const LatticeIncrementalDecoderConfig &config, FST *fst, const TransitionModel &trans_model)
 
void SetOptions (const LatticeIncrementalDecoderConfig &config)
 
const LatticeIncrementalDecoderConfigGetOptions () const
 
 ~LatticeIncrementalDecoderTpl ()
 
bool Decode (DecodableInterface *decodable)
 CAUTION: it's unlikely that you will ever want to call this function. More...
 
bool ReachedFinal () const
 says whether a final-state was active on the last frame. More...
 
const CompactLatticeGetLattice (int32 num_frames_to_include, bool use_final_probs=false)
 This decoder has no GetBestPath() function. More...
 
int NumFramesInLattice () const
 
void InitDecoding ()
 InitDecoding initializes the decoding, and should only be used if you intend to call AdvanceDecoding(). More...
 
void AdvanceDecoding (DecodableInterface *decodable, int32 max_num_frames=-1)
 This will decode until there are no more frames ready in the decodable object. More...
 
BaseFloat FinalRelativeCost () const
 FinalRelativeCost() serves the same purpose as ReachedFinal(), but gives more information. More...
 
int32 NumFramesDecoded () const
 Returns the number of frames decoded so far. More...
 
void FinalizeDecoding ()
 Finalizes the decoding, doing an extra pruning step on the last frame that uses the final-probs. More...
 

Additional Inherited Members

- Protected Types inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
using Elem = typename HashList< StateId, decoder::BackpointerToken *>::Elem
 
- Protected Member Functions inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
void PossiblyResizeHash (size_t num_toks)
 
decoder::BackpointerTokenFindOrAddToken (StateId state, int32 frame_plus_one, BaseFloat tot_cost, decoder::BackpointerToken *backpointer, bool *changed)
 
void PruneForwardLinks (int32 frame_plus_one, bool *extra_costs_changed, bool *links_pruned, BaseFloat delta)
 
void ComputeFinalCosts (unordered_map< decoder::BackpointerToken *, BaseFloat > *final_costs, BaseFloat *final_relative_cost, BaseFloat *final_best_cost) const
 
void PruneForwardLinksFinal ()
 
void PruneTokensForFrame (int32 frame_plus_one)
 
void PruneActiveTokens (BaseFloat delta)
 
BaseFloat GetCutoff (Elem *list_head, size_t *tok_count, BaseFloat *adaptive_beam, Elem **best_elem)
 Gets the weight cutoff. Also counts the active tokens. More...
 
BaseFloat ProcessEmitting (DecodableInterface *decodable)
 
void ProcessNonemitting (BaseFloat cost_cutoff)
 
Label AllocateNewTokenLabel ()
 
void DeleteElems (Elem *list)
 
void ClearActiveTokens ()
 
int32 GetNumToksForFrame (int32 frame)
 
void UpdateLatticeDeterminization ()
 UpdateLatticeDeterminization() ensures the work of determinization is kept up to date so that when you do need the lattice you can get it fast. More...
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (LatticeIncrementalDecoderTpl)
 
- Static Protected Member Functions inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
static void DeleteForwardLinks (decoder::BackpointerToken *tok)
 NOTE: for parts the internal implementation that are shared with LatticeFasterDecoer, we have removed the comments. More...
 
- Protected Attributes inherited from LatticeIncrementalDecoderTpl< FST, decoder::BackpointerToken >
HashList< StateId, decoder::BackpointerToken *> toks_
 
std::vector< TokenList > active_toks_
 
std::vector< StateIdqueue_
 
std::vector< BaseFloattmp_array_
 
const FST * fst_
 
bool delete_fst_
 
std::vector< BaseFloatcost_offsets_
 
int32 num_toks_
 
bool warned_
 
bool decoding_finalized_
 
unordered_map< decoder::BackpointerToken *, BaseFloatfinal_costs_
 
BaseFloat final_relative_cost_
 
BaseFloat final_best_cost_
 
LatticeIncrementalDecoderConfig config_
 
LatticeIncrementalDeterminizer determinizer_
 Much of the the incremental determinization algorithm is encapsulated in the determinize_ object. More...
 
unordered_map< decoder::BackpointerToken *, StateIdtemp_token_map_
 
int32 num_frames_in_lattice_
 num_frames_in_lattice_ is the highest `num_frames_to_include_` argument for any prior call to GetLattice(). More...
 
unordered_map< decoder::BackpointerToken *, Labeltoken2label_map_
 
unordered_map< decoder::BackpointerToken *, Labeltoken2label_map_temp_
 
Label next_token_label_
 

Detailed Description

template<typename FST>
class kaldi::LatticeIncrementalOnlineDecoderTpl< FST >

LatticeIncrementalOnlineDecoderTpl is as LatticeIncrementalDecoderTpl but also supports an efficient way to get the best path (see the function BestPathEnd()), which is useful in endpointing and in situations where you might want to frequently access the best path.

This is only templated on the FST type, since the Token type is required to be BackpointerToken. Actually it only makes sense to instantiate LatticeIncrementalDecoderTpl with Token == BackpointerToken if you do so indirectly via this child class.

Definition at line 51 of file lattice-incremental-online-decoder.h.

Member Typedef Documentation

◆ Arc

using Arc = typename FST::Arc

Definition at line 54 of file lattice-incremental-online-decoder.h.

◆ ForwardLinkT

◆ Label

using Label = typename Arc::Label

Definition at line 55 of file lattice-incremental-online-decoder.h.

◆ StateId

using StateId = typename Arc::StateId

Definition at line 56 of file lattice-incremental-online-decoder.h.

◆ Token

◆ Weight

using Weight = typename Arc::Weight

Definition at line 57 of file lattice-incremental-online-decoder.h.

Constructor & Destructor Documentation

◆ LatticeIncrementalOnlineDecoderTpl() [1/2]

LatticeIncrementalOnlineDecoderTpl ( const FST &  fst,
const TransitionModel trans_model,
const LatticeIncrementalDecoderConfig config 
)
inline

Definition at line 64 of file lattice-incremental-online-decoder.h.

66  :
67  LatticeIncrementalDecoderTpl<FST, Token>(fst, trans_model, config) { }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ LatticeIncrementalOnlineDecoderTpl() [2/2]

LatticeIncrementalOnlineDecoderTpl ( const LatticeIncrementalDecoderConfig config,
FST *  fst,
const TransitionModel trans_model 
)
inline

Definition at line 71 of file lattice-incremental-online-decoder.h.

73  :
74  LatticeIncrementalDecoderTpl<FST, Token>(config, fst, trans_model) { }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

Member Function Documentation

◆ BestPathEnd()

LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator BestPathEnd ( bool  use_final_probs,
BaseFloat final_cost = NULL 
) const

This function returns an iterator that can be used to trace back the best path.

If use_final_probs == true and at least one final state survived till the end, it will use the final-probs in working out the best final Token, and will output the final cost to *final_cost (if non-NULL), else it will use only the forward likelihood, and will put zero in *final_cost (if non-NULL). Requires that NumFramesDecoded() > 0.

Definition at line 54 of file lattice-incremental-online-decoder.cc.

References KALDI_ASSERT, KALDI_ERR, KALDI_WARN, and BackpointerToken::next.

Referenced by OnlineSilenceWeighting::ComputeCurrentTraceback(), and LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::Done().

56  {
57  if (this->decoding_finalized_ && !use_final_probs)
58  KALDI_ERR << "You cannot call FinalizeDecoding() and then call "
59  << "BestPathEnd() with use_final_probs == false";
60  KALDI_ASSERT(this->NumFramesDecoded() > 0 &&
61  "You cannot call BestPathEnd if no frames were decoded.");
62 
63  unordered_map<Token*, BaseFloat> final_costs_local;
64 
65  const unordered_map<Token*, BaseFloat> &final_costs =
66  (this->decoding_finalized_ ? this->final_costs_ :final_costs_local);
67  if (!this->decoding_finalized_ && use_final_probs)
68  this->ComputeFinalCosts(&final_costs_local, NULL, NULL);
69 
70  // Singly linked list of tokens on last frame (access list through "next"
71  // pointer).
72  BaseFloat best_cost = std::numeric_limits<BaseFloat>::infinity();
73  BaseFloat best_final_cost = 0;
74  Token *best_tok = NULL;
75  for (Token *tok = this->active_toks_.back().toks;
76  tok != NULL; tok = tok->next) {
77  BaseFloat cost = tok->tot_cost, final_cost = 0.0;
78  if (use_final_probs && !final_costs.empty()) {
79  // if we are instructed to use final-probs, and any final tokens were
80  // active on final frame, include the final-prob in the cost of the token.
81  typename unordered_map<Token*, BaseFloat>::const_iterator
82  iter = final_costs.find(tok);
83  if (iter != final_costs.end()) {
84  final_cost = iter->second;
85  cost += final_cost;
86  } else {
87  cost = std::numeric_limits<BaseFloat>::infinity();
88  }
89  }
90  if (cost < best_cost) {
91  best_cost = cost;
92  best_tok = tok;
93  best_final_cost = final_cost;
94  }
95  }
96  if (best_tok == NULL) { // this should not happen, and is likely a code error or
97  // caused by infinities in likelihoods, but I'm not making
98  // it a fatal error for now.
99  KALDI_WARN << "No final token found.";
100  }
101  if (final_cost_out == NULL)
102  *final_cost_out = best_final_cost;
103  return BestPathIterator(best_tok, this->NumFramesDecoded() - 1);
104 }
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 NumFramesDecoded() const
Returns the number of frames decoded so far.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
unordered_map< decoder::BackpointerToken *, BaseFloat > final_costs_
void ComputeFinalCosts(unordered_map< decoder::BackpointerToken *, BaseFloat > *final_costs, BaseFloat *final_relative_cost, BaseFloat *final_best_cost) const

◆ GetBestPath()

bool GetBestPath ( Lattice ofst,
bool  use_final_probs = true 
) const

Outputs an FST corresponding to the single best path through the lattice.

This is quite efficient because it doesn't get the entire raw lattice and find the best path through it; instead, it uses the BestPathEnd and BestPathIterator so it basically traces it back through the lattice. Returns true if result is nonempty (using the return status is deprecated, it will become void). If "use_final_probs" is true AND we reached the final-state of the graph then it will include those as final-probs, else it will treat all final-probs as one.

Definition at line 32 of file lattice-incremental-online-decoder.cc.

References LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::Done().

Referenced by LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::Done().

33  {
34  olat->DeleteStates();
35  BaseFloat final_graph_cost;
36  BestPathIterator iter = BestPathEnd(use_final_probs, &final_graph_cost);
37  if (iter.Done())
38  return false; // would have printed warning.
39  StateId state = olat->AddState();
40  olat->SetFinal(state, LatticeWeight(final_graph_cost, 0.0));
41  while (!iter.Done()) {
42  LatticeArc arc;
43  iter = TraceBackBestPath(iter, &arc);
44  arc.nextstate = state;
45  StateId new_state = olat->AddState();
46  olat->AddArc(new_state, arc);
47  state = new_state;
48  }
49  olat->SetStart(state);
50  return true;
51 }
fst::ArcTpl< LatticeWeight > LatticeArc
Definition: kaldi-lattice.h:40
BestPathIterator BestPathEnd(bool use_final_probs, BaseFloat *final_cost=NULL) const
This function returns an iterator that can be used to trace back the best path.
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
float BaseFloat
Definition: kaldi-types.h:29
BestPathIterator TraceBackBestPath(BestPathIterator iter, LatticeArc *arc) const
This function can be used in conjunction with BestPathEnd() to trace back the best path one link at a...

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

◆ TraceBackBestPath()

LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator TraceBackBestPath ( BestPathIterator  iter,
LatticeArc arc 
) const

This function can be used in conjunction with BestPathEnd() to trace back the best path one link at a time (e.g.

this can be useful in endpoint detection). By "link" we mean a link in the graph; not all links cross frame boundaries, but each time you see a nonzero ilabel you can interpret that as a frame. The return value is the updated iterator. It outputs the ilabel and olabel, and the (graph and acoustic) weight to the "arc" pointer, while leaving its "nextstate" variable unchanged.

Definition at line 108 of file lattice-incremental-online-decoder.cc.

References ForwardLink< Token >::acoustic_cost, LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::Done(), LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::frame, ForwardLink< Token >::graph_cost, ForwardLink< Token >::ilabel, KALDI_ASSERT, KALDI_ERR, ForwardLink< Token >::next, ForwardLink< Token >::next_tok, ForwardLink< Token >::olabel, LatticeWeightTpl< BaseFloat >::One(), and LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::tok.

Referenced by OnlineSilenceWeighting::ComputeCurrentTraceback(), and LatticeIncrementalOnlineDecoderTpl< FST >::BestPathIterator::Done().

109  {
110  KALDI_ASSERT(!iter.Done() && oarc != NULL);
111  Token *tok = static_cast<Token*>(iter.tok);
112  int32 cur_t = iter.frame, ret_t = cur_t;
113  if (tok->backpointer != NULL) {
114  ForwardLinkT *link;
115  for (link = tok->backpointer->links;
116  link != NULL; link = link->next) {
117  if (link->next_tok == tok) { // this is the link to "tok"
118  oarc->ilabel = link->ilabel;
119  oarc->olabel = link->olabel;
120  BaseFloat graph_cost = link->graph_cost,
121  acoustic_cost = link->acoustic_cost;
122  if (link->ilabel != 0) {
123  KALDI_ASSERT(static_cast<size_t>(cur_t) < this->cost_offsets_.size());
124  acoustic_cost -= this->cost_offsets_[cur_t];
125  ret_t--;
126  }
127  oarc->weight = LatticeWeight(graph_cost, acoustic_cost);
128  break;
129  }
130  }
131  if (link == NULL) { // Did not find correct link.
132  KALDI_ERR << "Error tracing best-path back (likely "
133  << "bug in token-pruning algorithm)";
134  }
135  } else {
136  oarc->ilabel = 0;
137  oarc->olabel = 0;
138  oarc->weight = LatticeWeight::One(); // zero costs.
139  }
140  return BestPathIterator(tok->backpointer, ret_t);
141 }
static const LatticeWeightTpl One()
kaldi::int32 int32
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

The documentation for this class was generated from the following files: