Skip to content

Commit

Permalink
Add support for layer modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Gaiser committed Feb 28, 2017
1 parent 50a5cd0 commit 13d0435
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 38 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ project(Caffe C CXX)
# ---[ Caffe version
set(CAFFE_TARGET_VERSION "1.0.0-rc5" CACHE STRING "Caffe logical version")
set(CAFFE_TARGET_SOVERSION "1.0.0-rc5" CACHE STRING "Caffe soname version")
add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION})
add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION} -DCMAKE_BUILD)

# ---[ Enable C++11 support
set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -44,6 +44,9 @@ caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
caffe_option(USE_OPENMP "Link with OpenMP (when your BLAS wants OpenMP and you get linker errors)" OFF)
set(DEFAULT_LAYER_PATH "${CMAKE_INSTALL_PREFIX}/lib/caffe/layers" CACHE STRING "Default layer module search path")
set(LAYER_MODULE_PREFIX "${CMAKE_SHARED_MODULE_PREFIX}" CACHE STRING "Prefix used to form filenames from module names")
set(LAYER_MODULE_SUFFIX "${CMAKE_SHARED_MODULE_SUFFIX}" CACHE STRING "Suffix used to form filenames from module names")

# ---[ Dependencies
include(cmake/Dependencies.cmake)
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ ifeq ($(WITH_PYTHON_LAYER), 1)
LIBRARIES += $(PYTHON_LIBRARIES)
endif

# Add options for layer modules
LAYER_MODULE_PREFIX ?= "lib"
COMMON_FLAGS += -DLAYER_MODULE_PREFIX="\"$(LAYER_MODULE_PREFIX)\""
LAYER_MODULE_SUFFIX ?= ".so"
COMMON_FLAGS += -DLAYER_MODULE_SUFFIX="\"$(LAYER_MODULE_SUFFIX)\""
DEFAULT_LAYER_PATH ?= $(DISTRIBUTE_DIR)/lib/caffe/layers
COMMON_FLAGS += -DDEFAULT_LAYER_PATH="\"$(DEFAULT_LAYER_PATH)\""
LIBRARIES += dl

# BLAS configuration (default = ATLAS)
BLAS ?= atlas
ifeq ($(BLAS), mkl)
Expand Down
7 changes: 7 additions & 0 deletions Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ PYTHON_LIB := /usr/lib
# Uncomment to support layers written in Python (will link against Python libs)
# WITH_PYTHON_LAYER := 1

# Uncomment to override default for layer search path (default is $DISTRIBUTE_DIR/lib/caffe/layers)
# DEFAULT_LAYER_PATH := /usr/local/lib/caffe/layers

# Uncomment to override default prefix/suffix used for finding layer modules.
# LAYER_MODULE_PREFIX := "lib"
# LAYER_MODULE_SUFFIX := ".so"

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
Expand Down
3 changes: 3 additions & 0 deletions cmake/Cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ endfunction()
find_package(CUDA 5.5 QUIET)
find_cuda_helper_libs(curand) # cmake 2.8.7 compartibility which doesn't search for curand

# disable propagation of CUDA host flags (CUDA cannot compil
set(CUDA_PROPAGATE_HOST_FLAGS off)

if(NOT CUDA_FOUND)
return()
endif()
Expand Down
24 changes: 7 additions & 17 deletions cmake/Templates/caffe_config.h.in
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
/* Sources directory */
#define SOURCE_FOLDER "${PROJECT_SOURCE_DIR}"
#ifndef CAFFE_CONFIG_H_
#define CAFFE_CONFIG_H_

/* Binaries directory */
#define BINARY_FOLDER "${PROJECT_BINARY_DIR}"
/* Layer module options */
#define DEFAULT_LAYER_PATH "${DEFAULT_LAYER_PATH}"
#define LAYER_MODULE_PREFIX "${LAYER_MODULE_PREFIX}"
#define LAYER_MODULE_SUFFIX "${LAYER_MODULE_SUFFIX}"

/* Test device */
#define CUDA_TEST_DEVICE ${CUDA_TEST_DEVICE}

/* Temporary (TODO: remove) */
#if 1
#define CMAKE_SOURCE_DIR SOURCE_FOLDER "/src/"
#define EXAMPLES_SOURCE_DIR BINARY_FOLDER "/examples/"
#define CMAKE_EXT ".gen.cmake"
#else
#define CMAKE_SOURCE_DIR "src/"
#define EXAMPLES_SOURCE_DIR "examples/"
#define CMAKE_EXT ""
#endif
#endif // CAFFE_CONFIG_H_
4 changes: 2 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ file(GLOB_RECURSE examples_srcs "${PROJECT_SOURCE_DIR}/examples/*.cpp")
foreach(source_file ${examples_srcs})
# get file name
get_filename_component(name ${source_file} NAME_WE)

# get folder name
get_filename_component(path ${source_file} PATH)
get_filename_component(folder ${path} NAME_WE)

