Skip to content

Commit 1d11325

Browse files
authored
Add CMake build for CpuBackend (#24251)
Summary: `CpuBackend` (`backends/cpu`) builds only with Buck today, so the backend test suite's `cpu_fp32` flow cannot run in OSS CI. This adds an opt-in CMake build. Reviewed By: shoumikhin Differential Revision: D127905370 Pull Request resolved: #24251
1 parent de5a630 commit 1d11325

13 files changed

Lines changed: 263 additions & 79 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ endif()
787787
if(EXECUTORCH_BUILD_NATIVE)
788788
string(APPEND _executorch_backend_dependencies
789789
"find_dependency(libzip CONFIG)\n"
790-
"find_dependency(nlohmann_json CONFIG)\n" "find_dependency(Threads)\n"
790+
"find_dependency(nlohmann_json CONFIG)\n"
791791
)
792792
endif()
793793
if(EXECUTORCH_BUILD_NATIVE_GGML)
@@ -797,7 +797,10 @@ if(EXECUTORCH_BUILD_NATIVE_GGML)
797797
" add_library(executorch_ggml ALIAS ggml::ggml)\n" "endif()\n"
798798
)
799799
endif()
800-
if(EXECUTORCH_BUILD_VULKAN)
800+
if(EXECUTORCH_BUILD_NATIVE
801+
OR EXECUTORCH_BUILD_CPU
802+
OR EXECUTORCH_BUILD_VULKAN
803+
)
801804
string(APPEND _executorch_backend_dependencies "find_dependency(Threads)\n")
802805
endif()
803806
configure_file(
@@ -1417,6 +1420,10 @@ if(EXECUTORCH_BUILD_PYBIND)
14171420
endif()
14181421
endif()
14191422

1423+
if(EXECUTORCH_BUILD_CPU)
1424+
list(APPEND _dep_libs cpu_backend)
1425+
endif()
1426+
14201427
if(EXECUTORCH_BUILD_VULKAN)
14211428
list(APPEND _dep_libs vulkan_backend)
14221429
endif()
@@ -1624,10 +1631,15 @@ if(EXECUTORCH_BUILD_VULKAN)
16241631
list(APPEND _executorch_backends vulkan_backend vulkan_schema)
16251632
endif()
16261633

1627-
if(EXECUTORCH_BUILD_NATIVE)
1634+
if(EXECUTORCH_BUILD_NATIVE OR EXECUTORCH_BUILD_CPU)
16281635
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/native)
16291636
endif()
16301637

1638+
if(EXECUTORCH_BUILD_CPU)
1639+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cpu)
1640+
list(APPEND _executorch_backends cpu_backend)
1641+
endif()
1642+
16311643
if(EXECUTORCH_BUILD_WEBGPU)
16321644
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/webgpu)
16331645
list(APPEND _executorch_backends webgpu_backend)

‎backends/cpu/CMakeLists.txt‎

