Skip to content

Commit

Permalink
Merge pull request #53 from JDAI-CV/export
Browse files Browse the repository at this point in the history
more proper public headers, dnn:: namespace, and cmake install
  • Loading branch information
daquexian authored May 24, 2019
2 parents 51b19b2 + 7f794cf commit 1014a45
Show file tree
Hide file tree
Showing 52 changed files with 326 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .daq_pm/configs/all-27
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
name DNNLibrary
type cpp
build_dir build-all-v27
cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-27 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -GNinja
cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-27 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -GNinja -DDNN_READ_ONNX=ON -DDNN_CUSTOM_PROTOC_EXECUTABLE=/usr/bin/protoc
2 changes: 1 addition & 1 deletion .daq_pm/configs/infer
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name DNNLibrary
type cpp
target dnn_retrieve_result
build_dir build
build_dir build_infer
cmake_options -DCMAKE_SYSTEM_NAME=Android -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_CPP_FEATURES=exceptions -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
program_arguments ~/adb_push_and_run.sh binaries/dnn_retrieve_result
2 changes: 1 addition & 1 deletion .daq_pm/configs/onnx_infer
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name DNNLibrary
type cpp
target dnn_retrieve_result
build_dir build_onnxinfer
cmake_options -DCMAKE_SYSTEM_NAME=Android -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_CPP_FEATURES=exceptions -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDNN_READ_ONNX=ON -DONNX_CUSTOM_PROTOC_EXECUTABLE=/usr/bin/protoc
cmake_options -DCMAKE_SYSTEM_NAME=Android -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_CPP_FEATURES=exceptions -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDNN_READ_ONNX=ON -DDNN_CUSTOM_PROTOC_EXECUTABLE=/usr/bin/protoc
program_arguments ~/adb_push_and_run.sh binaries/dnn_retrieve_result
12 changes: 12 additions & 0 deletions binaries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ if (DNN_BUILD_BIN)
dnn_retrieve_result.cpp)
target_link_libraries(dnn_retrieve_result
dnnlibrary)
target_include_directories(dnn_retrieve_result
PRIVATE
${PROJECT_SOURCE_DIR}
)

if ((NOT DNN_READ_ONNX) OR DNN_SYSTEM_PROTOBUF)
treat_warnings_as_errors(dnn_retrieve_result)
Expand All @@ -12,6 +16,10 @@ if (DNN_BUILD_BIN)
dnn_benchmark.cpp)
target_link_libraries(dnn_benchmark
dnnlibrary)
target_include_directories(dnn_benchmark
PRIVATE
${PROJECT_SOURCE_DIR}
)

if ((NOT DNN_READ_ONNX) OR DNN_SYSTEM_PROTOBUF)
treat_warnings_as_errors(dnn_benchmark)
Expand All @@ -21,6 +29,10 @@ if (DNN_BUILD_BIN)
ex_model_builder.cpp)
target_link_libraries(ex_model_builder
dnnlibrary)
target_include_directories(ex_model_builder
PRIVATE
${PROJECT_SOURCE_DIR}
)

if ((NOT DNN_READ_ONNX) OR DNN_SYSTEM_PROTOBUF)
treat_warnings_as_errors(ex_model_builder)
Expand Down
12 changes: 7 additions & 5 deletions binaries/dnn_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
#include <string>
#include <vector>

#include <DaqReader.h>
#include <common/helper.h>
#include <dnnlibrary/DaqReader.h>
#include <dnnlibrary/ModelBuilder.h>
#include <glog/logging.h>
#include "ModelBuilder.h"
#include "android_log_helper.h"

using std::cout;
using std::endl;
using std::string;
using Clock = std::chrono::high_resolution_clock;
using dnn::DaqReader;
using dnn::Model;
using dnn::ModelBuilder;

