Skip to content

Commit

Permalink
Last major set of ORT format model changes (microsoft#5056)
Browse files Browse the repository at this point in the history
* Add minimal build option to build.py
Group some of the build settings so binary size reduction options are all together
Make some cmake variable naming more consistent
Replace usage of std::hash with murmurhash3 for kernel. std::hash is implementation dependent so can't be used.
Add initial doco and ONNX to ORT model conversion script
Misc cleanups of minimal build breaks.
  • Loading branch information
skottmckay authored Sep 4, 2020
1 parent 6134994 commit b5c2932
Show file tree
Hide file tree
Showing 28 changed files with 983 additions and 223 deletions.
17 changes: 17 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4804,3 +4804,20 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

-----

MurmurHash3

MIT license

https://github.com/aappleby/smhasher

SMHasher is a test suite designed to test the distribution, collision, and
performance properties of non-cryptographic hash functions.
This is the home for the MurmurHash family of hash functions along with the
SMHasher test suite used to verify them.
SMHasher is released under the MIT license.
All MurmurHash versions are public domain software, and the author disclaims all copyright to their code.

-----
10 changes: 10 additions & 0 deletions cgmanifests/cgmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@
},
"comments": "Installed in the training docker image"
}
},
{
"component": {
"type": "git",
"git": {
"commitHash": "92cf3702fcfaadc84eb7bef59825a23e0cd84f56",
"repositoryUrl": "https://github.com/aappleby/smhasher"
},
"comments": "MurmurHash3"
}
}
],
"Version": 1
Expand Down
54 changes: 33 additions & 21 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ option(onnxruntime_CROSS_COMPILING "Cross compiling onnx runtime" OFF)

#It's preferred to turn it OFF when onnxruntime is dynamically linked to PROTOBUF
option(onnxruntime_USE_FULL_PROTOBUF "Link to libprotobuf instead of libprotobuf-lite when this option is ON" OFF)
option(onnxruntime_DISABLE_CONTRIB_OPS "Disable contrib ops" OFF)
option(onnxruntime_DISABLE_ML_OPS "Disable traditional ML ops" OFF)
option(onnxruntime_DISABLE_RTTI "Disable RTTI" OFF)
option(tensorflow_C_PACKAGE_PATH "Path to tensorflow C package installation dir")
option(onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS "Enable operator implemented in language other than cpp" OFF)
option(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS "Dump debug information about node inputs and outputs when executing the model." OFF)
Expand All @@ -109,11 +106,14 @@ option(onnxruntime_USE_TELEMETRY "Build with Telemetry" OFF)
#It's default OFF because it's experimental now.
option(onnxruntime_PREFER_SYSTEM_LIB "Experimental: Build with the preinstalled libraries in your system" OFF)

# ORT mininal build settings
# Options to reduce build size
option(onnxruntime_DISABLE_CONTRIB_OPS "Disable contrib ops" OFF)
option(onnxruntime_DISABLE_ML_OPS "Disable traditional ML ops" OFF)
option(onnxruntime_DISABLE_RTTI "Disable RTTI" OFF)
# For now onnxruntime_DISABLE_EXCEPTIONS will only work with onnxruntime_MINIMAL_BUILD, more changes (ONNX, non-CPU EP, ...) are required to run this standalone
option(onnxruntime_DISABLE_EXCEPTIONS "Disable exception handling. Requires onnxruntime_MINIMAL_BUILD currently." OFF)
option(onnxruntime_MINIMAL_BUILD "Exclude as much as possible from the build. Support ORT format models. No support for ONNX format models." OFF)
option(onnxruntime_REDUCED_OPS_BUILD "Reduced set of kernels are registered in build via modification of the kernel registration source files." OFF)
# For now onnxruntime_NO_EXCEPTIONS will only work with onnxruntime_MINIMAL_BUILD, more changes (ONNX, non-CPU EP, ...) are required to run this standalone
option(onnxruntime_NO_EXCEPTIONS "Disable exception handling." OFF)

# training options
option(onnxruntime_ENABLE_NVTX_PROFILE "Enable NVTX profile." OFF)
Expand Down Expand Up @@ -214,15 +214,18 @@ endif()
# Will expose option in build.py when all pieces are available
if(onnxruntime_MINIMAL_BUILD)
add_compile_definitions(ORT_MINIMAL_BUILD)
set(onnxruntime_REDUCED_OPS_BUILD ON) # TODO Defaulting to ON. TBD if we should always do that.
set(onnxruntime_REDUCED_OPS_BUILD ON)
set(onnxruntime_DISABLE_RTTI ON)

