Skip to content

Commit 7e13314

Browse files
author
Zhehuai Chen
committed
The previous code is 30% slower than the baseline.
Improvements: 1. with the same config, the previous code will result in 10% more tokens in each frame compared with baseline. The reason is because GetCutoff function is over emitting tokens, while for baseline it is over emitting&non emiting tokens. (please check it use --verbose 6 and the number of all tokens is shown in line 895, but not in line 693, which only contains emitting) Hence we should use 10% less max_active config. 2. the baseline use kaldi version HashList, while the previous code use std::unordered_map. please still use HashList or its variants. The reason is because HashList is allocating memory in Pool/Block fashion, hence it is 10% faster than std::unordered_map. To show this problem, I temporally do a hack in line 247, plese check it After these two improvements, the current code is with similar speed as the baseline. HOWEVER, please check the quality of 1-best & lattice, I have discuss about my worries here kaldi-asr#3061 (review)
1 parent 4b30697 commit 7e13314

File tree

4 files changed

+604
-1
lines changed

4 files changed

+604
-1
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// bin/latgen-faster-mapped.cc
2+
3+
// Copyright 2009-2012 Microsoft Corporation, Karel Vesely
4+
// 2013 Johns Hopkins University (author: Daniel Povey)
5+
// 2014 Guoguo Chen
6+
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+
22+
23+
#include "base/kaldi-common.h"
24+
#include "util/common-utils.h"
25+
#include "tree/context-dep.h"
26+
#include "hmm/transition-model.h"
27+
#include "fstext/fstext-lib.h"
28+
#include "decoder/decoder-wrappers.h"
29+
#include "decoder/decodable-matrix.h"
30+
#include "base/timer.h"
31+
32+
33+
int main(int argc, char *argv[]) {
34+
try {
35+
using namespace kaldi;
36+
typedef kaldi::int32 int32;
37+
using fst::SymbolTable;
38+
using fst::Fst;
39+
using fst::StdArc;
40+
41+
const char *usage =
42+
"Generate lattices, reading log-likelihoods as matrices\n"
43+
" (model is needed only for the integer mappings in its transition-model)\n"
44+
"Usage: latgen-faster-mapped [options] trans-model-in (fst-in|fsts-rspecifier) loglikes-rspecifier"
45+
" lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
46+
ParseOptions po(usage);
47+
Timer timer;
48+
bool allow_partial = false;
49+
BaseFloat acoustic_scale = 0.1;
50+
LatticeFasterDecoderCombineConfig config;
51+
52+
std::string word_syms_filename;
53+
config.Register(&po);
54+
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55+
56+
po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
57+
po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
58+
59+
po.Read(argc, argv);
60+
61+
if (po.NumArgs() < 4 || po.NumArgs() > 6) {
62+
po.PrintUsage();
63+
exit(1);
64+
}
65+
66+
std::string model_in_filename = po.GetArg(1),
67+
fst_in_str = po.GetArg(2),
68+
feature_rspecifier = po.GetArg(3),
69+
lattice_wspecifier = po.GetArg(4),
70+
words_wspecifier = po.GetOptArg(5),
71+
alignment_wspecifier = po.GetOptArg(6);
72+
73+
TransitionModel trans_model;
74+
ReadKaldiObject(model_in_filename, &trans_model);
75+
76+
bool determinize = config.determinize_lattice;
77+
CompactLatticeWriter compact_lattice_writer;
78+
LatticeWriter lattice_writer;
79+
if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
80+
: lattice_writer.Open(lattice_wspecifier)))
81+
KALDI_ERR << "Could not open table for writing lattices: "
82+
<< lattice_wspecifier;
83+
84+
Int32VectorWriter words_writer(words_wspecifier);
85+
86+
Int32VectorWriter alignment_writer(alignment_wspecifier);
87+
88+
fst::SymbolTable *word_syms = NULL;
89+
if (word_syms_filename != "")
90+
if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
91+
KALDI_ERR << "Could not read symbol table from file "
92+
<< word_syms_filename;
93+
94+
double tot_like = 0.0;
95+
kaldi::int64 frame_count = 0;
96+
int num_success = 0, num_fail = 0;
97+
98+
if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
99+
SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
100+
// Input FST is just one FST, not a table of FSTs.
101+
Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
102+
timer.Reset();
103+
104+
{
105+
LatticeFasterDecoderCombine decoder(*decode_fst, config);
106+
107+
for (; !loglike_reader.Done(); loglike_reader.Next()) {
108+
std::string utt = loglike_reader.Key();
109+
Matrix<BaseFloat> loglikes (loglike_reader.Value());
110+
loglike_reader.FreeCurrent();
111+
if (loglikes.NumRows() == 0) {
112+
KALDI_WARN << "Zero-length utterance: " << utt;
113+
num_fail++;
114+
continue;
115+
}
116+
117+
DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
118+
119+
double like;
120+
if (DecodeUtteranceLatticeFasterCombine(
121+
decoder, decodable, trans_model, word_syms, utt,
122+
acoustic_scale, determinize, allow_partial, &alignment_writer,
123+
&words_writer, &compact_lattice_writer, &lattice_writer,
124+
&like)) {
125+
tot_like += like;
126+
frame_count += loglikes.NumRows();
127+
num_success++;
128+
} else num_fail++;
129+
}
130+
}
131+
delete decode_fst; // delete this only after decoder goes out of scope.
132+
} else { // We have different FSTs for different utterances.
133+
SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
134+
RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
135+
for (; !fst_reader.Done(); fst_reader.Next()) {
136+
std::string utt = fst_reader.Key();
137+
if (!loglike_reader.HasKey(utt)) {
138+
KALDI_WARN << "Not decoding utterance " << utt
139+
<< " because no loglikes available.";
140+
num_fail++;
141+
continue;
142+
}
143+
const Matrix<BaseFloat> &loglikes = loglike_reader.Value(utt);
144+
if (loglikes.NumRows() == 0) {
145+
KALDI_WARN << "Zero-length utterance: " << utt;
146+
num_fail++;
147+
continue;
148+
}
149+
LatticeFasterDecoderCombine decoder(fst_reader.Value(), config);
150+
DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
151+
double like;
152+
if (DecodeUtteranceLatticeFasterCombine(
153+
decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
154+
determinize, allow_partial, &alignment_writer, &words_writer,
155+
&compact_lattice_writer, &lattice_writer, &like)) {
156+
tot_like += like;
157+
frame_count += loglikes.NumRows();
158+
num_success++;
159+
} else num_fail++;
160+
}
161+
}
162+
163+
double elapsed = timer.Elapsed();
164+
KALDI_LOG << "Time taken "<< elapsed
165+
<< "s: real-time factor assuming 100 frames/sec is "
166+
<< (elapsed*100.0/frame_count);
167+
KALDI_LOG << "Done " << num_success << " utterances, failed for "
168+
<< num_fail;
169+
KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
170+
<< frame_count<<" frames.";
171+
172+
delete word_syms;
173+
if (num_success != 0) return 0;
174+
else return 1;
175+
} catch(const std::exception &e) {
176+
std::cerr << e.what();
177+
return -1;
178+
}
179+
}

