You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1096 lines
44 KiB

  1. // decoder/lattice-faster-decoder.cc
  2. // Copyright 2009-2012 Microsoft Corporation Mirko Hannemann
  3. // 2013-2018 Johns Hopkins University (Author: Daniel Povey)
  4. // 2014 Guoguo Chen
  5. // 2018 Zhehuai Chen
  6. // 2021 Binbin Zhang, Zhendong Peng
  7. // See ../../COPYING for clarification regarding multiple authors
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  17. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  18. // MERCHANTABLITY OR NON-INFRINGEMENT.
  19. // See the Apache 2 License for the specific language governing permissions and
  20. // limitations under the License.
  21. #include <algorithm>
  22. #include <unordered_set>
  23. #include "decoder/lattice-faster-decoder.h"
  24. // #include "lat/lattice-functions.h"
  25. namespace kaldi {
  26. // instantiate this class once for each thing you have to decode.
  27. template <typename FST, typename Token>
  28. LatticeFasterDecoderTpl<FST, Token>::LatticeFasterDecoderTpl(
  29. const FST& fst, const LatticeFasterDecoderConfig& config,
  30. const std::shared_ptr<wenet::ContextGraph>& context_graph)
  31. : fst_(&fst),
  32. delete_fst_(false),
  33. config_(config),
  34. num_toks_(0),
  35. context_graph_(context_graph) {
  36. config.Check();
  37. toks_.SetSize(
  38. 1000); // just so on the first frame we do something reasonable.
  39. }
  40. template <typename FST, typename Token>
  41. LatticeFasterDecoderTpl<FST, Token>::LatticeFasterDecoderTpl(
  42. const LatticeFasterDecoderConfig& config, FST* fst)
  43. : fst_(fst), delete_fst_(true), config_(config), num_toks_(0) {
  44. config.Check();
  45. toks_.SetSize(
  46. 1000); // just so on the first frame we do something reasonable.
  47. }
  48. template <typename FST, typename Token>
  49. LatticeFasterDecoderTpl<FST, Token>::~LatticeFasterDecoderTpl() {
  50. DeleteElems(toks_.Clear());
  51. ClearActiveTokens();
  52. if (delete_fst_) delete fst_;
  53. }
  54. template <typename FST, typename Token>
  55. void LatticeFasterDecoderTpl<FST, Token>::InitDecoding() {
  56. // clean up from last time:
  57. DeleteElems(toks_.Clear());
  58. cost_offsets_.clear();
  59. ClearActiveTokens();
  60. warned_ = false;
  61. num_toks_ = 0;
  62. decoding_finalized_ = false;
  63. final_costs_.clear();
  64. StateId start_state = fst_->Start();
  65. KALDI_ASSERT(start_state != fst::kNoStateId);
  66. active_toks_.resize(1);
  67. Token* start_tok = new Token(0.0, 0.0, NULL, NULL, NULL);
  68. active_toks_[0].toks = start_tok;
  69. toks_.Insert(start_state, start_tok);
  70. num_toks_++;
  71. ProcessNonemitting(config_.beam);
  72. }
  73. // Returns true if any kind of traceback is available (not necessarily from
  74. // a final state). It should only very rarely return false; this indicates
  75. // an unusual search error.
  76. template <typename FST, typename Token>
  77. bool LatticeFasterDecoderTpl<FST, Token>::Decode(
  78. DecodableInterface* decodable) {
  79. InitDecoding();
  80. // We use 1-based indexing for frames in this decoder (if you view it in
  81. // terms of features), but note that the decodable object uses zero-based
  82. // numbering, which we have to correct for when we call it.
  83. AdvanceDecoding(decodable);
  84. FinalizeDecoding();
  85. // Returns true if we have any kind of traceback available (not necessarily
  86. // to the end state; query ReachedFinal() for that).
  87. return !active_toks_.empty() && active_toks_.back().toks != NULL;
  88. }
  89. // Outputs an FST corresponding to the single best path through the lattice.
  90. template <typename FST, typename Token>
  91. bool LatticeFasterDecoderTpl<FST, Token>::GetBestPath(
  92. Lattice* olat, bool use_final_probs) const {
  93. Lattice raw_lat;
  94. GetRawLattice(&raw_lat, use_final_probs);
  95. ShortestPath(raw_lat, olat);
  96. return (olat->NumStates() != 0);
  97. }
  98. // Outputs an FST corresponding to the raw, state-level lattice
  99. template <typename FST, typename Token>
  100. bool LatticeFasterDecoderTpl<FST, Token>::GetRawLattice(
  101. Lattice* ofst, bool use_final_probs) const {
  102. typedef LatticeArc Arc;
  103. typedef Arc::StateId StateId;
  104. typedef Arc::Weight Weight;
  105. typedef Arc::Label Label;
  106. // Note: you can't use the old interface (Decode()) if you want to
  107. // get the lattice with use_final_probs = false. You'd have to do
  108. // InitDecoding() and then AdvanceDecoding().
  109. if (decoding_finalized_ && !use_final_probs)
  110. KALDI_ERR << "You cannot call FinalizeDecoding() and then call "
  111. << "GetRawLattice() with use_final_probs == false";
  112. unordered_map<Token*, BaseFloat> final_costs_local;
  113. const unordered_map<Token*, BaseFloat>& final_costs =
  114. (decoding_finalized_ ? final_costs_ : final_costs_local);
  115. if (!decoding_finalized_ && use_final_probs)
  116. ComputeFinalCosts(&final_costs_local, NULL, NULL);
  117. ofst->DeleteStates();
  118. // num-frames plus one (since frames are one-based, and we have
  119. // an extra frame for the start-state).
  120. int32 num_frames = active_toks_.size() - 1;
  121. KALDI_ASSERT(num_frames > 0);
  122. const int32 bucket_count = num_toks_ / 2 + 3;
  123. unordered_map<Token*, StateId> tok_map(bucket_count);
  124. // First create all states.
  125. std::vector<Token*> token_list;
  126. for (int32 f = 0; f <= num_frames; f++) {
  127. if (active_toks_[f].toks == NULL) {
  128. KALDI_WARN << "GetRawLattice: no tokens active on frame " << f
  129. << ": not producing lattice.\n";
  130. return false;
  131. }
  132. TopSortTokens(active_toks_[f].toks, &token_list);
  133. for (size_t i = 0; i < token_list.size(); i++)
  134. if (token_list[i] != NULL) tok_map[token_list[i]] = ofst->AddState();
  135. }
  136. // The next statement sets the start state of the output FST. Because we
  137. // topologically sorted the tokens, state zero must be the start-state.
  138. ofst->SetStart(0);
  139. KALDI_VLOG(4) << "init:" << num_toks_ / 2 + 3
  140. << " buckets:" << tok_map.bucket_count()
  141. << " load:" << tok_map.load_factor()
  142. << " max:" << tok_map.max_load_factor();
  143. // Now create all arcs.
  144. for (int32 f = 0; f <= num_frames; f++) {
  145. for (Token* tok = active_toks_[f].toks; tok != NULL; tok = tok->next) {
  146. StateId cur_state = tok_map[tok];
  147. for (ForwardLinkT* l = tok->links; l != NULL; l = l->next) {
  148. typename unordered_map<Token*, StateId>::const_iterator iter =
  149. tok_map.find(l->next_tok);
  150. StateId nextstate = iter->second;
  151. KALDI_ASSERT(iter != tok_map.end());
  152. BaseFloat cost_offset = 0.0;
  153. if (l->ilabel != 0) { // emitting..
  154. KALDI_ASSERT(f >= 0 && f < cost_offsets_.size());
  155. cost_offset = cost_offsets_[f];
  156. }
  157. Arc arc(l->ilabel, l->olabel,
  158. Weight(l->graph_cost, l->acoustic_cost - cost_offset),
  159. nextstate);
  160. ofst->AddArc(cur_state, arc);
  161. }
  162. if (f == num_frames) {
  163. if (use_final_probs && !final_costs.empty()) {
  164. typename unordered_map<Token*, BaseFloat>::const_iterator iter =
  165. final_costs.find(tok);
  166. if (iter != final_costs.end())
  167. ofst->SetFinal(cur_state, LatticeWeight(iter->second, 0));
  168. } else {
  169. ofst->SetFinal(cur_state, LatticeWeight::One());
  170. }
  171. }
  172. }
  173. }
  174. return (ofst->NumStates() > 0);
  175. }
  176. // This function is now deprecated, since now we do determinization from outside
  177. // the LatticeFasterDecoder class. Outputs an FST corresponding to the
  178. // lattice-determinized lattice (one path per word sequence).
  179. template <typename FST, typename Token>
  180. bool LatticeFasterDecoderTpl<FST, Token>::GetLattice(
  181. CompactLattice* ofst, bool use_final_probs) const {
  182. Lattice raw_fst;
  183. GetRawLattice(&raw_fst, use_final_probs);
  184. Invert(&raw_fst); // make it so word labels are on the input.
  185. // (in phase where we get backward-costs).
  186. fst::ILabelCompare<LatticeArc> ilabel_comp;
  187. ArcSort(&raw_fst, ilabel_comp); // sort on ilabel; makes
  188. // lattice-determinization more efficient.
  189. fst::DeterminizeLatticePrunedOptions lat_opts;
  190. lat_opts.max_mem = config_.det_opts.max_mem;
  191. DeterminizeLatticePruned(raw_fst, config_.lattice_beam, ofst, lat_opts);
  192. raw_fst.DeleteStates(); // Free memory-- raw_fst no longer needed.
  193. Connect(ofst); // Remove unreachable states... there might be
  194. // a small number of these, in some cases.
  195. // Note: if something went wrong and the raw lattice was empty,
  196. // we should still get to this point in the code without warnings or failures.
  197. return (ofst->NumStates() != 0);
  198. }
  199. template <typename FST, typename Token>
  200. void LatticeFasterDecoderTpl<FST, Token>::PossiblyResizeHash(size_t num_toks) {
  201. size_t new_sz = static_cast<size_t>(static_cast<BaseFloat>(num_toks) *
  202. config_.hash_ratio);
  203. if (new_sz > toks_.Size()) {
  204. toks_.SetSize(new_sz);
  205. }
  206. }
  207. /*
  208. A note on the definition of extra_cost.
  209. extra_cost is used in pruning tokens, to save memory.
  210. extra_cost can be thought of as a beta (backward) cost assuming
  211. we had set the betas on currently-active tokens to all be the negative
  212. of the alphas for those tokens. (So all currently active tokens would
  213. be on (tied) best paths).
  214. We can use the extra_cost to accurately prune away tokens that we know will
  215. never appear in the lattice. If the extra_cost is greater than the desired
  216. lattice beam, the token would provably never appear in the lattice, so we can
  217. prune away the token.
  218. (Note: we don't update all the extra_costs every time we update a frame; we
  219. only do it every 'config_.prune_interval' frames).
  220. */
  221. // FindOrAddToken either locates a token in hash of toks_,
  222. // or if necessary inserts a new, empty token (i.e. with no forward links)
  223. // for the current frame. [note: it's inserted if necessary into hash toks_
  224. // and also into the singly linked list of tokens active on this frame
  225. // (whose head is at active_toks_[frame]).
  226. template <typename FST, typename Token>
  227. inline typename LatticeFasterDecoderTpl<FST, Token>::Elem*
  228. LatticeFasterDecoderTpl<FST, Token>::FindOrAddToken(StateId state,
  229. int32 frame_plus_one,
  230. BaseFloat tot_cost,
  231. Token* backpointer,
  232. bool* changed) {
  233. // Returns the Token pointer. Sets "changed" (if non-NULL) to true
  234. // if the token was newly created or the cost changed.
  235. KALDI_ASSERT(frame_plus_one < active_toks_.size());
  236. Token*& toks = active_toks_[frame_plus_one].toks;
  237. Elem* e_found = toks_.Insert(state, NULL);
  238. if (e_found->val == NULL) { // no such token presently.
  239. const BaseFloat extra_cost = 0.0;
  240. // tokens on the currently final frame have zero extra_cost
  241. // as any of them could end up
  242. // on the winning path.
  243. Token* new_tok = new Token(tot_cost, extra_cost, NULL, toks, backpointer);
  244. // NULL: no forward links yet
  245. toks = new_tok;
  246. num_toks_++;
  247. e_found->val = new_tok;
  248. if (changed) *changed = true;
  249. return e_found;
  250. } else {
  251. Token* tok = e_found->val; // There is an existing Token for this state.
  252. if (tok->tot_cost > tot_cost) { // replace old token
  253. tok->tot_cost = tot_cost;
  254. // SetBackpointer() just does tok->backpointer = backpointer in
  255. // the case where Token == BackpointerToken, else nothing.
  256. tok->SetBackpointer(backpointer);
  257. // we don't allocate a new token, the old stays linked in active_toks_
  258. // we only replace the tot_cost
  259. // in the current frame, there are no forward links (and no extra_cost)
  260. // only in ProcessNonemitting we have to delete forward links
  261. // in case we visit a state for the second time
  262. // those forward links, that lead to this replaced token before:
  263. // they remain and will hopefully be pruned later (PruneForwardLinks...)
  264. if (changed) *changed = true;
  265. } else {
  266. if (changed) *changed = false;
  267. }
  268. return e_found;
  269. }
  270. }
  271. // prunes outgoing links for all tokens in active_toks_[frame]
  272. // it's called by PruneActiveTokens
  273. // all links, that have link_extra_cost > lattice_beam are pruned
  274. template <typename FST, typename Token>
  275. void LatticeFasterDecoderTpl<FST, Token>::PruneForwardLinks(
  276. int32 frame_plus_one, bool* extra_costs_changed, bool* links_pruned,
  277. BaseFloat delta) {
  278. // delta is the amount by which the extra_costs must change
  279. // If delta is larger, we'll tend to go back less far
  280. // toward the beginning of the file.
  281. // extra_costs_changed is set to true if extra_cost was changed for any token
  282. // links_pruned is set to true if any link in any token was pruned
  283. *extra_costs_changed = false;
  284. *links_pruned = false;
  285. KALDI_ASSERT(frame_plus_one >= 0 && frame_plus_one < active_toks_.size());
  286. if (active_toks_[frame_plus_one].toks ==
  287. NULL) { // empty list; should not happen.
  288. if (!warned_) {
  289. KALDI_WARN << "No tokens alive [doing pruning].. warning first "
  290. "time only for each utterance\n";
  291. warned_ = true;
  292. }
  293. }
  294. // We have to iterate until there is no more change, because the links
  295. // are not guaranteed to be in topological order.
  296. bool changed = true; // difference new minus old extra cost >= delta ?
  297. while (changed) {
  298. changed = false;
  299. for (Token* tok = active_toks_[frame_plus_one].toks; tok != NULL;
  300. tok = tok->next) {
  301. ForwardLinkT *link, *prev_link = NULL;
  302. // will recompute tok_extra_cost for tok.
  303. BaseFloat tok_extra_cost = std::numeric_limits<BaseFloat>::infinity();
  304. // tok_extra_cost is the best (min) of link_extra_cost of outgoing links
  305. for (link = tok->links; link != NULL;) {
  306. // See if we need to excise this link...
  307. Token* next_tok = link->next_tok;
  308. BaseFloat link_extra_cost =
  309. next_tok->extra_cost +
  310. ((tok->tot_cost + link->acoustic_cost + link->graph_cost) -
  311. next_tok->tot_cost); // difference in brackets is >= 0
  312. // link_exta_cost is the difference in score between the best paths
  313. // through link source state and through link destination state
  314. KALDI_ASSERT(link_extra_cost == link_extra_cost); // check for NaN
  315. // the graph_cost contatins the context score
  316. // if it's the score of the backoff arc, it should be removed.
  317. if (link->context_score < 0) {
  318. link_extra_cost += link->context_score;
  319. }
  320. if (link_extra_cost > config_.lattice_beam) { // excise link
  321. ForwardLinkT* next_link = link->next;
  322. if (prev_link != NULL)
  323. prev_link->next = next_link;
  324. else
  325. tok->links = next_link;
  326. delete link;
  327. link = next_link; // advance link but leave prev_link the same.
  328. *links_pruned = true;
  329. } else { // keep the link and update the tok_extra_cost if needed.
  330. if (link_extra_cost < 0.0) { // this is just a precaution.
  331. // if (link_extra_cost < -0.01)
  332. // KALDI_WARN << "Negative extra_cost: " << link_extra_cost;
  333. link_extra_cost = 0.0;
  334. }
  335. if (link_extra_cost < tok_extra_cost)
  336. tok_extra_cost = link_extra_cost;
  337. prev_link = link; // move to next link
  338. link = link->next;
  339. }
  340. } // for all outgoing links
  341. if (fabs(tok_extra_cost - tok->extra_cost) > delta)
  342. changed = true; // difference new minus old is bigger than delta
  343. tok->extra_cost = tok_extra_cost;
  344. // will be +infinity or <= lattice_beam_.
  345. // infinity indicates, that no forward link survived pruning
  346. } // for all Token on active_toks_[frame]
  347. if (changed) *extra_costs_changed = true;
  348. // Note: it's theoretically possible that aggressive compiler
  349. // optimizations could cause an infinite loop here for small delta and
  350. // high-dynamic-range scores.
  351. } // while changed
  352. }
  353. // PruneForwardLinksFinal is a version of PruneForwardLinks that we call
  354. // on the final frame. If there are final tokens active, it uses
  355. // the final-probs for pruning, otherwise it treats all tokens as final.
  356. template <typename FST, typename Token>
  357. void LatticeFasterDecoderTpl<FST, Token>::PruneForwardLinksFinal() {
  358. KALDI_ASSERT(!active_toks_.empty());
  359. int32 frame_plus_one = active_toks_.size() - 1;
  360. if (active_toks_[frame_plus_one].toks ==
  361. NULL) // empty list; should not happen.
  362. KALDI_WARN << "No tokens alive at end of file";
  363. typedef typename unordered_map<Token*, BaseFloat>::const_iterator IterType;
  364. ComputeFinalCosts(&final_costs_, &final_relative_cost_, &final_best_cost_);
  365. decoding_finalized_ = true;
  366. // We call DeleteElems() as a nicety, not because it's really necessary;
  367. // otherwise there would be a time, after calling PruneTokensForFrame() on the
  368. // final frame, when toks_.GetList() or toks_.Clear() would contain pointers
  369. // to nonexistent tokens.
  370. DeleteElems(toks_.Clear());
  371. // Now go through tokens on this frame, pruning forward links... may have to
  372. // iterate a few times until there is no more change, because the list is not
  373. // in topological order. This is a modified version of the code in
  374. // PruneForwardLinks, but here we also take account of the final-probs.
  375. bool changed = true;
  376. BaseFloat delta = 1.0e-05;
  377. while (changed) {
  378. changed = false;
  379. for (Token* tok = active_toks_[frame_plus_one].toks; tok != NULL;
  380. tok = tok->next) {
  381. ForwardLinkT *link, *prev_link = NULL;
  382. // will recompute tok_extra_cost. It has a term in it that corresponds
  383. // to the "final-prob", so instead of initializing tok_extra_cost to
  384. // infinity below we set it to the difference between the
  385. // (score+final_prob) of this token, and the best such (score+final_prob).
  386. BaseFloat final_cost;
  387. if (final_costs_.empty()) {
  388. final_cost = 0.0;
  389. } else {
  390. IterType iter = final_costs_.find(tok);
  391. if (iter != final_costs_.end())
  392. final_cost = iter->second;
  393. else
  394. final_cost = std::numeric_limits<BaseFloat>::infinity();
  395. }
  396. BaseFloat tok_extra_cost = tok->tot_cost + final_cost - final_best_cost_;
  397. // tok_extra_cost will be a "min" over either directly being final, or
  398. // being indirectly final through other links, and the loop below may
  399. // decrease its value:
  400. for (link = tok->links; link != NULL;) {
  401. // See if we need to excise this link...
  402. Token* next_tok = link->next_tok;
  403. BaseFloat link_extra_cost =
  404. next_tok->extra_cost +
  405. ((tok->tot_cost + link->acoustic_cost + link->graph_cost) -
  406. next_tok->tot_cost);
  407. if (link_extra_cost > config_.lattice_beam) { // excise link
  408. ForwardLinkT* next_link = link->next;
  409. if (prev_link != NULL)
  410. prev_link->next = next_link;
  411. else
  412. tok->links = next_link;
  413. delete link;
  414. link = next_link; // advance link but leave prev_link the same.
  415. } else { // keep the link and update the tok_extra_cost if needed.
  416. if (link_extra_cost < 0.0) { // this is just a precaution.
  417. // if (link_extra_cost < -0.01)
  418. // KALDI_WARN << "Negative extra_cost: " << link_extra_cost;
  419. link_extra_cost = 0.0;
  420. }
  421. if (link_extra_cost < tok_extra_cost)
  422. tok_extra_cost = link_extra_cost;
  423. prev_link = link;
  424. link = link->next;
  425. }
  426. }
  427. // prune away tokens worse than lattice_beam above best path. This step
  428. // was not necessary in the non-final case because then, this case
  429. // showed up as having no forward links. Here, the tok_extra_cost has
  430. // an extra component relating to the final-prob.
  431. if (tok_extra_cost > config_.lattice_beam)
  432. tok_extra_cost = std::numeric_limits<BaseFloat>::infinity();
  433. // to be pruned in PruneTokensForFrame
  434. if (!ApproxEqual(tok->extra_cost, tok_extra_cost, delta)) changed = true;
  435. tok->extra_cost =
  436. tok_extra_cost; // will be +infinity or <= lattice_beam_.
  437. }
  438. } // while changed
  439. }
  440. template <typename FST, typename Token>
  441. BaseFloat LatticeFasterDecoderTpl<FST, Token>::FinalRelativeCost() const {
  442. if (!decoding_finalized_) {
  443. BaseFloat relative_cost;
  444. ComputeFinalCosts(NULL, &relative_cost, NULL);
  445. return relative_cost;
  446. } else {
  447. // we're not allowed to call that function if FinalizeDecoding() has
  448. // been called; return a cached value.
  449. return final_relative_cost_;
  450. }
  451. }
  452. // Prune away any tokens on this frame that have no forward links.
  453. // [we don't do this in PruneForwardLinks because it would give us
  454. // a problem with dangling pointers].
  455. // It's called by PruneActiveTokens if any forward links have been pruned
  456. template <typename FST, typename Token>
  457. void LatticeFasterDecoderTpl<FST, Token>::PruneTokensForFrame(
  458. int32 frame_plus_one) {
  459. KALDI_ASSERT(frame_plus_one >= 0 && frame_plus_one < active_toks_.size());
  460. Token*& toks = active_toks_[frame_plus_one].toks;
  461. if (toks == NULL) KALDI_WARN << "No tokens alive [doing pruning]";
  462. Token *tok, *next_tok, *prev_tok = NULL;
  463. for (tok = toks; tok != NULL; tok = next_tok) {
  464. next_tok = tok->next;
  465. if (tok->extra_cost == std::numeric_limits<BaseFloat>::infinity()) {
  466. // token is unreachable from end of graph; (no forward links survived)
  467. // excise tok from list and delete tok.
  468. if (prev_tok != NULL)
  469. prev_tok->next = tok->next;
  470. else
  471. toks = tok->next;
  472. delete tok;
  473. num_toks_--;
  474. } else { // fetch next Token
  475. prev_tok = tok;
  476. }
  477. }
  478. }
  479. // Go backwards through still-alive tokens, pruning them, starting not from
  480. // the current frame (where we want to keep all tokens) but from the frame
  481. // before that. We go backwards through the frames and stop when we reach a
  482. // point where the delta-costs are not changing (and the delta controls when we
  483. // consider a cost to have "not changed").
  484. template <typename FST, typename Token>
  485. void LatticeFasterDecoderTpl<FST, Token>::PruneActiveTokens(BaseFloat delta) {
  486. int32 cur_frame_plus_one = NumFramesDecoded();
  487. int32 num_toks_begin = num_toks_;
  488. // The index "f" below represents a "frame plus one", i.e. you'd have to
  489. // subtract one to get the corresponding index for the decodable object.
  490. for (int32 f = cur_frame_plus_one - 1; f >= 0; f--) {
  491. // Reason why we need to prune forward links in this situation:
  492. // (1) we have never pruned them (new TokenList)
  493. // (2) we have not yet pruned the forward links to the next f,
  494. // after any of those tokens have changed their extra_cost.
  495. if (active_toks_[f].must_prune_forward_links) {
  496. bool extra_costs_changed = false, links_pruned = false;
  497. PruneForwardLinks(f, &extra_costs_changed, &links_pruned, delta);
  498. if (extra_costs_changed && f > 0) // any token has changed extra_cost
  499. active_toks_[f - 1].must_prune_forward_links = true;
  500. if (links_pruned) // any link was pruned
  501. active_toks_[f].must_prune_tokens = true;
  502. active_toks_[f].must_prune_forward_links = false; // job done
  503. }
  504. if (f + 1 < cur_frame_plus_one && // except for last f (no forward links)
  505. active_toks_[f + 1].must_prune_tokens) {
  506. PruneTokensForFrame(f + 1);
  507. active_toks_[f + 1].must_prune_tokens = false;
  508. }
  509. }
  510. KALDI_VLOG(4) << "PruneActiveTokens: pruned tokens from " << num_toks_begin
  511. << " to " << num_toks_;
  512. }
  513. template <typename FST, typename Token>
  514. void LatticeFasterDecoderTpl<FST, Token>::ComputeFinalCosts(
  515. unordered_map<Token*, BaseFloat>* final_costs,
  516. BaseFloat* final_relative_cost, BaseFloat* final_best_cost) const {
  517. KALDI_ASSERT(!decoding_finalized_);
  518. if (final_costs != NULL) final_costs->clear();
  519. const Elem* final_toks = toks_.GetList();
  520. BaseFloat infinity = std::numeric_limits<BaseFloat>::infinity();
  521. BaseFloat best_cost = infinity, best_cost_with_final = infinity;
  522. while (final_toks != NULL) {
  523. StateId state = final_toks->key;
  524. Token* tok = final_toks->val;
  525. const Elem* next = final_toks->tail;
  526. BaseFloat final_cost = fst_->Final(state).Value();
  527. BaseFloat cost = tok->tot_cost, cost_with_final = cost + final_cost;
  528. best_cost = std::min(cost, best_cost);
  529. best_cost_with_final = std::min(cost_with_final, best_cost_with_final);
  530. if (final_costs != NULL && final_cost != infinity)
  531. (*final_costs)[tok] = final_cost;
  532. final_toks = next;
  533. }
  534. if (final_relative_cost != NULL) {
  535. if (best_cost == infinity && best_cost_with_final == infinity) {
  536. // Likely this will only happen if there are no tokens surviving.
  537. // This seems the least bad way to handle it.
  538. *final_relative_cost = infinity;
  539. } else {
  540. *final_relative_cost = best_cost_with_final - best_cost;
  541. }
  542. }
  543. if (final_best_cost != NULL) {
  544. if (best_cost_with_final != infinity) { // final-state exists.
  545. *final_best_cost = best_cost_with_final;
  546. } else { // no final-state exists.
  547. *final_best_cost = best_cost;
  548. }
  549. }
  550. }
  551. template <typename FST, typename Token>
  552. void LatticeFasterDecoderTpl<FST, Token>::AdvanceDecoding(
  553. DecodableInterface* decodable, int32 max_num_frames) {
  554. if (std::is_same<FST, fst::Fst<fst::StdArc> >::value) {
  555. // if the type 'FST' is the FST base-class, then see if the FST type of fst_
  556. // is actually VectorFst or ConstFst. If so, call the AdvanceDecoding()
  557. // function after casting *this to the more specific type.
  558. if (fst_->Type() == "const") {
  559. LatticeFasterDecoderTpl<fst::ConstFst<fst::StdArc>, Token>* this_cast =
  560. reinterpret_cast<
  561. LatticeFasterDecoderTpl<fst::ConstFst<fst::StdArc>, Token>*>(
  562. this);
  563. this_cast->AdvanceDecoding(decodable, max_num_frames);
  564. return;
  565. } else if (fst_->Type() == "vector") {
  566. LatticeFasterDecoderTpl<fst::VectorFst<fst::StdArc>, Token>* this_cast =
  567. reinterpret_cast<
  568. LatticeFasterDecoderTpl<fst::VectorFst<fst::StdArc>, Token>*>(
  569. this);
  570. this_cast->AdvanceDecoding(decodable, max_num_frames);
  571. return;
  572. }
  573. }
  574. KALDI_ASSERT(!active_toks_.empty() && !decoding_finalized_ &&
  575. "You must call InitDecoding() before AdvanceDecoding");
  576. int32 num_frames_ready = decodable->NumFramesReady();
  577. // num_frames_ready must be >= num_frames_decoded, or else
  578. // the number of frames ready must have decreased (which doesn't
  579. // make sense) or the decodable object changed between calls
  580. // (which isn't allowed).
  581. KALDI_ASSERT(num_frames_ready >= NumFramesDecoded());
  582. int32 target_frames_decoded = num_frames_ready;
  583. if (max_num_frames >= 0)
  584. target_frames_decoded =
  585. std::min(target_frames_decoded, NumFramesDecoded() + max_num_frames);
  586. while (NumFramesDecoded() < target_frames_decoded) {
  587. if (NumFramesDecoded() % config_.prune_interval == 0) {
  588. PruneActiveTokens(config_.lattice_beam * config_.prune_scale);
  589. }
  590. BaseFloat cost_cutoff = ProcessEmitting(decodable);
  591. ProcessNonemitting(cost_cutoff);
  592. }
  593. }
  594. // FinalizeDecoding() is a version of PruneActiveTokens that we call
  595. // (optionally) on the final frame. Takes into account the final-prob of
  596. // tokens. This function used to be called PruneActiveTokensFinal().
  597. template <typename FST, typename Token>
  598. void LatticeFasterDecoderTpl<FST, Token>::FinalizeDecoding() {
  599. int32 final_frame_plus_one = NumFramesDecoded();
  600. int32 num_toks_begin = num_toks_;
  601. if (context_graph_ != nullptr) {
  602. UpdateFinalContext();
  603. }
  604. // PruneForwardLinksFinal() prunes final frame (with final-probs), and
  605. // sets decoding_finalized_.
  606. PruneForwardLinksFinal();
  607. for (int32 f = final_frame_plus_one - 1; f >= 0; f--) {
  608. bool b1, b2; // values not used.
  609. BaseFloat dontcare = 0.0; // delta of zero means we must always update
  610. PruneForwardLinks(f, &b1, &b2, dontcare);
  611. PruneTokensForFrame(f + 1);
  612. }
  613. PruneTokensForFrame(0);
  614. KALDI_VLOG(4) << "pruned tokens from " << num_toks_begin << " to "
  615. << num_toks_;
  616. }
  617. /// Gets the weight cutoff. Also counts the active tokens.
  618. template <typename FST, typename Token>
  619. BaseFloat LatticeFasterDecoderTpl<FST, Token>::GetCutoff(
  620. Elem* list_head, size_t* tok_count, BaseFloat* adaptive_beam,
  621. Elem** best_elem) {
  622. BaseFloat best_weight = std::numeric_limits<BaseFloat>::infinity();
  623. // positive == high cost == bad.
  624. size_t count = 0;
  625. if (config_.max_active == std::numeric_limits<int32>::max() &&
  626. config_.min_active == 0) {
  627. for (Elem* e = list_head; e != NULL; e = e->tail, count++) {
  628. BaseFloat w = static_cast<BaseFloat>(e->val->tot_cost);
  629. if (w < best_weight) {
  630. best_weight = w;
  631. if (best_elem) *best_elem = e;
  632. }
  633. }
  634. if (tok_count != NULL) *tok_count = count;
  635. if (adaptive_beam != NULL) *adaptive_beam = config_.beam;
  636. return best_weight + config_.beam;
  637. } else {
  638. tmp_array_.clear();
  639. for (Elem* e = list_head; e != NULL; e = e->tail, count++) {
  640. BaseFloat w = e->val->tot_cost;
  641. tmp_array_.push_back(w);
  642. if (w < best_weight) {
  643. best_weight = w;
  644. if (best_elem) *best_elem = e;
  645. }
  646. }
  647. if (tok_count != NULL) *tok_count = count;
  648. BaseFloat beam_cutoff = best_weight + config_.beam,
  649. min_active_cutoff = std::numeric_limits<BaseFloat>::infinity(),
  650. max_active_cutoff = std::numeric_limits<BaseFloat>::infinity();
  651. KALDI_VLOG(6) << "Number of tokens active on frame " << NumFramesDecoded()
  652. << " is " << tmp_array_.size();
  653. if (tmp_array_.size() > static_cast<size_t>(config_.max_active)) {
  654. std::nth_element(tmp_array_.begin(),
  655. tmp_array_.begin() + config_.max_active,
  656. tmp_array_.end());
  657. max_active_cutoff = tmp_array_[config_.max_active];
  658. }
  659. if (max_active_cutoff < beam_cutoff) { // max_active is tighter than beam.
  660. if (adaptive_beam)
  661. *adaptive_beam = max_active_cutoff - best_weight + config_.beam_delta;
  662. return max_active_cutoff;
  663. }
  664. if (tmp_array_.size() > static_cast<size_t>(config_.min_active)) {
  665. if (config_.min_active == 0) {
  666. min_active_cutoff = best_weight;
  667. } else {
  668. std::nth_element(
  669. tmp_array_.begin(), tmp_array_.begin() + config_.min_active,
  670. tmp_array_.size() > static_cast<size_t>(config_.max_active)
  671. ? tmp_array_.begin() + config_.max_active
  672. : tmp_array_.end());
  673. min_active_cutoff = tmp_array_[config_.min_active];
  674. }
  675. }
  676. if (min_active_cutoff > beam_cutoff) { // min_active is looser than beam.
  677. if (adaptive_beam)
  678. *adaptive_beam = min_active_cutoff - best_weight + config_.beam_delta;
  679. return min_active_cutoff;
  680. } else {
  681. *adaptive_beam = config_.beam;
  682. return beam_cutoff;
  683. }
  684. }
  685. }
  686. template <typename FST, typename Token>
  687. BaseFloat LatticeFasterDecoderTpl<FST, Token>::ProcessEmitting(
  688. DecodableInterface* decodable) {
  689. KALDI_ASSERT(active_toks_.size() > 0);
  690. int32 frame =
  691. active_toks_.size() - 1; // frame is the frame-index
  692. // (zero-based) used to get likelihoods
  693. // from the decodable object.
  694. active_toks_.resize(active_toks_.size() + 1);
  695. Elem* final_toks =
  696. toks_.Clear(); // analogous to swapping prev_toks_ / cur_toks_
  697. // in simple-decoder.h. Removes the Elems from
  698. // being indexed in the hash in toks_.
  699. Elem* best_elem = NULL;
  700. BaseFloat adaptive_beam;
  701. size_t tok_cnt;
  702. BaseFloat cur_cutoff =
  703. GetCutoff(final_toks, &tok_cnt, &adaptive_beam, &best_elem);
  704. KALDI_VLOG(6) << "Adaptive beam on frame " << NumFramesDecoded() << " is "
  705. << adaptive_beam;
  706. PossiblyResizeHash(
  707. tok_cnt); // This makes sure the hash is always big enough.
  708. BaseFloat next_cutoff = std::numeric_limits<BaseFloat>::infinity();
  709. // pruning "online" before having seen all tokens
  710. BaseFloat cost_offset = 0.0; // Used to keep probabilities in a good
  711. // dynamic range.
  712. // First process the best token to get a hopefully
  713. // reasonably tight bound on the next cutoff. The only
  714. // products of the next block are "next_cutoff" and "cost_offset".
  715. if (best_elem) {
  716. StateId state = best_elem->key;
  717. Token* tok = best_elem->val;
  718. cost_offset = -tok->tot_cost;
  719. for (fst::ArcIterator<FST> aiter(*fst_, state); !aiter.Done();
  720. aiter.Next()) {
  721. const Arc& arc = aiter.Value();
  722. if (arc.ilabel != 0) { // propagate..
  723. BaseFloat new_weight = arc.weight.Value() + cost_offset -
  724. decodable->LogLikelihood(frame, arc.ilabel) +
  725. tok->tot_cost;
  726. if (state != arc.nextstate) {
  727. new_weight += config_.length_penalty;
  728. }
  729. if (new_weight + adaptive_beam < next_cutoff)
  730. next_cutoff = new_weight + adaptive_beam;
  731. }
  732. }
  733. }
  734. // Store the offset on the acoustic likelihoods that we're applying.
  735. // Could just do cost_offsets_.push_back(cost_offset), but we
  736. // do it this way as it's more robust to future code changes.
  737. cost_offsets_.resize(frame + 1, 0.0);
  738. cost_offsets_[frame] = cost_offset;
  739. // the tokens are now owned here, in final_toks, and the hash is empty.
  740. // 'owned' is a complex thing here; the point is we need to call DeleteElem
  741. // on each elem 'e' to let toks_ know we're done with them.
  742. for (Elem *e = final_toks, *e_tail; e != NULL; e = e_tail) {
  743. // loop this way because we delete "e" as we go.
  744. StateId state = e->key;
  745. Token* tok = e->val;
  746. if (tok->tot_cost <= cur_cutoff) {
  747. for (fst::ArcIterator<FST> aiter(*fst_, state); !aiter.Done();
  748. aiter.Next()) {
  749. const Arc& arc = aiter.Value();
  750. if (arc.ilabel != 0) { // propagate..
  751. BaseFloat ac_cost = cost_offset -
  752. decodable->LogLikelihood(frame, arc.ilabel),
  753. graph_cost = arc.weight.Value();
  754. if (state != arc.nextstate) {
  755. graph_cost += config_.length_penalty;
  756. }
  757. BaseFloat cur_cost = tok->tot_cost,
  758. tot_cost = cur_cost + ac_cost + graph_cost;
  759. if (tot_cost >= next_cutoff)
  760. continue;
  761. else if (tot_cost + adaptive_beam < next_cutoff)
  762. next_cutoff =
  763. tot_cost + adaptive_beam; // prune by best current token
  764. // Note: the frame indexes into active_toks_ are one-based,
  765. // hence the + 1.
  766. Elem* e_next =
  767. FindOrAddToken(arc.nextstate, frame + 1, tot_cost, tok, NULL);
  768. // NULL: no change indicator needed
  769. float context_score = 0;
  770. int context_state = 0;
  771. if (context_graph_ != nullptr) {
  772. // Current ilabel is blank or current ilabel equals to previous.
  773. if (arc.ilabel - 1 == 0 || arc.ilabel == tok->ilabel) {
  774. context_state = tok->context_state;
  775. } else {
  776. context_state = context_graph_->GetNextState(
  777. tok->context_state, arc.ilabel - 1, &context_score);
  778. graph_cost -= context_score;
  779. tot_cost -= context_score;
  780. }
  781. }
  782. // Add ForwardLink from tok to next_tok (put on head of list
  783. // tok->links)
  784. tok->links = new ForwardLinkT(e_next->val, arc.ilabel, arc.olabel,
  785. graph_cost, ac_cost, tok->links);
  786. if (context_graph_ != nullptr) {
  787. tok->links->context_score = context_score;
  788. }
  789. }
  790. } // for all arcs
  791. }
  792. e_tail = e->tail;
  793. toks_.Delete(e); // delete Elem
  794. }
  795. return next_cutoff;
  796. }
  797. // static inline
  798. template <typename FST, typename Token>
  799. void LatticeFasterDecoderTpl<FST, Token>::DeleteForwardLinks(Token* tok) {
  800. ForwardLinkT *l = tok->links, *m;
  801. while (l != NULL) {
  802. m = l->next;
  803. delete l;
  804. l = m;
  805. }
  806. tok->links = NULL;
  807. }
  808. template <typename FST, typename Token>
  809. void LatticeFasterDecoderTpl<FST, Token>::ProcessNonemitting(BaseFloat cutoff) {
  810. KALDI_ASSERT(!active_toks_.empty());
  811. int32 frame = static_cast<int32>(active_toks_.size()) - 2;
  812. // Note: "frame" is the time-index we just processed, or -1 if
  813. // we are processing the nonemitting transitions before the
  814. // first frame (called from InitDecoding()).
  815. // Processes nonemitting arcs for one frame. Propagates within toks_.
  816. // Note-- this queue structure is not very optimal as
  817. // it may cause us to process states unnecessarily (e.g. more than once),
  818. // but in the baseline code, turning this vector into a set to fix this
  819. // problem did not improve overall speed.
  820. KALDI_ASSERT(queue_.empty());
  821. if (toks_.GetList() == NULL) {
  822. if (!warned_) {
  823. KALDI_WARN << "Error, no surviving tokens: frame is " << frame;
  824. warned_ = true;
  825. }
  826. }
  827. int before = 0, after = 0;
  828. for (const Elem* e = toks_.GetList(); e != NULL; e = e->tail) {
  829. StateId state = e->key;
  830. if (fst_->NumInputEpsilons(state) != 0) queue_.push_back(e);
  831. ++before;
  832. }
  833. while (!queue_.empty()) {
  834. ++after;
  835. const Elem* e = queue_.back();
  836. queue_.pop_back();
  837. StateId state = e->key;
  838. Token* tok =
  839. e->val; // would segfault if e is a NULL pointer but this can't happen.
  840. BaseFloat cur_cost = tok->tot_cost;
  841. if (cur_cost >= cutoff) // Don't bother processing successors.
  842. continue;
  843. // If "tok" has any existing forward links, delete them,
  844. // because we're about to regenerate them. This is a kind
  845. // of non-optimality (remember, this is the simple decoder),
  846. // but since most states are emitting it's not a huge issue.
  847. DeleteForwardLinks(tok); // necessary when re-visiting
  848. tok->links = NULL;
  849. for (fst::ArcIterator<FST> aiter(*fst_, state); !aiter.Done();
  850. aiter.Next()) {
  851. const Arc& arc = aiter.Value();
  852. if (arc.ilabel == 0) { // propagate nonemitting only...
  853. BaseFloat graph_cost = arc.weight.Value(),
  854. tot_cost = cur_cost + graph_cost;
  855. if (tot_cost < cutoff) {
  856. bool changed;
  857. Elem* e_new =
  858. FindOrAddToken(arc.nextstate, frame + 1, tot_cost, tok, &changed);
  859. if (context_graph_ != nullptr && changed) {
  860. e_new->val->context_state = tok->context_state;
  861. }
  862. tok->links = new ForwardLinkT(e_new->val, 0, arc.olabel, graph_cost,
  863. 0, tok->links);
  864. // "changed" tells us whether the new token has a different
  865. // cost from before, or is new [if so, add into queue].
  866. if (changed && fst_->NumInputEpsilons(arc.nextstate) != 0)
  867. queue_.push_back(e_new);
  868. }
  869. }
  870. } // for all arcs
  871. } // while queue not empty
  872. KALDI_VLOG(3) << "ProcessNonemitting " << before << " " << after;
  873. }
  874. template <typename FST, typename Token>
  875. void LatticeFasterDecoderTpl<FST, Token>::DeleteElems(Elem* list) {
  876. for (Elem *e = list, *e_tail; e != NULL; e = e_tail) {
  877. e_tail = e->tail;
  878. toks_.Delete(e);
  879. }
  880. }
  881. template <typename FST, typename Token>
  882. void LatticeFasterDecoderTpl<
  883. FST, Token>::ClearActiveTokens() { // a cleanup routine, at utt end/begin
  884. for (size_t i = 0; i < active_toks_.size(); i++) {
  885. // Delete all tokens alive on this frame, and any forward
  886. // links they may have.
  887. for (Token* tok = active_toks_[i].toks; tok != NULL;) {
  888. DeleteForwardLinks(tok);
  889. Token* next_tok = tok->next;
  890. delete tok;
  891. num_toks_--;
  892. tok = next_tok;
  893. }
  894. }
  895. active_toks_.clear();
  896. KALDI_ASSERT(num_toks_ == 0);
  897. }
  898. template <typename FST, typename Token>
  899. void LatticeFasterDecoderTpl<FST, Token>::UpdateFinalContext() {
  900. int frame = active_toks_.size() - 1;
  901. for (Token* tok = active_toks_[frame].toks; tok != nullptr; tok = tok->next) {
  902. if (tok->context_state > 0 &&
  903. !context_graph_->IsFinalState(tok->context_state)) {
  904. int context_state = tok->context_state;
  905. float context_score = 0;
  906. tok->context_state = context_graph_->GetNextState(
  907. context_state, fst::kNoLabel, &context_score);
  908. tok->tot_cost -= context_score;
  909. if (nullptr != tok->links) {
  910. tok->links->context_score = context_score;
  911. }
  912. KALDI_VLOG(2) << "Final context state " << context_state
  913. << " context_score " << context_score;
  914. }
  915. }
  916. }
  917. // static
  918. template <typename FST, typename Token>
  919. void LatticeFasterDecoderTpl<FST, Token>::TopSortTokens(
  920. Token* tok_list, std::vector<Token*>* topsorted_list) {
  921. unordered_map<Token*, int32> token2pos;
  922. using std::unordered_set;
  923. typedef typename unordered_map<Token*, int32>::iterator IterType;
  924. int32 num_toks = 0;
  925. for (Token* tok = tok_list; tok != NULL; tok = tok->next) num_toks++;
  926. int32 cur_pos = 0;
  927. // We assign the tokens numbers num_toks - 1, ... , 2, 1, 0.
  928. // This is likely to be in closer to topological order than
  929. // if we had given them ascending order, because of the way
  930. // new tokens are put at the front of the list.
  931. for (Token* tok = tok_list; tok != NULL; tok = tok->next)
  932. token2pos[tok] = num_toks - ++cur_pos;
  933. unordered_set<Token*> reprocess;
  934. for (IterType iter = token2pos.begin(); iter != token2pos.end(); ++iter) {
  935. Token* tok = iter->first;
  936. int32 pos = iter->second;
  937. for (ForwardLinkT* link = tok->links; link != NULL; link = link->next) {
  938. if (link->ilabel == 0) {
  939. // We only need to consider epsilon links, since non-epsilon links
  940. // transition between frames and this function only needs to sort a list
  941. // of tokens from a single frame.
  942. IterType following_iter = token2pos.find(link->next_tok);
  943. if (following_iter != token2pos.end()) { // another token on this
  944. // frame, so must consider it.
  945. int32 next_pos = following_iter->second;
  946. if (next_pos < pos) { // reassign the position of the next Token.
  947. following_iter->second = cur_pos++;
  948. reprocess.insert(link->next_tok);
  949. }
  950. }
  951. }
  952. }
  953. // In case we had previously assigned this token to be reprocessed, we can
  954. // erase it from that set because it's "happy now" (we just processed it).
  955. reprocess.erase(tok);
  956. }
  957. size_t max_loop = 1000000,
  958. loop_count; // max_loop is to detect epsilon cycles.
  959. for (loop_count = 0; !reprocess.empty() && loop_count < max_loop;
  960. ++loop_count) {
  961. std::vector<Token*> reprocess_vec;
  962. for (typename unordered_set<Token*>::iterator iter = reprocess.begin();
  963. iter != reprocess.end(); ++iter)
  964. reprocess_vec.push_back(*iter);
  965. reprocess.clear();
  966. for (typename std::vector<Token*>::iterator iter = reprocess_vec.begin();
  967. iter != reprocess_vec.end(); ++iter) {
  968. Token* tok = *iter;
  969. int32 pos = token2pos[tok];
  970. // Repeat the processing we did above (for comments, see above).
  971. for (ForwardLinkT* link = tok->links; link != NULL; link = link->next) {
  972. if (link->ilabel == 0) {
  973. IterType following_iter = token2pos.find(link->next_tok);
  974. if (following_iter != token2pos.end()) {
  975. int32 next_pos = following_iter->second;
  976. if (next_pos < pos) {
  977. following_iter->second = cur_pos++;
  978. reprocess.insert(link->next_tok);
  979. }
  980. }
  981. }
  982. }
  983. }
  984. }
  985. KALDI_ASSERT(loop_count < max_loop &&
  986. "Epsilon loops exist in your decoding "
  987. "graph (this is not allowed!)");
  988. topsorted_list->clear();
  989. topsorted_list->resize(cur_pos,
  990. NULL); // create a list with NULLs in between.
  991. for (IterType iter = token2pos.begin(); iter != token2pos.end(); ++iter)
  992. (*topsorted_list)[iter->second] = iter->first;
  993. }
  994. // Instantiate the template for the combination of token types and FST types
  995. // that we'll need.
  996. template class LatticeFasterDecoderTpl<fst::Fst<fst::StdArc>,
  997. decoder::StdToken>;
  998. template class LatticeFasterDecoderTpl<fst::VectorFst<fst::StdArc>,
  999. decoder::StdToken>;
  1000. template class LatticeFasterDecoderTpl<fst::ConstFst<fst::StdArc>,
  1001. decoder::StdToken>;
  1002. // template class LatticeFasterDecoderTpl<fst::ConstGrammarFst,
  1003. // decoder::StdToken>; template class
  1004. // LatticeFasterDecoderTpl<fst::VectorGrammarFst, decoder::StdToken>;
  1005. template class LatticeFasterDecoderTpl<fst::Fst<fst::StdArc>,
  1006. decoder::BackpointerToken>;
  1007. template class LatticeFasterDecoderTpl<fst::VectorFst<fst::StdArc>,
  1008. decoder::BackpointerToken>;
  1009. template class LatticeFasterDecoderTpl<fst::ConstFst<fst::StdArc>,
  1010. decoder::BackpointerToken>;
  1011. // template class LatticeFasterDecoderTpl<fst::ConstGrammarFst,
  1012. // decoder::BackpointerToken>; template class
  1013. // LatticeFasterDecoderTpl<fst::VectorGrammarFst, decoder::BackpointerToken>;
  1014. } // end namespace kaldi.