Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SINGA-236 memory pool #236

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

PROJECT(singa)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 ")

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Thirdparty)
#message(STATUS "module path: ${CMAKE_MODULE_PATH}")
Expand All @@ -18,13 +18,14 @@ SET(SINGA_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/include;${CMAKE_SOURCE_DIR}/lib/cnmem/include;${PROJECT_BINARY_DIR}")
INCLUDE_DIRECTORIES(${SINGA_INCLUDE_DIR})

OPTION(USE_CBLAS "Use CBlas libs" ON)
OPTION(USE_CUDA "Use Cuda libs" ON)
OPTION(USE_CUDNN "Use Cudnn libs" ON)
OPTION(USE_CBLAS "Use CBlas libs" OFF)
OPTION(USE_CUDA "Use Cuda libs" OFF)
OPTION(USE_CUDNN "Use Cudnn libs" OFF)
OPTION(USE_OPENCV "Use opencv" OFF)
OPTION(USE_LMDB "Use LMDB libs" OFF)
OPTION(USE_PYTHON "Generate py wrappers" ON)
OPTION(USE_PYTHON "Generate py wrappers" OFF)
OPTION(USE_OPENCL "Use OpenCL" OFF)
OPTION(ENABLE_DIST "enable distributed training" OFF)
#OPTION(BUILD_OPENCL_TESTS "Build OpenCL tests" OFF)

INCLUDE("cmake/Dependencies.cmake")
Expand All @@ -46,6 +47,12 @@ IF (USE_CUDA)
ADD_SUBDIRECTORY(lib/cnmem)
LIST(APPEND SINGA_LINKER_LIBS cnmem)
ENDIF()

# TODO(wangwei) detect the ev lib
IF (ENABLE_DIST)
LIST(APPEND SINGA_LINKER_LIBS ev)
ENDIF()

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(examples)
4 changes: 4 additions & 0 deletions cmake/Templates/singa_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

#cmakedefine USE_CUDNN
#cmakedefine CUDNN_VERSION_MAJOR @CUDNN_VERSION_MAJOR@
#cmakedefine CUDNN_VERSION_MINOR @CUDNN_VERSION_MINOR@
#cmakedefine CUDNN_VERSION_PATCH @CUDNN_VERSION_PATCH@

#cmakedefine USE_OPENCL

#cmakedefine ENABLE_DIST

// lmdb
#cmakedefine USE_LMDB

2 changes: 1 addition & 1 deletion cmake/Thirdparty/FindCUDNN.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ IF(CUDNN_FOUND)
ELSE()
SET(CUDNN_VERSION "${CUDNN_VERSION_MAJOR}.${CUDNN_VERSION_MINOR}.${CUDNN_VERSION_PATCH}")
ENDIF()
MESSAGE(STATUS "Found Cudnn_v${CUDNN_VERSION} at ${CUDNN_INCLUDE_DIR}")
MESSAGE(STATUS "Found Cudnn_v${CUDNN_VERSION} at ${CUDNN_INCLUDE_DIR} ${CUDNN_LIBRARIES}")
MARK_AS_ADVANCED(CUDNN_INCLUDE_DIR CUDNN_LIBRARIES)

ENDIF()
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ADD_SUBDIRECTORY(cifar10)
ADD_SUBDIRECTORY(imagenet)
30 changes: 30 additions & 0 deletions examples/char-rnn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Train Char-RNN using SINGA

Recurrent neural networks (RNN) are widely used for modelling sequential data,
e.g., natural language sentences. This example describes how to implement a RNN
application (or model) using SINGA's RNN layers.
We will use the [char-rnn](https://github.com/karpathy/char-rnn) model as an
example, which trains over sentences or
source code, with each character as an input unit. Particularly, we will train
a RNN using GRU over Linux kernel source code. After training, we expect to
generate meaningful code from the model.


## Instructions

* Compile and install SINGA. Currently the RNN implementation depends on Cudnn with version >= 5.05.

* Prepare the dataset. Download the [kernel source code](http://cs.stanford.edu/people/karpathy/char-rnn/).
Other plain text files can also be used.

* Start the training,

python train.py input_linux.txt

Some hyper-parameters could be set through command line,

python train.py -h

* Sample characters from the model by providing the number of characters to sample and the seed string.

python sample.py 100 --seed '#include <std'
102 changes: 102 additions & 0 deletions examples/char-rnn/sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
'''Sample characters from the pre-trained model'''
import sys
import os
import cPickle as pickle
import numpy as np
import argparse

sys.path.append(os.path.join(os.path.dirname(__file__), '../../build/python'))
from singa import layer
from singa import tensor
from singa import device
from singa.proto import model_pb2


def sample(model_path, nsamples=100, seed_text='', do_sample=True):
with open(model_path, 'rb') as fd:
d=pickle.load(fd)
rnn_w = tensor.from_numpy(d['rnn_w'])
idx_to_char=d['idx_to_char']
char_to_idx=d['char_to_idx']
vocab_size = len(idx_to_char)
dense_w = tensor.from_numpy(d['dense_w'])
dense_b = tensor.from_numpy(d['dense_b'])
hidden_size = d['hidden_size']
num_stacks = d['num_stacks']
dropout = d['dropout']

cuda = device.create_cuda_gpu()
rnn = layer.LSTM(name='lstm', hidden_size=hidden_size,
num_stacks=num_stacks, dropout=dropout,
input_sample_shape=(len(idx_to_char),))
rnn.to_device(cuda)
rnn.param_values()[0].copy_data(rnn_w)
dense = layer.Dense('dense', vocab_size, input_sample_shape=(hidden_size,))
dense.to_device(cuda)
dense.param_values()[0].copy_data(dense_w)
dense.param_values()[1].copy_data(dense_b)
hx = tensor.Tensor((num_stacks, 1, hidden_size), cuda)
cx = tensor.Tensor((num_stacks, 1, hidden_size), cuda)
hx.set_value(0.0)
cx.set_value(0.0)
if len(seed_text) > 0:
for c in seed_text:
x = np.zeros((1, vocab_size), dtype=np.float32)
x[0, char_to_idx[c]] = 1
tx=tensor.from_numpy(x)
tx.to_device(cuda)
inputs=[tx, hx, cx]
outputs=rnn.forward(model_pb2.kEval, inputs)
y = dense.forward(model_pb2.kEval, outputs[0])
y = tensor.softmax(y)
hx = outputs[1]
cx = outputs[2]
sys.stdout.write(seed_text)
else:
y = tensor.Tensor((1, vocab_size), cuda)
y.set_value(1.0 / vocab_size)

for i in range(nsamples):
y.to_host()
prob = tensor.to_numpy(y)[0]
if do_sample:
cur=np.random.choice(vocab_size, 1, p=prob)[0]
else:
cur = np.argmax(prob)
sys.stdout.write(idx_to_char[cur])
x = np.zeros((1, vocab_size), dtype=np.float32)
x[0, cur] = 1
tx=tensor.from_numpy(x)
tx.to_device(cuda)
inputs=[tx, hx, cx]
outputs=rnn.forward(model_pb2.kEval, inputs)
y = dense.forward(model_pb2.kEval, outputs[0])
y = tensor.softmax(y)
hx = outputs[1]
cx = outputs[2]
print ''

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='sample chars from char-rnn')
parser.add_argument('--seed', help='seed text string which warms up the rnn'\
' states for sampling', default='')
parser.add_argument('n', type=int, help='num of characters to sample')
args = parser.parse_args()
assert args.n > 0, 'n must > 0'
sample('model.bin', args.n, seed_text=args.seed)
Loading