Skip to content

Commit af167d6

Browse files
committed
Finally have something functional.
The entrie flow works now, from reading in a model's pte file to rebuilding the executorch binary with selected ops and dtypes in OSS. Tests on add and add_mul match expectations. MV2 and MV3 suffer from unrelated issues (i.e. the parser is unable to get all the needed info from the pte file when building the YAML).
1 parent 65dcb04 commit af167d6

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

tools/cmake/Codegen.cmake

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function(generate_bindings_for_kernels)
152152
${_out_dir}/RegisterSchema.cpp ${_out_dir}/CustomOpsNativeFunctions.h
153153
)
154154
endif()
155-
155+
156156
add_custom_command(
157157
COMMENT "Generating code for kernel registration"
158158
OUTPUT ${_gen_command_sources}
@@ -172,7 +172,6 @@ endfunction()
172172
# Generate an AOT lib for registering custom ops into PyTorch
173173
function(gen_custom_ops_aot_lib)
174174
cmake_parse_arguments(GEN "" "LIB_NAME" "KERNEL_SOURCES" ${ARGN})
175-
message("REMOVE_ME-ops_aot_lib")
176175
message(STATUS "Generating custom ops aot lib:")
177176
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
178177
foreach(SOURCE IN LISTS GEN_KERNEL_SOURCES)
@@ -205,41 +204,86 @@ function(gen_operators_lib)
205204
set(multi_arg_names LIB_NAME KERNEL_LIBS DEPS DTYPE_SELECTIVE_BUILD)
206205
cmake_parse_arguments(GEN "" "" "${multi_arg_names}" ${ARGN})
207206

208-
message("REMOVE_ME-gen_op_lib")
209207
message(STATUS "Generating operator lib:")
210208
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
211209
message(STATUS " KERNEL_LIBS: ${GEN_KERNEL_LIBS}")
212210
message(STATUS " DEPS: ${GEN_DEPS}")
213211
message(STATUS " DTYPE_SELECTIVE_BUILD: ${GEN_DTYPE_SELECTIVE_BUILD}")
214212

215213
set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME})
216-
if(DTYPE_SELECTIVE_BUILD)
214+
if(GEN_DTYPE_SELECTIVE_BUILD)
217215
set(_opvariant_h
218216
${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}/selected_op_variants.h
219217
)
220218
endif()
221219

222220
add_library(${GEN_LIB_NAME})
223-
221+
224222
set(_srcs_list
225223
${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
226224
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h
227225
)
228-
if(DTYPE_SELECTIVE_BUILD)
226+
if(GEN_DTYPE_SELECTIVE_BUILD)
229227
list(APPEND _srcs_list ${_opvariant_h})
230228
endif()
231229
target_sources(
232230
${GEN_LIB_NAME}
233231
PRIVATE ${_srcs_list}
234232
)
235233
target_link_libraries(${GEN_LIB_NAME} PRIVATE ${GEN_DEPS})
234+
set(portable_kernels_check "portable_kernels")
236235
if(GEN_KERNEL_LIBS)
237-
target_link_libraries(${GEN_LIB_NAME} PUBLIC ${GEN_KERNEL_LIBS})
236+
237+
set(_common_compile_options -Wno-deprecated-declarations -ffunction-sections -fdata-sections -Os)
238+
239+
if("${portable_kernels_check}" IN_LIST GEN_KERNEL_LIBS)
240+
list(REMOVE_ITEM GEN_KERNEL_LIBS ${portable_kernels_check})
241+
242+
# Define portable kernels sources (same as in kernels/portable/CMakeLists.txt)
243+
file(GLOB_RECURSE _portable_kernels_srcs
244+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/*.cpp"
245+
)
246+
list(FILTER _portable_kernels_srcs EXCLUDE REGEX "test/*.cpp")
247+
list(FILTER _portable_kernels_srcs EXCLUDE REGEX "codegen")
248+
249+
# Build kernels_util_all_deps, since later selected_portable_kernels depends on it
250+
list(TRANSFORM _kernels_util_all_deps__srcs PREPEND "${EXECUTORCH_ROOT}/")
251+
add_library(selected_kernels_util_all_deps ${_kernels_util_all_deps__srcs})
252+
target_link_libraries(selected_kernels_util_all_deps PRIVATE executorch_core)
253+
target_include_directories(selected_kernels_util_all_deps PUBLIC ${_common_include_directories})
254+
target_compile_definitions(selected_kernels_util_all_deps PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
255+
target_compile_options(selected_kernels_util_all_deps PUBLIC ${_common_compile_options})
256+
257+
# Build selected_portable_kernels
258+
add_library(selected_portable_kernels ${_portable_kernels_srcs})
259+
target_link_libraries(selected_portable_kernels PRIVATE executorch_core selected_kernels_util_all_deps)
260+
target_compile_options(selected_portable_kernels PUBLIC ${_common_compile_options})
261+
target_include_directories(selected_portable_kernels PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}/)
262+
263+
# Add dependency on generated header if dtype selective build is enabled
264+
if(GEN_DTYPE_SELECTIVE_BUILD)
265+
# Make sure the header is generated before compiling the library
266+
add_dependencies(selected_portable_kernels ${GEN_LIB_NAME})
267+
# Create a custom target for the header to ensure proper dependency tracking
268+
add_custom_target(selected_portable_kernels_header DEPENDS ${_opvariant_h})
269+
add_dependencies(selected_portable_kernels selected_portable_kernels_header)
270+
# Apply the compile definition for dtype selective build
271+
target_compile_definitions(selected_portable_kernels PRIVATE EXECUTORCH_SELECTIVE_BUILD_DTYPE=1)
272+
endif()
273+
274+
target_link_libraries(${GEN_LIB_NAME} PUBLIC selected_portable_kernels)
275+
endif()
276+
277+
# portable_kernel is no longer in GEN_KERNEL_LIBS, but there may be others
278+
if(GEN_KERNEL_LIBS)
279+
280+
target_link_libraries(${GEN_LIB_NAME} PUBLIC ${GEN_KERNEL_LIBS})
281+
endif()
238282
endif()
239283

240284
target_link_options_shared_lib(${GEN_LIB_NAME})
241285
set(_generated_headers ${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h)
242-
if(DTYPE_SELECTIVE_BUILD)
286+
if(GEN_DTYPE_SELECTIVE_BUILD)
243287
list(APPEND _generated_headers ${_opvariant_h})
244288
endif()
245289
set_target_properties(

0 commit comments

Comments
 (0)