summaryrefslogtreecommitdiff
path: root/kaldi_decode/src
diff options
context:
space:
mode:
Diffstat (limited to 'kaldi_decode/src')
-rw-r--r--kaldi_decode/src/Makefile12
-rw-r--r--kaldi_decode/src/asr_propagator.lua84
-rw-r--r--kaldi_decode/src/nerv4decode.lua79
-rw-r--r--kaldi_decode/src/nnet-forward.cc18
4 files changed, 93 insertions, 100 deletions
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);