summaryrefslogtreecommitdiff
path: root/htk_io
diff options
context:
space:
mode:
Diffstat (limited to 'htk_io')
-rw-r--r--htk_io/Makefile9
-rw-r--r--htk_io/init.c2
-rw-r--r--htk_io/init.lua20
-rw-r--r--htk_io/src/cwrapper.cpp15
-rw-r--r--htk_io/src/cwrapper.h9
-rw-r--r--htk_io/src/init.c12
-rw-r--r--htk_io/src/test.c7
7 files changed, 52 insertions, 22 deletions
diff --git a/htk_io/Makefile b/htk_io/Makefile
index d32d17a..6a5f529 100644
--- a/htk_io/Makefile
+++ b/htk_io/Makefile
@@ -1,3 +1,7 @@
+ifndef LUA_BINDIR
+$(error Please build the package via luarocks: `luarocks make`)
+endif
+
.PHONY: tnet
SHELL := /bin/bash
BUILD_DIR := $(CURDIR)/build
@@ -17,6 +21,7 @@ OBJ_SUBDIR := $(addprefix $(OBJ_DIR)/,$(SUBDIR))
LUA_SUBDIR := $(addprefix $(LUA_DIR)/,$(SUBDIR))
LUA_LIBS := $(addprefix $(LUA_DIR)/,$(LUA_LIBS))
LIB_PATH := $(LUA_BINDIR)/../lib
+LUALIB_PATH := $(LUA_BINDIR)/../lib/lua/5.1/
build: $(OBJ_DIR) $(OBJ_SUBDIR) $(OBJS) tnet $(OBJ_DIR)/src/test
install: $(LUA_DIR) $(LUA_SUBDIR) $(LUA_LIBS) $(LIBS)
@@ -26,7 +31,7 @@ $(OBJ_DIR) $(LUA_DIR) $(OBJ_SUBDIR) $(LUA_SUBDIR):
$(LUA_DIR)/%.lua: %.lua
cp $< $@
$(LIBS): $(OBJ_DIR)/src/cwrapper.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/init.o $(OBJ_DIR)/src/libKaldiLib.a
- gcc -shared -o $@ $(OBJ_DIR)/src/cwrapper.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/libKaldiLib.a $(OBJ_DIR)/src/init.o -lstdc++ -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -lluaT
+ gcc -shared -o $@ $(OBJ_DIR)/src/cwrapper.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/libKaldiLib.a $(OBJ_DIR)/src/init.o -lstdc++ -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -Wl,-rpath=$(LUALIB_PATH) -L$(LUALIB_PATH) -lnerv -lluaT
$(OBJ_DIR)/src/test: $(OBJ_DIR)/src/cwrapper.o $(OBJ_DIR)/src/test.o $(OBJ_DIR)/src/libKaldiLib.a
gcc -o $@ $^ -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -Wl,-rpath=$(LUA_LIBDIR) -L$(LUA_LIBDIR) -lluajit-5.1 -lstdc++ -lm
$(OBJ_DIR)/src/cwrapper.o: src/cwrapper.cpp
@@ -34,7 +39,7 @@ $(OBJ_DIR)/src/cwrapper.o: src/cwrapper.cpp
$(OBJ_DIR)/%.o: %.c
gcc -o $@ -c $< -g $(INCLUDE) -fPIC
clean:
- -rm $(OBJ_DIR)/src/*.o
+ -rm -r $(OBJ_DIR)
$(MAKE) -C src/KaldiLib/ clean
tnet:
$(MAKE) -C src/KaldiLib/ OBJ_DIR=$(OBJ_DIR)/src
diff --git a/htk_io/init.c b/htk_io/init.c
index edd454f..5b373f9 100644
--- a/htk_io/init.c
+++ b/htk_io/init.c
@@ -1,4 +1,4 @@
-#include "../nerv/common.h"
+#include "nerv/lib/common.h"
#include <stdio.h>
extern void tnet_io_init(lua_State *L);
diff --git a/htk_io/init.lua b/htk_io/init.lua
index b360b67..1cdabf1 100644
--- a/htk_io/init.lua
+++ b/htk_io/init.lua
@@ -6,6 +6,11 @@ function TNetReader:__init(global_conf, reader_conf)
self.feat_id = reader_conf.id
self.frm_ext = reader_conf.frm_ext
self.gconf = global_conf
+ if self.gconf.use_cpu then
+ self.mat_type = self.gconf.mmat_type
+ else
+ self.mat_type = self.gconf.cumat_type
+ end
self.debug = global_conf.debug
if self.debug == nil then
self.debug = false
@@ -31,12 +36,15 @@ function TNetReader:get_data()
end
local res = {}
-- read HTK feature
- local raw = self.gconf.cumat_type.new_from_host(self.feat_repo:cur_utter(self.debug))
+ local raw = self.feat_repo:cur_utter(self.debug)
+ if not self.gconf.use_cpu then
+ raw = self.gconf.cumat_type.new_from_host(raw)
+ end
local rearranged
if self.frm_ext and self.frm_ext > 0 then
local step = self.frm_ext * 2 + 1
-- expand the feature
- local expanded = self.gconf.cumat_type(raw:nrow(), raw:ncol() * step)
+ local expanded = self.mat_type(raw:nrow(), raw:ncol() * step)
expanded:expand_frm(raw, self.frm_ext)
-- rearrange the feature (``transpose'' operation in TNet)
if self.gconf.rearrange then
@@ -53,8 +61,12 @@ function TNetReader:get_data()
feat_utter = self.gconf.mmat_type(rearranged:nrow() - self.gconf.frm_trim * 2, rearranged:ncol())
rearranged:copy_toh(feat_utter, self.gconf.frm_trim, rearranged:nrow() - self.gconf.frm_trim)
else
- feat_utter = self.gconf.mmat_type(rearranged:nrow(), rearranged:ncol())
- rearranged:copy_toh(feat_utter)
+ if self.gconf.use_cpu then
+ feat_utter = rearranged
+ else
+ feat_utter = self.gconf.mmat_type(rearranged:nrow(), rearranged:ncol())
+ rearranged:copy_toh(feat_utter)
+ end
end
res[self.feat_id] = feat_utter
-- add corresponding labels
diff --git a/htk_io/src/cwrapper.cpp b/htk_io/src/cwrapper.cpp
index b7ce2d5..efb5628 100644
--- a/htk_io/src/cwrapper.cpp
+++ b/htk_io/src/cwrapper.cpp
@@ -8,9 +8,10 @@
extern "C" {
#include "cwrapper.h"
#include "string.h"
-#include "nerv/common.h"
+#include "nerv/lib/common.h"
+#include "nerv/lib/matrix/mmatrix.h"
- extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status);
+ extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, MContext *context, Status *status);
struct TNetFeatureRepo {
TNet::FeatureRepository feature_repo;
@@ -53,7 +54,8 @@ extern "C" {
return repo;
}
- Matrix *tnet_feature_repo_read_utterance(TNetFeatureRepo *repo, lua_State *L, int debug) {
+ Matrix *tnet_feature_repo_read_utterance(TNetFeatureRepo *repo, lua_State *L,
+ int debug, MContext *context) {
Matrix *mat; /* nerv implementation */
repo->feature_repo.ReadFullMatrix(repo->feats_host);
std::string utter_str = repo->feature_repo.Current().Logical();
@@ -61,7 +63,7 @@ extern "C" {
int n = repo->feats_host.Rows();
int m = repo->feats_host.Cols();
Status status;
- mat = nerv_matrix_host_float_create(n, m, &status);
+ mat = nerv_matrix_host_float_create(n, m, context, &status);
NERV_LUA_CHECK_STATUS(L, status);
size_t stride = mat->stride;
if (debug)
@@ -119,7 +121,8 @@ extern "C" {
size_t sample_rate,
const char *tag,
lua_State *L,
- int debug) {
+ int debug,
+ MContext *context) {
std::vector<TNet::Matrix<float> > labs_hosts; /* KaldiLib implementation */
Matrix *mat;
repo->label_repo.GenDesiredMatrixExt(labs_hosts, frames,
@@ -127,7 +130,7 @@ extern "C" {
int n = labs_hosts[0].Rows();
int m = labs_hosts[0].Cols();
Status status;
- mat = nerv_matrix_host_float_create(n, m, &status);
+ mat = nerv_matrix_host_float_create(n, m, context, &status);
NERV_LUA_CHECK_STATUS(L, status);
size_t stride = mat->stride;
if (debug)
diff --git a/htk_io/src/cwrapper.h b/htk_io/src/cwrapper.h
index e1bce6e..0469773 100644
--- a/htk_io/src/cwrapper.h
+++ b/htk_io/src/cwrapper.h
@@ -1,7 +1,7 @@
#ifndef NERV_TNET_IO_CWRAPPER
#define NERV_TNET_IO_CWRAPPER
-#include "nerv/matrix/matrix.h"
-#include "nerv/common.h"
+#include "nerv/lib/matrix/mmatrix.h"
+#include "nerv/lib/common.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -10,7 +10,7 @@ extern "C" {
TNetFeatureRepo *tnet_feature_repo_new(const char *scp,
const char *config, int context);
- Matrix *tnet_feature_repo_read_utterance(TNetFeatureRepo *repo, lua_State *L, int debug);
+ Matrix *tnet_feature_repo_read_utterance(TNetFeatureRepo *repo, lua_State *L, int debug, MContext *context);
size_t tnet_feature_repo_current_samplerate(TNetFeatureRepo *repo);
const char *tnet_feature_repo_current_tag(TNetFeatureRepo *repo);
void tnet_feature_repo_next(TNetFeatureRepo *repo);
@@ -28,7 +28,8 @@ extern "C" {
size_t sample_rate,
const char *tag,
lua_State *L,
- int debug);
+ int debug,
+ MContext *context);
void tnet_label_repo_destroy(TNetLabelRepo *repo);
#ifdef __cplusplus
diff --git a/htk_io/src/init.c b/htk_io/src/init.c
index 8a1ec3b..a5132ba 100644
--- a/htk_io/src/init.c
+++ b/htk_io/src/init.c
@@ -1,4 +1,5 @@
-#include "nerv/common.h"
+#include "nerv/lib/common.h"
+#include "nerv/matrix/matrix.h"
#include "cwrapper.h"
#include <stdio.h>
@@ -28,12 +29,14 @@ static int feat_repo_current_tag(lua_State *L) {
}
static int feat_repo_current_utterance(lua_State *L) {
+ MContext *context;
+ MMATRIX_GET_CONTEXT(L, 3);
TNetFeatureRepo *repo = luaT_checkudata(L, 1, nerv_tnet_feat_repo_tname);
int debug;
if (!lua_isboolean(L, 2))
nerv_error(L, "debug flag should be a boolean");
debug = lua_toboolean(L, 2);
- Matrix *utter = tnet_feature_repo_read_utterance(repo, L, debug);
+ Matrix *utter = tnet_feature_repo_read_utterance(repo, L, debug, context);
luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
return 1;
}
@@ -72,6 +75,8 @@ static int label_repo_new(lua_State *L) {
}
static int label_repo_read_utterance(lua_State *L) {
+ MContext *context;
+ MMATRIX_GET_CONTEXT(L, 5);
TNetLabelRepo *repo = luaT_checkudata(L, 1, nerv_tnet_label_repo_tname);
TNetFeatureRepo *feat_repo = luaT_checkudata(L, 2, nerv_tnet_feat_repo_tname);
size_t frames = luaL_checkinteger(L, 3);
@@ -82,7 +87,8 @@ static int label_repo_read_utterance(lua_State *L) {
Matrix *utter = tnet_label_repo_read_utterance(repo,
frames,
tnet_feature_repo_current_samplerate(feat_repo),
- tnet_feature_repo_current_tag(feat_repo), L, debug);
+ tnet_feature_repo_current_tag(feat_repo),
+ L, debug, context);
luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
return 1;
}
diff --git a/htk_io/src/test.c b/htk_io/src/test.c
index 6812ef1..1ced108 100644
--- a/htk_io/src/test.c
+++ b/htk_io/src/test.c
@@ -1,6 +1,9 @@
#include "cwrapper.h"
+#include "nerv/lib/matrix/mmatrix.h"
#include <stdio.h>
+MContext context;
+
void print_nerv_matrix(Matrix *mat) {
int n = mat->nrow;
int m = mat->ncol;
@@ -22,7 +25,7 @@ int main() {
"/slfs1/users/mfy43/swb_ivec/train_bp.scp",
"/slfs1/users/mfy43/swb_ivec/plp_0_d_a.conf", 5);
Matrix *feat_utter;
- feat_utter = tnet_feature_repo_read_utterance(feat_repo, NULL, 1);
+ feat_utter = tnet_feature_repo_read_utterance(feat_repo, NULL, 1, &context);
TNetLabelRepo *lab_repo = tnet_label_repo_new(
"/slfs1/users/mfy43/swb_ivec/ref.mlf",
@@ -34,7 +37,7 @@ int main() {
feat_utter->nrow - 5 * 2,
tnet_feature_repo_current_samplerate(feat_repo),
tnet_feature_repo_current_tag(feat_repo), NULL,
- 1);
+ 1, &context);
print_nerv_matrix(lab_utter);
return 0;
}