Skip to content

Commit 7ce2369

Browse files
huydhnfacebook-github-bot
authored andcommitted
Build xnn_executor_runner with cmake (#153)
Summary: So that the binary can be used in the same way as buck2 Pull Request resolved: #153 Reviewed By: kirklandsign Differential Revision: D48727218 Pulled By: huydhn fbshipit-source-id: a06c3316e4b6c4c8edbbd309115a2b7283bf8359
1 parent 68f6c20 commit 7ce2369

File tree

11 files changed

+187
-10
lines changed

11 files changed

+187
-10
lines changed

.ci/scripts/setup-macos.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ install_buck() {
5757
fi
5858
}
5959

60-
if [[ "${BUILD_TOOL}" == "buck2" ]]; then
61-
install_buck
62-
fi
60+
# NB: we need buck2 in all cases because cmake build also depends on calling
61+
# buck2 atm
62+
install_buck
6363
install_conda
6464
install_pip_dependencies
6565
install_executorch

.ci/scripts/test.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ test_model_with_xnnpack() {
6565
if [[ "${BUILD_TOOL}" == "buck2" ]]; then
6666
buck2 run //examples/backend:xnn_executor_runner -- --model_path "${OUTPUT_MODEL_PATH}"
6767
elif [[ "${BUILD_TOOL}" == "cmake" ]]; then
68-
# TODO: Add cmake support for xnn_executor_runner
69-
echo "XNNPACK doesn't support cmake yet, skipping..."
68+
./${CMAKE_OUTPUT_DIR}/backends/xnnpack/xnn_executor_runner --model_path "${OUTPUT_MODEL_PATH}"
7069
else
7170
echo "Invalid build tool ${BUILD_TOOL}. Only buck2 and cmake are supported atm"
7271
exit 1

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ build/lib/
88
exir/serialize/scalar_type.fbs
99
exir/serialize/program.fbs
1010

11+
# Any exported models
12+
*.pte
13+
1114
# Editor temporaries
1215
*.swa
1316
*.swb

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,7 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
171171
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
172172
endif()
173173

174+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack)
175+
174176
# Print all summary
175177
executorch_print_configuration_summary()

backends/xnnpack/CMakeLists.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Please this file formatted by running:
8+
# ~~~
9+
# cmake-format --first-comment-is-literal=True CMakeLists.txt
10+
# ~~~
11+
12+
cmake_minimum_required(VERSION 3.19)
13+
14+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
15+
if(NOT CMAKE_CXX_STANDARD)
16+
set(CMAKE_CXX_STANDARD 17)
17+
endif()
18+
19+
if(NOT PYTHON_EXECUTABLE)
20+
set(PYTHON_EXECUTABLE python3)
21+
endif()
22+
# Source root directory for executorch.
23+
if(NOT EXECUTORCH_ROOT)
24+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
25+
endif()
26+
27+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
28+
29+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
30+
set(_common_compile_options -Wno-deprecated-declarations)
31+
32+
set(_xnnpack_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
33+
# Paths to headers generated from the .fbs files.
34+
set(_xnnpack_schema__outputs)
35+
foreach(fbs_file ${_xnnpack_schema__srcs})
36+
string(REGEX REPLACE "serialization/([^/]+)[.]fbs$" "\\1_generated.h"
37+
generated "${fbs_file}")
38+
list(APPEND _xnnpack_schema__outputs
39+
"${_xnnpack_schema__include_dir}/executorch/${generated}")
40+
endforeach()
41+
42+
# Generate the headers from the .fbs files.
43+
add_custom_command(
44+
OUTPUT ${_xnnpack_schema__outputs}
45+
COMMAND
46+
flatc --cpp --cpp-std c++11 --scoped-enums -o
47+
"${_xnnpack_schema__include_dir}/executorch/backends/xnnpack"
48+
${_xnnpack_schema__srcs}
49+
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
50+
COMMENT "Generating xnnpack_schema headers"
51+
VERBATIM)
52+
53+
add_library(xnnpack_schema INTERFACE ${_xnnpack_schema__outputs})
54+
set_target_properties(xnnpack_schema PROPERTIES LINKER_LANGUAGE CXX)
55+
target_include_directories(
56+
xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
57+
${EXECUTORCH_ROOT}/third-party/flatbuffers/include)
58+
59+
set(xnn_executor_runner_libs ${_libs} xnnpack_schema)
60+
61+
list(TRANSFORM _dynamic_quant_utils__srcs PREPEND "${EXECUTORCH_ROOT}/")
62+
add_library(dynamic_quant_utils ${_dynamic_quant_utils__srcs})
63+
set_target_properties(dynamic_quant_utils PROPERTIES LINKER_LANGUAGE CXX)
64+
target_include_directories(dynamic_quant_utils
65+
PUBLIC ${_common_include_directories})
66+
target_compile_options(dynamic_quant_utils PUBLIC ${_common_compile_options})
67+
68+
if(ENABLE_DYNAMIC_QUANTIZATION)
69+
list(APPEND xnn_executor_runner_libs dynamic_quant_utils)
70+
endif()
71+
72+
include(cmake/Dependencies.cmake)
73+
74+
list(TRANSFORM _xnnpack_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
75+
add_library(xnnpack_backend ${_xnnpack_backend__srcs})
76+
target_link_libraries(xnnpack_backend PRIVATE ${xnn_executor_runner_libs})
77+
target_include_directories(xnnpack_backend
78+
PUBLIC ${_common_include_directories})
79+
target_include_directories(xnnpack_backend PUBLIC ${XNNPACK_INCLUDE_DIR})
80+
target_compile_options(xnnpack_backend PUBLIC ${_common_compile_options})
81+
target_link_options_shared_lib(xnnpack_backend)
82+
83+
list(APPEND xnn_executor_runner_libs xnnpack_backend)
84+
85+
# ios can only build library but not binary
86+
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
87+
#
88+
# xnn_executor_runner: Like executor_runner but with XNNPACK, the binary will
89+
# be at ${CMAKE_BINARY_DIR}/backends/xnnpack
90+
#
91+
list(TRANSFORM _xnn_executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
92+
add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
93+
target_link_libraries(xnn_executor_runner ${xnn_executor_runner_libs})
94+
target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})
95+
endif()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# The logic is copied from https://github.com/pytorch/pytorch/blob/main/cmake/Dependencies.cmake
8+
set(THIRD_PARTY_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/third-party")
9+
10+
# --- cpuinfo
11+
set(CPUINFO_SOURCE_DIR "${THIRD_PARTY_ROOT}/cpuinfo")
12+
set(CPUINFO_BUILD_TOOLS OFF CACHE BOOL "")
13+
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "")
14+
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "")
15+
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "")
16+
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
17+
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
18+
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
19+
add_subdirectory("${CPUINFO_SOURCE_DIR}")
20+
list(APPEND xnn_executor_runner_libs cpuinfo)
21+
22+
# --- pthreadpool
23+
set(PTHREADPOOL_SOURCE_DIR "${THIRD_PARTY_ROOT}/pthreadpool")
24+
set(PTHREADPOOL_BUILD_TESTS OFF CACHE BOOL "")
25+
set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
26+
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
27+
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
28+
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
29+
list(APPEND xnn_executor_runner_libs pthreadpool)
30+
31+
# --- XNNPACK
32+
set(XNNPACK_SOURCE_DIR "${THIRD_PARTY_ROOT}/XNNPACK")
33+
set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include")
34+
set(XNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
35+
set(XNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
36+
set(XNNPACK_BUILD_TESTS OFF CACHE BOOL "")
37+
add_subdirectory("${XNNPACK_SOURCE_DIR}")
38+
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
39+
list(APPEND xnn_executor_runner_libs XNNPACK)

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
#include <executorch/backends/xnnpack/runtime/XNNCompiler.h>
10+
#include <executorch/backends/xnnpack/schema_generated.h>
1011
#include <executorch/backends/xnnpack/threadpool/threadpool.h>
11-
#include <executorch/backends/xnnpack/xnnpack_schema_generated.h>
1212
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
1313
#include <unordered_map>
1414

backends/xnnpack/targets.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def define_common_targets():
2828
# file. Use `outs` so that `${OUT}` is expanded as the containing
2929
# directory instead of the file itself.
3030
outs = {
31-
"xnnpack_schema_generated.h": ["schema_generated.h"],
31+
"schema_generated.h": ["schema_generated.h"],
3232
},
3333
cmd = " ".join([
3434
"$(exe {})".format(runtime.external_dep_location("flatc")),
@@ -45,7 +45,7 @@ def define_common_targets():
4545
name = "xnnpack_schema",
4646
srcs = [],
4747
exported_headers = {
48-
"xnnpack_schema_generated.h": ":gen_xnnpack_schema[xnnpack_schema_generated.h]",
48+
"schema_generated.h": ":gen_xnnpack_schema[schema_generated.h]",
4949
},
5050
exported_external_deps = ["flatbuffers-api"],
5151
)

build/cmake_deps.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,42 @@ deps = [
6262
"executorch",
6363
"portable_kernels",
6464
]
65+
66+
[targets.xnnpack_schema]
67+
buck_targets = [
68+
"//backends/xnnpack:xnnpack_schema"
69+
]
70+
filters = [
71+
".fbs$",
72+
]
73+
74+
[targets.dynamic_quant_utils]
75+
buck_targets = [
76+
"//backends/xnnpack:dynamic_quant_utils"
77+
]
78+
filters = [
79+
".cpp$",
80+
]
81+
82+
[targets.xnnpack_backend]
83+
buck_targets = [
84+
"//backends/xnnpack:xnnpack_backend"
85+
]
86+
filters = [
87+
".cpp$",
88+
]
89+
90+
[targets.xnn_executor_runner]
91+
buck_targets = [
92+
"//examples/backend:xnn_executor_runner",
93+
]
94+
filters = [
95+
".cpp$",
96+
]
97+
excludes = [
98+
"^codegen",
99+
]
100+
deps = [
101+
"xnnpack_backend",
102+
"portable_kernels",
103+
]

examples/backend/xnnpack_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"T161880157: Quantization-only without delegation is not supported yet"
6060
)
6161

62-
if args.model_name not in MODEL_NAME_TO_OPTIONS:
62+
if args.model_name not in MODEL_NAME_TO_OPTIONS and args.quantize:
6363
raise RuntimeError(
6464
f"Model {args.model_name} is not a valid name. or not quantizable right now, "
6565
"please contact executorch team if you want to learn why or how to support "

0 commit comments

Comments
 (0)