if (MSVC)
# add MSVC specific flags to reduce build size here
# add MSVC specific flags to reduce build size here if needed
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections")
# TODO: May need to use -dead_strip instead of --gc-sections for iOS(XCode)
add_link_options(-Wl,--gc-sections)
if (CMAKE_HOST_SYSTEM MATCHES "Darwin")
add_link_options(-Wl,-dead_strip)
else()
add_link_options(-Wl,--gc-sections)
endif()
endif()

endif()
Expand All @@ -243,9 +246,9 @@ endif()

# If this is only enabled in an onnxruntime_ORT_MODEL_FORMAT_ONLY build we don't need ONNX changes
# as we (currently) only pull in data_type_utils.cc/h which doesn't throw
if(onnxruntime_NO_EXCEPTIONS)
if(onnxruntime_DISABLE_EXCEPTIONS)
if(NOT onnxruntime_MINIMAL_BUILD)
message(FATAL_ERROR "onnxruntime_MINIMAL_BUILD required for onnxruntime_NO_EXCEPTIONS")
message(FATAL_ERROR "onnxruntime_MINIMAL_BUILD required for onnxruntime_DISABLE_EXCEPTIONS")
endif()

add_compile_definitions("ORT_NO_EXCEPTIONS")
Expand All @@ -259,7 +262,7 @@ if(onnxruntime_NO_EXCEPTIONS)
string(APPEND CMAKE_CXX_FLAGS " /wd4834 /wd4702")
add_compile_definitions("_HAS_EXCEPTIONS=0")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables")
endif()
endif()

Expand Down Expand Up @@ -319,13 +322,13 @@ if (MSVC)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
set(gtest_force_shared_crt ON CACHE BOOL "Use shared (DLL) run-time lib for gtest" FORCE)
endif()

#Always enable exception handling, even for Windows ARM
if(NOT onnxruntime_NO_EXCEPTIONS)
if(NOT onnxruntime_DISABLE_EXCEPTIONS)
string(APPEND CMAKE_CXX_FLAGS " /EHsc /wd26812")
string(APPEND CMAKE_C_FLAGS " /EHsc /wd26812")
endif()
string(APPEND CMAKE_CXX_FLAGS " /EHsc /wd26812")
string(APPEND CMAKE_C_FLAGS " /EHsc /wd26812")

if(onnxruntime_USE_AVX)
string(APPEND CMAKE_CXX_FLAGS " /arch:AVX")
string(APPEND CMAKE_C_FLAGS " /arch:AVX")
Expand All @@ -336,10 +339,12 @@ if (MSVC)
string(APPEND CMAKE_CXX_FLAGS " /arch:AVX512")
string(APPEND CMAKE_C_FLAGS " /arch:AVX512")
endif()

if (onnxruntime_ENABLE_LTO AND NOT onnxruntime_USE_CUDA)
SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gw /GL")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Gw /GL")
endif()

# The WinML build tool chain builds ARM/ARM64, and the internal tool chain does not have folders for spectre mitigation libs.
# WinML performs spectre mitigation differently.
if (NOT DEFINED onnxruntime_DISABLE_QSPECTRE_CHECK)
Expand Down Expand Up @@ -387,9 +392,9 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "iOSCross")
#For ios compliance
message("Adding flags for ios builds")
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target arm64-apple-darwin-macho")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target arm64-apple-darwin-macho")
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target armv7a-apple-darwin-macho")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target armv7a-apple-darwin-macho")
endif()
endif()

Expand Down Expand Up @@ -509,6 +514,7 @@ if(UNIX AND onnxruntime_ENABLE_LTO AND NOT onnxruntime_PREFER_SYSTEM_LIB)
#https://github.com/protocolbuffers/protobuf/issues/5923
target_link_options(protoc PRIVATE "-Wl,--no-as-needed")
endif()

include(protobuf_function.cmake)
#protobuf end

