diff --git a/cmake/external/xnnpack.cmake b/cmake/external/xnnpack.cmake index 4df57b5ed1..5c40e3f6b3 100644 --- a/cmake/external/xnnpack.cmake +++ b/cmake/external/xnnpack.cmake @@ -8,8 +8,13 @@ set(FP16_BUILD_BENCHMARKS OFF CACHE INTERNAL "") set(CLOG_SOURCE_DIR "${PYTORCH_CPUINFO_DIR}/deps/clog") set(CPUINFO_SOURCE_DIR ${PYTORCH_CPUINFO_DIR}) -if(onnxruntime_BUILD_WEBASSEMBLY) - execute_process(COMMAND git apply --ignore-space-change --ignore-whitespace ${PROJECT_SOURCE_DIR}/patches/xnnpack/AddEmscriptenSupport.patch WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/${XNNPACK_DIR}) +# BF16 instructions cause ICE in Android NDK compiler +if(CMAKE_ANDROID_ARCH_ABI STREQUAL armeabi-v7a) + set(XNNPACK_ENABLE_ARM_BF16 OFF) +ENDIF() + +if(onnxruntime_BUILD_WEBASSEMBLY OR CMAKE_SYSTEM_NAME STREQUAL "iOS") + execute_process(COMMAND git apply --ignore-space-change --ignore-whitespace ${PROJECT_SOURCE_DIR}/patches/xnnpack/AddEmscriptenAndIosSupport.patch WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/${XNNPACK_DIR}) endif() add_subdirectory(external/FP16) diff --git a/cmake/patches/xnnpack/AddEmscriptenAndIosSupport.patch b/cmake/patches/xnnpack/AddEmscriptenAndIosSupport.patch new file mode 100644 index 0000000000..7296f2f30f --- /dev/null +++ b/cmake/patches/xnnpack/AddEmscriptenAndIosSupport.patch @@ -0,0 +1,24 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index d53c48aa1..4c987bd7a 100755 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -105,7 +105,7 @@ ENDIF() + + IF(NOT CMAKE_SYSTEM_NAME) + MESSAGE(FATAL_ERROR "CMAKE_SYSTEM_NAME not defined") +-ELSEIF(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|Android|Windows|CYGWIN|MSYS)$") ++ELSEIF(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|Android|Windows|CYGWIN|MSYS|Emscripten|iOS)$") + MESSAGE(FATAL_ERROR "Unrecognized CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}") + ENDIF() + +@@ -7108,6 +7108,10 @@ IF(MSVC) + SET_PROPERTY(SOURCE ${ALL_MICROKERNEL_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS "$<$>: /O2 >") + SET_PROPERTY(SOURCE ${HOT_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS "$<$>: /O2 >") + SET_PROPERTY(SOURCE ${COLD_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS "$<$>: /O1 >") ++ELSEIF(CMAKE_GENERATOR STREQUAL Xcode) ++ TARGET_COMPILE_OPTIONS(all_microkernels PRIVATE $<$>: -O2 >) ++ TARGET_COMPILE_OPTIONS(XNNPACK PRIVATE $<$>: -O2 >) ++ TARGET_COMPILE_OPTIONS(XNNPACK PRIVATE $<$>: -Os >) + ELSE() + SET_PROPERTY(SOURCE ${ALL_MICROKERNEL_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS "$<$>: -O2 >") + SET_PROPERTY(SOURCE ${HOT_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS "$<$>: -O2 >") diff --git a/cmake/patches/xnnpack/AddEmscriptenSupport.patch b/cmake/patches/xnnpack/AddEmscriptenSupport.patch deleted file mode 100644 index d2374e3004..0000000000 --- a/cmake/patches/xnnpack/AddEmscriptenSupport.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 937b17f51..af0d2300f 100755 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -82,7 +82,7 @@ ENDIF() - - IF(NOT CMAKE_SYSTEM_NAME) - MESSAGE(FATAL_ERROR "CMAKE_SYSTEM_NAME not defined") --ELSEIF(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|Android|Windows|CYGWIN|MSYS)$") -+ELSEIF(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|Android|Windows|CYGWIN|MSYS|Emscripten)$") - MESSAGE(FATAL_ERROR "Unrecognized CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}") - ENDIF() - diff --git a/tools/ci_build/github/apple/default_full_ios_framework_build_settings.json b/tools/ci_build/github/apple/default_full_ios_framework_build_settings.json index 1bcad4d6ae..9a18e3bf9a 100644 --- a/tools/ci_build/github/apple/default_full_ios_framework_build_settings.json +++ b/tools/ci_build/github/apple/default_full_ios_framework_build_settings.json @@ -16,6 +16,7 @@ "--use_coreml", "--skip_tests", "--cmake_extra_defines=onnxruntime_BUILD_UNIT_TESTS=OFF", - "--apple_deploy_target=11.0" + "--apple_deploy_target=11.0", + "--use_xnnpack" ] } diff --git a/tools/ci_build/github/azure-pipelines/mac-ios-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/mac-ios-ci-pipeline.yml index 86c33c43ab..af2f497fdb 100644 --- a/tools/ci_build/github/azure-pipelines/mac-ios-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/mac-ios-ci-pipeline.yml @@ -1,3 +1,8 @@ +parameters: +- name: BuildXnnpack + default: false + type: boolean + jobs: - job: iOS_CI_CPU_on_Mac pool: @@ -58,3 +63,34 @@ jobs: --path_to_protoc_exe $(Build.BinariesDirectory)/protobuf_install/bin/protoc \ --parallel displayName: (CoreML EP) Build onnxruntime for iOS x86_64 and run tests using simulator + +- job: iOS_CI_XNNPACK_on_Mac + pool: + vmImage: 'macOS-11' + variables: + MACOSX_DEPLOYMENT_TARGET: '10.14' + timeoutInMinutes: 100 + condition: or(eq('${{parameters.BuildXnnpack}}', 'true'), eq(variables['Build.SourceBranch'], 'refs/heads/main')) + steps: + - script: | + /bin/bash $(Build.SourcesDirectory)/tools/ci_build/github/apple/build_host_protoc.sh \ + $(Build.SourcesDirectory) \ + $(Build.BinariesDirectory)/protobuf \ + $(Build.BinariesDirectory)/protobuf_install + displayName: Build Host Protoc + + - script: | + python3 $(Build.SourcesDirectory)/tools/ci_build/build.py \ + --skip_submodule_sync \ + --build_dir $(Build.BinariesDirectory)/iOS_xnnpack \ + --use_xnnpack \ + --ios \ + --ios_sysroot iphonesimulator \ + --osx_arch x86_64 \ + --apple_deploy_target 11.0 \ + --use_xcode \ + --config RelWithDebInfo \ + --build_apple_framework \ + --path_to_protoc_exe $(Build.BinariesDirectory)/protobuf_install/bin/protoc \ + --parallel + displayName: (XNNPACK EP) Build onnxruntime for iOS x86_64 and run tests using simulator