Unverified Commit a8e79079 authored by Tomasz Socha's avatar Tomasz Socha Committed by GitHub

[ONNX] Enable cross-compiling and ninja build for onnx_importer (#4450)

* [ONNX] Enable cross-compiling and ninja build for onnx_importer

- Change minimum cmake version to 3.11
- Enable cross-compilation of protobuf
- Rewrite ONNX external project into FetchContent
- Fix some issues with ninja build on windows

* Remove -Werror from ONNX CMAKE_CXX_FLAGS

* Unit-test: Link gcpu_backend if NGRAPH_GENERIC_CPU_ENABLE is ON

* Remove overwriting of main CMAKE_CXX_FLAGS from the onnx cmake file

* Fix onnx usage of CMAKE_CXX_FLAGS

* Ignore warning for onnx on windows, restore Windows CXX replacement

* Resolve some problems on OV ci

* Fix missing quotes

* Ignore range-loop-construct warning in onnx

* Just stop be pedantic for onnx

* Remove -Werror as well

* Remove unnecesary code

* style

* Add NGRAPH_API macro to builder::opset1::reorder_axes for OV transformations

* Move NGRAPH_API to header
Co-authored-by: 's avatarSang Ik Lee <sang.ik.lee@intel.com>
Co-authored-by: 's avatarRobert Kimball <robert.kimball@intel.com>
Co-authored-by: 's avatarScott Cyphers <scott.cyphers@intel.com>
Co-authored-by: 's avatarScott Cyphers <diyessi@users.noreply.github.com>
parent 0a2f9597
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# limitations under the License. # limitations under the License.
# ****************************************************************************** # ******************************************************************************
cmake_minimum_required (VERSION 3.4) cmake_minimum_required (VERSION 3.11)
# set directory where the custom finders live # set directory where the custom finders live
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
...@@ -568,7 +568,7 @@ if (NGRAPH_ONNX_IMPORT_ENABLE) ...@@ -568,7 +568,7 @@ if (NGRAPH_ONNX_IMPORT_ENABLE)
endif() endif()
include(cmake/external_onnx.cmake) include(cmake/external_onnx.cmake)
if (TARGET ext_protobuf) if (TARGET ext_protobuf)
add_dependencies(ext_onnx ext_protobuf) add_dependencies(onnx ext_protobuf)
endif() endif()
endif() endif()
......
...@@ -14,8 +14,7 @@ ...@@ -14,8 +14,7 @@
# limitations under the License. # limitations under the License.
# ****************************************************************************** # ******************************************************************************
# Enable ExternalProject CMake module include(FetchContent)
include(ExternalProject)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# ONNX.proto definition version # ONNX.proto definition version
...@@ -27,74 +26,38 @@ set(ONNX_VERSION 1.6.0) ...@@ -27,74 +26,38 @@ set(ONNX_VERSION 1.6.0)
# Download and install libonnx ... # Download and install libonnx ...
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Since this file is going to be modifying CMAKE_CXX_FLAGS we need to preserve
# it so we won't overwrite the caller's CMAKE_CXX_FLAGS
set(PUSH_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(ONNX_GIT_REPO_URL https://github.com/onnx/onnx.git) set(ONNX_GIT_REPO_URL https://github.com/onnx/onnx.git)
set(ONNX_GIT_BRANCH rel-${ONNX_VERSION}) set(ONNX_GIT_BRANCH rel-${ONNX_VERSION})
add_definitions(-DONNX_BUILD_SHARED_LIBS=ON) add_definitions(-DONNX_BUILD_SHARED_LIBS=ON)
add_definitions(-DONNX_ML=ON)
ExternalProject_Add( set(CMAKE_CXX_FLAGS ${CMAKE_ORIGINAL_CXX_FLAGS})
FetchContent_Declare(
ext_onnx ext_onnx
PREFIX onnx
GIT_REPOSITORY ${ONNX_GIT_REPO_URL} GIT_REPOSITORY ${ONNX_GIT_REPO_URL}
GIT_TAG ${ONNX_GIT_BRANCH} GIT_TAG ${ONNX_GIT_BRANCH}
INSTALL_COMMAND ""
UPDATE_COMMAND ""
CMAKE_GENERATOR ${CMAKE_GENERATOR}
CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
CMAKE_ARGS ${NGRAPH_FORWARD_CMAKE_ARGS}
-DCMAKE_CXX_FLAGS=${CMAKE_ORIGINAL_CXX_FLAGS}
-DONNX_GEN_PB_TYPE_STUBS=OFF
-DCMAKE_PREFIX_PATH=${Protobuf_INSTALL_PREFIX}
-DONNX_ML=TRUE
TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx/tmp"
STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx/stamp"
DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx/download"
SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx/src"
BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx/bin"
INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/onnx"
EXCLUDE_FROM_ALL TRUE
BUILD_BYPRODUCTS ${EXTERNAL_PROJECTS_ROOT}/onnx/bin/libonnx.a ${EXTERNAL_PROJECTS_ROOT}/onnx/bin/libonnx_proto.a
) )
# ----------------------------------------------------------------------------- FetchContent_GetProperties(ext_onnx)
if(NOT ext_onnx_POPULATED)
ExternalProject_Get_Property(ext_onnx SOURCE_DIR BINARY_DIR) FetchContent_Populate(ext_onnx)
set(ONNX_GEN_PB_TYPE_STUBS OFF)
set(ONNX_INCLUDE_DIR ${SOURCE_DIR}) if(CMAKE_CROSSCOMPILING)
set(ONNX_PROTO_INCLUDE_DIR ${BINARY_DIR}) set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${SYSTEM_PROTOC})
if (WIN32) endif()
set(ONNX_LIBRARY ${BINARY_DIR}/${CMAKE_BUILD_TYPE}/onnx.lib) add_subdirectory(${ext_onnx_SOURCE_DIR} ${ext_onnx_BINARY_DIR})
set(ONNX_PROTO_LIBRARY ${BINARY_DIR}/${CMAKE_BUILD_TYPE}/onnx_proto.lib)
ExternalProject_Add_Step(
ext_onnx
CopyONNX
COMMAND ${CMAKE_COMMAND} -E copy ${BINARY_DIR}/${CMAKE_BUILD_TYPE}/onnx.lib ${NGRAPH_LIBRARY_OUTPUT_DIRECTORY}/onnx.lib
COMMAND ${CMAKE_COMMAND} -E copy ${BINARY_DIR}/${CMAKE_BUILD_TYPE}/onnx_proto.lib ${NGRAPH_LIBRARY_OUTPUT_DIRECTORY}/onnx_proto.lib
COMMENT "Copy onnx libraries to ngraph build directory."
DEPENDEES install
)
else()
set(ONNX_LIBRARY ${BINARY_DIR}/libonnx.a)
set(ONNX_PROTO_LIBRARY ${BINARY_DIR}/libonnx_proto.a)
endif() endif()
set(ONNX_LIBRARIES ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY})
if (NOT TARGET onnx::libonnx) target_include_directories(onnx PRIVATE "${Protobuf_INCLUDE_DIR}")
add_library(onnx::libonnx UNKNOWN IMPORTED) target_include_directories(onnx_proto PRIVATE "${Protobuf_INCLUDE_DIR}")
set_target_properties(onnx::libonnx PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${ONNX_INCLUDE_DIR}
IMPORTED_LOCATION ${ONNX_LIBRARY})
add_dependencies(onnx::libonnx ext_onnx)
endif()
if (NOT TARGET onnx::libonnx_proto) set(ONNX_INCLUDE_DIR ${ext_onnx_SOURCE_DIR})
add_library(onnx::libonnx_proto UNKNOWN IMPORTED) set(ONNX_PROTO_INCLUDE_DIR ${ext_onnx_BINARY_DIR})
set_target_properties(onnx::libonnx_proto PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${ONNX_PROTO_INCLUDE_DIR} # Now make sure we restore the original CMAKE_CXX_FLAGS for the caller
IMPORTED_LOCATION ${ONNX_PROTO_LIBRARY}) set(CMAKE_CXX_FLAGS ${PUSH_CMAKE_CXX_FLAGS})
add_dependencies(onnx::libonnx_proto ext_onnx)
endif()
...@@ -24,17 +24,39 @@ include(ExternalProject) ...@@ -24,17 +24,39 @@ include(ExternalProject)
# This version of PROTOBUF is required by Microsoft ONNX Runtime. # This version of PROTOBUF is required by Microsoft ONNX Runtime.
set(NGRAPH_PROTOBUF_GIT_REPO_URL "https://github.com/protocolbuffers/protobuf") set(NGRAPH_PROTOBUF_GIT_REPO_URL "https://github.com/protocolbuffers/protobuf")
if(NGRAPH_ONNX_IMPORT_ENABLE) if(CMAKE_CROSSCOMPILING)
set(NGRAPH_PROTOBUF_GIT_TAG "v3.5.2") find_program(SYSTEM_PROTOC protoc PATHS ENV PATH)
if(SYSTEM_PROTOC)
execute_process(COMMAND ${SYSTEM_PROTOC} --version OUTPUT_VARIABLE PROTOC_VERSION)
string(REPLACE " " ";" PROTOC_VERSION ${PROTOC_VERSION})
list(GET PROTOC_VERSION -1 PROTOC_VERSION)
message("Detected system protoc version: ${PROTOC_VERSION}")
if(${PROTOC_VERSION} VERSION_EQUAL "3.0.0")
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()
else() else()
set(NGRAPH_PROTOBUF_GIT_TAG "v3.6.1") set(PROTOC_VERSION "3.7.1")
endif() endif()
set(NGRAPH_PROTOBUF_GIT_TAG "v${PROTOC_VERSION}")
set(Protobuf_INSTALL_PREFIX ${EXTERNAL_PROJECTS_ROOT}/protobuf) set(Protobuf_INSTALL_PREFIX ${EXTERNAL_PROJECTS_ROOT}/protobuf)
set(Protobuf_PROTOC_EXECUTABLE ${Protobuf_INSTALL_PREFIX}/bin/protoc) set(Protobuf_PROTOC_EXECUTABLE ${Protobuf_INSTALL_PREFIX}/bin/protoc)
set(Protobuf_INCLUDE_DIR ${Protobuf_INSTALL_PREFIX}/include) set(Protobuf_INCLUDE_DIR ${Protobuf_INSTALL_PREFIX}/include)
if (WIN32) if (WIN32)
set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.lib) 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() else()
set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a) set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a)
endif() endif()
...@@ -58,6 +80,7 @@ if (WIN32) ...@@ -58,6 +80,7 @@ if (WIN32)
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET} CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
CMAKE_ARGS CMAKE_ARGS
${NGRAPH_FORWARD_CMAKE_ARGS} ${NGRAPH_FORWARD_CMAKE_ARGS}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_CXX_FLAGS=${CMAKE_ORIGINAL_CXX_FLAGS} -DCMAKE_CXX_FLAGS=${CMAKE_ORIGINAL_CXX_FLAGS}
-Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF
-Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_WITH_ZLIB=OFF
...@@ -107,7 +130,7 @@ else() ...@@ -107,7 +130,7 @@ else()
GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG} GIT_TAG ${NGRAPH_PROTOBUF_GIT_TAG}
UPDATE_COMMAND "" UPDATE_COMMAND ""
PATCH_COMMAND "" PATCH_COMMAND ""
CONFIGURE_COMMAND ./autogen.sh COMMAND ./configure --prefix=${EXTERNAL_PROJECTS_ROOT}/protobuf --disable-shared CXX=${CMAKE_CXX_COMPILER} 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}" BUILD_COMMAND ${MAKE_UTIL} "${BUILD_FLAGS}"
TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp"
STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp"
...@@ -124,32 +147,14 @@ endif() ...@@ -124,32 +147,14 @@ endif()
# Use the interface of FindProtobuf.cmake # Use the interface of FindProtobuf.cmake
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
if(NGRAPH_ONNX_IMPORT_ENABLE) if (NOT TARGET protobuf::libprotobuf)
if (NOT TARGET libprotobuf) add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
add_library(libprotobuf INTERFACE) set_target_properties(protobuf::libprotobuf PROPERTIES
if (WIN32) INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
target_link_libraries(libprotobuf INTERFACE IMPORTED_LOCATION "${Protobuf_LIBRARY}")
debug ${Protobuf_INSTALL_PREFIX}/lib/libprotobufd.lib add_dependencies(protobuf::libprotobuf ext_protobuf)
optimized ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.lib)
else()
target_link_libraries(libprotobuf INTERFACE
${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a)
endif()
set_target_properties(libprotobuf PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}")
add_dependencies(libprotobuf ext_protobuf)
endif()
set(Protobuf_LIBRARIES libprotobuf)
else()
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)
endif() endif()
set(Protobuf_LIBRARIES protobuf::libprotobuf)
if (NOT TARGET protobuf::protoc) if (NOT TARGET protobuf::protoc)
add_executable(protobuf::protoc IMPORTED) add_executable(protobuf::protoc IMPORTED)
......
...@@ -122,6 +122,7 @@ namespace ngraph ...@@ -122,6 +122,7 @@ namespace ngraph
/// \param axes_order The permutation of axes. /// \param axes_order The permutation of axes.
/// ///
/// \return Transpose:v1 op. /// \return Transpose:v1 op.
NGRAPH_API
std::shared_ptr<Node> reorder_axes(const Output<Node>& value, std::shared_ptr<Node> reorder_axes(const Output<Node>& value,
std::vector<size_t> axes_order = {}); std::vector<size_t> axes_order = {});
......
...@@ -238,14 +238,14 @@ add_library(onnx_import STATIC ...@@ -238,14 +238,14 @@ add_library(onnx_import STATIC
set(ONNX_IMPORT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") set(ONNX_IMPORT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
add_dependencies(onnx_import_interface onnx::libonnx onnx::libonnx_proto) add_dependencies(onnx_import_interface onnx onnx_proto)
add_dependencies(onnx_import onnx_import_interface) add_dependencies(onnx_import onnx_import_interface)
set_property(TARGET onnx_import PROPERTY POSITION_INDEPENDENT_CODE ON) set_property(TARGET onnx_import PROPERTY POSITION_INDEPENDENT_CODE ON)
target_include_directories(onnx_import target_include_directories(onnx_import
SYSTEM PRIVATE ${ONNX_IMPORT_INCLUDE_DIR} ${NGRAPH_INCLUDE_PATH} SYSTEM PRIVATE ${ONNX_IMPORT_INCLUDE_DIR} ${NGRAPH_INCLUDE_PATH}
SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR}) SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR})
target_link_libraries(onnx_import PRIVATE ${Protobuf_LIBRARIES} ${ONNX_PROTO_LIBRARY}) target_link_libraries(onnx_import PRIVATE ${Protobuf_LIBRARIES} onnx_proto)
target_compile_definitions(onnx_import PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION}) target_compile_definitions(onnx_import PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION})
...@@ -254,6 +254,7 @@ target_include_directories(onnx_import_interface SYSTEM PRIVATE ${ONNX_IMPORT_IN ...@@ -254,6 +254,7 @@ target_include_directories(onnx_import_interface SYSTEM PRIVATE ${ONNX_IMPORT_IN
SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR}) SYSTEM PRIVATE ${ONNX_INCLUDE_DIR} ${ONNX_PROTO_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR})
target_compile_definitions(onnx_import_interface PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION}) target_compile_definitions(onnx_import_interface PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION})
target_compile_definitions(onnx_import_interface PRIVATE -DONNX_ML=ON)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$") if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$")
target_compile_options(onnx_import PRIVATE -Wno-undef -Wno-reserved-id-macro -Wno-switch-enum target_compile_options(onnx_import PRIVATE -Wno-undef -Wno-reserved-id-macro -Wno-switch-enum
......
...@@ -50,12 +50,12 @@ namespace ngraph ...@@ -50,12 +50,12 @@ namespace ngraph
} }
}; };
class NodeValidationFailure : public CheckFailure class OnnxNodeValidationFailure : public CheckFailure
{ {
public: public:
NodeValidationFailure(const CheckLocInfo& check_loc_info, OnnxNodeValidationFailure(const CheckLocInfo& check_loc_info,
const Node& node, const Node& node,
const std::string& explanation) const std::string& explanation)
: CheckFailure(check_loc_info, detail::get_error_msg_prefix(node), explanation) : CheckFailure(check_loc_info, detail::get_error_msg_prefix(node), explanation)
{ {
} }
...@@ -77,4 +77,4 @@ namespace ngraph ...@@ -77,4 +77,4 @@ namespace ngraph
#define CHECK_VALID_NODE(node_, cond_, ...) \ #define CHECK_VALID_NODE(node_, cond_, ...) \
NGRAPH_CHECK_HELPER( \ NGRAPH_CHECK_HELPER( \
::ngraph::onnx_import::error::NodeValidationFailure, (node_), (cond_), ##__VA_ARGS__) ::ngraph::onnx_import::error::OnnxNodeValidationFailure, (node_), (cond_), ##__VA_ARGS__)
...@@ -591,6 +591,10 @@ if (NGRAPH_INTERPRETER_ENABLE) ...@@ -591,6 +591,10 @@ if (NGRAPH_INTERPRETER_ENABLE)
target_link_libraries(unit-test PRIVATE interpreter_backend) target_link_libraries(unit-test PRIVATE interpreter_backend)
endif() endif()
if (NGRAPH_GENERIC_CPU_ENABLE)
target_link_libraries(unit-test PRIVATE gcpu_backend)
endif()
if (NGRAPH_GPU_ENABLE) if (NGRAPH_GPU_ENABLE)
target_link_libraries(unit-test PRIVATE gpu_backend) target_link_libraries(unit-test PRIVATE gpu_backend)
endif() endif()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment