From accffded5deaa561c400d1f8945ccd8dab589a35 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Wed, 1 Apr 2020 10:07:22 -0700 Subject: [PATCH] Build options for enabling AVX/AVX2/AVX512 (#3373) 1. Add build options for enabling AVX/AVX2/AVX512 2. Update eigen to a newer version, because the current one doesn't work with VC and AVX512. --- cgmanifest.json | 2 +- cmake/CMakeLists.txt | 39 +++++++++++++++++++++++++++++++++------ cmake/external/eigen | 2 +- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cgmanifest.json b/cgmanifest.json index 0adbebf636..57664cb0e8 100644 --- a/cgmanifest.json +++ b/cgmanifest.json @@ -139,7 +139,7 @@ "component": { "type": "git", "git": { - "commitHash": "efd9867ff0e8df23016ac6c9828d0d7bf8bec1b1", + "commitHash": "54a0a9c9dd83aad42e79238ec8f0932b2a5e7881", "repositoryUrl": "https://gitlab.com/libeigen/eigen.git" } } diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index d07a64b2f8..6a48a01a73 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -69,6 +69,9 @@ option(onnxruntime_USE_PREINSTALLED_EIGEN "Use pre-installed EIGEN. Need to prov option(onnxruntime_BUILD_BENCHMARKS "Build ONNXRuntime micro-benchmarks" OFF) option(onnxruntime_USE_TVM "Build tvm for code-gen" OFF) option(onnxruntime_BUILD_FOR_NATIVE_MACHINE "Enable this option for turning on optimization specific to this machine" OFF) +option(onnxruntime_USE_AVX "Use AVX instructions" OFF) +option(onnxruntime_USE_AVX2 "Use AVX2 instructions" OFF) +option(onnxruntime_USE_AVX512 "Use AVX512 instructions" OFF) option(onnxruntime_USE_LLVM "Build tvm with LLVM" OFF) option(onnxruntime_USE_OPENMP "Build with OpenMP support" OFF) option(onnxruntime_BUILD_SHARED_LIB "Build a shared library" OFF) @@ -115,7 +118,7 @@ if(NOT WIN32) else() check_cxx_compiler_flag(/d2FH4- HAS_D2FH4) if (HAS_D2FH4) - message("Enabling /d2FH4-") + message("Disabling /d2FH4") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /d2FH4-") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /d2FH4-") endif() @@ -208,8 +211,25 @@ 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() + #A compile option to improve code generation time, mostly for the code that heavily uses eigen. + check_cxx_compiler_flag(/d2ReducedOptimizeHugeFunctions HAS_D2ROHF) + if(HAS_D2ROHF) + SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /d2ReducedOptimizeHugeFunctions") + SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /d2ReducedOptimizeHugeFunctions") + endif() #Always enable exception handling, even for Windows ARM - SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") + string(APPEND CMAKE_CXX_FLAGS " /EHsc") + string(APPEND CMAKE_C_FLAGS " /EHsc") + if(onnxruntime_USE_AVX) + string(APPEND CMAKE_CXX_FLAGS " /arch:AVX") + string(APPEND CMAKE_C_FLAGS " /arch:AVX") + elseif(onnxruntime_USE_AVX2) + string(APPEND CMAKE_CXX_FLAGS " /arch:AVX2") + string(APPEND CMAKE_C_FLAGS " /arch:AVX2") + elseif(onnxruntime_USE_AVX512) + 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") @@ -234,10 +254,17 @@ if (MSVC) endif() else() if(onnxruntime_BUILD_FOR_NATIVE_MACHINE) - string(APPEND CMAKE_CXX_FLAGS_RELEASE " -march=native -mtune=native") - string(APPEND CMAKE_C_FLAGS_RELEASE " -march=native -mtune=native") - string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -march=native -mtune=native") - string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " -march=native -mtune=native") + string(APPEND CMAKE_CXX_FLAGS " -march=native -mtune=native") + string(APPEND CMAKE_C_FLAGS " -march=native -mtune=native") + elseif(onnxruntime_USE_AVX) + string(APPEND CMAKE_CXX_FLAGS " -mavx") + string(APPEND CMAKE_C_FLAGS " -mavx") + elseif(onnxruntime_USE_AVX2) + string(APPEND CMAKE_CXX_FLAGS " -mavx2") + string(APPEND CMAKE_C_FLAGS " -mavx2") + elseif(onnxruntime_USE_AVX512) + string(APPEND CMAKE_CXX_FLAGS " -mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl") + string(APPEND CMAKE_C_FLAGS " -mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl") endif() endif() diff --git a/cmake/external/eigen b/cmake/external/eigen index efd9867ff0..54a0a9c9dd 160000 --- a/cmake/external/eigen +++ b/cmake/external/eigen @@ -1 +1 @@ -Subproject commit efd9867ff0e8df23016ac6c9828d0d7bf8bec1b1 +Subproject commit 54a0a9c9dd83aad42e79238ec8f0932b2a5e7881