diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | examples/chunk_file_example.lua | 53 | ||||
-rw-r--r-- | io/chunk_file.c | 22 | ||||
-rw-r--r-- | io/chunk_file.h | 1 | ||||
-rw-r--r-- | io/init.lua | 3 | ||||
-rw-r--r-- | io/sgd_buffer.lua | 2 | ||||
-rw-r--r-- | matrix/cuda_helper.h | 4 | ||||
-rw-r--r-- | nn/layer_dag.lua | 11 |
8 files changed, 86 insertions, 13 deletions
@@ -12,7 +12,8 @@ LUA_LIBS := matrix/init.lua io/init.lua nerv.lua \ nn/init.lua nn/layer_repo.lua nn/param_repo.lua nn/layer_dag.lua \ io/sgd_buffer.lua INCLUDE := -I build/luajit-2.0/include/luajit-2.0/ -DLUA_USE_APICHECK -CUDA_BASE := /usr/local/cuda-6.5 +# CUDA_BASE := /usr/local/cuda-6.5 +CUDA_BASE := /usr/local/cuda-5.0 CUDA_INCLUDE := -I $(CUDA_BASE)/include/ INCLUDE += $(CUDA_INCLUDE) LDFLAGS := -L$(CUDA_BASE)/lib64/ -Wl,-rpath=$(CUDA_BASE)/lib64/ -lcudart -lcublas diff --git a/examples/chunk_file_example.lua b/examples/chunk_file_example.lua new file mode 100644 index 0000000..5961c98 --- /dev/null +++ b/examples/chunk_file_example.lua @@ -0,0 +1,53 @@ +-- To define a readable and writable chunk, one must define a class with the +-- following methods: __init(id, global_conf), read(handle), write(handle), +-- get_info(), set_info(info) and an id attribute. This file demonstrates a +-- basic chunk implementation which manages the I/O of a matrix + +local MatrixChunk = nerv.class("nerv.MatrixChunk") + +function MatrixChunk:__init(id, global_conf) + self.id = id + self.info = {} + self.gconf = global_conf +end + +function MatrixChunk:read(handle) + -- pass the read handle to the matrix method + self.data = nerv.MMatrixFloat.load(handle) +end + +function MatrixChunk:write(handle) + -- pass the write handle to the matrix method + self.data:save(handle) +end + +function MatrixChunk:get_info() + return self.info +end + +function MatrixChunk:set_info(info) + self.info = info +end + +function MatrixChunk.create_from_matrix(id, mat) + local ins = nerv.MatrixChunk(id) + ins.data = mat + return ins +end + +mat = nerv.MMatrixFloat(3, 4) +for i = 0, 2 do + for j = 0, 3 do + mat[i][j] = i + j + end +end + +cd = nerv.MatrixChunk.create_from_matrix("matrix1", mat) + +cf = nerv.ChunkFile("test.nerv", "w") +cf:write_chunk(cd) +cf:close() + +cf2 = nerv.ChunkFile("test.nerv", "r") +cd2 = cf2:read_chunk("matrix1") +print(cd2.data) diff --git a/io/chunk_file.c b/io/chunk_file.c index 4e987b7..aa7dd1c 100644 --- a/io/chunk_file.c +++ b/io/chunk_file.c @@ -10,6 +10,11 @@ do { \ if ((exp) != (ret)) INVALID_FORMAT_ERROR(fn); \ } while (0) +#define CHECK_FILE_OPEN(pfh) \ + do { \ + if ((pfh)->closed) \ + nerv_error(L, "operations on a closed file"); \ + } while (0) const char *nerv_chunk_file_tname = "nerv.ChunkFile"; const char *nerv_chunk_file_handle_tname = "nerv.ChunkFileHandle"; @@ -109,6 +114,7 @@ int nerv_chunk_file_open_write(lua_State *L, const char *fn) { if (!fp) nerv_error(L, "Error while opening chunk file: %s", fn); lfp = (ChunkFileHandle *)malloc(sizeof(ChunkFileHandle)); lfp->fp = fp; + lfp->closed = 0; luaT_pushudata(L, lfp, nerv_chunk_file_handle_tname); lua_setfield(L, -2, "handle"); luaT_pushmetatable(L, nerv_chunk_file_tname); @@ -174,6 +180,7 @@ int nerv_chunk_file_open_read(lua_State *L, const char *fn) { lua_setfield(L, -2, "metadata"); lfp = (ChunkFileHandle *)malloc(sizeof(ChunkFileHandle)); lfp->fp = fp; + lfp->closed = 0; luaT_pushudata(L, lfp, nerv_chunk_file_handle_tname); lua_setfield(L, -2, "handle"); luaT_pushmetatable(L, nerv_chunk_file_tname); @@ -215,6 +222,7 @@ int nerv_chunk_file_write_chunkdata(lua_State *L) { const char *metadata_str = lua_tolstring(L, 2, NULL); lua_getfield(L, 1, "handle"); pfh = luaT_checkudata(L, -1, nerv_chunk_file_handle_tname); + CHECK_FILE_OPEN(pfh); start = ftello(pfh->fp); write_chunk_header_plain(pfh->fp, 0, &status); /* fill zeros */ CHECK_WRITE(status); @@ -245,6 +253,7 @@ int nerv_chunk_file_get_chunkdata(lua_State *L) { lua_getfield(L, 1, "handle"); pfh = luaT_checkudata(L, -1, nerv_chunk_file_handle_tname); + CHECK_FILE_OPEN(pfh); lua_pop(L, 1); /* pop handle */ lua_getfield(L, 1, "metadata"); /* now stack: self, k, metadata */ @@ -260,10 +269,20 @@ int nerv_chunk_file_get_chunkdata(lua_State *L) { return 1; } +int nerv_chunk_file_close(lua_State *L) { + ChunkFileHandle *pfh; + lua_getfield(L, 1, "handle"); + pfh = luaT_checkudata(L, -1, nerv_chunk_file_handle_tname); + CHECK_FILE_OPEN(pfh); + fclose(pfh->fp); + pfh->closed = 1; + return 0; +} + int nerv_chunk_file_handle_destroy(lua_State *L) { ChunkFileHandle *pfh = luaT_checkudata(L, 1, nerv_chunk_file_handle_tname); - fclose(pfh->fp); + if (!pfh->closed) fclose(pfh->fp); free(pfh); return 0; } @@ -285,6 +304,7 @@ static int nerv_chunk_data_destroy(lua_State *L) { static const luaL_Reg nerv_chunk_file_methods[] = { {"get_chunkdata", nerv_chunk_file_get_chunkdata}, {"_write_chunkdata", nerv_chunk_file_write_chunkdata}, + {"close", nerv_chunk_file_close}, {"__init", nerv_chunk_file___init}, {NULL, NULL} }; diff --git a/io/chunk_file.h b/io/chunk_file.h index 9ece117..9bae59d 100644 --- a/io/chunk_file.h +++ b/io/chunk_file.h @@ -8,6 +8,7 @@ extern const char *nerv_chunk_data_tname; typedef struct ChunkFileHandle { FILE *fp; + int closed; } ChunkFileHandle; typedef struct ChunkInfo { diff --git a/io/init.lua b/io/init.lua index c151804..b722a81 100644 --- a/io/init.lua +++ b/io/init.lua @@ -18,6 +18,9 @@ function nerv.ChunkFile:write_chunk(chunk) end function nerv.ChunkFile:read_chunk(id, global_conf) + if self.metadata == nil then + nerv.error("wrong file opening mode") + end local metadata = self.metadata[id] if metadata == nil then nerv.error("chunk with id %s does not exist", id) diff --git a/io/sgd_buffer.lua b/io/sgd_buffer.lua index bf72744..381b863 100644 --- a/io/sgd_buffer.lua +++ b/io/sgd_buffer.lua @@ -15,7 +15,7 @@ function SGDBuffer:__init(global_conf, buffer_conf) local buffs = {} for id, width in pairs(reader_spec.data) do buffs[id] = {data = global_conf.mmat_type(self.buffer_size, width), - leftover = {}, + leftover = nil, width = width} end table.insert(self.readers, {buffs = buffs, diff --git a/matrix/cuda_helper.h b/matrix/cuda_helper.h index 88619fd..d6effdb 100644 --- a/matrix/cuda_helper.h +++ b/matrix/cuda_helper.h @@ -52,10 +52,10 @@ static const char *cublasGetErrorString(cublasStatus_t err) { return "CUBLAS_STATUS_EXECUTION_FAILED"; case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR"; - case CUBLAS_STATUS_NOT_SUPPORTED: +/* case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED"; case CUBLAS_STATUS_LICENSE_ERROR: - return "CUBLAS_STATUS_LICENSE_ERROR"; + return "CUBLAS_STATUS_LICENSE_ERROR"; */ } return "<unknown>"; } diff --git a/nn/layer_dag.lua b/nn/layer_dag.lua index 4ee829e..3951bfa 100644 --- a/nn/layer_dag.lua +++ b/nn/layer_dag.lua @@ -210,7 +210,9 @@ function nerv.DAGLayer:update(bp_err, input, output) self:set_err_inputs(bp_err) self:set_inputs(input) self:set_outputs(output) + -- print("update") for id, ref in pairs(self.queue) do + -- print(ref.layer.id) ref.layer:update(ref.err_inputs, ref.inputs, ref.outputs) end end @@ -220,11 +222,7 @@ function nerv.DAGLayer:propagate(input, output) self:set_outputs(output) for i = 1, #self.queue do local ref = self.queue[i] - --[[ - print(ref.inputs[1]) - print(ref.outputs[1]) - print(#ref.inputs, #ref.outputs) - --]] + -- print(ref.layer.id) ref.layer:propagate(ref.inputs, ref.outputs) end end @@ -238,8 +236,5 @@ function nerv.DAGLayer:back_propagate(next_bp_err, bp_err, input, output) local ref = self.queue[i] -- print(ref.layer.id) ref.layer:back_propagate(ref.err_outputs, ref.err_inputs, ref.inputs, ref.outputs) - -- if #ref.err_outputs > 0 then - -- print(ref.err_outputs[1]) - -- end end end |