Skip to content

Commit 0e031c2

Browse files
authored
[src,scripts,egs] Add lookahead graph decoding == dynamic graph composition (#3616)
1 parent 7a50987 commit 0e031c2

File tree

14 files changed

+860
-43
lines changed

14 files changed

+860
-43
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ GSYMS
151151
/tools/cub-1.8.0/
152152
/tools/cub
153153
/tools/python/
154+
/tools/ngram-1.3.7.tar.gz
155+
/tools/ngram-1.3.7/
154156

155157
# These CMakeLists.txt files are all genareted on the fly at the moment.
156158
# They are added here to avoid accidently checkin.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
. ./path.sh
4+
5+
# Example script for lookahead composition
6+
7+
lm=tgmed
8+
am=exp/chain_cleaned/tdnn_1d_sp
9+
testset=test_clean
10+
11+
# %WER 4.86 [ 2553 / 52576, 315 ins, 222 del, 2016 sub ] exp/chain_cleaned/tdnn_1d_sp/decode_test_clean_lookahead/wer_11_0.0
12+
# %WER 4.79 [ 2518 / 52576, 279 ins, 292 del, 1947 sub ] exp/chain_cleaned/tdnn_1d_sp/decode_test_clean_lookahead_arpa/wer_11_0.0
13+
# %WER 4.82 [ 2532 / 52576, 286 ins, 290 del, 1956 sub ] exp/chain_cleaned/tdnn_1d_sp/decode_test_clean_lookahead_arpa_fast/wer_11_0.0
14+
# %WER 4.86 [ 2553 / 52576, 314 ins, 222 del, 2017 sub ] exp/chain_cleaned/tdnn_1d_sp/decode_test_clean_lookahead_base/wer_11_0.0
15+
# %WER 4.86 [ 2553 / 52576, 315 ins, 222 del, 2016 sub ] exp/chain_cleaned/tdnn_1d_sp/decode_test_clean_lookahead_static/wer_11_0.0
16+
17+
18+
# Speed
19+
#
20+
# base 0.18 xRT
21+
# static 0.18 xRT
22+
# lookahead 0.29 xRT
23+
# arpa 0.35 xRT
24+
# arpa_fast 0.21 xRT
25+
26+
# Graph size
27+
#
28+
# Base 476 Mb
29+
# Static 621 Mb
30+
# Lookahead 48 Mb HCL + 77 Mb Grammar
31+
# Lookahead + OpenGrm 48 Mb HCL + 42 Mb Grammar
32+
33+
if [ ! -f "${KALDI_ROOT}/tools/openfst/lib/libfstlookahead.so" ]; then
34+
echo "Missing ${KALDI_ROOT}/tools/openfst/lib/libfstlookahead.so"
35+
echo "Make sure you compiled openfst with lookahead support. Run make in ${KALDI_ROOT}/tools after git pull."
36+
exit 1
37+
fi
38+
if [ ! -f "${KALDI_ROOT}/tools/openfst/bin/ngramread" ]; then
39+
echo "You appear to not have OpenGRM tools installed. Missing ${KALDI_ROOT}/tools/openfst/bin/ngramread"
40+
echo "cd to $KALDI_ROOT/tools and run extras/install_opengrm.sh."
41+
exit 1
42+
fi
43+
export LD_LIBRARY_PATH=${KALDI_ROOT}/tools/openfst/lib/fst
44+
45+
# Baseline
46+
utils/format_lm.sh data/lang data/local/lm/lm_${lm}.arpa.gz \
47+
data/local/dict/lexicon.txt data/lang_test_${lm}_base
48+
49+
utils/mkgraph.sh --self-loop-scale 1.0 --remove-oov \
50+
data/lang_test_${lm}_base ${am} ${am}/graph_${lm}_lookahead_base
51+
52+
steps/nnet3/decode.sh --nj 20 \
53+
--acwt 1.0 --post-decode-acwt 10.0 \
54+
--online-ivector-dir exp/nnet3_cleaned/ivectors_${testset}_hires \
55+
${am}/graph_${lm}_lookahead_base data/${testset}_hires ${am}/decode_${testset}_lookahead_base
56+
57+
utils/mkgraph_lookahead.sh --self-loop-scale 1.0 --remove-oov --compose-graph \
58+
data/lang_test_${lm}_base ${am} ${am}/graph_${lm}_lookahead
59+
60+
# Decode with statically composed lookahead graph
61+
steps/nnet3/decode.sh --nj 20 \
62+
--acwt 1.0 --post-decode-acwt 10.0 \
63+
--online-ivector-dir exp/nnet3_cleaned/ivectors_${testset}_hires \
64+
${am}/graph_${lm}_lookahead data/${testset}_hires ${am}/decode_${testset}_lookahead_static
65+
66+
# Decode with runtime composition
67+
steps/nnet3/decode_lookahead.sh --nj 20 \
68+
--acwt 1.0 --post-decode-acwt 10.0 \
69+
--online-ivector-dir exp/nnet3_cleaned/ivectors_${testset}_hires \
70+
${am}/graph_${lm}_lookahead data/${testset}_hires ${am}/decode_${testset}_lookahead
71+
72+
# Compile arpa graph
73+
utils/mkgraph_lookahead.sh --self-loop-scale 1.0 --compose-graph \
74+
data/lang_test_${lm}_base ${am} data/local/lm/lm_tgmed.arpa.gz ${am}/graph_${lm}_lookahead_arpa
75+
76+
# Decode with runtime composition
77+
steps/nnet3/decode_lookahead.sh --nj 20 \
78+
--acwt 1.0 --post-decode-acwt 10.0 \
79+
--online-ivector-dir exp/nnet3_cleaned/ivectors_${testset}_hires \
80+
${am}/graph_${lm}_lookahead_arpa data/${testset}_hires ${am}/decode_${testset}_lookahead_arpa
81+
82+
# Decode with runtime composition and tuned beams
83+
steps/nnet3/decode_lookahead.sh --nj 20 \
84+
--beam 12.0 --max-active 3000 \
85+
--acwt 1.0 --post-decode-acwt 10.0 \
86+
--online-ivector-dir exp/nnet3_cleaned/ivectors_${testset}_hires \
87+
${am}/graph_${lm}_lookahead_arpa data/${testset}_hires ${am}/decode_${testset}_lookahead_arpa_fast
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
. ./path.sh
4+
5+
# Example script for lookahead composition
6+
7+
lm=tgmed
8+
am=exp/chain_online_cmn/tdnn1k_sp
9+
testset=dev_clean_2
10+
11+
# %WER 10.32 [ 2078 / 20138, 201 ins, 275 del, 1602 sub ] exp/chain_online_cmn/tdnn1k_sp/decode_dev_clean_2_lookahead_base/wer_10_0.5
12+
# %WER 10.29 [ 2073 / 20138, 200 ins, 272 del, 1601 sub ] exp/chain_online_cmn/tdnn1k_sp/decode_dev_clean_2_lookahead_static/wer_10_0.5
13+
# %WER 10.25 [ 2064 / 20138, 192 ins, 277 del, 1595 sub ] exp/chain_online_cmn/tdnn1k_sp/decode_dev_clean_2_lookahead/wer_10_0.5
14+
# %WER 10.24 [ 2063 / 20138, 187 ins, 290 del, 1586 sub ] exp/chain_online_cmn/tdnn1k_sp/decode_dev_clean_2_lookahead_arpa/wer_10_0.5
15+
# %WER 10.29 [ 2072 / 20138, 228 ins, 242 del, 1602 sub ] exp/chain_online_cmn/tdnn1k_sp/decode_dev_clean_2_lookahead_arpa_fast/wer_9_0.5
16+
17+
# Speed
18+
#
19+
# base 0.29 xRT
20+
# static 0.31 xRT
21+
# lookahead 0.77 xRT
22+
# arpa 1.03 xRT
23+
# arpa_fast 0.31 xRT
24+
25+
# Graph size
26+
#
27+
# Base 461 Mb
28+
# Static 587 Mb
29+
# Lookahead 44 Mb HCL + 77 Mb Grammar
30+
# Lookahead + OpenGrm 44 Mb HCL + 42 Mb Grammar
31+
32+
if [ ! -f "${KALDI_ROOT}/tools/openfst/lib/libfstlookahead.so" ]; then
33+
echo "Missing ${KALDI_ROOT}/tools/openfst/lib/libfstlookahead.so"
34+
echo "Make sure you compiled openfst with lookahead support. Run make in ${KALDI_ROOT}/tools after git pull."
35+
exit 1
36+
fi
37+
if [ ! -f "${KALDI_ROOT}/tools/openfst/bin/ngramread" ]; then
38+
echo "You appear to not have OpenGRM tools installed. Missing ${KALDI_ROOT}/tools/openfst/bin/ngramread"
39+
echo "cd to $KALDI_ROOT/tools and run extras/install_opengrm.sh."
40+
exit 1
41+
fi
42+
export LD_LIBRARY_PATH=${KALDI_ROOT}/tools/openfst/lib/fst
43+
44+
# Baseline
45+
utils/format_lm.sh data/lang data/local/lm/lm_${lm}.arpa.gz \
46+
data/local/dict/lexicon.txt data/lang_test_${lm}_base
47+
48+
utils/mkgraph.sh --self-loop-scale 1.0 --remove-oov \
49+
data/lang_test_${lm}_base ${am} ${am}/graph_${lm}_lookahead_base
50+
51+
steps/nnet3/decode.sh --nj 20 \
52+
--acwt 1.0 --post-decode-acwt 10.0 \
53+
--online-ivector-dir exp/nnet3_online_cmn/ivectors_${testset}_hires \
54+
${am}/graph_${lm}_lookahead_base data/${testset}_hires ${am}/decode_${testset}_lookahead_base
55+
56+
utils/mkgraph_lookahead.sh --self-loop-scale 1.0 --remove-oov --compose-graph \
57+
data/lang_test_${lm}_base ${am} ${am}/graph_${lm}_lookahead
58+
59+
# Decode with statically composed lookahead graph
60+
steps/nnet3/decode.sh --nj 20 \
61+
--acwt 1.0 --post-decode-acwt 10.0 \
62+
--online-ivector-dir exp/nnet3_online_cmn/ivectors_${testset}_hires \
63+
${am}/graph_${lm}_lookahead data/${testset}_hires ${am}/decode_${testset}_lookahead_static
64+
65+
# Decode with runtime composition
66+
steps/nnet3/decode_lookahead.sh --nj 20 \
67+
--acwt 1.0 --post-decode-acwt 10.0 \
68+
--online-ivector-dir exp/nnet3_online_cmn/ivectors_${testset}_hires \
69+
${am}/graph_${lm}_lookahead data/${testset}_hires ${am}/decode_${testset}_lookahead
70+
71+
# Compile arpa graph
72+
utils/mkgraph_lookahead.sh --self-loop-scale 1.0 --compose-graph \
73+
data/lang_test_${lm}_base ${am} data/local/lm/lm_tgmed.arpa.gz ${am}/graph_${lm}_lookahead_arpa
74+
75+
# Decode with runtime composition
76+
steps/nnet3/decode_lookahead.sh --nj 20 \
77+
--acwt 1.0 --post-decode-acwt 10.0 \
78+
--online-ivector-dir exp/nnet3_online_cmn/ivectors_${testset}_hires \
79+
${am}/graph_${lm}_lookahead_arpa data/${testset}_hires ${am}/decode_${testset}_lookahead_arpa
80+
81+
# Decode with runtime composition and tuned beams
82+
steps/nnet3/decode_lookahead.sh --nj 20 \
83+
--beam 12.0 --max-active 3000 \
84+
--acwt 1.0 --post-decode-acwt 10.0 \
85+
--online-ivector-dir exp/nnet3_online_cmn/ivectors_${testset}_hires \
86+
${am}/graph_${lm}_lookahead_arpa data/${testset}_hires ${am}/decode_${testset}_lookahead_arpa_fast
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/bash
2+
3+
# Copyright 2019 Alpha Cephei Inc (Author: Nickolay Shmmyrev).
4+
# Copyright 2012-2015 Johns Hopkins University (Author: Daniel Povey).
5+
# Apache 2.0.
6+
7+
# This script does decoding with a neural-net with lookahead composition of HCL and G graphs.
8+
9+
# Begin configuration section.
10+
stage=1
11+
nj=4 # number of decoding jobs.
12+
acwt=0.1 # Just a default value, used for adaptation and beam-pruning..
13+
post_decode_acwt=1.0 # can be used in 'chain' systems to scale acoustics by 10 so the
14+
# regular scoring script works.
15+
cmd=run.pl
16+
beam=15.0
17+
frames_per_chunk=50
18+
max_active=7000
19+
min_active=200
20+
ivector_scale=1.0
21+
lattice_beam=8.0 # Beam we use in lattice generation.
22+
iter=final
23+
use_gpu=false # If true, will use a GPU, with nnet3-latgen-faster-batch.
24+
# In that case it is recommended to set num-threads to a large
25+
# number, e.g. 20 if you have that many free CPU slots on a GPU
26+
# node, and to use a small number of jobs.
27+
scoring_opts=
28+
skip_diagnostics=false
29+
skip_scoring=false
30+
extra_left_context=0
31+
extra_right_context=0
32+
extra_left_context_initial=-1
33+
extra_right_context_final=-1
34+
online_ivector_dir=
35+
minimize=false
36+
# End configuration section.
37+
38+
echo "$0 $@" # Print the command line for logging
39+
40+
[ -f ./path.sh ] && . ./path.sh; # source the path.
41+
. utils/parse_options.sh || exit 1;
42+
43+
if [ $# -ne 3 ]; then
44+
echo "Usage: $0 [options] <graph-dir> <data-dir> <decode-dir>"
45+
echo "e.g.: $0 --nj 8 \\"
46+
echo "--online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\"
47+
echo " exp/tri4b/graph_bg data/test_eval92_hires $dir/decode_bg_eval92"
48+
echo "main options (for others, see top of script file)"
49+
echo " --config <config-file> # config containing options"
50+
echo " --nj <nj> # number of parallel jobs"
51+
echo " --cmd <cmd> # Command to run in parallel with"
52+
echo " --beam <beam> # Decoding beam; default 15.0"
53+
echo " --iter <iter> # Iteration of model to decode; default is final."
54+
echo " --scoring-opts <string> # options to local/score.sh"
55+
echo " --num-threads <n> # number of threads to use, default 1."
56+
echo " --use-gpu <true|false> # default: false. If true, we recommend"
57+
echo " # to use large --num-threads as the graph"
58+
echo " # search becomes the limiting factor."
59+
exit 1;
60+
fi
61+
62+
graphdir=$1
63+
data=$2
64+
dir=$3
65+
srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
66+
model=$srcdir/$iter.mdl
67+
68+
69+
extra_files=
70+
if [ ! -z "$online_ivector_dir" ]; then
71+
steps/nnet2/check_ivectors_compatible.sh $srcdir $online_ivector_dir || exit 1
72+
extra_files="$online_ivector_dir/ivector_online.scp $online_ivector_dir/ivector_period"
73+
fi
74+
75+
#utils/lang/check_phones_compatible.sh {$srcdir,$graphdir}/phones.txt || exit 1
76+
77+
for f in $graphdir/HCLr.fst $graphdir/Gr.fst $graphdir/disambig_tid.int $data/feats.scp $model $extra_files; do
78+
[ ! -f $f ] && echo "$0: no such file $f" && exit 1;
79+
done
80+
81+
sdata=$data/split$nj;
82+
cmvn_opts=`cat $srcdir/cmvn_opts` || exit 1;
83+
thread_string=
84+
85+
mkdir -p $dir/log
86+
[[ -d $sdata && $data/feats.scp -ot $sdata ]] || split_data.sh $data $nj || exit 1;
87+
echo $nj > $dir/num_jobs
88+
89+
90+
## Set up features.
91+
echo "$0: feature type is raw"
92+
93+
feats="ark,s,cs:apply-cmvn $cmvn_opts --utt2spk=ark:$sdata/JOB/utt2spk scp:$sdata/JOB/cmvn.scp scp:$sdata/JOB/feats.scp ark:- |"
94+
95+
if [ ! -z "$online_ivector_dir" ]; then
96+
ivector_period=$(cat $online_ivector_dir/ivector_period) || exit 1;
97+
ivector_opts="--online-ivectors=scp:$online_ivector_dir/ivector_online.scp --online-ivector-period=$ivector_period"
98+
fi
99+
100+
if [ "$post_decode_acwt" == 1.0 ]; then
101+
lat_wspecifier="ark:|gzip -c >$dir/lat.JOB.gz"
102+
else
103+
lat_wspecifier="ark:|lattice-scale --acoustic-scale=$post_decode_acwt ark:- ark:- | gzip -c >$dir/lat.JOB.gz"
104+
fi
105+
106+
frame_subsampling_opt=
107+
if [ -f $srcdir/frame_subsampling_factor ]; then
108+
# e.g. for 'chain' systems
109+
frame_subsampling_opt="--frame-subsampling-factor=$(cat $srcdir/frame_subsampling_factor)"
110+
fi
111+
112+
if [ $stage -le 1 ]; then
113+
$cmd $queue_opt JOB=1:$nj $dir/log/decode.JOB.log \
114+
nnet3-latgen-faster-lookahead $ivector_opts $frame_subsampling_opt \
115+
--frames-per-chunk=$frames_per_chunk \
116+
--extra-left-context=$extra_left_context \
117+
--extra-right-context=$extra_right_context \
118+
--extra-left-context-initial=$extra_left_context_initial \
119+
--extra-right-context-final=$extra_right_context_final \
120+
--minimize=$minimize --max-active=$max_active --min-active=$min_active --beam=$beam \
121+
--lattice-beam=$lattice_beam --acoustic-scale=$acwt --allow-partial=true \
122+
--word-symbol-table=$graphdir/words.txt "$model" \
123+
$graphdir/HCLr.fst $graphdir/Gr.fst $graphdir/disambig_tid.int "$feats" "$lat_wspecifier" || exit 1;
124+
fi
125+
126+
127+
if [ $stage -le 2 ]; then
128+
if ! $skip_diagnostics ; then
129+
[ ! -z $iter ] && iter_opt="--iter $iter"
130+
steps/diagnostic/analyze_lats.sh --cmd "$cmd" $iter_opt $graphdir $dir
131+
fi
132+
fi
133+
134+
135+
# The output of this script is the files "lat.*.gz"-- we'll rescore this at
136+
# different acoustic scales to get the final output.
137+
if [ $stage -le 3 ]; then
138+
if ! $skip_scoring ; then
139+
[ ! -x local/score.sh ] && \
140+
echo "Not scoring because local/score.sh does not exist or not executable." && exit 1;
141+
echo "score best paths"
142+
[ "$iter" != "final" ] && iter_opt="--iter $iter"
143+
local/score.sh $scoring_opts --cmd "$cmd" $data $graphdir $dir
144+
echo "score confidence and timing with sclite"
145+
fi
146+
fi
147+
echo "Decoding done."
148+
exit 0;

0 commit comments

Comments
 (0)