GrammarFst is an FST that is 'stitched together' from multiple FSTs, that can recursively incorporate each other. More...
#include <grammar-fst.h>
Classes | |
struct | ExpandedState |
Represents an expanded state in an FstInstance. More... | |
struct | FstInstance |
Public Types | |
typedef GrammarFstArc | Arc |
typedef TropicalWeight | Weight |
typedef Arc::StateId | StateId |
typedef StdArc::StateId | BaseStateId |
typedef Arc::Label | Label |
Public Member Functions | |
GrammarFst (int32 nonterm_phones_offset, std::shared_ptr< const ConstFst< StdArc > > top_fst, const std::vector< std::pair< int32, std::shared_ptr< const ConstFst< StdArc > > > > &ifsts) | |
Constructor. More... | |
GrammarFst (const GrammarFst &other)=default | |
Copy constructor. More... | |
GrammarFst () | |
This constructor should only be used prior to calling Read(). More... | |
void | Write (std::ostream &os, bool binary) const |
void | Read (std::istream &os, bool binary) |
StateId | Start () const |
Weight | Final (StateId s) const |
size_t | NumInputEpsilons (StateId s) const |
std::string | Type () const |
~GrammarFst () | |
Private Member Functions | |
void | InitNonterminalMap () |
bool | InitEntryArcs (int32 i) |
void | InitInstances () |
void | Init () |
void | Destroy () |
void | InitEntryOrReentryArcs (const ConstFst< StdArc > &fst, int32 entry_state, int32 nonterminal_symbol, std::unordered_map< int32, int32 > *phone_to_arc) |
int32 | GetPhoneSymbolFor (enum NonterminalValues n) |
void | DecodeSymbol (Label label, int32 *nonterminal_symbol, int32 *left_context_phone) |
Decodes an ilabel into a pair (nonterminal, left_context_phone). More... | |
ExpandedState * | ExpandState (int32 instance_id, BaseStateId state_id) |
ExpandedState * | ExpandStateEnd (int32 instance_id, BaseStateId state_id) |
ExpandedState * | ExpandStateUserDefined (int32 instance_id, BaseStateId state_id) |
int32 | GetChildInstanceId (int32 instance_id, int32 nonterminal, int32 state) |
ExpandedState * | GetExpandedState (int32 instance_id, BaseStateId state_id) |
Called from the ArcIterator constructor when we encounter an FST state with nonzero final-prob, this function first looks up this state_id in 'expanded_states' member of the corresponding FstInstance, and returns it if already present; otherwise it populates the 'expanded_states' map with something for this state_id and returns the value. More... | |
Static Private Member Functions | |
static void | CombineArcs (const StdArc &leaving_arc, const StdArc &arriving_arc, float cost_correction, StdArc *arc) |
Called while expanding states, this function combines information from two arcs: one leaving one sub-fst and one arriving in another sub-fst. More... | |
Private Attributes | |
int32 | nonterm_phones_offset_ |
std::shared_ptr< const ConstFst< StdArc > > | top_fst_ |
std::vector< std::pair< int32, std::shared_ptr< const ConstFst< StdArc > > > > | ifsts_ |
std::unordered_map< int32, int32 > | nonterminal_map_ |
std::vector< std::unordered_map< int32, int32 > > | entry_arcs_ |
std::vector< FstInstance > | instances_ |
Friends | |
class | ArcIterator< GrammarFst > |
GrammarFst is an FST that is 'stitched together' from multiple FSTs, that can recursively incorporate each other.
(This is limited to left-biphone phonetic context). This class does not inherit from fst::Fst and does not support its full interface– only the parts that are necessary for the decoder to work when templated on it.
The basic interface is inspired by OpenFst's 'ReplaceFst' (see its replace.h), except that this handles left-biphone phonetic context, which requires, essentially, having multiple exit-points and entry-points for sub-FSTs that represent nonterminals in the grammar; and multiple return points whenever we invoke a nonterminal. For more information see Support for grammars and graphs with on-the-fly parts. (i.e. ../doc/grammar.dox).
THREAD SAFETY: you can't use this object from multiple threads; you should create lightweight copies of this object using the copy constructor, e.g. `new GrammarFst(this_grammar_fst)`, if you want to decode from multiple threads using the same GrammarFst.
Definition at line 96 of file grammar-fst.h.
typedef GrammarFstArc Arc |
Definition at line 98 of file grammar-fst.h.
typedef StdArc::StateId BaseStateId |
Definition at line 107 of file grammar-fst.h.
typedef Arc::Label Label |
Definition at line 109 of file grammar-fst.h.
typedef Arc::StateId StateId |
Definition at line 104 of file grammar-fst.h.
typedef TropicalWeight Weight |
Definition at line 99 of file grammar-fst.h.
GrammarFst | ( | int32 | nonterm_phones_offset, |
std::shared_ptr< const ConstFst< StdArc > > | top_fst, | ||
const std::vector< std::pair< int32, std::shared_ptr< const ConstFst< StdArc > > > > & | ifsts | ||
) |
Constructor.
This constructor is very lightweight; the only immediate work it does is to iterate over the arcs in the start states of the provided FSTs in order to set up the appropriate entry points.
For simplicity (to avoid templates), we limit the input FSTs to be of type ConstFst<StdArc>; this limitation could be removed later if needed. You can always construct a ConstFst<StdArc> if you have another StdArc-based FST type. If the FST was read from disk, it may already be of type ConstFst, and dynamic_cast might be sufficient to convert the type.
[in] | nonterm_phones_offset | The integer id of the symbol "#nonterm_bos" in phones.txt. |
[in] | top_fst | The top-level FST of the grammar, which will usually invoke the fsts in 'ifsts'. The fsts in 'ifsts' may also invoke each other recursively. Even left-recursion is allowed, although if it is with zero cost, it may blow up when you decode. When an FST invokes another, the invocation point will have sequences of two special symbols which would be decoded as: (#nonterm:foo,p1) (#nonterm_reenter,p2) where p1 and p2 (which may be real phones or #nonterm_bos) represent the phonetic left-context that we enter, and leave, the sub-graph with respectively. |
[in] | ifsts | ifsts is a list of pairs (nonterminal-symbol, the HCLG.fst corresponding to that symbol). The nonterminal symbols must be among the user-specified nonterminals in phones.txt, i.e. the things with names like "#nonterm:foo" and "#nonterm:bar" in phones.txt. Also no nonterminal may appear more than once in 'fsts'. ifsts may be empty, even though that doesn't make much sense. |
|
default |
Copy constructor.
Useful because this object is not thread safe so cannot be used by multiple parallel decoder threads, but it is lightweight and can copy it without causing the stored FSTs to be copied.
|
inline |
This constructor should only be used prior to calling Read().
Definition at line 154 of file grammar-fst.h.
~GrammarFst | ( | ) |
Definition at line 52 of file grammar-fst.cc.
|
inlinestaticprivate |
Called while expanding states, this function combines information from two arcs: one leaving one sub-fst and one arriving in another sub-fst.
[in] | leaving_arc | The arc leaving the first FST; must have zero olabel. The ilabel will have a nonterminal symbol like #nonterm:foo or #nonterm_end on it, encoded with a phonetic context, but we ignore the ilabel. |
[in] | arriving_arc | The arc arriving in the second FST. It will have an ilabel consisted of either #nonterm_begin or #nonterm_enter combined with a left-context phone, but we ignore the ilabel. |
[in] | cost_correction | A correction term that we add to the cost of the arcs. This basically cancels out the "1/num_options" part of the weight that we added in L.fst when we put in all the phonetic-context options. We did that to keep the FST stochastic, so that if we ever pushed the weights, it wouldn't lead to weird effects. This takes out that correction term... things will still sum to one in the appropriate way, because in fact when we cross these FST boundaries we only take one specific phonetic context, rather than all possibilities. |
[out] | arc | The arc that we output. Will have:
|
Definition at line 199 of file grammar-fst.cc.
References KALDI_ASSERT.
Decodes an ilabel into a pair (nonterminal, left_context_phone).
Crashes if something went wrong or ilabel did not represent that (e.g. was less than kNontermBigNumber).
[in] | the | ilabel to be decoded. Note: the type 'Label' will in practice be int. |
[out] | The | nonterminal part of the ilabel after decoding. Will be a value greater than nonterm_phones_offset_. |
[out] | The | left-context-phone part of the ilabel after decoding. Will either be a phone index, or the symbol corresponding to #nonterm_bos (meaning no left-context as we are at the beginning of the sequence). |
Definition at line 75 of file grammar-fst.cc.
References fst::GetEncodingMultiple(), KALDI_ASSERT, KALDI_ERR, fst::kNontermBigNumber, fst::kNontermBos, and fst::kNontermMediumNumber.
|
private |
Definition at line 56 of file grammar-fst.cc.
References GrammarFst::FstInstance::expanded_states, and rnnlm::i.
|
private |
Definition at line 171 of file grammar-fst.cc.
References fst::GetEncodingMultiple(), KALDI_ASSERT, KALDI_ERR, fst::kNontermBegin, fst::kNontermBigNumber, fst::kNontermEnd, fst::kNontermReenter, and fst::kNontermUserDefined.
|
private |
Definition at line 220 of file grammar-fst.cc.
References GrammarFst::ExpandedState::arcs, GrammarFst::ExpandedState::dest_fst_instance, GrammarFst::FstInstance::fst, GrammarFst::FstInstance::ifst_index, KALDI_ASSERT, KALDI_ERR, fst::kNontermEnd, GrammarFst::FstInstance::parent_instance, and GrammarFst::FstInstance::parent_state.
|
private |
Definition at line 324 of file grammar-fst.cc.
References GrammarFst::ExpandedState::arcs, GrammarFst::ExpandedState::dest_fst_instance, GrammarFst::FstInstance::fst, GrammarFst::FstInstance::ifst_index, and KALDI_ERR.
Definition at line 171 of file grammar-fst.h.
References KALDI_GRAMMAR_FST_SPECIAL_WEIGHT.
Referenced by fst::CopyToVectorFst().
Definition at line 281 of file grammar-fst.cc.
References GrammarFst::FstInstance::fst, GrammarFst::FstInstance::ifst_index, KALDI_ERR, fst::kNontermReenter, GrammarFst::FstInstance::parent_instance, GrammarFst::FstInstance::parent_reentry_arcs, and GrammarFst::FstInstance::parent_state.
|
inlineprivate |
Called from the ArcIterator constructor when we encounter an FST state with nonzero final-prob, this function first looks up this state_id in 'expanded_states' member of the corresponding FstInstance, and returns it if already present; otherwise it populates the 'expanded_states' map with something for this state_id and returns the value.
Definition at line 352 of file grammar-fst.h.
Referenced by ArcIterator< GrammarFst >::ArcIterator().
|
inlineprivate |
|
private |
Definition at line 36 of file grammar-fst.cc.
References KALDI_ASSERT.
Definition at line 113 of file grammar-fst.cc.
References rnnlm::i, KALDI_ASSERT, and fst::kNontermBegin.
|
private |
Definition at line 133 of file grammar-fst.cc.
References KALDI_ERR, and fst::kNontermBigNumber.
|
private |
Definition at line 124 of file grammar-fst.cc.
References KALDI_ASSERT.
|
private |
Definition at line 97 of file grammar-fst.cc.
References rnnlm::i, KALDI_ERR, and fst::kNontermUserDefined.
Definition at line 190 of file grammar-fst.h.
References GrammarFst::FstInstance::fst, and KALDI_GRAMMAR_FST_SPECIAL_WEIGHT.
void Read | ( | std::istream & | os, |
bool | binary | ||
) |
Definition at line 420 of file grammar-fst.cc.
References kaldi::ExpectToken(), rnnlm::i, KALDI_ERR, kaldi::ReadBasicType(), and fst::ReadConstFstFromStream().
|
inline |
Definition at line 165 of file grammar-fst.h.
Referenced by fst::CopyToVectorFst().
|
inline |
Definition at line 203 of file grammar-fst.h.
void Write | ( | std::ostream & | os, |
bool | binary | ||
) | const |
Definition at line 383 of file grammar-fst.cc.
References rnnlm::i, KALDI_ERR, kaldi::WriteBasicType(), and kaldi::WriteToken().
|
friend |
Definition at line 208 of file grammar-fst.h.
|
private |
Definition at line 480 of file grammar-fst.h.
|
private |
Definition at line 466 of file grammar-fst.h.
|
private |
Definition at line 485 of file grammar-fst.h.
Referenced by ArcIterator< GrammarFst >::ArcIterator().
|
private |
Definition at line 456 of file grammar-fst.h.
|
private |
Definition at line 470 of file grammar-fst.h.
|
private |
Definition at line 461 of file grammar-fst.h.