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 <[email protected]> Authored: Tue Nov 15 21:24:14 2016 +0800 Committer: Wei Wang <[email protected]> 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);
