From df1961c19192f7d404a9948f0be63f7078f3b802 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 24 Aug 2023 03:28:12 +0200 Subject: [PATCH] Build ONNXRuntime wheels --- Dockerfile | 67 ++++++++++++++++++- build.yaml | 1 + ...nnx-0001-Remove-MATH_NO_EXCEPT-macro.patch | 49 ++++++++++++++ ...ject-destruction-compile-error-16134.patch | 52 ++++++++++++++ patches/onnx-cxx17.patch | 26 +++++++ patches/onnx-no-execinfo.patch | 22 ++++++ patches/onnx-system.patch | 57 ++++++++++++++++ 7 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 patches/onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch create mode 100644 patches/onnx-0002-prevent-object-destruction-compile-error-16134.patch create mode 100644 patches/onnx-cxx17.patch create mode 100644 patches/onnx-no-execinfo.patch create mode 100644 patches/onnx-system.patch diff --git a/Dockerfile b/Dockerfile index 9f3010b51..218965847 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ ARG \ SSOCR_VERSION \ LIBCEC_VERSION \ PICOTTS_HASH \ - TELLDUS_COMMIT + TELLDUS_COMMIT \ + ONNXRUNTIME_VERSION # Add Home Assistant wheels repository ENV WHEELS_LINKS=https://wheels.home-assistant.io/musllinux/ @@ -144,6 +145,70 @@ RUN \ /usr/src/telldus \ /usr/src/telldus-fix-gcc-11-issues.patch \ /usr/src/telldus-fix-alpine-3-17-issues.patch + +# ONNX Runtime +COPY patches/onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch /usr/src/ +COPY patches/onnx-0002-prevent-object-destruction-compile-error-16134.patch /usr/src/ +COPY patches/onnx-cxx17.patch /usr/src/ +COPY patches/onnx-no-execinfo.patch /usr/src/ +COPY patches/onnx-system.patch /usr/src/ +RUN \ + apk add --no-cache \ + abseil-cpp-log-internal-check-op \ + abseil-cpp-log-internal-message \ + abseil-cpp-raw-hash-set \ + libprotobuf-lite \ + libstdc++ \ + re2 \ + && apk add --no-cache --virtual .build-dependencies \ + build-base \ + abseil-cpp-dev \ + patchelf \ + cmake \ + icu-dev \ + linux-headers \ + nlohmann-json \ + patch \ + protobuf-dev \ + re2-dev \ + samurai \ + zlib-dev \ + && pip3 install --no-cache-dir --no-index --only-binary=:all: --find-links "${WHEELS_LINKS}" numpy packaging \ + && pip3 install --no-cache-dir pybind11[global] auditwheel \ + && mkdir /usr/src/onnxruntime \ + && curl -J -L -o /tmp/onnxruntime.tar.gz \ + "https://github.com/microsoft/onnxruntime/archive/refs/tags/v${ONNXRUNTIME_VERSION}.tar.gz" \ + && tar zxvf \ + /tmp/onnxruntime.tar.gz \ + --strip 1 -C /usr/src/onnxruntime \ + && cd onnxruntime \ + && patch -p1 < ../onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch \ + && patch -p1 < ../onnx-0002-prevent-object-destruction-compile-error-16134.patch \ + && patch -p1 < ../onnx-cxx17.patch \ + && patch -p1 < ../onnx-no-execinfo.patch \ + && patch -p1 < ../onnx-system.patch \ + && cmake -S cmake -B build -G Ninja \ + -DCMAKE_BUILD_TYPE=None \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DBUILD_ONNX_PYTHON=ON \ + -Donnxruntime_BUILD_SHARED_LIB=ON \ + -Donnxruntime_ENABLE_PYTHON=ON \ + && sed -i 's|CMAKE_CXX_STANDARD 11|CMAKE_CXX_STANDARD 17|' build/_deps/onnx-src/CMakeLists.txt \ + && cd build \ + && cmake --build . \ + && cmake --install . \ + && python3 ../setup.py bdist_wheel \ + && auditwheel repair dist/*.whl \ + \ + # Here they are... + && ls -la wheelhouse/*.whl \ + \ + && apk del .build-dependencies \ + && rm -rf \ + /tmp/onnxruntime.tar.gz \ + /usr/src/onnxruntime \ + /usr/src/onnx-*.patch + ### # Base S6-Overlay COPY rootfs / diff --git a/build.yaml b/build.yaml index a313e9a23..d393a1a2f 100644 --- a/build.yaml +++ b/build.yaml @@ -23,5 +23,6 @@ labels: args: SSOCR_VERSION: 2.22.1 LIBCEC_VERSION: 6.0.2 + ONNXRUNTIME_VERSION: 1.15.1 TELLDUS_COMMIT: 2598bbed16ffd701f2a07c99582f057a3decbaf3 PICOTTS_HASH: e3ba46009ee868911fa0b53db672a55f9cc13b1c diff --git a/patches/onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch b/patches/onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch new file mode 100644 index 000000000..4961374c4 --- /dev/null +++ b/patches/onnx-0001-Remove-MATH_NO_EXCEPT-macro.patch @@ -0,0 +1,49 @@ +From 8caf31352140d655e2c88fb9784982c4e5db3b5b Mon Sep 17 00:00:00 2001 +From: sudotac +Date: Sun, 6 Aug 2023 23:46:28 +0900 +Subject: [PATCH] Remove MATH_NO_EXCEPT macro + +Signed-off-by: sudotac +--- + .../test/providers/cpu/math/element_wise_ops_test.cc | 12 +++--------- + 1 file changed, 3 insertions(+), 9 deletions(-) + +diff --git a/onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc b/onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc +index 04fbd05669..5ec4d915f5 100644 +--- a/onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc ++++ b/onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc +@@ -2603,13 +2603,7 @@ TEST(MathOpTest, Mean_8) { + test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider}); // TensorRT: Input batch size is inconsistent + } + +-#ifdef _LIBCPP_VERSION +-#define MATH_NO_EXCEPT +-#else +-#define MATH_NO_EXCEPT noexcept +-#endif +- +-template ++template + void TrigFloatTest(OpTester& test, std::initializer_list input) { + std::vector dims{static_cast(input.size())}; + +@@ -2622,7 +2616,7 @@ void TrigFloatTest(OpTester& test, std::initializer_list input) { + test.Run(); + } + +-template ++template + void TrigDoubleTest(OpTester& test, std::initializer_list input, + const std::unordered_set excluded_provider_types = {}) { + std::vector dims{static_cast(input.size())}; +@@ -2636,7 +2630,7 @@ void TrigDoubleTest(OpTester& test, std::initializer_list input, + test.Run(OpTester::ExpectResult::kExpectSuccess, "", excluded_provider_types); + } + +-template ++template + void TrigFloat16Test(OpTester& test, std::initializer_list input) { + std::vector dims{static_cast(input.size())}; + +-- +2.41.0 diff --git a/patches/onnx-0002-prevent-object-destruction-compile-error-16134.patch b/patches/onnx-0002-prevent-object-destruction-compile-error-16134.patch new file mode 100644 index 000000000..589c6fe02 --- /dev/null +++ b/patches/onnx-0002-prevent-object-destruction-compile-error-16134.patch @@ -0,0 +1,52 @@ +From b674a15d5c0133e76f3b1c4372ff4cf50d6cb608 Mon Sep 17 00:00:00 2001 +From: Somdev Sangwan +Date: Fri, 2 Jun 2023 23:49:53 +0530 +Subject: [PATCH] prevent object destruction compile error (#16134) + +### Description +The proposed fix is to store the result of AsBlockSparse() in a variable +to ensure the object isn't destroyed until the end of the current scope. + +### Motivation and Context +"own_buffer_tensor" is a temporary object that is destroyed at the end +of the expression and causes a compile error. +--- + onnxruntime/test/framework/sparse_kernels_test.cc | 9 ++++++--- + 1 file changed, 6 insertions(+), 3 deletions(-) + +diff --git a/onnxruntime/test/framework/sparse_kernels_test.cc b/onnxruntime/test/framework/sparse_kernels_test.cc +index 1a535144f4..b269b3231b 100644 +--- a/onnxruntime/test/framework/sparse_kernels_test.cc ++++ b/onnxruntime/test/framework/sparse_kernels_test.cc +@@ -1815,7 +1815,8 @@ TEST(SparseTensorConversionTests, BlockSparse) { + ASSERT_EQ(data_blocks.size(), data_span.size()); + ASSERT_TRUE(std::equal(data_blocks.cbegin(), data_blocks.cend(), data_span.begin(), data_span.end())); + +- const auto& indices = own_buffer_tensor.AsBlockSparse().Indices(); ++ auto block_sparse = own_buffer_tensor.AsBlockSparse(); ++ const auto& indices = block_sparse.Indices(); + ASSERT_EQ(indices_shape, indices.Shape()); + auto indices_span = indices.DataAsSpan(); + ASSERT_TRUE(std::equal(blocksparse_indices.cbegin(), blocksparse_indices.cend(), +@@ -1834,7 +1835,8 @@ TEST(SparseTensorConversionTests, BlockSparse) { + ASSERT_EQ(data_blocks.size(), data_span.size()); + ASSERT_TRUE(std::equal(data_blocks.cbegin(), data_blocks.cend(), data_span.begin(), data_span.end())); + +- const auto& indices = user_buffer_tensor.AsBlockSparse().Indices(); ++ auto block_sparse = user_buffer_tensor.AsBlockSparse(); ++ const auto& indices = block_sparse.Indices(); + ASSERT_EQ(indices_shape, indices.Shape()); + auto indices_span = indices.DataAsSpan(); + ASSERT_TRUE(std::equal(blocksparse_indices.cbegin(), blocksparse_indices.cend(), +@@ -1855,7 +1857,8 @@ TEST(SparseTensorConversionTests, BlockSparse) { + ASSERT_EQ(expected_span.size(), data_span.size()); + ASSERT_TRUE(std::equal(expected_span.begin(), expected_span.end(), data_span.begin(), data_span.end())); + +- const auto& indices = own_buffer_tensor.AsBlockSparse().Indices(); ++ auto block_sparse = own_buffer_tensor.AsBlockSparse(); ++ const auto& indices = block_sparse.Indices(); + ASSERT_EQ(indices_shape, indices.Shape()); + auto indices_span = indices.DataAsSpan(); + ASSERT_TRUE(std::equal(blocksparse_indices.cbegin(), blocksparse_indices.cend(), +-- +2.41.0 diff --git a/patches/onnx-cxx17.patch b/patches/onnx-cxx17.patch new file mode 100644 index 000000000..8242d0a45 --- /dev/null +++ b/patches/onnx-cxx17.patch @@ -0,0 +1,26 @@ +diff --git a/js/node/CMakeLists.txt b/js/node/CMakeLists.txt +index 5557440..f8c8b19 100644 +--- a/js/node/CMakeLists.txt ++++ b/js/node/CMakeLists.txt +@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.11) + + project (onnxruntime-node) + +-set(CMAKE_CXX_STANDARD 14) ++set(CMAKE_CXX_STANDARD 17) + + add_compile_definitions(NAPI_VERSION=${napi_build_version}) + add_compile_definitions(ORT_API_MANUAL_INIT) +diff --git a/onnxruntime/python/tools/tensorrt/perf/mem_test/CMakeLists.txt b/onnxruntime/python/tools/tensorrt/perf/mem_test/CMakeLists.txt +index 2d969cf..fd9de3e 100644 +--- a/onnxruntime/python/tools/tensorrt/perf/mem_test/CMakeLists.txt ++++ b/onnxruntime/python/tools/tensorrt/perf/mem_test/CMakeLists.txt +@@ -4,7 +4,7 @@ set(CMAKE_BUILD_TYPE Debug) + + cmake_minimum_required (VERSION 2.6) + +-set(CMAKE_CXX_STANDARD 14) ++set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + + include_directories( \ No newline at end of file diff --git a/patches/onnx-no-execinfo.patch b/patches/onnx-no-execinfo.patch new file mode 100644 index 000000000..4bc83fe5c --- /dev/null +++ b/patches/onnx-no-execinfo.patch @@ -0,0 +1,22 @@ +diff --git a/onnxruntime/core/platform/posix/stacktrace.cc b/onnxruntime/core/platform/posix/stacktrace.cc +index 76864b2..185f131 100644 +--- a/onnxruntime/core/platform/posix/stacktrace.cc ++++ b/onnxruntime/core/platform/posix/stacktrace.cc +@@ -3,7 +3,7 @@ + + #include "core/common/common.h" + +-#if !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX) ++#if defined(__GLIBC__) && !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX) + #include + #endif + #include +@@ -13,7 +13,7 @@ namespace onnxruntime { + std::vector GetStackTrace() { + std::vector stack; + +-#if !defined(NDEBUG) && !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) ++#if defined(__GLIBC__) && !defined(NDEBUG) && !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) + constexpr int kCallstackLimit = 64; // Maximum depth of callstack + + void* array[kCallstackLimit]; \ No newline at end of file diff --git a/patches/onnx-system.patch b/patches/onnx-system.patch new file mode 100644 index 000000000..1d05bd958 --- /dev/null +++ b/patches/onnx-system.patch @@ -0,0 +1,57 @@ +diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake +index 9effd1a2db..ed15ad31aa 100644 +--- a/cmake/external/onnxruntime_external_deps.cmake ++++ b/cmake/external/onnxruntime_external_deps.cmake +@@ -20,16 +20,16 @@ endforeach() + message("Loading Dependencies ...") + # ABSL should be included before protobuf because protobuf may use absl + if(NOT onnxruntime_DISABLE_ABSEIL) +- include(external/abseil-cpp.cmake) ++ find_package(absl REQUIRED) ++ set(ABSEIL_LIBS absl::inlined_vector absl::flat_hash_set ++ absl::flat_hash_map absl::node_hash_set absl::node_hash_map absl::base absl::throw_delegate absl::raw_hash_set ++ absl::hash absl::city absl::low_level_hash absl::raw_logging_internal) + endif() ++find_package(Protobuf REQUIRED) ++find_package(re2 REQUIRED) ++find_package(nlohmann_json REQUIRED) + + set(RE2_BUILD_TESTING OFF CACHE BOOL "" FORCE) +-FetchContent_Declare( +- re2 +- URL ${DEP_URL_re2} +- URL_HASH SHA1=${DEP_SHA1_re2} +- FIND_PACKAGE_ARGS NAMES re2 +-) + + if (onnxruntime_BUILD_UNIT_TESTS) + # WebAssembly threading support in Node.js is still an experimental feature and +@@ -163,7 +163,6 @@ if (onnxruntime_DISABLE_RTTI) + set(protobuf_DISABLE_RTTI ON CACHE BOOL "Remove runtime type information in the binaries" FORCE) + endif() + +-include(protobuf_function) + #protobuf end + + set(ENABLE_DATE_TESTING OFF CACHE BOOL "" FORCE) +@@ -303,7 +302,7 @@ FetchContent_Declare( + ) + + # The next line will generate an error message "fatal: not a git repository", but it is ok. It is from flatbuffers +-onnxruntime_fetchcontent_makeavailable(Protobuf nlohmann_json mp11 re2 safeint GSL flatbuffers) ++onnxruntime_fetchcontent_makeavailable(mp11 safeint GSL flatbuffers) + if(NOT flatbuffers_FOUND) + if(NOT TARGET flatbuffers::flatbuffers) + add_library(flatbuffers::flatbuffers ALIAS flatbuffers) +diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake +index 9347be180d..8c60ff40f4 100644 +--- a/cmake/onnxruntime_unittests.cmake ++++ b/cmake/onnxruntime_unittests.cmake +@@ -876,6 +876,7 @@ endif() + onnxruntime_add_include_to_target(onnx_test_data_proto onnx_proto) + target_include_directories(onnx_test_data_proto PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + set_target_properties(onnx_test_data_proto PROPERTIES FOLDER "ONNXRuntimeTest") ++include(protobuf_function) + onnxruntime_protobuf_generate(APPEND_PATH IMPORT_DIRS ${onnx_SOURCE_DIR} TARGET onnx_test_data_proto) + + # \ No newline at end of file