Skip to content

Commit

Permalink
Caffe2 module update: move observers as well as binaries. (pytorch#2145)
Browse files Browse the repository at this point in the history
* Caffe2 module update: move observers as well as binaries.

* Add threads linkage

* Add Threads dependency to public interface
  • Loading branch information
Yangqing authored Mar 6, 2018
1 parent 7d141d4 commit dd1564b
Show file tree
Hide file tree
Showing 45 changed files with 101 additions and 57 deletions.
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ option(BUILD_SHARED_LIBS "Build libcaffe2.so" ON)
cmake_dependent_option(
CAFFE2_USE_MSVC_STATIC_RUNTIME "Using MSVC static runtime libraries" ON
"NOT BUILD_SHARED_LIBS" OFF)
option(BUILD_OBSERVERS "Build performance observers/loggers in caffe2/share/observers directory" OFF)
option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" ON)
option(USE_ACL "Use ARM Compute Library" OFF)
option(USE_ASAN "Use Address Sanitizer" OFF)
Expand All @@ -54,7 +53,7 @@ option(USE_NERVANA_GPU "Use Nervana GPU backend" OFF)
option(USE_NNAPI "Use NNAPI" OFF)
option(USE_NNPACK "Use NNPACK" ON)
option(USE_NUMA "Use NUMA (only available on Linux)" ON)
option(USE_OBSERVERS "Use Observer Library" OFF)
option(USE_OBSERVERS "Use observers module." OFF)
option(USE_OPENCV "Use openCV" ON)
option(USE_OPENMP "Use OpenMP for parallel code" OFF)
option(USE_PROF "Use profiling" OFF)
Expand Down Expand Up @@ -254,6 +253,7 @@ if (BUILD_SHARED_LIBS)
${PROJECT_SOURCE_DIR}/cmake/public/glog.cmake
${PROJECT_SOURCE_DIR}/cmake/public/gflags.cmake
${PROJECT_SOURCE_DIR}/cmake/public/protobuf.cmake
${PROJECT_SOURCE_DIR}/cmake/public/threads.cmake
${PROJECT_SOURCE_DIR}/cmake/public/utils.cmake
DESTINATION share/cmake/Caffe2/public
COMPONENT dev)
Expand All @@ -269,5 +269,15 @@ endif()
# ---[ Modules
add_subdirectory(modules)

# ---[ Binaries
# Binaries will be built after the Caffe2 main libraries and the modules
# are built. For the binaries, they will be linked to the Caffe2 main
# libraries, as well as all the modules that are built with Caffe2 (the ones
# built in the previous Modules section above).

if (BUILD_BINARY)
add_subdirectory(binaries)
endif()

include(cmake/Summary.cmake)
caffe2_print_configuration_summary()
11 changes: 6 additions & 5 deletions caffe2/binaries/CMakeLists.txt → binaries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ caffe2_binary_target("run_plan.cc")
caffe2_binary_target("speed_benchmark.cc")
caffe2_binary_target("split_db.cc")

if (USE_THREADS)
caffe2_binary_target("db_throughput.cc")
target_link_libraries(db_throughput ${CMAKE_THREAD_LIBS_INIT})
endif()
caffe2_binary_target("db_throughput.cc")

if (USE_CUDA)
caffe2_binary_target("inspect_gpus.cc")
Expand Down Expand Up @@ -44,7 +41,11 @@ endif()

