From a09d45d9d5c270f246ab7a994d882c6767713c9e Mon Sep 17 00:00:00 2001 From: Tomasz Socha Date: Thu, 9 Jul 2020 15:30:17 +0200 Subject: [PATCH] [nGraph] Allow to use protobuf lite in onnx importer (#687) --- CMakeLists.txt | 7 + ngraph/CMakeLists.txt | 3 + ngraph/cmake/external_onnx.cmake | 11 +- ngraph/cmake/external_protobuf.cmake | 222 +++++++++------------ .../src/ngraph/frontend/onnx_import/CMakeLists.txt | 10 +- .../ngraph/frontend/onnx_import/core/attribute.hpp | 7 + .../ngraph/frontend/onnx_import/core/tensor.hpp | 14 ++ ngraph/src/ngraph/frontend/onnx_import/onnx.cpp | 4 + .../ngraph/frontend/onnx_import/utils/common.cpp | 4 + ngraph/test/CMakeLists.txt | 8 +- 10 files changed, 153 insertions(+), 137 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 54fdc35..afcf04a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,13 @@ function(build_ngraph) elseif(WIN32) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4308 /wd4146 /wd4703 /wd4244 /wd4819") endif() + + # Preserve the original flags for further use + set(CMAKE_ORIGINAL_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") + set(CMAKE_ORIGINAL_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") + set(CMAKE_ORIGINAL_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") + set(CMAKE_ORIGINAL_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") + set(CMAKE_ORIGINAL_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE}") if(ENABLE_LTO) ie_enable_lto() diff --git a/ngraph/CMakeLists.txt b/ngraph/CMakeLists.txt index 356ce18..a2af18d 100644 --- a/ngraph/CMakeLists.txt +++ b/ngraph/CMakeLists.txt @@ -127,6 +127,7 @@ option(NGRAPH_WARNINGS_AS_ERRORS "Make all nGraph compile-time warnings into err option(NGRAPH_ADDRESS_SANITIZER_ENABLE "Compiles and links with Address Sanitizer" FALSE) option(NGRAPH_THREAD_SANITIZER_ENABLE "Compiles and links with Thread Sanitizer" FALSE) option(NGRAPH_UB_SANITIZER_ENABLE "Compiles and links with Undefined Behavior Sanitizer" FALSE) +option(NGRAPH_USE_PROTOBUF_LITE "Compiles and links with protobuf-lite" FALSE) if (NGRAPH_ONNX_IMPORT_ENABLE) option(NGRAPH_USE_SYSTEM_PROTOBUF "Use system provided Protobuf shared object" FALSE) @@ -165,6 +166,7 @@ NORMALIZE_BOOL(NGRAPH_WARNINGS_AS_ERRORS) NORMALIZE_BOOL(NGRAPH_ADDRESS_SANITIZER_ENABLE) NORMALIZE_BOOL(NGRAPH_THREAD_SANITIZER_ENABLE) NORMALIZE_BOOL(NGRAPH_UB_SANITIZER_ENABLE) +NORMALIZE_BOOL(NGRAPH_USE_PROTOBUF_LITE) message(STATUS "NGRAPH_ADDRESS_SANITIZER_ENABLE: ${NGRAPH_ADDRESS_SANITIZER_ENABLE}") message(STATUS "NGRAPH_CODE_COVERAGE_ENABLE: ${NGRAPH_CODE_COVERAGE_ENABLE}") @@ -183,6 +185,7 @@ message(STATUS "NGRAPH_TEST_UTIL_ENABLE: ${NGRAPH_TEST_UTIL_ENABLE} message(STATUS "NGRAPH_THREAD_SANITIZER_ENABLE: ${NGRAPH_THREAD_SANITIZER_ENABLE}") message(STATUS "NGRAPH_TOOLS_ENABLE: ${NGRAPH_TOOLS_ENABLE}") message(STATUS "NGRAPH_UB_SANITIZER_ENABLE: ${NGRAPH_UB_SANITIZER_ENABLE}") +message(STATUS "NGRAPH_USE_PROTOBUF_LITE: ${NGRAPH_USE_PROTOBUF_LITE}") message(STATUS "NGRAPH_UNIT_TEST_ENABLE: ${NGRAPH_UNIT_TEST_ENABLE}") message(STATUS "NGRAPH_WARNINGS_AS_ERRORS: ${NGRAPH_WARNINGS_AS_ERRORS}") diff --git a/ngraph/cmake/external_onnx.cmake b/ngraph/cmake/external_onnx.cmake index 3f8c4c5..ffd0f9e 100644 --- a/ngraph/cmake/external_onnx.cmake +++ b/ngraph/cmake/external_onnx.cmake @@ -34,9 +34,6 @@ set(ONNX_GIT_REPO_URL https://github.com/onnx/onnx.git) set(ONNX_GIT_BRANCH rel-${ONNX_VERSION}) set(NGRAPH_ONNX_NAMESPACE ngraph_onnx) -add_definitions(-DONNX_BUILD_SHARED_LIBS=ON) -add_definitions(-DONNX_NAMESPACE=${NGRAPH_ONNX_NAMESPACE}) - set(CMAKE_CXX_FLAGS ${CMAKE_ORIGINAL_CXX_FLAGS}) FetchContent_Declare( @@ -48,16 +45,18 @@ FetchContent_Declare( FetchContent_GetProperties(ext_onnx) if(NOT ext_onnx_POPULATED) FetchContent_Populate(ext_onnx) - set(ONNX_GEN_PB_TYPE_STUBS OFF) set(ONNX_NAMESPACE ${NGRAPH_ONNX_NAMESPACE}) + set(ONNX_USE_LITE_PROTO ${NGRAPH_USE_PROTOBUF_LITE} CACHE BOOL "Use protobuf lite for ONNX library") + set(ONNX_ML ON CACHE BOOL "Use ONNX ML") + set(ONNX_BUILD_SHARED_LIBS ON CACHE BOOL "Build ONNX as a shared library") if(CMAKE_CROSSCOMPILING) set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${SYSTEM_PROTOC}) endif() add_subdirectory(${ext_onnx_SOURCE_DIR} ${ext_onnx_BINARY_DIR} EXCLUDE_FROM_ALL) endif() -target_include_directories(onnx PRIVATE "${Protobuf_INCLUDE_DIR}") -target_include_directories(onnx_proto PRIVATE "${Protobuf_INCLUDE_DIR}") +target_include_directories(onnx PRIVATE "${Protobuf_INCLUDE_DIRS}") +target_include_directories(onnx_proto PRIVATE "${Protobuf_INCLUDE_DIRS}") if(MSVC) target_compile_options(onnx PRIVATE /WX-) diff --git a/ngraph/cmake/external_protobuf.cmake b/ngraph/cmake/external_protobuf.cmake index bcee993..d4ee0dd 100644 --- a/ngraph/cmake/external_protobuf.cmake +++ b/ngraph/cmake/external_protobuf.cmake @@ -14,13 +14,35 @@ # limitations under the License. # ****************************************************************************** -# Enable ExternalProject CMake module -include(ExternalProject) +include(FetchContent) #------------------------------------------------------------------------------ # Download and install Google Protobuf ... #------------------------------------------------------------------------------ +# Since this file is going to be modifying CMAKE_*_FLAGS we need to preserve +# it so we won't overwrite the caller's CMAKE_*_FLAGS +set(PUSH_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) +set(PUSH_CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") +set(PUSH_CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") +set(PUSH_CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") +set(PUSH_CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") +set(PUSH_CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE}") + +set(CMAKE_CXX_FLAGS ${CMAKE_ORIGINAL_CXX_FLAGS}) +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_ORIGINAL_CXX_FLAGS_RELEASE}") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_ORIGINAL_C_FLAGS_RELEASE}") +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_ORIGINAL_EXE_LINKER_FLAGS_RELEASE}") +set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_ORIGINAL_SHARED_LINKER_FLAGS_RELEASE}") +set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_ORIGINAL_MODULE_LINKER_FLAGS_RELEASE}") + +if (MSVC) + string(REPLACE "/W3" "/W0" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error -fno-lto") +endif() + # This version of PROTOBUF is required by Microsoft ONNX Runtime. set(NGRAPH_PROTOBUF_GIT_REPO_URL "https://github.com/protocolbuffers/protobuf") @@ -37,30 +59,22 @@ if(CMAKE_CROSSCOMPILING) message(WARNING "Protobuf 3.0.0 detected switching to 3.0.2 due to bug in gmock url") set(PROTOC_VERSION "3.0.2") endif() - - set(PROTOBUF_SYSTEM_PROTOC --with-protoc=${SYSTEM_PROTOC}) - set(PROTOBUF_SYSTEM_PROCESSOR --host=${CMAKE_HOST_SYSTEM_PROCESSOR}) else() message(FATAL_ERROR "System Protobuf is needed while cross-compiling") endif() + + set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "Build libprotoc and protoc compiler" FORCE) +elseif(NGRAPH_USE_PROTOBUF_LITE) + set(PROTOC_VERSION "3.9.2") + if(ENABLE_LTO) + message(WARNING "Protobuf in version 3.8.0+ can throw runtime exceptions if LTO is enabled.") + endif() else() set(PROTOC_VERSION "3.7.1") endif() set(NGRAPH_PROTOBUF_GIT_TAG "v${PROTOC_VERSION}") -set(Protobuf_INSTALL_PREFIX ${EXTERNAL_PROJECTS_ROOT}/protobuf) -set(Protobuf_PROTOC_EXECUTABLE ${Protobuf_INSTALL_PREFIX}/bin/protoc) -set(Protobuf_INCLUDE_DIR ${Protobuf_INSTALL_PREFIX}/include) -if (WIN32) - if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobufd.lib) - else() - set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.lib) - endif() -else() - set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a) -endif() if ("${CMAKE_GENERATOR}" STREQUAL "Ninja") set(MAKE_UTIL make) @@ -68,91 +82,16 @@ else() set(MAKE_UTIL $(MAKE)) endif() -if (WIN32) - ExternalProject_Add( - ext_protobuf - PREFIX protobuf - GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL} - GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} - UPDATE_COMMAND "" - PATCH_COMMAND "" - CMAKE_GENERATOR ${CMAKE_GENERATOR} - CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM} - CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET} - CMAKE_ARGS - ${NGRAPH_FORWARD_CMAKE_ARGS} - -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DCMAKE_CXX_FLAGS=${CMAKE_ORIGINAL_CXX_FLAGS} - -Dprotobuf_MSVC_STATIC_RUNTIME=OFF - -Dprotobuf_WITH_ZLIB=OFF - -Dprotobuf_BUILD_TESTS=OFF - -DCMAKE_INSTALL_PREFIX=${EXTERNAL_PROJECTS_ROOT}/protobuf - TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" - STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" - DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/download" - SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" - SOURCE_SUBDIR "cmake" - BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/build" - INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" - EXCLUDE_FROM_ALL TRUE - BUILD_BYPRODUCTS ${Protobuf_PROTOC_EXECUTABLE} ${Protobuf_LIBRARY} - ) -elseif (APPLE) - # Don't manually set compiler on macos since it causes compile error on macos >= 10.14 - ExternalProject_Add( - ext_protobuf - PREFIX protobuf - GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL} - GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} - UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND ./autogen.sh COMMAND ./configure --prefix=${EXTERNAL_PROJECTS_ROOT}/protobuf --disable-shared - BUILD_COMMAND ${MAKE_UTIL} "CXXFLAGS=-std=c++${NGRAPH_CXX_STANDARD} -fPIC" - TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" - STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" - DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/download" - SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" - BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" - INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" - EXCLUDE_FROM_ALL TRUE - BUILD_BYPRODUCTS ${Protobuf_PROTOC_EXECUTABLE} ${Protobuf_LIBRARY} - ) +if(PROTOC_VERSION VERSION_LESS "3.9" AND NGRAPH_USE_PROTOBUF_LITE) + message(FATAL_ERROR "Minimum supported version of protobuf-lite library is 3.9.0") else() - if (DEFINED NGRAPH_USE_CXX_ABI) - set(BUILD_FLAGS "CXXFLAGS=-std=c++${NGRAPH_CXX_STANDARD} -fPIC -D_GLIBCXX_USE_CXX11_ABI=${NGRAPH_USE_CXX_ABI}") - else() - set(BUILD_FLAGS "CXXFLAGS=-std=c++${NGRAPH_CXX_STANDARD} -fPIC") - endif() - - if(PROTOC_VERSION VERSION_GREATER "3.0" AND CMAKE_CROSSCOMPILING) - ExternalProject_Add( - ext_protobuf - PREFIX protobuf - GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL} - GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} - UPDATE_COMMAND "" - PATCH_COMMAND "" - CMAKE_GENERATOR ${CMAKE_GENERATOR} - CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM} - CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET} - CMAKE_ARGS - ${NGRAPH_FORWARD_CMAKE_ARGS} - -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DCMAKE_CXX_FLAGS=${CMAKE_ORIGINAL_CXX_FLAGS} - -Dprotobuf_WITH_ZLIB=OFF - -Dprotobuf_BUILD_TESTS=OFF - -DCMAKE_INSTALL_PREFIX=${EXTERNAL_PROJECTS_ROOT}/protobuf - TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" - STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" - DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/download" - SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" - SOURCE_SUBDIR "cmake" - BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/build" - INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" - EXCLUDE_FROM_ALL TRUE - BUILD_BYPRODUCTS ${Protobuf_PROTOC_EXECUTABLE} ${Protobuf_LIBRARY} - ) - else() + if(CMAKE_CXX_COMPILER_ID MATCHES ".*[Cc]lang") + include(ExternalProject) + set(Protobuf_INSTALL_PREFIX ${EXTERNAL_PROJECTS_ROOT}/protobuf) + set(Protobuf_PROTOC_EXECUTABLE ${Protobuf_INSTALL_PREFIX}/bin/protoc) + set(Protobuf_INCLUDE_DIRS ${Protobuf_INSTALL_PREFIX}/include) + set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a) + # Don't manually set compiler on macos since it causes compile error on macos >= 10.14 ExternalProject_Add( ext_protobuf PREFIX protobuf @@ -160,8 +99,8 @@ else() GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} UPDATE_COMMAND "" PATCH_COMMAND "" - CONFIGURE_COMMAND ./autogen.sh COMMAND ./configure ${PROTOBUF_SYSTEM_PROTOC} ${PROTOBUF_SYSTEM_PROCESSOR} CXX=${CMAKE_CXX_COMPILER} --prefix=${EXTERNAL_PROJECTS_ROOT}/protobuf --disable-shared - BUILD_COMMAND ${MAKE_UTIL} "${BUILD_FLAGS}" + CONFIGURE_COMMAND ./autogen.sh COMMAND ./configure --prefix=${EXTERNAL_PROJECTS_ROOT}/protobuf --disable-shared + BUILD_COMMAND ${MAKE_UTIL} "CXXFLAGS=-std=c++${NGRAPH_CXX_STANDARD} -fPIC" TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/download" @@ -170,30 +109,63 @@ else() INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" EXCLUDE_FROM_ALL TRUE BUILD_BYPRODUCTS ${Protobuf_PROTOC_EXECUTABLE} ${Protobuf_LIBRARY} + ) + + # ----------------------------------------------------------------------------- + # Use the interface of FindProtobuf.cmake + # ----------------------------------------------------------------------------- + if (NOT TARGET protobuf::libprotobuf) + add_library(protobuf::libprotobuf UNKNOWN IMPORTED) + set_target_properties(protobuf::libprotobuf PROPERTIES + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}" + IMPORTED_LOCATION "${Protobuf_LIBRARY}") + add_dependencies(protobuf::libprotobuf ext_protobuf) + endif() + set(Protobuf_LIBRARIES protobuf::libprotobuf) + + if (NOT TARGET protobuf::protoc) + add_executable(protobuf::protoc IMPORTED) + set_target_properties(protobuf::protoc PROPERTIES + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_PROTOC_EXECUTABLE}" + IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}") + add_dependencies(protobuf::protoc ext_protobuf) + endif() + + set(Protobuf_FOUND TRUE) + set(PROTOBUF_FOUND TRUE) + #add_dependencies(onnx ext_protobuf) + else() + if(PROTOC_VERSION VERSION_GREATER_EQUAL "3.0") + FetchContent_Declare( + ext_protobuf + GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL} + GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} ) - endif() -endif() -# ----------------------------------------------------------------------------- -# Use the interface of FindProtobuf.cmake -# ----------------------------------------------------------------------------- + FetchContent_GetProperties(ext_protobuf) + if(NOT ext_protobuf_POPULATED) + FetchContent_Populate(ext_protobuf) + set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build tests") + set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build with zlib support") + add_subdirectory(${ext_protobuf_SOURCE_DIR}/cmake ${ext_protobuf_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() + else() + message(FATAL_ERROR "Minimum supported version of protobuf library is 3.0.0") + endif() -if (NOT TARGET protobuf::libprotobuf) - add_library(protobuf::libprotobuf UNKNOWN IMPORTED) - set_target_properties(protobuf::libprotobuf PROPERTIES - INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}" - IMPORTED_LOCATION "${Protobuf_LIBRARY}") - add_dependencies(protobuf::libprotobuf ext_protobuf) -endif() -set(Protobuf_LIBRARIES protobuf::libprotobuf) - -if (NOT TARGET protobuf::protoc) - add_executable(protobuf::protoc IMPORTED) - set_target_properties(protobuf::protoc PROPERTIES - INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_PROTOC_EXECUTABLE}" - IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}") - add_dependencies(protobuf::protoc ext_protobuf) + set(Protobuf_INCLUDE_DIRS ${ext_protobuf_SOURCE_DIR}) + if(NGRAPH_USE_PROTOBUF_LITE) + set(Protobuf_LIBRARIES libprotobuf-lite) + else() + set(Protobuf_LIBRARIES libprotobuf) + endif() + endif() endif() -set(Protobuf_FOUND TRUE) -set(PROTOBUF_FOUND TRUE) +# Now make sure we restore the original CMAKE_*_FLAGS for the caller +set(CMAKE_CXX_FLAGS ${PUSH_CMAKE_CXX_FLAGS}) +set(CMAKE_CXX_FLAGS_RELEASE "${PUSH_CMAKE_CXX_FLAGS_RELEASE}") +set(CMAKE_C_FLAGS_RELEASE "${PUSH_CMAKE_C_FLAGS_RELEASE}") +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${PUSH_CMAKE_EXE_LINKER_FLAGS_RELEASE}") +set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${PUSH_CMAKE_SHARED_LINKER_FLAGS_RELEASE}") +set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${PUSH_CMAKE_MODULE_LINKER_FLAGS_RELEASE}") diff --git a/ngraph/src/ngraph/frontend/onnx_import/CMakeLists.txt b/ngraph/src/ngraph/frontend/onnx_import/CMakeLists.txt index 36371e5..c265969 100644 --- a/ngraph/src/ngraph/frontend/onnx_import/CMakeLists.txt +++ b/ngraph/src/ngraph/frontend/onnx_import/CMakeLists.txt @@ -266,8 +266,8 @@ add_library(onnx_importer SHARED set(ONNX_IMPORT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") -target_link_libraries(onnx_importer PRIVATE onnx onnx_proto) -target_link_libraries(onnx_importer PUBLIC ngraph) +target_link_libraries(onnx_importer PRIVATE onnx onnx_proto ${Protobuf_LIBRARIES}) +target_link_libraries(onnx_importer PUBLIC ngraph) set_target_properties(onnx_importer PROPERTIES CXX_VISIBILITY_PRESET hidden @@ -277,10 +277,14 @@ set_target_properties(onnx_importer PROPERTIES target_include_directories(onnx_importer SYSTEM PUBLIC $ $) target_include_directories(onnx_importer SYSTEM PRIVATE ${NGRAPH_INCLUDE_PATH} - ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR}) + ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIRS}) target_compile_definitions(onnx_importer PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION}) +if(NGRAPH_USE_PROTOBUF_LITE) + target_compile_definitions(onnx_importer PRIVATE NGRAPH_USE_PROTOBUF_LITE) +endif() + if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$") target_compile_options(onnx_importer PRIVATE -Wno-undef -Wno-reserved-id-macro -Wno-switch-enum -Wno-invalid-offsetof -Wno-shorten-64-to-32 -Wno-unused-macros -Wno-missing-variable-declarations diff --git a/ngraph/src/ngraph/frontend/onnx_import/core/attribute.hpp b/ngraph/src/ngraph/frontend/onnx_import/core/attribute.hpp index fcbf190..d9151cf 100644 --- a/ngraph/src/ngraph/frontend/onnx_import/core/attribute.hpp +++ b/ngraph/src/ngraph/frontend/onnx_import/core/attribute.hpp @@ -45,11 +45,18 @@ namespace ngraph { struct Attribute : ngraph_error { +#ifdef NGRAPH_USE_PROTOBUF_LITE + Attribute(const std::string& msg, AttributeProto_AttributeType type) + : ngraph_error{msg} + { + } +#else Attribute(const std::string& msg, AttributeProto_AttributeType type) : ngraph_error{msg + ": " + ONNX_NAMESPACE::AttributeProto_AttributeType_Name(type)} { } +#endif }; } // namespace detail diff --git a/ngraph/src/ngraph/frontend/onnx_import/core/tensor.hpp b/ngraph/src/ngraph/frontend/onnx_import/core/tensor.hpp index 2c040d9..0f06004 100644 --- a/ngraph/src/ngraph/frontend/onnx_import/core/tensor.hpp +++ b/ngraph/src/ngraph/frontend/onnx_import/core/tensor.hpp @@ -41,22 +41,36 @@ namespace ngraph { struct invalid_data_type : ngraph_error { +#ifdef NGRAPH_USE_PROTOBUF_LITE + explicit invalid_data_type(TensorProto_DataType type) + : ngraph_error{"invalid data type"} + { + } +#else explicit invalid_data_type(TensorProto_DataType type) : ngraph_error{"invalid data type: " + ONNX_NAMESPACE::TensorProto_DataType_Name( static_cast(type))} { } +#endif }; struct unsupported_data_type : ngraph_error { +#ifdef NGRAPH_USE_PROTOBUF_LITE + explicit unsupported_data_type(TensorProto_DataType type) + : ngraph_error{"unsupported data type"} + { + } +#else explicit unsupported_data_type(TensorProto_DataType type) : ngraph_error{"unsupported data type: " + ONNX_NAMESPACE::TensorProto_DataType_Name( static_cast(type))} { } +#endif }; struct unspecified_name : ngraph_error diff --git a/ngraph/src/ngraph/frontend/onnx_import/onnx.cpp b/ngraph/src/ngraph/frontend/onnx_import/onnx.cpp index 602d4d3..8b5ac04 100644 --- a/ngraph/src/ngraph/frontend/onnx_import/onnx.cpp +++ b/ngraph/src/ngraph/frontend/onnx_import/onnx.cpp @@ -58,6 +58,9 @@ namespace ngraph // Try parsing input as a binary protobuf message if (!model_proto.ParseFromIstream(&stream)) { +#ifdef NGRAPH_USE_PROTOBUF_LITE + throw detail::error::stream_parse{stream}; +#else // Rewind to the beginning and clear stream state. stream.clear(); stream.seekg(0); @@ -67,6 +70,7 @@ namespace ngraph { throw detail::error::stream_parse{stream}; } +#endif } Model model{model_proto}; diff --git a/ngraph/src/ngraph/frontend/onnx_import/utils/common.cpp b/ngraph/src/ngraph/frontend/onnx_import/utils/common.cpp index ffe432d..75a0e84 100644 --- a/ngraph/src/ngraph/frontend/onnx_import/utils/common.cpp +++ b/ngraph/src/ngraph/frontend/onnx_import/utils/common.cpp @@ -43,10 +43,14 @@ namespace ngraph case ONNX_NAMESPACE::TensorProto_DataType_UINT64: return element::u64; case ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED: return element::dynamic; } +#ifdef NGRAPH_USE_PROTOBUF_LITE + throw ngraph_error("unsupported element type"); +#else throw ngraph_error( "unsupported element type: " + ONNX_NAMESPACE::TensorProto_DataType_Name( static_cast(onnx_type))); +#endif } std::shared_ptr get_monotonic_range_along_node_rank( diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 60c855f..f963f87 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -378,7 +378,7 @@ set(MULTI_TEST_SRC backend/zero_sized.in.cpp ) -if (NGRAPH_ONNX_IMPORT_ENABLE) +if (NGRAPH_ONNX_IMPORT_ENABLE AND NOT NGRAPH_USE_PROTOBUF_LITE) list(APPEND MULTI_TEST_SRC onnx/onnx_import.in.cpp onnx/onnx_import_controlflow.in.cpp @@ -434,9 +434,11 @@ target_link_libraries(unit-test PRIVATE ngraph_test_util) target_link_libraries(unit-test PRIVATE ngraph) target_link_libraries(unit-test PRIVATE ngraph_backend libgtest) -if (NGRAPH_ONNX_IMPORT_ENABLE) +# Protobuf-lite does not support parsing files from prototxt format +# Since most of the onnx models are stored in this format it have to be disabled +if (NGRAPH_ONNX_IMPORT_ENABLE AND NOT NGRAPH_USE_PROTOBUF_LITE) target_include_directories(unit-test - SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR}) + SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIRS}) target_link_libraries(unit-test PRIVATE ${Protobuf_LIBRARIES} ${ONNX_LIBRARIES}) endif() -- 2.7.4