[cmake] add USE_SYSTEM_{XNNPACK,ONNX} options. (#37501)

Summary:
ezyang
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37501

Differential Revision: D21303527

Pulled By: ezyang

fbshipit-source-id: 58353d78c66e5bcc9198ce8cde36ac7232bb4b2f
This commit is contained in:
Mo Zhou 2020-04-29 09:20:15 -07:00 committed by Facebook GitHub Bot
parent 0d9e3b48c4
commit 58a46a174e
3 changed files with 51 additions and 12 deletions

View File

@ -222,6 +222,8 @@ option(USE_SYSTEM_PTHREADPOOL "Use system-provided pthreadpool." OFF)
option(USE_SYSTEM_PSIMD "Use system-provided psimd." OFF) option(USE_SYSTEM_PSIMD "Use system-provided psimd." OFF)
option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF) option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF)
option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF) option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF)
option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF)
option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF)
if(USE_SYSTEM_LIBS) if(USE_SYSTEM_LIBS)
set(USE_SYSTEM_CPUINFO ON) set(USE_SYSTEM_CPUINFO ON)
set(USE_SYSTEM_SLEEF ON) set(USE_SYSTEM_SLEEF ON)
@ -233,6 +235,8 @@ if(USE_SYSTEM_LIBS)
set(USE_SYSTEM_PSIMD ON) set(USE_SYSTEM_PSIMD ON)
set(USE_SYSTEM_FXDIV ON) set(USE_SYSTEM_FXDIV ON)
set(USE_SYSTEM_BENCHMARK ON) set(USE_SYSTEM_BENCHMARK ON)
set(USE_SYSTEM_ONNX ON)
set(USE_SYSTEM_XNNPACK ON)
endif() endif()
# Used when building Caffe2 through setup.py # Used when building Caffe2 through setup.py
@ -246,7 +250,11 @@ cmake_dependent_option(
MSVC_Z7_OVERRIDE "Work around sccache bug by replacing /Zi and /ZI with /Z7 when using MSVC (if you are not using sccache, you can turn this OFF)" ON MSVC_Z7_OVERRIDE "Work around sccache bug by replacing /Zi and /ZI with /Z7 when using MSVC (if you are not using sccache, you can turn this OFF)" ON
"MSVC" OFF) "MSVC" OFF)
set(ONNX_NAMESPACE "onnx_torch" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.") if(NOT USE_SYSTEM_ONNX)
set(ONNX_NAMESPACE "onnx_torch" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.")
elseif()
set(ONNX_NAMESPACE "onnx" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.")
endif()
set(SELECTED_OP_LIST "" CACHE STRING set(SELECTED_OP_LIST "" CACHE STRING
"Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default.") "Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default.")
set(OP_DEPENDENCY "" CACHE STRING set(OP_DEPENDENCY "" CACHE STRING

View File

@ -299,7 +299,7 @@ endif()
# that allows us to hijack pthreadpool interface. # that allows us to hijack pthreadpool interface.
# Thus not doing this ends up building pthreadpool as well as # Thus not doing this ends up building pthreadpool as well as
# the internal implemenation of pthreadpool which results in symbol conflicts. # the internal implemenation of pthreadpool which results in symbol conflicts.
if(USE_XNNPACK) if(USE_XNNPACK AND NOT USE_SYSTEM_XNNPACK)
if(NOT DEFINED PTHREADPOOL_SOURCE_DIR) if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party") set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory") set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
@ -430,7 +430,7 @@ if(USE_NNPACK)
endif() endif()
# ---[ XNNPACK # ---[ XNNPACK
if(USE_XNNPACK) if(USE_XNNPACK AND NOT USE_SYSTEM_XNNPACK)
if(NOT DEFINED XNNPACK_SOURCE_DIR) if(NOT DEFINED XNNPACK_SOURCE_DIR)
set(XNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/XNNPACK" CACHE STRING "XNNPACK source directory") set(XNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/XNNPACK" CACHE STRING "XNNPACK source directory")
endif() endif()
@ -463,6 +463,15 @@ if(USE_XNNPACK)
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR}) include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK) list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
elseif(NOT TARGET XNNPACK AND USE_SYSTEM_XNNPACK)
add_library(XNNPACK SHARED IMPORTED)
find_library(XNNPACK_LIBRARY XNNPACK)
set_property(TARGET XNNPACK PROPERTY IMPORTED_LOCATION "${XNNPACK_LIBRARY}")
if(NOT XNNPACK_LIBRARY)
message(FATAL_ERROR "Cannot find XNNPACK")
endif()
message("-- Found XNNPACK: ${XNNPACK_LIBRARY}")
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
endif() endif()
# ---[ gflags # ---[ gflags
@ -1280,20 +1289,39 @@ if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_DISABLE_ONNX)
add_definitions(-DONNXIFI_ENABLE_EXT=1) add_definitions(-DONNXIFI_ENABLE_EXT=1)
# Add op schemas in "ai.onnx.pytorch" domain # Add op schemas in "ai.onnx.pytorch" domain
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../caffe2/onnx/torch_ops") add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../caffe2/onnx/torch_ops")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL) if(NOT USE_SYSTEM_ONNX)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)
endif()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL)
include_directories(${ONNX_INCLUDE_DIRS})
include_directories(${FOXI_INCLUDE_DIRS})
add_definitions(-DONNX_NAMESPACE=${ONNX_NAMESPACE}) add_definitions(-DONNX_NAMESPACE=${ONNX_NAMESPACE})
# In mobile build we care about code size, and so we need drop if(NOT USE_SYSTEM_ONNX)
# everything (e.g. checker, optimizer) in onnx but the pb definition. include_directories(${ONNX_INCLUDE_DIRS})
if(ANDROID OR IOS) # In mobile build we care about code size, and so we need drop
caffe2_interface_library(onnx_proto onnx_library) # everything (e.g. checker, optimizer) in onnx but the pb definition.
if(ANDROID OR IOS)
caffe2_interface_library(onnx_proto onnx_library)
else()
caffe2_interface_library(onnx onnx_library)
endif()
list(APPEND Caffe2_DEPENDENCY_WHOLE_LINK_LIBS onnx_library)
else() else()
caffe2_interface_library(onnx onnx_library) add_library(onnx SHARED IMPORTED)
find_library(ONNX_LIBRARY onnx)
if(NOT ONNX_LIBRARY)
message(FATAL_ERROR "Cannot find onnx")
endif()
set_property(TARGET onnx PROPERTY IMPORTED_LOCATION ${ONNX_LIBRARY})
add_library(onnx_proto SHARED IMPORTED)
find_library(ONNX_PROTO_LIBRARY onnx_proto)
if(NOT ONNX_PROTO_LIBRARY)
message(FATAL_ERROR "Cannot find onnx")
endif()
set_property(TARGET onnx_proto PROPERTY IMPORTED_LOCATION ${ONNX_PROTO_LIBRARY})
message("-- Found onnx: ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY}")
list(APPEND Caffe2_DEPENDENCY_LIBS onnx_proto onnx)
endif() endif()
list(APPEND Caffe2_DEPENDENCY_WHOLE_LINK_LIBS onnx_library) include_directories(${FOXI_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS foxi_loader) list(APPEND Caffe2_DEPENDENCY_LIBS foxi_loader)
# Recover the build shared libs option. # Recover the build shared libs option.
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS}) set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS})

View File

@ -47,6 +47,9 @@ add_executable(test_jit
${TORCH_ROOT}/test/cpp/common/main.cpp ${TORCH_ROOT}/test/cpp/common/main.cpp
${JIT_TEST_ROOT}/gtest.cpp ${JIT_TEST_ROOT}/gtest.cpp
${JIT_TEST_SRCS}) ${JIT_TEST_SRCS})
if(USE_SYSTEM_ONNX)
target_link_libraries(test_jit PRIVATE onnx_proto onnx)
endif()
set(JIT_TEST_DEPENDENCIES torch gtest) set(JIT_TEST_DEPENDENCIES torch gtest)