if (USE_OPENCV)
caffe2_binary_target("make_image_db.cc")
target_link_libraries(make_image_db ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(make_image_db ${OpenCV_LIBS})
endif()

if (USE_OBSERVERS)
caffe2_binary_target("caffe2_benchmark.cc")
endif()

# ---[ tutorials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/proto/caffe2.pb.h"
#include "caffe2/share/contrib/observers/observer_config.h"
#include "caffe2/utils/proto_utils.h"
#include "caffe2/utils/string_utils.h"

#include "observers/observer_config.h"

CAFFE2_DEFINE_string(
backend,
"builtin",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 4 additions & 11 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ install(FILES ${PROJECT_BINARY_DIR}/caffe2/core/macros.h

# Compile exposed libraries.
add_library(caffe2 ${Caffe2_CPU_SRCS} $<TARGET_OBJECTS:Caffe_PROTO> $<TARGET_OBJECTS:Caffe2_PROTO>)
add_dependencies(caffe2 Caffe_PROTO Caffe2_PROTO)
target_link_libraries(caffe2 PUBLIC ${Caffe2_PUBLIC_DEPENDENCY_LIBS})
target_link_libraries(caffe2 PRIVATE ${Caffe2_DEPENDENCY_LIBS})
target_include_directories(caffe2 INTERFACE $<INSTALL_INTERFACE:include>)
Expand Down Expand Up @@ -262,15 +263,7 @@ if (BUILD_PYTHON)
FILES_MATCHING PATTERN "*.py")
endif()

# Finally, set the Caffe2_MAIN_LIBS variable in the parent scope.
set(Caffe2_MAIN_LIBS ${Caffe2_MAIN_LIBS} PARENT_SCOPE)


# ---[ Any builds that should happen after the main targets should be added here.
# Binaries
if (BUILD_BINARY)
add_subdirectory(binaries)
# Benchmarking binaries require observers included in the build
# There is a linking issue that happens in some of the Windows builds.
# TODO(Yangqing): after the module redesign, enable this back.
if (BUILD_OBSERVERS AND NOT MSVC)
add_subdirectory(share/contrib/binaries)
endif()
endif()
9 changes: 0 additions & 9 deletions caffe2/share/contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
set(Caffe2_CPU_OBSERVER_SRCS)

if (USE_NNPACK)
add_subdirectory(nnpack)
endif()
if (BUILD_OBSERVERS)
add_subdirectory(observers)
endif()
if (USE_ZSTD)
add_subdirectory(zstd)
endif()

if (BUILD_OBSERVERS)
add_library(Caffe2_CPU_OBSERVER STATIC ${Caffe2_CPU_OBSERVER_SRCS})
endif()

# CPU source, test sources, binary sources
set(Caffe2_CPU_SRCS ${Caffe2_CPU_SRCS} PARENT_SCOPE)
set(Caffe2_CPU_TEST_SRCS ${Caffe2_CPU_TEST_SRCS} PARENT_SCOPE)
Expand Down
1 change: 0 additions & 1 deletion caffe2/share/contrib/binaries/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions caffe2/share/contrib/binaries/caffe2_benchmark/CMakeLists.txt

This file was deleted.

7 changes: 0 additions & 7 deletions caffe2/share/contrib/observers/CMakeLists.txt

This file was deleted.

7 changes: 6 additions & 1 deletion cmake/BuildVariables.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ set(Caffe2_CUDA_DEPENDENCY_LIBS "")
# Lists for Caffe2 public dependency libraries. These libraries will be
# transitive to any libraries that depends on Caffe2.
set(Caffe2_PUBLIC_DEPENDENCY_LIBS "")
set(Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS "")
set(Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS "")

# List of modules that is built as part of the main Caffe2 build. For all
# binary targets, such as Python and native binaries, they will be linked
# automatically with these modules.
set(Caffe2_MODULES "")
5 changes: 5 additions & 0 deletions cmake/Caffe2Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ set(CAFFE2_VERSION "@CAFFE2_VERSION@")
# Utils functions.
include("${CMAKE_CURRENT_LIST_DIR}/public/utils.cmake")

# Depending on whether Caffe2 is built with threads, include threads lib.
if(@USE_THREADS@)
include("${CMAKE_CURRENT_LIST_DIR}/public/threads.cmake")
endif()

# Depending on whether Caffe2 uses gflags during compile time or
# not, invoke gflags.
if (@USE_GFLAGS@)
Expand Down
8 changes: 2 additions & 6 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ include("cmake/ProtoBuf.cmake")

# ---[ Threads
if(USE_THREADS)
find_package(Threads REQUIRED)
list(APPEND Caffe2_DEPENDENCY_LIBS ${CMAKE_THREAD_LIBS_INIT})
include(cmake/public/threads.cmake)
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS Threads::Threads)
endif()

# ---[ protobuf
Expand Down Expand Up @@ -77,10 +77,6 @@ if(USE_NNPACK)
endif()
endif()

if(USE_OBSERVERS)
list(APPEND Caffe2_DEPENDENCY_LIBS Caffe2_CPU_OBSERVER)
endif()

# ---[ On Android, Caffe2 uses cpufeatures library in the thread pool
if (ANDROID)
if (NOT TARGET cpufeatures)
Expand Down
2 changes: 1 addition & 1 deletion cmake/public/glog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ else()
find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_ROOT_DIR})
endif()

find_library(GLOG_LIBRARY glog
PATHS ${GLOG_ROOT_DIR}
PATH_SUFFIXES lib lib64)
Expand Down
2 changes: 1 addition & 1 deletion cmake/public/protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ elseif(Protobuf_FOUND OR PROTOBUF_FOUND)
IMPORTED_LOCATION_DEBUG "${PROTOBUF_LITE_LIBRARY_DEBUG}")
endif()
endif()