auto GetModel(css &daq_name, const bool allow_fp16,
const PreferenceCode &compile_preference) {
Expand All @@ -32,7 +34,7 @@ auto GetModel(css &daq_name, const bool allow_fp16,
#if __ANDROID_API__ >= __ANDROID_API_P__
model = builder.AllowFp16(allow_fp16).Compile(compile_preference);
#else
(void) allow_fp16;
(void)allow_fp16;
model = builder.Compile(compile_preference);
#endif
return model;
Expand Down Expand Up @@ -81,7 +83,7 @@ int main(int argc, char **argv) {
} \
}

#define BENCHMARK(fp16_candidates, preference_candidates) \
#define BENCHMARK(fp16_candidates, preference_candidates) \
for (const auto allow_fp16 : fp16_candidates) { \
for (const auto compile_preference : preference_candidates) { \
auto model = GetModel(daq_name, allow_fp16, compile_preference); \
Expand Down
15 changes: 9 additions & 6 deletions binaries/dnn_retrieve_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
#include <string>
#include <vector>

#include <DaqReader.h>
#include "argh.h"
#include <common/helper.h>
#include <dnnlibrary/DaqReader.h>
#include <dnnlibrary/ModelBuilder.h>
#ifdef DNN_READ_ONNX
#include <OnnxReader.h>
#include <dnnlibrary/OnnxReader.h>
#endif
#include <common/helper.h>
#include <glog/logging.h>
#include "ModelBuilder.h"
#include "android_log_helper.h"
#include "argh.h"

using std::cout;
using std::endl;
using std::string;
using Clock = std::chrono::high_resolution_clock;
using dnn::DaqReader;
using dnn::Model;
using dnn::ModelBuilder;
using dnn::OnnxReader;

bool hasEnding(std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
Expand Down
7 changes: 4 additions & 3 deletions binaries/ex_model_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include <vector>

#include <common/helper.h>
#include <dnnlibrary/ModelBuilder.h>
#include <glog/logging.h>
#include "ModelBuilder.h"
#include "android_log_helper.h"

using namespace android::nn::wrapper;
using dnn::ModelBuilder;

int main() {
using namespace android::nn::wrapper;
ModelBuilder builder;
builder.Prepare();
const bool quant8 = true;
Expand Down
18 changes: 18 additions & 0 deletions cmake/DNNLibraryConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
get_filename_component(DNNLibrary_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

if (@DNN_READ_ONNX@)
if (NOT TARGET protobuf::libprotobuf)
find_package(Protobuf REQUIRED)
endif()
if (NOT TARGET onnx)
find_package(ONNX REQUIRED)
endif()
endif()

if (NOT TARGET glog::glog)
find_package(glog REQUIRED)
endif()

if (NOT TARGET dnnlibrary::dnnlibrary)
include("${DNNLibrary_CMAKE_DIR}/DNNLibraryTargets.cmake")
endif()
17 changes: 17 additions & 0 deletions cmake/ONNX2daqConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
get_filename_component(ONNX2daq_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

if (NOT TARGET protobuf::libprotobuf)
find_package(Protobuf REQUIRED)
endif()

if (NOT TARGET onnx)
find_package(ONNX REQUIRED)
endif()

if (NOT TARGET glog::glog)
find_package(glog REQUIRED)
endif()

if (NOT TARGET dnnlibrary::onnx2daq)
include("${ONNX2daq_CMAKE_DIR}/ONNX2daqTargets.cmake")
endif()
2 changes: 1 addition & 1 deletion cmake/onnx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro(configure_onnx)
endif()
set(ONNX_BUILD_MAIN_LIB ON)
set(ONNX_NAMESPACE ${DAQ_ONNX_NAMESPACE} CACHE STRING "onnx namespace")
if (NOT EXISTS ${DNN_CUSTOM_PROTOC_EXECUTABLE})
if (${CMAKE_SYSTEM_NAME} STREQUAL "Android" AND NOT EXISTS ${DNN_CUSTOM_PROTOC_EXECUTABLE})
message(FATAL ERROR "DNN_CUSTOM_PROTOC_EXECUTABLE is not set or wrong.")
endif()
set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${DNN_CUSTOM_PROTOC_EXECUTABLE})
Expand Down
2 changes: 1 addition & 1 deletion cmake/protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro(configure_protobuf)
option(protobuf_BUILD_PROTOC_BINARIES "" ON)
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/protobuf/cmake)
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Android")
if (protobuf_BUILD_PROTOC_BINARIES)
add_executable(protobuf::protoc ALIAS protoc)
endif()
add_library(protobuf::libprotobuf ALIAS libprotobuf)
Expand Down
14 changes: 13 additions & 1 deletion common/Shaper.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "Shaper.h"
#include <common/Shaper.h>

#include <common/helper.h>
#include <glog/logging.h>

using std::string;
using std::vector;

Shaper::len_t Shaper::total(const Shape &shape) {
return Product(shape);
}

/**
* strides: [stride_y, stride_x]
* paddings: [top, left, bottom, right]
Expand Down Expand Up @@ -295,3 +299,11 @@ size_t Shaper::GetSize(const std::string &name) {
void Shaper::Clear() {
shape_map_.clear();
}

std::ostream &operator<<(std::ostream &os, const Shaper &shaper) {
for (const auto &p : shaper.shape_map_) {
os << (p.first + ": ") << p.second << std::endl;
}
return os;
}

51 changes: 38 additions & 13 deletions dnnlibrary/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# dnn_protobuf_generate_cpp(ONNX_SRCS ONNX_HDRS onnx.proto3)

set(dnnlibrary_src
include/ModelBuilder.h
include/Model.h
include/DaqReader.h
include/android_log_helper.h
include/operand_helper.h
include/flatbuffers_helper.h
${PROJECT_SOURCE_DIR}/include/dnnlibrary/ModelBuilder.h
${PROJECT_SOURCE_DIR}/include/dnnlibrary/Model.h
${PROJECT_SOURCE_DIR}/include/dnnlibrary/DaqReader.h
${PROJECT_SOURCE_DIR}/include/dnnlibrary/NeuralNetworksWrapper.h
src/android_log_helper.h
src/operand_helper.h
src/flatbuffers_helper.h
src/ModelBuilder.cpp
src/Model.cpp
src/DaqReader.cpp
${PROJECT_SOURCE_DIR}/common/Shaper.h
src/NeuralNetworksWrapper.cpp
${PROJECT_SOURCE_DIR}/include/common/Shaper.h
${PROJECT_SOURCE_DIR}/common/Shaper.cpp
${PROJECT_SOURCE_DIR}/common/StrKeyMap.h
${PROJECT_SOURCE_DIR}/include/common/StrKeyMap.h
)
if (DNN_READ_ONNX)
list(APPEND dnnlibrary_src
src/OnnxReader.cpp
src/OnnxReader.h)
${PROJECT_SOURCE_DIR}/include/dnnlibrary/OnnxReader.h)
endif()

add_library(dnnlibrary
Expand All @@ -28,10 +30,10 @@ target_include_directories(
dnnlibrary
PUBLIC
$<INSTALL_INTERFACE:include>
${CMAKE_CURRENT_SOURCE_DIR}/src
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
PRIVATE
${PROJECT_SOURCE_DIR}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
${CMAKE_SYSROOT}/usr/include
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR})

find_library(
Expand Down Expand Up @@ -72,7 +74,15 @@ else()
treat_warnings_as_errors(dnnlibrary)
endif()


include(GNUInstallDirs)
install(TARGETS dnnlibrary
EXPORT DNNLibraryTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION include)
add_library(dnnlibrary::dnnlibrary ALIAS dnnlibrary)

if (DNN_BUILD_JNI)
add_library(
Expand All @@ -81,4 +91,19 @@ if (DNN_BUILD_JNI)
src/JavaWrapper.cpp)
target_link_libraries(daq-jni
dnnlibrary)
install(TARGETS daq-jni
EXPORT DNNLibraryTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
add_library(dnnlibrary::daq-jni ALIAS daq-jni)
endif()

install(EXPORT DNNLibraryTargets
NAMESPACE dnnlibrary::
DESTINATION share/cmake/DNNLibrary
)

configure_file("${PROJECT_SOURCE_DIR}/cmake/DNNLibraryConfig.cmake.in"
"${PROJECT_BINARY_DIR}/cmake/DNNLibraryConfig.cmake"
@ONLY)

install(FILES "${PROJECT_BINARY_DIR}/cmake/DNNLibraryConfig.cmake" DESTINATION share/cmake/DNNLibrary)
4 changes: 3 additions & 1 deletion dnnlibrary/src/DaqReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by daquexian on 8/13/18.
//

#include "DaqReader.h"
#include <dnnlibrary/DaqReader.h>

#include <fcntl.h>
#include <sys/mman.h>
Expand All @@ -14,6 +14,7 @@
#include <flatbuffers_helper.h>
#include <glog/logging.h>

namespace dnn {
void ReadDaqImpl(const uint8_t *buf, ModelBuilder &builder);

std::string layer_type_to_str(DNN::LayerType type) {
Expand Down Expand Up @@ -358,3 +359,4 @@ void ReadDaqImpl(const uint8_t *buf, ModelBuilder &builder) {
AddInputs(*model, builder);
AddLayers(*model, builder);
}
}
7 changes: 5 additions & 2 deletions dnnlibrary/src/JavaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#include <map>
#include <vector>

#include <DaqReader.h>
#include <android/asset_manager_jni.h>
#include "ModelBuilder.h"
#include <dnnlibrary/DaqReader.h>
#include <dnnlibrary/ModelBuilder.h>
#include "jni_handle.h"

using dnn::DaqReader;
using dnn::Model;
using dnn::ModelBuilder;
using std::map;
using std::string;

Expand Down
Loading

0 comments on commit 1014a45

Please sign in to comment.