Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly optimized decoders. More...
#include <lattice-simple-decoder.h>
Classes | |
struct | ForwardLink |
struct | Token |
struct | TokenList |
Public Types | |
typedef fst::StdArc | Arc |
typedef Arc::Label | Label |
typedef Arc::StateId | StateId |
typedef Arc::Weight | Weight |
Public Member Functions | |
LatticeSimpleDecoder (const fst::Fst< fst::StdArc > &fst, const LatticeSimpleDecoderConfig &config) | |
~LatticeSimpleDecoder () | |
const LatticeSimpleDecoderConfig & | GetOptions () const |
bool | Decode (DecodableInterface *decodable) |
bool | ReachedFinal () const |
says whether a final-state was active on the last frame. More... | |
void | InitDecoding () |
InitDecoding initializes the decoding, and should only be used if you intend to call AdvanceDecoding(). More... | |
void | FinalizeDecoding () |
This function may be optionally called after AdvanceDecoding(), when you do not plan to decode any further. More... | |
BaseFloat | FinalRelativeCost () const |
FinalRelativeCost() serves the same purpose as ReachedFinal(), but gives more information. More... | |
bool | GetBestPath (Lattice *lat, bool use_final_probs=true) const |
bool | GetRawLattice (Lattice *lat, bool use_final_probs=true) const |
bool | GetLattice (CompactLattice *clat, bool use_final_probs=true) const |
int32 | NumFramesDecoded () const |
Private Member Functions | |
Token * | FindOrAddToken (StateId state, int32 frame_plus_one, BaseFloat tot_cost, bool emitting, bool *changed) |
void | PruneForwardLinks (int32 frame, bool *extra_costs_changed, bool *links_pruned, BaseFloat delta) |
void | PruneForwardLinksFinal () |
void | PruneTokensForFrame (int32 frame) |
void | PruneActiveTokens (BaseFloat delta) |
void | ProcessEmitting (DecodableInterface *decodable) |
void | ProcessNonemitting () |
void | ClearActiveTokens () |
void | ComputeFinalCosts (unordered_map< Token *, BaseFloat > *final_costs, BaseFloat *final_relative_cost, BaseFloat *final_best_cost) const |
void | PruneCurrentTokens (BaseFloat beam, unordered_map< StateId, Token *> *toks) |
Private Attributes | |
unordered_map< StateId, Token * > | cur_toks_ |
unordered_map< StateId, Token * > | prev_toks_ |
std::vector< TokenList > | active_toks_ |
const fst::Fst< fst::StdArc > & | fst_ |
LatticeSimpleDecoderConfig | config_ |
int32 | num_toks_ |
bool | warned_ |
bool | decoding_finalized_ |
decoding_finalized_ is true if someone called FinalizeDecoding(). More... | |
unordered_map< Token *, BaseFloat > | final_costs_ |
For the meaning of the next 3 variables, see the comment for decoding_finalized_ above., and ComputeFinalCosts(). More... | |
BaseFloat | final_relative_cost_ |
BaseFloat | final_best_cost_ |
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly optimized decoders.
See SimpleDecoder: the simplest possible decoder for more information.
Definition at line 76 of file lattice-simple-decoder.h.
typedef fst::StdArc Arc |
Definition at line 78 of file lattice-simple-decoder.h.
typedef Arc::Label Label |
Definition at line 79 of file lattice-simple-decoder.h.
typedef Arc::StateId StateId |
Definition at line 80 of file lattice-simple-decoder.h.
typedef Arc::Weight Weight |
Definition at line 81 of file lattice-simple-decoder.h.
|
inline |
Definition at line 83 of file lattice-simple-decoder.h.
References LatticeSimpleDecoderConfig::Check().
|
inline |
Definition at line 87 of file lattice-simple-decoder.h.
|
private |
Definition at line 617 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, rnnlm::i, KALDI_ASSERT, LatticeSimpleDecoder::Token::next, and LatticeSimpleDecoder::num_toks_.
Referenced by LatticeSimpleDecoder::InitDecoding().
|
private |
Definition at line 292 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::decoding_finalized_, LatticeSimpleDecoder::fst_, KALDI_ASSERT, and LatticeSimpleDecoder::Token::tot_cost.
Referenced by LatticeSimpleDecoder::FinalRelativeCost(), LatticeSimpleDecoder::GetRawLattice(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
bool Decode | ( | DecodableInterface * | decodable | ) |
Definition at line 46 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoderConfig::beam, LatticeSimpleDecoder::config_, LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::final_costs_, LatticeSimpleDecoder::FinalizeDecoding(), LatticeSimpleDecoder::InitDecoding(), DecodableInterface::IsLastFrame(), LatticeSimpleDecoderConfig::lattice_beam, LatticeSimpleDecoder::NumFramesDecoded(), LatticeSimpleDecoder::ProcessEmitting(), LatticeSimpleDecoder::ProcessNonemitting(), LatticeSimpleDecoderConfig::prune_interval, LatticeSimpleDecoderConfig::prune_scale, LatticeSimpleDecoder::PruneActiveTokens(), and LatticeSimpleDecoder::PruneCurrentTokens().
Referenced by kaldi::DecodeUtteranceLatticeSimple().
void FinalizeDecoding | ( | ) |
This function may be optionally called after AdvanceDecoding(), when you do not plan to decode any further.
It does an extra pruning step that will help to prune the lattices output by GetLattice and (particularly) GetRawLattice more accurately, particularly toward the end of the utterance. It does this by using the final-probs in pruning (if any final-state survived); it also does a final pruning step that visits all states (the pruning that is done during decoding may fail to prune states that are within kPruningScale = 0.1 outside of the beam). If you call this, you cannot call AdvanceDecoding again (it will fail), and you cannot call GetLattice() and related functions with use_final_probs = false. Used to be called PruneActiveTokensFinal().
Definition at line 495 of file lattice-simple-decoder.cc.
References KALDI_VLOG, LatticeSimpleDecoder::num_toks_, LatticeSimpleDecoder::NumFramesDecoded(), LatticeSimpleDecoder::PruneForwardLinks(), LatticeSimpleDecoder::PruneForwardLinksFinal(), and LatticeSimpleDecoder::PruneTokensForFrame().
Referenced by LatticeSimpleDecoder::Decode().
BaseFloat FinalRelativeCost | ( | ) | const |
FinalRelativeCost() serves the same purpose as ReachedFinal(), but gives more information.
It returns the difference between the best (final-cost plus cost) of any token on the final frame, and the best cost of any token on the final frame. If it is infinity it means no final-states were present on the final frame. It will usually be nonnegative. If it not too positive (e.g. < 5 is my first guess, but this is not tested) you can take it as a good indication that we reached the final-state with reasonable likelihood.
Definition at line 421 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::decoding_finalized_, and LatticeSimpleDecoder::final_relative_cost_.
|
inlineprivate |
Definition at line 194 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoder::cur_toks_, KALDI_ASSERT, LatticeSimpleDecoder::num_toks_, and LatticeSimpleDecoder::Token::tot_cost.
Referenced by LatticeSimpleDecoder::ProcessEmitting(), and LatticeSimpleDecoder::ProcessNonemitting().
Definition at line 68 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::GetRawLattice().
Referenced by kaldi::DecodeUtteranceLatticeSimple().
bool GetLattice | ( | CompactLattice * | clat, |
bool | use_final_probs = true |
||
) | const |
Definition at line 158 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::config_, LatticeSimpleDecoderConfig::det_opts, fst::DeterminizeLatticePruned(), LatticeSimpleDecoder::GetRawLattice(), KALDI_WARN, LatticeSimpleDecoderConfig::lattice_beam, DeterminizeLatticePrunedOptions::max_mem, and DeterminizeLatticePhonePrunedOptions::max_mem.
|
inline |
Definition at line 89 of file lattice-simple-decoder.h.
Referenced by kaldi::DecodeUtteranceLatticeSimple().
Definition at line 78 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::decoding_finalized_, LatticeSimpleDecoder::final_costs_, KALDI_ASSERT, KALDI_ERR, KALDI_WARN, LatticeSimpleDecoder::ForwardLink::next, LatticeSimpleDecoder::num_toks_, LatticeSimpleDecoder::NumFramesDecoded(), and LatticeWeightTpl< BaseFloat >::One().
Referenced by kaldi::DecodeUtteranceLatticeSimple(), LatticeSimpleDecoder::GetBestPath(), and LatticeSimpleDecoder::GetLattice().
void InitDecoding | ( | ) |
InitDecoding initializes the decoding, and should only be used if you intend to call AdvanceDecoding().
If you call Decode(), you don't need to call this. You can call InitDecoding if you have already decoded an utterance and want to start with a new utterance.
Definition at line 27 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoder::ClearActiveTokens(), LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::decoding_finalized_, LatticeSimpleDecoder::final_costs_, LatticeSimpleDecoder::fst_, KALDI_ASSERT, LatticeSimpleDecoder::num_toks_, LatticeSimpleDecoder::prev_toks_, LatticeSimpleDecoder::ProcessNonemitting(), and LatticeSimpleDecoder::warned_.
Referenced by LatticeSimpleDecoder::Decode().
|
inline |
Definition at line 160 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::Decode(), LatticeSimpleDecoder::FinalizeDecoding(), LatticeSimpleDecoder::GetRawLattice(), and LatticeSimpleDecoder::PruneActiveTokens().
|
private |
Definition at line 510 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoderConfig::beam, LatticeSimpleDecoder::config_, LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::FindOrAddToken(), LatticeSimpleDecoder::fst_, LatticeSimpleDecoder::Token::links, DecodableInterface::LogLikelihood(), LatticeSimpleDecoder::prev_toks_, and LatticeSimpleDecoder::Token::tot_cost.
Referenced by LatticeSimpleDecoder::Decode().
|
private |
Definition at line 550 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoderConfig::beam, LatticeSimpleDecoder::config_, LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::Token::DeleteForwardLinks(), LatticeSimpleDecoder::FindOrAddToken(), LatticeSimpleDecoder::fst_, KALDI_ASSERT, KALDI_ERR, LatticeSimpleDecoder::Token::links, LatticeSimpleDecoder::Token::tot_cost, and LatticeSimpleDecoder::warned_.
Referenced by LatticeSimpleDecoder::Decode(), and LatticeSimpleDecoder::InitDecoding().
|
private |
Definition at line 462 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, KALDI_VLOG, LatticeSimpleDecoder::num_toks_, LatticeSimpleDecoder::NumFramesDecoded(), LatticeSimpleDecoder::PruneForwardLinks(), and LatticeSimpleDecoder::PruneTokensForFrame().
Referenced by LatticeSimpleDecoder::Decode().
Definition at line 636 of file lattice-simple-decoder.cc.
References rnnlm::i, and KALDI_VLOG.
Referenced by LatticeSimpleDecoder::Decode().
|
private |
Definition at line 228 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::ForwardLink::acoustic_cost, LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoder::config_, LatticeSimpleDecoder::Token::extra_cost, LatticeSimpleDecoder::ForwardLink::graph_cost, KALDI_ASSERT, KALDI_WARN, LatticeSimpleDecoderConfig::lattice_beam, LatticeSimpleDecoder::ForwardLink::next, LatticeSimpleDecoder::ForwardLink::next_tok, LatticeSimpleDecoder::Token::tot_cost, and LatticeSimpleDecoder::warned_.
Referenced by LatticeSimpleDecoder::FinalizeDecoding(), and LatticeSimpleDecoder::PruneActiveTokens().
|
private |
Definition at line 338 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::ForwardLink::acoustic_cost, LatticeSimpleDecoder::active_toks_, kaldi::ApproxEqual(), LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::config_, LatticeSimpleDecoder::cur_toks_, LatticeSimpleDecoder::decoding_finalized_, LatticeSimpleDecoder::Token::extra_cost, LatticeSimpleDecoder::final_best_cost_, LatticeSimpleDecoder::final_costs_, LatticeSimpleDecoder::final_relative_cost_, LatticeSimpleDecoder::ForwardLink::graph_cost, KALDI_ASSERT, KALDI_WARN, LatticeSimpleDecoderConfig::lattice_beam, LatticeSimpleDecoder::ForwardLink::next, LatticeSimpleDecoder::ForwardLink::next_tok, and LatticeSimpleDecoder::Token::tot_cost.
Referenced by LatticeSimpleDecoder::FinalizeDecoding().
|
private |
Definition at line 436 of file lattice-simple-decoder.cc.
References LatticeSimpleDecoder::active_toks_, LatticeSimpleDecoder::Token::extra_cost, KALDI_ASSERT, KALDI_WARN, LatticeSimpleDecoder::Token::next, and LatticeSimpleDecoder::num_toks_.
Referenced by LatticeSimpleDecoder::FinalizeDecoding(), and LatticeSimpleDecoder::PruneActiveTokens().
|
inline |
says whether a final-state was active on the last frame.
If it was not, the lattice (or traceback) will end with states that are not final-states.
Definition at line 100 of file lattice-simple-decoder.h.
Referenced by kaldi::DecodeUtteranceLatticeSimple().
|
private |
Definition at line 291 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::ClearActiveTokens(), LatticeSimpleDecoder::FindOrAddToken(), LatticeSimpleDecoder::GetRawLattice(), LatticeSimpleDecoder::InitDecoding(), LatticeSimpleDecoder::ProcessEmitting(), LatticeSimpleDecoder::ProcessNonemitting(), LatticeSimpleDecoder::PruneActiveTokens(), LatticeSimpleDecoder::PruneForwardLinks(), LatticeSimpleDecoder::PruneForwardLinksFinal(), and LatticeSimpleDecoder::PruneTokensForFrame().
|
private |
Definition at line 294 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::Decode(), LatticeSimpleDecoder::GetLattice(), LatticeSimpleDecoder::ProcessEmitting(), LatticeSimpleDecoder::ProcessNonemitting(), LatticeSimpleDecoder::PruneForwardLinks(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
Definition at line 289 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::Decode(), LatticeSimpleDecoder::FindOrAddToken(), LatticeSimpleDecoder::InitDecoding(), LatticeSimpleDecoder::ProcessEmitting(), LatticeSimpleDecoder::ProcessNonemitting(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
|
private |
decoding_finalized_ is true if someone called FinalizeDecoding().
[note, calling this is optional]. If true, it's forbidden to decode more. Also, if this is set, then the output of ComputeFinalCosts() is in the next three variables. The reason we need to do this is that after FinalizeDecoding() calls PruneTokensForFrame() for the final frame, some of the tokens on the last frame are freed, so we free the list from cur_toks_ to avoid having dangling pointers hanging around.
Definition at line 306 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::FinalRelativeCost(), LatticeSimpleDecoder::GetRawLattice(), LatticeSimpleDecoder::InitDecoding(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
|
private |
Definition at line 311 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::PruneForwardLinksFinal().
For the meaning of the next 3 variables, see the comment for decoding_finalized_ above., and ComputeFinalCosts().
Definition at line 309 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::Decode(), LatticeSimpleDecoder::GetRawLattice(), LatticeSimpleDecoder::InitDecoding(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
|
private |
Definition at line 310 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::FinalRelativeCost(), and LatticeSimpleDecoder::PruneForwardLinksFinal().
|
private |
Definition at line 293 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::ComputeFinalCosts(), LatticeSimpleDecoder::InitDecoding(), LatticeSimpleDecoder::ProcessEmitting(), and LatticeSimpleDecoder::ProcessNonemitting().
|
private |
Definition at line 295 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::ClearActiveTokens(), LatticeSimpleDecoder::FinalizeDecoding(), LatticeSimpleDecoder::FindOrAddToken(), LatticeSimpleDecoder::GetRawLattice(), LatticeSimpleDecoder::InitDecoding(), LatticeSimpleDecoder::PruneActiveTokens(), and LatticeSimpleDecoder::PruneTokensForFrame().
Definition at line 290 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::InitDecoding(), and LatticeSimpleDecoder::ProcessEmitting().
|
private |
Definition at line 296 of file lattice-simple-decoder.h.
Referenced by LatticeSimpleDecoder::InitDecoding(), LatticeSimpleDecoder::ProcessNonemitting(), and LatticeSimpleDecoder::PruneForwardLinks().