Skip to content

Commit ce77eaf

Browse files
committed
Add top-level CMake kernels target
ghstack-source-id: e7343f2 ghstack-comment-id: 3111912326 Pull-Request: #12806
1 parent 7dc56a2 commit ce77eaf

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

CMakeLists.txt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ set(_executorch_backends "")
357357
# A list of all configured extensions.
358358
set(_executorch_extensions "")
359359

360+
# A list of all configured kernel libraries.
361+
set(_executorch_kernels "")
362+
360363
target_link_libraries(executorch_core PRIVATE program_schema)
361364
if(ANDROID)
362365
target_link_libraries(executorch_core PUBLIC log)
@@ -706,11 +709,13 @@ endif()
706709
if(EXECUTORCH_BUILD_KERNELS_LLM)
707710
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
708711
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops)
712+
list(APPEND _executorch_kernels custom_ops_aot_lib)
709713
endif()
710714

711715
if(EXECUTORCH_BUILD_KERNELS_QUANTIZED)
712716
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized)
713717
target_link_options_shared_lib(quantized_ops_lib)
718+
list(APPEND _executorch_kernels quantized_ops_lib)
714719
endif()
715720

716721
if(EXECUTORCH_BUILD_VULKAN)
@@ -728,8 +733,61 @@ target_link_libraries(executorch_backends INTERFACE ${_executorch_backends})
728733
add_library(executorch_extensions INTERFACE)
729734
target_link_libraries(executorch_extensions INTERFACE ${_executorch_extensions})
730735

736+
# A target containing all configured kernels, with selective build, if enabled.
737+
add_library(executorch_kernels INTERFACE)
738+
if(NOT EXECUTORCH_SELECT_OPS_YAML STREQUAL ""
739+
OR NOT EXECUTORCH_SELECT_OPS_LIST STREQUAL ""
740+
OR NOT EXECUTORCH_SELECT_OPS_MODEL STREQUAL ""
741+
)
742+
gen_selected_ops(
743+
LIB_NAME
744+
"executorch_selected_kernels"
745+
OPS_SCHEMA_YAML
746+
"${EXECUTORCH_SELECT_OPS_LIB}"
747+
ROOT_OPS
748+
"${EXECUTORCH_SELECT_OPS_LIST}"
749+
INCLUDE_ALL_OPS
750+
FALSE
751+
OPS_FROM_MODEL
752+
"${EXECUTORCH_SELECT_OPS_MODEL}"
753+
DTYPE_SELECTIVE_BUILD
754+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
755+
)
756+
757+
generate_bindings_for_kernels(
758+
LIB_NAME
759+
"executorch_selected_kernels"
760+
FUNCTIONS_YAML
761+
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
762+
CUSTOM_OPS_YAML
763+
""
764+
DTYPE_SELECTIVE_BUILD
765+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
766+
)
767+
768+
gen_operators_lib(
769+
LIB_NAME
770+
"executorch_selected_kernels"
771+
KERNEL_LIBS
772+
"portable_kernels"
773+
DEPS
774+
executorch_core
775+
DTYPE_SELECTIVE_BUILD
776+
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
777+
)
778+
list(APPEND _executorch_kernels executorch_selected_kernels)
779+
else()
780+
# No selective build - link the full library.
781+
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
782+
list(APPEND _executorch_kernels optimized_native_cpu_ops_lib)
783+
else()
784+
list(APPEND _executorch_kernels portable_ops_lib)
785+
endif()
786+
endif()
787+
target_link_libraries(executorch_kernels INTERFACE ${_executorch_kernels})
788+
731789
install(
732-
TARGETS executorch_backends executorch_extensions
790+
TARGETS executorch_backends executorch_extensions executorch_kernels
733791
INCLUDES
734792
DESTINATION ${_common_include_directories}
735793
)

tools/cmake/preset/default.cmake

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,28 @@ define_overridable_option(
203203
EXECUTORCH_USE_CPP_CODE_COVERAGE "Build with code coverage enabled" BOOL OFF
204204
)
205205

206+
# Selective build options. These affect the executorch_kernels target.
207+
define_overridable_option(
208+
EXECUTORCH_SELECT_OPS_YAML
209+
"Build the executorch_kernels target with YAML selective build config."
210+
STRING ""
211+
)
212+
define_overridable_option(
213+
EXECUTORCH_SELECT_OPS_LIST
214+
"Build the executorch_kernels target with a list of selected operators."
215+
STRING ""
216+
)
217+
define_overridable_option(
218+
EXECUTORCH_SELECT_OPS_MODEL
219+
"Build the executorch_kernels target with only operators from the given model .pte file."
220+
STRING ""
221+
)
222+
define_overridable_option(
223+
EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD
224+
"Build the executorch_kernels target with only operator implementations for selected data types."
225+
BOOL FALSE
226+
)
227+
206228
# ------------------------------------------------------------------------------
207229
# Validations
208230
#
@@ -253,6 +275,17 @@ check_conflicting_options_on(
253275
EXECUTORCH_BUILD_CPUINFO
254276
)
255277

278+
# Selective build specifiers are mutually exclusive.
279+
check_conflicting_options_on(
280+
IF_ON EXECUTORCH_SELECT_OPS_YAML CONFLICTS_WITH
281+
EXECUTORCH_SELECT_OPS_LIST EXECUTORCH_SELECT_OPS_MODEL
282+
)
283+
284+
check_conflicting_options_on(
285+
IF_ON EXECUTORCH_SELECT_OPS_LIST CONFLICTS_WITH
286+
EXECUTORCH_SELECT_OPS_MODEL
287+
)
288+
256289
if(NOT EXISTS ${EXECUTORCH_PAL_DEFAULT_FILE_PATH})
257290
message(
258291
FATAL_ERROR

0 commit comments

Comments
 (0)