Commit a1957ca7 authored by Junfeng Dong's avatar Junfeng Dong Committed by Robert Kimball

Enable onnx, protobuf and onnx import on Windows. (#2621)

* Enable onnx, protobuf and onnx import on Windows.

* Apply style.
parent 72a66a48
...@@ -395,6 +395,13 @@ if(NOT DEFINED EXTERNAL_PROJECTS_ROOT) ...@@ -395,6 +395,13 @@ if(NOT DEFINED EXTERNAL_PROJECTS_ROOT)
endif() endif()
if (NGRAPH_ONNX_IMPORT_ENABLE) if (NGRAPH_ONNX_IMPORT_ENABLE)
if (MSVS)
# When we build dll libraries. These flags make sure onnx and protobuf build with /MD, not /MT.
# These two options can't be mixed, because they requires link two imcompatiable runtime.
set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
endif(MSVS)
if (NOT NGRAPH_USE_SYSTEM_PROTOBUF) if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
include(cmake/external_protobuf.cmake) include(cmake/external_protobuf.cmake)
else() else()
......
...@@ -59,8 +59,23 @@ ExternalProject_Get_Property(ext_onnx SOURCE_DIR BINARY_DIR) ...@@ -59,8 +59,23 @@ ExternalProject_Get_Property(ext_onnx SOURCE_DIR BINARY_DIR)
set(ONNX_INCLUDE_DIR ${SOURCE_DIR}/onnx) set(ONNX_INCLUDE_DIR ${SOURCE_DIR}/onnx)
set(ONNX_PROTO_INCLUDE_DIR ${BINARY_DIR}/onnx) set(ONNX_PROTO_INCLUDE_DIR ${BINARY_DIR}/onnx)
set(ONNX_LIBRARY ${BINARY_DIR}/libonnx.a) if (WIN32)
set(ONNX_PROTO_LIBRARY ${BINARY_DIR}/libonnx_proto.a) set(ONNX_LIBRARY ${BINARY_DIR}/${CMAKE_BUILD_TYPE}/onnx.lib)
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()
set(ONNX_LIBRARIES ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY}) set(ONNX_LIBRARIES ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY})
if (NOT TARGET onnx::libonnx) if (NOT TARGET onnx::libonnx)
......
...@@ -25,7 +25,34 @@ include(ExternalProject) ...@@ -25,7 +25,34 @@ include(ExternalProject)
set(NGRAPH_PROTOBUF_GIT_REPO_URL "https://github.com/protocolbuffers/protobuf") set(NGRAPH_PROTOBUF_GIT_REPO_URL "https://github.com/protocolbuffers/protobuf")
set(NGRAPH_PROTOBUF_GIT_TAG "v3.5.2") set(NGRAPH_PROTOBUF_GIT_TAG "v3.5.2")
ExternalProject_Add( 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}
-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
)
else()
ExternalProject_Add(
ext_protobuf ext_protobuf
PREFIX protobuf PREFIX protobuf
GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL} GIT_REPOSITORY ${NGRAPH_PROTOBUF_GIT_REPO_URL}
...@@ -41,7 +68,7 @@ ExternalProject_Add( ...@@ -41,7 +68,7 @@ ExternalProject_Add(
INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf"
EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_ALL TRUE
) )
endif()
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Use the interface of FindProtobuf.cmake # Use the interface of FindProtobuf.cmake
...@@ -50,7 +77,11 @@ ExternalProject_Add( ...@@ -50,7 +77,11 @@ ExternalProject_Add(
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)
set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a) if (WIN32)
set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.lib)
else()
set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a)
endif()
set(Protobuf_LIBRARIES ${Protobuf_LIBRARY}) set(Protobuf_LIBRARIES ${Protobuf_LIBRARY})
if (NOT TARGET protobuf::libprotobuf) if (NOT TARGET protobuf::libprotobuf)
......
...@@ -121,7 +121,8 @@ namespace ngraph ...@@ -121,7 +121,8 @@ namespace ngraph
template <typename T> template <typename T>
inline std::vector<T> get_data(const onnx::TensorProto& tensor) inline std::vector<T> get_data(const onnx::TensorProto& tensor)
{ {
throw error::tensor::unsupported_data_type{tensor.data_type()}; throw ngraph::onnx_import::error::tensor::unsupported_data_type{
tensor.data_type()};
} }
template <> template <>
...@@ -135,7 +136,7 @@ namespace ngraph ...@@ -135,7 +136,7 @@ namespace ngraph
{ {
return detail::__get_data<double>(tensor.double_data()); return detail::__get_data<double>(tensor.double_data());
} }
if ((tensor.data_type() == onnx::TensorProto_DataType_FLOAT) or if ((tensor.data_type() == onnx::TensorProto_DataType_FLOAT) ||
(tensor.data_type() == onnx::TensorProto_DataType_FLOAT16)) (tensor.data_type() == onnx::TensorProto_DataType_FLOAT16))
{ {
return detail::__get_data<double>(tensor.float_data()); return detail::__get_data<double>(tensor.float_data());
...@@ -162,7 +163,7 @@ namespace ngraph ...@@ -162,7 +163,7 @@ namespace ngraph
{ {
return detail::__get_raw_data<float>(tensor.raw_data()); return detail::__get_raw_data<float>(tensor.raw_data());
} }
if ((tensor.data_type() == onnx::TensorProto_DataType_FLOAT) or if ((tensor.data_type() == onnx::TensorProto_DataType_FLOAT) ||
(tensor.data_type() == onnx::TensorProto_DataType_FLOAT16)) (tensor.data_type() == onnx::TensorProto_DataType_FLOAT16))
{ {
return detail::__get_data<float>(tensor.float_data()); return detail::__get_data<float>(tensor.float_data());
......
...@@ -38,4 +38,12 @@ if(NGRAPH_LIB_VERSIONING_ENABLE) ...@@ -38,4 +38,12 @@ if(NGRAPH_LIB_VERSIONING_ENABLE)
SOVERSION ${ONNXIFI_ABI_VERSION}) SOVERSION ${ONNXIFI_ABI_VERSION})
endif() endif()
if(WIN32)
if(MSVC)
target_compile_definitions(onnxifi-ngraph PRIVATE "ONNXIFI_PUBLIC=__declspec(dllexport)")
else()
target_compile_definitions(onnxifi-ngraph PRIVATE "ONNXIFI_PUBLIC=__attribute__((__dllexport__))")
endif()
endif()
install(TARGETS onnxifi-ngraph DESTINATION ${NGRAPH_INSTALL_LIB}) install(TARGETS onnxifi-ngraph DESTINATION ${NGRAPH_INSTALL_LIB})
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