Skip to content

Commit 132eb42

Browse files
LvHangdanpovey
authored andcommitted
[src,scripts,egs] Speedups to GRU-based networks (special components) (kaldi-asr#2712)
1 parent 93a0a46 commit 132eb42

File tree

12 files changed

+4889
-1840
lines changed

12 files changed

+4889
-1840
lines changed

egs/swbd/s5c/local/chain/tuning/run_tdnn_opgru_1a.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@
44

55
# This is based on TDNN_LSTM_1b, but using the NormOPGRU to replace the LSTMP,
66
# and adding chunk-{left,right}-context-initial=0
7+
# For the details of OPGRU structure, please check the paper
8+
# "Output-Gate Projected Gated Recurrent Unit for Speech Recognition"
9+
# by Gaofeng Cheng et al,
10+
# http://www.danielpovey.com/files/2018_interspeech_opgru.pdf
11+
712
# Different from the vanilla OPGRU, Norm-OPGRU adds batchnorm in its output (forward direction)
813
# and renorm in its recurrence. Experiments show that the TDNN-NormOPGRU could achieve similar
914
# results than TDNN-LSTMP and BLSTMP in both large or small data sets (80 ~ 2300 Hrs).
1015

1116
# ./local/chain/compare_wer_general.sh --looped tdnn_lstm_1e_sp tdnn_opgru_1a_sp
1217
# System tdnn_lstm_1e_sp tdnn_opgru_1a_sp
13-
# WER on train_dev(tg) 12.81 12.39
14-
# [looped:] 12.93 12.32
15-
# WER on train_dev(fg) 11.92 11.39
16-
# [looped:] 12.07 11.35
18+
# WER on train_dev(tg) 12.81 12.31
19+
# [looped:] 12.93 12.26
20+
# WER on train_dev(fg) 11.92 11.60
21+
# [looped:] 12.07 11.65
1722
# WER on eval2000(tg) 15.6 15.1
1823
# [looped:] 16.0 15.1
19-
# WER on eval2000(fg) 14.1 13.6
24+
# WER on eval2000(fg) 14.1 13.5
2025
# [looped:] 14.5 13.5
21-
# Final train prob -0.065 -0.066
22-
# Final valid prob -0.087 -0.085
23-
# Final train prob (xent) -0.918 -0.889
24-
# Final valid prob (xent) -1.0309 -0.9837
26+
# Final train prob -0.065 -0.068
27+
# Final valid prob -0.087 -0.091
28+
# Final train prob (xent) -0.918 -0.879
29+
# Final valid prob (xent) -1.0309 -0.9667
2530

2631

2732

2833
set -e
2934

3035
# configs for 'chain'
31-
stage=12
36+
stage=0
3237
train_stage=-10
3338
get_egs_stage=-10
3439
speed_perturb=true
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
#!/bin/bash
2+
# Apache 2.0
3+
4+
# This is based on TDNN_OPGRU_1A, but using the FastNormOPGRU to replace the NormPGRU.
5+
# For the details of OPGRU structure, please check the paper
6+
# "Output-Gate Projected Gated Recurrent Unit for Speech Recognition"
7+
# by Gaofeng Cheng et al,
8+
# http://www.danielpovey.com/files/2018_interspeech_opgru.pdf
9+
10+
# Different from the vanilla OPGRU, Norm-OPGRU adds batchnorm in its output (forward direction)
11+
# and renorm in its recurrence. Experiments show that the TDNN-NormOPGRU could achieve similar
12+
# results than TDNN-LSTMP and BLSTMP in both large or small data sets (80 ~ 2300 Hrs).
13+
14+
# ./local/chain/compare_wer_general.sh --looped tdnn_opgru_1a_sp tdnn_opgru_1b_sp
15+
# System tdnn_opgru_1a_sp tdnn_opgru_1b_sp
16+
# WER on train_dev(tg) 12.31 12.41
17+
# [looped:] 12.26 12.38
18+
# WER on train_dev(fg) 11.49 11.60
19+
# [looped:] 11.43 11.65
20+
# WER on eval2000(tg) 14.9 15.1
21+
# [looped:] 15.0 15.1
22+
# WER on eval2000(fg) 13.5 13.7
23+
# [looped:] 13.5 13.7
24+
# Final train prob -0.068 -0.070
25+
# Final valid prob -0.091 -0.092
26+
# Final train prob (xent) -0.879 -0.889
27+
# Final valid prob (xent) -0.9667 -0.9723
28+
29+
30+
31+
set -e
32+
33+
# configs for 'chain'
34+
stage=0
35+
train_stage=-10
36+
get_egs_stage=-10
37+
speed_perturb=true
38+
dir=exp/chain/tdnn_opgru_1b # Note: _sp will get added to this if $speed_perturb == true.
39+
decode_iter=
40+
decode_dir_affix=
41+
42+
# training options
43+
leftmost_questions_truncate=-1
44+
chunk_width=150
45+
chunk_left_context=40
46+
chunk_right_context=0
47+
xent_regularize=0.025
48+
self_repair_scale=0.00001
49+
label_delay=5
50+
dropout_schedule='0,0@0.20,0.2@0.50,0'
51+
# decode options
52+
extra_left_context=50
53+
extra_right_context=0
54+
frames_per_chunk=
55+
test_online_decoding=
56+
57+
remove_egs=false
58+
common_egs_dir=
59+
60+
affix=
61+
# End configuration section.
62+
echo "$0 $@" # Print the command line for logging
63+
64+
. ./cmd.sh
65+
. ./path.sh
66+
. ./utils/parse_options.sh
67+
68+
if ! cuda-compiled; then
69+
cat <<EOF && exit 1
70+
This script is intended to be used with GPUs but you have not compiled Kaldi with CUDA
71+
If you want to use GPUs (and have them), go to src/, and configure and make on a machine
72+
where "nvcc" is installed.
73+
EOF
74+
fi
75+
76+
# The iVector-extraction and feature-dumping parts are the same as the standard
77+
# nnet3 setup, and you can skip them by setting "--stage 8" if you have already
78+
# run those things.
79+
80+
suffix=
81+
if [ "$speed_perturb" == "true" ]; then
82+
suffix=_sp
83+
fi
84+
85+
dir=$dir${affix:+_$affix}
86+
dir=${dir}$suffix
87+
train_set=train_nodup$suffix
88+
ali_dir=exp/tri4_ali_nodup$suffix
89+
treedir=exp/chain/tri5_7d_tree$suffix
90+
lang=data/lang_chain_2y
91+
92+
93+
# if we are using the speed-perturbed data we need to generate
94+
# alignments for it.
95+
local/nnet3/run_ivector_common.sh --stage $stage \
96+
--speed-perturb $speed_perturb \
97+
--generate-alignments $speed_perturb || exit 1;
98+
99+
100+
if [ $stage -le 9 ]; then
101+
# Get the alignments as lattices (gives the CTC training more freedom).
102+
# use the same num-jobs as the alignments
103+
nj=$(cat exp/tri4_ali_nodup$suffix/num_jobs) || exit 1;
104+
steps/align_fmllr_lats.sh --nj $nj --cmd "$train_cmd" data/$train_set \
105+
data/lang exp/tri4 exp/tri4_lats_nodup$suffix
106+
rm exp/tri4_lats_nodup$suffix/fsts.*.gz # save space
107+
fi
108+
109+
110+
if [ $stage -le 10 ]; then
111+
# Create a version of the lang/ directory that has one state per phone in the
112+
# topo file. [note, it really has two states.. the first one is only repeated
113+
# once, the second one has zero or more repeats.]
114+
rm -rf $lang
115+
cp -r data/lang $lang
116+
silphonelist=$(cat $lang/phones/silence.csl) || exit 1;
117+
nonsilphonelist=$(cat $lang/phones/nonsilence.csl) || exit 1;
118+
# Use our special topology... note that later on may have to tune this
119+
# topology.
120+
steps/nnet3/chain/gen_topo.py $nonsilphonelist $silphonelist >$lang/topo
121+
fi
122+
123+
if [ $stage -le 11 ]; then
124+
# Build a tree using our new topology.
125+
steps/nnet3/chain/build_tree.sh --frame-subsampling-factor 3 \
126+
--leftmost-questions-truncate $leftmost_questions_truncate \
127+
--context-opts "--context-width=2 --central-position=1" \
128+
--cmd "$train_cmd" 7000 data/$train_set $lang $ali_dir $treedir
129+
fi
130+
131+
if [ $stage -le 12 ]; then
132+
echo "$0: creating neural net configs using the xconfig parser";
133+
134+
num_targets=$(tree-info $treedir/tree |grep num-pdfs|awk '{print $2}')
135+
learning_rate_factor=$(echo "print 0.5/$xent_regularize" | python)
136+
gru_opts="dropout-per-frame=true dropout-proportion=0.0 gru-nonlinearity-options=\"max-change=0.75\""
137+
138+
mkdir -p $dir/configs
139+
cat <<EOF > $dir/configs/network.xconfig
140+
input dim=100 name=ivector
141+
input dim=40 name=input
142+
143+
# please note that it is important to have input layer with the name=input
144+
# as the layer immediately preceding the fixed-affine-layer to enable
145+
# the use of short notation for the descriptor
146+
fixed-affine-layer name=lda input=Append(-2,-1,0,1,2,ReplaceIndex(ivector, t, 0)) affine-transform-file=$dir/configs/lda.mat
147+
148+
# the first splicing is moved before the lda layer, so no splicing here
149+
relu-batchnorm-layer name=tdnn1 dim=1024
150+
relu-batchnorm-layer name=tdnn2 input=Append(-1,0,1) dim=1024
151+
relu-batchnorm-layer name=tdnn3 input=Append(-1,0,1) dim=1024
152+
153+
# check steps/libs/nnet3/xconfig/gru.py for the other options and defaults
154+
fast-norm-opgru-layer name=opgru1 cell-dim=1024 recurrent-projection-dim=256 non-recurrent-projection-dim=256 delay=-3 $gru_opts
155+
relu-batchnorm-layer name=tdnn4 input=Append(-3,0,3) dim=1024
156+
relu-batchnorm-layer name=tdnn5 input=Append(-3,0,3) dim=1024
157+
fast-norm-opgru-layer name=opgru2 cell-dim=1024 recurrent-projection-dim=256 non-recurrent-projection-dim=256 delay=-3 $gru_opts
158+
relu-batchnorm-layer name=tdnn6 input=Append(-3,0,3) dim=1024
159+
relu-batchnorm-layer name=tdnn7 input=Append(-3,0,3) dim=1024
160+
fast-norm-opgru-layer name=opgru3 cell-dim=1024 recurrent-projection-dim=256 non-recurrent-projection-dim=256 delay=-3 $gru_opts
161+
162+
## adding the layers for chain branch
163+
output-layer name=output input=opgru3 output-delay=$label_delay include-log-softmax=false dim=$num_targets max-change=1.5
164+
165+
# adding the layers for xent branch
166+
# This block prints the configs for a separate output that will be
167+
# trained with a cross-entropy objective in the 'chain' models... this
168+
# has the effect of regularizing the hidden parts of the model. we use
169+
# 0.5 / args.xent_regularize as the learning rate factor- the factor of
170+
# 0.5 / args.xent_regularize is suitable as it means the xent
171+
# final-layer learns at a rate independent of the regularization
172+
# constant; and the 0.5 was tuned so as to make the relative progress
173+
# similar in the xent and regular final layers.
174+
output-layer name=output-xent input=opgru3 output-delay=$label_delay dim=$num_targets learning-rate-factor=$learning_rate_factor max-change=1.5
175+
176+
EOF
177+
steps/nnet3/xconfig_to_configs.py --xconfig-file $dir/configs/network.xconfig --config-dir $dir/configs/
178+
fi
179+
180+
if [ $stage -le 13 ]; then
181+
if [[ $(hostname -f) == *.clsp.jhu.edu ]] && [ ! -d $dir/egs/storage ]; then
182+
utils/create_split_dir.pl \
183+
/export/b0{5,6,7,8}/$USER/kaldi-data/egs/swbd-$(date +'%m_%d_%H_%M')/s5c/$dir/egs/storage $dir/egs/storage
184+
fi
185+
186+
steps/nnet3/chain/train.py --stage $train_stage \
187+
--cmd "$decode_cmd" \
188+
--feat.online-ivector-dir exp/nnet3/ivectors_${train_set} \
189+
--feat.cmvn-opts "--norm-means=false --norm-vars=false" \
190+
--chain.xent-regularize $xent_regularize \
191+
--chain.leaky-hmm-coefficient 0.1 \
192+
--chain.l2-regularize 0.00005 \
193+
--chain.apply-deriv-weights false \
194+
--chain.lm-opts="--num-extra-lm-states=2000" \
195+
--trainer.num-chunk-per-minibatch 64 \
196+
--trainer.frames-per-iter 1200000 \
197+
--trainer.max-param-change 2.0 \
198+
--trainer.num-epochs 4 \
199+
--trainer.optimization.shrink-value 0.99 \
200+
--trainer.optimization.num-jobs-initial 3 \
201+
--trainer.optimization.num-jobs-final 16 \
202+
--trainer.optimization.initial-effective-lrate 0.001 \
203+
--trainer.optimization.final-effective-lrate 0.0001 \
204+
--trainer.optimization.momentum 0.0 \
205+
--trainer.deriv-truncate-margin 8 \
206+
--egs.stage $get_egs_stage \
207+
--egs.opts "--frames-overlap-per-eg 0" \
208+
--egs.chunk-width $chunk_width \
209+
--egs.chunk-left-context $chunk_left_context \
210+
--egs.chunk-right-context $chunk_right_context \
211+
--trainer.dropout-schedule $dropout_schedule \
212+
--egs.chunk-left-context-initial 0 \
213+
--egs.chunk-right-context-final 0 \
214+
--egs.dir "$common_egs_dir" \
215+
--cleanup.remove-egs $remove_egs \
216+
--feat-dir data/${train_set}_hires \
217+
--tree-dir $treedir \
218+
--lat-dir exp/tri4_lats_nodup$suffix \
219+
--dir $dir || exit 1;
220+
fi
221+
222+
if [ $stage -le 14 ]; then
223+
# Note: it might appear that this $lang directory is mismatched, and it is as
224+
# far as the 'topo' is concerned, but this script doesn't read the 'topo' from
225+
# the lang directory.
226+
utils/mkgraph.sh --self-loop-scale 1.0 data/lang_sw1_tg $dir $dir/graph_sw1_tg
227+
fi
228+
229+
decode_suff=sw1_tg
230+
graph_dir=$dir/graph_sw1_tg
231+
if [ $stage -le 15 ]; then
232+
[ -z $extra_left_context ] && extra_left_context=$chunk_left_context;
233+
[ -z $extra_right_context ] && extra_right_context=$chunk_right_context;
234+
[ -z $frames_per_chunk ] && frames_per_chunk=$chunk_width;
235+
iter_opts=
236+
if [ ! -z $decode_iter ]; then
237+
iter_opts=" --iter $decode_iter "
238+
fi
239+
for decode_set in train_dev eval2000; do
240+
(
241+
steps/nnet3/decode.sh --acwt 1.0 --post-decode-acwt 10.0 \
242+
--nj 50 --cmd "$decode_cmd" $iter_opts \
243+
--extra-left-context $extra_left_context \
244+
--extra-right-context $extra_right_context \
245+
--extra-left-context-initial 0 \
246+
--extra-right-context-final 0 \
247+
--frames-per-chunk "$frames_per_chunk" \
248+
--online-ivector-dir exp/nnet3/ivectors_${decode_set} \
249+
$graph_dir data/${decode_set}_hires \
250+
$dir/decode_${decode_set}${decode_dir_affix:+_$decode_dir_affix}_${decode_suff} || exit 1;
251+
if $has_fisher; then
252+
steps/lmrescore_const_arpa.sh --cmd "$decode_cmd" \
253+
data/lang_sw1_{tg,fsh_fg} data/${decode_set}_hires \
254+
$dir/decode_${decode_set}${decode_dir_affix:+_$decode_dir_affix}_sw1_{tg,fsh_fg} || exit 1;
255+
fi
256+
) &
257+
done
258+
fi
259+
260+
if $test_online_decoding && [ $stage -le 16 ]; then
261+
# note: if the features change (e.g. you add pitch features), you will have to
262+
# change the options of the following command line.
263+
steps/online/nnet3/prepare_online_decoding.sh \
264+
--mfcc-config conf/mfcc_hires.conf \
265+
$lang exp/nnet3/extractor $dir ${dir}_online
266+
267+
rm $dir/.error 2>/dev/null || true
268+
for decode_set in train_dev eval2000; do
269+
(
270+
# note: we just give it "$decode_set" as it only uses the wav.scp, the
271+
# feature type does not matter.
272+
steps/online/nnet3/decode.sh --nj 50 --cmd "$decode_cmd" $iter_opts \
273+
--acwt 1.0 --post-decode-acwt 10.0 \
274+
$graph_dir data/${decode_set}_hires \
275+
${dir}_online/decode_${decode_set}${decode_iter:+_$decode_iter}_sw1_tg || exit 1;
276+
if $has_fisher; then
277+
steps/lmrescore_const_arpa.sh --cmd "$decode_cmd" \
278+
data/lang_sw1_{tg,fsh_fg} data/${decode_set}_hires \
279+
${dir}_online/decode_${decode_set}${decode_iter:+_$decode_iter}_sw1_{tg,fsh_fg} || exit 1;
280+
fi
281+
) || touch $dir/.error &
282+
done
283+
wait
284+
if [ -f $dir/.error ]; then
285+
echo "$0: something went wrong in online decoding"
286+
exit 1
287+
fi
288+
fi
289+
290+
if [ $stage -le 17 ]; then
291+
rm $dir/.error 2>/dev/null || true
292+
for decode_set in train_dev eval2000; do
293+
(
294+
steps/nnet3/decode_looped.sh \
295+
--acwt 1.0 --post-decode-acwt 10.0 \
296+
--nj 50 --cmd "$decode_cmd" $iter_opts \
297+
--online-ivector-dir exp/nnet3/ivectors_${decode_set} \
298+
$graph_dir data/${decode_set}_hires \
299+
$dir/decode_${decode_set}${decode_iter:+_$decode_iter}_sw1_tg_looped || exit 1;
300+
if $has_fisher; then
301+
steps/lmrescore_const_arpa.sh --cmd "$decode_cmd" \
302+
data/lang_sw1_{tg,fsh_fg} data/${decode_set}_hires \
303+
$dir/decode_${decode_set}${decode_iter:+_$decode_iter}_sw1_{tg,fsh_fg}_looped || exit 1;
304+
fi
305+
) &
306+
done
307+
wait
308+
if [ -f $dir/.error ]; then
309+
echo "$0: something went wrong in looped decoding"
310+
exit 1
311+
fi
312+
fi
313+
314+
wait;
315+
exit 0;

0 commit comments

Comments
 (0)