add_executable(${name} ${source_file})
target_link_libraries(${name} ${Caffe_LINK})
caffe_default_properties(${name})
Expand Down
9 changes: 9 additions & 0 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ class Caffe {
inline static bool multiprocess() { return Get().multiprocess_; }
inline static void set_multiprocess(bool val) { Get().multiprocess_ = val; }
inline static bool root_solver() { return Get().solver_rank_ == 0; }
// Module layer search path
inline void SetLayerPath(const vector<string> & search_path) {
layer_path_ = search_path;
}
inline void ClearLayerPath() { layer_path_.clear(); }
inline vector<string> LayerPath() { return layer_path_; }

protected:
#ifndef CPU_ONLY
Expand All @@ -181,6 +187,9 @@ class Caffe {
int solver_rank_;
bool multiprocess_;

// Layer module search path
vector<string> layer_path_;

private:
// The private constructor to avoid duplicate instantiation.
Caffe();
Expand Down
29 changes: 28 additions & 1 deletion include/caffe/layer_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ class LayerRegisterer {
}
};

#define EXPORT_LAYER_MODULE_DELETER(deleter) \
extern "C" void deleter##_float(Layer<float> * layer) { \
return deleter<float>(layer); \
} \
extern "C" void deleter##_double(Layer<double> * layer) { \
return deleter<double>(layer); \
} \

#define EXPORT_LAYER_MODULE_CREATOR(type, creator) \
extern "C" Layer<float> * creator##_float( \
const LayerParameter& param \
) { \
return creator<float>(param); \
} \
extern "C" Layer<double> * creator##_double( \
const LayerParameter& param \
) { \
return creator<double>(param); \
} \

#define EXPORT_LAYER_MODULE_CLASS(type) \
template <typename Dtype> \
Layer<Dtype> * CreateModule_##type##Layer(const LayerParameter& param) \
{ \
return new type##Layer<Dtype>(param); \
} \
EXPORT_LAYER_MODULE_CREATOR(type, CreateModule_##type##Layer) \

#define REGISTER_LAYER_CREATOR(type, creator) \
static LayerRegisterer<float> g_creator_f_##type(#type, creator<float>); \
Expand All @@ -134,7 +161,7 @@ class LayerRegisterer {
{ \
return shared_ptr<Layer<Dtype> >(new type##Layer<Dtype>(param)); \
} \
REGISTER_LAYER_CREATOR(type, Creator_##type##Layer)
REGISTER_LAYER_CREATOR(type, Creator_##type##Layer) \

} // namespace caffe

Expand Down
12 changes: 4 additions & 8 deletions include/caffe/test/test_caffe_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@
using std::cout;
using std::endl;

#ifdef CMAKE_BUILD
#include "caffe_config.h"
#else
#define CUDA_TEST_DEVICE -1
#define CMAKE_SOURCE_DIR "src/"
#define EXAMPLES_SOURCE_DIR "examples/"
#define CMAKE_EXT ""
#endif
#define CUDA_TEST_DEVICE -1
#define CMAKE_SOURCE_DIR "src/"
#define EXAMPLES_SOURCE_DIR "examples/"
#define CMAKE_EXT ""

int main(int argc, char** argv);

Expand Down
18 changes: 18 additions & 0 deletions include/caffe/util/search_path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CAFFE_UTIL_SEARCH_PATH_H_
#define CAFFE_UTIL_SEARCH_PATH_H_

#include <string>
#include <vector>

namespace caffe {

/**
* @brief Parses a list of colon separated paths as a vector of paths.
*/
std::vector<std::string> ParseSearchPath(
std::string const & search_path
);

} // namespace caffe

#endif // CAFFE_UTIL_SEARCH_PATH_H_
3 changes: 3 additions & 0 deletions src/caffe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ target_include_directories(proto PUBLIC ${PROTOBUF_INCLUDE_DIR})

list(INSERT Caffe_LINKER_LIBS 0 PUBLIC proto) # note, crucial to prepend!

# Add dependency on dl to load runtime modules
list(APPEND Caffe_LINKER_LIBS ${CMAKE_DL_LIBS})

# --[ Caffe library

# creates 'test_srcs', 'srcs', 'test_cuda', 'cuda' lists
Expand Down
23 changes: 21 additions & 2 deletions src/caffe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
#include <cmath>
#include <cstdio>
#include <ctime>
#include <string>
#include <vector>

#include "caffe/common.hpp"
#include "caffe/util/rng.hpp"
#include "caffe/util/search_path.hpp"

#ifdef CMAKE_BUILD
// CMake defines it in caffe_config.h, Makefile passes it to compiler
#include "caffe_config.h"
#endif

namespace caffe {

Expand Down Expand Up @@ -49,11 +57,21 @@ void GlobalInit(int* pargc, char*** pargv) {
::google::InstallFailureSignalHandler();
}

vector<string> GetFallbackSearchPath() {
char const * env_layer_path = std::getenv("CAFFE_LAYER_PATH");
if (env_layer_path) {
return ParseSearchPath(env_layer_path);
} else {
return ParseSearchPath(DEFAULT_LAYER_PATH);
}
}

#ifdef CPU_ONLY // CPU-only Caffe.

Caffe::Caffe()
: random_generator_(), mode_(Caffe::CPU),
solver_count_(1), solver_rank_(0), multiprocess_(false) { }
solver_count_(1), solver_rank_(0), multiprocess_(false),
layer_path_(GetFallbackSearchPath()) {}

Caffe::~Caffe() { }

Expand Down Expand Up @@ -107,7 +125,8 @@ void* Caffe::RNG::generator() {
Caffe::Caffe()
: cublas_handle_(NULL), curand_generator_(NULL), random_generator_(),
mode_(Caffe::CPU),
solver_count_(1), solver_rank_(0), multiprocess_(false) {
solver_count_(1), solver_rank_(0), multiprocess_(false),
layer_path_(GetFallbackSearchPath()) {
// Try to create a cublas handler, and report an error if failed (but we will
// keep the program running as one might just want to run CPU code).
if (cublasCreate(&cublas_handle_) != CUBLAS_STATUS_SUCCESS) {
Expand Down
Loading

0 comments on commit 13d0435

Please sign in to comment.