diff options
Diffstat (limited to 'kaldi_decode')
-rw-r--r-- | kaldi_decode/Makefile | 43 | ||||
-rwxr-xr-x | kaldi_decode/README | 13 | ||||
-rwxr-xr-x | kaldi_decode/README.timit | 15 | ||||
-rwxr-xr-x | kaldi_decode/cmd.sh | 36 | ||||
-rw-r--r-- | kaldi_decode/conf/decode_dnn.config | 2 | ||||
-rwxr-xr-x | kaldi_decode/decode_with_nerv.sh (renamed from kaldi_decode/decode.sh) | 26 | ||||
-rw-r--r-- | kaldi_decode/kaldi_decode-scm-1.rockspec | 36 | ||||
-rwxr-xr-x | kaldi_decode/local/score.sh | 67 | ||||
-rw-r--r-- | kaldi_decode/nnet-forward-with-nerv.sh | 2 | ||||
-rwxr-xr-x | kaldi_decode/path.sh | 5 | ||||
-rw-r--r-- | kaldi_decode/src/Makefile | 12 | ||||
-rw-r--r-- | kaldi_decode/src/asr_propagator.lua | 84 | ||||
-rw-r--r-- | kaldi_decode/src/nerv4decode.lua | 79 | ||||
-rw-r--r-- | kaldi_decode/src/nnet-forward.cc | 18 | ||||
-rwxr-xr-x | kaldi_decode/utils/int2sym.pl | 71 | ||||
-rwxr-xr-x | kaldi_decode/utils/parse_options.sh | 97 | ||||
-rwxr-xr-x | kaldi_decode/utils/queue.pl | 580 | ||||
-rwxr-xr-x | kaldi_decode/utils/run.pl | 264 | ||||
-rwxr-xr-x | kaldi_decode/utils/split_data.sh | 135 |
19 files changed, 207 insertions, 1378 deletions
diff --git a/kaldi_decode/Makefile b/kaldi_decode/Makefile new file mode 100644 index 0000000..e3a7c2d --- /dev/null +++ b/kaldi_decode/Makefile @@ -0,0 +1,43 @@ +ifndef LUA_BINDIR +$(error Please build the package via luarocks: `luarocks make`) +endif + +ifndef KALDI_BASE +$(error KALDI_BASE is not set) +endif + +ifndef CUDA_BASE +$(error CUDA_BASE is not set) +endif + +KDIR := $(KALDI_BASE) +BUILD_DIR := $(CURDIR)/build +INC_PATH := $(LUA_BINDIR)/../include/ +OBJS := src/nnet-forward.o nnet-forward + +SUBDIR := src +OBJ_DIR := $(BUILD_DIR)/objs +LUA_DIR = $(INST_LUADIR)/kaldi_decode +KALDIINCLUDE := -I $(KDIR)/tools/ATLAS/include/ -I $(KDIR)/tools/openfst/include/ -I $(KDIR)/src/ + +OBJS := $(addprefix $(OBJ_DIR)/,$(OBJS)) +OBJ_SUBDIR := $(addprefix $(OBJ_DIR)/,$(SUBDIR)) + +KL := $(KDIR)/src/feat/kaldi-feat.a $(KDIR)/src/cudamatrix/kaldi-cudamatrix.a $(KDIR)/src/matrix/kaldi-matrix.a $(KDIR)/src/base/kaldi-base.a $(KDIR)/src/util/kaldi-util.a $(KDIR)/src/hmm/kaldi-hmm.a $(KDIR)/src/tree/kaldi-tree.a $(KDIR)/src/nnet/kaldi-nnet.a $(BLAS_LDFLAGS) + +build: $(OBJ_DIR) $(LUA_DIR) $(OBJ_SUBDIR) $(OBJS) +$(OBJ_DIR)/%.o: %.cc + g++ -c -o $@ $< -Wall $(KALDIINCLUDE) -DHAVE_ATLAS -DKALDI_DOUBLEPRECISION=0 -DHAVE_POSIX_MEMALIGN -DLUA_USE_APICHECK -I $(LUA_INCDIR) -I $(INC_PATH) $(CFLAGS) +$(OBJ_DIR)/nnet-forward: $(OBJ_DIR)/src/nnet-forward.o + g++ -o $@ $< $(KL) -L$(LUA_LIBDIR) -Wl,-rpath=$(LUA_LIBDIR) -lluajit-5.1 -L$(CUDA_BASE)/lib64/ -Wl,-rpath=$(CUDA_BASE)/lib64/ -lcudart -lcublas -ldl +$(OBJ_DIR) $(LUA_DIR) $(OBJ_SUBDIR): + -mkdir -p $@ +install: $(LUA_DIR) + cp $(OBJ_DIR)/nnet-forward $(LUA_BINDIR)/nnet-forward-with-nerv + cp src/asr_propagator.lua $(LUA_DIR)/ + sed 's*nnet_forward=*nnet_forward=$(LUA_BINDIR)/nnet-forward-with-nerv.sh*g;s*asr_propagator=*asr_propagator=$(LUA_BINDIR)/../share/lua/5.1/kaldi_decode/asr_propagator.lua*g' decode_with_nerv.sh > $(LUA_BINDIR)/decode_with_nerv.sh + echo '$(LUA_BINDIR)/nnet-forward-with-nerv "$$@"' | cat nnet-forward-with-nerv.sh - | sed 's*\.\./\.\./install/bin/luarocks*$(LUA_BINDIR)/luarocks*g' > $(LUA_BINDIR)/nnet-forward-with-nerv.sh + chmod +x $(LUA_BINDIR)/nnet-forward-with-nerv.sh + chmod +x $(LUA_BINDIR)/decode_with_nerv.sh +clean: + -rm -r $(OBJ_DIR) diff --git a/kaldi_decode/README b/kaldi_decode/README deleted file mode 100755 index 8d0a95b..0000000 --- a/kaldi_decode/README +++ /dev/null @@ -1,13 +0,0 @@ -source path.sh -source cmd.sh - -acwt=0.1 -dir=/slfs5/users/ymz09/chime/baseline/ASR/exp/nerv_seq/ -graph=/slfs5/users/ymz09/chime/baseline/ASR/exp/tri4a_dnn_tr05_multi_enhanced/graph_tgpr_5k -data=/slfs5/users/ymz09/chime/baseline/ASR/data-fbank/et05_real_enhanced -config=/slfs6/users/ymz09/nerv-project/nerv/nerv/examples/mpe_chime3.lua - -decode.sh --nj 4 --cmd "$decode_cmd" --config conf/decode_dnn.config --acwt $acwt \ - $graph $data $config \ - $dir/decode_tgpr_5k_et05_real_enhanced_nerv - diff --git a/kaldi_decode/README.timit b/kaldi_decode/README.timit new file mode 100755 index 0000000..7fac918 --- /dev/null +++ b/kaldi_decode/README.timit @@ -0,0 +1,15 @@ +#!/bin/bash +source path.sh +source cmd.sh + +gmmdir=/speechlab/users/mfy43/timit/s5/exp/tri3/ +data_fmllr=/speechlab/users/mfy43/timit/s5/data-fmllr-tri3/ +dir=/speechlab/users/mfy43/timit/s5/exp/dnn4_nerv_prepare/ +nerv_config=/speechlab/users/mfy43/nerv/nerv/examples/timit_baseline2.lua +decode=/speechlab/users/mfy43/nerv/install/bin/decode_with_nerv.sh + +# Decode (reuse HCLG graph) +$decode --nj 20 --cmd "$decode_cmd" --acwt 0.2 \ + $gmmdir/graph $data_fmllr/test $nerv_config $dir/decode_test || exit 1; +$decode --nj 20 --cmd "$decode_cmd" --acwt 0.2 \ + $gmmdir/graph $data_fmllr/dev $nerv_config $dir/decode_dev || exit 1; diff --git a/kaldi_decode/cmd.sh b/kaldi_decode/cmd.sh deleted file mode 100755 index e2e54e8..0000000 --- a/kaldi_decode/cmd.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -# "queue.pl" uses qsub. The options to it are -# options to qsub. If you have GridEngine installed, -# change this to a queue you have access to. -# Otherwise, use "run.pl", which will run jobs locally -# (make sure your --num-jobs options are no more than -# the number of cpus on your machine. - -#a) JHU cluster options -#export train_cmd="queue.pl -l arch=*64" -#export decode_cmd="queue.pl -l arch=*64,mem_free=2G,ram_free=2G" -#export mkgraph_cmd="queue.pl -l arch=*64,ram_free=4G,mem_free=4G" - -#export cuda_cmd="..." - - -#b) BUT cluster options -#export train_cmd="queue.pl -q all.q@@blade -l ram_free=1200M,mem_free=1200M" -#export decode_cmd="queue.pl -q all.q@@blade -l ram_free=1700M,mem_free=1700M" -#export decodebig_cmd="queue.pl -q all.q@@blade -l ram_free=4G,mem_free=4G" - -#export cuda_cmd="queue.pl -q long.q@@pco203 -l gpu=1" -#export cuda_cmd="queue.pl -q long.q@pcspeech-gpu" -#export mkgraph_cmd="queue.pl -q all.q@@servers -l ram_free=4G,mem_free=4G" - -#c) run it locally... -export train_cmd=run.pl -export decode_cmd=run.pl -export cuda_cmd=run.pl -export mkgraph_cmd=run.pl - -#export train_cmd='queue.pl' -#export decode_cmd='queue.pl' -#export cuda_cmd='queue.pl -l gpu=1 -l hostname="markov|date|hamming"' -#export mkgraph_cmd='queue.pl"' - diff --git a/kaldi_decode/conf/decode_dnn.config b/kaldi_decode/conf/decode_dnn.config deleted file mode 100644 index 89dd992..0000000 --- a/kaldi_decode/conf/decode_dnn.config +++ /dev/null @@ -1,2 +0,0 @@ -beam=18.0 # beam for decoding. Was 13.0 in the scripts. -lattice_beam=10.0 # this has most effect on size of the lattices. diff --git a/kaldi_decode/decode.sh b/kaldi_decode/decode_with_nerv.sh index aa7e089..5554b2e 100755 --- a/kaldi_decode/decode.sh +++ b/kaldi_decode/decode_with_nerv.sh @@ -1,5 +1,4 @@ #!/bin/bash - # Copyright 2012-2013 Karel Vesely, Daniel Povey # Apache 2.0 @@ -20,7 +19,7 @@ lattice_beam=8.0 min_active=200 max_active=7000 # limit of active tokens max_mem=50000000 # approx. limit to memory consumption during minimization in bytes -nnet_forward_opts="--prior-scale=1.0" +nnet_forward_opts="--apply-log=true" # IMPORTANT, to apply log before to substract log-prior, and to know the modified 'nnet-forward' removed '--no-softmax' option skip_scoring=false scoring_opts="--min-lmwt 4 --max-lmwt 15" @@ -28,6 +27,13 @@ scoring_opts="--min-lmwt 4 --max-lmwt 15" num_threads=1 # if >1, will use latgen-faster-parallel parallel_opts= # Ignored now. use_gpu="no" # yes|no|optionaly + +cmvn_opts= +splice_opts= +delta_opts= + +asr_propagator= +nnet_forward= # End configuration section. echo "$0 $@" # Print the command line for logging @@ -36,7 +42,7 @@ echo "$0 $@" # Print the command line for logging . parse_options.sh || exit 1; if [ $# != 4 ]; then - echo "Usage: $0 [options] <graph-dir> <data-dir> <nerv-config> <decode-dir>" + echo "Usage: $0 [options] <graph-dir> <data-dir> <nerv-model-config> <decode-dir>" echo "... where <decode-dir> is assumed to be a sub-directory of the directory" echo " where the DNN and transition model is." echo "e.g.: $0 exp/dnn1/graph_tgpr data/test config.lua exp/dnn1/decode_tgpr" @@ -62,8 +68,9 @@ fi graphdir=$1 data=$2 -config=$3 +model_conf=$3 dir=$4 + [ -z $srcdir ] && srcdir=`dirname $dir`; # Default model directory one level up from decoding directory. sdata=$data/split$nj; @@ -90,11 +97,10 @@ thread_string= # PREPARE FEATURE EXTRACTION PIPELINE # import config, -cmvn_opts= -delta_opts= D=$srcdir [ -e $D/norm_vars ] && cmvn_opts="--norm-means=true --norm-vars=$(cat $D/norm_vars)" # Bwd-compatibility, [ -e $D/cmvn_opts ] && cmvn_opts=$(cat $D/cmvn_opts) +[ -e $D/splice_opts ] && splice_opts=$(cat $D/splice_opts) [ -e $D/delta_order ] && delta_opts="--delta-order=$(cat $D/delta_order)" # Bwd-compatibility, [ -e $D/delta_opts ] && delta_opts=$(cat $D/delta_opts) # @@ -103,13 +109,17 @@ feats="ark,s,cs:copy-feats scp:$sdata/JOB/feats.scp ark:- |" # apply-cmvn (optional), [ ! -z "$cmvn_opts" -a ! -f $sdata/1/cmvn.scp ] && echo "$0: Missing $sdata/1/cmvn.scp" && exit 1 [ ! -z "$cmvn_opts" ] && feats="$feats apply-cmvn $cmvn_opts --utt2spk=ark:$sdata/JOB/utt2spk scp:$sdata/JOB/cmvn.scp ark:- ark:- |" +# splice-opts (optional), +[ ! -z "$splice_opts" ] && feats="$feats splice-feats $splice_opts ark:- ark:- |" # add-deltas (optional), [ ! -z "$delta_opts" ] && feats="$feats add-deltas $delta_opts ark:- ark:- |" # # Run the decoding in the queue, if [ $stage -le 0 ]; then - $cmd --num-threads $((num_threads+1)) JOB=1:$nj $dir/log/decode.JOB.log \ - ./src/nnet-forward $nnet_forward_opts --class-frame-counts=$class_frame_counts --use-gpu=$use_gpu $config "$feats" ark:- \| \ +# $cmd --num-threads $((num_threads+1)) JOB=1:$nj $dir/log/decode.JOB.log \ +# remove multi-threads to avoid smp requirement + $cmd --num-threads $((num_threads)) JOB=1:$nj $dir/log/decode.JOB.log \ + $nnet_forward $nnet_forward_opts --class-frame-counts=$class_frame_counts --use-gpu=$use_gpu $model_conf "$feats" ark:- $asr_propagator \| \ latgen-faster-mapped$thread_string --min-active=$min_active --max-active=$max_active --max-mem=$max_mem --beam=$beam \ --lattice-beam=$lattice_beam --acoustic-scale=$acwt --allow-partial=true --word-symbol-table=$graphdir/words.txt \ $model $graphdir/HCLG.fst ark:- "ark:|gzip -c > $dir/lat.JOB.gz" || exit 1; diff --git a/kaldi_decode/kaldi_decode-scm-1.rockspec b/kaldi_decode/kaldi_decode-scm-1.rockspec new file mode 100644 index 0000000..cc533ae --- /dev/null +++ b/kaldi_decode/kaldi_decode-scm-1.rockspec @@ -0,0 +1,36 @@ +package = "kaldi_decode" +version = "scm-1" +source = { + url = "https://github.com/Nerv-SJTU/nerv-speech.git" +} +description = { + summary = "Kaldi decode support for NERV", + detailed = [[ + ]], + homepage = "https://github.com/Determinant/nerv-speech", + license = "BSD" +} +dependencies = { + "nerv >= scm-1", + "lua >= 5.1" +} +build = { + type = "make", + build_variables = { + CFLAGS="$(CFLAGS) -Wall -Wextra -g -O2", + --CFLAGS="$(CFLAGS) -Wall -Wextra -g", + LIBFLAG="$(LIBFLAG)", + LUA_LIBDIR="$(LUA_LIBDIR)", + LUA_BINDIR="$(LUA_BINDIR)", + LUA_INCDIR="$(LUA_INCDIR)", + LUA="$(LUA)", + }, + install_variables = { + LUA_BINDIR="$(LUA_BINDIR)", + INST_PREFIX="$(PREFIX)", + INST_BINDIR="$(BINDIR)", + INST_LIBDIR="$(LIBDIR)", + INST_LUADIR="$(LUADIR)", + INST_CONFDIR="$(CONFDIR)", + }, +} diff --git a/kaldi_decode/local/score.sh b/kaldi_decode/local/score.sh deleted file mode 100755 index b18f350..0000000 --- a/kaldi_decode/local/score.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash -# Copyright 2012 Johns Hopkins University (Author: Daniel Povey) -# Apache 2.0 - -[ -f ./path.sh ] && . ./path.sh - -# begin configuration section. -cmd=run.pl -stage=0 -decode_mbr=true -reverse=false -word_ins_penalty=0.0 -min_lmwt=5 -max_lmwt=20 -#end configuration section. - -[ -f ./path.sh ] && . ./path.sh -. parse_options.sh || exit 1; - -if [ $# -ne 3 ]; then - echo "Usage: local/score.sh [--cmd (run.pl|queue.pl...)] <data-dir> <lang-dir|graph-dir> <decode-dir>" - echo " Options:" - echo " --cmd (run.pl|queue.pl...) # specify how to run the sub-processes." - echo " --stage (0|1|2) # start scoring script from part-way through." - echo " --decode_mbr (true/false) # maximum bayes risk decoding (confusion network)." - echo " --min_lmwt <int> # minumum LM-weight for lattice rescoring " - echo " --max_lmwt <int> # maximum LM-weight for lattice rescoring " - echo " --reverse (true/false) # score with time reversed features " - exit 1; -fi - -data=$1 -lang_or_graph=$2 -dir=$3 - -symtab=$lang_or_graph/words.txt - -for f in $symtab $dir/lat.1.gz $data/text; do - [ ! -f $f ] && echo "score.sh: no such file $f" && exit 1; -done - -mkdir -p $dir/scoring/log - -cat $data/text | sed 's:<NOISE>::g' | sed 's:<SPOKEN_NOISE>::g' > $dir/scoring/test_filt.txt - -$cmd LMWT=$min_lmwt:$max_lmwt $dir/scoring/log/best_path.LMWT.log \ - lattice-scale --inv-acoustic-scale=LMWT "ark:gunzip -c $dir/lat.*.gz|" ark:- \| \ - lattice-add-penalty --word-ins-penalty=$word_ins_penalty ark:- ark:- \| \ - lattice-best-path --word-symbol-table=$symtab \ - ark:- ark,t:$dir/scoring/LMWT.tra || exit 1; - -if $reverse; then - for lmwt in `seq $min_lmwt $max_lmwt`; do - mv $dir/scoring/$lmwt.tra $dir/scoring/$lmwt.tra.orig - awk '{ printf("%s ",$1); for(i=NF; i>1; i--){ printf("%s ",$i); } printf("\n"); }' \ - <$dir/scoring/$lmwt.tra.orig >$dir/scoring/$lmwt.tra - done -fi - -# Note: the double level of quoting for the sed command -$cmd LMWT=$min_lmwt:$max_lmwt $dir/scoring/log/score.LMWT.log \ - cat $dir/scoring/LMWT.tra \| \ - utils/int2sym.pl -f 2- $symtab \| sed 's:\<UNK\>::g' \| \ - compute-wer --text --mode=present \ - ark:$dir/scoring/test_filt.txt ark,p:- ">&" $dir/wer_LMWT || exit 1; - -exit 0; diff --git a/kaldi_decode/nnet-forward-with-nerv.sh b/kaldi_decode/nnet-forward-with-nerv.sh new file mode 100644 index 0000000..71bf239 --- /dev/null +++ b/kaldi_decode/nnet-forward-with-nerv.sh @@ -0,0 +1,2 @@ +#!/bin/bash +source <(../../install/bin/luarocks path) diff --git a/kaldi_decode/path.sh b/kaldi_decode/path.sh deleted file mode 100755 index 5aebc72..0000000 --- a/kaldi_decode/path.sh +++ /dev/null @@ -1,5 +0,0 @@ -export KALDI_ROOT=/slfs6/users/ymz09/kaldi/ -export PATH=$PWD/utils/:$KALDI_ROOT/src/bin:$KALDI_ROOT/tools/openfst/bin:$KALDI_ROOT/src/fstbin/:$KALDI_ROOT/src/gmmbin/:$KALDI_ROOT/src/featbin/:$KALDI_ROOT/src/lm/:$KALDI_ROOT/src/sgmmbin/:$KALDI_ROOT/src/sgmm2bin/:$KALDI_ROOT/src/fgmmbin/:$KALDI_ROOT/src/latbin/:$KALDI_ROOT/src/nnetbin:$KALDI_ROOT/src/nnet2bin/:$KALDI_ROOT/src/kwsbin:$PWD:$PATH -export LC_ALL=C -export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/slhome/ymz09/mylibs/:. - diff --git a/kaldi_decode/src/Makefile b/kaldi_decode/src/Makefile deleted file mode 100644 index 7cffbc2..0000000 --- a/kaldi_decode/src/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# Change KDIR to `kaldi-trunk' path (Kaldi must be compiled with --share) -KDIR := /slfs6/users/ymz09/kaldi/ -NERVDIR := /slfs6/users/ymz09/nerv-project/nerv/ -CUDADIR := /usr/local/cuda/ - -nnet-forward: - g++ -msse -msse2 -Wall -I $(KDIR)/src/ -pthread -DKALDI_DOUBLEPRECISION=0 -DHAVE_POSIX_MEMALIGN -Wno-sign-compare -Wno-unused-local-typedefs -Winit-self -DHAVE_EXECINFO_H=1 -rdynamic -DHAVE_CXXABI_H -DHAVE_ATLAS -I $(KDIR)/tools/ATLAS/include -I $(KDIR)/tools/openfst/include -Wno-sign-compare -g -fPIC -DHAVE_CUDA -I $(CUDADIR)/include -DKALDI_NO_EXPF -I $(NERVDIR)/install//include/luajit-2.0/ -I $(NERVDIR)/install/include/ -DLUA_USE_APICHECK -c -o nnet-forward.o nnet-forward.cc - g++ -rdynamic -Wl,-rpath=$(KDIR)/tools/openfst/lib -L$(CUDADIR)/lib64 -Wl,-rpath=$(CUDADIR)/lib64 -Wl,-rpath=$(KDIR)/src/lib -L. -L$(KDIR)/src/nnet/ -L$(KDIR)/src/cudamatrix/ -L$(KDIR)/src/lat/ -L$(KDIR)/src/hmm/ -L$(KDIR)/src/tree/ -L$(KDIR)/src/matrix/ -L$(KDIR)/src/util/ -L$(KDIR)/src/base/ nnet-forward.o $(KDIR)/src/nnet//libkaldi-nnet.so $(KDIR)/src/cudamatrix//libkaldi-cudamatrix.so $(KDIR)/src/lat//libkaldi-lat.so $(KDIR)/src/hmm//libkaldi-hmm.so $(KDIR)/src/tree//libkaldi-tree.so $(KDIR)/src/matrix//libkaldi-matrix.so $(KDIR)/src/util//libkaldi-util.so $(KDIR)/src/base//libkaldi-base.so -L$(KDIR)/tools/openfst/lib -lfst /usr/lib/liblapack.so /usr/lib/libcblas.so /usr/lib/libatlas.so /usr/lib/libf77blas.so -lm -lpthread -ldl -lcublas -lcudart -lkaldi-nnet -lkaldi-cudamatrix -lkaldi-lat -lkaldi-hmm -lkaldi-tree -lkaldi-matrix -lkaldi-util -lkaldi-base -lstdc++ -L$(NERVDIR)/install/lib -Wl,-rpath=$(NERVDIR)/install/lib -lnervcore -lluaT -rdynamic -Wl,-rpath=$(KDIR)//tools/openfst/lib -L$(CUDADIR)/lib64 -Wl,-rpath=$(CUDADIR)/lib64 -Wl,-rpath=$(KDIR)//src/lib -lfst -lm -lpthread -ldl -lcublas -lcudart -L $(NERVDIR)/luajit-2.0/src/ -lluajit -o nnet-forward - -clean: - -rm nnet-forward.o nnet-forward - diff --git a/kaldi_decode/src/asr_propagator.lua b/kaldi_decode/src/asr_propagator.lua new file mode 100644 index 0000000..5d0ad7c --- /dev/null +++ b/kaldi_decode/src/asr_propagator.lua @@ -0,0 +1,84 @@ +print = function(...) io.write(table.concat({...}, "\t")) end +io.output('/dev/null') +-- path and cpath are correctly set by `path.sh` +local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("nerv","scm-1") +require 'nerv' +nerv.printf("*** NERV: A Lua-based toolkit for high-performance deep learning (alpha) ***\n") +nerv.info("automatically initialize a default MContext...") +nerv.MMatrix._default_context = nerv.MContext() +nerv.info("the default MContext is ok") +-- only for backward compatibilty, will be removed in the future +local function _add_profile_method(cls) + local c = cls._default_context + cls.print_profile = function () c:print_profile() end + cls.clear_profile = function () c:clear_profile() end +end +_add_profile_method(nerv.MMatrix) + +function build_propagator(ifname, feature) + local param_repo = nerv.ParamRepo() + param_repo:import(ifname, nil, gconf) + local layer_repo = make_layer_repo(param_repo) + local network = get_decode_network(layer_repo) + local global_transf = get_global_transf(layer_repo) + local input_order = get_decode_input_order() + local readers = make_decode_readers(feature, layer_repo) + + local batch_propagator = function() + local data = nil + for ri = 1, #readers do + data = readers[ri].reader:get_data() + if data ~= nil then + break + end + end + + if data == nil then + return "", nil + end + + gconf.batch_size = data[input_order[1].id]:nrow() + network:init(gconf.batch_size) + + local input = {} + for i, e in ipairs(input_order) do + local id = e.id + if data[id] == nil then + nerv.error("input data %s not found", id) + end + local transformed + if e.global_transf then + transformed = nerv.speech_utils.global_transf(data[id], + global_transf, + gconf.frm_ext or 0, 0, + gconf) + else + transformed = data[id] + end + table.insert(input, transformed) + end + local output = {nerv.MMatrixFloat(input[1]:nrow(), network.dim_out[1])} + network:propagate(input, output) + + local utt = data["key"] + if utt == nil then + nerv.error("no key found.") + end + + collectgarbage("collect") + return utt, output[1] + end + + return batch_propagator +end + +function init(config, feature) + dofile(config) + gconf.use_cpu = true -- use CPU to decode + trainer = build_propagator(gconf.decode_param, feature) +end + +function feed() + local utt, mat = trainer() + return utt, mat +end diff --git a/kaldi_decode/src/nerv4decode.lua b/kaldi_decode/src/nerv4decode.lua deleted file mode 100644 index b2ff344..0000000 --- a/kaldi_decode/src/nerv4decode.lua +++ /dev/null @@ -1,79 +0,0 @@ -package.path="/home/slhome/ymz09/.luarocks/share/lua/5.1/?.lua;/home/slhome/ymz09/.luarocks/share/lua/5.1/?/init.lua;/slfs6/users/ymz09/nerv-project/nerv/install/share/lua/5.1/?.lua;/slfs6/users/ymz09/nerv-project/nerv/install/share/lua/5.1/?/init.lua;"..package.path; -package.cpath="/home/slhome/ymz09/.luarocks/lib/lua/5.1/?.so;/slfs6/users/ymz09/nerv-project/nerv/install/lib/lua/5.1/?.so;"..package.cpath; -local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("nerv","scm-1") -require 'nerv' - -function build_trainer(ifname, feature) - local param_repo = nerv.ParamRepo() - param_repo:import(ifname, nil, gconf) - local layer_repo = make_layer_repo(param_repo) - local network = get_decode_network(layer_repo) - local global_transf = get_global_transf(layer_repo) - local input_order = get_input_order() - local readers = make_readers(feature, layer_repo) - network:init(1) - - local iterative_trainer = function() - local data = nil - for ri = 1, #readers, 1 do - data = readers[ri].reader:get_data() - if data ~= nil then - break - end - end - - if data == nil then - return "", nil - end - - local input = {} - for i, e in ipairs(input_order) do - local id = e.id - if data[id] == nil then - nerv.error("input data %s not found", id) - end - local transformed - if e.global_transf then - local batch = gconf.cumat_type(data[id]:nrow(), data[id]:ncol()) - batch:copy_fromh(data[id]) - transformed = nerv.speech_utils.global_transf(batch, - global_transf, - gconf.frm_ext or 0, 0, - gconf) - else - transformed = data[id] - end - table.insert(input, transformed) - end - local output = {nerv.CuMatrixFloat(input[1]:nrow(), network.dim_out[1])} - network:batch_resize(input[1]:nrow()) - network:propagate(input, output) - - local utt = data["key"] - if utt == nil then - nerv.error("no key found.") - end - - local mat = nerv.MMatrixFloat(output[1]:nrow(), output[1]:ncol()) - output[1]:copy_toh(mat) - - collectgarbage("collect") - return utt, mat - end - - return iterative_trainer -end - -function init(config, feature) - local tmp = io.write - io.write = function(...) - end - dofile(config) - trainer = build_trainer(gconf.decode_param, feature) - io.write = tmp -end - -function feed() - local utt, mat = trainer() - return utt, mat -end diff --git a/kaldi_decode/src/nnet-forward.cc b/kaldi_decode/src/nnet-forward.cc index 007f623..8781705 100644 --- a/kaldi_decode/src/nnet-forward.cc +++ b/kaldi_decode/src/nnet-forward.cc @@ -21,9 +21,9 @@ extern "C"{ #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#include "nerv/matrix/matrix.h" -#include "nerv/common.h" -#include "nerv/luaT/luaT.h" +#include "nerv/lib/matrix/matrix.h" +#include "nerv/lib/common.h" +#include "nerv/lib/luaT/luaT.h" } #include <limits> @@ -46,7 +46,7 @@ int main(int argc, char *argv[]) { const char *usage = "Perform forward pass through Neural Network.\n" "\n" - "Usage: nnet-forward [options] <nerv-config> <feature-rspecifier> <feature-wspecifier> [nerv4decode.lua]\n" + "Usage: nnet-forward [options] <nerv-config> <feature-rspecifier> <feature-wspecifier> [asr_propagator.lua]\n" "e.g.: \n" " nnet-forward config.lua ark:features.ark ark:mlpoutput.ark\n"; @@ -78,9 +78,9 @@ int main(int argc, char *argv[]) { std::string config = po.GetArg(1), feature_rspecifier = po.GetArg(2), feature_wspecifier = po.GetArg(3), - nerv4decode = "src/nerv4decode.lua"; - if(po.NumArgs() >= 4) - nerv4decode = po.GetArg(4); + propagator = "src/asr_propagator.lua"; + if(po.NumArgs() >= 4) + propagator = po.GetArg(4); //Select the GPU #if HAVE_CUDA==1 @@ -99,8 +99,8 @@ int main(int argc, char *argv[]) { lua_State *L = lua_open(); luaL_openlibs(L); - if(luaL_loadfile(L, nerv4decode.c_str())) - KALDI_ERR << "luaL_loadfile() " << nerv4decode << " failed " << lua_tostring(L, -1); + if(luaL_loadfile(L, propagator.c_str())) + KALDI_ERR << "luaL_loadfile() " << propagator << " failed " << lua_tostring(L, -1); if(lua_pcall(L, 0, 0, 0)) KALDI_ERR << "lua_pall failed " << lua_tostring(L, -1); diff --git a/kaldi_decode/utils/int2sym.pl b/kaldi_decode/utils/int2sym.pl deleted file mode 100755 index d618939..0000000 --- a/kaldi_decode/utils/int2sym.pl +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env perl -# Copyright 2010-2012 Microsoft Corporation Johns Hopkins University (Author: Daniel Povey) -# Apache 2.0. - -undef $field_begin; -undef $field_end; - - -if ($ARGV[0] eq "-f") { - shift @ARGV; - $field_spec = shift @ARGV; - if ($field_spec =~ m/^\d+$/) { - $field_begin = $field_spec - 1; $field_end = $field_spec - 1; - } - if ($field_spec =~ m/^(\d*)[-:](\d*)/) { # accept e.g. 1:10 as a courtesty (properly, 1-10) - if ($1 ne "") { - $field_begin = $1 - 1; # Change to zero-based indexing. - } - if ($2 ne "") { - $field_end = $2 - 1; # Change to zero-based indexing. - } - } - if (!defined $field_begin && !defined $field_end) { - die "Bad argument to -f option: $field_spec"; - } -} -$symtab = shift @ARGV; -if(!defined $symtab) { - print STDERR "Usage: sym2int.pl [options] symtab [input] > output\n" . - "options: [-f (<field>|<field_start>-<field-end>)]\n" . - "e.g.: -f 2, or -f 3-4\n"; - exit(1); -} - -open(F, "<$symtab") || die "Error opening symbol table file $symtab"; -while(<F>) { - @A = split(" ", $_); - @A == 2 || die "bad line in symbol table file: $_"; - $int2sym{$A[1]} = $A[0]; -} - -sub int2sym { - my $a = shift @_; - my $pos = shift @_; - if($a !~ m:^\d+$:) { # not all digits.. - $pos1 = $pos+1; # make it one-based. - die "int2sym.pl: found noninteger token $a [in position $pos1]\n"; - } - $s = $int2sym{$a}; - if(!defined ($s)) { - die "int2sym.pl: integer $a not in symbol table $symtab."; - } - return $s; -} - -$error = 0; -while (<>) { - @A = split(" ", $_); - for ($pos = 0; $pos <= $#A; $pos++) { - $a = $A[$pos]; - if ( (!defined $field_begin || $pos >= $field_begin) - && (!defined $field_end || $pos <= $field_end)) { - $a = int2sym($a, $pos); - } - print $a . " "; - } - print "\n"; -} - - - diff --git a/kaldi_decode/utils/parse_options.sh b/kaldi_decode/utils/parse_options.sh deleted file mode 100755 index fdc8a36..0000000 --- a/kaldi_decode/utils/parse_options.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/bin/bash - -# Copyright 2012 Johns Hopkins University (Author: Daniel Povey); -# Arnab Ghoshal, Karel Vesely - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -# WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -# MERCHANTABLITY OR NON-INFRINGEMENT. -# See the Apache 2 License for the specific language governing permissions and -# limitations under the License. - - -# Parse command-line options. -# To be sourced by another script (as in ". parse_options.sh"). -# Option format is: --option-name arg -# and shell variable "option_name" gets set to value "arg." -# The exception is --help, which takes no arguments, but prints the -# $help_message variable (if defined). - - -### -### The --config file options have lower priority to command line -### options, so we need to import them first... -### - -# Now import all the configs specified by command-line, in left-to-right order -for ((argpos=1; argpos<$#; argpos++)); do - if [ "${!argpos}" == "--config" ]; then - argpos_plus1=$((argpos+1)) - config=${!argpos_plus1} - [ ! -r $config ] && echo "$0: missing config '$config'" && exit 1 - . $config # source the config file. - fi -done - - -### -### No we process the command line options -### -while true; do - [ -z "${1:-}" ] && break; # break if there are no arguments - case "$1" in - # If the enclosing script is called with --help option, print the help - # message and exit. Scripts should put help messages in $help_message - --help|-h) if [ -z "$help_message" ]; then echo "No help found." 1>&2; - else printf "$help_message\n" 1>&2 ; fi; - exit 0 ;; - --*=*) echo "$0: options to scripts must be of the form --name value, got '$1'" - exit 1 ;; - # If the first command-line argument begins with "--" (e.g. --foo-bar), - # then work out the variable name as $name, which will equal "foo_bar". - --*) name=`echo "$1" | sed s/^--// | sed s/-/_/g`; - # Next we test whether the variable in question is undefned-- if so it's - # an invalid option and we die. Note: $0 evaluates to the name of the - # enclosing script. - # The test [ -z ${foo_bar+xxx} ] will return true if the variable foo_bar - # is undefined. We then have to wrap this test inside "eval" because - # foo_bar is itself inside a variable ($name). - eval '[ -z "${'$name'+xxx}" ]' && echo "$0: invalid option $1" 1>&2 && exit 1; - - oldval="`eval echo \\$$name`"; - # Work out whether we seem to be expecting a Boolean argument. - if [ "$oldval" == "true" ] || [ "$oldval" == "false" ]; then - was_bool=true; - else - was_bool=false; - fi - - # Set the variable to the right value-- the escaped quotes make it work if - # the option had spaces, like --cmd "queue.pl -sync y" - eval $name=\"$2\"; - - # Check that Boolean-valued arguments are really Boolean. - if $was_bool && [[ "$2" != "true" && "$2" != "false" ]]; then - echo "$0: expected \"true\" or \"false\": $1 $2" 1>&2 - exit 1; - fi - shift 2; - ;; - *) break; - esac -done - - -# Check for an empty argument to the --cmd option, which can easily occur as a -# result of scripting errors. -[ ! -z "${cmd+xxx}" ] && [ -z "$cmd" ] && echo "$0: empty argument to --cmd option" 1>&2 && exit 1; - - -true; # so this script returns exit code 0. diff --git a/kaldi_decode/utils/queue.pl b/kaldi_decode/utils/queue.pl deleted file |