Expand Down Expand Up @@ -571,8 +577,8 @@ endif()
# we make it through via a handler so CUDA does not complain
# The following -DGSL macros are recognized by gsl-lite along with -Dgsl macros
# no bounds checking in release build so no perf cost
# if we enable onnxruntime_NO_EXCEPTIONS, gsl will terminate
if (onnxruntime_NO_EXCEPTIONS)
# if we enable onnxruntime_DISABLE_EXCEPTIONS, gsl will terminate
if (onnxruntime_DISABLE_EXCEPTIONS)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGSL_TERMINATE_ON_CONTRACT_VIOLATION")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGSL_THROW_ON_CONTRACT_VIOLATION")
Expand Down Expand Up @@ -703,7 +709,13 @@ if(NOT onnxruntime_USE_FULL_PROTOBUF)
else()
set(ONNX_USE_LITE_PROTO OFF CACHE BOOL "" FORCE)
endif()
add_subdirectory(external/onnx)

if (NOT onnxruntime_MINIMAL_BUILD)
add_subdirectory(external/onnx)
else()
include(onnx_minimal)
endif()

target_compile_definitions(onnx PUBLIC $<TARGET_PROPERTY:onnx_proto,INTERFACE_COMPILE_DEFINITIONS> PRIVATE "__ONNX_DISABLE_STATIC_REGISTRATION")
if (NOT onnxruntime_USE_FULL_PROTOBUF)
target_compile_definitions(onnx PUBLIC "__ONNX_NO_DOC_STRINGS")
Expand Down
100 changes: 100 additions & 0 deletions cmake/external/onnx_minimal.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

#
# Setup onnx and onnx_protobuf for a build with onnxruntime_MINIMAL_BUILD enabled.
# We exclude everything but the essentials from the onnx library.
#

if(NOT onnxruntime_MINIMAL_BUILD)
message(FATAL_ERROR "This file should only be included in a minimal build")
endif()

#TODO: if protobuf is a shared lib and onnxruntime_USE_FULL_PROTOBUF is ON, then onnx_proto should be built as a shared lib instead of a static lib. Otherwise any code outside onnxruntime.dll can't use onnx protobuf definitions if they share the protobuf.dll with onnxruntime. For example, if protobuf is a shared lib and onnx_proto is a static lib then onnxruntime_perf_test won't work.

set(ONNX_SOURCE_ROOT ${PROJECT_SOURCE_DIR}/external/onnx)

add_library(onnx_proto ${ONNX_SOURCE_ROOT}/onnx/onnx-ml.proto ${ONNX_SOURCE_ROOT}/onnx/onnx-operators-ml.proto)

target_include_directories(onnx_proto PUBLIC $<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_INCLUDE_DIRECTORIES> "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_definitions(onnx_proto PUBLIC $<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_COMPILE_DEFINITIONS>)

set(_src_prefix "onnx/")
onnxruntime_protobuf_generate(NO_SRC_INCLUDES GEN_SRC_PREFIX ${_src_prefix} IMPORT_DIRS ${ONNX_SOURCE_ROOT} TARGET onnx_proto)

if (WIN32)
target_compile_options(onnx_proto PRIVATE "/wd4146" "/wd4125" "/wd4456" "/wd4267" "/wd4309")
else()
if(HAS_UNUSED_VARIABLE)
target_compile_options(onnx_proto PRIVATE "-Wno-unused-variable")
endif()

if(HAS_UNUSED_BUT_SET_VARIABLE)
target_compile_options(onnx_proto PRIVATE "-Wno-unused-but-set-variable")
endif()
endif()

# For reference, this would be the full ONNX source include. We only need data_type_utils.* in this build.
# file(GLOB_RECURSE onnx_src CONFIGURE_DEPENDS
# "${ONNX_SOURCE_ROOT}/onnx/*.h"
# "${ONNX_SOURCE_ROOT}/onnx/*.cc"
# )
# file(GLOB_RECURSE onnx_exclude_src CONFIGURE_DEPENDS
# "${ONNX_SOURCE_ROOT}/onnx/py_utils.h"
# "${ONNX_SOURCE_ROOT}/onnx/proto_utils.h"
# "${ONNX_SOURCE_ROOT}/onnx/backend/test/cpp/*"
# "${ONNX_SOURCE_ROOT}/onnx/test/*"
# "${ONNX_SOURCE_ROOT}/onnx/cpp2py_export.cc"
# )
# list(REMOVE_ITEM onnx_src ${onnx_exclude_src})
file(GLOB onnx_src CONFIGURE_DEPENDS
"${ONNX_SOURCE_ROOT}/onnx/defs/data_type_utils.*"
)

if (MSVC)
SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gw /GL")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Gw /GL")
endif()

