Skip to content

Commit 83692db

Browse files
fix selective build validation and dtype build plumbing
1 parent a49171d commit 83692db

4 files changed

Lines changed: 132 additions & 14 deletions

File tree

examples/selective_build/test_selective_build.sh

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ test_cmake_select_ops_in_model() {
150150
# the .pte via gen_selected_max_kernel_num().
151151
retry cmake -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
152152
-DEXECUTORCH_SELECT_OPS_MODEL="./${model_export_name}" \
153-
-DEXECUTORCH_DTYPE_SELECTIVE_BUILD=ON \
153+
-DEXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD=ON \
154154
-DEXECUTORCH_OPTIMIZE_SIZE=ON \
155155
-DCMAKE_INSTALL_PREFIX=cmake-out \
156156
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
@@ -171,6 +171,58 @@ test_cmake_select_ops_in_model() {
171171
|| { echo "ERROR: header missing expected define"; exit 1; }
172172
echo "Generated: $(cat ${generated_header} | tail -1)"
173173

174+
echo "Verifying dtype-selective variant header was generated"
175+
local generated_variant_header
176+
generated_variant_header=$(find "${build_dir}" -name selected_op_variants.h -print -quit)
177+
if [[ -z "${generated_variant_header}" ]]; then
178+
echo "ERROR: selected_op_variants.h not generated"
179+
exit 1
180+
fi
181+
182+
echo 'Running selective build test'
183+
${build_dir}/selective_build_test --model_path="./${model_export_name}"
184+
185+
echo "Removing ${model_export_name}"
186+
rm "./${model_export_name}"
187+
}
188+
189+
test_cmake_select_ops_in_model_and_list() {
190+
local model_name="add_mul"
191+
local model_export_name="${model_name}.pte"
192+
echo "Exporting ${model_name}"
193+
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="${model_name}"
194+
local example_dir=examples/selective_build/basic
195+
local build_dir=cmake-out/${example_dir}_combined
196+
rm -rf ${build_dir}
197+
retry cmake -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
198+
-DEXECUTORCH_SELECT_OPS_MODEL="./${model_export_name}" \
199+
-DEXECUTORCH_SELECT_OPS_LIST="aten::relu.out" \
200+
-DCMAKE_INSTALL_PREFIX=cmake-out \
201+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
202+
-B${build_dir} \
203+
${example_dir}
204+
205+
echo "Building ${example_dir} with model and list selectors"
206+
cmake --build ${build_dir} -j9 --config $CMAKE_BUILD_TYPE
207+
208+
echo "Verifying merged selected_operators.yaml contains both selector inputs"
209+
local selected_operators_yaml=${build_dir}/executorch/executorch_selected_kernels/selected_operators.yaml
210+
if [[ ! -f "${selected_operators_yaml}" ]]; then
211+
echo "ERROR: selected_operators.yaml not generated"
212+
exit 1
213+
fi
214+
${PYTHON_EXECUTABLE} - <<PY
215+
import yaml
216+
217+
with open("${selected_operators_yaml}") as f:
218+
ops = set(yaml.safe_load(f)["operators"].keys())
219+
220+
expected = {"aten::add.out", "aten::mm.out", "aten::relu.out"}
221+
missing = expected - ops
222+
if missing:
223+
raise RuntimeError(f"Missing merged ops: {sorted(missing)}")
224+
PY
225+
174226
echo 'Running selective build test'
175227
${build_dir}/selective_build_test --model_path="./${model_export_name}"
176228

@@ -199,6 +251,7 @@ then
199251
test_cmake_select_ops_in_list
200252
test_cmake_select_ops_in_yaml
201253
test_cmake_select_ops_in_model
254+
test_cmake_select_ops_in_model_and_list
202255
elif [[ $1 == "buck2" ]];
203256
then
204257
test_buck2_select_all_ops

tools/cmake/Codegen.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ function(gen_operators_lib)
355355
selected_portable_kernels PRIVATE EXECUTORCH_SELECTIVE_BUILD_DTYPE=1
356356
)
357357

358+
install(
359+
TARGETS selected_kernels_util_all_deps selected_portable_kernels
360+
EXPORT ExecuTorchTargets
361+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
362+
)
363+
358364
target_link_libraries(${GEN_LIB_NAME} PUBLIC selected_portable_kernels)
359365
else()
360366
message(

tools/cmake/common/preset.cmake

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,75 @@ macro(load_build_preset)
117117
endmacro()
118118

119119
# Check if the required options are set.
120+
function(is_option_enabled NAME OUT_VAR)
121+
if(NOT DEFINED ${NAME} AND NOT DEFINED CACHE{${NAME}})
122+
set(${OUT_VAR}
123+
FALSE
124+
PARENT_SCOPE
125+
)
126+
return()
127+
endif()
128+
129+
get_property(
130+
_option_type_set
131+
CACHE ${NAME}
132+
PROPERTY TYPE
133+
SET
134+
)
135+
if(_option_type_set)
136+
get_property(
137+
_option_type
138+
CACHE ${NAME}
139+
PROPERTY TYPE
140+
)
141+
if("${_option_type}" STREQUAL "BOOL")
142+
if(${NAME})
143+
set(${OUT_VAR}
144+
TRUE
145+
PARENT_SCOPE
146+
)
147+
else()
148+
set(${OUT_VAR}
149+
FALSE
150+
PARENT_SCOPE
151+
)
152+
endif()
153+
else()
154+
if(NOT "${${NAME}}" STREQUAL "")
155+
set(${OUT_VAR}
156+
TRUE
157+
PARENT_SCOPE
158+
)
159+
else()
160+
set(${OUT_VAR}
161+
FALSE
162+
PARENT_SCOPE
163+
)
164+
endif()
165+
endif()
166+
else()
167+
if("${${NAME}}")
168+
set(${OUT_VAR}
169+
TRUE
170+
PARENT_SCOPE
171+
)
172+
else()
173+
set(${OUT_VAR}
174+
FALSE
175+
PARENT_SCOPE
176+
)
177+
endif()
178+
endif()
179+
endfunction()
180+
120181
function(check_required_options_on)
121182
cmake_parse_arguments(ARG "" "IF_ON" "REQUIRES" ${ARGN})
122183

123-
if(${${ARG_IF_ON}})
184+
is_option_enabled(${ARG_IF_ON} _if_on)
185+
if(_if_on)
124186
foreach(required ${ARG_REQUIRES})
125-
if(NOT ${${required}})
187+
is_option_enabled(${required} _required_on)
188+
if(NOT _required_on)
126189
message(FATAL_ERROR "Use of '${ARG_IF_ON}' requires '${required}'")
127190
endif()
128191
endforeach()
@@ -133,9 +196,11 @@ endfunction()
133196
function(check_conflicting_options_on)
134197
cmake_parse_arguments(ARG "" "IF_ON" "CONFLICTS_WITH" ${ARGN})
135198

136-
if(${${ARG_IF_ON}})
199+
is_option_enabled(${ARG_IF_ON} _if_on)
200+
if(_if_on)
137201
foreach(conflict ${ARG_CONFLICTS_WITH})
138-
if(${${conflict}})
202+
is_option_enabled(${conflict} _conflict_on)
203+
if(_conflict_on)
139204
message(FATAL_ERROR "Both '${ARG_IF_ON}' and '${conflict}' can't be ON")
140205
endif()
141206
endforeach()

tools/cmake/preset/default.cmake

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,9 @@ check_conflicting_options_on(
423423
EXECUTORCH_BUILD_PTHREADPOOL EXECUTORCH_BUILD_CPUINFO
424424
)
425425

426-
# Selective build specifiers are mutually exclusive.
427-
check_conflicting_options_on(
428-
IF_ON EXECUTORCH_SELECT_OPS_YAML CONFLICTS_WITH EXECUTORCH_SELECT_OPS_LIST
429-
EXECUTORCH_SELECT_OPS_MODEL
430-
)
431-
432-
check_conflicting_options_on(
433-
IF_ON EXECUTORCH_SELECT_OPS_LIST CONFLICTS_WITH EXECUTORCH_SELECT_OPS_MODEL
434-
)
426+
# Legacy selective build selectors are intentionally allowed to compose.
427+
# This matches gen_oplist.py, which merges list, YAML, and model inputs into
428+
# one selected_operators.yaml file.
435429

436430
check_required_options_on(
437431
IF_ON EXECUTORCH_BUILD_WASM REQUIRES EXECUTORCH_BUILD_EXTENSION_MODULE

0 commit comments

Comments
 (0)