if(PROTOBUF_PROTOC_EXECUTABLE)
if (NOT TARGET protobuf::protoc)
add_executable(protobuf::protoc IMPORTED)
Expand Down
18 changes: 18 additions & 0 deletions cmake/public/threads.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
find_package(Threads REQUIRED)
# For newer CMake, Threads::Threads is already defined. Otherwise, we will
# provide a backward compatible wrapper for Threads::Threads.
if(THREADS_FOUND AND NOT TARGET Threads::Threads)
add_library(Threads::Threads INTERFACE IMPORTED)

if(THREADS_HAVE_PTHREAD_ARG)
set_property(
TARGET Threads::Threads
PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
endif()

if(CMAKE_THREAD_LIBS_INIT)
set_property(
TARGET Threads::Threads
PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
endif()
endif()
4 changes: 4 additions & 0 deletions cmake/public/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,9 @@ function(caffe2_binary_target target_name_or_src)
endif()
add_executable(${__target} ${__srcs})
target_link_libraries(${__target} ${Caffe2_MAIN_LIBS})
# If we have Caffe2_MODULES defined, we will also link with the modules.
if (DEFINED Caffe2_MODULES)
target_link_libraries(${__target} ${Caffe2_MODULES})
endif()
install(TARGETS ${__target} DESTINATION bin)
endfunction()
4 changes: 4 additions & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# ---[ Add modules
add_subdirectory(detectron)
add_subdirectory(module_test)
add_subdirectory(observers)
add_subdirectory(rocksdb)

# Finally, set Caffe2_MODULES to parent scope.
set(Caffe2_MODULES ${Caffe2_MODULES} PARENT_SCOPE)
2 changes: 1 addition & 1 deletion modules/module_test/module_test_dynamic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// An explicitly defined module, testing correctness when we dynamically link a
// module
CAFFE2_MODULE(caffe2_module_test_dynamic, "Dynamic module for testing.");
CAFFE2_MODULE(caffe2_module_test_dynamic, "Dynamic module only used for testing.");

namespace caffe2 {

Expand Down
25 changes: 25 additions & 0 deletions modules/observers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
if (CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
if (NOT USE_OBSERVERS)
return()
endif()
else()
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(caffe2_observers CXX)
find_package(Caffe2 REQUIRED)
option(BUILD_SHARED_LIBS "Build shared libs." ON)
endif()

add_library(caffe2_observers
"${CMAKE_CURRENT_SOURCE_DIR}/net_observer_reporter_print.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/observer_config.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/perf_observer.cc"
)
target_link_libraries(caffe2_observers PUBLIC caffe2_library)
target_include_directories(caffe2_observers PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
install(TARGETS caffe2_observers DESTINATION lib)
caffe2_interface_library(caffe2_observers caffe2_observers_library)

if (CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
set(Caffe2_MODULES ${Caffe2_MODULES} caffe2_observers_library PARENT_SCOPE)
endif()

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "caffe2/share/contrib/observers/net_observer_reporter_print.h"
#include "observers/net_observer_reporter_print.h"

#include "caffe2/core/init.h"
#include "caffe2/share/contrib/observers/observer_config.h"
#include "observers/observer_config.h"

namespace caffe2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "caffe2/share/contrib/observers/net_observer_reporter.h"
#include "observers/net_observer_reporter.h"

namespace caffe2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "caffe2/share/contrib/observers/observer_config.h"
#include "observers/observer_config.h"

namespace caffe2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "caffe2/share/contrib/observers/net_observer_reporter.h"
#include "observers/net_observer_reporter.h"

namespace caffe2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "caffe2/share/contrib/observers/perf_observer.h"
#include "caffe2/share/contrib/observers/observer_config.h"
#include "observers/perf_observer.h"
#include "observers/observer_config.h"

#include <random>
#include "caffe2/core/common.h"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion modules/rocksdb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ install(TARGETS caffe2_rocksdb DESTINATION lib)
# Note(jiayq): this also depends on a separate cmake move to reorg test builds
# and binary builds after modules. When it is done, this note should be removed.
if (CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
list(APPEND Caffe2_MAIN_LIBS caffe2_rocksdb)
set(Caffe2_MODULES ${Caffe2_MODULES} caffe2_rocksdb PARENT_SCOPE)
endif()
2 changes: 2 additions & 0 deletions modules/rocksdb/rocksdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ REGISTER_CAFFE2_DB(RocksDB, RocksDB);
REGISTER_CAFFE2_DB(rocksdb, RocksDB);

} // namespace db

CAFFE2_MODULE(caffe2_rocksdb, "RocksDB implementation for caffe2::DB.");
} // namespace caffe2

0 comments on commit dd1564b

Please sign in to comment.