Skip to content

Add top-level CMake kernels target #12806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
# Instead please use `find_package(executorch REQUIRED)` in the example
# directory and add a new executable in the example `CMakeLists.txt`.

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

if(NOT EXECUTORCH_ENABLE_LOGGING)
# Avoid pulling in the logging strings, which can be large. Note that this
# will set the compiler flag for all targets in this directory, and for all
Expand Down Expand Up @@ -386,6 +388,9 @@ set(_executorch_backends "")
# A list of all configured extensions.
set(_executorch_extensions "")

# A list of all configured kernel libraries.
set(_executorch_kernels "")

target_link_libraries(executorch_core PRIVATE program_schema)
if(ANDROID)
target_link_libraries(executorch_core PUBLIC log)
Expand Down Expand Up @@ -774,11 +779,13 @@ endif()
if(EXECUTORCH_BUILD_KERNELS_LLM)
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops)
list(APPEND _executorch_kernels custom_ops_aot_lib)
endif()

if(EXECUTORCH_BUILD_KERNELS_QUANTIZED)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized)
executorch_target_link_options_shared_lib(quantized_ops_lib)
list(APPEND _executorch_kernels quantized_ops_lib)
endif()

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

# A target containing all configured kernels, with selective build, if enabled.
add_library(executorch_kernels INTERFACE)
add_library(executorch::kernels ALIAS executorch_kernels)
if(NOT EXECUTORCH_SELECT_OPS_YAML STREQUAL ""
OR NOT EXECUTORCH_SELECT_OPS_LIST STREQUAL ""
OR NOT EXECUTORCH_SELECT_OPS_MODEL STREQUAL ""
)
Comment on lines +816 to +819
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this!

gen_selected_ops(
LIB_NAME
"executorch_selected_kernels"
OPS_SCHEMA_YAML
"${EXECUTORCH_SELECT_OPS_LIB}"
ROOT_OPS
"${EXECUTORCH_SELECT_OPS_LIST}"
INCLUDE_ALL_OPS
FALSE
OPS_FROM_MODEL
"${EXECUTORCH_SELECT_OPS_MODEL}"
DTYPE_SELECTIVE_BUILD
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
)

generate_bindings_for_kernels(
LIB_NAME
"executorch_selected_kernels"
FUNCTIONS_YAML
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
CUSTOM_OPS_YAML
""
DTYPE_SELECTIVE_BUILD
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
)

gen_operators_lib(
LIB_NAME
"executorch_selected_kernels"
KERNEL_LIBS
"portable_kernels"
Copy link
Contributor

@lucylq lucylq Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this to top-level cmake!

So, executorch_selected_kernels is a selective build library for portable_kernels only it seems? If a user wants to selectively build on optimized or custom kernel libs, they should either:

  1. Update here (for test purposes, I guess).
  2. Follow the current flow and create their own selective build lib.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, as a follow-up, I intend to enable optimized (and portable_optimized) kernels. To enable selective build for other libraries (quantized ops, llm ops, etc.), I should just be able to use the merge_yaml functionality to generate a combined op yaml, right?

For user's custom op libs, I think they'll probably need to define their own targets, as we can't access their targets from here (unless we re-wrote as a macro/function, but we can look at that separately).

Backend op libs are another case that needs more thinking through, as well. But hopefully this should be a good starting point.

DEPS
executorch_core
DTYPE_SELECTIVE_BUILD
"${EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD}"
)
list(APPEND _executorch_kernels executorch_selected_kernels)
else()
# No selective build - link the full library.
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
list(APPEND _executorch_kernels optimized_native_cpu_ops_lib)
else()
list(APPEND _executorch_kernels portable_ops_lib)
endif()
endif()
target_link_libraries(executorch_kernels INTERFACE ${_executorch_kernels})

if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
# Baseline libraries that executor_runner will link against.
set(_executor_runner_libs executorch extension_evalue_util
Expand Down
33 changes: 33 additions & 0 deletions tools/cmake/preset/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ define_overridable_option(
EXECUTORCH_USE_CPP_CODE_COVERAGE "Build with code coverage enabled" BOOL OFF
)

# Selective build options. These affect the executorch_kernels target.
define_overridable_option(
EXECUTORCH_SELECT_OPS_YAML
"Build the executorch_kernels target with YAML selective build config."
STRING ""
)
define_overridable_option(
EXECUTORCH_SELECT_OPS_LIST
"Build the executorch_kernels target with a list of selected operators."
STRING ""
)
define_overridable_option(
EXECUTORCH_SELECT_OPS_MODEL
"Build the executorch_kernels target with only operators from the given model .pte file."
STRING ""
)
define_overridable_option(
EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD
"Build the executorch_kernels target with only operator implementations for selected data types."
BOOL FALSE
)

# ------------------------------------------------------------------------------
# Validations
#
Expand Down Expand Up @@ -284,6 +306,17 @@ check_conflicting_options_on(
EXECUTORCH_BUILD_CPUINFO
)

# Selective build specifiers are mutually exclusive.
check_conflicting_options_on(
IF_ON EXECUTORCH_SELECT_OPS_YAML CONFLICTS_WITH
EXECUTORCH_SELECT_OPS_LIST EXECUTORCH_SELECT_OPS_MODEL
)
Comment on lines +310 to +313
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean in the future it should be composable. I'm OK with having this for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GregoryComer Should this also check for a conflict with the OPS_FROM_MODEL API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see it there, later down.


check_conflicting_options_on(
IF_ON EXECUTORCH_SELECT_OPS_LIST CONFLICTS_WITH
EXECUTORCH_SELECT_OPS_MODEL
)

if(NOT EXISTS ${EXECUTORCH_PAL_DEFAULT_FILE_PATH})
message(
FATAL_ERROR
Expand Down
Loading