Skip to content

Commit 5d1143f

Browse files
committed
Add top-level CMake kernels target
ghstack-source-id: 934dff9 ghstack-comment-id: 3111912326 Pull-Request: #12806
1 parent cb75826 commit 5d1143f

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

CMakeLists.txt

Lines changed: 62 additions & 1 deletion
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)
@@ -778,11 +783,13 @@ endif()
778783
if(EXECUTORCH_BUILD_KERNELS_LLM)
779784
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
780785
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops)
786+
list(APPEND _executorch_kernels custom_ops_aot_lib)
781787
endif()
782788

783789
if(EXECUTORCH_BUILD_KERNELS_QUANTIZED)
784790
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized)
785791
executorch_target_link_options_shared_lib(quantized_ops_lib)
792+
list(APPEND _executorch_kernels quantized_ops_lib)
786793
endif()
787794

788795
if(EXECUTORCH_BUILD_VULKAN)
@@ -809,8 +816,62 @@ add_library(executorch_extensions INTERFACE)
809816
add_library(executorch::extensions ALIAS executorch_extensions)
810817
target_link_libraries(executorch_extensions INTERFACE ${_executorch_extensions})
811818

819+
# A target containing all configured kernels, with selective build, if enabled.
820+
add_library(executorch_kernels INTERFACE)
821+
add_library(executorch::kernels ALIAS executorch_kernels)
822+
if(NOT EXECUTORCH_SELECT_OPS_YAML STREQUAL ""
823+
OR NOT EXECUTORCH_SELECT_OPS_LIST STREQUAL ""
824+
OR NOT EXECUTORCH_SELECT_OPS_MODEL STREQUAL ""
825+
)
826+
gen_selected_ops(
827+
LIB_NAME
828+
"executorch_selected_kernels"
829+
OPS_SCHEMA_YAML
830+
"${EXECUTORCH_SELECT_OPS_LIB}"
831+
ROOT_OPS
832+
"${EXECUTORCH_SELECT_OPS_LIST}"
833+
INCLUDE_ALL_OPS
834+
FALSE
835+
OPS_FROM_MODEL
836+
"${EXECUTORCH_SELECT_OPS_MODEL}"
837+
DTYPE_SELECTIVE_BUILD
838+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
839+
)
840+
841+
generate_bindings_for_kernels(
842+
LIB_NAME
843+
"executorch_selected_kernels"
844+
FUNCTIONS_YAML
845+
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
846+
CUSTOM_OPS_YAML
847+
""
848+
DTYPE_SELECTIVE_BUILD
849+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
850+
)
851+
852+
gen_operators_lib(
853+
LIB_NAME
854+
"executorch_selected_kernels"
855+
KERNEL_LIBS
856+
"portable_kernels"
857+
DEPS
858+
executorch_core
859+
DTYPE_SELECTIVE_BUILD
860+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
861+
)
862+
list(APPEND _executorch_kernels executorch_selected_kernels)
863+
else()
864+
# No selective build - link the full library.
865+
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
866+
list(APPEND _executorch_kernels optimized_native_cpu_ops_lib)
867+
else()
868+
list(APPEND _executorch_kernels portable_ops_lib)
869+
endif()
870+
endif()
871+
target_link_libraries(executorch_kernels INTERFACE ${_executorch_kernels})
872+
812873
install(
813-
TARGETS executorch_backends executorch_extensions
874+
TARGETS executorch_backends executorch_extensions executorch_kernels
814875
INCLUDES
815876
DESTINATION ${_common_include_directories}
816877
)

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)