Skip to content

Commit a70b7a8

Browse files
authored
Add top-level CMake kernels target (#12806)
Add a top level kernels CMake target, which includes all configured kernel libraries. I validate this change by building a simple runner using the executorch_kernels target and verifying that it was able to build and run MobileNet v2, with and without selective build. Once the changes land in ExecuTorch, this will be long-term validated in executorch-examples CI. There are a few follow-ups needed, which I will handle shortly. I still need to add merging logic for the YAML files to generate a single selective library that includes quantized and LLM ops, when configured to build them. This is done in the context of top-level CMake targets, tracked in #12293.
1 parent fead7c7 commit a70b7a8

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

CMakeLists.txt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
123123
# Instead please use `find_package(executorch REQUIRED)` in the example
124124
# directory and add a new executable in the example `CMakeLists.txt`.
125125

126+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
127+
126128
if(NOT EXECUTORCH_ENABLE_LOGGING)
127129
# Avoid pulling in the logging strings, which can be large. Note that this
128130
# will set the compiler flag for all targets in this directory, and for all
@@ -386,6 +388,9 @@ set(_executorch_backends "")
386388
# A list of all configured extensions.
387389
set(_executorch_extensions "")
388390

391+
# A list of all configured kernel libraries.
392+
set(_executorch_kernels "")
393+
389394
target_link_libraries(executorch_core PRIVATE program_schema)
390395
if(ANDROID)
391396
target_link_libraries(executorch_core PUBLIC log)
@@ -774,11 +779,13 @@ endif()
774779
if(EXECUTORCH_BUILD_KERNELS_LLM)
775780
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
776781
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops)
782+
list(APPEND _executorch_kernels custom_ops_aot_lib)
777783
endif()
778784

779785
if(EXECUTORCH_BUILD_KERNELS_QUANTIZED)
780786
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized)
781787
executorch_target_link_options_shared_lib(quantized_ops_lib)
788+
list(APPEND _executorch_kernels quantized_ops_lib)
782789
endif()
783790

784791
if(EXECUTORCH_BUILD_VULKAN)
@@ -803,6 +810,60 @@ add_library(executorch_extensions INTERFACE)
803810
add_library(executorch::extensions ALIAS executorch_extensions)
804811
target_link_libraries(executorch_extensions INTERFACE ${_executorch_extensions})
805812

813+
# A target containing all configured kernels, with selective build, if enabled.
814+
add_library(executorch_kernels INTERFACE)
815+
add_library(executorch::kernels ALIAS executorch_kernels)
816+
if(NOT EXECUTORCH_SELECT_OPS_YAML STREQUAL ""
817+
OR NOT EXECUTORCH_SELECT_OPS_LIST STREQUAL ""
818+
OR NOT EXECUTORCH_SELECT_OPS_MODEL STREQUAL ""
819+
)
820+
gen_selected_ops(
821+
LIB_NAME
822+
"executorch_selected_kernels"
823+
OPS_SCHEMA_YAML
824+
"${EXECUTORCH_SELECT_OPS_LIB}"
825+
ROOT_OPS
826+
"${EXECUTORCH_SELECT_OPS_LIST}"
827+
INCLUDE_ALL_OPS
828+
FALSE
829+
OPS_FROM_MODEL
830+
"${EXECUTORCH_SELECT_OPS_MODEL}"
831+
DTYPE_SELECTIVE_BUILD
832+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
833+
)
834+
835+
generate_bindings_for_kernels(
836+
LIB_NAME
837+
"executorch_selected_kernels"
838+
FUNCTIONS_YAML
839+
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
840+
CUSTOM_OPS_YAML
841+
""
842+
DTYPE_SELECTIVE_BUILD
843+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
844+
)
845+
846+
gen_operators_lib(
847+
LIB_NAME
848+
"executorch_selected_kernels"
849+
KERNEL_LIBS
850+
"portable_kernels"
851+
DEPS
852+
executorch_core
853+
DTYPE_SELECTIVE_BUILD
854+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
855+
)
856+
list(APPEND _executorch_kernels executorch_selected_kernels)
857+
else()
858+
# No selective build - link the full library.
859+
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
860+
list(APPEND _executorch_kernels optimized_native_cpu_ops_lib)
861+
else()
862+
list(APPEND _executorch_kernels portable_ops_lib)
863+
endif()
864+
endif()
865+
target_link_libraries(executorch_kernels INTERFACE ${_executorch_kernels})
866+
806867
if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
807868
# Baseline libraries that executor_runner will link against.
808869
set(_executor_runner_libs executorch extension_evalue_util

tools/cmake/preset/default.cmake

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ define_overridable_option(
214214
EXECUTORCH_USE_CPP_CODE_COVERAGE "Build with code coverage enabled" BOOL OFF
215215
)
216216

217+
# Selective build options. These affect the executorch_kernels target.
218+
define_overridable_option(
219+
EXECUTORCH_SELECT_OPS_YAML
220+
"Build the executorch_kernels target with YAML selective build config."
221+
STRING ""
222+
)
223+
define_overridable_option(
224+
EXECUTORCH_SELECT_OPS_LIST
225+
"Build the executorch_kernels target with a list of selected operators."
226+
STRING ""
227+
)
228+
define_overridable_option(
229+
EXECUTORCH_SELECT_OPS_MODEL
230+
"Build the executorch_kernels target with only operators from the given model .pte file."
231+
STRING ""
232+
)
233+
define_overridable_option(
234+
EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD
235+
"Build the executorch_kernels target with only operator implementations for selected data types."
236+
BOOL FALSE
237+
)
238+
217239
# ------------------------------------------------------------------------------
218240
# Validations
219241
#
@@ -284,6 +306,17 @@ check_conflicting_options_on(
284306
EXECUTORCH_BUILD_CPUINFO
285307
)
286308

309+
# Selective build specifiers are mutually exclusive.
310+
check_conflicting_options_on(
311+
IF_ON EXECUTORCH_SELECT_OPS_YAML CONFLICTS_WITH
312+
EXECUTORCH_SELECT_OPS_LIST EXECUTORCH_SELECT_OPS_MODEL
313+
)
314+
315+
check_conflicting_options_on(
316+
IF_ON EXECUTORCH_SELECT_OPS_LIST CONFLICTS_WITH
317+
EXECUTORCH_SELECT_OPS_MODEL
318+
)
319+
287320
if(NOT EXISTS ${EXECUTORCH_PAL_DEFAULT_FILE_PATH})
288321
message(
289322
FATAL_ERROR

0 commit comments

Comments
 (0)