You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by ka...@apache.org on 2016/11/24 07:18:04 UTC
[1/2] incubator-singa git commit: SINGA-261 Add version ID into the
checkpoint files
Repository: incubator-singa
Updated Branches:
refs/heads/master 5afd81c84 -> e43ea6945
SINGA-261 Add version ID into the checkpoint files
1. Rename the cudnn versions to CUDNN_MAJOR_VERSION, CUDNN_MINOR_VERSION
and CUDNN_PATCH_VERSION
2. Add SINGA_MAJOR_VERSION, SINGA_MINOR_VRESION, SINGA_PATCH_VERSION,
SINGA_VERSION
into cmake and singa_confi.g
3. Write the SINGA_VERSION into the snapshot file (both bin and desc
files); And Read it when loading model parameters from the snapshot file; The versions
in the bin file and desc file must match;
3. rename the generated snapshot file sufix from .model to .bin; It is still able
to load .model file.
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/0bf1475e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/0bf1475e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/0bf1475e
Branch: refs/heads/master
Commit: 0bf1475ecf4c4267b5a5f6667a71f8142eb24e42
Parents: 5afd81c
Author: Wei Wang <wa...@comp.nus.edu.sg>
Authored: Tue Nov 15 21:24:14 2016 +0800
Committer: Wei Wang <wa...@gmail.com>
Committed: Tue Nov 22 18:04:23 2016 +0800
----------------------------------------------------------------------
CMakeLists.txt | 26 +++++++++++++++-----------
cmake/Templates/singa_config.h.in | 18 ++++++++++++------
cmake/Thirdparty/FindCUDNN.cmake | 22 +++++++++++-----------
include/singa/io/snapshot.h | 6 ++++++
src/api/config.i.in | 8 +++++++-
src/api/model_layer.i | 4 ++--
src/io/binfile_reader.cc | 3 ++-
src/io/snapshot.cc | 32 +++++++++++++++++++++++++++-----
src/io/textfile_reader.cc | 3 ++-
test/singa/test_snapshot.cc | 1 +
10 files changed, 85 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a07b694..0d763c5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,30 +6,34 @@
# 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.
-#
+#
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(singa)
-SET(PACKAGE_VERSION "1.0.0")
+SET(PACKAGE_VERSION "1.0.1")
+SET(SINGA_MAJOR_VERSION 1) # 0 -
+SET(SINGA_MINOR_VERSION 0) # 0 - 9
+SET(SINGA_PATCH_VERSION 1) # 0 - 99
+MATH(EXPR SINGA_VERSION "${SINGA_MAJOR_VERSION} * 1000 + ${SINGA_MINOR_VERSION} * 100 + ${SINGA_PATCH_VERSION}")
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Thirdparty)
#message(STATUS "module path: ${CMAKE_MODULE_PATH}")
# Flags
IF(UNIX OR APPLE)
- SET(CMAKE_CXX_FLAGS
+ SET(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 -fPIC -Wall -pthread")
ENDIF()
-IF(WIN32)
+IF(WIN32)
IF (MSVC)
MESSAGE(STATUS "Using msvc compiler")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_SCL_SECURE_NO_WARNINGS")
@@ -101,13 +105,13 @@ IF (USE_MODULES)
# ExternalProject_Add(protobuf
# GIT_REPOSITORY "https://github.com/google/protobuf.git"
# GIT_TAG "2.7.0"
- # SOURCE_DIR "protobuf/"
+ # SOURCE_DIR "protobuf/"
# CONFIGURE_COMMAND "${CMAKE_COMMAND}"
# "-H${CMAKE_BINARY_DIR}/protobuf/cmake" "-B."
# "-Dprotobuf_BUILD_TESTS=OFF" "-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}"
# "-Dprotobuf_BUILD_SHARED_LIBS=ON" "-DCMAKE_C_FLAGS=-fPIC"
# "-DCMAKE_CXX_FLAGS=-fPIC"
- # BUILD_COMMAND "${CMAKE_COMMAND}" --build "."
+ # BUILD_COMMAND "${CMAKE_COMMAND}" --build "."
# )
#ELSE()
#ENDIF()
@@ -117,7 +121,7 @@ IF (USE_MODULES)
"-O" "protobuf-2.6.1.tar.gz"
UPDATE_COMMAND "tar" "zxvf"
"${CMAKE_BINARY_DIR}/protobuf-prefix/src/protobuf-2.6.1.tar.gz" "-C" ".."
- SOURCE_DIR "protobuf-2.6.1/"
+ SOURCE_DIR "protobuf-2.6.1/"
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND "./configure" "--disable-shared"
"--prefix=${CMAKE_BINARY_DIR}/" "CXXFLAGS=-fPIC"
@@ -127,10 +131,10 @@ IF (USE_MODULES)
GIT_REPOSITORY "https://github.com/xianyi/OpenBLAS.git"
#GIT_TAG "develop"
GIT_TAG "f3419e6"
- SOURCE_DIR "openblas/"
+ SOURCE_DIR "openblas/"
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ""
- BUILD_COMMAND "make" "ONLY_CBLAS=1"
+ BUILD_COMMAND "make" "ONLY_CBLAS=1"
INSTALL_COMMAND "make" "PREFIX=${CMAKE_BINARY_DIR}/" "install"
)
ENDIF()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/cmake/Templates/singa_config.h.in
----------------------------------------------------------------------
diff --git a/cmake/Templates/singa_config.h.in b/cmake/Templates/singa_config.h.in
index f3500d0..baa625c 100644
--- a/cmake/Templates/singa_config.h.in
+++ b/cmake/Templates/singa_config.h.in
@@ -30,12 +30,6 @@
// cuda
#cmakedefine USE_CUDA
-#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 CUDNN_VERSION_SWIG @CUDNN_VERSION_SWIG@
-
#cmakedefine USE_OPENCL
#cmakedefine ENABLE_DIST
@@ -43,3 +37,15 @@
// lmdb
#cmakedefine USE_LMDB
+// singa version
+#cmakedefine SINGA_MAJOR_VERSION @SINGA_MAJOR_VERSION@
+#cmakedefine SINGA_MINOR_VERSION @SINGA_MINOR_VERSION@
+#cmakedefine SINGA_PATCH_VERSION @SINGA_PATCH_VERSION@
+#cmakedefine SINGA_VERSION @SINGA_VERSION@
+
+// cudnn version
+#cmakedefine USE_CUDNN
+#cmakedefine CUDNN_MAJOR_VERSION @CUDNN_MAJOR_VERSION@
+#cmakedefine CUDNN_MINOR_VERSION @CUDNN_MINOR_VERSION@
+#cmakedefine CUDNN_PATCH_VERSION @CUDNN_PATCH_VERSION@
+#cmakedefine CUDNN_VERSION @CUDNN_VERSION@
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/cmake/Thirdparty/FindCUDNN.cmake
----------------------------------------------------------------------
diff --git a/cmake/Thirdparty/FindCUDNN.cmake b/cmake/Thirdparty/FindCUDNN.cmake
index 451b79b..d48fc0e 100644
--- a/cmake/Thirdparty/FindCUDNN.cmake
+++ b/cmake/Thirdparty/FindCUDNN.cmake
@@ -6,15 +6,15 @@
# 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.
-#
+#
FIND_PATH(CUDNN_INCLUDE_DIR NAME "cudnn.h" PATHS "$ENV{CMAKE_INCLUDE_PATH}")
@@ -28,24 +28,24 @@ find_package_handle_standard_args(CUDNN DEFAULT_MSG CUDNN_INCLUDE_DIR CUDNN_LIBR
IF(CUDNN_FOUND)
FILE(READ ${CUDNN_INCLUDE_DIR}/cudnn.h CUDNN_VERSION_FILE_CONTENTS)
STRING(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
- CUDNN_VERSION_MAJOR "${CUDNN_VERSION_FILE_CONTENTS}")
+ CUDNN_MAJOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
STRING(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1"
- CUDNN_VERSION_MAJOR "${CUDNN_VERSION_MAJOR}")
+ CUDNN_MAJOR_VERSION "${CUDNN_MAJOR_VERSION}")
STRING(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)"
- CUDNN_VERSION_MINOR "${CUDNN_VERSION_FILE_CONTENTS}")
+ CUDNN_MINOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
STRING(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1"
- CUDNN_VERSION_MINOR "${CUDNN_VERSION_MINOR}")
+ CUDNN_MINOR_VERSION "${CUDNN_MINOR_VERSION}")
STRING(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)"
- CUDNN_VERSION_PATCH "${CUDNN_VERSION_FILE_CONTENTS}")
+ CUDNN_PATCH_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
STRING(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
- CUDNN_VERSION_PATCH "${CUDNN_VERSION_PATCH}")
+ CUDNN_PATCH_VERSION "${CUDNN_PATCH_VERSION}")
IF(NOT CUDNN_VERSION_MAJOR)
SET(CUDNN_VERSION "???")
ELSE()
- MATH(EXPR CUDNN_VERSION_SWIG "${CUDNN_VERSION_MAJOR} * 1000 + ${CUDNN_VERSION_MINOR} * 100 + ${CUDNN_VERSION_PATCH}")
+ MATH(EXPR CUDNN_VERSION "${CUDNN_MAJOR_VERSION} * 1000 + ${CUDNN_MINOR_VERSION} * 100 + ${CUDNN_PATCH_VERSION}")
ENDIF()
- MESSAGE(STATUS "Found Cudnn_v${CUDNN_VERSION_SWIG} at ${CUDNN_INCLUDE_DIR} ${CUDNN_LIBRARIES}")
+ MESSAGE(STATUS "Found Cudnn_${CUDNN_VERSION} at ${CUDNN_INCLUDE_DIR} ${CUDNN_LIBRARIES}")
MARK_AS_ADVANCED(CUDNN_INCLUDE_DIR CUDNN_LIBRARIES)
ENDIF()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/include/singa/io/snapshot.h
----------------------------------------------------------------------
diff --git a/include/singa/io/snapshot.h b/include/singa/io/snapshot.h
index 0d5aa66..0d0df1a 100644
--- a/include/singa/io/snapshot.h
+++ b/include/singa/io/snapshot.h
@@ -64,8 +64,14 @@ class Snapshot {
/// binary file is for serialized tensors, the other csv file is for parameter
/// names and shapes.
void Write(const std::string& key, const Tensor& param);
+ /// available for singa > 1.0.1
+ int version() const {
+ return version_;
+ }
private:
+ /// version of SINGA which generates the snapshot
+ int version_ = 0;
std::string prefix_;
Mode mode_;
std::unique_ptr<io::BinFileWriter> bin_writer_ptr_;
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/src/api/config.i.in
----------------------------------------------------------------------
diff --git a/src/api/config.i.in b/src/api/config.i.in
index 7da916c..cea3517 100644
--- a/src/api/config.i.in
+++ b/src/api/config.i.in
@@ -3,4 +3,10 @@
#cmakedefine01 USE_CUDNN
#cmakedefine01 USE_PYTHON
#cmakedefine01 USE_JAVA
-#cmakedefine CUDNN_VERSION_SWIG ${CUDNN_VERSION_SWIG}
+#cmakedefine CUDNN_VERSION ${CUDNN_VERSION}
+
+// SINGA version
+#cmakedefine SINGA_MAJOR_VERSION ${SINGA_MAJOR_VERSION}
+#cmakedefine SINGA_MINOR_VERSION ${SINGA_MINOR_VERSION}
+#cmakedefine SINGA_PATCH_VERSION ${SINGA_PATCH_VERSION}
+#cmakedefine SINGA_VERSION ${SINGA_VERSION}
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/src/api/model_layer.i
----------------------------------------------------------------------
diff --git a/src/api/model_layer.i b/src/api/model_layer.i
index 7f582e7..e5df285 100644
--- a/src/api/model_layer.i
+++ b/src/api/model_layer.i
@@ -92,7 +92,7 @@ class RNN : public Layer {
};
#if USE_CUDA && USE_CUDNN
-#if CUDNN_VERSION_SWIG >= 5005
+#if CUDNN_VERSION >= 5005
class CudnnRNN : public RNN {
public:
// note: Must use std::vector instead of vector.
@@ -105,7 +105,7 @@ class CudnnRNN : public RNN {
const std::vector<size_t> GetOutputSampleShape() const override;
};
-#endif // CUDNN_VERSION_SWIG >= 5005
+#endif // CUDNN_VERSION >= 5005
#endif // USE_CUDA && USE_CUDNN
}
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/src/io/binfile_reader.cc
----------------------------------------------------------------------
diff --git a/src/io/binfile_reader.cc b/src/io/binfile_reader.cc
index a6081e3..3ed87e4 100644
--- a/src/io/binfile_reader.cc
+++ b/src/io/binfile_reader.cc
@@ -97,7 +97,8 @@ void BinFileReader::SeekToFirst() {
bool BinFileReader::OpenFile() {
buf_ = new char[capacity_];
fdat_.open(path_, std::ios::in | std::ios::binary);
- CHECK(fdat_.is_open()) << "Cannot open file " << path_;
+ if (!fdat_.is_open())
+ LOG(WARNING) << "Cannot open file " << path_;
return fdat_.is_open();
}
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/src/io/snapshot.cc
----------------------------------------------------------------------
diff --git a/src/io/snapshot.cc b/src/io/snapshot.cc
index 58c7044..d24513e 100644
--- a/src/io/snapshot.cc
+++ b/src/io/snapshot.cc
@@ -19,6 +19,7 @@
*
*************************************************************/
+#include "singa/singa_config.h"
#include "singa/io/snapshot.h"
#include <string>
@@ -36,16 +37,37 @@ Snapshot::Snapshot(const std::string& prefix, Mode mode, int max_param_size /*in
text_writer_ptr_(mode_ == kWrite ? (new io::TextFileWriter) : nullptr),
bin_reader_ptr_(mode_ == kRead ? (new io::BinFileReader) : nullptr) {
if (mode_ == kWrite) {
- bin_writer_ptr_->Open(prefix + ".model", io::kCreate, max_param_size << 20);
+ // changed to .bin since v1.0.1
+ bin_writer_ptr_->Open(prefix + ".bin", io::kCreate, max_param_size << 20);
text_writer_ptr_->Open(prefix + ".desc", io::kCreate);
+
+ // write the current version ids
+ text_writer_ptr_->Write("SINGA_VERSION", std::to_string(SINGA_VERSION));
} else if (mode == kRead) {
- bin_reader_ptr_->Open(prefix + ".model", max_param_size << 20);
- std::string key, serialized_str;
+ auto text_reader_ptr = new io::TextFileReader();
+ text_reader_ptr->Open(prefix + ".desc");
+ std::string key, val;
+ while (text_reader_ptr->Read(&key, &val)) {
+ if (key == "SINGA_VERSION")
+ version_ = std::stoi(val);
+ }
+ delete text_reader_ptr;
+
+ if (!bin_reader_ptr_->Open(prefix + ".bin", max_param_size << 20))
+ CHECK(bin_reader_ptr_->Open(prefix + ".model", max_param_size << 20))
+ << "Cannot open the checkpoint bin file:" << prefix + ".bin (>=1.0.1) "
+ <<" or " << prefix + " .model (used by 1.0.0)";
singa::TensorProto tp;
- while (bin_reader_ptr_->Read(&key, &serialized_str)) {
+ while (bin_reader_ptr_->Read(&key, &val)) {
+ if (key == "SINGA_VERSION") {
+ CHECK(version_ == std::stoi(val)) << key << " in .bin and .desc mismatch: "
+ << val << " (bin) vs " << version_ << " (desc)";
+ continue;
+ }
+
CHECK(param_names_.count(key) == 0);
param_names_.insert(key);
- CHECK(tp.ParseFromString(serialized_str));
+ CHECK(tp.ParseFromString(val));
param_map_[key].FromProto(tp);
}
} else {
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/src/io/textfile_reader.cc
----------------------------------------------------------------------
diff --git a/src/io/textfile_reader.cc b/src/io/textfile_reader.cc
index 16abc9e..02c6903 100644
--- a/src/io/textfile_reader.cc
+++ b/src/io/textfile_reader.cc
@@ -24,7 +24,8 @@ namespace io {
bool TextFileReader::Open(const std::string& path) {
path_ = path;
fdat_.open(path_, std::ios::in);
- CHECK(fdat_.is_open()) << "Cannot open file " << path_;
+ if (!fdat_.is_open())
+ LOG(WARNING) << "Cannot open file " << path_;
return fdat_.is_open();
}
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0bf1475e/test/singa/test_snapshot.cc
----------------------------------------------------------------------
diff --git a/test/singa/test_snapshot.cc b/test/singa/test_snapshot.cc
index a9e8ee6..6fb5f03 100644
--- a/test/singa/test_snapshot.cc
+++ b/test/singa/test_snapshot.cc
@@ -68,6 +68,7 @@ TEST(Snapshot, ReadTest) {
std::ifstream desc_file(prefix + ".desc");
std::string line;
getline(desc_file, line);
+ getline(desc_file, line);
EXPECT_EQ(line, desc_1);
getline(desc_file, line);
EXPECT_EQ(line, desc_2);
[2/2] incubator-singa git commit: SINGA-261 Add version ID into the
checkpoint files
Posted by ka...@apache.org.
SINGA-261 Add version ID into the checkpoint files
Add singa version and cudnn version for PySINGA, singa.__version, singa.layer.cudnn_version
Fixed a bug in using PySINGA rnn (caused by swig rename of forward and backward)
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/e43ea694
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/e43ea694
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/e43ea694
Branch: refs/heads/master
Commit: e43ea694521d64bac276d9e3e634edd5d6a04be9
Parents: 0bf1475
Author: Wei Wang <wa...@gmail.com>
Authored: Wed Nov 23 14:42:04 2016 +0800
Committer: Wei Wang <wa...@gmail.com>
Committed: Wed Nov 23 14:42:04 2016 +0800
----------------------------------------------------------------------
cmake/Templates/singa_config.h.in | 2 +-
cmake/Thirdparty/FindCUDNN.cmake | 2 +-
python/singa/__init__.py | 8 +++++---
python/singa/layer.py | 21 +++++++++++++++++++--
src/api/model_layer.i | 17 +++++++++++------
5 files changed, 37 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e43ea694/cmake/Templates/singa_config.h.in
----------------------------------------------------------------------
diff --git a/cmake/Templates/singa_config.h.in b/cmake/Templates/singa_config.h.in
index baa625c..181c9fd 100644
--- a/cmake/Templates/singa_config.h.in
+++ b/cmake/Templates/singa_config.h.in
@@ -48,4 +48,4 @@
#cmakedefine CUDNN_MAJOR_VERSION @CUDNN_MAJOR_VERSION@
#cmakedefine CUDNN_MINOR_VERSION @CUDNN_MINOR_VERSION@
#cmakedefine CUDNN_PATCH_VERSION @CUDNN_PATCH_VERSION@
-#cmakedefine CUDNN_VERSION @CUDNN_VERSION@
+// #cmakedefine CUDNN_VERSION @CUDNN_VERSION@
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e43ea694/cmake/Thirdparty/FindCUDNN.cmake
----------------------------------------------------------------------
diff --git a/cmake/Thirdparty/FindCUDNN.cmake b/cmake/Thirdparty/FindCUDNN.cmake
index d48fc0e..c287ace 100644
--- a/cmake/Thirdparty/FindCUDNN.cmake
+++ b/cmake/Thirdparty/FindCUDNN.cmake
@@ -40,7 +40,7 @@ IF(CUDNN_FOUND)
STRING(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
CUDNN_PATCH_VERSION "${CUDNN_PATCH_VERSION}")
- IF(NOT CUDNN_VERSION_MAJOR)
+ IF(NOT CUDNN_MAJOR_VERSION)
SET(CUDNN_VERSION "???")
ELSE()
MATH(EXPR CUDNN_VERSION "${CUDNN_MAJOR_VERSION} * 1000 + ${CUDNN_MINOR_VERSION} * 100 + ${CUDNN_PATCH_VERSION}")
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e43ea694/python/singa/__init__.py
----------------------------------------------------------------------
diff --git a/python/singa/__init__.py b/python/singa/__init__.py
index c81c6ef..3f24896 100644
--- a/python/singa/__init__.py
+++ b/python/singa/__init__.py
@@ -6,14 +6,16 @@
# 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.
-#
+#
+from . import singa_wrap
+__version__ = singa_wrap.SINGA_VERSION
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e43ea694/python/singa/layer.py
----------------------------------------------------------------------
diff --git a/python/singa/layer.py b/python/singa/layer.py
index f0024c4..0244454 100644
--- a/python/singa/layer.py
+++ b/python/singa/layer.py
@@ -63,6 +63,11 @@ engine is case insensitive. Each python layer would create the correct specific
layer using the engine attribute.
'''
+if singa_wrap.USE_CUDNN:
+ cudnn_version = singa_wrap.CUDNN_VERSION
+else:
+ cudnn_version = 0
+
class Layer(object):
'''Base Python layer class.
@@ -219,6 +224,12 @@ class Layer(object):
<dx, <dp1, dp2..>>, dx is a (set of) tensor(s) for the gradient of x
, dpi is the gradient of the i-th parameter
'''
+ if type(flag) is bool:
+ if flag:
+ flag = model_pb2.kTrain
+ else:
+ flag = model_pb2.kEval
+
if type(dy) == list:
dys = [t.singa_tensor for t in dy]
ret = self.layer.BackwardWithMultInputs(flag, dys)
@@ -918,7 +929,7 @@ class RNN(Layer):
flag = model_pb2.kTrain
else:
flag = model_pb2.kEval
- y = self.layer.Forward(flag, tensors)
+ y = self.layer.ForwardWithMultInputs(flag, tensors)
return tensor.from_raw_tensors(y)
def backward(self, flag, grad):
@@ -942,11 +953,17 @@ class RNN(Layer):
hidden state. dcx is the gradient for the initial cell state,
which is valid only for lstm.
'''
+ if type(flag) is bool:
+ if flag:
+ flag = model_pb2.kTrain
+ else:
+ flag = model_pb2.kEval
+
tensors = []
for t in grad:
assert isinstance(t, tensor.Tensor), 'grad must be py Tensor'
tensors.append(t.singa_tensor)
- ret = self.layer.Backward(flag, tensors)
+ ret = self.layer.BackwardWithMultInputs(flag, tensors)
return tensor.from_raw_tensors(ret[0]), tensor.from_raw_tensors(ret[1])
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e43ea694/src/api/model_layer.i
----------------------------------------------------------------------
diff --git a/src/api/model_layer.i b/src/api/model_layer.i
index e5df285..92919fd 100644
--- a/src/api/model_layer.i
+++ b/src/api/model_layer.i
@@ -74,15 +74,17 @@ class Layer {
virtual const std::vector<size_t> GetOutputSampleShape(int k);
virtual void ToDevice(std::shared_ptr<Device> device);
virtual void AsType(DataType dtype);
+
virtual const Tensor Forward(int flag, const Tensor& input);
%rename(ForwardWithMultInputs) Forward(int flag, const std::vector<Tensor>&);
virtual const std::vector<Tensor> Forward(
int flag, const std::vector<Tensor>& inputs);
+
virtual const std::pair<Tensor, std::vector<Tensor>> Backward(
int flag, const Tensor& grad);
- %rename(BackwardWithMultInputs) Backward(int, const vector<Tensor>&);
+ %rename(BackwardWithMultInputs) Backward(int, const std::vector<Tensor>&);
virtual const std::pair<std::vector<Tensor>, std::vector<Tensor>>
- Backward(int flag, const vector<Tensor>& grads);
+ Backward(int flag, const std::vector<Tensor>& grads);
};
std::shared_ptr<Layer> CreateLayer(const std::string& type);
@@ -95,11 +97,14 @@ class RNN : public Layer {
#if CUDNN_VERSION >= 5005
class CudnnRNN : public RNN {
public:
- // note: Must use std::vector instead of vector.
- const std::vector<Tensor> Forward(int flag,
- const std::vector<Tensor>& inputs) override;
+ // note: Must use std::vector instead of vector.
+ %rename(ForwardWithMultInputs) Forward(int flag, const std::vector<Tensor>&);
+ const std::vector<Tensor> Forward(
+ int flag, const std::vector<Tensor>& inputs);
+ %rename(BackwardWithMultInputs) Backward(int, const std::vector<Tensor>&);
const std::pair<std::vector<Tensor>, std::vector<Tensor>>
- Backward(int flag, const std::vector<Tensor>& grads) override;
+ Backward(int flag, const std::vector<Tensor>& grads);
+
void ToDevice(std::shared_ptr<Device> device) override;
const std::vector<Tensor> param_values() override;
const std::vector<size_t> GetOutputSampleShape() const override;