From 2505cfe0d67ebfe4e23ac14222c7b436f3572e01 Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 5 Sep 2018 19:38:24 +0900 Subject: [PATCH 01/53] remove random operation --- menoh/onnx.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/menoh/onnx.cpp b/menoh/onnx.cpp index 21d4d19..0290aec 100644 --- a/menoh/onnx.cpp +++ b/menoh/onnx.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -202,10 +201,6 @@ namespace menoh_impl { trim_dropout(node_list); trim_reshape(node_list); - std::random_device rd; - std::mt19937 g(rd()); - std::shuffle(node_list.begin(), node_list.end(), g); - std::vector all_parameter_name_list; all_parameter_name_list.reserve( onnx_model.graph().initializer().size()); From 946099f3a4a38fc4c0ebc032d72deb9396f13200 Mon Sep 17 00:00:00 2001 From: Gentaro Watanabe Date: Mon, 10 Sep 2018 16:10:07 +0900 Subject: [PATCH 02/53] Use RPATH instead of RUNPATH for test --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3d3a32..beb906b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.1) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,--disable-new-dtags") + set(MENOH_MAJOR_VERSION 1) set(MENOH_MINOR_VERSION 1) set(MENOH_PATCH_VERSION 0) From f1d348801fa92182ad2241acb791a1dc71610fce Mon Sep 17 00:00:00 2001 From: Gentaro Watanabe Date: Tue, 11 Sep 2018 13:09:23 +0900 Subject: [PATCH 03/53] Fix cmake files for environments other than Linux --- CMakeLists.txt | 2 -- test/CMakeLists.txt | 5 +++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index beb906b..a3d3a32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.1) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") -set(CMAKE_EXE_LINKER_FLAGS "-Wl,--disable-new-dtags") - set(MENOH_MAJOR_VERSION 1) set(MENOH_MINOR_VERSION 1) set(MENOH_PATCH_VERSION 0) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d27878b..4348424 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,5 +12,10 @@ add_executable(menoh_test ) target_link_libraries(menoh_test gtest_main menoh_test_target) +if(UNIX AND NOT APPLE) + set_property( + TARGET menoh_test APPEND_STRING PROPERTY + LINK_FLAGS "-Wl,--disable-new-dtags") +endif() add_test(NAME menoh_test COMMAND menoh_test) From 6570196e841c3dc577f604a92d762a415feec256 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 14 Sep 2018 17:49:53 +0900 Subject: [PATCH 04/53] link libprotobuf to menoh_test --- .gitmodules | 3 +++ CMakeLists.txt | 6 ------ test/CMakeLists.txt | 15 ++++++++++++++- test/lib/filesystem | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) create mode 160000 test/lib/filesystem diff --git a/.gitmodules b/.gitmodules index 2ee7f0e..17edb59 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "test/lib/googletest"] path = test/lib/googletest url = https://github.com/google/googletest.git +[submodule "test/lib/filesystem"] + path = test/lib/filesystem + url = https://github.com/wjakob/filesystem.git diff --git a/CMakeLists.txt b/CMakeLists.txt index b51806e..751fe2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,12 +67,6 @@ endif() include_directories("${MKLDNN_INCLUDE_DIR}") if(${ENABLE_TEST}) - enable_testing() - # GTest setup - set(GTEST_DIR test/lib/googletest) - execute_process(COMMAND git submodule update --init -- ${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) - message(STATUS "# add_subdirectory\(${GTEST_DIR}\)") - add_subdirectory(${GTEST_DIR}) message(STATUS "# add_subdirectory\(test\)") add_subdirectory(test) endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d920bce..8d302d8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,16 @@ +enable_testing() + +# GTest setup +set(GTEST_DIR "lib/googletest") +execute_process(COMMAND git submodule update --init -- test/${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) +message(STATUS "# add_subdirectory\(${GTEST_DIR}\)") +add_subdirectory(${GTEST_DIR}) + +# filesystem setup +set(FILESYSTEM_DIR "lib/filesystem") +execute_process(COMMAND git submodule update --init -- test/${FILESYSTEM_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) +include_directories("${FILESYSTEM_DIR}") + add_executable(menoh_test np_io.cpp array.cpp @@ -13,6 +26,6 @@ add_executable(menoh_test #vgg16.cpp ) -target_link_libraries(menoh_test gtest_main menoh_test_target) +target_link_libraries(menoh_test gtest_main menoh_test_target ${PROTOBUF_LIBRARY}) add_test(NAME menoh_test COMMAND menoh_test) diff --git a/test/lib/filesystem b/test/lib/filesystem new file mode 160000 index 0000000..0a539a6 --- /dev/null +++ b/test/lib/filesystem @@ -0,0 +1 @@ +Subproject commit 0a539a6c988dc8691af317e077893e831dee2908 From a9a06b9957353f1c0ff9780f363b87fc61784534 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 14 Sep 2018 17:51:26 +0900 Subject: [PATCH 05/53] add test code --- menoh/onnx.cpp | 10 +- test/common.hpp | 4 +- test/operator.cpp | 1229 ++++++++------------------------------------- 3 files changed, 225 insertions(+), 1018 deletions(-) diff --git a/menoh/onnx.cpp b/menoh/onnx.cpp index e1813e6..bc4ac2b 100644 --- a/menoh/onnx.cpp +++ b/menoh/onnx.cpp @@ -23,7 +23,7 @@ namespace menoh_impl { - auto tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt) { + dtype_t tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt) { if(tpdt == onnx::TensorProto_DataType_FLOAT) { return dtype_t::float_; } @@ -167,7 +167,6 @@ namespace menoh_impl { return node_list; } - model_data make_model_from_onnx(onnx::ModelProto& onnx_model) { // onnx opset version check if(onnx_model.opset_import_size() != 0) { @@ -229,14 +228,17 @@ namespace menoh_impl { return make_model_from_onnx(onnx_model); } - model_data make_model_data_from_onnx_data_on_memory(const uint8_t* onnx_data, int32_t size) { + model_data + make_model_data_from_onnx_data_on_memory(const uint8_t* onnx_data, + int32_t size) { namespace gpio = ::google::protobuf::io; gpio::ArrayInputStream ais(onnx_data, size); gpio::CodedInputStream cis(&ais); cis.SetTotalBytesLimit(std::numeric_limits::max(), std::numeric_limits::max()); onnx::ModelProto onnx_model; - if(!onnx_model.ParseFromCodedStream(&cis) || !cis.ConsumedEntireMessage()) { + if(!onnx_model.ParseFromCodedStream(&cis) || + !cis.ConsumedEntireMessage()) { throw onnx_parse_error("parse binary onnx data on memory"); } return make_model_from_onnx(onnx_model); diff --git a/test/common.hpp b/test/common.hpp index 31474a2..3fb29a8 100644 --- a/test/common.hpp +++ b/test/common.hpp @@ -6,11 +6,11 @@ namespace menoh_impl { template - auto assert_eq_list(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2) { + auto assert_eq_list(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, float eps=1.e-4) { ASSERT_EQ(std::distance(first1, last1), std::distance(first2, last2)) << "size is different"; while(first1 != last1) { - ASSERT_EQ(*first1, *first2) + ASSERT_NEAR(*first1, *first2, eps) << *first1 << " and " << *first2 << " are different"; ++first1; ++first2; diff --git a/test/operator.cpp b/test/operator.cpp index 1f610ae..729e664 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -1,5 +1,5 @@ +/* #include -#include #include #include #include @@ -15,1027 +15,232 @@ #include #include #include +*/ +#include -namespace menoh_impl { - namespace mkldnn_backend { - - auto load_np_array_as_memory(std::string const& filename, - mkldnn::engine const& engine) { - auto data = menoh_impl::load_np_array_as_array(filename); - assert(data.dims().size() == 2 || data.dims().size() == 4); - mkldnn::memory::format format = data.dims().size() == 2 - ? mkldnn::memory::format::nc - : mkldnn::memory::format::nchw; - mkldnn::memory memory( - {{{data.dims()}, - dtype_to_mkldnn_memory_data_type(data.dtype()), - format}, - engine}); - std::copy(fbegin(data), fend(data), - static_cast(memory.get_data_handle())); - return memory; - } - - class OperatorTest : public ::testing::Test { - protected: - OperatorTest() = default; - virtual void SetUp() {} - - auto relu_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{"", {"input"}, {"output"}, {}}; - auto factory_return = make_relu_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto - leaky_relu_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{ - "", {"input"}, {"output"}, {{"alpha", 0.001f}}}; - auto factory_return = make_leaky_relu_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto elu_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{ - "", {"input"}, {"output"}, {{"alpha", 1.1f}}}; - auto factory_return = make_elu_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto abs_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{"", {"input"}, {"output"}, {}}; - auto factory_return = make_abs_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto sqrt_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{"", {"input"}, {"output"}, {}}; - auto factory_return = make_sqrt_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto tanh_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto node = menoh_impl::node{"", {"input"}, {"output"}, {}}; - auto factory_return = make_tanh_primitive( - node, 0, {node}, {}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto softmax_test(std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_softmax_primitive( - menoh_impl::node{"", {"input"}, {"output"}, {}}, {}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto fc_test(std::string const& input_filename, - std::string const& weight_filename, - std::string const& bias_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = - menoh_impl::load_np_array_as_array(weight_filename); - auto bias = menoh_impl::load_np_array_as_array(bias_filename); - std::vector output_dims{extract_dims(input_memory).at(0), - weight.dims().at(0)}; - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_fc_primitive( - menoh_impl::node{"", - {"input", "weight", "bias"}, - {"output"}, - {{"axis", 1}, {"axis_w", 1}}}, - {{"weight", weight}, {"bias", bias}}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto max_pool_test(std::string const& input_filename, int k, int s, - int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims( - input_dims, input_dims.at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_max_pool_primitive( - menoh_impl::node{"", - {"input"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {}, {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/max_pooling_2d_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + ".txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto global_max_pool_test( - std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto input_dims = extract_dims(input_memory); - std::vector strides{1, 1}; - std::vector kernel_shape{input_dims.at(2), - input_dims.at(3)}; - std::vector pads{0, 0, 0, 0}; - auto output_dims = calc_2d_output_dims( - input_dims, input_dims.at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_global_max_pool_primitive( - menoh_impl::node{"", {"input"}, {"output"}, {}}, {}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto average_pool_test(std::string const& input_filename, int k, - int s, int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims( - input_dims, input_dims.at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_average_pool_primitive( - menoh_impl::node{"", - {"input"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {}, {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/average_pooling_2d_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + ".txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto global_average_pool_test( - std::string const& input_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto input_dims = extract_dims(input_memory); - std::vector strides{1, 1}; - std::vector kernel_shape{input_dims.at(2), - input_dims.at(3)}; - std::vector pads{0, 0, 0, 0}; - auto output_dims = calc_2d_output_dims( - input_dims, input_dims.at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_global_average_pool_primitive( - menoh_impl::node{"", {"input"}, {"output"}, {}}, {}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto conv_test(std::string const& input_filename, int k, int s, - int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = menoh_impl::load_np_array_as_array( - "../data/random_weight_5_4_" + std::to_string(k) + "_" + - std::to_string(k) + ".txt"); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims( - input_dims, weight.dims().at(0), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_conv_primitive( - menoh_impl::node{"", - {"input", "weight"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {{"weight", weight}}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/convolution_2d_w5_4_" + std::to_string(k) + "_" + - std::to_string(k) + "_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + ".txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto conv_with_bias_test(std::string const& input_filename, int k, - int s, int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = menoh_impl::load_np_array_as_array( - "../data/random_weight_5_4_" + std::to_string(k) + "_" + - std::to_string(k) + ".txt"); - auto bias = menoh_impl::load_np_array_as_array( - "../data/random_bias_5.txt"); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims( - input_dims, weight.dims().at(0), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_conv_primitive( - menoh_impl::node{"", - {"input", "weight", "bias"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {{"weight", weight}, {"bias", bias}}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/convolution_2d_w5_4_" + std::to_string(k) + "_" + - std::to_string(k) + "_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + - "_with_bias.txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto conv_transpose_test(std::string const& input_filename, int k, - int s, int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = menoh_impl::load_np_array_as_array( - "../data/random_weight_4_5_" + std::to_string(k) + "_" + - std::to_string(k) + ".txt"); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims_for_conv_transpose( - input_dims, weight.dims().at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_conv_transpose_primitive( - menoh_impl::node{"", - {"input", "weight"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {{"weight", weight}}, {{"input", input_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/deconvolution_2d_w4_5_" + std::to_string(k) + "_" + - std::to_string(k) + "_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + ".txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto - conv_transpose_with_bias_test(std::string const& input_filename, - int k, int s, int p) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = menoh_impl::load_np_array_as_array( - "../data/random_weight_4_5_" + std::to_string(k) + "_" + - std::to_string(k) + ".txt"); - auto bias = menoh_impl::load_np_array_as_array( - "../data/random_bias_4.txt"); - std::vector strides{{s, s}}; - std::vector kernel_shape{{k, k}}; - std::vector pads{{p, p, p, p}}; - auto input_dims = extract_dims(input_memory); - auto output_dims = calc_2d_output_dims_for_conv_transpose( - input_dims, weight.dims().at(1), kernel_shape, strides, pads); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_conv_transpose_primitive( - menoh_impl::node{"", - {"input", "weight", "bias"}, - {"output"}, - {{"strides", strides}, - {"kernel_shape", kernel_shape}, - {"pads", pads}}}, - {{"weight", weight}, {"bias", bias}}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - - auto true_output = menoh_impl::load_np_array_as_array( - "../data/deconvolution_2d_w4_5_" + std::to_string(k) + "_" + - std::to_string(k) + "_k" + std::to_string(k) + "_s" + - std::to_string(s) + "_p" + std::to_string(p) + - "_with_bias.txt"); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto - batch_norm_test(std::string const& input_filename, - std::string const& mean_filename, - std::string const& var_fileanme, - std::string const& gamma_filename, - std::string const& beta_filename, - std::string const& true_output_filename) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto mean = menoh_impl::load_np_array_as_array(mean_filename); - auto var = menoh_impl::load_np_array_as_array(var_fileanme); - auto gamma = menoh_impl::load_np_array_as_array(gamma_filename); - auto beta = menoh_impl::load_np_array_as_array(beta_filename); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_batch_norm_primitive( - menoh_impl::node{"", - {"input", "gamma", "beta", "mean", "var"}, - {"output"}, - {{"epsilon", 1e-5f}, {"is_test", 1}}}, - {{"gamma", gamma}, - {"beta", beta}, - {"mean", mean}, - {"var", var}}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); +#include +#include +#include - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); +#include +#include +#include +#include - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } +#include +#include +#include - auto add_test(std::string const& input_a_filename, - std::string const& input_b_filename, - std::string const& true_output_filename) const { - auto input_a_memory = - load_np_array_as_memory(input_a_filename, engine_); - auto input_b_memory = - load_np_array_as_memory(input_b_filename, engine_); - auto output_dims = extract_dims(input_a_memory); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_add_primitive( - menoh_impl::node{ - "", {"input_a", "input_b"}, {"output"}, {{"broadcast", 0}}}, - {}, - {{"input_a", input_a_memory}, {"input_b", input_b_memory}}, - {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); +#include - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } +#include "common.hpp" - auto - concat_test(std::vector const& input_filename_list, - int axis, - std::string const& true_output_filename) const { - std::vector input_name_list; - std::unordered_map - input_memory_table; - auto i = 0; - for(auto const& input_filename : input_filename_list) { - auto input_name = std::to_string(i); - input_name_list.push_back(input_name); - input_memory_table.insert( - {input_name, - load_np_array_as_memory(input_filename, engine_)}); - ++i; +namespace menoh_impl { + dtype_t tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt); +} + +namespace { + + struct named_array_data { + std::string name; + menoh::dtype_t dtype; + std::vector dims; + std::unique_ptr data; + }; + + auto load_param(filesystem::path const& filepath, + bool squash_dims = false) { + namespace gpio = ::google::protobuf::io; + + std::ifstream ifs(filepath.str(), std::ios::binary); + if(!ifs) { + std::cout << "invalid filename" << std::endl; + throw "invalid_filename"; + } + gpio::IstreamInputStream iis(&ifs); + gpio::CodedInputStream cis(&iis); + cis.SetTotalBytesLimit(std::numeric_limits::max(), + std::numeric_limits::max()); + onnx::TensorProto tensor; + if(!tensor.ParseFromCodedStream(&cis)) { + std::cout << "invalid filename" << std::endl; + throw "onnx_parse_error"; + } + + // TODO int array + std::vector dims(tensor.dims().begin(), tensor.dims().end()); + assert(2 <= dims.size()); + if(squash_dims) { + dims.at(1) = std::accumulate(dims.begin() + 1, dims.end(), 1, + std::multiplies()); + dims.erase(dims.begin() + 2, dims.end()); + } + auto total_size = + std::accumulate(dims.begin(), dims.end(), 1, std::multiplies()); + assert(tensor.has_raw_data()); + assert( + tensor.raw_data().length() == + static_cast(total_size * 4)); + + using float_t = + menoh_impl::dtype_to_type_t; + auto data = std::make_unique(total_size * 4); + std::copy(tensor.raw_data().begin(), tensor.raw_data().end(), + data.get()); + return named_array_data{ + tensor.name(), + static_cast( + menoh_impl::tensor_proto_data_type_to_dtype(tensor.data_type())), + std::move(dims), std::move(data)}; + } + + class OperatorTest : public ::testing::Test { + protected: + OperatorTest() + : onnx_test_data_dir_path_( + "../external/onnx/onnx/backend/test/data/node/") {} + + void run_test(std::string backend_name, std::string const& test_name, + float eps, bool squash_dims = false) { + auto parent_dir_path = onnx_test_data_dir_path_ / test_name; + + for(int data_set_index = 0; true; ++data_set_index) { + auto dataset_path = + parent_dir_path / + ("test_data_set_" + std::to_string(data_set_index)); + if(!dataset_path.exists()) { + break; } - auto output_dims = - extract_dims(input_memory_table.begin()->second); - output_dims.at(axis) = 0; - for(auto const& name_and_memory : input_memory_table) { - output_dims.at(axis) += - extract_dims(name_and_memory.second).at(axis); + std::vector input_list; + for(int input_index = 0;; ++input_index) { + auto input_data_path = + dataset_path / + ("input_" + std::to_string(input_index) + ".pb"); + if(!input_data_path.exists()) { + break; + } + input_list.push_back( + load_param(input_data_path, squash_dims)); + } + std::vector true_output_list; + for(int output_index = 0;; ++output_index) { + auto output_data_path = + dataset_path / + ("output_" + std::to_string(output_index) + ".pb"); + if(!output_data_path.exists()) { + break; + } + true_output_list.push_back( + load_param(output_data_path, squash_dims)); } - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_concat_primitive( - menoh_impl::node{ - "", input_name_list, {"output"}, {{"axis", axis}}}, - {}, input_memory_table, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto lrn_test(std::string const& input_filename, float alpha, - float beta, float bias, int size, - std::string const& true_output_filename) { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto output_dims = extract_dims(input_memory); - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_lrn_primitive( - menoh_impl::node{"", - {"input"}, - {"output"}, - {{"alpha", alpha}, - {"beta", beta}, - {"bias", bias}, - {"size", size}}}, - {}, {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - /* - auto true_output = menoh_impl::load_np_array_as_array( - "../data/lrn_alpha" + std::to_string(alpha) + "_beta" + - std::to_string(beta) + "_bias" + std::to_string(bias) + - "_size" + std::to_string(size) + ".txt"); - */ - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); - } - - auto gemm_test(std::string const& input_filename, - std::string const& weight_filename, - std::string const& bias_filename, - std::string const& true_output_filename, float alpha, - float beta, int trans_a, int trans_b) const { - auto input_memory = - load_np_array_as_memory(input_filename, engine_); - auto weight = - menoh_impl::load_np_array_as_array(weight_filename); - auto bias = menoh_impl::load_np_array_as_array(bias_filename); - std::vector output_dims{extract_dims(input_memory).at(0), - weight.dims().at(0)}; - auto output = array(dtype_t::float_, output_dims); - auto factory_return = make_gemm_primitive( - menoh_impl::node{"", - {"input", "weight", "bias"}, - {"output"}, - {{"alpha", alpha}, - {"beta", beta}, - {"transA", trans_a}, - {"transB", trans_b}}}, - {{"weight", weight}, {"bias", bias}}, - {{"input", input_memory}}, {{"output", output}}, engine_); - auto& net = std::get<0>(factory_return); - mkldnn::stream(mkldnn::stream::kind::eager).submit(net).wait(); - auto true_output = - menoh_impl::load_np_array_as_array(true_output_filename); + menoh::variable_profile_table_builder vpt_builder; + for(auto const& input : input_list) { + vpt_builder.add_input_profile(input.name, input.dtype, + input.dims); + } + for(auto const& output : true_output_list) { + vpt_builder.add_output_profile(output.name, output.dtype); + } + auto onnx_model_filename = parent_dir_path / "model.onnx"; + auto model_data = + menoh::make_model_data_from_onnx(onnx_model_filename.str()); + auto vpt = vpt_builder.build_variable_profile_table(model_data); + menoh::model_builder model_builder(vpt); + + for(auto const& input : input_list) { + model_builder.attach_external_buffer( + input.name, static_cast(input.data.get())); + } + auto model = + model_builder.build_model(model_data, backend_name); + model_data.reset(); - assert_near_list(fbegin(output), fend(output), - fbegin(true_output), fend(true_output), - 10.e-4); + std::vector output_list; + for(auto const& true_output : true_output_list) { + output_list.push_back(model.get_variable(true_output.name)); + } + model.run(); + assert(true_output_list.size() == output_list.size()); + auto static_cast_to_float_ptr = [](auto p) { + return static_cast(static_cast(p)); + }; + for(int output_index = 0; + output_index < true_output_list.size(); ++output_index) { + auto const& input = input_list.front(); + auto const& output = output_list.at(output_index); + auto const& true_output = true_output_list.at(output_index); + auto total_size = std::accumulate(true_output.dims.begin(), + true_output.dims.end(), 1, + std::multiplies()); + /* + std::cout << true_output.name << std::endl; + for(auto i = 0; i < 10; ++i) { + std::cout + << *(static_cast( + static_cast(input.data.get())) + + i) + << " " + << *(static_cast(output.buffer_handle) + i) + << " " + << *(static_cast( + static_cast(true_output.data.get())) + + i) + << std::endl; + } + */ + menoh_impl::assert_eq_list( + static_cast(output.buffer_handle), + static_cast(output.buffer_handle) + total_size, + static_cast_to_float_ptr(true_output.data.get()), + static_cast_to_float_ptr(true_output.data.get()) + + total_size, + eps); + } } - - mkldnn::engine engine_{mkldnn::engine::cpu, 0}; - }; - - TEST_F(OperatorTest, relu_1d_test) { - relu_test("../data/random_input_3_4096.txt", "../data/relu_1d.txt"); - } - TEST_F(OperatorTest, relu_2d_test) { - relu_test("../data/random_input_3_4_32_32.txt", - "../data/relu_2d.txt"); - } - - TEST_F(OperatorTest, leaky_relu_1d_test) { - leaky_relu_test("../data/random_input_3_4096.txt", - "../data/leaky_relu_1d.txt"); - } - TEST_F(OperatorTest, leaky_relu_2d_test) { - leaky_relu_test("../data/random_input_3_4_32_32.txt", - "../data/leaky_relu_2d.txt"); - } - - TEST_F(OperatorTest, elu_1d_test) { - elu_test("../data/random_input_3_4096.txt", "../data/elu_1d.txt"); - } - TEST_F(OperatorTest, elu_2d_test) { - elu_test("../data/random_input_3_4_32_32.txt", - "../data/elu_2d.txt"); - } - - TEST_F(OperatorTest, abs_1d_test) { - abs_test("../data/random_input_3_4096.txt", "../data/abs_1d.txt"); - } - TEST_F(OperatorTest, abs_2d_test) { - abs_test("../data/random_input_3_4_32_32.txt", - "../data/abs_2d.txt"); - } - - TEST_F(OperatorTest, sqrt_1d_test) { - sqrt_test("../data/random_positive_input_3_4096.txt", - "../data/sqrt_1d.txt"); - } - TEST_F(OperatorTest, sqrt_2d_test) { - sqrt_test("../data/random_positive_input_3_4_32_32.txt", - "../data/sqrt_2d.txt"); - } - - TEST_F(OperatorTest, tanh_1d_test) { - tanh_test("../data/random_input_3_4096.txt", "../data/tanh_1d.txt"); - } - TEST_F(OperatorTest, tanh_2d_test) { - tanh_test("../data/random_input_3_4_32_32.txt", - "../data/tanh_2d.txt"); - } - - TEST_F(OperatorTest, softmax_1d_test) { - softmax_test("../data/random_input_3_4096.txt", - "../data/softmax_1d.txt"); - } - TEST_F(OperatorTest, softmax_2d_test) { - softmax_test("../data/random_input_3_4_32_32.txt", - "../data/softmax_2d.txt"); - } - - TEST_F(OperatorTest, fc_1d_test) { - fc_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt"); - } - TEST_F(OperatorTest, fc_2d_test) { - fc_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt"); - } - - TEST_F(OperatorTest, max_pool_2_2_0_test) { - max_pool_test("../data/random_input_3_4_32_32.txt", 2, 2, 0); - } - TEST_F(OperatorTest, max_pool_3_2_0_test) { - max_pool_test("../data/random_input_3_4_32_32.txt", 3, 2, 0); - } - TEST_F(OperatorTest, max_pool_3_2_1_test) { - max_pool_test("../data/random_input_3_4_32_32.txt", 3, 2, 1); - } - - TEST_F(OperatorTest, average_pool_2_2_0_test) { - average_pool_test("../data/random_input_3_4_32_32.txt", 2, 2, 0); - } - TEST_F(OperatorTest, average_pool_3_2_0_test) { - average_pool_test("../data/random_input_3_4_32_32.txt", 3, 2, 0); - } - TEST_F(OperatorTest, average_pool_3_2_1_test) { - average_pool_test("../data/random_input_3_4_32_32.txt", 3, 2, 1); - } - - TEST_F(OperatorTest, global_max_pool_test) { - global_max_pool_test("../data/random_input_3_4_32_32.txt", - "../data/global_max_pooling_2d.txt"); - } - - TEST_F(OperatorTest, global_average_pool_test) { - global_average_pool_test("../data/random_input_3_4_32_32.txt", - "../data/global_average_pooling_2d.txt"); - } - - TEST_F(OperatorTest, conv_1_1_0_test) { - conv_test("../data/random_input_3_4_32_32.txt", 1, 1, 0); - } - TEST_F(OperatorTest, conv_2_1_0_test) { - conv_test("../data/random_input_3_4_32_32.txt", 2, 1, 0); - } - TEST_F(OperatorTest, conv_2_1_1_test) { - conv_test("../data/random_input_3_4_32_32.txt", 2, 1, 1); - } - TEST_F(OperatorTest, conv_2_2_0_test) { - conv_test("../data/random_input_3_4_32_32.txt", 2, 2, 0); - } - TEST_F(OperatorTest, conv_2_2_1_test) { - conv_test("../data/random_input_3_4_32_32.txt", 2, 2, 1); - } - TEST_F(OperatorTest, conv_3_1_1_test) { - conv_test("../data/random_input_3_4_32_32.txt", 3, 1, 1); - } - TEST_F(OperatorTest, conv_3_2_0_test) { - conv_test("../data/random_input_3_4_32_32.txt", 3, 2, 0); - } - TEST_F(OperatorTest, conv_3_2_1_test) { - conv_test("../data/random_input_3_4_32_32.txt", 3, 2, 1); - } - TEST_F(OperatorTest, conv_with_bias_1_1_0_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 1, 1, 0); - } - TEST_F(OperatorTest, conv_with_bias_2_1_0_test) { - conv_test("../data/random_input_3_4_32_32.txt", 2, 1, 0); - } - TEST_F(OperatorTest, conv_with_bias_2_1_1_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 2, 1, 1); - } - TEST_F(OperatorTest, conv_with_bias_2_2_0_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 2, 2, 0); - } - TEST_F(OperatorTest, conv_with_bias_2_2_1_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 2, 2, 1); - } - TEST_F(OperatorTest, conv_with_bias_3_1_1_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 3, 1, 1); - } - TEST_F(OperatorTest, conv_with_bias_3_2_0_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 3, 2, 0); - } - TEST_F(OperatorTest, conv_with_bias_3_2_1_test) { - conv_with_bias_test("../data/random_input_3_4_32_32.txt", 3, 2, 1); - } - - TEST_F(OperatorTest, conv_transpose_1_1_0_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 1, 1, 0); - } - TEST_F(OperatorTest, conv_transpose_2_1_0_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 2, 1, 0); - } - TEST_F(OperatorTest, conv_transpose_2_1_1_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 2, 1, 1); - } - TEST_F(OperatorTest, conv_transpose_2_2_0_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 2, 2, 0); - } - TEST_F(OperatorTest, conv_transpose_2_2_1_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 2, 2, 1); - } - TEST_F(OperatorTest, conv_transpose_3_1_1_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 3, 1, 1); - } - TEST_F(OperatorTest, conv_transpose_3_2_0_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 3, 2, 0); - } - TEST_F(OperatorTest, conv_transpose_3_2_1_test) { - conv_transpose_test("../data/random_input_3_4_32_32.txt", 3, 2, 1); - } - - TEST_F(OperatorTest, conv_transpose_with_bias_1_1_0_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 1, 1, 0); - } - TEST_F(OperatorTest, conv_transpose_with_bias_2_1_0_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 2, 1, 0); - } - TEST_F(OperatorTest, conv_transpose_with_bias_2_1_1_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 2, 1, 1); - } - TEST_F(OperatorTest, conv_transpose_with_bias_2_2_0_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 2, 2, 0); - } - TEST_F(OperatorTest, conv_transpose_with_bias_2_2_1_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 2, 2, 1); - } - TEST_F(OperatorTest, conv_transpose_with_bias_3_1_1_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 3, 1, 1); - } - TEST_F(OperatorTest, conv_transpose_with_bias_3_2_0_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 3, 2, 0); - } - TEST_F(OperatorTest, conv_transpose_with_bias_3_2_1_test) { - conv_transpose_with_bias_test("../data/random_input_3_4_32_32.txt", - 3, 2, 1); - } - - TEST_F(OperatorTest, batch_norm_test) { - batch_norm_test( - "../data/random_input_3_4_32_32.txt", "../data/random_mean_4.txt", - "../data/random_var_4.txt", "../data/random_gamma_4.txt", - "../data/random_beta_4.txt", "../data/batch_normalization.txt"); - } - - TEST_F(OperatorTest, add_1d_test) { - add_test("../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt", "../data/add_1d.txt"); - } - TEST_F(OperatorTest, add_2d_test) { - add_test("../data/random_input_3_4_32_32.txt", - "../data/random_input_3_4_32_32.txt", - "../data/add_2d.txt"); - } - - TEST_F(OperatorTest, concat_1d_2_inputs_axis_0_test) { - concat_test({"../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt"}, - 0, "../data/concat_1d_6_4096.txt"); - } - TEST_F(OperatorTest, concat_1d_2_inputs_axis_1_test) { - concat_test({"../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt"}, - 1, "../data/concat_1d_3_8192.txt"); - } - TEST_F(OperatorTest, concat_1d_3_inputs_axis_0_test) { - concat_test({"../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt"}, - 0, "../data/concat_1d_9_4096.txt"); - } - TEST_F(OperatorTest, concat_1d_3_inputs_axis_1_test) { - concat_test({"../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt", - "../data/random_input_3_4096.txt"}, - 1, "../data/concat_1d_3_12288.txt"); - } - - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_1_size_1_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 1, 1, - "../data/lrn_alpha0.0001_beta0.75_bias1_size1.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_1_size_2_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 1, 2, - "../data/lrn_alpha0.0001_beta0.75_bias1_size2.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_1_size_3_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 1, 3, - "../data/lrn_alpha0.0001_beta0.75_bias1_size3.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_1_size_4_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 1, 4, - "../data/lrn_alpha0.0001_beta0.75_bias1_size4.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_2_size_1_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 2, 1, - "../data/lrn_alpha0.0001_beta0.75_bias2_size1.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_2_size_2_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 2, 2, - "../data/lrn_alpha0.0001_beta0.75_bias2_size2.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_2_size_3_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 2, 3, - "../data/lrn_alpha0.0001_beta0.75_bias2_size3.txt"); - } - TEST_F(OperatorTest, lrn_alpha_00004_beta_075_bias_2_size_4_test) { - lrn_test("../data/random_input_3_4_32_32.txt", 0.0001, 0.75, 2, 4, - "../data/lrn_alpha0.0001_beta0.75_bias2_size4.txt"); - } - - TEST_F(OperatorTest, gemm_1d_test) { - gemm_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt", 1, 1, 0, 1); - } - TEST_F(OperatorTest, gemm_2d_test) { - gemm_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt", 1, 1, 0, 1); - } - TEST_F(OperatorTest, gemm_1d_test_invalid_alpha) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt", 2, 1, 0, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_2d_test_invalid_alpha) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt", 2, 1, 0, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_1d_test_invalid_beta) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt", 1, 2, 0, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_2d_test_invalid_beta) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt", 1, 2, 0, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_1d_test_invalid_transA) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt", 1, 1, 1, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_2d_test_invalid_transA) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt", 1, 1, 1, - 1); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_1d_test_invalid_transB) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4096.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_1d_w256_4096_b_256.txt", 1, 1, 0, - 0); - }, - failed_to_configure_operator); - } - TEST_F(OperatorTest, gemm_2d_test_invalid_transB) { - EXPECT_THROW( - { - gemm_test("../data/random_input_3_4_32_32.txt", - "../data/random_weight_256_4096.txt", - "../data/random_bias_256.txt", - "../data/linear_2d_w256_4096_b_256.txt", 1, 1, 0, - 0); - }, - failed_to_configure_operator); } - } // namespace mkldnn_backend -} // namespace menoh_impl + private: + filesystem::path onnx_test_data_dir_path_; + }; + +#define TEST_OP(backend_name, test_name, eps) \ + TEST_F(OperatorTest, test_name) { run_test(backend_name, #test_name, eps); } +#define TEST_OP_SQUASH_DIMS(backend_name, test_name, eps) \ + TEST_F(OperatorTest, test_name) { \ + run_test(backend_name, #test_name, eps, true); \ + } + + float eps = 1.e-4; + TEST_OP_SQUASH_DIMS("mkldnn", test_abs, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_elu, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_elu_default, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_leakyrelu, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_leakyrelu_default, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_relu, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_sqrt, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_tanh, eps); + + TEST_OP("mkldnn", test_averagepool_2d_default, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_add, eps); + // TEST_OP_SQUASH_DIMS("mkldnn", test_batchnormalization, eps); // not found + // TEST_OP("mkldnn", test_concat_2d_axis_0, eps); + // TEST_OP("mkldnn", test_concat_2d_axis_1, eps); + // TEST_OP("mkldnn", test_conv_with_strides_padding, eps); + // TEST_OP_SQUASH_DIMS("mkldnn", test_convtranspose, eps); // not found + // TEST_OP("mkldnn", test_gemm_nobroadcast, eps); + TEST_OP("mkldnn", test_globalaveragepool, eps); + TEST_OP("mkldnn", test_globalmaxpool, eps); + TEST_OP("mkldnn", test_maxpool_2d_default, eps); + TEST_OP_SQUASH_DIMS("mkldnn", test_softmax_axis_1, eps); + // TEST_OP_SQUASH_DIMS("mkldnn", test_sum_one_input, eps); + // TEST_OP_SQUASH_DIMS("mkldnn", test_sum_two_inputs, eps); + + // TEST_OP("mkldnn", test_averagepool_2d_pads, eps); + // TEST_OP("mkldnn", test_averagepool_2d_precomputed_pads, eps); + // TEST_OP("mkldnn", test_averagepool_2d_precomputed_same_upper, eps); + +#undef TEST_OP_SQUASH_DIMS +#undef TEST_OP + +} // namespace From 28e0474afba690c16b088b97808f3d35548da293 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 14 Sep 2018 23:58:15 +0900 Subject: [PATCH 06/53] remove comments --- test/operator.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/test/operator.cpp b/test/operator.cpp index 729e664..f67d65d 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -1,21 +1,3 @@ -/* -#include -#include -#include -#include -#include - -#include -#include - -#include "./common.hpp" -#include "np_io.hpp" - -#include -#include -#include -#include -*/ #include #include From aaa52f3699bff1122c4dc709731708c5bf9ac9b9 Mon Sep 17 00:00:00 2001 From: okdshin Date: Sun, 16 Sep 2018 00:38:19 +0900 Subject: [PATCH 07/53] add inline keyword --- test/operator.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/operator.cpp b/test/operator.cpp index f67d65d..8bef961 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -18,7 +18,8 @@ #include "common.hpp" namespace menoh_impl { - dtype_t tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt); + inline dtype_t + tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt); } namespace { From a9fe370556acab650b05e558507b9bed628898de Mon Sep 17 00:00:00 2001 From: okdshin Date: Sun, 16 Sep 2018 12:43:51 +0900 Subject: [PATCH 08/53] remove dependency to menoh_impl --- test/operator.cpp | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/test/operator.cpp b/test/operator.cpp index 8bef961..3a788df 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -9,19 +9,10 @@ #include #include -#include -#include -#include - #include #include "common.hpp" -namespace menoh_impl { - inline dtype_t - tensor_proto_data_type_to_dtype(onnx::TensorProto_DataType tpdt); -} - namespace { struct named_array_data { @@ -65,16 +56,12 @@ namespace { tensor.raw_data().length() == static_cast(total_size * 4)); - using float_t = - menoh_impl::dtype_to_type_t; auto data = std::make_unique(total_size * 4); std::copy(tensor.raw_data().begin(), tensor.raw_data().end(), data.get()); - return named_array_data{ - tensor.name(), - static_cast( - menoh_impl::tensor_proto_data_type_to_dtype(tensor.data_type())), - std::move(dims), std::move(data)}; + // TODO other dtype + return named_array_data{tensor.name(), menoh::dtype_t::float_, + std::move(dims), std::move(data)}; } class OperatorTest : public ::testing::Test { From 5fb7dfee027f7c9fe792b120b7393125f3f398e7 Mon Sep 17 00:00:00 2001 From: okdshin Date: Sun, 16 Sep 2018 14:18:23 +0900 Subject: [PATCH 09/53] indicate backend name when operator testing --- test/operator.cpp | 73 ++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/test/operator.cpp b/test/operator.cpp index 3a788df..9e0097b 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -174,43 +174,50 @@ namespace { filesystem::path onnx_test_data_dir_path_; }; -#define TEST_OP(backend_name, test_name, eps) \ - TEST_F(OperatorTest, test_name) { run_test(backend_name, #test_name, eps); } -#define TEST_OP_SQUASH_DIMS(backend_name, test_name, eps) \ - TEST_F(OperatorTest, test_name) { \ - run_test(backend_name, #test_name, eps, true); \ - } +#define TEST_OP_IMPL(backend_name, test_name, eps, squash) \ + TEST_F(OperatorTest, backend_name##_##test_name) { run_test(#backend_name, #test_name, eps, squash); } +#define TEST_OP(backend_name, test_name, eps) TEST_OP_IMPL(backend_name, test_name, eps, false) +#define TEST_OP_SQUASH_DIMS(backend_name, test_name, eps) TEST_OP_IMPL(backend_name, test_name, eps, true) float eps = 1.e-4; - TEST_OP_SQUASH_DIMS("mkldnn", test_abs, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_elu, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_elu_default, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_leakyrelu, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_leakyrelu_default, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_relu, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_sqrt, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_tanh, eps); - - TEST_OP("mkldnn", test_averagepool_2d_default, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_add, eps); - // TEST_OP_SQUASH_DIMS("mkldnn", test_batchnormalization, eps); // not found - // TEST_OP("mkldnn", test_concat_2d_axis_0, eps); - // TEST_OP("mkldnn", test_concat_2d_axis_1, eps); - // TEST_OP("mkldnn", test_conv_with_strides_padding, eps); - // TEST_OP_SQUASH_DIMS("mkldnn", test_convtranspose, eps); // not found - // TEST_OP("mkldnn", test_gemm_nobroadcast, eps); - TEST_OP("mkldnn", test_globalaveragepool, eps); - TEST_OP("mkldnn", test_globalmaxpool, eps); - TEST_OP("mkldnn", test_maxpool_2d_default, eps); - TEST_OP_SQUASH_DIMS("mkldnn", test_softmax_axis_1, eps); - // TEST_OP_SQUASH_DIMS("mkldnn", test_sum_one_input, eps); - // TEST_OP_SQUASH_DIMS("mkldnn", test_sum_two_inputs, eps); - - // TEST_OP("mkldnn", test_averagepool_2d_pads, eps); - // TEST_OP("mkldnn", test_averagepool_2d_precomputed_pads, eps); - // TEST_OP("mkldnn", test_averagepool_2d_precomputed_same_upper, eps); + + // Tests for MKLDNN backend + TEST_OP_SQUASH_DIMS(mkldnn, test_abs, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_elu, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_elu_default, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_leakyrelu, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_leakyrelu_default, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_relu, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_sqrt, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_tanh, eps); + + TEST_OP(mkldnn, test_averagepool_2d_default, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_add, eps); + // TEST_OP_SQUASH_DIMS(mkldnn, test_batchnormalization, eps); // not found + // TEST_OP(mkldnn, test_concat_2d_axis_0, eps); + // TEST_OP(mkldnn, test_concat_2d_axis_1, eps); + // TEST_OP(mkldnn, test_conv_with_strides_padding, eps); + // TEST_OP_SQUASH_DIMS(mkldnn, test_convtranspose, eps); // not found + // TEST_OP(mkldnn, test_gemm_nobroadcast, eps); + TEST_OP(mkldnn, test_globalaveragepool, eps); + TEST_OP(mkldnn, test_globalmaxpool, eps); + TEST_OP(mkldnn, test_maxpool_2d_default, eps); + TEST_OP_SQUASH_DIMS(mkldnn, test_softmax_axis_1, eps); + // TEST_OP_SQUASH_DIMS(mkldnn, test_sum_one_input, eps); + // TEST_OP_SQUASH_DIMS(mkldnn, test_sum_two_inputs, eps); + + // TEST_OP(mkldnn, test_averagepool_2d_pads, eps); + // TEST_OP(mkldnn, test_averagepool_2d_precomputed_pads, eps); + // TEST_OP(mkldnn, test_averagepool_2d_precomputed_same_upper, eps); + + + // Tests for MKLDNN with Generic fallback backend + TEST_OP(mkldnn_with_generic_fallback, test_relu, eps); + + TEST_OP(mkldnn_with_generic_fallback, test_gemm_nobroadcast, eps); #undef TEST_OP_SQUASH_DIMS #undef TEST_OP +#undef TEST_OP_IMPL } // namespace From 7d27e9737f9b6c38cc140121a41c5d61b7a2dd79 Mon Sep 17 00:00:00 2001 From: okdshin Date: Sun, 16 Sep 2018 21:26:25 +0900 Subject: [PATCH 10/53] modify shape inference for test --- menoh/graph.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/menoh/graph.cpp b/menoh/graph.cpp index 569f193..2a049f9 100644 --- a/menoh/graph.cpp +++ b/menoh/graph.cpp @@ -250,16 +250,20 @@ namespace menoh_impl { std::unordered_map> variable_dims_table( input_name_and_dims_pair_list.begin(), input_name_and_dims_pair_list.end()); - auto graph = make_graph(model_data.node_list); - auto parameter_table = std::unordered_map( + std::transform( model_data.parameter_name_and_array_list.begin(), - model_data.parameter_name_and_array_list.end()); + model_data.parameter_name_and_array_list.end(), + std::inserter(variable_dims_table, variable_dims_table.end()), + [](auto const& p) { + return std::make_pair(p.first, p.second.dims()); + }); + auto graph = make_graph(model_data.node_list); for(auto const& node : graph.node_list()) { if(node.op_type == "Conv") { auto weight_name = node.input_name_list.at(1); auto output_channel_num = get_output_channel_num_from_parameter_dims( - find_value(parameter_table, weight_name).dims()); + find_value(variable_dims_table, weight_name)); auto output_dims = calc_2d_output_dims(node, output_channel_num, variable_dims_table); auto dilations = @@ -283,7 +287,7 @@ namespace menoh_impl { auto weight_name = node.input_name_list.at(1); auto output_channel_num = get_output_channel_num_from_parameter_dims( - find_value(parameter_table, weight_name).dims()); + find_value(variable_dims_table, weight_name)); auto output_dims = calc_2d_output_dims_for_conv_transpose( node, output_channel_num, variable_dims_table); auto dilations = @@ -339,8 +343,7 @@ namespace menoh_impl { auto batch_size = get_batch_size_from_variable_dims( find_value(variable_dims_table, input_name)); auto weight_dims = - find_value(parameter_table, node.input_name_list.at(1)) - .dims(); + find_value(variable_dims_table, node.input_name_list.at(1)); auto input_size = std::accumulate(input_dims.begin() + 1, input_dims.end(), 1, std::multiplies()); @@ -359,8 +362,7 @@ namespace menoh_impl { auto batch_size = get_batch_size_from_variable_dims( find_value(variable_dims_table, input_name)); auto weight_dims = - find_value(parameter_table, node.input_name_list.at(1)) - .dims(); + find_value(variable_dims_table, node.input_name_list.at(1)); auto trans_a = optional_attribute_int(node, "transA", 0); if(trans_a) { throw unsupported_operator_attribute( From a79c0593036cb36c311d1971159c609d73680264 Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 19 Sep 2018 17:45:02 +0900 Subject: [PATCH 11/53] remove gemm test --- test/operator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/operator.cpp b/test/operator.cpp index 9e0097b..53893ed 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -214,7 +214,7 @@ namespace { // Tests for MKLDNN with Generic fallback backend TEST_OP(mkldnn_with_generic_fallback, test_relu, eps); - TEST_OP(mkldnn_with_generic_fallback, test_gemm_nobroadcast, eps); + //TEST_OP(mkldnn_with_generic_fallback, test_gemm_nobroadcast, eps); #undef TEST_OP_SQUASH_DIMS #undef TEST_OP From a7a597b8336d5a70e7eacb79047ded1416d592b1 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Thu, 20 Sep 2018 12:35:02 +0900 Subject: [PATCH 12/53] add build*-.sh --- .travis.yml | 7 ++- .travis/build-menoh.sh | 40 ++++++++------ .travis/build-mkldnn.sh | 98 +++++++++++++++++++++++++++++++++++ .travis/build-protobuf.sh | 88 +++++++++++++++++++++++++++++++ .travis/init-build-linux.sh | 26 +++++++--- .travis/init-build-osx.sh | 2 + .travis/install-mkldnn.sh | 74 +++----------------------- .travis/install-protobuf.sh | 61 +++------------------- .travis/linux-x86_64/build.sh | 2 + 9 files changed, 256 insertions(+), 142 deletions(-) create mode 100644 .travis/build-mkldnn.sh create mode 100644 .travis/build-protobuf.sh diff --git a/.travis.yml b/.travis.yml index b2233ef..82cc3f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,12 +60,15 @@ matrix: install: - if [ "$TRAVIS_OS_NAME" = "linux" -a "$CXX" = "g++" ]; then export CXX="g++-7" CC="gcc-7"; fi - | - bash -ex ${TRAVIS_BUILD_DIR}/.travis/install-mkldnn.sh \ + bash -ex ${TRAVIS_BUILD_DIR}/.travis/build-mkldnn.sh \ --version ${MKLDNN_VERSION} \ --download-dir ${HOME}/downloads \ - --build-dir ${HOME}/build \ + --extract-dir ${HOME}/build \ --install-dir ${HOME}/mkl-dnn-${MKLDNN_VERSION} \ --parallel ${MAKE_JOBS} + - | + bash -ex ${TRAVIS_BUILD_DIR}/.travis/install-mkldnn.sh \ + --build-dir ${HOME}/build/mkl-dnn-${MKLDNN_VERSION}/build script: true # skip build and test script: diff --git a/.travis/build-menoh.sh b/.travis/build-menoh.sh index 4d35f01..40db953 100644 --- a/.travis/build-menoh.sh +++ b/.travis/build-menoh.sh @@ -7,32 +7,40 @@ while [[ $# != 0 ]]; do shift break ;; + --build-type) + readonly ARG_BUILD_TYPE="$2" + shift 2 + ;; --source-dir) - ARG_SOURCE_DIR="$2" + readonly ARG_SOURCE_DIR="$2" + shift 2 + ;; + --build-dir) + readonly ARG_BUILD_DIR="$2" shift 2 ;; --install-dir) - ARG_INSTALL_DIR="$2" + readonly ARG_INSTALL_DIR="$2" shift 2 ;; --mkldnn-dir) - ARG_MKLDNN_DIR="$2" + readonly ARG_MKLDNN_DIR="$2" shift 2 ;; --link-static-libgcc) - ARG_LINK_STATIC_LIBGCC="$2" + readonly ARG_LINK_STATIC_LIBGCC="$2" shift 2 ;; --link-static-libstdcxx) - ARG_LINK_STATIC_LIBSTDCXX="$2" + readonly ARG_LINK_STATIC_LIBSTDCXX="$2" shift 2 ;; --link-static-libprotobuf) - ARG_LINK_STATIC_LIBPROTOBUF="$2" + readonly ARG_LINK_STATIC_LIBPROTOBUF="$2" shift 2 ;; -*) - err Unknown option \"$1\" + echo Unknown option \"$1\" 1>&2 exit ;; *) @@ -45,16 +53,18 @@ done # validate the arguments test -n "${ARG_SOURCE_DIR}" || { echo "--source-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_LINK_STATIC_LIBGCC}" || ARG_LINK_STATIC_LIBGCC='OFF' -test -n "${ARG_LINK_STATIC_LIBSTDCXX}" || ARG_LINK_STATIC_LIBSTDCXX='OFF' -test -n "${ARG_LINK_STATIC_LIBPROTOBUF}" || ARG_LINK_STATIC_LIBPROTOBUF='OFF' +# options that have default value +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${ARG_SOURCE_DIR}/build +test -n "${ARG_BUILD_TYPE}" || readonly ARG_BUILD_TYPE=Debug +test -n "${ARG_LINK_STATIC_LIBGCC}" || readonly ARG_LINK_STATIC_LIBGCC='OFF' +test -n "${ARG_LINK_STATIC_LIBSTDCXX}" || readonly ARG_LINK_STATIC_LIBSTDCXX='OFF' +test -n "${ARG_LINK_STATIC_LIBPROTOBUF}" || readonly ARG_LINK_STATIC_LIBPROTOBUF='OFF' echo -e "\e[33;1mBuilding Menoh\e[0m" -cd ${ARG_SOURCE_DIR} -[ -d "build" ] || mkdir -p build +[ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} -cd build +cd ${ARG_BUILD_DIR} if [ -n "${ARG_INSTALL_DIR}" ]; then OPT_CMAKE_INSTALL_PREFIX=-DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR} fi @@ -63,7 +73,7 @@ if [ -n "${ARG_MKLDNN_DIR}" ]; then OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so fi cmake \ - -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_BUILD_TYPE=${ARG_BUILD_TYPE} \ ${OPT_CMAKE_INSTALL_PREFIX} \ ${OPT_MKLDNN_INCLUDE_DIR} \ ${OPT_MKLDNN_LIBRARY} \ @@ -71,6 +81,6 @@ cmake \ -DLINK_STATIC_LIBSTDCXX=${ARG_LINK_STATIC_LIBSTDCXX} \ -DLINK_STATIC_LIBPROTOBUF=${ARG_LINK_STATIC_LIBPROTOBUF} \ -DENABLE_TEST=ON \ - .. + ${ARG_SOURCE_DIR} make diff --git a/.travis/build-mkldnn.sh b/.travis/build-mkldnn.sh new file mode 100644 index 0000000..0277c69 --- /dev/null +++ b/.travis/build-mkldnn.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +# retrieve arguments +while [[ $# != 0 ]]; do + case $1 in + --) + shift + break + ;; + --version) + readonly ARG_VERSION="$2" + shift 2 + ;; + --download-dir) + readonly ARG_DOWNLOAD_DIR="$2" + shift 2 + ;; + --extract-dir) + readonly ARG_EXTRACT_DIR="$2" + shift 2 + ;; + --build-dir) + readonly ARG_BUILD_DIR="$2" + shift 2 + ;; + --install-dir) + readonly ARG_INSTALL_DIR="$2" + shift 2 + ;; + --parallel) + readonly ARG_PARALLEL="$2" + shift 2 + ;; + -*) + echo Unknown option \"$1\" 1>&2 + exit + ;; + *) + break + ;; + esac +done + +# validate the arguments +test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } +test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } +test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } +test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } + +readonly LIBRARY_NAME=mkl-dnn-${ARG_VERSION} +readonly SOURCE_DIR=${ARG_EXTRACT_DIR}/${LIBRARY_NAME} + +# options that have default value +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${SOURCE_DIR}/build +test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 + +# download (if it isn't cached) +if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then + echo -e "\e[33;1mDownloading libmkldnn\e[0m" + + [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} + + cd ${ARG_DOWNLOAD_DIR} + if [ ! -e "${LIBRARY_NAME}.tar.gz" ]; then + download_url="https://github.com/intel/mkl-dnn/archive/v${ARG_VERSION}.tar.gz" + wget -O ${LIBRARY_NAME}.tar.gz ${download_url} + fi + tar -zxf ${LIBRARY_NAME}.tar.gz -C ${ARG_EXTRACT_DIR} + + echo -e "\e[32;1mlibmkldnn was successfully downloaded.\e[0m" +else + echo -e "\e[32;1mlibmkldnn has been downloaded.\e[0m" +fi + +# build (if it isn't cached) +if [ ! -e "${ARG_BUILD_DIR}/src/libmkldnn.so" ]; then + echo -e "\e[33;1mBuilding libmkldnn\e[0m" + + cd ${SOURCE_DIR}/scripts + ./prepare_mkl.sh + + [ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} + + cd ${ARG_BUILD_DIR} + cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR} \ + -DWITH_TEST=OFF \ + -DWITH_EXAMPLE=OFF \ + -DARCH_OPT_FLAGS='' \ + -Wno-error=unused-result \ + ${SOURCE_DIR} + make -j${ARG_PARALLEL} + + echo -e "\e[32;1mlibmkldnn was successfully built.\e[0m" +else + echo -e "\e[32;1mlibmkldnn has been built.\e[0m" +fi diff --git a/.travis/build-protobuf.sh b/.travis/build-protobuf.sh new file mode 100644 index 0000000..aee877c --- /dev/null +++ b/.travis/build-protobuf.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +# retrieve arguments +while [[ $# != 0 ]]; do + case $1 in + --) + shift + break + ;; + --version) + readonly ARG_VERSION="$2" + shift 2 + ;; + --download-dir) + readonly ARG_DOWNLOAD_DIR="$2" + shift 2 + ;; + --extract-dir) + readonly ARG_EXTRACT_DIR="$2" + shift 2 + ;; + --build-dir) + readonly ARG_BUILD_DIR="$2" + shift 2 + ;; + --install-dir) + readonly ARG_INSTALL_DIR="$2" + shift 2 + ;; + --parallel) + readonly ARG_PARALLEL="$2" + shift 2 + ;; + -*) + echo Unknown option \"$1\" 1>&2 + exit + ;; + *) + break + ;; + esac +done + +# validate the arguments +test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } +test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } +test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } +test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } + +readonly LIBRARY_NAME=protobuf-${ARG_VERSION} +readonly SOURCE_DIR=${ARG_EXTRACT_DIR}/${LIBRARY_NAME} + +# options that have default value +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${SOURCE_DIR} +test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 + +# download (if it isn't cached) +if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then + echo -e "\e[33;1mDownloading libprotobuf\e[0m" + + [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} + + cd ${ARG_DOWNLOAD_DIR} + if [ ! -e "protobuf-cpp-${ARG_VERSION}.tar.gz" ]; then + download_dir="https://github.com/protocolbuffers/protobuf/releases/download/v${ARG_VERSION}/protobuf-cpp-${ARG_VERSION}.tar.gz" + wget ${download_dir} + fi + tar -zxf protobuf-cpp-${ARG_VERSION}.tar.gz -C ${ARG_EXTRACT_DIR} + + echo -e "\e[32;1mlibprotobuf was successfully downloaded.\e[0m" +else + echo -e "\e[32;1mlibprotobuf has been downloaded.\e[0m" +fi + +# build (if it isn't cached) +if [ ! -e "${ARG_BUILD_DIR}/src/libprotobuf.la" ]; then + echo -e "\e[33;1mBuilding libprotobuf\e[0m" + + [ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} + + cd ${ARG_BUILD_DIR} + ${SOURCE_DIR}/configure --prefix=${ARG_INSTALL_DIR} CFLAGS=-fPIC CXXFLAGS=-fPIC + make -j${ARG_PARALLEL} + + echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" +else + echo -e "\e[32;1mlibprotobuf has been built.\e[0m" +fi diff --git a/.travis/init-build-linux.sh b/.travis/init-build-linux.sh index 1c4eaba..0a18580 100644 --- a/.travis/init-build-linux.sh +++ b/.travis/init-build-linux.sh @@ -34,26 +34,38 @@ function docker_exec_script() { return $? } -function install_protobuf() { +function build_protobuf() { docker_exec_script \ - ${PROJ_DIR}/.travis/install-protobuf.sh \ + ${PROJ_DIR}/.travis/build-protobuf.sh \ --version ${PROTOBUF_VERSION} \ --download-dir ${WORK_DIR}/downloads \ - --build-dir ${WORK_DIR}/build \ + --extract-dir ${WORK_DIR}/build \ --install-dir ${PROTOBUF_INSTALL_DIR} \ --parallel ${MAKE_JOBS} } -function install_mkldnn() { +function install_protobuf() { docker_exec_script \ - ${PROJ_DIR}/.travis/install-mkldnn.sh \ + ${PROJ_DIR}/.travis/install-protobuf.sh \ + --build-dir ${WORK_DIR}/build/protobuf-${PROTOBUF_VERSION} +} + +function build_mkldnn() { + docker_exec_script \ + ${PROJ_DIR}/.travis/build-mkldnn.sh \ --version ${MKLDNN_VERSION} \ --download-dir ${WORK_DIR}/downloads \ - --build-dir ${WORK_DIR}/build \ + --extract-dir ${WORK_DIR}/build \ --install-dir ${MKLDNN_INSTALL_DIR} \ --parallel ${MAKE_JOBS} } +function install_mkldnn() { + docker_exec_script \ + ${PROJ_DIR}/.travis/install-mkldnn.sh \ + --build-dir ${WORK_DIR}/build/mkl-dnn-${MKLDNN_VERSION}/build +} + function prepare_menoh_data() { docker_exec_script \ ${PROJ_DIR}/.travis/prepare-menoh-data.sh \ @@ -65,10 +77,12 @@ function build_menoh() { if [ "${LINK_STATIC}" != "true" ]; then docker_exec_script \ ${PROJ_DIR}/.travis/build-menoh.sh \ + --build-type Release \ --source-dir ${PROJ_DIR} else docker_exec_script \ ${PROJ_DIR}/.travis/build-menoh.sh \ + --build-type Release \ --source-dir ${PROJ_DIR} \ --link-static-libgcc ON \ --link-static-libstdcxx ON \ diff --git a/.travis/init-build-osx.sh b/.travis/init-build-osx.sh index 627e070..3b74a82 100644 --- a/.travis/init-build-osx.sh +++ b/.travis/init-build-osx.sh @@ -16,10 +16,12 @@ function prepare_menoh_data() { function build_menoh() { if [ "${LINK_STATIC}" != "true" ]; then bash -ex ${PROJ_DIR}/.travis/build-menoh.sh \ + --build-type Release \ --source-dir ${PROJ_DIR} else # Does not set --link-static-libgcc and --link-static-libstdcxx in macOS bash -ex ${PROJ_DIR}/.travis/build-menoh.sh \ + --build-type Release \ --source-dir ${PROJ_DIR} \ --link-static-libprotobuf ON fi diff --git a/.travis/install-mkldnn.sh b/.travis/install-mkldnn.sh index cb797e8..3024147 100644 --- a/.travis/install-mkldnn.sh +++ b/.travis/install-mkldnn.sh @@ -7,90 +7,32 @@ while [[ $# != 0 ]]; do shift break ;; - --version) - ARG_VERSION="$2" - shift 2 - ;; - --download-dir) - ARG_DOWNLOAD_DIR="$2" - shift 2 - ;; --build-dir) - ARG_BUILD_DIR="$2" + readonly ARG_BUILD_DIR="$2" shift 2 ;; - --install-dir) - ARG_INSTALL_DIR="$2" - shift 2 - ;; - --parallel) - ARG_PARALLEL="$2" + --dest-dir) + readonly ARG_DESTDIR="$2" shift 2 ;; -*) - err Unknown option \"$1\" + echo Unknown option \"$1\" 1>&2 exit ;; *) break ;; - esac done # validate the arguments -test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } -test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } test -n "${ARG_BUILD_DIR}" || { echo "--build-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_PARALLEL}" || ARG_PARALLEL=1 - -# download (if it isn't cached) -if [ ! -e "${ARG_BUILD_DIR}/mkl-dnn-${ARG_VERSION}/LICENSE" ]; then - echo -e "\e[33;1mDownloading libmkldnn\e[0m" - - [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} - - cd ${ARG_DOWNLOAD_DIR} - if [ ! -e "mkl-dnn-${ARG_VERSION}.tar.gz" ]; then - download_url="https://github.com/intel/mkl-dnn/archive/v${ARG_VERSION}.tar.gz" - wget -O mkl-dnn-${ARG_VERSION}.tar.gz ${download_url} - fi - tar -zxf mkl-dnn-${ARG_VERSION}.tar.gz -C ${ARG_BUILD_DIR} - - echo -e "\e[32;1mlibmkldnn was successfully downloaded.\e[0m" -else - echo -e "\e[32;1mlibmkldnn has been downloaded.\e[0m" -fi - -# build (if it isn't cached) -if [ ! -e "${ARG_BUILD_DIR}/mkl-dnn-${ARG_VERSION}/build/src/libmkldnn.so" ]; then - echo -e "\e[33;1mBuilding libmkldnn\e[0m" - - cd ${ARG_BUILD_DIR}/mkl-dnn-${ARG_VERSION}/scripts - ./prepare_mkl.sh - - cd ${ARG_BUILD_DIR}/mkl-dnn-${ARG_VERSION} - [ -d "build" ] || mkdir -p build - - cd build - cmake \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR} \ - -DWITH_TEST=OFF \ - -DWITH_EXAMPLE=OFF \ - -DARCH_OPT_FLAGS='' \ - -Wno-error=unused-result \ - .. - make -j${ARG_PARALLEL} - - echo -e "\e[32;1mlibmkldnn was successfully built.\e[0m" -else - echo -e "\e[32;1mlibmkldnn has been built.\e[0m" -fi # install (always) echo -e "\e[33;1mInstalling libmkldnn\e[0m" -cd ${ARG_BUILD_DIR}/mkl-dnn-${ARG_VERSION}/build +# install to ${DESTDIR}/${CMAKE_INSTALL_PREFIX} if it is specified +[ -n "${ARG_DESTDIR}" ] && export DESTDIR=${ARG_DESTDIR} + +cd ${ARG_BUILD_DIR} make install/strip diff --git a/.travis/install-protobuf.sh b/.travis/install-protobuf.sh index f3b4fd5..131e0e3 100644 --- a/.travis/install-protobuf.sh +++ b/.travis/install-protobuf.sh @@ -7,77 +7,32 @@ while [[ $# != 0 ]]; do shift break ;; - --version) - ARG_VERSION="$2" - shift 2 - ;; - --download-dir) - ARG_DOWNLOAD_DIR="$2" - shift 2 - ;; --build-dir) - ARG_BUILD_DIR="$2" + readonly ARG_BUILD_DIR="$2" shift 2 ;; - --install-dir) - ARG_INSTALL_DIR="$2" - shift 2 - ;; - --parallel) - ARG_PARALLEL="$2" + --dest-dir) + readonly ARG_DESTDIR="$2" shift 2 ;; -*) - err Unknown option \"$1\" + echo Unknown option \"$1\" 1>&2 exit ;; *) break ;; - esac done # validate the arguments -test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } -test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } test -n "${ARG_BUILD_DIR}" || { echo "--build-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_PARALLEL}" || ARG_PARALLEL=1 - -# download (if it isn't cached) -if [ ! -e "${ARG_BUILD_DIR}/protobuf-${ARG_VERSION}/LICENSE" ]; then - echo -e "\e[33;1mDownloading libprotobuf\e[0m" - - [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} - - cd ${ARG_DOWNLOAD_DIR} - if [ ! -e "protobuf-cpp-${ARG_VERSION}.tar.gz" ]; then - download_dir="https://github.com/protocolbuffers/protobuf/releases/download/v${ARG_VERSION}/protobuf-cpp-${ARG_VERSION}.tar.gz" - wget ${download_dir} - fi - tar -zxf protobuf-cpp-${ARG_VERSION}.tar.gz -C ${ARG_BUILD_DIR} - - echo -e "\e[32;1mlibprotobuf was successfully downloaded.\e[0m" -else - echo -e "\e[32;1mlibprotobuf has been downloaded.\e[0m" -fi - -# build (if it isn't cached) -if [ ! -e "${ARG_BUILD_DIR}/protobuf-${ARG_VERSION}/src/libprotobuf.la" ]; then - echo -e "\e[33;1mBuilding libprotobuf\e[0m" - - cd ${ARG_BUILD_DIR}/protobuf-${ARG_VERSION} - ./configure --prefix=${ARG_INSTALL_DIR} CFLAGS=-fPIC CXXFLAGS=-fPIC - make -j${ARG_PARALLEL} - - echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" -else - echo -e "\e[32;1mlibprotobuf has been built.\e[0m" -fi # install (always) echo -e "\e[33;1mInstalling libprotobuf\e[0m" -cd ${ARG_BUILD_DIR}/protobuf-${ARG_VERSION} +# install to ${DESTDIR}/`--prefix` if it is specified +[ -n "${ARG_DESTDIR}" ] && export DESTDIR=${ARG_DESTDIR} + +cd ${ARG_BUILD_DIR} make install diff --git a/.travis/linux-x86_64/build.sh b/.travis/linux-x86_64/build.sh index 0af7711..692b86c 100644 --- a/.travis/linux-x86_64/build.sh +++ b/.travis/linux-x86_64/build.sh @@ -13,7 +13,9 @@ docker_exec "ls -l ${WORK_DIR}/build/${TRAVIS_REPO_SLUG}" docker_exec "(printenv | grep PATH) && make --version && cmake --version && g++ --version && ldd --version" # build and install prerequisites +build_protobuf install_protobuf +build_mkldnn install_mkldnn docker_exec "pip3 install --user chainer" # for generating test data From 83f1b749ab56ffd21a8d4f7544fb1a919678c725 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Thu, 20 Sep 2018 15:23:52 +0900 Subject: [PATCH 13/53] move scripts under scripts directory --- .travis.yml | 4 +- .travis/README.md | 2 +- .travis/init-build-linux.sh | 44 ++++++++++---------- .travis/init-build-osx.sh | 16 +++---- .travis/linux-x86_64/build.sh | 2 +- .travis/macosx-x86_64/build.sh | 2 +- BUILDING.md | 2 +- README.md | 6 +-- appveyor.yml | 8 ++-- docs/getting_started.md | 4 +- docs/main.md | 2 +- {.travis => scripts}/build-menoh.sh | 36 ++++++++-------- {.travis => scripts}/build-mkldnn.sh | 29 ++++++------- {.travis => scripts}/build-protobuf.sh | 25 +++++------ gen_test_data.py => scripts/gen_test_data.py | 0 {.travis => scripts}/install-mkldnn.sh | 6 +-- {.travis => scripts}/install-protobuf.sh | 6 +-- {.travis => scripts}/prepare-menoh-data.sh | 18 ++++---- retrieve_data.py => scripts/retrieve_data.py | 0 {.travis => scripts}/run-container.sh | 0 20 files changed, 108 insertions(+), 104 deletions(-) rename {.travis => scripts}/build-menoh.sh (79%) mode change 100644 => 100755 rename {.travis => scripts}/build-mkldnn.sh (76%) mode change 100644 => 100755 rename {.travis => scripts}/build-protobuf.sh (75%) mode change 100644 => 100755 rename gen_test_data.py => scripts/gen_test_data.py (100%) rename {.travis => scripts}/install-mkldnn.sh (88%) mode change 100644 => 100755 rename {.travis => scripts}/install-protobuf.sh (88%) mode change 100644 => 100755 rename {.travis => scripts}/prepare-menoh-data.sh (52%) mode change 100644 => 100755 rename retrieve_data.py => scripts/retrieve_data.py (100%) rename {.travis => scripts}/run-container.sh (100%) diff --git a/.travis.yml b/.travis.yml index 82cc3f7..ac28c6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,14 +60,14 @@ matrix: install: - if [ "$TRAVIS_OS_NAME" = "linux" -a "$CXX" = "g++" ]; then export CXX="g++-7" CC="gcc-7"; fi - | - bash -ex ${TRAVIS_BUILD_DIR}/.travis/build-mkldnn.sh \ + bash -ex ${TRAVIS_BUILD_DIR}/scripts/build-mkldnn.sh \ --version ${MKLDNN_VERSION} \ --download-dir ${HOME}/downloads \ --extract-dir ${HOME}/build \ --install-dir ${HOME}/mkl-dnn-${MKLDNN_VERSION} \ --parallel ${MAKE_JOBS} - | - bash -ex ${TRAVIS_BUILD_DIR}/.travis/install-mkldnn.sh \ + bash -ex ${TRAVIS_BUILD_DIR}/scripts/install-mkldnn.sh \ --build-dir ${HOME}/build/mkl-dnn-${MKLDNN_VERSION}/build script: true # skip build and test diff --git a/.travis/README.md b/.travis/README.md index 1ef0312..8f45bcc 100644 --- a/.travis/README.md +++ b/.travis/README.md @@ -12,7 +12,7 @@ Linux-based platforms requires a container image with the following softwares: Currently it uses [okapies/buildenv](https://hub.docker.com/r/okapies/buildenv/) image for linux-x86_64 platform. ## Architecture -`.travis.yml` -> `run-build.sh` -> `build.sh` -> `install-*.sh` & `build-menoh.sh` +`.travis.yml` -> `run-build.sh` -> `build.sh` -> `scripts/*.sh` 1. `run-build.sh` just calls a platform's `build.sh` for running the actual build workflow 2. `build.sh` diff --git a/.travis/init-build-linux.sh b/.travis/init-build-linux.sh index 0a18580..a5e5f16 100644 --- a/.travis/init-build-linux.sh +++ b/.travis/init-build-linux.sh @@ -16,8 +16,8 @@ export MKLDNN_INSTALL_DIR=/usr/local # $HOME:$HOME = /home/travis : /home/travis # /home/travis/build : /home/travis/build # /home/travis/build// : /home/travis/build// (= ${TRAVIS_BUILD_DIR}) -SHARED_SCRIPT_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd) -source ${SHARED_SCRIPT_DIR}/run-container.sh --image ${BUILDENV_IMAGE} --work-dir ${WORK_DIR} +SOURCE_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}/..")"; pwd) +source ${SOURCE_DIR}/scripts/run-container.sh --image ${BUILDENV_IMAGE} --work-dir ${WORK_DIR} test -n "${BUILDENV_CONTAINER_ID}" || { echo "BUILDENV_CONTAINER_ID can't be empty" 1>&2; exit 1; } ## define shared functions for Linux-based platforms @@ -36,54 +36,54 @@ function docker_exec_script() { function build_protobuf() { docker_exec_script \ - ${PROJ_DIR}/.travis/build-protobuf.sh \ + "${PROJ_DIR}/scripts/build-protobuf.sh" \ --version ${PROTOBUF_VERSION} \ - --download-dir ${WORK_DIR}/downloads \ - --extract-dir ${WORK_DIR}/build \ - --install-dir ${PROTOBUF_INSTALL_DIR} \ + --download-dir "${WORK_DIR}/downloads" \ + --extract-dir "${WORK_DIR}/build" \ + --install-dir "${PROTOBUF_INSTALL_DIR}" \ --parallel ${MAKE_JOBS} } function install_protobuf() { docker_exec_script \ - ${PROJ_DIR}/.travis/install-protobuf.sh \ - --build-dir ${WORK_DIR}/build/protobuf-${PROTOBUF_VERSION} + "${PROJ_DIR}/scripts/install-protobuf.sh" \ + --build-dir "${WORK_DIR}/build/protobuf-${PROTOBUF_VERSION}" } function build_mkldnn() { docker_exec_script \ - ${PROJ_DIR}/.travis/build-mkldnn.sh \ + "${PROJ_DIR}/scripts/build-mkldnn.sh" \ --version ${MKLDNN_VERSION} \ - --download-dir ${WORK_DIR}/downloads \ - --extract-dir ${WORK_DIR}/build \ - --install-dir ${MKLDNN_INSTALL_DIR} \ + --download-dir "${WORK_DIR}/downloads" \ + --extract-dir "${WORK_DIR}/build" \ + --install-dir "${MKLDNN_INSTALL_DIR}" \ --parallel ${MAKE_JOBS} } function install_mkldnn() { docker_exec_script \ - ${PROJ_DIR}/.travis/install-mkldnn.sh \ - --build-dir ${WORK_DIR}/build/mkl-dnn-${MKLDNN_VERSION}/build + "${PROJ_DIR}/scripts/install-mkldnn.sh" \ + --build-dir "${WORK_DIR}/build/mkl-dnn-${MKLDNN_VERSION}/build" } function prepare_menoh_data() { docker_exec_script \ - ${PROJ_DIR}/.travis/prepare-menoh-data.sh \ - --source-dir ${PROJ_DIR} \ + "${PROJ_DIR}/scripts/prepare-menoh-data.sh" \ + --source-dir "${PROJ_DIR}" \ --python-executable python3 } function build_menoh() { if [ "${LINK_STATIC}" != "true" ]; then docker_exec_script \ - ${PROJ_DIR}/.travis/build-menoh.sh \ + "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir ${PROJ_DIR} + --source-dir "${PROJ_DIR}" else docker_exec_script \ - ${PROJ_DIR}/.travis/build-menoh.sh \ + "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir ${PROJ_DIR} \ + --source-dir "${PROJ_DIR}" \ --link-static-libgcc ON \ --link-static-libstdcxx ON \ --link-static-libprotobuf ON @@ -91,9 +91,9 @@ function build_menoh() { } function test_menoh() { - docker_exec "cd ${PROJ_DIR}/build && ./test/menoh_test" + docker_exec "cd \"${PROJ_DIR}/build\" && ./test/menoh_test" } function check_menoh_artifact() { - docker_exec "ldd ${PROJ_DIR}/build/menoh/libmenoh.so" + docker_exec "ldd \"${PROJ_DIR}/build/menoh/libmenoh.so\"" } diff --git a/.travis/init-build-osx.sh b/.travis/init-build-osx.sh index 3b74a82..c1228d8 100644 --- a/.travis/init-build-osx.sh +++ b/.travis/init-build-osx.sh @@ -8,30 +8,30 @@ export PROJ_DIR=${TRAVIS_BUILD_DIR} # = ${HOME}/build/${TRAVIS_REPO_SLUG} ## define shared functions for macOS (OSX) platforms function prepare_menoh_data() { - bash -ex ${PROJ_DIR}/.travis/prepare-menoh-data.sh \ - --source-dir ${PROJ_DIR} \ + bash -ex "${PROJ_DIR}/scripts/prepare-menoh-data.sh" \ + --source-dir "${PROJ_DIR}" \ --python-executable python } function build_menoh() { if [ "${LINK_STATIC}" != "true" ]; then - bash -ex ${PROJ_DIR}/.travis/build-menoh.sh \ + bash -ex "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir ${PROJ_DIR} + --source-dir "${PROJ_DIR}" else # Does not set --link-static-libgcc and --link-static-libstdcxx in macOS - bash -ex ${PROJ_DIR}/.travis/build-menoh.sh \ + bash -ex "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir ${PROJ_DIR} \ + --source-dir "${PROJ_DIR}" \ --link-static-libprotobuf ON fi } function test_menoh() { - cd ${PROJ_DIR}/build + cd "${PROJ_DIR}/build" ./test/menoh_test } function check_menoh_artifact() { - otool -L ${PROJ_DIR}/build/menoh/libmenoh.dylib + otool -L "${PROJ_DIR}/build/menoh/libmenoh.dylib" } diff --git a/.travis/linux-x86_64/build.sh b/.travis/linux-x86_64/build.sh index 692b86c..205f028 100644 --- a/.travis/linux-x86_64/build.sh +++ b/.travis/linux-x86_64/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -ex -BASE_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd) +BASE_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) # initialize this script source ${BASE_DIR}/../init-build-linux.sh diff --git a/.travis/macosx-x86_64/build.sh b/.travis/macosx-x86_64/build.sh index a81b8ca..ec672cc 100644 --- a/.travis/macosx-x86_64/build.sh +++ b/.travis/macosx-x86_64/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -BASE_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd) +BASE_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) # initialize this script source ${BASE_DIR}/../init-build-osx.sh diff --git a/BUILDING.md b/BUILDING.md index 698309d..eb6fea1 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -124,7 +124,7 @@ make install # as root To run the example, you also need to download model data: ``` -python retrieve_data.py +python scripts/retrieve_data.py ``` #### Static linking diff --git a/README.md b/README.md index 3516f9c..03663ac 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ This codebase contains C API and C++ API. Execute following commands in root directory. ``` -python retrieve_data.py +python scripts/retrieve_data.py mkdir build && cd build cmake .. make @@ -111,7 +111,7 @@ Setup chainer Then, execute following commands in root directory. ``` -python gen_test_data.py +python scripts/gen_test_data.py cd build cmake -DENABLE_TEST=ON .. make @@ -155,7 +155,7 @@ make Menoh is released under MIT License. Please see the LICENSE file for details. -Note: `retrieve_data.py` downloads `data/VGG16.onnx`. `data/VGG16.onnx` is generated by onnx-chainer from pre-trained model which is uploaded +Note: `scripts/retrieve_data.py` downloads `data/VGG16.onnx`. `data/VGG16.onnx` is generated by onnx-chainer from pre-trained model which is uploaded at http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel That pre-trained model is released under Creative Commons Attribution License. diff --git a/appveyor.yml b/appveyor.yml index 90e1e4a..ea5580f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -40,13 +40,13 @@ install: - if [%TARGET%]==[mingw] ( pip3 install --user chainer && mkdir -p data && - python3 retrieve_data.py && - python3 gen_test_data.py + python3 scripts/retrieve_data.py && + python3 scripts/gen_test_data.py ) else ( pip install --user chainer && md data && - python retrieve_data.py && - python gen_test_data.py + python scripts/retrieve_data.py && + python scripts/gen_test_data.py ) before_build: diff --git a/docs/getting_started.md b/docs/getting_started.md index 2060c40..26be544 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -39,7 +39,7 @@ make install Execute below command in root directory: ``` -python retrieve_data.py +python scripts/retrieve_data.py cd build ./example/vgg16_example ``` @@ -68,7 +68,7 @@ Please give `--help` option for details Execute below commands in root directory: ``` -python gen_test_data.py +python scripts/gen_test_data.py cd build cmake -DENABLE_TEST=ON .. make diff --git a/docs/main.md b/docs/main.md index 4b7a94d..c123ee3 100644 --- a/docs/main.md +++ b/docs/main.md @@ -43,7 +43,7 @@ Menoh is released under MIT License. Menoh is released under MIT License. Please see the LICENSE file for details. -Note: `retrieve_data.sh` downloads `data/VGG16.onnx`. +Note: `scripts/retrieve_data.sh` downloads `data/VGG16.onnx`. `data/VGG16.onnx` is generated by onnx-chainer from pre-trained model which is uploaded at http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel diff --git a/.travis/build-menoh.sh b/scripts/build-menoh.sh old mode 100644 new mode 100755 similarity index 79% rename from .travis/build-menoh.sh rename to scripts/build-menoh.sh index 40db953..6f7f80b --- a/.travis/build-menoh.sh +++ b/scripts/build-menoh.sh @@ -1,4 +1,6 @@ -#!/bin/bash +#!/bin/bash -e + +BASE_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) # retrieve arguments while [[ $# != 0 ]]; do @@ -50,37 +52,35 @@ while [[ $# != 0 ]]; do esac done -# validate the arguments -test -n "${ARG_SOURCE_DIR}" || { echo "--source-dir is not specified" 1>&2; exit 1; } - # options that have default value -test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${ARG_SOURCE_DIR}/build test -n "${ARG_BUILD_TYPE}" || readonly ARG_BUILD_TYPE=Debug +test -n "${ARG_SOURCE_DIR}" || readonly ARG_SOURCE_DIR="${BASE_DIR}/.." +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${ARG_SOURCE_DIR}/build" +test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local + +if [ -n "${ARG_MKLDNN_DIR}" ]; then + OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include + OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so +fi + test -n "${ARG_LINK_STATIC_LIBGCC}" || readonly ARG_LINK_STATIC_LIBGCC='OFF' test -n "${ARG_LINK_STATIC_LIBSTDCXX}" || readonly ARG_LINK_STATIC_LIBSTDCXX='OFF' test -n "${ARG_LINK_STATIC_LIBPROTOBUF}" || readonly ARG_LINK_STATIC_LIBPROTOBUF='OFF' echo -e "\e[33;1mBuilding Menoh\e[0m" -[ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} +[ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" -cd ${ARG_BUILD_DIR} -if [ -n "${ARG_INSTALL_DIR}" ]; then - OPT_CMAKE_INSTALL_PREFIX=-DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR} -fi -if [ -n "${ARG_MKLDNN_DIR}" ]; then - OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include - OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so -fi +cd "${ARG_BUILD_DIR}" cmake \ -DCMAKE_BUILD_TYPE=${ARG_BUILD_TYPE} \ - ${OPT_CMAKE_INSTALL_PREFIX} \ - ${OPT_MKLDNN_INCLUDE_DIR} \ - ${OPT_MKLDNN_LIBRARY} \ + "-DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR}" \ + "${OPT_MKLDNN_INCLUDE_DIR}" \ + "${OPT_MKLDNN_LIBRARY}" \ -DLINK_STATIC_LIBGCC=${ARG_LINK_STATIC_LIBGCC} \ -DLINK_STATIC_LIBSTDCXX=${ARG_LINK_STATIC_LIBSTDCXX} \ -DLINK_STATIC_LIBPROTOBUF=${ARG_LINK_STATIC_LIBPROTOBUF} \ -DENABLE_TEST=ON \ - ${ARG_SOURCE_DIR} + "${ARG_SOURCE_DIR}" make diff --git a/.travis/build-mkldnn.sh b/scripts/build-mkldnn.sh old mode 100644 new mode 100755 similarity index 76% rename from .travis/build-mkldnn.sh rename to scripts/build-mkldnn.sh index 0277c69..52ed504 --- a/.travis/build-mkldnn.sh +++ b/scripts/build-mkldnn.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # retrieve arguments while [[ $# != 0 ]]; do @@ -42,30 +42,31 @@ while [[ $# != 0 ]]; do done # validate the arguments -test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } + +# options that have default value +test -n "${ARG_VERSION}" || readonly ARG_VERSION=0.16 readonly LIBRARY_NAME=mkl-dnn-${ARG_VERSION} -readonly SOURCE_DIR=${ARG_EXTRACT_DIR}/${LIBRARY_NAME} +readonly SOURCE_DIR="${ARG_EXTRACT_DIR}/${LIBRARY_NAME}" -# options that have default value -test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${SOURCE_DIR}/build +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}/build" +test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 # download (if it isn't cached) if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then echo -e "\e[33;1mDownloading libmkldnn\e[0m" - [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} + [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p "${ARG_DOWNLOAD_DIR}" - cd ${ARG_DOWNLOAD_DIR} + cd "${ARG_DOWNLOAD_DIR}" if [ ! -e "${LIBRARY_NAME}.tar.gz" ]; then download_url="https://github.com/intel/mkl-dnn/archive/v${ARG_VERSION}.tar.gz" wget -O ${LIBRARY_NAME}.tar.gz ${download_url} fi - tar -zxf ${LIBRARY_NAME}.tar.gz -C ${ARG_EXTRACT_DIR} + tar -zxf ${LIBRARY_NAME}.tar.gz -C "${ARG_EXTRACT_DIR}" echo -e "\e[32;1mlibmkldnn was successfully downloaded.\e[0m" else @@ -76,20 +77,20 @@ fi if [ ! -e "${ARG_BUILD_DIR}/src/libmkldnn.so" ]; then echo -e "\e[33;1mBuilding libmkldnn\e[0m" - cd ${SOURCE_DIR}/scripts + cd "${SOURCE_DIR}/scripts" ./prepare_mkl.sh - [ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} + [ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" - cd ${ARG_BUILD_DIR} + cd "${ARG_BUILD_DIR}" cmake \ -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR} \ + "-DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR}" \ -DWITH_TEST=OFF \ -DWITH_EXAMPLE=OFF \ -DARCH_OPT_FLAGS='' \ -Wno-error=unused-result \ - ${SOURCE_DIR} + "${SOURCE_DIR}" make -j${ARG_PARALLEL} echo -e "\e[32;1mlibmkldnn was successfully built.\e[0m" diff --git a/.travis/build-protobuf.sh b/scripts/build-protobuf.sh old mode 100644 new mode 100755 similarity index 75% rename from .travis/build-protobuf.sh rename to scripts/build-protobuf.sh index aee877c..cb668b9 --- a/.travis/build-protobuf.sh +++ b/scripts/build-protobuf.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # retrieve arguments while [[ $# != 0 ]]; do @@ -42,30 +42,31 @@ while [[ $# != 0 ]]; do done # validate the arguments -test -n "${ARG_VERSION}" || { echo "--version is not specified" 1>&2; exit 1; } test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_INSTALL_DIR}" || { echo "--install-dir is not specified" 1>&2; exit 1; } + +# options that have default value +test -n "${ARG_VERSION}" || readonly ARG_VERSION=3.6.1 readonly LIBRARY_NAME=protobuf-${ARG_VERSION} -readonly SOURCE_DIR=${ARG_EXTRACT_DIR}/${LIBRARY_NAME} +readonly SOURCE_DIR="${ARG_EXTRACT_DIR}/${LIBRARY_NAME}" -# options that have default value -test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR=${SOURCE_DIR} +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}" +test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 # download (if it isn't cached) if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then echo -e "\e[33;1mDownloading libprotobuf\e[0m" - [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p ${ARG_DOWNLOAD_DIR} + [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p "${ARG_DOWNLOAD_DIR}" - cd ${ARG_DOWNLOAD_DIR} + cd "${ARG_DOWNLOAD_DIR}" if [ ! -e "protobuf-cpp-${ARG_VERSION}.tar.gz" ]; then download_dir="https://github.com/protocolbuffers/protobuf/releases/download/v${ARG_VERSION}/protobuf-cpp-${ARG_VERSION}.tar.gz" wget ${download_dir} fi - tar -zxf protobuf-cpp-${ARG_VERSION}.tar.gz -C ${ARG_EXTRACT_DIR} + tar -zxf protobuf-cpp-${ARG_VERSION}.tar.gz -C "${ARG_EXTRACT_DIR}" echo -e "\e[32;1mlibprotobuf was successfully downloaded.\e[0m" else @@ -76,10 +77,10 @@ fi if [ ! -e "${ARG_BUILD_DIR}/src/libprotobuf.la" ]; then echo -e "\e[33;1mBuilding libprotobuf\e[0m" - [ -d "${ARG_BUILD_DIR}" ] || mkdir -p ${ARG_BUILD_DIR} + [ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" - cd ${ARG_BUILD_DIR} - ${SOURCE_DIR}/configure --prefix=${ARG_INSTALL_DIR} CFLAGS=-fPIC CXXFLAGS=-fPIC + cd "${ARG_BUILD_DIR}" + "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS=-fPIC CXXFLAGS=-fPIC make -j${ARG_PARALLEL} echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" diff --git a/gen_test_data.py b/scripts/gen_test_data.py similarity index 100% rename from gen_test_data.py rename to scripts/gen_test_data.py diff --git a/.travis/install-mkldnn.sh b/scripts/install-mkldnn.sh old mode 100644 new mode 100755 similarity index 88% rename from .travis/install-mkldnn.sh rename to scripts/install-mkldnn.sh index 3024147..763cdec --- a/.travis/install-mkldnn.sh +++ b/scripts/install-mkldnn.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # retrieve arguments while [[ $# != 0 ]]; do @@ -32,7 +32,7 @@ test -n "${ARG_BUILD_DIR}" || { echo "--build-dir is not specified" 1>&2; exit 1 echo -e "\e[33;1mInstalling libmkldnn\e[0m" # install to ${DESTDIR}/${CMAKE_INSTALL_PREFIX} if it is specified -[ -n "${ARG_DESTDIR}" ] && export DESTDIR=${ARG_DESTDIR} +[ -n "${ARG_DESTDIR}" ] && export DESTDIR="${ARG_DESTDIR}" -cd ${ARG_BUILD_DIR} +cd "${ARG_BUILD_DIR}" make install/strip diff --git a/.travis/install-protobuf.sh b/scripts/install-protobuf.sh old mode 100644 new mode 100755 similarity index 88% rename from .travis/install-protobuf.sh rename to scripts/install-protobuf.sh index 131e0e3..c5f5fd0 --- a/.travis/install-protobuf.sh +++ b/scripts/install-protobuf.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # retrieve arguments while [[ $# != 0 ]]; do @@ -32,7 +32,7 @@ test -n "${ARG_BUILD_DIR}" || { echo "--build-dir is not specified" 1>&2; exit 1 echo -e "\e[33;1mInstalling libprotobuf\e[0m" # install to ${DESTDIR}/`--prefix` if it is specified -[ -n "${ARG_DESTDIR}" ] && export DESTDIR=${ARG_DESTDIR} +[ -n "${ARG_DESTDIR}" ] && export DESTDIR="${ARG_DESTDIR}" -cd ${ARG_BUILD_DIR} +cd "${ARG_BUILD_DIR}" make install diff --git a/.travis/prepare-menoh-data.sh b/scripts/prepare-menoh-data.sh old mode 100644 new mode 100755 similarity index 52% rename from .travis/prepare-menoh-data.sh rename to scripts/prepare-menoh-data.sh index e82bf44..9f99002 --- a/.travis/prepare-menoh-data.sh +++ b/scripts/prepare-menoh-data.sh @@ -1,4 +1,6 @@ -#!/bin/bash +#!/bin/bash -e + +BASE_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) # retrieve arguments while [[ $# != 0 ]]; do @@ -26,14 +28,14 @@ while [[ $# != 0 ]]; do esac done -# validate the arguments -test -n "${ARG_SOURCE_DIR}" || { echo "ARG_SOURCE_DIR can't be empty" 1>&2; exit 1; } -test -n "${ARG_PYTHON_EXECUTABLE}" || ARG_PYTHON_EXECUTABLE=python +# options that have default value +test -n "${ARG_SOURCE_DIR}" || readonly ARG_SOURCE_DIR="${BASE_DIR}/.." +test -n "${ARG_PYTHON_EXECUTABLE}" || readonly ARG_PYTHON_EXECUTABLE=python -echo -e "\e[33;1mPreparing data/ for Menoh\e[0m" +echo -e "\e[33;1mPreparing data for Menoh tests and examples\e[0m" -cd ${ARG_SOURCE_DIR} +cd "${ARG_SOURCE_DIR}" [ -d "data" ] || mkdir -p data -${ARG_PYTHON_EXECUTABLE} retrieve_data.py -${ARG_PYTHON_EXECUTABLE} gen_test_data.py +${ARG_PYTHON_EXECUTABLE} scripts/retrieve_data.py +${ARG_PYTHON_EXECUTABLE} scripts/gen_test_data.py diff --git a/retrieve_data.py b/scripts/retrieve_data.py similarity index 100% rename from retrieve_data.py rename to scripts/retrieve_data.py diff --git a/.travis/run-container.sh b/scripts/run-container.sh similarity index 100% rename from .travis/run-container.sh rename to scripts/run-container.sh From 7ee11204ab28720615672140da43384bccfc8a84 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Thu, 20 Sep 2018 16:54:45 +0900 Subject: [PATCH 14/53] add default options explicitly --- scripts/build-protobuf.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-protobuf.sh b/scripts/build-protobuf.sh index cb668b9..f694b8b 100755 --- a/scripts/build-protobuf.sh +++ b/scripts/build-protobuf.sh @@ -80,7 +80,7 @@ if [ ! -e "${ARG_BUILD_DIR}/src/libprotobuf.la" ]; then [ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" cd "${ARG_BUILD_DIR}" - "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS=-fPIC CXXFLAGS=-fPIC + "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS="-g -O2 -fPIC" CXXFLAGS="-g -O2 -fPIC" make -j${ARG_PARALLEL} echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" From b7d51fe8a9a851a37d5cd184f6a8dcf9f35bc3a2 Mon Sep 17 00:00:00 2001 From: Masahiro Sakai Date: Fri, 21 Sep 2018 11:47:35 +0900 Subject: [PATCH 15/53] fix multiple definition of protobuf symbols in menoh_test --- test/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8d302d8..506b0ac 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,11 @@ add_executable(menoh_test #vgg16.cpp ) -target_link_libraries(menoh_test gtest_main menoh_test_target ${PROTOBUF_LIBRARY}) + +if(LINK_STATIC_LIBPROTOBUF) + target_link_libraries(menoh_test gtest_main menoh_test_target) +else() + target_link_libraries(menoh_test gtest_main menoh_test_target ${PROTOBUF_LIBRARY}) +endif() add_test(NAME menoh_test COMMAND menoh_test) From 2a96c2c069d52f10a3b14009c5aaa9f1e93dca0d Mon Sep 17 00:00:00 2001 From: okdshin Date: Thu, 27 Sep 2018 12:40:07 +0900 Subject: [PATCH 16/53] add new APIs to construct model_data by hand - menoh_make_model_data() - menoh_model_data_add_new_node() - menoh_model_data_add_input_name_to_current_node() - menoh_model_data_add_output_name_to_current_node() - menoh_model_data_add_attribute_int_to_current_node() - menoh_model_data_add_attribute_float_to_current_node() - menoh_model_data_add_attribute_ints_to_current_node() - menoh_model_data_add_attribute_floats_to_current_node() - menoh_model_data_add_initializer() --- include/menoh/menoh.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/menoh/menoh.h b/include/menoh/menoh.h index d561a1b..c3d4841 100644 --- a/include/menoh/menoh.h +++ b/include/menoh/menoh.h @@ -121,7 +121,6 @@ menoh_error_code MENOH_API menoh_make_model_data_from_onnx_data_on_memory( menoh_error_code MENOH_API menoh_make_model_data(menoh_model_data_handle* dst_handle); /*! \brief Add a new parameter in model_data - * * \note Duplication of parameter_name is not allowed and it throws error. */ menoh_error_code MENOH_API menoh_model_data_add_parameter( From eac67905d2e6d674a1dbeb7088cc8acffefd7bfa Mon Sep 17 00:00:00 2001 From: okdshin Date: Tue, 18 Sep 2018 20:07:06 +0900 Subject: [PATCH 17/53] add array_profile --- menoh/array.hpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/menoh/array.hpp b/menoh/array.hpp index 2bb70bc..9b9ce2c 100644 --- a/menoh/array.hpp +++ b/menoh/array.hpp @@ -11,6 +11,21 @@ namespace menoh_impl { + class array_profile { + public: + array_profile() = default; + + array_profile(dtype_t dtype, std::vector const& dims) + : dtype_(dtype), dims_(dims) {} + + dtype_t dtype() const { return dtype_; } + auto const& dims() const { return dims_; } + + private: + dtype_t dtype_ = dtype_t::undefined; + std::vector dims_; + }; + class array { public: array() = default; @@ -22,14 +37,25 @@ namespace menoh_impl { array(dtype_t d, std::vector const& dims); + array(array_profile const& profile, void* data_handle) + : array(profile.dtype(), profile.dims(), data_handle) {} + + array(array_profile const& profile, std::shared_ptr const& data) + : array(profile.dtype(), profile.dims(), data) {} + + explicit array(array_profile const& profile) + : array(profile.dtype(), profile.dims()) {} + dtype_t dtype() const { return dtype_; } auto const& dims() const { return dims_; } + auto* data() const { return data_handle_; } bool has_ownership() const { return static_cast(data_); } private: dtype_t dtype_ = dtype_t::undefined; std::vector dims_; + std::shared_ptr data_; void* data_handle_ = nullptr; }; From 96f00d3a7f599dd5c1fc5e1f08eec8c75e043ac1 Mon Sep 17 00:00:00 2001 From: okdshin Date: Tue, 18 Sep 2018 20:11:03 +0900 Subject: [PATCH 18/53] deprecated add_output_profile and add add_output_name --- include/menoh/menoh.h | 13 ++++++++++++- menoh/menoh.cpp | 34 +++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/include/menoh/menoh.h b/include/menoh/menoh.h index c3d4841..5ec3a8a 100644 --- a/include/menoh/menoh.h +++ b/include/menoh/menoh.h @@ -232,11 +232,22 @@ menoh_variable_profile_table_builder_add_input_profile_dims_4( menoh_dtype dtype, int32_t num, int32_t channel, int32_t height, int32_t width); -/*! \brief Add output profile +/*! \brief Add output name + * + * dims amd dtype of output are calculated automatically + * when calling of menoh_build_variable_profile_table. + */ +menoh_error_code MENOH_API menoh_variable_profile_table_builder_add_output_name( + menoh_variable_profile_table_builder_handle builder, const char* name); + +/*! \brief [DEPRECATED] Add output profile * * Output profile contains name and dtype. Its dims are calculated automatically * when calling of menoh_build_variable_profile_table. */ +MENOH_DEPRECATED_ATTRIBUTE( + "please use menoh_variable_profile_table_builder_add_output_name() instead. " + "dtype is totally ignored.") menoh_error_code MENOH_API menoh_variable_profile_table_builder_add_output_profile( menoh_variable_profile_table_builder_handle builder, const char* name, diff --git a/menoh/menoh.cpp b/menoh/menoh.cpp index dfc56d5..dde02c8 100644 --- a/menoh/menoh.cpp +++ b/menoh/menoh.cpp @@ -10,6 +10,7 @@ #include +#include #include #include #include @@ -272,37 +273,44 @@ menoh_error_code menoh_variable_profile_table_builder_add_input_profile_dims_4( int32_t width) { return check_error([&]() { std::vector dims = {num, channel, height, width}; - builder->input_name_and_dtype_and_dims_list.push_back( - std::make_tuple(std::string(name), dtype, dims)); + builder->input_name_and_profile_list.emplace_back( + std::string(name), menoh_impl::array_profile( + static_cast(dtype), dims)); return menoh_error_code_success; }); } -menoh_error_code menoh_variable_profile_table_builder_add_output_profile( - menoh_variable_profile_table_builder_handle builder, const char* name, - menoh_dtype dtype) { +menoh_error_code menoh_variable_profile_table_builder_add_output_name( + menoh_variable_profile_table_builder_handle builder, const char* name) { return check_error([&]() { - auto found = std::find_if( - builder->output_name_and_dtype_list.begin(), - builder->output_name_and_dtype_list.end(), - [name](auto const& t) { return name == std::get<0>(t); }); - if(found != builder->output_name_and_dtype_list.end()) { + auto found = std::find(builder->required_output_name_list.begin(), + builder->required_output_name_list.end(), + std::string(name)); + if(found != builder->required_output_name_list.end()) { auto message = std::string("menoh same named variable already exist: ") + name; menoh_impl::set_last_error_message(message.c_str()); return menoh_error_code_same_named_variable_already_exist; } - builder->output_name_and_dtype_list.push_back( - std::make_tuple(std::string(name), dtype)); + builder->required_output_name_list.emplace_back(name); return menoh_error_code_success; }); } +/* + * deprecated. dtype is totally ignored. + */ +menoh_error_code menoh_variable_profile_table_builder_add_output_profile( + menoh_variable_profile_table_builder_handle builder, const char* name, + menoh_dtype dtype) { + return menoh_variable_profile_table_builder_add_output_name(builder, name); +} + /* * variable_profile_table */ struct menoh_variable_profile_table { - std::unordered_map>> + std::unordered_map input_profile_table; std::unordered_map>> output_profile_table; From c6fe6b2656783d6eecc45a95b9058fcf79e13454 Mon Sep 17 00:00:00 2001 From: okdshin Date: Tue, 18 Sep 2018 20:11:36 +0900 Subject: [PATCH 19/53] modify implementations to use array_profile and context to take output_profile_table --- menoh/menoh.cpp | 150 ++++++++---------- .../backend/generic/generic_context.cpp | 2 + .../backend/generic/generic_context.hpp | 2 + .../backend/mkldnn/mkldnn_context.cpp | 4 +- .../backend/mkldnn/mkldnn_context.hpp | 4 +- .../mkldnn_with_generic_fallback/context.hpp | 7 +- .../model_core.cpp | 5 +- .../model_core.hpp | 2 + menoh/model.cpp | 71 --------- menoh/model.hpp | 43 ----- menoh/model_core_factory.cpp | 10 +- menoh/model_core_factory.hpp | 13 +- test/CMakeLists.txt | 2 - test/mkldnn.cpp | 104 ------------ test/model.cpp | 45 ------ 15 files changed, 101 insertions(+), 363 deletions(-) delete mode 100644 menoh/model.cpp delete mode 100644 menoh/model.hpp delete mode 100644 test/mkldnn.cpp delete mode 100644 test/model.cpp diff --git a/menoh/menoh.cpp b/menoh/menoh.cpp index dde02c8..ecc035d 100644 --- a/menoh/menoh.cpp +++ b/menoh/menoh.cpp @@ -227,10 +227,9 @@ menoh_error_code MENOH_API menoh_model_data_add_parameter( * variable_profile_table_builder */ struct menoh_variable_profile_table_builder { - std::vector>> - input_name_and_dtype_and_dims_list; - std::vector> - output_name_and_dtype_list; + std::vector> + input_name_and_profile_list; + std::vector required_output_name_list; }; menoh_error_code menoh_make_variable_profile_table_builder( @@ -250,9 +249,10 @@ menoh_error_code menoh_variable_profile_table_builder_add_input_profile( menoh_variable_profile_table_builder_handle builder, const char* name, menoh_dtype dtype, int32_t dims_size, const int32_t* dims) { return check_error([&]() { - builder->input_name_and_dtype_and_dims_list.push_back( - std::make_tuple(std::string(name), dtype, - std::vector(dims, dims + dims_size))); + builder->input_name_and_profile_list.emplace_back( + std::string(name), menoh_impl::array_profile( + static_cast(dtype), + std::vector(dims, dims + dims_size))); return menoh_error_code_success; }); } @@ -262,8 +262,9 @@ menoh_error_code menoh_variable_profile_table_builder_add_input_profile_dims_2( menoh_dtype dtype, int32_t num, int32_t size) { return check_error([&]() { std::vector dims = {num, size}; - builder->input_name_and_dtype_and_dims_list.push_back( - std::make_tuple(std::string(name), dtype, dims)); + builder->input_name_and_profile_list.emplace_back( + std::string(name), menoh_impl::array_profile( + static_cast(dtype), dims)); return menoh_error_code_success; }); } @@ -312,8 +313,9 @@ menoh_error_code menoh_variable_profile_table_builder_add_output_profile( struct menoh_variable_profile_table { std::unordered_map input_profile_table; - std::unordered_map>> + std::unordered_map output_profile_table; + std::vector required_output_name_list; }; menoh_error_code menoh_build_variable_profile_table( @@ -321,49 +323,41 @@ menoh_error_code menoh_build_variable_profile_table( const menoh_model_data_handle model_data, menoh_variable_profile_table_handle* dst_handle) { return check_error([&]() { - std::unordered_map>> - input_profile_table; - std::transform( - builder->input_name_and_dtype_and_dims_list.begin(), - builder->input_name_and_dtype_and_dims_list.end(), - std::inserter(input_profile_table, input_profile_table.end()), - [](auto const& t) { - return std::make_pair( - std::get<0>(t), - std::make_tuple(std::get<1>(t), std::get<2>(t))); - }); + std::unordered_map + input_profile_table(builder->input_name_and_profile_list.begin(), + builder->input_name_and_profile_list.end()); + // FIXME BEGIN dtype inference is also needed + // currently dtype is fixed to float std::vector>> input_name_and_dims_pair_list; - std::transform( - builder->input_name_and_dtype_and_dims_list.begin(), - builder->input_name_and_dtype_and_dims_list.end(), - std::back_inserter(input_name_and_dims_pair_list), [](auto const& t) { - return std::make_pair(std::get<0>(t), std::get<2>(t)); - }); + std::transform(builder->input_name_and_profile_list.begin(), + builder->input_name_and_profile_list.end(), + std::back_inserter(input_name_and_dims_pair_list), + [](auto const& p) { + return std::make_pair(p.first, p.second.dims()); + }); auto output_dims_table = menoh_impl::make_output_dims_table( model_data->model_data, input_name_and_dims_pair_list); - std::unordered_map>> + std::unordered_map output_profile_table; std::transform( - builder->output_name_and_dtype_list.begin(), - builder->output_name_and_dtype_list.end(), + output_dims_table.begin(), output_dims_table.end(), std::inserter(output_profile_table, output_profile_table.end()), - [&output_dims_table](auto const& t) { - std::string name; - menoh_dtype dtype; - std::tie(name, dtype) = t; + [](auto const& p) { + // here fixed to float return std::make_pair( - name, std::make_tuple(dtype, menoh_impl::find_value( - output_dims_table, name))); + p.first, menoh_impl::array_profile( + static_cast(menoh_dtype_float), + p.second)); }); + // FIXME END *dst_handle = std::make_unique( menoh_variable_profile_table{std::move(input_profile_table), - std::move(output_profile_table)}) + std::move(output_profile_table), + builder->required_output_name_list}) .release(); return menoh_error_code_success; }); @@ -405,18 +399,16 @@ menoh_error_code menoh_variable_profile_table_get_dtype( const menoh_variable_profile_table_handle variable_profile_table, const char* name, menoh_dtype* dst_dtype) { return impl::menoh_variable_profile_table_get_variable_attribute( - variable_profile_table, name, - [&](std::tuple> const& t) { - *dst_dtype = std::get<0>(t); + variable_profile_table, name, [&](auto const& profile) { + *dst_dtype = static_cast(profile.dtype()); }); } menoh_error_code menoh_variable_profile_table_get_dims_size( const menoh_variable_profile_table_handle variable_profile_table, const char* name, int32_t* dst_size) { return impl::menoh_variable_profile_table_get_variable_attribute( - variable_profile_table, name, - [&](std::tuple> const& t) { - *dst_size = static_cast(std::get<1>(t).size()); + variable_profile_table, name, [&](auto const& profile) { + *dst_size = static_cast(profile.dims().size()); }); } menoh_error_code menoh_variable_profile_table_get_dims_at( @@ -424,9 +416,7 @@ menoh_error_code menoh_variable_profile_table_get_dims_at( const char* name, int32_t index, int32_t* dst_size) { return impl::menoh_variable_profile_table_get_variable_attribute( variable_profile_table, name, - [&](std::tuple> const& t) { - *dst_size = std::get<1>(t).at(index); - }); + [&](auto const& profile) { *dst_size = profile.dims().at(index); }); } menoh_error_code menoh_model_data_optimize( @@ -449,10 +439,11 @@ menoh_error_code menoh_model_data_optimize( * model builder */ struct menoh_model_builder { - std::unordered_map>> + std::unordered_map input_profile_table; - std::unordered_map>> + std::unordered_map output_profile_table; + std::vector required_output_name_list; std::unordered_map external_buffer_handle_table; }; @@ -460,12 +451,13 @@ menoh_error_code menoh_make_model_builder( const menoh_variable_profile_table_handle variable_profile_table, menoh_model_builder_handle* dst_handle) { return check_error([&]() { - *dst_handle = - std::make_unique( - menoh_model_builder{variable_profile_table->input_profile_table, - variable_profile_table->output_profile_table, - {}}) - .release(); + *dst_handle = std::make_unique( + menoh_model_builder{ + variable_profile_table->input_profile_table, + variable_profile_table->output_profile_table, + variable_profile_table->required_output_name_list, + {}}) + .release(); return menoh_error_code_success; }); } @@ -511,54 +503,44 @@ menoh_error_code menoh_build_model(const menoh_model_builder_handle builder, std::unordered_map input_table; for(auto p : builder->input_profile_table) { std::string name; - std::tuple> t; - std::tie(name, t) = p; - menoh_dtype dtype; - std::vector dims; - std::tie(dtype, dims) = t; + menoh_impl::array_profile profile; + std::tie(name, profile) = p; auto buff = builder->external_buffer_handle_table.find(name); if(buff == builder->external_buffer_handle_table.end()) { - input_table.insert( - {name, menoh_impl::array( - static_cast(dtype), dims)}); + input_table.emplace(name, menoh_impl::array(profile)); } else { - input_table.insert( - {name, - menoh_impl::array(static_cast(dtype), - dims, buff->second)}); + input_table.emplace(name, + menoh_impl::array(profile, buff->second)); } } - std::unordered_map output_table; - for(auto p : builder->output_profile_table) { + std::unordered_map + required_output_table; + for(auto const& required_output_name : + builder->required_output_name_list) { + auto p = *builder->output_profile_table.find(required_output_name); std::string name; - std::tuple> t; - std::tie(name, t) = p; - menoh_dtype dtype; - std::vector dims; - std::tie(dtype, dims) = t; + menoh_impl::array_profile profile; + std::tie(name, profile) = p; auto buff = builder->external_buffer_handle_table.find(name); if(buff == builder->external_buffer_handle_table.end()) { - output_table.insert( - {name, menoh_impl::array( - static_cast(dtype), dims)}); + required_output_table.emplace(name, menoh_impl::array(profile)); } else { - output_table.insert( - {name, - menoh_impl::array(static_cast(dtype), - dims, buff->second)}); + required_output_table.emplace( + name, menoh_impl::array(profile, buff->second)); } } *dst_model_handle = std::make_unique( - menoh_model{input_table, output_table, + menoh_model{input_table, required_output_table, menoh_impl::make_model_core( - input_table, output_table, model_data->model_data, + input_table, required_output_table, + builder->output_profile_table, model_data->model_data, backend_name, backend_config)}) .release(); return menoh_error_code_success; diff --git a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp index e810b35..0488321 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp +++ b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp @@ -18,6 +18,8 @@ namespace menoh_impl { std::unordered_map const& common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, diff --git a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp index 7a1bb2b..251e918 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp +++ b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp @@ -32,6 +32,8 @@ namespace menoh_impl { common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, diff --git a/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.cpp b/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.cpp index 243a9e3..14ea6b8 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.cpp +++ b/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.cpp @@ -22,6 +22,8 @@ namespace menoh_impl { std::unordered_map const& common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, @@ -157,5 +159,5 @@ namespace menoh_impl { } } // namespace mkldnn_backend - } // namespace mkldnn_with_generic_fallback_backend + } // namespace mkldnn_with_generic_fallback_backend } // namespace menoh_impl diff --git a/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.hpp b/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.hpp index 65fb382..493095b 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.hpp +++ b/menoh/mkldnn_with_generic_fallback/backend/mkldnn/mkldnn_context.hpp @@ -27,7 +27,7 @@ namespace menoh_impl { auto dims = ::menoh_impl::mkldnn_backend::extract_dims(found->second); auto variable_memory = found->second; // mutable - procedure copy_proc(nullptr); // mutable + procedure copy_proc(nullptr); // mutable if(dims.size() == 4) { auto format = static_cast( found->second.get_primitive_desc() @@ -68,6 +68,8 @@ namespace menoh_impl { common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, diff --git a/menoh/mkldnn_with_generic_fallback/context.hpp b/menoh/mkldnn_with_generic_fallback/context.hpp index f8aafbc..de56e1f 100644 --- a/menoh/mkldnn_with_generic_fallback/context.hpp +++ b/menoh/mkldnn_with_generic_fallback/context.hpp @@ -29,6 +29,8 @@ namespace menoh_impl { std::unordered_map const& common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, @@ -36,7 +38,8 @@ namespace menoh_impl { return do_process_node_list( context_name, current_index, node_list, common_parameter_table, common_input_table, - required_output_table, context_list, logger); + required_output_table, output_profile_table, context_list, + logger); } // for specialized optimization across backends @@ -57,6 +60,8 @@ namespace menoh_impl { std::unordered_map const& common_input_table, std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, std::vector< std::pair>> const& context_list, diff --git a/menoh/mkldnn_with_generic_fallback/model_core.cpp b/menoh/mkldnn_with_generic_fallback/model_core.cpp index a39c678..273d607 100644 --- a/menoh/mkldnn_with_generic_fallback/model_core.cpp +++ b/menoh/mkldnn_with_generic_fallback/model_core.cpp @@ -25,6 +25,8 @@ namespace menoh_impl { context_list, std::unordered_map const& input_table, std::unordered_map const& output_table, + std::unordered_map const& + output_profile_table, menoh_impl::model_data const& model_data, backend_config const& config) : menoh_impl::model_core(), @@ -72,7 +74,8 @@ namespace menoh_impl { context->process_node_list( context_name, current_index, graph.node_list(), common_parameter_table_, common_input_table_, - required_output_table_, context_list_, logger_.get()); + required_output_table_, output_profile_table, + context_list_, logger_.get()); // if succeeded processing, add procedures into // procedure_list diff --git a/menoh/mkldnn_with_generic_fallback/model_core.hpp b/menoh/mkldnn_with_generic_fallback/model_core.hpp index e4cc405..dc19dc4 100644 --- a/menoh/mkldnn_with_generic_fallback/model_core.hpp +++ b/menoh/mkldnn_with_generic_fallback/model_core.hpp @@ -24,6 +24,8 @@ namespace menoh_impl { context_list, std::unordered_map const& input_table, std::unordered_map const& output_table, + std::unordered_map const& + output_profile_table, menoh_impl::model_data const& model_data, backend_config const& config); private: diff --git a/menoh/model.cpp b/menoh/model.cpp deleted file mode 100644 index 24be8e9..0000000 --- a/menoh/model.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include - -#include - -namespace menoh_impl { - - model::model( - std::vector< - std::tuple, void*>> const& - input_name_and_dtype_and_dims_and_data_handle_list, - std::vector> const& - required_output_name_and_dtype_and_data_handle_list, - menoh_impl::model_data const& model_data, std::string const& backend_name, - backend_config const& config) { - std::vector>> - input_name_and_dims_pair_list; - for(auto const& t : - input_name_and_dtype_and_dims_and_data_handle_list) { - std::string input_name; - dtype_t dtype; - std::vector input_dims; - void* data_handle; - std::tie(input_name, dtype, input_dims, data_handle) = t; - assert(input_table_.find(input_name) == input_table_.end()); - if(data_handle) { - input_table_.insert( - {input_name, array(dtype, input_dims, data_handle)}); - } else { - input_table_.insert({input_name, array(dtype, input_dims)}); - } - input_name_and_dims_pair_list.push_back({input_name, input_dims}); - } - - auto output_dims_table = - make_output_dims_table(model_data, input_name_and_dims_pair_list); - - std::unordered_map output_table; - for(auto const& t : - required_output_name_and_dtype_and_data_handle_list) { - std::string output_name; - dtype_t dtype; - void* data_handle; - std::tie(output_name, dtype, data_handle) = t; - assert(output_table_.find(output_name) == output_table_.end()); - if(data_handle) { - output_table_.insert( - {output_name, - array(dtype, find_value(output_dims_table, output_name), - data_handle)}); - } else { - output_table_.insert( - {output_name, - array(dtype, find_value(output_dims_table, output_name))}); - } - } - - model_ = make_model_core(input_table_, output_table_, model_data, - backend_name, config); - } - - array const& model::input(std::string const& name) const { - return find_value(input_table_, name); - } - - array const& model::output(std::string const& name) const { - return find_value(output_table_, name); - } - - void model::run() { model_->run(); } - -} // namespace menoh_impl diff --git a/menoh/model.hpp b/menoh/model.hpp deleted file mode 100644 index 0664ef9..0000000 --- a/menoh/model.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef MENOH_MODEL_HPP -#define MENOH_MODEL_HPP - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace menoh_impl { - - class model { - public: - model( - std::vector< - std::tuple, void*>> const& - input_name_and_dtype_and_dims_and_data_handle_list, - std::vector> const& - required_output_name_and_dtype_and_data_handle_list, - menoh_impl::model_data const& model_data, std::string const& backend_name, - backend_config const& config = backend_config()); - - array const& input(std::string const& name) const; - - array const& output(std::string const& name) const; - - void run(); - - private: - std::unordered_map input_table_; - std::unordered_map output_table_; - std::unique_ptr model_; - }; - -} // namespace menoh_impl -#endif // MENOH_MODEL_HPP diff --git a/menoh/model_core_factory.cpp b/menoh/model_core_factory.cpp index 9d1a582..aa47dd4 100644 --- a/menoh/model_core_factory.cpp +++ b/menoh/model_core_factory.cpp @@ -11,13 +11,15 @@ namespace menoh_impl { std::unique_ptr make_model_core(std::unordered_map const& input_table, - std::unordered_map const& output_table, + std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, menoh_impl::model_data const& model_data, std::string const& backend_name, backend_config const& config) { if(backend_name == "mkldnn") { return std::make_unique( - mkldnn_backend::make_model_core(input_table, output_table, + mkldnn_backend::make_model_core(input_table, required_output_table, model_data, config)); } else if(backend_name == "mkldnn_with_generic_fallback") { using namespace mkldnn_with_generic_fallback_backend; @@ -32,8 +34,8 @@ namespace menoh_impl { generic_backend::generic_context>()); return std::make_unique< mkldnn_with_generic_fallback_backend::model_core>( - std::move(context_list), input_table, output_table, model_data, - config); + std::move(context_list), input_table, required_output_table, + output_profile_table, model_data, config); } throw invalid_backend_name(backend_name); diff --git a/menoh/model_core_factory.hpp b/menoh/model_core_factory.hpp index d249057..52037e7 100644 --- a/menoh/model_core_factory.hpp +++ b/menoh/model_core_factory.hpp @@ -22,12 +22,13 @@ namespace menoh_impl { struct model_data; - std::unique_ptr - make_model_core(std::unordered_map const& input_table, - std::unordered_map const& output_table, - menoh_impl::model_data const& model_data, - std::string const& backend_name, - backend_config const& config = backend_config()); + std::unique_ptr make_model_core( + std::unordered_map const& input_table, + std::unordered_map const& required_output_table, + std::unordered_map const& + output_profile_table, + menoh_impl::model_data const& model_data, std::string const& backend_name, + backend_config const& config = backend_config()); } // namespace menoh_impl diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dd301be..04f9813 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,10 +17,8 @@ add_executable(menoh_test node.cpp graph.cpp onnx.cpp - mkldnn.cpp operator.cpp - model.cpp mkldnn_with_generic_fallback_backend.cpp model_data.cpp #vgg16.cpp diff --git a/test/mkldnn.cpp b/test/mkldnn.cpp deleted file mode 100644 index 8799591..0000000 --- a/test/mkldnn.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -namespace menoh_impl { - namespace { - - class MKLDNNTest : public ::testing::Test {}; - - TEST_F(MKLDNNTest, run_onnx_model) { - static mkldnn::engine engine(mkldnn::engine::cpu, 0); - auto model_data = - make_model_data_from_onnx_file("../data/VGG16.onnx"); - // model_data = trim_redundant_nodes(model_data); - - auto input_name_list = extract_model_input_name_list(model_data); - if(input_name_list.size() != 1) { - throw std::runtime_error( - "VGG16 data is invalid: input name list size is " + - std::to_string(input_name_list.size())); - } - auto const& input_name = input_name_list.front(); - constexpr auto batch_size = 1; - constexpr auto channel_num = 3; - constexpr auto height = 224; - constexpr auto width = 224; - std::vector input_dims{batch_size, channel_num, height, width}; - array input_arr(dtype_t::float_, input_dims); - - auto output_name_list = extract_model_output_name_list(model_data); - if(output_name_list.size() != 1) { - throw std::runtime_error( - "VGG16 data is invalid: input name list size is " + - std::to_string(output_name_list.size())); - } - auto const& output_name = output_name_list.front(); - auto output_dims_table = - make_output_dims_table(model_data, {{input_name, input_dims}}); - array output_arr(dtype_t::float_, - find_value(output_dims_table, output_name)); - auto model = menoh_impl::mkldnn_backend::model_core( - { - {input_name, input_arr}, - }, - { - {output_name, output_arr}, - }, - model_data, engine); - model.run(); - auto max_i = - std::max_element(fbegin(output_arr), fend(output_arr)) - - fbegin(output_arr); - std::cout << "max_i " << max_i << std::endl; - } - - TEST_F(MKLDNNTest, make_mkldnn_model_with_invalid_backend_config) { - // Aliases to onnx's node input and output tensor name - auto conv1_1_in_name = "140326425860192"; - auto softmax_out_name = "140326200803680"; - - constexpr auto batch_size = 1; - constexpr auto channel_num = 3; - constexpr auto height = 224; - constexpr auto width = 224; - // category_num is 1000 - - std::vector input_dims{batch_size, channel_num, height, width}; - - // Load ONNX model data - auto model_data = - menoh_impl::make_model_data_from_onnx_file("../data/VGG16.onnx"); - - auto cpu_count = - mkldnn::engine::get_count(mkldnn::engine::kind::cpu); - - // Construct computation primitive list and memories - ASSERT_THROW( - menoh_impl::model( - {std::make_tuple( - conv1_1_in_name, menoh_impl::dtype_t::float_, input_dims, - nullptr)}, // table of input_name, dtype, input_dims and - // data_handle - {std::make_tuple( - softmax_out_name, menoh_impl::dtype_t::float_, - nullptr)}, // list of output names, dtypes and data_handles - model_data, - "mkldnn", "{\"cpu_id\":" + std::to_string(cpu_count + 2) + "}"), - menoh_impl::backend_error); - } - - } // namespace -} // namespace menoh_impl diff --git a/test/model.cpp b/test/model.cpp deleted file mode 100644 index 8509662..0000000 --- a/test/model.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include - -#include -#include -#include - -#include - -#include -#include - -namespace menoh_impl { - namespace { - class ModelTest : public ::testing::Test {}; - - TEST_F(ModelTest, make_model) { - // Aliases to onnx's node input and output tensor name - auto conv1_1_in_name = "140326425860192"; - auto softmax_out_name = "140326200803680"; - - constexpr auto batch_size = 1; - constexpr auto channel_num = 3; - constexpr auto height = 224; - constexpr auto width = 224; - // category_num is 1000 - - std::vector input_dims{batch_size, channel_num, height, width}; - - // Load ONNX model data - auto model_data = std::make_unique( - menoh_impl::make_model_data_from_onnx_file("../data/VGG16.onnx")); - - // Construct computation primitive list and memories - auto model = menoh_impl::model( - {std::make_tuple(conv1_1_in_name, dtype_t::float_, input_dims, - nullptr)}, - {std::make_tuple(softmax_out_name, dtype_t::float_, nullptr)}, - *model_data, "mkldnn"); - model_data.reset(); // delete model_data - - model.run(); - } - - } // namespace -} // namespace menoh_impl From fc3ec79e1b5fcd1e54b178c63620d7b785abf1e3 Mon Sep 17 00:00:00 2001 From: okdshin Date: Thu, 27 Sep 2018 10:58:06 +0900 Subject: [PATCH 20/53] update C++ API --- include/menoh/menoh.hpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/include/menoh/menoh.hpp b/include/menoh/menoh.hpp index e901f0b..b884971 100644 --- a/include/menoh/menoh.hpp +++ b/include/menoh/menoh.hpp @@ -280,13 +280,23 @@ namespace menoh { dims.size(), dims.data())); } - //! Add output profile. That profile contains name, dtype. - /*! dims of output is calculated automatically. + //! Add a name of required output + /*! dtype and dims of output are calculated automatically. */ - void add_output_profile(std::string const& name, dtype_t dtype) { + void add_output_name(std::string const& name) { MENOH_CPP_API_ERROR_CHECK( - menoh_variable_profile_table_builder_add_output_profile( - impl_.get(), name.c_str(), static_cast(dtype))); + menoh_variable_profile_table_builder_add_output_name( + impl_.get(), name.c_str())); + } + + //! Add output profile. That profile contains name and dtype + /*! dims of output are calculated automatically. + * \note This function is deprecated. Given dtype is totally ignored and + * inferenced by dtype of input. Use add_output_name() instead. + */ + [[deprecated("Use add_output_name() instead")]] + void add_output_profile(std::string const& name, dtype_t) { + add_output_name(name); } //! Factory function for variable_profile_table. From 9dac97a8926df67eae80d7d06f4fabb7087596ed Mon Sep 17 00:00:00 2001 From: okdshin Date: Thu, 27 Sep 2018 10:59:18 +0900 Subject: [PATCH 21/53] update benchmark and test code --- benchmark/vgg16_benchmark.cpp | 2 +- test/backend.hpp | 3 +-- test/mkldnn_with_generic_fallback_backend.cpp | 5 ++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/benchmark/vgg16_benchmark.cpp b/benchmark/vgg16_benchmark.cpp index 230edac..845b9f3 100644 --- a/benchmark/vgg16_benchmark.cpp +++ b/benchmark/vgg16_benchmark.cpp @@ -35,7 +35,7 @@ int main(int argc, char** argv) { menoh::variable_profile_table_builder vpt_builder; vpt_builder.add_input_profile(conv1_1_in_name, menoh::dtype_t::float_, {batch_size, 3, 224, 224}); - vpt_builder.add_output_profile(softmax_out_name, menoh::dtype_t::float_); + vpt_builder.add_output_name(softmax_out_name); auto vpt = vpt_builder.build_variable_profile_table(model_data); // Build model diff --git a/test/backend.hpp b/test/backend.hpp index 0f42985..f16c150 100644 --- a/test/backend.hpp +++ b/test/backend.hpp @@ -61,9 +61,8 @@ namespace menoh { std::vector data; std::tie(std::ignore, output_dims, data) = menoh_impl::load_np_array(output_filename); - dtype_t dtype = dtype_t::float_; // TODO other dtype true_output_table.insert({output_name, data}); - vpt_builder.add_output_profile(output_name, dtype); + vpt_builder.add_output_name(output_name); } auto vpt = vpt_builder.build_variable_profile_table(model_data); diff --git a/test/mkldnn_with_generic_fallback_backend.cpp b/test/mkldnn_with_generic_fallback_backend.cpp index cfe8c45..58540ce 100644 --- a/test/mkldnn_with_generic_fallback_backend.cpp +++ b/test/mkldnn_with_generic_fallback_backend.cpp @@ -69,15 +69,14 @@ namespace menoh { std::string output_name; std::tie(output_name, std::ignore) = gemm_true_output_filename; model_data.add_output_name_to_current_node(output_name); - dtype_t dtype = dtype_t::float_; // TODO other dtype - vpt_builder.add_output_profile(output_name, dtype); + vpt_builder.add_output_name(output_name); } { model_data.add_new_node("Relu"); model_data.add_input_name_to_current_node("gemm_out"); model_data.add_output_name_to_current_node("relu_out"); - vpt_builder.add_output_profile("relu_out", dtype_t::float_); + vpt_builder.add_output_name("relu_out"); } auto vpt = vpt_builder.build_variable_profile_table(model_data); From ee82a5d4d52427d440ae796178d6949dea16a1a9 Mon Sep 17 00:00:00 2001 From: okdshin Date: Mon, 24 Sep 2018 23:36:14 +0900 Subject: [PATCH 22/53] add gen_graph_completion_hpp.py --- gen_graph_completion_hpp.py | 320 ++++++++++++++++++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 gen_graph_completion_hpp.py diff --git a/gen_graph_completion_hpp.py b/gen_graph_completion_hpp.py new file mode 100644 index 0000000..dc46b12 --- /dev/null +++ b/gen_graph_completion_hpp.py @@ -0,0 +1,320 @@ + + +def make_completion_code(op_name, + attribute_list=[], + shape_inference_code=''' +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); +''', + preprocess="", + postprocess=""): + # attribute completion and definition + attribute_completion_code_list = [] + attribute_definition_list = [] + for attribute in attribute_list: + attr_name, attr_type, default_value = attribute + inner_code = '' + if default_value is None: + inner_code = ''' +assert(!"attribute not found: {attr_name}"); +'''.format(attr_name=attr_name) + else: + inner_code = ''' +node.attribute_table.emplace( + "{attr_name}", {default_value}); +'''.format(attr_name=attr_name, default_value=default_value) + + attribute_completion_code = ''' +{{ + auto found = node.attribute_table.find("{attr_name}"); + if(found == node.attribute_table.end()) {{ + {code} + }} +}} +'''.format(attr_name=attr_name, attr_type=attr_type, code=inner_code) + attribute_completion_code_list.append(attribute_completion_code) + + attribute_definition = ''' +auto {attr_name} = get<{attr_type}>(node.attribute_table.at("{attr_name}")); +'''.format(attr_name=attr_name, attr_type=attr_type) + attribute_definition_list.append(attribute_definition) + # end for + + template = ''' +if(node.op_type == "{op_name}") {{ + {preprocess} + {attribute_completion_code} + {postprocess} + {{ + {attribute_definition} + {shape_inference_code} + }} +}} +else +''' + return template.format( + op_name=op_name, + preprocess=preprocess, + attribute_definition="\n".join(attribute_definition_list), + shape_inference_code=shape_inference_code, + postprocess=postprocess, + attribute_completion_code="\n".join( + attribute_completion_code_list)) + + +def main(): + template = """ +#ifndef MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP +#define MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP + +#include + +#include +#include +#include +#include + +#include +#include + +namespace menoh_impl {{ + inline auto complete_attribute_and_inference_shape( + model_data& model_data, + std::unordered_map const& + input_profile_table) {{ + using ints = std::vector; + std::unordered_map variable_profile_table( + input_profile_table.begin(), input_profile_table.end()); + std::transform( + model_data.parameter_name_and_array_list.begin(), + model_data.parameter_name_and_array_list.end(), + std::inserter(variable_profile_table, + variable_profile_table.end()), + [](auto const& p){{ + return std::make_pair( + p.first, + array_profile(p.second.dtype(), p.second.dims())); }}); + auto profile_of = [&variable_profile_table](std::string const& name){{ + assert(variable_profile_table.find(name) == + variable_profile_table.end()); + return variable_profile_table.at(name); + }}; + auto dims_of = [&variable_profile_table, profile_of]( + std::string const& name){{ + return profile_of(name).dims(); + }}; + auto dtype_of = [&variable_profile_table, profile_of]( + std::string const& name){{ + return profile_of(name).dtype(); + }}; + auto ndims_of = [&dims_of](std::string const& parameter_name) {{ + return dims_of(parameter_name).size(); + }}; + auto add_variable_to_table = [&variable_profile_table]( + std::string const& name, + dtype_t dtype, ints const& dims){{ + variable_profile_table.emplace( + name, array_profile(dtype, dims)); + }}; + + auto graph = make_graph(model_data.node_list); // FIXME reorder nodes + model_data.node_list = graph.node_list(); + for(auto& node : model_data.node_list) {{ + auto input = [&node](int i){{ + return node.input_name_list.at(i); + }}; + auto output = [&node](int i){{ + return node.output_name_list.at(i); + }}; + {code} + {unsupported_operator} + }} + return variable_profile_table; + }} +}} // namespace menoh_impl + +#endif // MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP +""" + code_list = [] + code_list.append(make_completion_code("Abs")) + code_list.append(make_completion_code("Add")) + code_list.append( + make_completion_code("AveragePool", [ + ("count_include_pad", "int", "0"), + ("kernel_shape", "ints", None), + ("pads", "ints", "ints(2*(ndims_of(input(0))-2), 0)"), + ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct + ], ''' +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(0)).at(1), + kernel_shape, strides, pads)); +''', preprocess=''' +assert(2 <= ndims_of(input(0))); +''')) + code_list.append( + make_completion_code("BatchNorm", [ + ("epsilon", "float", "1.e-05f"), + ("momentum", "float", "0.9f"), + ("spatial", "int", "1"), + ])) + code_list.append( + make_completion_code("Concat", [ + ("axis", "int", None), + ], ''' +auto output_dims = dims_of(input(0)); +for(int i = 1; i < node.input_name_list.size(); ++i) { + // TODO dim check + output_dims.at(axis) += dims_of(input(i)).at(axis); +} +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); +''')) + code_list.append( + make_completion_code( + "Conv", [ + ("dilations", "ints", "ints(kernel_ndims, 1)"), + ("group", "int", "1"), + ("kernel_shape", "ints", "kernel_shape"), + ("pads", "ints", "ints(kernel_ndims*2, 0)"), + ("strides", "ints", "ints(kernel_ndims, 1)"), + ], ''' +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(1)).at(0), + kernel_shape, strides, pads)); +''', + preprocess=''' +auto kernel_ndims = ndims_of(input(1))-2; +auto weights_shape = dims_of(input(1)); +auto kernel_shape = ints(weights_shape.begin()+2, weights_shape.end()); +''')) + code_list.append( + make_completion_code( + "ConvTranspose", + [ + ("dilations", "ints", None), + ("group", "int", "1"), + ("kernel_shape", "ints", "kernel_shape"), + ("output_padding", "ints", None), + # ("output_shape", "ints", None), + # ("pads", "ints", None), + ("strides", "ints", "ints(kernel_ndims, 1)"), + ], ''' +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims_for_conv_transpose( + dims_of(input(0)), dims_of(input(1)).at(0), + kernel_shape, strides, get(node.attribute_table.at("pads")))); +''', + preprocess=''' +auto kernel_ndims = ndims_of(input(1))-2; +auto weights_shape = dims_of(input(1)); +auto kernel_shape = ints(weights_shape.begin()+2, weights_shape.end()); +''', + postprocess=''' +{ + auto found = node.attribute_table.find("output_shape"); + assert(!(found == node.attribute_table.end() && + node.attribute_table.find("pads") == node.attribute_table.end())); + if(found != node.attribute_table.end()) { + auto output_shape = get(found->second); + /* [dim0_begin, dim1_begin, ... , dim0_end, dim1_end, ..., ...] */ + ints pads(kernel_ndims*2, 0); + auto output_padding = + get(node.attribute_table.at("output_padding")); + auto strides = get(node.attribute_table.at("strides")); + auto input_profile = input_profile_table.at(input(0)); + ints input_size(input_profile.dims().begin()+2, + input_profile.dims().end()); + + for(int i = 0; i < kernel_ndims; ++i) { + auto total_padding = strides[i] * (input_size[i] - 1) + + output_padding[i] + kernel_shape[i] - output_shape[i]; + pads[i] = total_padding - (total_padding/2); + pads[i+kernel_ndims] = (total_padding/2); + } + + node.attribute_table["pads"] = pads; + } +} +''')) + code_list.append(make_completion_code("Elu", [("alpha", "float", "1.f")])) + code_list.append( + make_completion_code("FC", [], ''' +auto output_dims = ints({dims_of(input(0)).at(0), dims_of(input(1)).at(0)}); +add_variable_to_table(output(0), dtype_of(input(0)), + output_dims); +''')) + code_list.append( + make_completion_code("Gemm", [ + ("alpha", "float", "1.f"), + ("beta", "float", "1.f"), + ("transA", "int", "0"), + ("transB", "int", "0"), + ], ''' +auto a_dims = dims_of(input(0)); +assert(a_dims.size() == 2); +if(transA) { + std::swap(a_dims.at(0), a_dims.at(1)); +} + +auto b_dims = dims_of(input(1)); +assert(b_dims.size() == 2); +if(transB) { + std::swap(b_dims.at(0), b_dims.at(1)); +} + +auto output_dims = ints({a_dims.at(0), b_dims.at(1)}); +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); +''')) + code_list.append( + make_completion_code("LeakyRelu", [("alpha", "float", "0.01f")])) + code_list.append( + make_completion_code("LRN", [ + ("alpha", "float", "0.0001f"), + ("beta", "float", "0.75f"), + ("bias", "float", "1.0f"), + ("size", "float", None), + ])) + code_list.append( + make_completion_code("MaxPool", [ + ("kernel_shape", "ints", None), + ("pads", "ints", "ints(2*(ndims_of(input(0))-2), 0)"), + ("storage_order", "int", "0"), + ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct + ], ''' +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(0)).at(1), + kernel_shape, strides, pads)); +''')) + code_list.append(make_completion_code("Relu")) + code_list.append(make_completion_code("Softmax", [("axis", "int", "1")])) + code_list.append(make_completion_code("Sum")) + code_list.append(make_completion_code("Sqrt")) + code_list.append(make_completion_code("Tanh")) + code_list.append( + make_completion_code("Transpose", [ + ("perm", "ints", "perm"), + ], ''' +auto input_dims = dims_of(input(0)); +ints output_dims(input_dims.size()); +for(int i = 0; i < input_dims.size(); ++i) { + output_dims.at(i) = input_dims.at(perm.at(i)); +} +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); +''', preprocess=""" +ints perm(ndims_of(input(0))); +for(int i = 0; i < perm.size(); ++i) {{ + perm.at(i) = perm.size()-i-1; +}} +""")) + print(template.format(code="\n".join(code_list), unsupported_operator=''' +{ + throw unsupported_operator(node.op_type); +} +''')) + + +if __name__ == "__main__": + main() From 2c23b3030881c24b4808ab72c9282d4fd09afe23 Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 26 Sep 2018 20:54:01 +0900 Subject: [PATCH 23/53] replace make_output_dims_table --- ...bute_completion_and_shape_inference_hpp.py | 0 menoh/graph.cpp | 182 ------------------ menoh/graph.hpp | 5 - menoh/menoh.cpp | 29 +-- 4 files changed, 4 insertions(+), 212 deletions(-) rename gen_graph_completion_hpp.py => gen_attribute_completion_and_shape_inference_hpp.py (100%) diff --git a/gen_graph_completion_hpp.py b/gen_attribute_completion_and_shape_inference_hpp.py similarity index 100% rename from gen_graph_completion_hpp.py rename to gen_attribute_completion_and_shape_inference_hpp.py diff --git a/menoh/graph.cpp b/menoh/graph.cpp index 2a049f9..0f8789a 100644 --- a/menoh/graph.cpp +++ b/menoh/graph.cpp @@ -221,186 +221,4 @@ namespace menoh_impl { node_list.end()); } - std::unordered_map> make_output_dims_table( - menoh_impl::model_data const& model_data, - std::vector>> const& - input_name_and_dims_pair_list) { - - std::vector supported_operator_list{{"Abs", - "Elu", - "LeakyRelu", - "Relu", - "Sqrt", - "Tanh", - "AveragePool", - "Add", - "BatchNormalization", - "Concat", - "Conv", - "ConvTranspose", - "FC", - "Gemm", - "GlobalAveragePool", - "GlobalMaxPool", - "LRN", - "MaxPool", - "Softmax", - "Sum"}}; - - std::unordered_map> variable_dims_table( - input_name_and_dims_pair_list.begin(), - input_name_and_dims_pair_list.end()); - std::transform( - model_data.parameter_name_and_array_list.begin(), - model_data.parameter_name_and_array_list.end(), - std::inserter(variable_dims_table, variable_dims_table.end()), - [](auto const& p) { - return std::make_pair(p.first, p.second.dims()); - }); - auto graph = make_graph(model_data.node_list); - for(auto const& node : graph.node_list()) { - if(node.op_type == "Conv") { - auto weight_name = node.input_name_list.at(1); - auto output_channel_num = - get_output_channel_num_from_parameter_dims( - find_value(variable_dims_table, weight_name)); - auto output_dims = calc_2d_output_dims(node, output_channel_num, - variable_dims_table); - auto dilations = - optional_attribute_ints(node, "dilations", {1, 1}); - if(dilations != std::vector({1, 1})) { - auto actual = "(" + std::to_string(dilations.at(0)) + ", " + - std::to_string(dilations.at(1)) + ")"; - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "dilations", - actual, "(1, 1)"); - } - auto group = optional_attribute_int(node, "group", 1); - if(group != 1) { - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "group", - std::to_string(group), "1"); - } - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(node.op_type == "ConvTranspose") { - auto weight_name = node.input_name_list.at(1); - auto output_channel_num = - get_output_channel_num_from_parameter_dims( - find_value(variable_dims_table, weight_name)); - auto output_dims = calc_2d_output_dims_for_conv_transpose( - node, output_channel_num, variable_dims_table); - auto dilations = - optional_attribute_ints(node, "dilations", {1, 1}); - if(dilations != std::vector({1, 1})) { - auto actual = "(" + std::to_string(dilations.at(0)) + ", " + - std::to_string(dilations.at(1)) + ")"; - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "dilations", - actual, "(1, 1)"); - } - auto group = optional_attribute_int(node, "group", 1); - if(group != 1) { - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "group", - std::to_string(group), "1"); - } - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(node.op_type == "MaxPool" || - node.op_type == "AveragePool") { - auto input_name = node.input_name_list.at(0); - auto output_channel_num = get_channel_num_from_variable_dims( - find_value(variable_dims_table, input_name)); - if(node.op_type == "AveragePool") { - auto pads = optional_attribute_ints(node, "pads", {0, 0}); - auto count_include_pad = optional_attribute_int( - node, "count_include_pad", 1); // TODO - if(pads != std::vector({0, 0}) && - count_include_pad == 0) { - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), - "count_include_pad", - std::to_string(count_include_pad), "0"); - } - } - auto output_dims = calc_2d_output_dims(node, output_channel_num, - variable_dims_table); - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(node.op_type == "GlobalMaxPool" || - node.op_type == "GlobalAveragePool") { - auto input_name = node.input_name_list.at(0); - auto input_dims = find_value(variable_dims_table, input_name); - auto output_dims = input_dims; - output_dims.at(2) = 1; - output_dims.at(3) = 1; - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(node.op_type == "FC") { - auto input_name = node.input_name_list.at(0); - auto input_dims = find_value(variable_dims_table, input_name); - auto batch_size = get_batch_size_from_variable_dims( - find_value(variable_dims_table, input_name)); - auto weight_dims = - find_value(variable_dims_table, node.input_name_list.at(1)); - auto input_size = - std::accumulate(input_dims.begin() + 1, input_dims.end(), 1, - std::multiplies()); - if(input_size != weight_dims[1]) { - throw dimension_mismatch( - node.op_type, node.output_name_list.front(), - "input[1] and weight[1]", std::to_string(input_size), - std::to_string(weight_dims[1])); - } - std::vector output_dims{batch_size, weight_dims[0]}; - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(node.op_type == "Gemm") { - auto input_name = node.input_name_list.at(0); - auto input_dims = find_value(variable_dims_table, input_name); - auto batch_size = get_batch_size_from_variable_dims( - find_value(variable_dims_table, input_name)); - auto weight_dims = - find_value(variable_dims_table, node.input_name_list.at(1)); - auto trans_a = optional_attribute_int(node, "transA", 0); - if(trans_a) { - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "transA", - std::to_string(trans_a), "0"); - } - auto trans_b = optional_attribute_int(node, "transB", 0); - if(!trans_b) { - throw unsupported_operator_attribute( - node.op_type, node.output_name_list.front(), "transB", - std::to_string(trans_b), "1"); - } - auto input_size = - std::accumulate(input_dims.begin() + 1, input_dims.end(), 1, - std::multiplies()); - if(input_size != weight_dims[1]) { - throw dimension_mismatch( - node.op_type, node.output_name_list.front(), - "input[1] and weight[1]", std::to_string(input_size), - std::to_string(weight_dims[1])); - } - std::vector output_dims{batch_size, weight_dims[0]}; - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else if(std::find(supported_operator_list.begin(), - supported_operator_list.end(), - node.op_type) != - supported_operator_list - .end()) { // check if supported operator - auto input_name = node.input_name_list.at(0); - auto output_dims = find_value(variable_dims_table, input_name); - variable_dims_table.insert( - {node.output_name_list.at(0), output_dims}); - } else { - throw unsupported_operator(node.op_type); - } - } - return variable_dims_table; - } // namespace menoh_impl - } // namespace menoh_impl diff --git a/menoh/graph.hpp b/menoh/graph.hpp index 801a44d..7c7fe17 100644 --- a/menoh/graph.hpp +++ b/menoh/graph.hpp @@ -82,11 +82,6 @@ namespace menoh_impl { " valid value: " + valid_value) {} }; - std::unordered_map> make_output_dims_table( - menoh_impl::model_data const& model_data, - std::vector>> const& - input_dims_table); - } // namespace menoh_impl #endif // MENOH_GRAPH_HPP diff --git a/menoh/menoh.cpp b/menoh/menoh.cpp index ecc035d..3065e9f 100644 --- a/menoh/menoh.cpp +++ b/menoh/menoh.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -327,32 +328,10 @@ menoh_error_code menoh_build_variable_profile_table( input_profile_table(builder->input_name_and_profile_list.begin(), builder->input_name_and_profile_list.end()); - // FIXME BEGIN dtype inference is also needed - // currently dtype is fixed to float - std::vector>> - input_name_and_dims_pair_list; - std::transform(builder->input_name_and_profile_list.begin(), - builder->input_name_and_profile_list.end(), - std::back_inserter(input_name_and_dims_pair_list), - [](auto const& p) { - return std::make_pair(p.first, p.second.dims()); - }); - auto output_dims_table = menoh_impl::make_output_dims_table( - model_data->model_data, input_name_and_dims_pair_list); + auto output_profile_table = + menoh_impl::complete_attribute_and_inference_shape( + model_data->model_data, input_profile_table); - std::unordered_map - output_profile_table; - std::transform( - output_dims_table.begin(), output_dims_table.end(), - std::inserter(output_profile_table, output_profile_table.end()), - [](auto const& p) { - // here fixed to float - return std::make_pair( - p.first, menoh_impl::array_profile( - static_cast(menoh_dtype_float), - p.second)); - }); - // FIXME END *dst_handle = std::make_unique( menoh_variable_profile_table{std::move(input_profile_table), From 2af69e641efde2a24b7f62ac0c9c56ae060797ed Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 26 Sep 2018 21:02:28 +0900 Subject: [PATCH 24/53] remove global_max/average_pool test --- ...tribute_completion_and_shape_inference.hpp | 887 ++++++++++++++++++ ...bute_completion_and_shape_inference_hpp.py | 0 test/operator.cpp | 4 +- 3 files changed, 889 insertions(+), 2 deletions(-) create mode 100644 menoh/attribute_completion_and_shape_inference.hpp rename gen_attribute_completion_and_shape_inference_hpp.py => scripts/gen_attribute_completion_and_shape_inference_hpp.py (100%) diff --git a/menoh/attribute_completion_and_shape_inference.hpp b/menoh/attribute_completion_and_shape_inference.hpp new file mode 100644 index 0000000..1a6db14 --- /dev/null +++ b/menoh/attribute_completion_and_shape_inference.hpp @@ -0,0 +1,887 @@ + +#ifndef MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP +#define MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP + +#include + +#include +#include +#include +#include + +#include +#include + +namespace menoh_impl { + inline auto complete_attribute_and_inference_shape( + model_data& model_data, + std::unordered_map const& + input_profile_table) { + using ints = std::vector; + std::unordered_map variable_profile_table( + input_profile_table.begin(), input_profile_table.end()); + std::transform( + model_data.parameter_name_and_array_list.begin(), + model_data.parameter_name_and_array_list.end(), + std::inserter(variable_profile_table, + variable_profile_table.end()), + [](auto const& p){ + return std::make_pair( + p.first, + array_profile(p.second.dtype(), p.second.dims())); }); + auto profile_of = [&variable_profile_table](std::string const& name){ + assert(variable_profile_table.find(name) == + variable_profile_table.end()); + return variable_profile_table.at(name); + }; + auto dims_of = [&variable_profile_table, profile_of]( + std::string const& name){ + return profile_of(name).dims(); + }; + auto dtype_of = [&variable_profile_table, profile_of]( + std::string const& name){ + return profile_of(name).dtype(); + }; + auto ndims_of = [&dims_of](std::string const& parameter_name) { + return dims_of(parameter_name).size(); + }; + auto add_variable_to_table = [&variable_profile_table]( + std::string const& name, + dtype_t dtype, ints const& dims){ + variable_profile_table.emplace( + name, array_profile(dtype, dims)); + }; + + auto graph = make_graph(model_data.node_list); // FIXME reorder nodes + model_data.node_list = graph.node_list(); + for(auto& node : model_data.node_list) { + auto input = [&node](int i){ + return node.input_name_list.at(i); + }; + auto output = [&node](int i){ + return node.output_name_list.at(i); + }; + +if(node.op_type == "Abs") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Add") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "AveragePool") { + +assert(2 <= ndims_of(input(0))); + + +{ + auto found = node.attribute_table.find("count_include_pad"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "count_include_pad", 0); + + } +} + + +{ + auto found = node.attribute_table.find("kernel_shape"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: kernel_shape"); + + } +} + + +{ + auto found = node.attribute_table.find("pads"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "pads", ints(2*(ndims_of(input(0))-2), 0)); + + } +} + + +{ + auto found = node.attribute_table.find("strides"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "strides", ints(ndims_of(input(0))-2, 1)); + + } +} + + + { + +auto count_include_pad = get(node.attribute_table.at("count_include_pad")); + + +auto kernel_shape = get(node.attribute_table.at("kernel_shape")); + + +auto pads = get(node.attribute_table.at("pads")); + + +auto strides = get(node.attribute_table.at("strides")); + + +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(0)).at(1), + kernel_shape, strides, pads)); + + } +} +else + + +if(node.op_type == "BatchNorm") { + + +{ + auto found = node.attribute_table.find("epsilon"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "epsilon", 1.e-05f); + + } +} + + +{ + auto found = node.attribute_table.find("momentum"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "momentum", 0.9f); + + } +} + + +{ + auto found = node.attribute_table.find("spatial"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "spatial", 1); + + } +} + + + { + +auto epsilon = get(node.attribute_table.at("epsilon")); + + +auto momentum = get(node.attribute_table.at("momentum")); + + +auto spatial = get(node.attribute_table.at("spatial")); + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Concat") { + + +{ + auto found = node.attribute_table.find("axis"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: axis"); + + } +} + + + { + +auto axis = get(node.attribute_table.at("axis")); + + +auto output_dims = dims_of(input(0)); +for(int i = 1; i < node.input_name_list.size(); ++i) { + // TODO dim check + output_dims.at(axis) += dims_of(input(i)).at(axis); +} +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); + + } +} +else + + +if(node.op_type == "Conv") { + +auto kernel_ndims = ndims_of(input(1))-2; +auto weights_shape = dims_of(input(1)); +auto kernel_shape = ints(weights_shape.begin()+2, weights_shape.end()); + + +{ + auto found = node.attribute_table.find("dilations"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "dilations", ints(kernel_ndims, 1)); + + } +} + + +{ + auto found = node.attribute_table.find("group"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "group", 1); + + } +} + + +{ + auto found = node.attribute_table.find("kernel_shape"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "kernel_shape", kernel_shape); + + } +} + + +{ + auto found = node.attribute_table.find("pads"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "pads", ints(kernel_ndims*2, 0)); + + } +} + + +{ + auto found = node.attribute_table.find("strides"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "strides", ints(kernel_ndims, 1)); + + } +} + + + { + +auto dilations = get(node.attribute_table.at("dilations")); + + +auto group = get(node.attribute_table.at("group")); + + +auto kernel_shape = get(node.attribute_table.at("kernel_shape")); + + +auto pads = get(node.attribute_table.at("pads")); + + +auto strides = get(node.attribute_table.at("strides")); + + +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(1)).at(0), + kernel_shape, strides, pads)); + + } +} +else + + +if(node.op_type == "ConvTranspose") { + +auto kernel_ndims = ndims_of(input(1))-2; +auto weights_shape = dims_of(input(1)); +auto kernel_shape = ints(weights_shape.begin()+2, weights_shape.end()); + + +{ + auto found = node.attribute_table.find("dilations"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: dilations"); + + } +} + + +{ + auto found = node.attribute_table.find("group"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "group", 1); + + } +} + + +{ + auto found = node.attribute_table.find("kernel_shape"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "kernel_shape", kernel_shape); + + } +} + + +{ + auto found = node.attribute_table.find("output_padding"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: output_padding"); + + } +} + + +{ + auto found = node.attribute_table.find("strides"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "strides", ints(kernel_ndims, 1)); + + } +} + + +{ + auto found = node.attribute_table.find("output_shape"); + assert(!(found == node.attribute_table.end() && + node.attribute_table.find("pads") == node.attribute_table.end())); + if(found != node.attribute_table.end()) { + auto output_shape = get(found->second); + /* [dim0_begin, dim1_begin, ... , dim0_end, dim1_end, ..., ...] */ + ints pads(kernel_ndims*2, 0); + auto output_padding = + get(node.attribute_table.at("output_padding")); + auto strides = get(node.attribute_table.at("strides")); + auto input_profile = input_profile_table.at(input(0)); + ints input_size(input_profile.dims().begin()+2, + input_profile.dims().end()); + + for(int i = 0; i < kernel_ndims; ++i) { + auto total_padding = strides[i] * (input_size[i] - 1) + + output_padding[i] + kernel_shape[i] - output_shape[i]; + pads[i] = total_padding - (total_padding/2); + pads[i+kernel_ndims] = (total_padding/2); + } + + node.attribute_table["pads"] = pads; + } +} + + { + +auto dilations = get(node.attribute_table.at("dilations")); + + +auto group = get(node.attribute_table.at("group")); + + +auto kernel_shape = get(node.attribute_table.at("kernel_shape")); + + +auto output_padding = get(node.attribute_table.at("output_padding")); + + +auto strides = get(node.attribute_table.at("strides")); + + +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims_for_conv_transpose( + dims_of(input(0)), dims_of(input(1)).at(0), + kernel_shape, strides, get(node.attribute_table.at("pads")))); + + } +} +else + + +if(node.op_type == "Elu") { + + +{ + auto found = node.attribute_table.find("alpha"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "alpha", 1.f); + + } +} + + + { + +auto alpha = get(node.attribute_table.at("alpha")); + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "FC") { + + + + { + + +auto output_dims = ints({dims_of(input(0)).at(0), dims_of(input(1)).at(0)}); +add_variable_to_table(output(0), dtype_of(input(0)), + output_dims); + + } +} +else + + +if(node.op_type == "Gemm") { + + +{ + auto found = node.attribute_table.find("alpha"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "alpha", 1.f); + + } +} + + +{ + auto found = node.attribute_table.find("beta"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "beta", 1.f); + + } +} + + +{ + auto found = node.attribute_table.find("transA"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "transA", 0); + + } +} + + +{ + auto found = node.attribute_table.find("transB"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "transB", 0); + + } +} + + + { + +auto alpha = get(node.attribute_table.at("alpha")); + + +auto beta = get(node.attribute_table.at("beta")); + + +auto transA = get(node.attribute_table.at("transA")); + + +auto transB = get(node.attribute_table.at("transB")); + + +auto a_dims = dims_of(input(0)); +assert(a_dims.size() == 2); +if(transA) { + std::swap(a_dims.at(0), a_dims.at(1)); +} + +auto b_dims = dims_of(input(1)); +assert(b_dims.size() == 2); +if(transB) { + std::swap(b_dims.at(0), b_dims.at(1)); +} + +auto output_dims = ints({a_dims.at(0), b_dims.at(1)}); +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); + + } +} +else + + +if(node.op_type == "LeakyRelu") { + + +{ + auto found = node.attribute_table.find("alpha"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "alpha", 0.01f); + + } +} + + + { + +auto alpha = get(node.attribute_table.at("alpha")); + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "LRN") { + + +{ + auto found = node.attribute_table.find("alpha"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "alpha", 0.0001f); + + } +} + + +{ + auto found = node.attribute_table.find("beta"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "beta", 0.75f); + + } +} + + +{ + auto found = node.attribute_table.find("bias"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "bias", 1.0f); + + } +} + + +{ + auto found = node.attribute_table.find("size"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: size"); + + } +} + + + { + +auto alpha = get(node.attribute_table.at("alpha")); + + +auto beta = get(node.attribute_table.at("beta")); + + +auto bias = get(node.attribute_table.at("bias")); + + +auto size = get(node.attribute_table.at("size")); + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "MaxPool") { + + +{ + auto found = node.attribute_table.find("kernel_shape"); + if(found == node.attribute_table.end()) { + +assert(!"attribute not found: kernel_shape"); + + } +} + + +{ + auto found = node.attribute_table.find("pads"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "pads", ints(2*(ndims_of(input(0))-2), 0)); + + } +} + + +{ + auto found = node.attribute_table.find("storage_order"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "storage_order", 0); + + } +} + + +{ + auto found = node.attribute_table.find("strides"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "strides", ints(ndims_of(input(0))-2, 1)); + + } +} + + + { + +auto kernel_shape = get(node.attribute_table.at("kernel_shape")); + + +auto pads = get(node.attribute_table.at("pads")); + + +auto storage_order = get(node.attribute_table.at("storage_order")); + + +auto strides = get(node.attribute_table.at("strides")); + + +add_variable_to_table(output(0), dtype_of(input(0)), + calc_2d_output_dims( + dims_of(input(0)), dims_of(input(0)).at(1), + kernel_shape, strides, pads)); + + } +} +else + + +if(node.op_type == "Relu") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Softmax") { + + +{ + auto found = node.attribute_table.find("axis"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "axis", 1); + + } +} + + + { + +auto axis = get(node.attribute_table.at("axis")); + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Sum") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Sqrt") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Tanh") { + + + + { + + +assert(node.input_name_list.size() == 1); +assert(node.output_name_list.size() == 1); +add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); + + } +} +else + + +if(node.op_type == "Transpose") { + +ints perm(ndims_of(input(0))); +for(int i = 0; i < perm.size(); ++i) {{ + perm.at(i) = perm.size()-i-1; +}} + + +{ + auto found = node.attribute_table.find("perm"); + if(found == node.attribute_table.end()) { + +node.attribute_table.emplace( + "perm", perm); + + } +} + + + { + +auto perm = get(node.attribute_table.at("perm")); + + +auto input_dims = dims_of(input(0)); +ints output_dims(input_dims.size()); +for(int i = 0; i < input_dims.size(); ++i) { + output_dims.at(i) = input_dims.at(perm.at(i)); +} +add_variable_to_table(output(0), dtype_of(input(0)), output_dims); + + } +} +else + + +{ + throw unsupported_operator(node.op_type); +} + + } + return variable_profile_table; + } +} // namespace menoh_impl + +#endif // MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP + diff --git a/gen_attribute_completion_and_shape_inference_hpp.py b/scripts/gen_attribute_completion_and_shape_inference_hpp.py similarity index 100% rename from gen_attribute_completion_and_shape_inference_hpp.py rename to scripts/gen_attribute_completion_and_shape_inference_hpp.py diff --git a/test/operator.cpp b/test/operator.cpp index 53893ed..bef8ee3 100644 --- a/test/operator.cpp +++ b/test/operator.cpp @@ -199,8 +199,8 @@ namespace { // TEST_OP(mkldnn, test_conv_with_strides_padding, eps); // TEST_OP_SQUASH_DIMS(mkldnn, test_convtranspose, eps); // not found // TEST_OP(mkldnn, test_gemm_nobroadcast, eps); - TEST_OP(mkldnn, test_globalaveragepool, eps); - TEST_OP(mkldnn, test_globalmaxpool, eps); + //TEST_OP(mkldnn, test_globalaveragepool, eps); + //TEST_OP(mkldnn, test_globalmaxpool, eps); TEST_OP(mkldnn, test_maxpool_2d_default, eps); TEST_OP_SQUASH_DIMS(mkldnn, test_softmax_axis_1, eps); // TEST_OP_SQUASH_DIMS(mkldnn, test_sum_one_input, eps); From a6cff7e44b5be6bf55ad9186f1dff46b0ef28b34 Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 26 Sep 2018 21:54:27 +0900 Subject: [PATCH 25/53] add model_data_completion test --- test/model_data_completion.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/model_data_completion.cpp diff --git a/test/model_data_completion.cpp b/test/model_data_completion.cpp new file mode 100644 index 0000000..f02a540 --- /dev/null +++ b/test/model_data_completion.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include + +#include "common.hpp" +namespace { + + class ModelDataCompletionTest : public ::testing::Test {}; + + TEST_F(ModelDataCompletionTest, completion) { + menoh_impl::model_data model_data; + model_data.node_list.push_back( + menoh_impl::node{"Conv", {"x", "w"}, {"y"}, {}}); + std::unordered_map + input_profile_table; + input_profile_table.emplace( + "w", + menoh_impl::array_profile(menoh_impl::dtype_t::float_, {1, 1, 3, 3})); + menoh_impl::complete_model_data(model_data, input_profile_table); + auto const& node = model_data.node_list.at(0); + auto found = node.attribute_table.find("kernel_shape"); + ASSERT_NE(found, node.attribute_table.end()); + menoh_impl::assert_eq_list( + menoh_impl::get>(found->second), + std::vector({3, 3})); + } + +} // namespace From 1384e4199c0bb707dcf5dbc80e14322b0acfc054 Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 26 Sep 2018 23:25:57 +0900 Subject: [PATCH 26/53] add notification --- menoh/attribute_completion_and_shape_inference.hpp | 7 ++++--- ...attribute_completion_and_shape_inference_hpp.py | 14 ++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/menoh/attribute_completion_and_shape_inference.hpp b/menoh/attribute_completion_and_shape_inference.hpp index 1a6db14..9c98fe8 100644 --- a/menoh/attribute_completion_and_shape_inference.hpp +++ b/menoh/attribute_completion_and_shape_inference.hpp @@ -1,9 +1,10 @@ #ifndef MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP #define MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP - -#include - +/* + * This file is generated by gen_attribute_completion_and_shape_inference_hpp.py + * Do NOT modify this file directly + */ #include #include #include diff --git a/scripts/gen_attribute_completion_and_shape_inference_hpp.py b/scripts/gen_attribute_completion_and_shape_inference_hpp.py index dc46b12..8d7b9e3 100644 --- a/scripts/gen_attribute_completion_and_shape_inference_hpp.py +++ b/scripts/gen_attribute_completion_and_shape_inference_hpp.py @@ -1,3 +1,4 @@ +import os def make_completion_code(op_name, @@ -67,9 +68,10 @@ def main(): template = """ #ifndef MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP #define MENOH_ATTRIBUTE_COMPLETION_AND_SHAPE_INFERENCE_HPP - -#include - +/* + * This file is generated by {script_name} + * Do NOT modify this file directly + */ #include #include #include @@ -144,7 +146,7 @@ def main(): ("count_include_pad", "int", "0"), ("kernel_shape", "ints", None), ("pads", "ints", "ints(2*(ndims_of(input(0))-2), 0)"), - ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct + ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct # NOQA ], ''' add_variable_to_table(output(0), dtype_of(input(0)), calc_2d_output_dims( @@ -281,7 +283,7 @@ def main(): ("kernel_shape", "ints", None), ("pads", "ints", "ints(2*(ndims_of(input(0))-2), 0)"), ("storage_order", "int", "0"), - ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct + ("strides", "ints", "ints(ndims_of(input(0))-2, 1)"), # WORKAROUND: None is correct # NOQA ], ''' add_variable_to_table(output(0), dtype_of(input(0)), calc_2d_output_dims( @@ -309,7 +311,7 @@ def main(): perm.at(i) = perm.size()-i-1; }} """)) - print(template.format(code="\n".join(code_list), unsupported_operator=''' + print(template.format(script_name=os.path.basename(__file__), code="\n".join(code_list), unsupported_operator=''' { throw unsupported_operator(node.op_type); } From 6b7e0dce2cdbd9e0e987575b088eb67b162ab92e Mon Sep 17 00:00:00 2001 From: okdshin Date: Wed, 19 Sep 2018 15:37:23 +0900 Subject: [PATCH 27/53] simplify generic backend operator --- .../backend/generic/generic_context.cpp | 33 +++++++++++---- .../backend/generic/generic_context.hpp | 9 ++-- .../backend/generic/operator/relu.hpp | 41 +++++-------------- .../model_core.cpp | 2 +- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp index 0488321..2c3f964 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp +++ b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.cpp @@ -103,23 +103,40 @@ namespace menoh_impl { assert(is_found_from_other_context); } while(false); } + std::vector output_list; + for(auto const& output_name : node.output_name_list) { + auto found = required_output_table.find(output_name); + if(found == required_output_table.end()) { + // allocate new array by using profile + output_list.push_back( + array(output_profile_table.at(output_name))); + } else { + // use already allocated array + output_list.push_back(found->second); + } + } + procedure op_proc; - std::vector> new_outputs; try { auto factory = procedure_factory_table_.at(node.op_type); - std::tie(op_proc, new_outputs) = - factory.operator()(current_index, node_list, - input_list, required_output_table); - } catch(...) { break; } + op_proc = + factory.operator()(node, input_list, output_list); + } catch(std::exception const& e) { + *logger << e.what() << std::endl; + break; + } new_op_proc_list.push_back(op_proc); procedure_list.insert( procedure_list.end(), std::make_move_iterator(new_copy_procedure_list.begin()), std::make_move_iterator(new_copy_procedure_list.end())); - variable_table_.insert( - std::make_move_iterator(new_outputs.begin()), - std::make_move_iterator(new_outputs.end())); + + assert(node.output_name_list.size() == output_list.size()); + for(int i = 0; i < node.output_name_list.size(); ++i) { + variable_table_.emplace(node.output_name_list.at(i), + output_list.at(i)); + } } // when no nodes are processed diff --git a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp index 251e918..cdfda3f 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp +++ b/menoh/mkldnn_with_generic_fallback/backend/generic/generic_context.hpp @@ -45,10 +45,11 @@ namespace menoh_impl { return variable_table_.at(name); } - using procedure_factory = std::function>>( - int, std::vector const&, std::vector const&, - std::unordered_map const&)>; + using procedure_factory = std::function const&, // input list + std::vector const& // output list + )>; optional> try_to_get_input_from_common_table( std::string const& input_name, diff --git a/menoh/mkldnn_with_generic_fallback/backend/generic/operator/relu.hpp b/menoh/mkldnn_with_generic_fallback/backend/generic/operator/relu.hpp index 092f59e..98e258a 100644 --- a/menoh/mkldnn_with_generic_fallback/backend/generic/operator/relu.hpp +++ b/menoh/mkldnn_with_generic_fallback/backend/generic/operator/relu.hpp @@ -1,46 +1,27 @@ #ifndef MENOH_IMPL_MKLDNN_WITH_GENERIC_FALLBACK_BACKEND_BACKEND_GENERIC_OPERATOR_RELU_HPP #define MENOH_IMPL_MKLDNN_WITH_GENERIC_FALLBACK_BACKEND_BACKEND_GENERIC_OPERATOR_RELU_HPP +#include #include namespace menoh_impl { namespace mkldnn_with_generic_fallback_backend { namespace generic_backend { - inline std::tuple>> - make_relu(int node_index, std::vector const& node_list, - std::vector const& input_list, - std::unordered_map const& - required_output_table) { + inline procedure make_relu(node const& node, + std::vector const& input_list, + std::vector const& output_list) { assert(input_list.size() == 1); - auto const& node = node_list.at(node_index); + assert(output_list.size() == 1); - auto const& x_arr = input_list.at(0); - - auto found = - required_output_table.find(node.output_name_list.at(0)); - optional output_opt; - if(found == required_output_table.end()) { - output_opt = array(dtype_t::float_, - x_arr.dims()); // TODO check inplace-able - } else { - output_opt = - found->second; // output is required so not inplace-able - } - - auto procedure = [x_arr, output = *output_opt]() { - for(decltype(total_size(x_arr)) i = 0; - i < total_size(x_arr); ++i) { - fat(output, i) = std::max(fat(x_arr, i), 0.f); + auto procedure = [input = input_list.at(0), + output = output_list.at(0)]() { + for(decltype(total_size(input)) i = 0; + i < total_size(input); ++i) { + fat(output, i) = std::max(fat(input, i), 0.f); } }; - std::vector> outputs; - if(found == required_output_table.end()) { - outputs.push_back(std::pair( - node.output_name_list.at(0), *output_opt)); - } - return std::make_tuple(procedure, outputs); + return procedure; } } // namespace generic_backend diff --git a/menoh/mkldnn_with_generic_fallback/model_core.cpp b/menoh/mkldnn_with_generic_fallback/model_core.cpp index 273d607..027cfed 100644 --- a/menoh/mkldnn_with_generic_fallback/model_core.cpp +++ b/menoh/mkldnn_with_generic_fallback/model_core.cpp @@ -105,7 +105,7 @@ namespace menoh_impl { } // if any context can not process the node if(!is_found) { - *logger_ << "failed to interpret" + *logger_ << "failed to interpret: no contexts can interpret" << graph.node_list().at(current_index).op_type << "with all context"; throw unsupported_operator(node.op_type); From 465f1f1c44a727cefe3eda7893c1012b72e8b7cb Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 14:29:42 +0900 Subject: [PATCH 28/53] fix type int -> int32_t --- include/menoh/menoh.h | 2 +- menoh/menoh.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/menoh/menoh.h b/include/menoh/menoh.h index 5ec3a8a..f89f5e1 100644 --- a/include/menoh/menoh.h +++ b/include/menoh/menoh.h @@ -158,7 +158,7 @@ menoh_error_code MENOH_API menoh_model_data_add_attribute_float_to_current_node( */ menoh_error_code MENOH_API menoh_model_data_add_attribute_ints_to_current_node( menoh_model_data_handle model_data, const char* attribute_name, int32_t size, - const int* value); + const int32_t* value); /*! \brief Add a new float array attribute to latest added node in model_data * * \note Duplication of attribute_name is not allowed and it throws error. diff --git a/menoh/menoh.cpp b/menoh/menoh.cpp index ecc035d..9d832f0 100644 --- a/menoh/menoh.cpp +++ b/menoh/menoh.cpp @@ -188,7 +188,7 @@ menoh_error_code menoh_model_data_add_attribute_array_to_current_node( menoh_error_code MENOH_API menoh_model_data_add_attribute_ints_to_current_node( menoh_model_data_handle model_data, const char* attribute_name, int32_t size, - const int* value) { + const int32_t* value) { return menoh_model_data_add_attribute_array_to_current_node( model_data, attribute_name, size, value); } From 854cbef2b571ce6ae2bc9d4526c30c202daf6de6 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 14:46:44 +0900 Subject: [PATCH 29/53] update onnx version --- external/onnx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/onnx b/external/onnx index c7b6005..bae6333 160000 --- a/external/onnx +++ b/external/onnx @@ -1 +1 @@ -Subproject commit c7b60050ad6ae963bf2cc1d0eb3c32b07eb7eeed +Subproject commit bae6333e149a59a3faa9c4d9c44974373dcf5256 From 0a4fdecc44e0f810e916acaae7b134de391c246d Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 15:11:37 +0900 Subject: [PATCH 30/53] update onnx submodule and suported ONNX version --- include/menoh/menoh.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/menoh/menoh.h b/include/menoh/menoh.h index 5ec3a8a..a4629ba 100644 --- a/include/menoh/menoh.h +++ b/include/menoh/menoh.h @@ -11,7 +11,7 @@ #define MENOH_API #endif -#define MENOH_SUPPORTED_ONNX_OPSET_VERSION 7 +#define MENOH_SUPPORTED_ONNX_OPSET_VERSION 8 #ifndef MENOH_ERROR_MESSAGE_MAX_LENGTH #define MENOH_ERROR_MESSAGE_MAX_LENGTH 1024 From 1fb970231acc5125660c00278968adcb9c237cef Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 15:34:12 +0900 Subject: [PATCH 31/53] fix error message --- menoh/mkldnn_with_generic_fallback/model_core.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/menoh/mkldnn_with_generic_fallback/model_core.cpp b/menoh/mkldnn_with_generic_fallback/model_core.cpp index 027cfed..ad9b019 100644 --- a/menoh/mkldnn_with_generic_fallback/model_core.cpp +++ b/menoh/mkldnn_with_generic_fallback/model_core.cpp @@ -105,9 +105,9 @@ namespace menoh_impl { } // if any context can not process the node if(!is_found) { - *logger_ << "failed to interpret: no contexts can interpret" - << graph.node_list().at(current_index).op_type - << "with all context"; + *logger_ + << "failed to interpret: no contexts can interpret '" + << node.op_type << "'"; throw unsupported_operator(node.op_type); } } From 3722edcc03e952487da9fa7ceb666ce45ce79412 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 17:00:31 +0900 Subject: [PATCH 32/53] rename complete_attribute_and_inference_shape -> complete_attribute_and_infer_shape --- menoh/attribute_completion_and_shape_inference.hpp | 2 +- menoh/menoh.cpp | 2 +- scripts/gen_attribute_completion_and_shape_inference_hpp.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/menoh/attribute_completion_and_shape_inference.hpp b/menoh/attribute_completion_and_shape_inference.hpp index 9c98fe8..d7b07d9 100644 --- a/menoh/attribute_completion_and_shape_inference.hpp +++ b/menoh/attribute_completion_and_shape_inference.hpp @@ -14,7 +14,7 @@ #include namespace menoh_impl { - inline auto complete_attribute_and_inference_shape( + inline auto complete_attribute_and_infer_shape( model_data& model_data, std::unordered_map const& input_profile_table) { diff --git a/menoh/menoh.cpp b/menoh/menoh.cpp index 3065e9f..5e3bdb0 100644 --- a/menoh/menoh.cpp +++ b/menoh/menoh.cpp @@ -329,7 +329,7 @@ menoh_error_code menoh_build_variable_profile_table( builder->input_name_and_profile_list.end()); auto output_profile_table = - menoh_impl::complete_attribute_and_inference_shape( + menoh_impl::complete_attribute_and_infer_shape( model_data->model_data, input_profile_table); *dst_handle = diff --git a/scripts/gen_attribute_completion_and_shape_inference_hpp.py b/scripts/gen_attribute_completion_and_shape_inference_hpp.py index 8d7b9e3..e0ddf31 100644 --- a/scripts/gen_attribute_completion_and_shape_inference_hpp.py +++ b/scripts/gen_attribute_completion_and_shape_inference_hpp.py @@ -81,7 +81,7 @@ def main(): #include namespace menoh_impl {{ - inline auto complete_attribute_and_inference_shape( + inline auto complete_attribute_and_infer_shape( model_data& model_data, std::unordered_map const& input_profile_table) {{ From 8c3b685cfe8f7d6a859debe047477a70f8aef428 Mon Sep 17 00:00:00 2001 From: okdshin Date: Fri, 28 Sep 2018 21:43:02 +0900 Subject: [PATCH 33/53] add check code if MaxPool has multiple outputs when it has multiple outputs, mkldnn backend throws unsupported_operator error --- menoh/mkldnn/operator/pool.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/menoh/mkldnn/operator/pool.cpp b/menoh/mkldnn/operator/pool.cpp index 18f7fcf..8b6e8b3 100644 --- a/menoh/mkldnn/operator/pool.cpp +++ b/menoh/mkldnn/operator/pool.cpp @@ -9,6 +9,8 @@ #include #include +#include // for unsupported_operator error + namespace menoh_impl { namespace mkldnn_backend { @@ -94,6 +96,9 @@ namespace menoh_impl { variable_memory_table, std::unordered_map const& required_output_table, mkldnn::engine const& engine) { + if(node.output_name_list.size() != 1) { + throw unsupported_operator("MaxPool issuing multiple outputs"); + } return make_pool_primitive( node, variable_memory_table, required_output_table, engine); } From 08289fcf5b99e470e11eb09669ec13a57a4f811c Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Mon, 1 Oct 2018 10:29:14 +0900 Subject: [PATCH 34/53] add cross building support for protobuf --- scripts/build-boost.sh | 120 ++++++++++++++++++++++++++++++++++++++ scripts/build-menoh.sh | 4 +- scripts/build-protobuf.sh | 18 +++++- scripts/install-boost.sh | 35 +++++++++++ 4 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 scripts/build-boost.sh create mode 100644 scripts/install-boost.sh diff --git a/scripts/build-boost.sh b/scripts/build-boost.sh new file mode 100644 index 0000000..183247f --- /dev/null +++ b/scripts/build-boost.sh @@ -0,0 +1,120 @@ +#!/bin/bash -e + +# retrieve arguments +while [[ $# != 0 ]]; do + case $1 in + --) + shift + break + ;; + --version) + readonly ARG_VERSION="$2" + shift 2 + ;; + --download-dir) + readonly ARG_DOWNLOAD_DIR="$2" + shift 2 + ;; + --extract-dir) + readonly ARG_EXTRACT_DIR="$2" + shift 2 + ;; + --build-dir) + readonly ARG_BUILD_DIR="$2" + shift 2 + ;; + --stage-dir) + readonly ARG_STAGE_DIR="$2" + shift 2 + ;; + --install-dir) + readonly ARG_INSTALL_DIR="$2" + shift 2 + ;; + --toolset) + readonly ARG_TOOLSET="$2" + shift 2 + ;; + --cxx) + readonly ARG_CXX_CMD="$2" + shift 2 + ;; + --parallel) + readonly ARG_PARALLEL="$2" + shift 2 + ;; + -*) + echo Unknown option \"$1\" 1>&2 + exit + ;; + *) + break + ;; + esac +done + +# validate the arguments +test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } +test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } + +# options that have default value +test -n "${ARG_VERSION}" || readonly ARG_VERSION=1.64.0 + +readonly LIBRARY_NAME=boost_$(echo ${ARG_VERSION} | sed -e 's/\./_/g') # e.g. boost_1_64_0 +readonly SOURCE_DIR="${ARG_EXTRACT_DIR}/${LIBRARY_NAME}" + +test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}/build" +test -n "${ARG_STAGE_DIR}" || readonly ARG_STAGE_DIR="${SOURCE_DIR}/stage" +test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local +test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 + +# options for cross compiling +if [ -n "${ARG_TOOLSET}" ]; then # e.g. gcc-arm + # requires a location of g++ command + test -n "${ARG_CXX_CMD}" || { echo "--cxx is not specified" 1>&2; exit 1; } + + readonly USER_CONFIG_JAM="using $(echo ${ARG_TOOLSET} | sed -e 's/-/ : /') : ${ARG_CXX_CMD} ;" + readonly OPT_TOOLSET=toolset=${ARG_TOOLSET} +fi + +# download (if it isn't cached) +if [ ! -e "${SOURCE_DIR}/INSTALL" ]; then + echo -e "\e[33;1mDownloading libboost\e[0m" + + [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p "${ARG_DOWNLOAD_DIR}" + + cd "${ARG_DOWNLOAD_DIR}" + if [ ! -e "${LIBRARY_NAME}.tar.gz" ]; then + download_dir="https://dl.bintray.com/boostorg/release/${ARG_VERSION}/source/${LIBRARY_NAME}.tar.gz" + curl -LO ${download_dir} # wget doesn't work for bintray with 403 errpr + fi + tar -zxf ${LIBRARY_NAME}.tar.gz -C "${ARG_EXTRACT_DIR}" + + echo -e "\e[32;1mlibboost was successfully downloaded.\e[0m" +else + echo -e "\e[32;1mlibboost has been downloaded.\e[0m" +fi + +# build (if it isn't cached) +if [ ! -e "${ARG_STAGE_DIR}/lib/libboost_system.a" ]; then + echo -e "\e[33;1mBuilding libprotobuf\e[0m" + + cd "${SOURCE_DIR}" + + # bootstrap + ./bootstrap.sh --prefix="${ARG_INSTALL_DIR}" + + # configure options + if [ -n "${USER_CONFIG_JAM}" ]; then + echo "${USER_CONFIG_JAM}" > ${SOURCE_DIR}/user_config.jam + readonly OPT_USER_CONFIG_JAM=--user-config=${SOURCE_DIR}/user_config.jam + fi + echo "-j${ARG_PARALLEL} ${OPT_TOOLSET} ${OPT_USER_CONFIG_JAM} link=static cflags=-fPIC cxxflags=-fPIC --with-filesystem --with-test --with-log --with-program_options --build-dir=${ARG_BUILD_DIR} --stage-dir=${ARG_STAGE_DIR}" > b2_opts.txt + + # run a build + ./b2 stage $(cat b2_opts.txt) + + echo -e "\e[32;1mlibboost was successfully built.\e[0m" +else + echo -e "\e[32;1mlibboost has been built.\e[0m" +fi diff --git a/scripts/build-menoh.sh b/scripts/build-menoh.sh index 6f7f80b..38c4fed 100755 --- a/scripts/build-menoh.sh +++ b/scripts/build-menoh.sh @@ -59,8 +59,8 @@ test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${ARG_SOURCE_DIR}/build" test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local if [ -n "${ARG_MKLDNN_DIR}" ]; then - OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include - OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so + readonly OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include + readonly OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so fi test -n "${ARG_LINK_STATIC_LIBGCC}" || readonly ARG_LINK_STATIC_LIBGCC='OFF' diff --git a/scripts/build-protobuf.sh b/scripts/build-protobuf.sh index f694b8b..f1fdb34 100755 --- a/scripts/build-protobuf.sh +++ b/scripts/build-protobuf.sh @@ -27,6 +27,14 @@ while [[ $# != 0 ]]; do readonly ARG_INSTALL_DIR="$2" shift 2 ;; + --host) + readonly ARG_HOST="$2" + shift 2 + ;; + --with-protoc) + readonly ARG_WITH_PROTOC="$2" + shift 2 + ;; --parallel) readonly ARG_PARALLEL="$2" shift 2 @@ -55,6 +63,14 @@ test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}" test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 +# options for cross compiling +if [ -n "${ARG_HOST}" ]; then + readonly OPT_HOST=--host=${ARG_HOST} +fi +if [ -n "${ARG_WITH_PROTOC}" ]; then + readonly OPT_WITH_PROTOC=--with-protoc=${ARG_WITH_PROTOC} +fi + # download (if it isn't cached) if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then echo -e "\e[33;1mDownloading libprotobuf\e[0m" @@ -80,7 +96,7 @@ if [ ! -e "${ARG_BUILD_DIR}/src/libprotobuf.la" ]; then [ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" cd "${ARG_BUILD_DIR}" - "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS="-g -O2 -fPIC" CXXFLAGS="-g -O2 -fPIC" + "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS="-g -O2 -fPIC" CXXFLAGS="-g -O2 -fPIC" "${OPT_HOST}" "${OPT_WITH_PROTOC}" make -j${ARG_PARALLEL} echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" diff --git a/scripts/install-boost.sh b/scripts/install-boost.sh new file mode 100644 index 0000000..fdb3a13 --- /dev/null +++ b/scripts/install-boost.sh @@ -0,0 +1,35 @@ +#!/bin/bash -e + +# retrieve arguments +while [[ $# != 0 ]]; do + case $1 in + --) + shift + break + ;; + --source-dir) + readonly ARG_SOURCE_DIR="$2" + shift 2 + ;; + --dest-dir) + readonly ARG_DESTDIR="$2" + shift 2 + ;; + -*) + echo Unknown option \"$1\" 1>&2 + exit + ;; + *) + break + ;; + esac +done + +# validate the arguments +test -n "${ARG_SOURCE_DIR}" || { echo "--source-dir is not specified" 1>&2; exit 1; } + +# install (always) +echo -e "\e[33;1mInstalling libboost\e[0m" + +cd "${ARG_SOURCE_DIR}" +./b2 install $(cat ${ARG_SOURCE_DIR}/b2_opts.txt) From a28b4f31913243d5226285ec132dd2f738f53ffa Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Mon, 1 Oct 2018 17:34:07 +0900 Subject: [PATCH 35/53] switch to use libonnx directly --- CMakeLists.txt | 42 ++++++++++++++++++++-------- cmake/BuildProtobuf.cmake | 44 +++++++++++++++++++++++++++++ cmake/ConfigureMenoh.cmake | 5 ++-- cmake/GenerateOnnxSrc.cmake | 25 ----------------- cmake/SetupProtobuf.cmake | 56 ------------------------------------- menoh/CMakeLists.txt | 14 ---------- test/CMakeLists.txt | 8 ++---- 7 files changed, 80 insertions(+), 114 deletions(-) create mode 100644 cmake/BuildProtobuf.cmake delete mode 100644 cmake/GenerateOnnxSrc.cmake delete mode 100644 cmake/SetupProtobuf.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 751fe2f..a000058 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,6 @@ set(MENOH_MINOR_VERSION 1) set(MENOH_PATCH_VERSION 0) # Options -option(BUILD_SHARED_LIBS "Build shared libs" ON) - option(USE_OLD_GLIBCXX_ABI "Generate binaries for the old libstdc++ ABI" OFF) option(LINK_STATIC_LIBPROTOBUF "Link static libprotobuf to libmenoh" OFF) @@ -52,11 +50,33 @@ mark_as_advanced(DOWNLOAD_LOCATION) # Enable ExternalProject_Add include(ExternalProject) -# Setup protobuf -include(SetupProtobuf) +# Setup protobuf (MUST run before building ONNX) +if(LINK_STATIC_LIBPROTOBUF) + # Note: We can't use `set(PROTOBUF_BUILD_SHARED_LIBS OFF)` in `FindProtobuf` module + # because `libprotobuf.a` produced by the package manager is not PIC. So we need to + # build it by ourselves. + if(UNIX OR MINGW) + include(BuildProtobuf) + else() + message(FATAL_ERROR "LINK_STATIC_LIBPROTOBUF is supported only in UNIX-like environments") + endif() +else() + include(FindProtobuf) + find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED) +endif() + +include_directories(${PROTOBUF_INCLUDE_DIRS}) + +# Build libonnx.a +message(STATUS "Adding external/onnx") + +set(ONNX_SRC_DIR ${EXTERNAL_DIR}/onnx) +execute_process(COMMAND git submodule update --init -- ${ONNX_SRC_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) -# Generate source codes from ONNX protobuf schema -include(GenerateOnnxSrc) +# TODO: enable the following option when it is ready for migrating to onnx-ml +#set(ONNX_ML 1) +add_subdirectory(external/onnx) # Note: BUILD_SHARED_LIBS must be OFF in this place +include_directories("${ONNX_INCLUDE_DIRS}") # Setup MKLDNN find_package(MKLDNN "0.14") @@ -67,24 +87,24 @@ endif() include_directories("${MKLDNN_INCLUDE_DIR}") if(${ENABLE_TEST}) - message(STATUS "# add_subdirectory\(test\)") + message(STATUS "Adding test") add_subdirectory(test) endif() if(${ENABLE_BENCHMARK}) - message(STATUS "# add_subdirectory\(benchmark\)") + message(STATUS "Adding benchmark") add_subdirectory(benchmark) endif() if(${ENABLE_EXAMPLE}) - message(STATUS "# add_subdirectory\(example\)") + message(STATUS "Adding example") add_subdirectory(example) endif() -message(STATUS "# add_subdirectory\(menoh\)") +message(STATUS "Adding menoh") add_subdirectory(menoh) -message(STATUS "# add_subdirectory\(include\)") +message(STATUS "Adding include") add_subdirectory(include) if(SHOW_ALL_VARIABLES) diff --git a/cmake/BuildProtobuf.cmake b/cmake/BuildProtobuf.cmake new file mode 100644 index 0000000..3822f96 --- /dev/null +++ b/cmake/BuildProtobuf.cmake @@ -0,0 +1,44 @@ +set(PROTOBUF_VERSION_STATIC "3.6.1") + +set(PROTOBUF_DIR ${CMAKE_CURRENT_BINARY_DIR}/protobuf-${PROTOBUF_VERSION_STATIC}) +set(PROTOBUF_URL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION_STATIC}/protobuf-cpp-${PROTOBUF_VERSION_STATIC}.tar.gz") +set(PROTOBUF_HASH MD5=406d5b8636576b1c86730ca5cbd1e576) + +# Requires `-fPIC` for linking with a shared library +set(PROTOBUF_CFLAGS -fPIC) +set(PROTOBUF_CXXFLAGS -fPIC) +if(USE_OLD_GLIBCXX_ABI) + set(PROTOBUF_CXXFLAGS "${PROTOBUF_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0") +endif() + +ExternalProject_Add(Protobuf + PREFIX ${PROTOBUF_DIR} + URL ${PROTOBUF_URL} + URL_HASH ${PROTOBUF_HASH} + DOWNLOAD_DIR "${DOWNLOAD_LOCATION}" + BUILD_IN_SOURCE 1 + CONFIGURE_COMMAND bash -ex ${CMAKE_MODULE_PATH}/configure-helper.sh ${CMAKE_C_COMPILER} ${CMAKE_CXX_COMPILER} ${PROTOBUF_DIR} ${PROTOBUF_CFLAGS} ${PROTOBUF_CXXFLAGS} + BUILD_COMMAND make -j4 + INSTALL_COMMAND make install +) + +set(PROTOBUF_LIBRARY_STATIC ${PROTOBUF_DIR}/lib/libprotobuf.a) +set(PROTOBUF_LIBRARY_SHARED ${PROTOBUF_DIR}/lib/libprotobuf.so) + +# Mimic the behavior of `FindProtobuf` module +# Use the old variable names to ensure backward compatibility +set(PROTOBUF_INCLUDE_DIR ${PROTOBUF_DIR}/include) +set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR}) +set(PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY_STATIC}) # use the static library +set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARY}) +set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_DIR}/bin/protoc) +set(PROTOBUF_FOUND TRUE) + +add_library(protobuf::libprotobuf UNKNOWN IMPORTED) +# Note: INTERFACE_INCLUDE_DIRECTORIES can't set in this place because include/ is +# not installed during executing `cmake` +set_target_properties(protobuf::libprotobuf PROPERTIES + IMPORTED_LOCATION "${PROTOBUF_LIBRARY_STATIC}") +add_executable(protobuf::protoc IMPORTED) +set_target_properties(protobuf::protoc PROPERTIES + IMPORTED_LOCATION "${PROTOBUF_PROTOC_EXECUTABLE}") diff --git a/cmake/ConfigureMenoh.cmake b/cmake/ConfigureMenoh.cmake index a2f956a..fbbe2f0 100644 --- a/cmake/ConfigureMenoh.cmake +++ b/cmake/ConfigureMenoh.cmake @@ -8,6 +8,9 @@ macro(menoh_link_libraries TARGET_NAME SCOPE) target_link_libraries(${TARGET_NAME} ${SCOPE} -static-libstdc++) endif() + target_link_libraries(${TARGET_NAME} ${SCOPE} ${PROTOBUF_LIBRARIES}) + target_link_libraries(${TARGET_NAME} ${SCOPE} onnx) + if(NOT ${SCOPE}) # PUBLIC will add transitive dependencies (`mklml_intel` and `iomp5`) to the link interface # Note: change it to PRIVATE after building mkldnn itself @@ -15,6 +18,4 @@ macro(menoh_link_libraries TARGET_NAME SCOPE) else() target_link_libraries(${TARGET_NAME} ${MKLDNN_LIBRARIES}) endif() - - target_link_libraries(${TARGET_NAME} ${SCOPE} ${PROTOBUF_LIBRARIES}) endmacro() diff --git a/cmake/GenerateOnnxSrc.cmake b/cmake/GenerateOnnxSrc.cmake deleted file mode 100644 index 0c2d8b5..0000000 --- a/cmake/GenerateOnnxSrc.cmake +++ /dev/null @@ -1,25 +0,0 @@ -set(ONNX_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/onnx) - -if(NOT EXISTS "${ONNX_OUTPUT_DIR}") - file(MAKE_DIRECTORY "${ONNX_OUTPUT_DIR}") -endif() - -set(ONNX_SRC_DIR ${EXTERNAL_DIR}/onnx) -execute_process(COMMAND git submodule update --init -- ${ONNX_SRC_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) - -set(ONNX_PROTO_HEADER ${ONNX_OUTPUT_DIR}/onnx/onnx.pb.h) -set(ONNX_PROTO_SRC ${ONNX_OUTPUT_DIR}/onnx/onnx.pb.cc) - -set(ONNX_GENERATED_OUTPUTS ${ONNX_PROTO_HEADER} ${ONNX_PROTO_SRC}) - -add_custom_target(gen_onnx_outputs DEPENDS ${ONNX_GENERATED_OUTPUTS}) -add_custom_command( - OUTPUT ${ONNX_GENERATED_OUTPUTS} - COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} - ARGS -I ${ONNX_SRC_DIR} --cpp_out . ${ONNX_SRC_DIR}/onnx/onnx.proto - DEPENDS ${PROTOBUF_PROTOC_EXECUTABLE} ${ONNX_SRC_DIR}/onnx/onnx.proto - COMMENT "Generating ONNX source files" - WORKING_DIRECTORY ${ONNX_OUTPUT_DIR} - VERBATIM) - -include_directories(${ONNX_OUTPUT_DIR}) # for ONNX_PROTO_HEADER diff --git a/cmake/SetupProtobuf.cmake b/cmake/SetupProtobuf.cmake deleted file mode 100644 index 327edf4..0000000 --- a/cmake/SetupProtobuf.cmake +++ /dev/null @@ -1,56 +0,0 @@ -set(PROTOBUF_VERSION "2.6.1") - -if(LINK_STATIC_LIBPROTOBUF) - # Note: We can't use `set(PROTOBUF_BUILD_SHARED_LIBS OFF)` in `FindProtobuf` module - # because `libprotobuf.a` produced by the package manager is not PIC. So we need to - # build it by ourselves. - - if(UNIX OR MINGW) - set(PROTOBUF_VERSION_STATIC "3.6.1") - set(PROTOBUF_DIR ${CMAKE_CURRENT_BINARY_DIR}/protobuf-${PROTOBUF_VERSION_STATIC}) - set(PROTOBUF_URL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION_STATIC}/protobuf-cpp-${PROTOBUF_VERSION_STATIC}.tar.gz") - set(PROTOBUF_HASH MD5=406d5b8636576b1c86730ca5cbd1e576) - - # Requires `-fPIC` for linking with a shared library - set(PROTOBUF_CFLAGS -fPIC) - set(PROTOBUF_CXXFLAGS -fPIC) - if(USE_OLD_GLIBCXX_ABI) - set(PROTOBUF_CXXFLAGS "${PROTOBUF_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0") - endif() - - ExternalProject_Add(Protobuf - PREFIX ${PROTOBUF_DIR} - URL ${PROTOBUF_URL} - URL_HASH ${PROTOBUF_HASH} - DOWNLOAD_DIR "${DOWNLOAD_LOCATION}" - BUILD_IN_SOURCE 1 - CONFIGURE_COMMAND bash -ex ${CMAKE_MODULE_PATH}/configure-helper.sh ${CMAKE_C_COMPILER} ${CMAKE_CXX_COMPILER} ${PROTOBUF_DIR} ${PROTOBUF_CFLAGS} ${PROTOBUF_CXXFLAGS} - BUILD_COMMAND make -j4 - INSTALL_COMMAND make install - ) - - set(PROTOBUF_LIBRARY_STATIC ${PROTOBUF_DIR}/lib/libprotobuf.a) - set(PROTOBUF_LIBRARY_SHARED ${PROTOBUF_DIR}/lib/libprotobuf.so) - - # Mimic the behavior of `FindProtobuf` module - # Use the old variable names to ensure backward compatibility - set(PROTOBUF_INCLUDE_DIR ${PROTOBUF_DIR}/include) - set(PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY_STATIC}) # use the static library - set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARY}) - set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_DIR}/bin/protoc) - set(PROTOBUF_FOUND TRUE) - - add_library(protobuf::libprotobuf UNKNOWN IMPORTED) - # Note: INTERFACE_INCLUDE_DIRECTORIES can't set in this place because include/ is - # not installed during executing `cmake` - set_target_properties(protobuf::libprotobuf PROPERTIES - IMPORTED_LOCATION "${PROTOBUF_LIBRARY_STATIC}") - else() - message(FATAL_ERROR "LINK_STATIC_LIBPROTOBUF is supported only in UNIX-like environments") - endif() -else() - include(FindProtobuf) - find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED) -endif() - -include_directories(${PROTOBUF_INCLUDE_DIR}) diff --git a/menoh/CMakeLists.txt b/menoh/CMakeLists.txt index 9bd995b..06e9f3a 100644 --- a/menoh/CMakeLists.txt +++ b/menoh/CMakeLists.txt @@ -2,13 +2,6 @@ option(LINK_STATIC_LIBGCC "Link static libgcc to libmenoh" OFF) option(LINK_STATIC_LIBSTDCXX "Link static libstdc++ to libmenoh" OFF) -if(NOT DEFINED ONNX_PROTO_SRC) - message(FATAL_ERROR "ONNX_PROTO_SRC is not found") -endif() -if(NOT DEFINED ONNX_PROTO_HEADER) - message(FATAL_ERROR "ONNX_PROTO_HEADER is not found") -endif() - # Note: The libraries can be static (.a) or shared (.so) if(NOT DEFINED MKLDNN_LIBRARIES) message(FATAL_ERROR "MKLDNN_LIBRARIES is not found") @@ -21,10 +14,6 @@ file(GLOB_RECURSE SOURCES "." "*.cpp") # Create a object library for generating shared library add_library(menoh_objlib OBJECT ${SOURCES}) - -add_dependencies(menoh_objlib gen_onnx_outputs) -target_sources(menoh_objlib PRIVATE ${ONNX_PROTO_SRC}) - set_target_properties(menoh_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON) include(ConfigureMenoh) @@ -43,9 +32,6 @@ menoh_link_libraries(menoh PRIVATE) add_library(menoh_test_target SHARED $) menoh_link_libraries(menoh_test_target PRIVATE) -set_source_files_properties(${ONNX_PROTO_SRC} PROPERTIES GENERATED TRUE) -set_source_files_properties(${ONNX_PROTO_HEADER} PROPERTIES GENERATED TRUE) - install(TARGETS menoh RUNTIME DESTINATION "bin" LIBRARY DESTINATION "lib" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 04f9813..3085c5a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,7 +3,7 @@ enable_testing() # GTest setup set(GTEST_DIR "lib/googletest") execute_process(COMMAND git submodule update --init -- test/${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) -message(STATUS "# add_subdirectory\(${GTEST_DIR}\)") +message(STATUS "Adding_${GTEST_DIR}") add_subdirectory(${GTEST_DIR}) # filesystem setup @@ -25,11 +25,7 @@ add_executable(menoh_test ) -if(LINK_STATIC_LIBPROTOBUF) - target_link_libraries(menoh_test gtest_main menoh_test_target) -else() - target_link_libraries(menoh_test gtest_main menoh_test_target ${PROTOBUF_LIBRARY}) -endif() +target_link_libraries(menoh_test gtest_main menoh_test_target onnx_proto) if(UNIX AND NOT APPLE) set_property( From 8d373ea59300b6d1e329cf3dab4bc74acc9f2e4f Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Mon, 1 Oct 2018 18:08:11 +0900 Subject: [PATCH 36/53] add PYTHON_EXECUTABLE option --- .travis/init-build-linux.sh | 4 +++- .travis/init-build-osx.sh | 4 +++- scripts/build-menoh.sh | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.travis/init-build-linux.sh b/.travis/init-build-linux.sh index a5e5f16..ae5f0db 100644 --- a/.travis/init-build-linux.sh +++ b/.travis/init-build-linux.sh @@ -78,12 +78,14 @@ function build_menoh() { docker_exec_script \ "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir "${PROJ_DIR}" + --source-dir "${PROJ_DIR}" \ + --python-executable python3 else docker_exec_script \ "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ --source-dir "${PROJ_DIR}" \ + --python-executable python3 \ --link-static-libgcc ON \ --link-static-libstdcxx ON \ --link-static-libprotobuf ON diff --git a/.travis/init-build-osx.sh b/.travis/init-build-osx.sh index c1228d8..afe593a 100644 --- a/.travis/init-build-osx.sh +++ b/.travis/init-build-osx.sh @@ -17,12 +17,14 @@ function build_menoh() { if [ "${LINK_STATIC}" != "true" ]; then bash -ex "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ - --source-dir "${PROJ_DIR}" + --source-dir "${PROJ_DIR}" \ + --python-executable python else # Does not set --link-static-libgcc and --link-static-libstdcxx in macOS bash -ex "${PROJ_DIR}/scripts/build-menoh.sh" \ --build-type Release \ --source-dir "${PROJ_DIR}" \ + --python-executable python \ --link-static-libprotobuf ON fi } diff --git a/scripts/build-menoh.sh b/scripts/build-menoh.sh index 38c4fed..5184c4e 100755 --- a/scripts/build-menoh.sh +++ b/scripts/build-menoh.sh @@ -29,6 +29,10 @@ while [[ $# != 0 ]]; do readonly ARG_MKLDNN_DIR="$2" shift 2 ;; + --python-executable) + ARG_PYTHON_EXECUTABLE="$2" + shift 2 + ;; --link-static-libgcc) readonly ARG_LINK_STATIC_LIBGCC="$2" shift 2 @@ -63,6 +67,7 @@ if [ -n "${ARG_MKLDNN_DIR}" ]; then readonly OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so fi +test -n "${ARG_PYTHON_EXECUTABLE}" || readonly ARG_PYTHON_EXECUTABLE=python test -n "${ARG_LINK_STATIC_LIBGCC}" || readonly ARG_LINK_STATIC_LIBGCC='OFF' test -n "${ARG_LINK_STATIC_LIBSTDCXX}" || readonly ARG_LINK_STATIC_LIBSTDCXX='OFF' test -n "${ARG_LINK_STATIC_LIBPROTOBUF}" || readonly ARG_LINK_STATIC_LIBPROTOBUF='OFF' @@ -77,6 +82,7 @@ cmake \ "-DCMAKE_INSTALL_PREFIX=${ARG_INSTALL_DIR}" \ "${OPT_MKLDNN_INCLUDE_DIR}" \ "${OPT_MKLDNN_LIBRARY}" \ + -DPYTHON_EXECUTABLE=${ARG_PYTHON_EXECUTABLE} \ -DLINK_STATIC_LIBGCC=${ARG_LINK_STATIC_LIBGCC} \ -DLINK_STATIC_LIBSTDCXX=${ARG_LINK_STATIC_LIBSTDCXX} \ -DLINK_STATIC_LIBPROTOBUF=${ARG_LINK_STATIC_LIBPROTOBUF} \ From 2f1dfb216d45cafecf320b38ac99b555fc79771a Mon Sep 17 00:00:00 2001 From: okdshin Date: Mon, 1 Oct 2018 22:12:53 +0900 Subject: [PATCH 37/53] fix bug --- scripts/gen_attribute_completion_and_shape_inference_hpp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/gen_attribute_completion_and_shape_inference_hpp.py b/scripts/gen_attribute_completion_and_shape_inference_hpp.py index e0ddf31..5913d0a 100644 --- a/scripts/gen_attribute_completion_and_shape_inference_hpp.py +++ b/scripts/gen_attribute_completion_and_shape_inference_hpp.py @@ -4,8 +4,8 @@ def make_completion_code(op_name, attribute_list=[], shape_inference_code=''' -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); ''', preprocess="", @@ -98,7 +98,7 @@ def main(): p.first, array_profile(p.second.dtype(), p.second.dims())); }}); auto profile_of = [&variable_profile_table](std::string const& name){{ - assert(variable_profile_table.find(name) == + assert(variable_profile_table.find(name) != variable_profile_table.end()); return variable_profile_table.at(name); }}; From 5726b6785d77e42319d51bd72e2239ac63789fae Mon Sep 17 00:00:00 2001 From: okdshin Date: Mon, 1 Oct 2018 22:13:14 +0900 Subject: [PATCH 38/53] generate attribute_completion_and_shape_inference.hpp --- ...tribute_completion_and_shape_inference.hpp | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/menoh/attribute_completion_and_shape_inference.hpp b/menoh/attribute_completion_and_shape_inference.hpp index d7b07d9..dc20fd3 100644 --- a/menoh/attribute_completion_and_shape_inference.hpp +++ b/menoh/attribute_completion_and_shape_inference.hpp @@ -31,7 +31,7 @@ namespace menoh_impl { p.first, array_profile(p.second.dtype(), p.second.dims())); }); auto profile_of = [&variable_profile_table](std::string const& name){ - assert(variable_profile_table.find(name) == + assert(variable_profile_table.find(name) != variable_profile_table.end()); return variable_profile_table.at(name); }; @@ -70,8 +70,8 @@ if(node.op_type == "Abs") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -86,8 +86,8 @@ if(node.op_type == "Add") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -214,8 +214,8 @@ auto momentum = get(node.attribute_table.at("momentum")); auto spatial = get(node.attribute_table.at("spatial")); -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -474,8 +474,8 @@ node.attribute_table.emplace( auto alpha = get(node.attribute_table.at("alpha")); -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -599,8 +599,8 @@ node.attribute_table.emplace( auto alpha = get(node.attribute_table.at("alpha")); -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -668,8 +668,8 @@ auto bias = get(node.attribute_table.at("bias")); auto size = get(node.attribute_table.at("size")); -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -754,8 +754,8 @@ if(node.op_type == "Relu") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -782,8 +782,8 @@ node.attribute_table.emplace( auto axis = get(node.attribute_table.at("axis")); -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -798,8 +798,8 @@ if(node.op_type == "Sum") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -814,8 +814,8 @@ if(node.op_type == "Sqrt") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } @@ -830,8 +830,8 @@ if(node.op_type == "Tanh") { { -assert(node.input_name_list.size() == 1); -assert(node.output_name_list.size() == 1); +assert(node.input_name_list.size() > 0); +assert(node.output_name_list.size() > 0); add_variable_to_table(output(0), dtype_of(input(0)), dims_of(input(0))); } From 5bdf660dfd07a0c9f52596b3b5258a92da7d6ea2 Mon Sep 17 00:00:00 2001 From: okdshin Date: Mon, 1 Oct 2018 22:13:56 +0900 Subject: [PATCH 39/53] rename --- test/CMakeLists.txt | 2 +- ...tribute_completion_and_shape_inference.cpp | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 test/attribute_completion_and_shape_inference.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 04f9813..7e197a1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,8 +21,8 @@ add_executable(menoh_test operator.cpp mkldnn_with_generic_fallback_backend.cpp model_data.cpp + attribute_completion_and_shape_inference.cpp #vgg16.cpp - ) if(LINK_STATIC_LIBPROTOBUF) diff --git a/test/attribute_completion_and_shape_inference.cpp b/test/attribute_completion_and_shape_inference.cpp new file mode 100644 index 0000000..b998c75 --- /dev/null +++ b/test/attribute_completion_and_shape_inference.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include + +#include "common.hpp" +namespace { + + class AttributeCompletionAndShapeInferenceTest : public ::testing::Test {}; + + TEST_F(AttributeCompletionAndShapeInferenceTest, conv_completion) { + menoh_impl::model_data model_data; + model_data.node_list.push_back( + menoh_impl::node{"Conv", {"x", "w"}, {"y"}, {}}); + std::unordered_map + input_profile_table; + input_profile_table.emplace( + "x", + menoh_impl::array_profile(menoh_impl::dtype_t::float_, {1, 1, 3, 3})); + input_profile_table.emplace( + "w", + menoh_impl::array_profile(menoh_impl::dtype_t::float_, {1, 1, 3, 3})); + menoh_impl::complete_attribute_and_infer_shape(model_data, + input_profile_table); + auto const& node = model_data.node_list.at(0); + auto found = node.attribute_table.find("kernel_shape"); + ASSERT_NE(found, node.attribute_table.end()); + menoh_impl::assert_eq_list( + menoh_impl::get>(found->second), + std::vector({3, 3})); + } + + TEST_F(AttributeCompletionAndShapeInferenceTest, sum_check) { + menoh_impl::model_data model_data; + model_data.node_list.push_back( + menoh_impl::node{"Sum", {"x0", "x1", "x2"}, {"y"}, {}}); + std::unordered_map + input_profile_table; + for(int i = 0; i < 3; ++i) { + input_profile_table.emplace( + "x" + std::to_string(i), + menoh_impl::array_profile(menoh_impl::dtype_t::float_, + {1, 1, 3, 3})); + } + ASSERT_NO_THROW(menoh_impl::complete_attribute_and_infer_shape( + model_data, input_profile_table)); + } + + TEST_F(AttributeCompletionAndShapeInferenceTest, add_check) { + menoh_impl::model_data model_data; + model_data.node_list.push_back( + menoh_impl::node{"Add", {"x0", "x1"}, {"y"}, {}}); + std::unordered_map + input_profile_table; + for(int i = 0; i < 2; ++i) { + input_profile_table.emplace( + "x" + std::to_string(i), + menoh_impl::array_profile(menoh_impl::dtype_t::float_, + {1, 1, 3, 3})); + } + ASSERT_NO_THROW(menoh_impl::complete_attribute_and_infer_shape( + model_data, input_profile_table)); + } + +} // namespace From 6a7413e24ac59c5fded4e85e32ec7341583b3742 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Tue, 2 Oct 2018 11:25:42 +0900 Subject: [PATCH 40/53] fix to link protobuf in test for shared library mode --- CMakeLists.txt | 4 +++- cmake/ConfigureMenoh.cmake | 3 +-- menoh/CMakeLists.txt | 3 --- test/CMakeLists.txt | 6 +++++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a000058..4ed72f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ mark_as_advanced(DOWNLOAD_LOCATION) # Enable ExternalProject_Add include(ExternalProject) -# Setup protobuf (MUST run before building ONNX) +# Setup protobuf (it is used in ONNX and Menoh) if(LINK_STATIC_LIBPROTOBUF) # Note: We can't use `set(PROTOBUF_BUILD_SHARED_LIBS OFF)` in `FindProtobuf` module # because `libprotobuf.a` produced by the package manager is not PIC. So we need to @@ -61,6 +61,8 @@ if(LINK_STATIC_LIBPROTOBUF) message(FATAL_ERROR "LINK_STATIC_LIBPROTOBUF is supported only in UNIX-like environments") endif() else() + # Note: It may conflict with the loading mechanism in onnx's CMake configuration. + # See external/onnx/CMakeLists.txt for more details. include(FindProtobuf) find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED) endif() diff --git a/cmake/ConfigureMenoh.cmake b/cmake/ConfigureMenoh.cmake index fbbe2f0..1752799 100644 --- a/cmake/ConfigureMenoh.cmake +++ b/cmake/ConfigureMenoh.cmake @@ -8,8 +8,7 @@ macro(menoh_link_libraries TARGET_NAME SCOPE) target_link_libraries(${TARGET_NAME} ${SCOPE} -static-libstdc++) endif() - target_link_libraries(${TARGET_NAME} ${SCOPE} ${PROTOBUF_LIBRARIES}) - target_link_libraries(${TARGET_NAME} ${SCOPE} onnx) + target_link_libraries(${TARGET_NAME} ${SCOPE} onnx) # onnx also contains protobuf if(NOT ${SCOPE}) # PUBLIC will add transitive dependencies (`mklml_intel` and `iomp5`) to the link interface diff --git a/menoh/CMakeLists.txt b/menoh/CMakeLists.txt index 06e9f3a..398d48f 100644 --- a/menoh/CMakeLists.txt +++ b/menoh/CMakeLists.txt @@ -6,9 +6,6 @@ option(LINK_STATIC_LIBSTDCXX "Link static libstdc++ to libmenoh" OFF) if(NOT DEFINED MKLDNN_LIBRARIES) message(FATAL_ERROR "MKLDNN_LIBRARIES is not found") endif() -if(NOT DEFINED PROTOBUF_LIBRARIES) - message(FATAL_ERROR "PROTOBUF_LIBRARIES is not found") -endif() file(GLOB_RECURSE SOURCES "." "*.cpp") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3085c5a..2ca5a7c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,7 +25,11 @@ add_executable(menoh_test ) -target_link_libraries(menoh_test gtest_main menoh_test_target onnx_proto) +if(LINK_STATIC_LIBPROTOBUF) + target_link_libraries(menoh_test gtest_main menoh_test_target) +else() + target_link_libraries(menoh_test gtest_main menoh_test_target ${PROTOBUF_LIBRARIES}) +endif() if(UNIX AND NOT APPLE) set_property( From 8770abd750b9a8558c8b2c1be0899f51b2fe0ce9 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Tue, 2 Oct 2018 12:08:40 +0900 Subject: [PATCH 41/53] add explicit dependency to gen_onnx_proto --- appveyor.yml | 2 +- menoh/CMakeLists.txt | 2 ++ test/CMakeLists.txt | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 7c67464..9951f2c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -72,7 +72,7 @@ build_script: cmake -G "MSYS Makefiles" -DENABLE_TEST=ON %STATIC_OPTION% -DCMAKE_INSTALL_PREFIX=/mingw64 .. && make ) else ( - cmake -G "Visual Studio 14 Win64" -DENABLE_TEST=OFF -DENABLE_BENCHMARK=OFF -DENABLE_EXAMPLE=OFF -DENABLE_TOOL=OFF -DCMAKE_INSTALL_PREFIX=c:\menoh-%MENOH_REV%-msvc .. && + cmake -G "Visual Studio 14 Win64" -DENABLE_TEST=OFF -DENABLE_BENCHMARK=OFF -DENABLE_EXAMPLE=OFF -DCMAKE_INSTALL_PREFIX=c:\menoh-%MENOH_REV%-msvc .. && cmake --build . --config Release --target install ) diff --git a/menoh/CMakeLists.txt b/menoh/CMakeLists.txt index 398d48f..d420997 100644 --- a/menoh/CMakeLists.txt +++ b/menoh/CMakeLists.txt @@ -11,6 +11,8 @@ file(GLOB_RECURSE SOURCES "." "*.cpp") # Create a object library for generating shared library add_library(menoh_objlib OBJECT ${SOURCES}) +add_dependencies(menoh_objlib gen_onnx_proto) # custom target defined in onnx + set_target_properties(menoh_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON) include(ConfigureMenoh) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2ca5a7c..2b9f9e4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,7 +3,7 @@ enable_testing() # GTest setup set(GTEST_DIR "lib/googletest") execute_process(COMMAND git submodule update --init -- test/${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) -message(STATUS "Adding_${GTEST_DIR}") +message(STATUS "Adding ${GTEST_DIR}") add_subdirectory(${GTEST_DIR}) # filesystem setup From 5c1a39a1e78c5d782ee773a4852fd9d5b613ac7b Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Tue, 2 Oct 2018 14:35:19 +0900 Subject: [PATCH 42/53] Update BUILDING.md --- BUILDING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index eb6fea1..4dc0667 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -25,7 +25,7 @@ You also need to install the dependent libraries on your system: `protobuf` can be installed through most package managers instead of building it yourself. `mkl-dnn` package, unfortunatelly, is not available in many environments at the moment (except for `brew` in macOS). -Note that you can use ProtoBuf either version 2 or 3, but, for example, if you build Menoh with `protoc` ver 3 you should use the binary with runtime ver 3. +Note that you can use ProtoBuf either version 2 or 3, but the runtime version should be the same as `protoc` in your system. ### Debian/Ubuntu ``` @@ -86,7 +86,7 @@ pacman -S mingw-w64-x86_64-protobuf mingw-w64-x86_64-protobuf-c #### Installing MKL-DNN from binary package ``` curl -omingw-w64-x86_64-mkl-dnn-0.15-1-x86_64.pkg.tar.xz -L https://github.com/pfnet-research/menoh/releases/download/v1.0.3/mingw-w64-x86_64-mkl-dnn-0.15-1-x86_64.pkg.tar.xz -pacman -S --noconfirm mingw-w64-x86_64-mkl-dnn-0.15-1-x86_64.pkg.tar.xz +pacman -U --noconfirm mingw-w64-x86_64-mkl-dnn-0.15-1-x86_64.pkg.tar.xz ``` #### Installing MKL-DNN from source From 823996917f09fee2788cd2f446488c8b2d1d41f8 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Tue, 2 Oct 2018 14:41:02 +0900 Subject: [PATCH 43/53] add `make` --- BUILDING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILDING.md b/BUILDING.md index 4dc0667..c4ebe64 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -78,7 +78,7 @@ cd ../.. ### Windows (MINGW) ``` pacman -S mingw-w64-x86_64-toolchain -pacman -S git +pacman -S git make pacman -S mingw-w64-x86_64-cmake pacman -S mingw-w64-x86_64-protobuf mingw-w64-x86_64-protobuf-c ``` From 99a77bcca9a44c85382eaa84f3ba2b5817808895 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Tue, 2 Oct 2018 14:52:43 +0900 Subject: [PATCH 44/53] add PYTHON_EXECUTABLE option --- BUILDING.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/BUILDING.md b/BUILDING.md index eb6fea1..a308f45 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -194,3 +194,12 @@ MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=/mingw64 make ``` + +### Note + +#### Python command name +Menoh requires `python` command to generate source codes at build time. Add `PYTHON_EXECUTABLE` option to `cmake` if you want to use `python` command with non-standard name (e.g. `python3`). + +```bash +cmake -DPYTHON_EXECUTABLE=python3 .. +``` From 750ccfb83bd60d77c16485a6cec8306f69cac226 Mon Sep 17 00:00:00 2001 From: okdshin Date: Tue, 2 Oct 2018 22:03:04 +0900 Subject: [PATCH 45/53] remove model_data_completion test (it is moved to test/attribute_completion_and_shape_inference.cpp --- test/model_data_completion.cpp | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 test/model_data_completion.cpp diff --git a/test/model_data_completion.cpp b/test/model_data_completion.cpp deleted file mode 100644 index f02a540..0000000 --- a/test/model_data_completion.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include - -#include -#include - -#include "common.hpp" -namespace { - - class ModelDataCompletionTest : public ::testing::Test {}; - - TEST_F(ModelDataCompletionTest, completion) { - menoh_impl::model_data model_data; - model_data.node_list.push_back( - menoh_impl::node{"Conv", {"x", "w"}, {"y"}, {}}); - std::unordered_map - input_profile_table; - input_profile_table.emplace( - "w", - menoh_impl::array_profile(menoh_impl::dtype_t::float_, {1, 1, 3, 3})); - menoh_impl::complete_model_data(model_data, input_profile_table); - auto const& node = model_data.node_list.at(0); - auto found = node.attribute_table.find("kernel_shape"); - ASSERT_NE(found, node.attribute_table.end()); - menoh_impl::assert_eq_list( - menoh_impl::get>(found->second), - std::vector({3, 3})); - } - -} // namespace From db60d6695ee018467e195ae56a3b49011d41620a Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Wed, 3 Oct 2018 12:48:08 +0900 Subject: [PATCH 46/53] add a patch for CMake configuration in onnx --- CMakeLists.txt | 6 +- external/onnx-v1.3.0-patch_CMakeLists.txt | 565 ++++++++++++++++++++++ menoh/CMakeLists.txt | 5 + test/CMakeLists.txt | 2 + 4 files changed, 576 insertions(+), 2 deletions(-) create mode 100644 external/onnx-v1.3.0-patch_CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ed72f3..6cce354 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,14 +67,16 @@ else() find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED) endif() -include_directories(${PROTOBUF_INCLUDE_DIRS}) - # Build libonnx.a message(STATUS "Adding external/onnx") set(ONNX_SRC_DIR ${EXTERNAL_DIR}/onnx) execute_process(COMMAND git submodule update --init -- ${ONNX_SRC_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) +# see https://github.com/onnx/onnx/pull/1466 +message(STATUS "Patching to external/onnx") +configure_file(${EXTERNAL_DIR}/onnx-v1.3.0-patch_CMakeLists.txt ${EXTERNAL_DIR}/onnx/CMakeLists.txt COPYONLY) + # TODO: enable the following option when it is ready for migrating to onnx-ml #set(ONNX_ML 1) add_subdirectory(external/onnx) # Note: BUILD_SHARED_LIBS must be OFF in this place diff --git a/external/onnx-v1.3.0-patch_CMakeLists.txt b/external/onnx-v1.3.0-patch_CMakeLists.txt new file mode 100644 index 0000000..23ec68b --- /dev/null +++ b/external/onnx-v1.3.0-patch_CMakeLists.txt @@ -0,0 +1,565 @@ +# Minimum CMake required +cmake_minimum_required(VERSION 3.1) +include(cmake/Utils.cmake) +# Set default build type +if(NOT CMAKE_BUILD_TYPE) + message(STATUS "Build type not set - defaulting to Release") + set( + CMAKE_BUILD_TYPE "Release" + CACHE + STRING + "Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage." + FORCE) +endif() +cmake_policy(SET CMP0063 NEW) + +# Project +project(onnx C CXX) +option(ONNX_BUILD_BENCHMARKS "Build ONNX micro-benchmarks" OFF) + +option(BUILD_ONNX_PYTHON "Build Python binaries" OFF) +option(ONNX_GEN_PB_TYPE_STUBS "Generate protobuf python type stubs" ON) +option(ONNX_WERROR "Build with Werror" OFF) +option(ONNX_COVERAGE "Build with coverage instrumentation" OFF) +option(ONNX_BUILD_TESTS "Build ONNX C++ APIs Tests" OFF) +option(ONNX_USE_LITE_PROTO "Use lite protobuf instead of full." OFF) + +set(ONNX_NAMESPACE "onnx" CACHE STRING "onnx namespace") + +# Set C++11 as standard for the whole project +if(NOT MSVC) + set(CMAKE_CXX_STANDARD 11) +endif(NOT MSVC) + +set(ONNX_ROOT ${PROJECT_SOURCE_DIR}) + +# Read ONNX version +file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" ONNX_VERSION) +string(STRIP "${ONNX_VERSION}" ONNX_VERSION) + +if(NOT MSVC) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") + if(ONNX_COVERAGE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") + endif() +endif() + +if(ONNX_BUILD_TESTS) + list(APPEND CMAKE_MODULE_PATH ${ONNX_ROOT}/cmake/external) + include(googletest) +endif() + +if((ONNX_USE_LITE_PROTO AND TARGET protobuf::libprotobuf-lite) OR ((NOT ONNX_USE_LITE_PROTO) AND TARGET protobuf::libprotobuf)) + # Sometimes we need to use protoc compiled for host architecture while linking + # libprotobuf against target architecture. See https://github.com/caffe2/caffe + # 2/blob/96f35ad75480b25c1a23d6e9e97bccae9f7a7f9c/cmake/ProtoBuf.cmake#L92-L99 + if(EXISTS "${ONNX_CUSTOM_PROTOC_EXECUTABLE}") + message(STATUS "Using custom protoc executable") + set(ONNX_PROTOC_EXECUTABLE ${ONNX_CUSTOM_PROTOC_EXECUTABLE}) + else() + set(ONNX_PROTOC_EXECUTABLE $) + endif() +else() + # Customized version of find Protobuf. We need to avoid situations mentioned + # in https://github.com/caffe2/caffe2/blob/b7d983f255ef5496474f1ea188edb5e0ac4 + # 42761/cmake/ProtoBuf.cmake#L82-L92 The following section is stolen from + # cmake/ProtoBuf.cmake in Caffe2 + find_program(Protobuf_PROTOC_EXECUTABLE + NAMES protoc + DOC "The Google Protocol Buffers Compiler") + + # Only if protoc was found, seed the include directories and libraries. We + # assume that protoc is installed at PREFIX/bin. We use get_filename_component + # to resolve PREFIX. + if(Protobuf_PROTOC_EXECUTABLE) + set(ONNX_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE}) + get_filename_component(_PROTOBUF_INSTALL_PREFIX + ${Protobuf_PROTOC_EXECUTABLE} DIRECTORY) + get_filename_component(_PROTOBUF_INSTALL_PREFIX + ${_PROTOBUF_INSTALL_PREFIX}/.. REALPATH) + find_library(Protobuf_PROTOC_LIBRARY + NAMES protoc + PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib + NO_DEFAULT_PATH) + if(ONNX_USE_LITE_PROTO) + find_library(Protobuf_LITE_LIBRARY + NAMES protobuf-lite + PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib + NO_DEFAULT_PATH) + else(ONNX_USE_LITE_PROTO) + find_library(Protobuf_LIBRARY + NAMES protobuf + PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib + NO_DEFAULT_PATH) + endif(ONNX_USE_LITE_PROTO) + find_path(Protobuf_INCLUDE_DIR google/protobuf/service.h + PATHS ${_PROTOBUF_INSTALL_PREFIX}/include + NO_DEFAULT_PATH) + find_package(Protobuf REQUIRED) + endif() +endif() + +# Build the libraries with -fPIC +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR) from https://githu +# b.com/tensorflow/tensorflow/blob/d2c3b873c6f8ff999a2e4ee707a84ff00d9c15a5/tens +# orflow/contrib/cmake/tf_core_framework.cmake to solve the problem that +# customized dir can't be specified when calling PROTOBUF_GENERATE_CPP. +function(RELATIVE_PROTOBUF_GENERATE_CPP NAME SRCS HDRS ROOT_DIR DEPEND) + if(NOT ARGN) + message( + SEND_ERROR + "Error: RELATIVE_PROTOBUF_GENERATE_CPP() called without any proto files" + ) + return() + endif() + + if(MSVC AND BUILD_SHARED_LIBS) + set(ONNX_DLLEXPORT_STR "dllexport_decl=ONNX_API:") + else() + set(ONNX_DLLEXPORT_STR "") + endif() + + set(${SRCS}) + set(${HDRS}) + + set(GEN_PROTO_PY ${ROOT_DIR}/onnx/gen_proto.py) + foreach(INFILE ${ARGN}) + set(ABS_FILE ${ROOT_DIR}/${INFILE}) + get_filename_component(FILE_DIR ${ABS_FILE} DIRECTORY) + get_filename_component(FILE_WE ${INFILE} NAME_WE) + if(ONNX_ML) + if(ONNX_NAMESPACE STREQUAL "onnx") + set(GENERATED_FILE_WE "${FILE_WE}-ml") + else() + set(GENERATED_FILE_WE "${FILE_WE}_${ONNX_NAMESPACE}-ml") + endif() + else() + if(ONNX_NAMESPACE STREQUAL "onnx") + set(GENERATED_FILE_WE "${FILE_WE}") + else() + set(GENERATED_FILE_WE "${FILE_WE}_${ONNX_NAMESPACE}") + endif() + endif() + file(RELATIVE_PATH REL_DIR ${ROOT_DIR} ${FILE_DIR}) + set(OUTPUT_PROTO_DIR "${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}") + + set(OUTPUT_PB_HEADER "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.h") + set(OUTPUT_PB_SRC "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.cc") + set(GENERATED_PROTO "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.proto") + if(NOT (ONNX_NAMESPACE STREQUAL "onnx")) + # We need this dummy header generated by gen_proto.py when ONNX_NAMESPACE + # is not onnx + list(APPEND ${HDRS} "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.h") + endif() + list(APPEND ${SRCS} "${OUTPUT_PB_SRC}") + list(APPEND ${HDRS} "${OUTPUT_PB_HEADER}") + + if(NOT EXISTS "${OUTPUT_PROTO_DIR}") + file(MAKE_DIRECTORY "${OUTPUT_PROTO_DIR}") + endif() + + if("${PYTHON_EXECUTABLE}" STREQUAL "") + set(_python_exe "python") + else() + set(_python_exe "${PYTHON_EXECUTABLE}") + endif() + set(GEN_PROTO_ARGS + -p + "${ONNX_NAMESPACE}" + -o + "${OUTPUT_PROTO_DIR}" + "${FILE_WE}") + if(ONNX_ML) + list(APPEND GEN_PROTO_ARGS -m) + endif() + if(ONNX_USE_LITE_PROTO) + list(APPEND GEN_PROTO_ARGS -l) + endif() + add_custom_command(OUTPUT "${GENERATED_PROTO}" + COMMAND "${_python_exe}" "${GEN_PROTO_PY}" + ARGS ${GEN_PROTO_ARGS} + DEPENDS ${INFILE} + COMMENT "Running gen_proto.py on ${INFILE}" + VERBATIM) + + set(PROTOC_ARGS + ${GENERATED_PROTO} + -I + ${CMAKE_CURRENT_BINARY_DIR} + --cpp_out + ${ONNX_DLLEXPORT_STR}${CMAKE_CURRENT_BINARY_DIR}) + if(BUILD_ONNX_PYTHON) + list(APPEND PROTOC_ARGS --python_out + ${ONNX_DLLEXPORT_STR}${CMAKE_CURRENT_BINARY_DIR}) + if(ONNX_GEN_PB_TYPE_STUBS) + # Haven't figured out how to generate mypy stubs on Windows yet + if(NOT WIN32) + # If onnx was packaged to pypi from Windows, protoc-gen-mypy.py is + # missing the +x flag. Add it. + execute_process(COMMAND chmod +x ${ROOT_DIR}/tools/protoc-gen-mypy.py) + set(PROTOC_MYPY_PLUGIN_FILE ${ROOT_DIR}/tools/protoc-gen-mypy.py) + else(NOT WIN32) + set(PROTOC_MYPY_PLUGIN_FILE ${ROOT_DIR}/tools/protoc-gen-mypy.bat) + endif() + list(APPEND PROTOC_ARGS + --plugin + protoc-gen-mypy=${PROTOC_MYPY_PLUGIN_FILE} + --mypy_out + ${ONNX_DLLEXPORT_STR}${CMAKE_CURRENT_BINARY_DIR}) + endif() + endif() + if(NOT ONNX_PROTOC_EXECUTABLE) + message(FATAL_ERROR "Protobuf compiler not found") + endif() + if(ONNX_PROTO_POST_BUILD_SCRIPT) + add_custom_command( + OUTPUT "${OUTPUT_PB_SRC}" "${OUTPUT_PB_HEADER}" + COMMAND ${ONNX_PROTOC_EXECUTABLE} ARGS ${PROTOC_ARGS} + COMMAND "${CMAKE_COMMAND}" -DFILENAME=${OUTPUT_PB_HEADER} + -DNAMESPACES=${ONNX_NAMESPACE} -P + ${ONNX_PROTO_POST_BUILD_SCRIPT} + COMMAND "${CMAKE_COMMAND}" -DFILENAME=${OUTPUT_PB_SRC} + -DNAMESPACES=${ONNX_NAMESPACE} -P + ${ONNX_PROTO_POST_BUILD_SCRIPT} + DEPENDS ${GENERATED_PROTO} ${DEPEND} + COMMENT "Running C++ protocol buffer compiler on ${GENERATED_PROTO}" + VERBATIM) + else() + add_custom_command( + OUTPUT "${OUTPUT_PB_SRC}" "${OUTPUT_PB_HEADER}" + COMMAND ${ONNX_PROTOC_EXECUTABLE} ARGS ${PROTOC_ARGS} + DEPENDS ${GENERATED_PROTO} ${DEPEND} + COMMENT "Running C++ protocol buffer compiler on ${GENERATED_PROTO}" + VERBATIM) + endif() + add_custom_target(${NAME} DEPENDS ${OUTPUT_PB_SRC} ${OUTPUT_PB_HEADER}) + endforeach() + + set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) + set(${SRCS} ${${SRCS}} PARENT_SCOPE) + set(${HDRS} ${${HDRS}} PARENT_SCOPE) +endfunction() + +relative_protobuf_generate_cpp(gen_onnx_proto + PROTO_SRCS + PROTO_HDRS + ${ONNX_ROOT} + "" + onnx/onnx.in.proto) +relative_protobuf_generate_cpp(gen_onnx_operators_proto + PROTO_SRCS2 + PROTO_HDRS2 + ${ONNX_ROOT} + gen_onnx_proto + onnx/onnx-operators.in.proto) +list(APPEND PROTO_SRCS ${PROTO_SRCS2}) +list(APPEND PROTO_HDRS ${PROTO_HDRS2}) + +file(GLOB_RECURSE onnx_src "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc") +file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h" + "${ONNX_ROOT}/onnx/test/cpp/*.cc") +list(REMOVE_ITEM onnx_src "${ONNX_ROOT}/onnx/cpp2py_export.cc") +list(REMOVE_ITEM onnx_src ${onnx_gtests_src}) + +add_library(onnx_proto ${PROTO_SRCS} ${PROTO_HDRS}) +target_include_directories(onnx_proto PUBLIC + $ + $ + $) + + +if(ONNX_USE_LITE_PROTO) + if(TARGET protobuf::libprotobuf-lite) + target_link_libraries(onnx_proto PUBLIC protobuf::libprotobuf-lite) + else() + target_link_libraries(onnx_proto PUBLIC ${PROTOBUF_LITE_LIBRARIES}) + endif() +else() + if(TARGET protobuf::libprotobuf) + target_link_libraries(onnx_proto PUBLIC protobuf::libprotobuf) + else() + target_link_libraries(onnx_proto PUBLIC ${PROTOBUF_LIBRARIES}) + endif() +endif() +add_onnx_global_defines(onnx_proto) + +add_library(onnx ${onnx_src}) +target_include_directories(onnx PUBLIC + $ + $ + $) +target_link_libraries(onnx PUBLIC onnx_proto) +add_onnx_global_defines(onnx) + +if(BUILD_ONNX_PYTHON) + if("${PY_EXT_SUFFIX}" STREQUAL "") + if(MSVC) + set(PY_EXT_SUFFIX ".pyd") + else() + set(PY_EXT_SUFFIX ".so") + endif() + endif() + + add_library(onnx_cpp2py_export MODULE "${ONNX_ROOT}/onnx/cpp2py_export.cc") + set_target_properties(onnx_cpp2py_export PROPERTIES PREFIX "") + set_target_properties(onnx_cpp2py_export + PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") + set_target_properties(onnx_cpp2py_export PROPERTIES SUFFIX ${PY_EXT_SUFFIX}) + set_target_properties(onnx_cpp2py_export + PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + target_include_directories(onnx_cpp2py_export PRIVATE + $ + $ + $ + ${PYTHON_INCLUDE_DIR}) + + # pybind11 is a header only lib + find_package(pybind11 2.2) + if(pybind11_FOUND) + target_include_directories(onnx_cpp2py_export PUBLIC + ${pybind11_INCLUDE_DIRS}) + else() + if(EXISTS ${ONNX_ROOT}/third_party/pybind11/include/pybind11/pybind11.h) + target_include_directories(onnx_cpp2py_export PUBLIC + ${ONNX_ROOT}/third_party/pybind11/include) + else() + message(FATAL_ERROR "cannot find pybind") + endif() + endif() + + if(APPLE) + set_target_properties(onnx_cpp2py_export + PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") + target_link_libraries(onnx_cpp2py_export + PRIVATE -Wl,-force_load,$) + elseif(MSVC) + # In MSVC, we will add whole archive in default + target_link_libraries(onnx_cpp2py_export + PRIVATE -WHOLEARCHIVE:$) + else() + # Assume everything else is like gcc + target_link_libraries(onnx_cpp2py_export + PRIVATE "-Wl,--whole-archive" $ + "-Wl,--no-whole-archive") + endif() + + target_link_libraries(onnx_cpp2py_export PRIVATE onnx) + + if(MSVC) + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set(MP_FLAG "-Xclang" "-fopenmp") + set(EXTRA_FLAGS + "-Wno-implicit-function-declaration" + "-Wno-undefined-inline" + "-Wno-incompatible-pointer-types" + "-Wno-dllexport-explicit-instantiation-decl" + "-Wno-microsoft-unqualified-friend" + "-Wno-absolute-value" + "-Wno-unused-variable" + "-Wno-writable-strings" + "-Qunused-arguments") + else() + set(MP_FLAG "/MP") + set(EXTRA_FLAGS "") + endif() + find_package(PythonInterp ${PY_VERSION} REQUIRED) + find_package(PythonLibs ${PY_VERSION} REQUIRED) + target_link_libraries(onnx_cpp2py_export PRIVATE ${PYTHON_LIBRARIES}) + target_compile_options(onnx_cpp2py_export + PRIVATE ${MP_FLAG} + /WX + /wd4800 # disable warning type' : forcing + # value to bool 'true' or 'false' + # (performance warning) + /wd4503 # identifier' : decorated name length + # exceeded, name was truncated + /wd4146 # unary minus operator applied to + # unsigned type, result still + # unsigned from include\google\protob + # uf\wire_format_lite.h + ${EXTRA_FLAGS}) + target_compile_options(onnx_cpp2py_export PRIVATE /MT) + add_onnx_global_defines(onnx_cpp2py_export) + endif() +endif() + +if(ONNX_BUILD_BENCHMARKS) + if(NOT TARGET benchmark) + # We will not need to test benchmark lib itself. + set(BENCHMARK_ENABLE_TESTING OFF + CACHE BOOL "Disable benchmark testing as we don't need it.") + # We will not need to install benchmark since we link it statically. + set(BENCHMARK_ENABLE_INSTALL OFF + CACHE BOOL + "Disable benchmark install to avoid overwriting vendor install.") + add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/benchmark) + endif() + + add_executable(protobuf-bench tools/protobuf-bench.cc) + target_include_directories(protobuf-bench PUBLIC + $ + $ + $ + $) + target_link_libraries(protobuf-bench onnx_proto benchmark) +endif() + +# Export include directories +set(ONNX_INCLUDE_DIRS "${ONNX_ROOT}" "${CMAKE_CURRENT_BINARY_DIR}") +get_directory_property(hasParent PARENT_DIRECTORY) +if(hasParent) + set(ONNX_INCLUDE_DIRS ${ONNX_INCLUDE_DIRS} PARENT_SCOPE) +endif() + +if(MSVC) + target_compile_options(onnx_proto + PRIVATE ${MP_FLAG} + /WX + /wd4800 # disable warning type' : forcing value + # to bool 'true' or 'false' + # (performance warning) + /wd4503 # identifier' : decorated name length + # exceeded, name was truncated + /wd4146 # unary minus operator applied to + # unsigned type, result still unsigned: + # include\google\protobuf\wire_format_l + # ite.h + ${EXTRA_FLAGS}) + target_compile_options(onnx + PRIVATE ${MP_FLAG} + /WX + /wd4800 # disable warning type' : forcing value + # to bool 'true' or 'false' + # (performance warning) + /wd4503 # identifier' : decorated name length + # exceeded, name was truncated + /wd4146 # unary minus operator applied to + # unsigned type, result still unsigned + ${EXTRA_FLAGS}) + add_msvc_runtime_flag(onnx_proto) + add_msvc_runtime_flag(onnx) + set(onnx_static_library_flags + -IGNORE:4221 # LNK4221: This object file does not define any previously + # undefined public symbols, so it will not be used by any + # link operation that consumes this library + ) + set_target_properties(onnx + PROPERTIES STATIC_LIBRARY_FLAGS + "${onnx_static_library_flags}") +elseif(APPLE) + +else() + if(${ONNX_WERROR}) + target_compile_options(onnx PRIVATE -Werror=sign-compare -Werror=conversion) + endif() +endif() + +if(APPLE) + set_target_properties(onnx PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") +endif() + +# ---[ ONNX Interface for Framework Integratin (ONNXIFI) +add_library(onnxifi INTERFACE) +target_include_directories(onnxifi INTERFACE + $ + $) + +# ---[ ONNXIFI loader +add_library(onnxifi_loader STATIC onnx/onnxifi_loader.c) +target_include_directories(onnxifi_loader PUBLIC + $ + $) +# Users of ONNX backend API would compile it with their toolchain, so it is +# implemented in standard C89 for maximum compatibility +set_target_properties(onnxifi_loader + PROPERTIES C_STANDARD + 90 + C_EXTENSIONS + NO) +target_link_libraries(onnxifi_loader PUBLIC onnxifi ${CMAKE_DL_LIBS}) +if(MSVC) + add_msvc_runtime_flag(onnxifi_loader) +endif() + +if (NOT ANDROID AND NOT IOS) + # ---[ ONNXIFI wrapper + add_library(onnxifi_wrapper MODULE onnx/onnxifi_wrapper.c) + target_include_directories(onnxifi_wrapper PRIVATE + $ + $) + set_target_properties(onnxifi_wrapper PROPERTIES + C_STANDARD 99 + C_EXTENSIONS NO + OUTPUT_NAME "onnxifi" + POSITION_INDEPENDENT_CODE YES) + target_link_libraries(onnxifi_wrapper PRIVATE onnxifi_loader onnxifi) + if(DEFINED ONNXIFI_SEARCH_DIR) + target_compile_definitions(onnxifi_wrapper PRIVATE "ONNXIFI_SEARCH_DIR=\"${ONNXIFI_SEARCH_DIR}\"") + endif() + if(WIN32) + if(MSVC) + target_compile_definitions(onnxifi_wrapper PRIVATE "ONNXIFI_PUBLIC=__declspec(dllexport)") + else() + target_compile_definitions(onnxifi_wrapper PRIVATE "ONNXIFI_PUBLIC=__attribute__((__dllexport__))") + endif() + endif() + if(APPLE) + # By default CMake would use .so suffix on Mac + set_target_properties(onnxifi_wrapper PROPERTIES SUFFIX ".dylib") + endif() +endif() + +# ---[ ONNXIFI dummy backend +add_library(onnxifi_dummy SHARED onnx/onnxifi_dummy.c) +target_include_directories(onnxifi_dummy PRIVATE + $ + $) +target_link_libraries(onnxifi_dummy PUBLIC onnxifi ${CMAKE_DL_LIBS}) +target_compile_definitions(onnxifi_dummy PRIVATE ONNXIFI_BUILD_LIBRARY=TRUE) +if(MSVC) + add_msvc_runtime_flag(onnxifi_dummy) +endif() + +install(DIRECTORY ${ONNX_ROOT}/onnx + DESTINATION include + FILES_MATCHING + PATTERN "*.h") +install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/onnx + DESTINATION include + FILES_MATCHING + PATTERN "*.h") + +configure_file( + ${PROJECT_SOURCE_DIR}/cmake/ONNXConfigVersion.cmake.in + ${PROJECT_BINARY_DIR}/ONNXConfigVersion.cmake + @ONLY) +configure_file( + ${PROJECT_SOURCE_DIR}/cmake/ONNXConfig.cmake.in + ${PROJECT_BINARY_DIR}/ONNXConfig.cmake + @ONLY) +install(FILES + ${PROJECT_BINARY_DIR}/ONNXConfigVersion.cmake + ${PROJECT_BINARY_DIR}/ONNXConfig.cmake + DESTINATION share/cmake/ONNX + COMPONENT dev) +install(EXPORT ONNXTargets DESTINATION share/cmake/ONNX) +install(TARGETS + onnx onnx_proto + onnxifi onnxifi_dummy onnxifi_loader + EXPORT ONNXTargets DESTINATION lib) + +if(NOT ANDROID AND NOT IOS) + install(TARGETS onnxifi_wrapper + EXPORT ONNXTargets DESTINATION lib) +endif() + +if(ONNX_BUILD_TESTS) + include(${ONNX_ROOT}/cmake/unittest.cmake) +endif() + +include(cmake/summary.cmake) +onnx_print_configuration_summary() diff --git a/menoh/CMakeLists.txt b/menoh/CMakeLists.txt index d420997..a4fe9e0 100644 --- a/menoh/CMakeLists.txt +++ b/menoh/CMakeLists.txt @@ -3,6 +3,9 @@ option(LINK_STATIC_LIBGCC "Link static libgcc to libmenoh" OFF) option(LINK_STATIC_LIBSTDCXX "Link static libstdc++ to libmenoh" OFF) # Note: The libraries can be static (.a) or shared (.so) +if(NOT DEFINED PROTOBUF_INCLUDE_DIRS) + message(FATAL_ERROR "PROTOBUF_INCLUDE_DIRS is not found") +endif() if(NOT DEFINED MKLDNN_LIBRARIES) message(FATAL_ERROR "MKLDNN_LIBRARIES is not found") endif() @@ -11,7 +14,9 @@ file(GLOB_RECURSE SOURCES "." "*.cpp") # Create a object library for generating shared library add_library(menoh_objlib OBJECT ${SOURCES}) + add_dependencies(menoh_objlib gen_onnx_proto) # custom target defined in onnx +target_include_directories(menoh_objlib PUBLIC $) set_target_properties(menoh_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f118d7f..96c18ff 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,6 +25,8 @@ add_executable(menoh_test #vgg16.cpp ) +target_include_directories(menoh_test PUBLIC $) + if(LINK_STATIC_LIBPROTOBUF) target_link_libraries(menoh_test gtest_main menoh_test_target) else() From 7ce597e4232066bd511efa703c851fe2fce54921 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Wed, 3 Oct 2018 15:11:50 +0900 Subject: [PATCH 47/53] add EXCLUDE_FROM_ALL to external projects --- CMakeLists.txt | 2 +- test/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cce354..6aa884e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,7 +79,7 @@ configure_file(${EXTERNAL_DIR}/onnx-v1.3.0-patch_CMakeLists.txt ${EXTERNAL_DIR}/ # TODO: enable the following option when it is ready for migrating to onnx-ml #set(ONNX_ML 1) -add_subdirectory(external/onnx) # Note: BUILD_SHARED_LIBS must be OFF in this place +add_subdirectory(external/onnx EXCLUDE_FROM_ALL) # Note: BUILD_SHARED_LIBS must be OFF in this place include_directories("${ONNX_INCLUDE_DIRS}") # Setup MKLDNN diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 96c18ff..866092c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,7 +4,7 @@ enable_testing() set(GTEST_DIR "lib/googletest") execute_process(COMMAND git submodule update --init -- test/${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) message(STATUS "Adding ${GTEST_DIR}") -add_subdirectory(${GTEST_DIR}) +add_subdirectory(${GTEST_DIR} EXCLUDE_FROM_ALL) # filesystem setup set(FILESYSTEM_DIR "lib/filesystem") From 9d91a14f3c24d9d9333d7f2bcfb9fba327b20842 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Wed, 3 Oct 2018 15:16:08 +0900 Subject: [PATCH 48/53] add ignore=dirty option to external/onnx --- .gitmodules | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 17edb59..056710b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "external/onnx"] path = external/onnx - url = https://github.com/onnx/onnx + url = https://github.com/onnx/onnx.git + ignore = dirty [submodule "test/lib/googletest"] path = test/lib/googletest url = https://github.com/google/googletest.git From 6a0fb845f599a282fc3d0b2cc9ed242d639313f0 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Wed, 3 Oct 2018 17:54:37 +0900 Subject: [PATCH 49/53] update BUILDING.md --- BUILDING.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index a308f45..19d8c63 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -53,10 +53,9 @@ Download and unzip https://github.com/protocolbuffers/protobuf/releases/download ``` cd protobuf-3.6.1/cmake -mdir build +mkdir build cd build cmake .. -G "Visual Studio 14" -A x64 -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=(CMake_Install_Dir) -cmake --build . --config Debug --target install cmake --build . --config Release --target install cd ../../.. ``` @@ -67,10 +66,9 @@ git clone https://github.com/intel/mkl-dnn.git cd mkl-dnn/scripts .\prepare_mkl.bat cd .. -mdir build +mkdir build cd build cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX=(CMake_Install_Dir) -cmake --build . --config Debug --target install cmake --build . --config Release --target install cd ../.. ``` @@ -177,10 +175,9 @@ Please replace `(CMake_Install_Dir)` in the following with your working director ``` git clone https://github.com/pfnet-research/menoh.git cd menoh -mdir build +mkdir build cd build -cmake .. -G "Visual Studio 14 Win64" -DCMAKE_PREFIX_PATH=CMake_Install_Dir) -DCMAKE_INSTALL_PREFIX=CMake_Install_Dir) -DENABLE_TEST=OFF -DENABLE_BENCHMARK=OFF -DENABLE_EXAMPLE=OFF -DENABLE_TOOL=OFF -cmake --build . --config Debug --target install +cmake .. -G "Visual Studio 14 Win64" -DCMAKE_PREFIX_PATH=(CMake_Install_Dir) -DCMAKE_INSTALL_PREFIX=(CMake_Install_Dir) -DENABLE_TEST=OFF -DENABLE_BENCHMARK=OFF -DENABLE_EXAMPLE=OFF cmake --build . --config Release --target install ``` From c1573ed1084f4a6be12921b3e0766e9fbdf50ab7 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Thu, 4 Oct 2018 19:41:30 +0900 Subject: [PATCH 50/53] fix some environment specific bugs --- external/cmdline.h | 6 +++++- test/CMakeLists.txt | 6 ++++++ test/lib/googletest | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/external/cmdline.h b/external/cmdline.h index f498b66..f6b3b9b 100644 --- a/external/cmdline.h +++ b/external/cmdline.h @@ -39,8 +39,12 @@ #ifdef _MSC_VER #define NOMINMAX -#include +/* + You must include Windows.h before DbgHelp.h. + See https://stackoverflow.com/a/43283926/1014818 for more details. +*/ #include +#include #else #include #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 866092c..6224acb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,7 +3,13 @@ enable_testing() # GTest setup set(GTEST_DIR "lib/googletest") execute_process(COMMAND git submodule update --init -- test/${GTEST_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) + message(STATUS "Adding ${GTEST_DIR}") + +# Prevent overriding the parent project's compiler/linker settings on Windows +# See https://github.com/google/googletest/blob/master/googletest/README.md#visual-studio-dynamic-vs-static-runtimes +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + add_subdirectory(${GTEST_DIR} EXCLUDE_FROM_ALL) # filesystem setup diff --git a/test/lib/googletest b/test/lib/googletest index ba96d0b..2fe3bd9 160000 --- a/test/lib/googletest +++ b/test/lib/googletest @@ -1 +1 @@ -Subproject commit ba96d0b1161f540656efdaed035b3c062b60e006 +Subproject commit 2fe3bd994b3189899d93f1d5a881e725e046fdc2 From 38f65a8433e11edef039ecba47404264ed96936d Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Fri, 5 Oct 2018 14:02:13 +0900 Subject: [PATCH 51/53] Revert "add cross building support for protobuf" This reverts commit 08289fcf5b99e470e11eb09669ec13a57a4f811c. --- scripts/build-boost.sh | 120 -------------------------------------- scripts/build-menoh.sh | 4 +- scripts/build-protobuf.sh | 18 +----- scripts/install-boost.sh | 35 ----------- 4 files changed, 3 insertions(+), 174 deletions(-) delete mode 100644 scripts/build-boost.sh delete mode 100644 scripts/install-boost.sh diff --git a/scripts/build-boost.sh b/scripts/build-boost.sh deleted file mode 100644 index 183247f..0000000 --- a/scripts/build-boost.sh +++ /dev/null @@ -1,120 +0,0 @@ -#!/bin/bash -e - -# retrieve arguments -while [[ $# != 0 ]]; do - case $1 in - --) - shift - break - ;; - --version) - readonly ARG_VERSION="$2" - shift 2 - ;; - --download-dir) - readonly ARG_DOWNLOAD_DIR="$2" - shift 2 - ;; - --extract-dir) - readonly ARG_EXTRACT_DIR="$2" - shift 2 - ;; - --build-dir) - readonly ARG_BUILD_DIR="$2" - shift 2 - ;; - --stage-dir) - readonly ARG_STAGE_DIR="$2" - shift 2 - ;; - --install-dir) - readonly ARG_INSTALL_DIR="$2" - shift 2 - ;; - --toolset) - readonly ARG_TOOLSET="$2" - shift 2 - ;; - --cxx) - readonly ARG_CXX_CMD="$2" - shift 2 - ;; - --parallel) - readonly ARG_PARALLEL="$2" - shift 2 - ;; - -*) - echo Unknown option \"$1\" 1>&2 - exit - ;; - *) - break - ;; - esac -done - -# validate the arguments -test -n "${ARG_DOWNLOAD_DIR}" || { echo "--download-dir is not specified" 1>&2; exit 1; } -test -n "${ARG_EXTRACT_DIR}" || { echo "--extract-dir is not specified" 1>&2; exit 1; } - -# options that have default value -test -n "${ARG_VERSION}" || readonly ARG_VERSION=1.64.0 - -readonly LIBRARY_NAME=boost_$(echo ${ARG_VERSION} | sed -e 's/\./_/g') # e.g. boost_1_64_0 -readonly SOURCE_DIR="${ARG_EXTRACT_DIR}/${LIBRARY_NAME}" - -test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}/build" -test -n "${ARG_STAGE_DIR}" || readonly ARG_STAGE_DIR="${SOURCE_DIR}/stage" -test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local -test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 - -# options for cross compiling -if [ -n "${ARG_TOOLSET}" ]; then # e.g. gcc-arm - # requires a location of g++ command - test -n "${ARG_CXX_CMD}" || { echo "--cxx is not specified" 1>&2; exit 1; } - - readonly USER_CONFIG_JAM="using $(echo ${ARG_TOOLSET} | sed -e 's/-/ : /') : ${ARG_CXX_CMD} ;" - readonly OPT_TOOLSET=toolset=${ARG_TOOLSET} -fi - -# download (if it isn't cached) -if [ ! -e "${SOURCE_DIR}/INSTALL" ]; then - echo -e "\e[33;1mDownloading libboost\e[0m" - - [ -d "${ARG_DOWNLOAD_DIR}" ] || mkdir -p "${ARG_DOWNLOAD_DIR}" - - cd "${ARG_DOWNLOAD_DIR}" - if [ ! -e "${LIBRARY_NAME}.tar.gz" ]; then - download_dir="https://dl.bintray.com/boostorg/release/${ARG_VERSION}/source/${LIBRARY_NAME}.tar.gz" - curl -LO ${download_dir} # wget doesn't work for bintray with 403 errpr - fi - tar -zxf ${LIBRARY_NAME}.tar.gz -C "${ARG_EXTRACT_DIR}" - - echo -e "\e[32;1mlibboost was successfully downloaded.\e[0m" -else - echo -e "\e[32;1mlibboost has been downloaded.\e[0m" -fi - -# build (if it isn't cached) -if [ ! -e "${ARG_STAGE_DIR}/lib/libboost_system.a" ]; then - echo -e "\e[33;1mBuilding libprotobuf\e[0m" - - cd "${SOURCE_DIR}" - - # bootstrap - ./bootstrap.sh --prefix="${ARG_INSTALL_DIR}" - - # configure options - if [ -n "${USER_CONFIG_JAM}" ]; then - echo "${USER_CONFIG_JAM}" > ${SOURCE_DIR}/user_config.jam - readonly OPT_USER_CONFIG_JAM=--user-config=${SOURCE_DIR}/user_config.jam - fi - echo "-j${ARG_PARALLEL} ${OPT_TOOLSET} ${OPT_USER_CONFIG_JAM} link=static cflags=-fPIC cxxflags=-fPIC --with-filesystem --with-test --with-log --with-program_options --build-dir=${ARG_BUILD_DIR} --stage-dir=${ARG_STAGE_DIR}" > b2_opts.txt - - # run a build - ./b2 stage $(cat b2_opts.txt) - - echo -e "\e[32;1mlibboost was successfully built.\e[0m" -else - echo -e "\e[32;1mlibboost has been built.\e[0m" -fi diff --git a/scripts/build-menoh.sh b/scripts/build-menoh.sh index 5184c4e..5740377 100755 --- a/scripts/build-menoh.sh +++ b/scripts/build-menoh.sh @@ -63,8 +63,8 @@ test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${ARG_SOURCE_DIR}/build" test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local if [ -n "${ARG_MKLDNN_DIR}" ]; then - readonly OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include - readonly OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so + OPT_MKLDNN_INCLUDE_DIR=-DMKLDNN_INCLUDE_DIR=${ARG_MKLDNN_DIR}/include + OPT_MKLDNN_LIBRARY=-DMKLDNN_LIBRARY=${ARG_MKLDNN_DIR}/lib/libmkldnn.so fi test -n "${ARG_PYTHON_EXECUTABLE}" || readonly ARG_PYTHON_EXECUTABLE=python diff --git a/scripts/build-protobuf.sh b/scripts/build-protobuf.sh index f1fdb34..f694b8b 100755 --- a/scripts/build-protobuf.sh +++ b/scripts/build-protobuf.sh @@ -27,14 +27,6 @@ while [[ $# != 0 ]]; do readonly ARG_INSTALL_DIR="$2" shift 2 ;; - --host) - readonly ARG_HOST="$2" - shift 2 - ;; - --with-protoc) - readonly ARG_WITH_PROTOC="$2" - shift 2 - ;; --parallel) readonly ARG_PARALLEL="$2" shift 2 @@ -63,14 +55,6 @@ test -n "${ARG_BUILD_DIR}" || readonly ARG_BUILD_DIR="${SOURCE_DIR}" test -n "${ARG_INSTALL_DIR}" || readonly ARG_INSTALL_DIR=/usr/local test -n "${ARG_PARALLEL}" || readonly ARG_PARALLEL=1 -# options for cross compiling -if [ -n "${ARG_HOST}" ]; then - readonly OPT_HOST=--host=${ARG_HOST} -fi -if [ -n "${ARG_WITH_PROTOC}" ]; then - readonly OPT_WITH_PROTOC=--with-protoc=${ARG_WITH_PROTOC} -fi - # download (if it isn't cached) if [ ! -e "${SOURCE_DIR}/LICENSE" ]; then echo -e "\e[33;1mDownloading libprotobuf\e[0m" @@ -96,7 +80,7 @@ if [ ! -e "${ARG_BUILD_DIR}/src/libprotobuf.la" ]; then [ -d "${ARG_BUILD_DIR}" ] || mkdir -p "${ARG_BUILD_DIR}" cd "${ARG_BUILD_DIR}" - "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS="-g -O2 -fPIC" CXXFLAGS="-g -O2 -fPIC" "${OPT_HOST}" "${OPT_WITH_PROTOC}" + "${SOURCE_DIR}/configure" --prefix="${ARG_INSTALL_DIR}" CFLAGS="-g -O2 -fPIC" CXXFLAGS="-g -O2 -fPIC" make -j${ARG_PARALLEL} echo -e "\e[32;1mlibprotobuf was successfully built.\e[0m" diff --git a/scripts/install-boost.sh b/scripts/install-boost.sh deleted file mode 100644 index fdb3a13..0000000 --- a/scripts/install-boost.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -e - -# retrieve arguments -while [[ $# != 0 ]]; do - case $1 in - --) - shift - break - ;; - --source-dir) - readonly ARG_SOURCE_DIR="$2" - shift 2 - ;; - --dest-dir) - readonly ARG_DESTDIR="$2" - shift 2 - ;; - -*) - echo Unknown option \"$1\" 1>&2 - exit - ;; - *) - break - ;; - esac -done - -# validate the arguments -test -n "${ARG_SOURCE_DIR}" || { echo "--source-dir is not specified" 1>&2; exit 1; } - -# install (always) -echo -e "\e[33;1mInstalling libboost\e[0m" - -cd "${ARG_SOURCE_DIR}" -./b2 install $(cat ${ARG_SOURCE_DIR}/b2_opts.txt) From 8e8e487d97c8056699ba8f2f149606a2565b671e Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Fri, 5 Oct 2018 14:06:38 +0900 Subject: [PATCH 52/53] fix an indent --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 056710b..19ec233 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,7 @@ [submodule "external/onnx"] path = external/onnx url = https://github.com/onnx/onnx.git - ignore = dirty + ignore = dirty [submodule "test/lib/googletest"] path = test/lib/googletest url = https://github.com/google/googletest.git From 2d0d5f42ae189f46ddc1b69c31ce8103767b742c Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Fri, 5 Oct 2018 14:59:50 +0900 Subject: [PATCH 53/53] add python to prerequisites --- BUILDING.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/BUILDING.md b/BUILDING.md index 19d8c63..492fb45 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -5,18 +5,25 @@ You need to install [prerequisites](#prerequisites) for your platform before [bu To build Menoh, you require the following toolchains: Unix: -- CMake 3.1 or later - GCC 4.9 or later +- CMake 3.1 or later +- Python 2.7 or later macOS (OSX): - XCode - [Homebrew](https://brew.sh/) +- CMake 3.1 or later +- Python 2.7 or later Windows: - Visual Studio 2015 +- CMake 3.1 or later +- Python 2.7 or later Windows (MINGW): - [MSYS2](http://www.msys2.org/) +- CMake 3.1 or later +- Python 2.7 or later You also need to install the dependent libraries on your system: