Skip to content

Commit 907e97e

Browse files
authored
Build optimized_portable_kernels if threadpool is enabled (#8987)
This is step (5) of #8932. At this exact moment, this rebuild is inefficient because it rebuilds the whole portable op library, but ops don't support optional parallelization just yet. This will become less true when we roll out parallel_for support across portable ops immediately following this PR.
1 parent acae017 commit 907e97e

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

build/cmake_deps.toml

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ deps = [
8989
"executorch",
9090
"executorch_core",
9191
"extension_threadpool",
92+
"optimized_cpublas",
9293
"portable_kernels",
9394
]
9495

@@ -146,6 +147,7 @@ deps = [
146147
"executorch_core",
147148
"executorch",
148149
"extension_threadpool",
150+
"optimized_cpublas",
149151
"portable_kernels",
150152
]
151153
# ---------------------------------- core end ----------------------------------
@@ -413,6 +415,7 @@ excludes = [
413415
deps = [
414416
"executorch",
415417
"executorch_core",
418+
"optimized_cpublas",
416419
"optimized_kernels",
417420
"extension_threadpool",
418421
"reduce_util",
@@ -452,6 +455,7 @@ deps = [
452455
"extension_data_loader",
453456
"extension_module",
454457
"extension_threadpool",
458+
"optimized_cpublas",
455459
"portable_kernels",
456460
"quantized_kernels",
457461
"xnnpack_backend",

build/executorch-config.cmake

+21-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ set(lib_list
8989
pthreadpool
9090
vulkan_backend
9191
optimized_kernels
92+
optimized_portable_kernels
9293
cpublas
9394
eigen_blas
9495
optimized_ops_lib
@@ -132,7 +133,26 @@ endforeach()
132133
# target_compile_options/target_compile_definitions for everything.
133134
if(TARGET cpublas)
134135
set_target_properties(
135-
cpublas PROPERTIES INTERFACE_LINK_LIBRARIES extension_threadpool
136+
cpublas PROPERTIES INTERFACE_LINK_LIBRARIES
137+
"extension_threadpool;eigen_blas"
138+
)
139+
endif()
140+
if(TARGET optimized_kernels)
141+
set_target_properties(
142+
optimized_kernels PROPERTIES INTERFACE_LINK_LIBRARIES
143+
"executorch_core;cpublas;extension_threadpool"
144+
)
145+
endif()
146+
if(TARGET optimized_native_cpu_ops_lib)
147+
if(TARGET optimized_portable_kernels)
148+
set(_maybe_optimized_portable_kernels_lib optimized_portable_kernels)
149+
else()
150+
set(_maybe_optimized_portable_kernels_lib portable_kernels)
151+
endif()
152+
set_target_properties(
153+
optimized_native_cpu_ops_lib
154+
PROPERTIES INTERFACE_LINK_LIBRARIES
155+
"optimized_kernels;${_maybe_optimized_portable_kernels_lib}"
136156
)
137157
endif()
138158
if(TARGET extension_threadpool)

configurations/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
4747
message("Generated files ${gen_command_sources}")
4848

4949
# optimized_native_cpu_ops_lib: Register optimized op kernels into the runtime
50+
if(TARGET optimized_portable_kernels)
51+
set(_optimized_native_cpu_ops_lib_portable_kernels_lib optimized_portable_kernels)
52+
else()
53+
set(_optimized_native_cpu_ops_lib_portable_kernels_lib portable_kernels)
54+
endif()
5055
gen_operators_lib(
5156
LIB_NAME
5257
"optimized_native_cpu_ops_lib"
5358
KERNEL_LIBS
54-
portable_kernels
5559
optimized_kernels
60+
${_optimized_native_cpu_ops_lib_portable_kernels_lib}
5661
DEPS
5762
executorch
5863
)

extension/android/CMakeLists.txt

-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ if(TARGET optimized_native_cpu_ops_lib)
8484
APPEND
8585
link_libraries
8686
optimized_native_cpu_ops_lib
87-
optimized_kernels
88-
portable_kernels
89-
cpublas
90-
eigen_blas
9187
)
9288
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
9389
else()

kernels/portable/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ gen_operators_lib(
6363
LIB_NAME "portable_ops_lib" KERNEL_LIBS portable_kernels DEPS executorch
6464
)
6565

66+
# Portable kernels support optional parallelization (and, in the
67+
# future, perhaps other performance features). If support is present,
68+
# produce an optimized version.
69+
set(BUILD_OPTIMIZED_PORTABLE_KERNELS EXECUTORCH_BUILD_PTHREADPOOL)
70+
71+
if(BUILD_OPTIMIZED_PORTABLE_KERNELS)
72+
add_library(optimized_portable_kernels ${_portable_kernels__srcs})
73+
target_link_libraries(optimized_portable_kernels PRIVATE executorch)
74+
target_link_libraries(optimized_portable_kernels PUBLIC extension_threadpool)
75+
target_compile_options(optimized_portable_kernels PUBLIC ${_common_compile_options})
76+
install(
77+
TARGETS optimized_portable_kernels
78+
DESTINATION lib
79+
)
80+
endif()
81+
6682
install(
6783
TARGETS portable_kernels portable_ops_lib
6884
DESTINATION lib

kernels/test/CMakeLists.txt

+25-11
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ foreach(kernel ${_kernels})
2323
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/test"
2424
)
2525
set(_wrapper_path "${_wrapper_dir}/FunctionHeaderWrapper.h")
26+
set(_functions_include "#include <executorch/kernels/${kernel}/Functions.h>")
2627
add_custom_command(
2728
OUTPUT "${_wrapper_path}"
2829
COMMAND mkdir -p ${_wrapper_dir}
29-
COMMAND echo "#include <executorch/kernels/${kernel}/Functions.h>" >
30-
"${_wrapper_path}"
30+
COMMAND echo ${_functions_include} > "${_wrapper_path}"
3131
DEPENDS
3232
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/${kernel}/Functions.h"
3333
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/${kernel}/NativeFunctions.h"
@@ -53,7 +53,17 @@ foreach(kernel ${_kernels})
5353
COMMENT "Generating ${_wrapper_dir}/supported_features.cpp and header"
5454
VERBATIM
5555
)
56-
56+
if(${kernel} STREQUAL "optimized")
57+
set(_kernel_ops_lib "optimized_native_cpu_ops_lib")
58+
set(_kernel_ops_lib_path
59+
"${CMAKE_CURRENT_BINARY_DIR}/../../configurations/optimized_native_cpu_ops_lib"
60+
)
61+
else()
62+
set(_kernel_ops_lib "${kernel}_ops_lib")
63+
set(_kernel_ops_lib_path
64+
"${CMAKE_CURRENT_BINARY_DIR}/../../kernels/${kernel}/${kernel}_ops_lib"
65+
)
66+
endif()
5767
add_custom_command(
5868
OUTPUT
5969
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/${kernel}/Functions.h"
@@ -63,10 +73,9 @@ foreach(kernel ${_kernels})
6373
mkdir -p
6474
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/${kernel}/"
6575
COMMAND
66-
cp
67-
"${CMAKE_CURRENT_BINARY_DIR}/../../kernels/${kernel}/${kernel}_ops_lib/*.h"
76+
cp "${_kernel_ops_lib_path}/*.h"
6877
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/${kernel}/"
69-
DEPENDS "${kernel}_ops_lib"
78+
DEPENDS ${_kernel_ops_lib}
7079
)
7180
endforeach()
7281

@@ -280,23 +289,28 @@ set(_optimized_kernels_test_sources
280289
${CMAKE_CURRENT_BINARY_DIR}/include/optimized/executorch/kernels/test/supported_features.cpp
281290
)
282291

292+
if(TARGET optimized_portable_kernels)
293+
list(APPEND _optimized_kernels_test_sources ${all_test_sources})
294+
list(REMOVE_DUPLICATES _optimized_kernels_test_sources)
295+
endif()
296+
283297
et_cxx_test(
284298
optimized_kernels_test
285299
SOURCES
286300
${_optimized_kernels_test_sources}
287301
EXTRA_LIBS
288302
cpuinfo
289303
extension_threadpool
290-
optimized_kernels
291-
optimized_ops_lib
292-
portable_kernels
304+
optimized_native_cpu_ops_lib
293305
pthreadpool
294306
eigen_blas
295307
)
296308
add_dependencies(optimized_kernels_test generate_wrapper)
297309
target_include_directories(
298-
optimized_kernels_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include/optimized"
299-
"${CMAKE_INSTALL_PREFIX}/include"
310+
optimized_kernels_test
311+
PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include/optimized"
312+
"${CMAKE_CURRENT_BINARY_DIR}/include/portable"
313+
"${CMAKE_INSTALL_PREFIX}/include"
300314
)
301315

302316
if(TARGET quantized_kernels)

0 commit comments

Comments
 (0)