Lines changed: 127 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,147 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
add_library(
8-
cpu_runtime runtime/KernelProvider.cpp runtime/CPUPlan.cpp
9-
runtime/op_helpers/Convolution.cpp
10-
)
11-
target_link_libraries(
12-
cpu_runtime
13-
PUBLIC native_runtime
14-
PRIVATE extension_threadpool
7+
cmake_minimum_required(VERSION 3.24)
8+
9+
if(NOT EXECUTORCH_ROOT)
10+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
11+
endif()
12+
13+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
14+
15+
if(EXECUTORCH_BUILD_NATIVE_CPU)
16+
add_library(
17+
cpu_runtime runtime/KernelProvider.cpp runtime/CPUPlan.cpp
18+
runtime/op_helpers/Convolution.cpp
19+
)
20+
target_link_libraries(
21+
cpu_runtime
22+
PUBLIC native_runtime
23+
PRIVATE extension_threadpool
24+
)
25+
if(EXECUTORCH_BUILD_SHARED)
26+
target_link_libraries(cpu_runtime PUBLIC executorch_shared)
27+
else()
28+
target_link_libraries(cpu_runtime PUBLIC executorch_core)
29+
endif()
30+
31+
add_library(cpu_reference runtime/providers/reference/ReferenceProvider.cpp)
32+
target_link_libraries(cpu_reference PUBLIC cpu_runtime)
33+
34+
add_library(cpu_xnnpack runtime/providers/xnnpack/XNNPACKProvider.cpp)
35+
target_link_libraries(
36+
cpu_xnnpack
37+
PUBLIC cpu_runtime
38+
PRIVATE extension_threadpool
39+
)
40+
if(EXECUTORCH_BUILD_SHARED)
41+
target_link_libraries(cpu_xnnpack PRIVATE xnnpack_shared)
42+
else()
43+
target_link_libraries(cpu_xnnpack PRIVATE XNNPACK)
44+
endif()
45+
46+
foreach(target cpu_runtime cpu_reference cpu_xnnpack)
47+
add_library(executorch::${target} ALIAS ${target})
48+
target_compile_features(${target} PUBLIC cxx_std_20)
49+
target_compile_options(
50+
${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
51+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
52+
)
53+
endforeach()
54+
55+
install(
56+
TARGETS cpu_runtime cpu_reference cpu_xnnpack
57+
EXPORT ExecuTorchTargets
58+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
59+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
60+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
61+
)
62+
install(
63+
DIRECTORY runtime/
64+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/cpu/runtime
65+
FILES_MATCHING
66+
PATTERN "*.h"
67+
PATTERN "test" EXCLUDE
68+
)
69+
endif()
70+
71+
if(NOT EXECUTORCH_BUILD_CPU)
72+
return()
73+
endif()
74+
75+
# The provider composition that cpu_backend() in targets.bzl generates for the
76+
# default :cpu_backend target.
77+
set(_cpu_provider_registry ${CMAKE_CURRENT_BINARY_DIR}/provider_registry.cpp)
78+
file(
79+
GENERATE
80+
OUTPUT ${_cpu_provider_registry}
81+
CONTENT
82+
"#include <executorch/backends/cpu/runtime/KernelProvider.h>
83+
#include <executorch/backends/cpu/runtime/providers/xnnpack/XNNPACKProvider.h>
84+
#include <executorch/backends/cpu/runtime/providers/reference/ReferenceProvider.h>
85+
namespace executorch::backends::cpu {
86+
namespace {
87+
RuntimeConfiguration runtime_configuration() {
88+
static const ProviderFactory factories[] = {create_xnnpack_provider,create_reference_provider};
89+
return {{factories, sizeof(factories) / sizeof(factories[0])}, {}, false};
90+
}
91+
const bool registered = (register_runtime_configuration(runtime_configuration), true);
92+
}
93+
}
94+
"
1595
)
96+
97+
list(TRANSFORM _cpu_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
1698
if(EXECUTORCH_BUILD_SHARED)
17-
target_link_libraries(cpu_runtime PUBLIC executorch_shared)
99+
set(_cpu_backend_library_type SHARED)
18100
else()
19-
target_link_libraries(cpu_runtime PUBLIC executorch_core)
101+
set(_cpu_backend_library_type STATIC)
20102
endif()
21-
22-
add_library(cpu_reference runtime/providers/reference/ReferenceProvider.cpp)
23-
target_link_libraries(cpu_reference PUBLIC cpu_runtime)
24-
25-
add_library(cpu_xnnpack runtime/providers/xnnpack/XNNPACKProvider.cpp)
103+
add_library(
104+
cpu_backend ${_cpu_backend_library_type} ${_cpu_backend__srcs}
105+
${_cpu_provider_registry}
106+
)
26107
target_link_libraries(
27-
cpu_xnnpack
28-
PUBLIC cpu_runtime
29-
PRIVATE extension_threadpool
108+
cpu_backend PRIVATE native_program extension_threadpool
109+
optimized_native_cpu_ops_lib
30110
)
31111
if(EXECUTORCH_BUILD_SHARED)
32-
target_link_libraries(cpu_xnnpack PRIVATE xnnpack_shared)
112+
set_target_properties(
113+
cpu_backend PROPERTIES OUTPUT_NAME executorch_backend_cpu
114+
)
115+
executorch_target_soname_policy(cpu_backend)
116+
executorch_target_link_shared_runtime(cpu_backend)
117+
# The XNNPACK delegate library already bundles XNNPACK, so resolve against it
118+
# rather than shipping a second copy.
119+
target_link_libraries(
120+
cpu_backend
121+
PUBLIC executorch_shared
122+
PRIVATE xnnpack_backend
123+
)
124+
target_include_directories(
125+
cpu_backend
126+
PRIVATE $<TARGET_PROPERTY:XNNPACK,INTERFACE_INCLUDE_DIRECTORIES>
127+
)
128+
executorch_target_shipped_runtime_path(cpu_backend)
33129
else()
34-
target_link_libraries(cpu_xnnpack PRIVATE XNNPACK)
130+
target_link_libraries(
131+
cpu_backend
132+
PUBLIC executorch_core
133+
PRIVATE XNNPACK xnnpack-microkernels-prod
134+
)
35135
endif()
36-
37-
foreach(target cpu_runtime cpu_reference cpu_xnnpack)
38-
add_library(executorch::${target} ALIAS ${target})
39-
target_compile_features(${target} PUBLIC cxx_std_20)
40-
target_compile_options(
41-
${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
136+
target_compile_features(cpu_backend PRIVATE cxx_std_20)
137+
target_compile_options(
138+
cpu_backend PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
42139
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
43-
)
44-
endforeach()
140+
)
141+
set_target_properties(cpu_backend PROPERTIES POSITION_INDEPENDENT_CODE ON)
142+
executorch_target_link_options_shared_lib(cpu_backend)
45143

46144
install(
47-
TARGETS cpu_runtime cpu_reference cpu_xnnpack
145+
TARGETS cpu_backend
48146
EXPORT ExecuTorchTargets
49147
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
50148
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
51149
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
52150
)
53-
install(
54-
DIRECTORY runtime/
55-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/cpu/runtime
56-
FILES_MATCHING
57-
PATTERN "*.h"
58-
PATTERN "test" EXCLUDE
59-
)

‎backends/cpu/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ The shared `KernelProvider` contract, `CPUPlan`, and `CpuBackend` remain in
3737
[`targets.bzl`](targets.bzl).
3838

3939
The Buck `cpu_backend` target links the baseline XNNPACK and Reference providers.
40+
The CMake `cpu_backend` target (`-DEXECUTORCH_BUILD_CPU=ON`, which requires
41+
`-DEXECUTORCH_BUILD_XNNPACK=ON` and `-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON`)
42+
links the same composition, including the full optimized and portable kernel
43+
library for the Reference provider. Selective kernel builds are not supported.
44+
The CPU backend is disabled by default, including in Python wheels. Enable it
45+
for an editable install with
46+
`CMAKE_ARGS="-DEXECUTORCH_BUILD_CPU=ON" ./install_executorch.sh --editable`.
4047
Applications compose providers with [`cpu_backend()`](cpu_backend.bzl).
4148

4249
`CPUPlan` retains selection, regions, storage requirements and prepared executables

‎backends/cpu/targets.bzl‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
2+
load("@fbsource//xplat/executorch/build:build_variables.bzl", "CPU_RUNTIME_BUCK_SRCS")
23
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
34
load(":cpu_backend.bzl", "cpu_backend", "cpu_provider")
45

56
def define_common_targets():
67
runtime.cxx_library(
78
name = "cpu_runtime",
8-
srcs = [
9-
"runtime/KernelProvider.cpp",
10-
"runtime/CPUPlan.cpp",
11-
"runtime/op_helpers/Convolution.cpp",
12-
],
9+
srcs = CPU_RUNTIME_BUCK_SRCS,
1310
exported_headers = [
1411
"runtime/KernelProvider.h",
1512
"runtime/CPUPlan.h",

‎backends/native/CMakeLists.txt‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
1111
endif()
1212

1313
find_package(Threads REQUIRED)
14-
find_package(libzip CONFIG REQUIRED)
15-
if(NOT TARGET nlohmann_json::nlohmann_json)
16-
find_package(nlohmann_json CONFIG REQUIRED)
14+
15+
# A CPU backend build without EXECUTORCH_BUILD_NATIVE only needs the graph and
16+
# the program reader, which have no third-party dependencies.
17+
if(EXECUTORCH_BUILD_NATIVE)
18+
find_package(libzip CONFIG REQUIRED)
19+
if(NOT TARGET nlohmann_json::nlohmann_json)
20+
find_package(nlohmann_json CONFIG REQUIRED)
21+
endif()
1722
endif()
1823

1924
add_subdirectory(runtime)
@@ -45,6 +50,6 @@ if(EXECUTORCH_BUILD_NATIVE_VISION_RUNNER OR EXECUTORCH_BUILD_NATIVE_LLM_RUNNER)
4550
add_subdirectory(examples)
4651
endif()
4752

48-
if(EXECUTORCH_BUILD_TESTS)
53+
if(EXECUTORCH_BUILD_NATIVE AND EXECUTORCH_BUILD_TESTS)
4954
add_subdirectory(test)
5055
endif()

‎backends/native/runtime/CMakeLists.txt‎

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,58 @@ add_library(
4242
graph/utils/GraphUtils.cpp
4343
)
4444

45-
add_library(native_reference_gguf_kquant utils/ReferenceGgufKQuant.cpp)
46-
target_link_libraries(native_reference_gguf_kquant PRIVATE Threads::Threads)
47-
48-
add_library(
49-
native_package
50-
deserialize/OwnedBytes.cpp deserialize/Package.cpp
51-
deserialize/SafeTensorsReader.cpp deserialize/Samples.cpp
52-
deserialize/ZipReader.cpp
53-
)
45+
add_library(native_program Deserialize.cpp Program.cpp)
5446
target_link_libraries(
55-
native_package
47+
native_program
5648
PUBLIC native_graph
57-
PRIVATE libzip::zip nlohmann_json::nlohmann_json Threads::Threads
58-
)
59-
60-
add_library(
61-
native_runtime Deserialize.cpp Program.cpp MethodMeta.cpp TensorInfo.cpp
62-
Validation.cpp
63-
)
64-
target_link_libraries(
65-
native_runtime
66-
PUBLIC native_graph native_package
6749
PRIVATE $<BUILD_INTERFACE:native_graph_schema> Threads::Threads
6850
)
69-
add_dependencies(native_runtime native_graph_schema_gen)
51+
add_dependencies(native_program native_graph_schema_gen)
7052

71-
add_library(native_engine engine/Engine.cpp)
72-
target_link_libraries(native_engine PUBLIC native_runtime)
53+
set(_native_runtime_targets native_graph native_program)
7354

74-
add_library(native_api api/Model.cpp)
75-
target_link_libraries(native_api PUBLIC native_engine)
55+
if(EXECUTORCH_BUILD_NATIVE)
56+
add_library(native_reference_gguf_kquant utils/ReferenceGgufKQuant.cpp)
57+
target_link_libraries(native_reference_gguf_kquant PRIVATE Threads::Threads)
7658

77-
foreach(target native_graph native_reference_gguf_kquant native_package
78-
native_runtime native_engine native_api
79-
)
59+
add_library(
60+
native_package
61+
deserialize/OwnedBytes.cpp deserialize/Package.cpp
62+
deserialize/SafeTensorsReader.cpp deserialize/Samples.cpp
63+
deserialize/ZipReader.cpp
64+
)
65+
target_link_libraries(
66+
native_package
67+
PUBLIC native_graph
68+
PRIVATE libzip::zip nlohmann_json::nlohmann_json Threads::Threads
69+
)
70+
71+
add_library(native_runtime MethodMeta.cpp TensorInfo.cpp Validation.cpp)
72+
target_link_libraries(
73+
native_runtime
74+
PUBLIC native_program native_package
75+
PRIVATE $<BUILD_INTERFACE:native_graph_schema> Threads::Threads
76+
)
77+
add_dependencies(native_runtime native_graph_schema_gen)
78+
79+
add_library(native_engine engine/Engine.cpp)
80+
target_link_libraries(native_engine PUBLIC native_runtime)
81+
82+
add_library(native_api api/Model.cpp)
83+
target_link_libraries(native_api PUBLIC native_engine)
84+
85+
list(
86+
APPEND
87+
_native_runtime_targets
88+
native_reference_gguf_kquant
89+
native_package
90+
native_runtime
91+
native_engine
92+
native_api
93+
)
94+
endif()
95+
96+
foreach(target ${_native_runtime_targets})
8097
add_library(executorch::${target} ALIAS ${target})
8198
target_compile_features(${target} PUBLIC cxx_std_20)
8299
target_compile_options(
@@ -90,8 +107,7 @@ foreach(target native_graph native_reference_gguf_kquant native_package
90107
endforeach()
91108

92109
install(
93-
TARGETS native_graph native_reference_gguf_kquant native_package
94-
native_runtime native_engine native_api
110+
TARGETS ${_native_runtime_targets}
95111
EXPORT ExecuTorchTargets
96112
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
97113
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

‎backends/native/runtime/cpu/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
if(NOT TARGET cpu_runtime)
7+
if(NOT EXECUTORCH_BUILD_CPU AND NOT TARGET cpu_runtime)
88
add_subdirectory(
99
${EXECUTORCH_ROOT}/backends/cpu ${CMAKE_CURRENT_BINARY_DIR}/cpu_runtime
1010
)

0 commit comments

Comments
 (0)