src/decoder/lattice-faster-decoder-combine.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ void LatticeFasterDecoderCombineTpl<FST, Token>::ProcessForFrame(
892892
} // end of while loop
893893
frame_processed_[frame] = true;
894894
frame_processed_[frame + 1] = false;
895+
KALDI_VLOG(6) << "toks after: " << cur_toks_.size();
895896
}
896897

897898

src/decoder/lattice-faster-decoder-combine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "lat/kaldi-lattice.h"
3333
#include "decoder/grammar-fst.h"
3434
#include "decoder/lattice-faster-decoder.h"
35+
#include "memory.h"
3536

3637
namespace kaldi {
3738

@@ -242,7 +243,8 @@ class LatticeFasterDecoderCombineTpl {
242243
using Weight = typename Arc::Weight;
243244
using ForwardLinkT = decodercombine::ForwardLink<Token>;
244245

245-
using StateIdToTokenMap = typename std::unordered_map<StateId, Token*>;
246+
//using StateIdToTokenMap = typename std::unordered_map<StateId, Token*>;
247+
using StateIdToTokenMap = typename std::unordered_map<StateId, Token*, std::hash<StateId>, std::equal_to<StateId>, fkaldi::PoolAllocator<std::pair<const StateId, Token*>>>;
246248
using IterType = typename StateIdToTokenMap::const_iterator;
247249

248250
// Instantiate this class once for each thing you have to decode.

0 commit comments

Comments
 (0)