xref: /aosp_15_r20/external/pytorch/cmake/External/nnpack.cmake (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1if(__NNPACK_INCLUDED)
2  return()
3endif()
4set(__NNPACK_INCLUDED TRUE)
5
6if(NOT USE_NNPACK)
7  return()
8endif()
9
10##############################################################################
11# NNPACK is built together with Caffe2
12# By default, it builds code from third-party/NNPACK submodule.
13# Define NNPACK_SOURCE_DIR to build with a different version.
14##############################################################################
15
16##############################################################################
17# (1) MSVC - unsupported
18##############################################################################
19
20if(MSVC)
21  message(WARNING "NNPACK not supported on MSVC yet. Turn this warning off by USE_NNPACK=OFF.")
22  set(USE_NNPACK OFF)
23  return()
24endif()
25
26##############################################################################
27# (2) Anything but x86, x86-64, ARM, ARM64 - unsupported
28##############################################################################
29if(CMAKE_SYSTEM_PROCESSOR)
30  if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|x86_64|armv5te|armv7-a|armv7l|arm64|aarch64)$")
31    message(WARNING "NNPACK is not supported on ${CMAKE_SYSTEM_PROCESSOR} processors. "
32      "The only supported architectures are x86, x86-64, ARM, and ARM64. "
33      "Turn this warning off by USE_NNPACK=OFF.")
34    set(USE_NNPACK OFF)
35    return()
36  endif()
37endif()
38
39##############################################################################
40# (3) Android, iOS, Linux, macOS - supported
41##############################################################################
42
43if(ANDROID OR IOS OR ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
44  message(STATUS "Brace yourself, we are building NNPACK")
45  set(CAFFE2_THIRD_PARTY_ROOT ${PROJECT_SOURCE_DIR}/third_party)
46
47  # Directories for NNPACK dependencies submoduled in Caffe2
48  set(PYTHON_PEACHPY_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/python-peachpy" CACHE STRING "PeachPy (Python package) source directory")
49  if(NOT DEFINED CPUINFO_SOURCE_DIR)
50    set(CPUINFO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/cpuinfo" CACHE STRING "cpuinfo source directory")
51  endif()
52  set(NNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/NNPACK" CACHE STRING "NNPACK source directory")
53  set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
54  set(FXDIV_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FXdiv" CACHE STRING "FXdiv source directory")
55  set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
56  set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
57  set(GOOGLETEST_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/googletest" CACHE STRING "Google Test source directory")
58
59  if(NOT TARGET nnpack)
60    if(NOT USE_SYSTEM_PTHREADPOOL AND USE_INTERNAL_PTHREADPOOL_IMPL)
61      set(NNPACK_CUSTOM_THREADPOOL ON CACHE BOOL "")
62    endif()
63
64    set(NNPACK_BUILD_TESTS OFF CACHE BOOL "")
65    set(NNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
66    set(NNPACK_LIBRARY_TYPE "static" CACHE STRING "")
67    set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
68    set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
69    add_subdirectory(
70      "${NNPACK_SOURCE_DIR}"
71      "${CONFU_DEPENDENCIES_BINARY_DIR}/NNPACK")
72    # We build static versions of nnpack and pthreadpool but link
73    # them into a shared library for Caffe2, so they need PIC.
74    set_property(TARGET nnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
75    set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
76    set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
77
78    if(NNPACK_CUSTOM_THREADPOOL)
79      target_compile_definitions(
80        nnpack PRIVATE
81        pthreadpool_t=legacy_pthreadpool_t
82        pthreadpool_function_1d_t=legacy_pthreadpool_function_1d_t
83        pthreadpool_function_1d_tiled_t=legacy_pthreadpool_function_1d_tiled_t
84        pthreadpool_function_2d_t=legacy_pthreadpool_function_2d_t
85        pthreadpool_function_2d_tiled_t=legacy_pthreadpool_function_2d_tiled_t
86        pthreadpool_function_3d_tiled_t=legacy_pthreadpool_function_3d_tiled_t
87        pthreadpool_function_4d_tiled_t=legacy_pthreadpool_function_4d_tiled_t
88        pthreadpool_create=legacy_pthreadpool_create
89        pthreadpool_destroy=legacy_pthreadpool_destroy
90        pthreadpool_get_threads_count=legacy_pthreadpool_get_threads_count
91        pthreadpool_compute_1d=legacy_pthreadpool_compute_1d
92        pthreadpool_parallelize_1d=legacy_pthreadpool_parallelize_1d
93        pthreadpool_compute_1d_tiled=legacy_pthreadpool_compute_1d_tiled
94        pthreadpool_compute_2d=legacy_pthreadpool_compute_2d
95        pthreadpool_compute_2d_tiled=legacy_pthreadpool_compute_2d_tiled
96        pthreadpool_compute_3d_tiled=legacy_pthreadpool_compute_3d_tiled
97        pthreadpool_compute_4d_tiled=legacy_pthreadpool_compute_4d_tiled)
98    endif()
99  endif()
100
101  set(NNPACK_FOUND TRUE)
102  if(TARGET nnpack)
103    set(NNPACK_INCLUDE_DIRS
104      $<TARGET_PROPERTY:nnpack,INCLUDE_DIRECTORIES>
105      $<TARGET_PROPERTY:pthreadpool,INCLUDE_DIRECTORIES>)
106    set(NNPACK_LIBRARIES $<TARGET_OBJECTS:nnpack> $<TARGET_OBJECTS:cpuinfo>)
107  endif()
108  return()
109endif()
110
111##############################################################################
112# (4) Catch-all: not supported.
113##############################################################################
114
115message(WARNING "Unknown platform - I don't know how to build NNPACK. "
116                "See cmake/External/nnpack.cmake for details.")
117set(USE_NNPACK OFF)
118