lattice-faster-decoder.cc 43 KB

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