Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Adding suggestions from code review.

Co-authored-by: Cy 'kkm' Katsnelson <[email protected]>
  • Loading branch information
KarelVesely84 and kkm000 authored Jun 21, 2021
1 parent f184046 commit 60fa225
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions egs/wsj/s5/steps/nnet3/decode_compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ echo "$0 $@" # Print the command line for logging
if [ $# -ne 3 ]; then
echo "Usage: $0 [options] <graph-dir> <data-dir> <decode-dir>"
echo "e.g.: steps/nnet3/decode.sh --nj 8 \\"
echo "--online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\"
echo " --online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\"
echo " exp/tri4b/graph_bg data/test_eval92_hires $dir/decode_bg_eval92"
echo "main options (for others, see top of script file)"
echo " --config <config-file> # config containing options"
Expand All @@ -67,7 +67,7 @@ fi
graphdir=$1
data=$2
dir=$3
srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
srcdir=$(dirname $dir) # Assume model directory one level up from decoding directory.
model=$srcdir/$iter.mdl

[ -z "$boosting_graphs" ] && echo "Error, \$boosting_graphs have to be set !" && exit 1
Expand Down
28 changes: 17 additions & 11 deletions src/latbin/lattice-compose-fsts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ int main(int argc, char *argv[]) {

bool write_compact = true;
int32 num_states_cache = 50000;
int32 phi_label = fst::kNoLabel; // == -1
po.Register("write-compact", &write_compact, "If true, write in normal (compact) form.");
po.Register("phi-label", &phi_label, "If >0, the label on backoff arcs of the LM");
int32 phi_label = fst::kNoLabel; // == -1
po.Register("write-compact", &write_compact,
"If true, write in normal (compact) form.");
po.Register("phi-label", &phi_label,
"If >0, the label on backoff arcs of the LM");
po.Register("num-states-cache", &num_states_cache,
"Number of states we cache when mapping LM FST to lattice type. "
"More -> more memory but faster.");
"Number of states we cache when mapping LM FST to lattice type."
" More -> more memory but faster.");
po.Read(argc, argv);

if (po.NumArgs() != 3) {
Expand All @@ -72,10 +74,11 @@ int main(int argc, char *argv[]) {
CompactLatticeWriter compact_lattice_writer;
LatticeWriter lattice_writer;

if (write_compact)
if (write_compact) {
compact_lattice_writer.Open(lats_wspecifier);
else
} else {
lattice_writer.Open(lats_wspecifier);
}

if (ClassifyRspecifier(arg2, NULL, NULL) == kNoRspecifier) {
std::string fst_rxfilename = arg2;
Expand Down Expand Up @@ -105,8 +108,11 @@ int main(int argc, char *argv[]) {
Lattice lat1 = lattice_reader1.Value();
ArcSort(&lat1, fst::OLabelCompare<LatticeArc>());
Lattice composed_lat;
if (phi_label > 0) PhiCompose(lat1, mapped_fst2, phi_label, &composed_lat);
else Compose(lat1, mapped_fst2, &composed_lat);
if (phi_label > 0) {
PhiCompose(lat1, mapped_fst2, phi_label, &composed_lat);
} else {
Compose(lat1, mapped_fst2, &composed_lat);
}
if (composed_lat.Start() == fst::kNoStateId) {
KALDI_WARN << "Empty lattice for utterance " << key << " (incompatible LM?)";
n_fail++;
Expand All @@ -123,7 +129,7 @@ int main(int argc, char *argv[]) {
}
delete fst2;
} else {
// composing with each utterance with different fst,
// Compose each utterance with its matching (by key) FST.
std::string fst_rspecifier2 = arg2;
RandomAccessTableReader<fst::VectorFstHolder> fst_reader2(fst_rspecifier2);

Expand All @@ -135,7 +141,7 @@ int main(int argc, char *argv[]) {

if (!fst_reader2.HasKey(key)) {
KALDI_WARN << "Not producing output for utterance " << key
<< " because not present in second table.";
<< " because it's not present in second table.";
n_fail++;
continue;
}
Expand Down
35 changes: 19 additions & 16 deletions src/nnet3bin/nnet3-latgen-faster-compose.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
// limitations under the License.


#include <fst/compose.h>
#include <fst/rmepsilon.h>

#include <memory>

#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "tree/context-dep.h"
#include "hmm/transition-model.h"
#include "fstext/fstext-lib.h"
#include "base/timer.h"
#include "decoder/decoder-wrappers.h"
#include "fstext/fstext-lib.h"
#include "hmm/transition-model.h"
#include "nnet3/nnet-am-decodable-simple.h"
#include "nnet3/nnet-utils.h"
#include "base/timer.h"

#include <fst/compose.h>
#include <fst/rmepsilon.h>
#include <memory>
#include "tree/context-dep.h"
#include "util/common-utils.h"


int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -121,9 +122,10 @@ int main(int argc, char *argv[]) {
CompactLatticeWriter compact_lattice_writer;
LatticeWriter lattice_writer;
if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
: lattice_writer.Open(lattice_wspecifier)))
: lattice_writer.Open(lattice_wspecifier))) {
KALDI_ERR << "Could not open table for writing lattices: "
<< lattice_wspecifier;
<< lattice_wspecifier;
}

RandomAccessBaseFloatMatrixReader online_ivector_reader(
online_ivector_rspecifier);
Expand Down Expand Up @@ -213,13 +215,13 @@ int main(int argc, char *argv[]) {

timer_compose.Reset();

// RmEpsilon saved 30% of composition runtime...
// RmEpsilon saved 30% of composition runtime.
// - Note: we are loading 2-state graphs with eps back-link to the initial state.
if (boosting_fst.Properties(fst::kIEpsilons, true) != 0) {
fst::RmEpsilon(&boosting_fst);
}

// make sure boosting graph is sorted on ilabel,
// Make sure boosting graph is sorted on ilabel.
if (boosting_fst.Properties(fst::kILabelSorted, true) == 0) {
fst::ILabelCompare<StdArc> ilabel_comp;
fst::ArcSort(&boosting_fst, ilabel_comp);
Expand Down Expand Up @@ -267,7 +269,9 @@ int main(int argc, char *argv[]) {
tot_like += like;
frame_count += nnet_decodable.NumFramesReady();
num_success++;
} else num_fail++;
} else {
++num_fail;
}
}
}

Expand All @@ -286,8 +290,7 @@ int main(int argc, char *argv[]) {
<< (tot_like / frame_count) << " over "
<< frame_count << " frames.";

if (num_success != 0) return 0;
else return 1;
return num_success != 0 ? 0 : 1;
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
Expand Down

0 comments on commit 60fa225

Please sign in to comment.