add_library(onnx ${onnx_src})
add_dependencies(onnx onnx_proto)
target_include_directories(onnx PUBLIC "${ONNX_SOURCE_ROOT}")
target_include_directories(onnx PUBLIC $<TARGET_PROPERTY:onnx_proto,INTERFACE_INCLUDE_DIRECTORIES>)
if (onnxruntime_USE_FULL_PROTOBUF)
target_compile_definitions(onnx PUBLIC "ONNX_ML" "ONNX_NAMESPACE=onnx")
else()
target_compile_definitions(onnx PUBLIC "ONNX_ML" "ONNX_NAMESPACE=onnx" "ONNX_USE_LITE_PROTO")
endif()

if (WIN32)
target_compile_options(onnx PRIVATE
/wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
/wd4125 # decimal digit terminates octal escape sequence
/wd4100 # 'param' : unreferenced formal parameter
/wd4244 # 'argument' conversion from 'google::protobuf::int64' to 'int', possible loss of data
/wd4996 # 'argument' Using double parameter version instead of single parameter version of SetTotalBytesLimit(). The second parameter is ignored.
)
if (NOT onnxruntime_DISABLE_EXCEPTIONS)
target_compile_options(onnx PRIVATE
/EHsc # exception handling - C++ may throw, extern "C" will not
)
endif()

target_compile_options(onnx_proto PRIVATE
/wd4244 # 'argument' conversion from 'google::protobuf::int64' to 'int', possible loss of data
)

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}")
else()
if(HAS_UNUSED_PARAMETER)
target_compile_options(onnx PRIVATE "-Wno-unused-parameter")
endif()
if(HAS_UNUSED_BUT_SET_VARIABLE)
target_compile_options(onnx PRIVATE "-Wno-unused-but-set-variable")
endif()
endif()

16 changes: 9 additions & 7 deletions cmake/onnxruntime_graph.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ if (onnxruntime_ENABLE_TRAINING)
endif()

if (WIN32)
set(onnxruntime_graph_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(onnxruntime_graph_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(onnxruntime_graph PROPERTIES
STATIC_LIBRARY_FLAGS "${onnxruntime_graph_static_library_flags}")
set_target_properties(onnxruntime_graph PROPERTIES
STATIC_LIBRARY_FLAGS "${onnxruntime_graph_static_library_flags}")

if (NOT onnxruntime_DISABLE_EXCEPTIONS)
target_compile_options(onnxruntime_graph PRIVATE
/EHsc # exception handling - C++ may throw, extern "C" will not
)
endif()

# Add Code Analysis properties to enable C++ Core checks. Have to do it via a props file include.
set_target_properties(onnxruntime_graph PROPERTIES VS_USER_PROPS ${PROJECT_SOURCE_DIR}/EnableVisualStudioCodeAnalysis.props)
# Add Code Analysis properties to enable C++ Core checks. Have to do it via a props file include.
set_target_properties(onnxruntime_graph PROPERTIES VS_USER_PROPS ${PROJECT_SOURCE_DIR}/EnableVisualStudioCodeAnalysis.props)
endif()
15 changes: 11 additions & 4 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ else() # minimal and/or reduced ops build
)

if (onnxruntime_MINIMAL_BUILD)
# TODO: Add tests that can be used in a minimal build
else()
list(APPEND onnxruntime_test_framework_src_patterns
"${TEST_SRC_DIR}/framework/ort_model_only_test.cc"
)

else() # reduced ops build
file(GLOB onnxruntime_test_ir_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/ir/*.cc"
"${TEST_SRC_DIR}/ir/*.h"
Expand Down Expand Up @@ -261,12 +264,16 @@ set (ONNXRUNTIME_API_TESTS_WITHOUT_ENV_SRC_DIR "${ONNXRUNTIME_ROOT}/test/api_tes

set (onnxruntime_shared_lib_test_SRC
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_fixture.h
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_inference.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_session_options.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_run_options.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_allocator.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_nontensor_types.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_model_loading.cc)

if (NOT onnxruntime_MINIMAL_BUILD)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_inference.cc)
endif()

if(onnxruntime_RUN_ONNX_TESTS)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_io_types.cc)
endif()
Expand Down Expand Up @@ -837,7 +844,7 @@ if (onnxruntime_BUILD_SHARED_LIB)
)

# test inference using global threadpools
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Android" AND NOT onnxruntime_MINIMAL_BUILD)
AddTest(DYN
TARGET onnxruntime_global_thread_pools_test
SOURCES ${onnxruntime_global_thread_pools_test_SRC}
Expand Down
Loading

0 comments on commit b5c2932

Please sign in to comment.