latgen-faster-mapped-parallel.cc
Go to the documentation of this file.
1 // bin/latgen-faster-mapped-parallel.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation, Karel Vesely
4 // 2013 Johns Hopkins University (author: Daniel Povey)
5 // 2013 GoVIvace Inc. (author: Nagendra Goel)
6 // 2014 Guoguo Chen
7 
8 // See ../../COPYING for clarification regarding multiple authors
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19 // MERCHANTABLITY OR NON-INFRINGEMENT.
20 // See the Apache 2 License for the specific language governing permissions and
21 // limitations under the License.
22 
23 
24 #include "base/kaldi-common.h"
25 #include "util/common-utils.h"
26 #include "tree/context-dep.h"
27 #include "hmm/transition-model.h"
28 #include "fstext/fstext-lib.h"
31 #include "base/timer.h"
32 #include "util/kaldi-thread.h"
33 
34 int main(int argc, char *argv[]) {
35  try {
36  using namespace kaldi;
37  typedef kaldi::int32 int32;
38  using fst::SymbolTable;
39  using fst::Fst;
40  using fst::StdArc;
41 
42  const char *usage =
43  "Generate lattices, reading log-likelihoods as matrices, using multiple decoding threads\n"
44  " (model is needed only for the integer mappings in its transition-model)\n"
45  "Usage: latgen-faster-mapped-parallel [options] trans-model-in (fst-in|fsts-rspecifier) loglikes-rspecifier"
46  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
47  ParseOptions po(usage);
48  Timer timer;
49  bool allow_partial = false;
50  BaseFloat acoustic_scale = 0.1;
52  TaskSequencerConfig sequencer_config; // has --num-threads option
53 
54  std::string word_syms_filename;
55  config.Register(&po);
56  sequencer_config.Register(&po);
57 
58  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
59 
60  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
61  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
62 
63  po.Read(argc, argv);
64 
65  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
66  po.PrintUsage();
67  exit(1);
68  }
69 
70  std::string model_in_filename = po.GetArg(1),
71  fst_in_str = po.GetArg(2),
72  feature_rspecifier = po.GetArg(3),
73  lattice_wspecifier = po.GetArg(4),
74  words_wspecifier = po.GetOptArg(5),
75  alignment_wspecifier = po.GetOptArg(6);
76 
77  TransitionModel trans_model;
78  ReadKaldiObject(model_in_filename, &trans_model);
79 
80  bool determinize = config.determinize_lattice;
81  CompactLatticeWriter compact_lattice_writer;
82  LatticeWriter lattice_writer;
83  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
84  : lattice_writer.Open(lattice_wspecifier)))
85  KALDI_ERR << "Could not open table for writing lattices: "
86  << lattice_wspecifier;
87 
88  Int32VectorWriter words_writer(words_wspecifier);
89 
90  Int32VectorWriter alignment_writer(alignment_wspecifier);
91 
92  fst::SymbolTable *word_syms = NULL;
93  if (word_syms_filename != "")
94  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
95  KALDI_ERR << "Could not read symbol table from file "
96  << word_syms_filename;
97 
98  double tot_like = 0.0;
99  kaldi::int64 frame_count = 0;
100  int num_success = 0, num_fail = 0;
101  Fst<StdArc> *decode_fst = NULL; // only used if there is a single
102  // decoding graph.
103 
104  TaskSequencer<DecodeUtteranceLatticeFasterClass> sequencer(sequencer_config);
105  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
106  SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
107  // Input FST is just one FST, not a table of FSTs.
108  decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
109 
110  {
111  for (; !loglike_reader.Done(); loglike_reader.Next()) {
112  std::string utt = loglike_reader.Key();
113  Matrix<BaseFloat> *loglikes =
114  new Matrix<BaseFloat>(loglike_reader.Value());
115  loglike_reader.FreeCurrent();
116  if (loglikes->NumRows() == 0) {
117  KALDI_WARN << "Zero-length utterance: " << utt;
118  num_fail++;
119  delete loglikes;
120  continue;
121  }
122 
123  LatticeFasterDecoder *decoder = new LatticeFasterDecoder(*decode_fst,
124  config);
125  DecodableMatrixScaledMapped *decodable =
126  new DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
129  decoder, decodable, trans_model, word_syms, utt,
130  acoustic_scale, determinize, allow_partial, &alignment_writer,
131  &words_writer, &compact_lattice_writer, &lattice_writer,
132  &tot_like, &frame_count, &num_success, &num_fail, NULL);
133 
134  sequencer.Run(task); // takes ownership of "task",
135  // and will delete it when done.
136  }
137  }
138  } else { // We have different FSTs for different utterances.
139  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
140  RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
141  for (; !fst_reader.Done(); fst_reader.Next()) {
142  std::string utt = fst_reader.Key();
143  if (!loglike_reader.HasKey(utt)) {
144  KALDI_WARN << "Not decoding utterance " << utt
145  << " because no loglikes available.";
146  num_fail++;
147  continue;
148  }
149  const Matrix<BaseFloat> *loglikes =
150  new Matrix<BaseFloat>(loglike_reader.Value(utt));
151  if (loglikes->NumRows() == 0) {
152  KALDI_WARN << "Zero-length utterance: " << utt;
153  num_fail++;
154  delete loglikes;
155  continue;
156  }
157  fst::VectorFst<StdArc> *fst =
158  new fst::VectorFst<StdArc>(fst_reader.Value());
159  LatticeFasterDecoder *decoder =
160  new LatticeFasterDecoder(config, fst);
161  DecodableMatrixScaledMapped *decodable = new
162  DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
165  decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
166  determinize, allow_partial, &alignment_writer, &words_writer,
167  &compact_lattice_writer, &lattice_writer, &tot_like,
168  &frame_count, &num_success, &num_fail, NULL);
169  sequencer.Run(task); // takes ownership of "task",
170  // and will delete it when done.
171  }
172  }
173  sequencer.Wait();
174 
175  delete decode_fst;
176 
177  double elapsed = timer.Elapsed();
178  KALDI_LOG << "Decoded with " << sequencer_config.num_threads << " threads.";
179  KALDI_LOG << "Time taken "<< elapsed
180  << "s: real-time factor per thread assuming 100 frames/sec is "
181  << (sequencer_config.num_threads*elapsed*100.0/frame_count);
182  KALDI_LOG << "Done " << num_success << " utterances, failed for "
183  << num_fail;
184  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
185  << frame_count<<" frames.";
186 
187  delete word_syms;
188  if (num_success != 0) return 0;
189  else return 1;
190  } catch(const std::exception &e) {
191  std::cerr << e.what();
192  return -1;
193  }
194 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
void Run(C *c)
This function takes ownership of the pointer "c", and will delete it in the same sequence as Run was ...
Definition: kaldi-thread.h:190
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
int main(int argc, char *argv[])
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
float BaseFloat
Definition: kaldi-types.h:29
This class basically does the same job as the function DecodeUtteranceLatticeFaster, but in a way that allows us to build a multi-threaded command line program more easily.
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
This is the "normal" lattice-generating decoder.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
LatticeFasterDecoderTpl< fst::StdFst, decoder::StdToken > LatticeFasterDecoder
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
std::string GetOptArg(int param) const
void Register(OptionsItf *opts)
Definition: kaldi-thread.h:160