Skip to content

Commit ed9c4b2

Browse files
authored
Lint the yamls, test op registration, and register the CPU runner's kernels (#24935)
Summary: The x86 `cadence_runner` couldn't load most programs the Cadence compiler produces for CPU, because `backends/cadence/aot/functions.yaml` didn't register their kernels, and nothing checked the yamls or the registry. This diff adds both checks and registers the missing kernels. The yaml lint, `aot/tests/test_functions_yaml.py`, checks every `functions*.yaml` for kernel names codegen can't resolve and for namespaces outside the libraries the backend builds. It found five bad entries, fixed here. `functions.yaml` registered `cadence::quantized_max_pool2d.out`, an op that no longer exists, instead of the `quantized_max_pool2d_nchw.out` the compiler emits. `functions_hifi.yaml` bound `quantize_per_tensor_asym16u` and `dequantize_per_tensor_asym32s` to the asym16s kernels, which clamp to int16 and read int32 as int16; on the simulator a round trip of 1.0 returned 0.65534 and alternating -0.31072 and 0.00001, and with the right kernels it returns 1.0, so this changes HiFi results for those two ops from wrong to correct. `functions_fusion_g3.yaml` had an extra `native::` in two names; Fusion G3 doesn't build against the pinned nnlib yet, so no CI result changes. The registry test, `cadence_op_registry_test`, links the ops library and checks with `registry_has_op_function` that every op codegen selected from the yaml is registered. It catches a dropped registration, an unlinked kernel library, or the kernel table filling up before every op is registered. It reads its expected ops from the same yaml, so it can't catch an op missing from the yaml altogether; the backend test suite catches that. It runs on the host for `functions.yaml`, where it shares the runner's codegen and yaml but not its link line, because the runner links `cadence_ops_lib` again in its second-stage build, and on the simulator for the HiFi cores. Vision isn't included because OSS has neither a Vision op-test job nor the `vision/operators/tests/CMakeLists.txt` that would supply gtest; with a stub CMakeLists the test passes on the Vision simulator, so adding it is a separate change with its own CI job. The diff registers the 7 Cadence ops the compiler emits for CPU (`conv1d`, `conv2d`, `avg_pool2d`, `fully_connected`, `transposed_im2row`, `transposed_convolution` and `_softmax_f32_f32`) against the existing generic kernels, and 38 ATen ops against their portable kernels. `conv3d` stays unregistered until a test covers it, and the 3-D pools have no portable kernel. `aten::linear.out` maps to the generic `fully_connected` kernel because the compiler keeps `aten.linear` on purpose and there is no portable linear kernel. `ReplaceLinearWithFullyConnectedOpPass` rewrites a linear to `fully_connected` only when it has one row, because the HiFi kernels compute one row, but the generic kernel computes every leading row with the same schema, and the kernel diff earlier in this stack added its dtype and shape checks. `cadence_ops_lib` now links `portable_kernels` instead of the hand-kept source list in `aten_ops_cadence`, which is removed, so `functions.yaml` is the only list. Registration still comes only from the yaml and the linker keeps only the kernels it references, so the runner grows from 3.1 MB to 4.4 MB. The HiFi, Vision and Fusion G3 builds don't change. Authored with Claude Code. Reviewed By: DrJessop Differential Revision: D128295395 Pull Request resolved: #24935
1 parent f96d78c commit ed9c4b2

16 files changed

Lines changed: 521 additions & 121 deletions

‎.ci/scripts/test-cadence-generic.sh‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
# LICENSE file in the root directory of this source tree.
77
#
88
# Build the Cadence generic (reference) op-level gtest tests for the host and
9-
# run them.
9+
# run them, then build and run cadence_op_registry_test, which checks that every
10+
# op in backends/cadence/aot/functions.yaml is registered by cadence_ops_lib.
1011
#
1112
# Unlike backends/cadence/build_cadence_runner.sh (built -fno-exceptions
1213
# -fno-rtti), the gtest tests need exceptions + RTTI, so those flags are NOT set
1314
# here.
1415

1516
set -euo pipefail
1617

17-
TEST_TARGET=cadence_generic_op_tests
18-
TEST_BIN="cmake-out/backends/cadence/generic/operators/tests/${TEST_TARGET}"
18+
BUILD_TARGETS=(cadence_generic_op_tests cadence_op_registry_test)
19+
TEST_BINS=(
20+
cmake-out/backends/cadence/generic/operators/tests/cadence_generic_op_tests
21+
cmake-out/backends/cadence/cadence_op_registry_test
22+
)
1923

2024
rm -rf cmake-out
2125
cmake \
@@ -28,6 +32,9 @@ cmake \
2832
-DPYTHON_EXECUTABLE="$(which python3)" \
2933
-Bcmake-out .
3034

31-
cmake --build cmake-out --target "${TEST_TARGET}" -j"$(nproc)"
35+
cmake --build cmake-out --target "${BUILD_TARGETS[@]}" -j"$(nproc)"
3236

33-
"${TEST_BIN}"
37+
for TEST_BIN in "${TEST_BINS[@]}"; do
38+
echo "=== running $(basename "${TEST_BIN}") ==="
39+
"${TEST_BIN}"
40+
done

‎.ci/scripts/test-cadence-xtensa.sh‎

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# LICENSE file in the root directory of this source tree.
77
#
88
# Build the Cadence Xtensa op-level gtest tests for the configured backend and
9-
# run them on the Instruction Set Simulator (xt-run).
9+
# run them on the Instruction Set Simulator (xt-run). On backends that opt in,
10+
# also build and run cadence_op_registry_test, which checks that every op in the
11+
# backend's functions yaml is registered by cadence_ops_lib.
1012
#
1113
# Requires the Xtensa toolchain env to already be set (run
1214
# .ci/scripts/setup-xtensa-tools.sh <backend> first): XTENSA_TOOLCHAIN,
@@ -22,21 +24,27 @@ set -euo pipefail
2224
: "${XTENSA_CORE:?run setup-xtensa-tools.sh first}"
2325
: "${CADENCE_OPT_FLAG:?run setup-xtensa-tools.sh first}"
2426

25-
# Map the optimized-kernel flag to the backend dir + gtest target name.
27+
# Map the optimized-kernel flag to the backend dir + gtest target name, and
28+
# whether the backend runs cadence_op_registry_test.
2629
case "${CADENCE_OPT_FLAG}" in
27-
EXECUTORCH_NNLIB_OPT) TARGET_DIR=hifi ;;
28-
EXECUTORCH_VISION_OPT) TARGET_DIR=vision ;;
29-
EXECUTORCH_FUSION_G3_OPT) TARGET_DIR=fusion_g3 ;;
30+
EXECUTORCH_NNLIB_OPT) TARGET_DIR=hifi RUN_REGISTRY_TEST=1 ;;
31+
EXECUTORCH_VISION_OPT) TARGET_DIR=vision RUN_REGISTRY_TEST=0 ;;
32+
EXECUTORCH_FUSION_G3_OPT) TARGET_DIR=fusion_g3 RUN_REGISTRY_TEST=0 ;;
3033
*)
3134
echo "ERROR: unknown CADENCE_OPT_FLAG='${CADENCE_OPT_FLAG}'" >&2
3235
exit 1
3336
;;
3437
esac
3538
TEST_TARGET="cadence_${TARGET_DIR}_op_tests"
36-
TEST_ELF="cmake-out/backends/cadence/${TARGET_DIR}/operators/tests/${TEST_TARGET}"
39+
TEST_ELFS=("cmake-out/backends/cadence/${TARGET_DIR}/operators/tests/${TEST_TARGET}")
40+
BUILD_TARGETS=("${TEST_TARGET}")
41+
if [[ "${RUN_REGISTRY_TEST}" == "1" ]]; then
42+
BUILD_TARGETS+=(cadence_op_registry_test)
43+
TEST_ELFS+=(cmake-out/backends/cadence/cadence_op_registry_test)
44+
fi
3745

3846
NPROC=$(nproc)
39-
echo "=== building ${TEST_TARGET} for ${XTENSA_CORE} (${CADENCE_OPT_FLAG}) ==="
47+
echo "=== building ${BUILD_TARGETS[*]} for ${XTENSA_CORE} (${CADENCE_OPT_FLAG}) ==="
4048
xt-clang --version | head -1
4149

4250
rm -rf cmake-out
@@ -61,24 +69,26 @@ cmake \
6169
-DPYTHON_EXECUTABLE="$(which python3)" \
6270
-Bcmake-out .
6371

64-
cmake --build cmake-out --target "${TEST_TARGET}" -j"${NPROC}"
65-
66-
if [[ ! -f "${TEST_ELF}" ]]; then
67-
echo "ERROR: ${TEST_ELF} was not produced" >&2
68-
exit 1
69-
fi
72+
cmake --build cmake-out --target "${BUILD_TARGETS[@]}" -j"${NPROC}"
7073

71-
echo "=== running ${TEST_TARGET} on xt-run ==="
72-
LOG=$(mktemp)
73-
# --exit_with_target_code propagates gtest_main's exit code, so a failing test
74-
# fails this step; also assert on the gtest summary lines as a backstop.
75-
xt-run --turbo --exit_with_target_code "${TEST_ELF}" 2>&1 | tee "${LOG}"
76-
if grep -q "\[ FAILED \]" "${LOG}"; then
77-
echo "ERROR: gtest reported failures for ${TEST_TARGET}" >&2
78-
exit 1
79-
fi
80-
if ! grep -q "\[ PASSED \]" "${LOG}"; then
81-
echo "ERROR: ${TEST_TARGET} did not report a gtest PASSED summary" >&2
82-
exit 1
83-
fi
74+
for TEST_ELF in "${TEST_ELFS[@]}"; do
75+
if [[ ! -f "${TEST_ELF}" ]]; then
76+
echo "ERROR: ${TEST_ELF} was not produced" >&2
77+
exit 1
78+
fi
79+
TEST_NAME=$(basename "${TEST_ELF}")
80+
echo "=== running ${TEST_NAME} on xt-run ==="
81+
LOG=$(mktemp)
82+
# --exit_with_target_code propagates gtest_main's exit code, so a failing test
83+
# fails this step; also assert on the gtest summary lines as a backstop.
84+
xt-run --turbo --exit_with_target_code "${TEST_ELF}" 2>&1 | tee "${LOG}"
85+
if grep -q "\[ FAILED \]" "${LOG}"; then
86+
echo "ERROR: gtest reported failures for ${TEST_NAME}" >&2
87+
exit 1
88+
fi
89+
if ! grep -q "\[ PASSED \]" "${LOG}"; then
90+
echo "ERROR: ${TEST_NAME} did not report a gtest PASSED summary" >&2
91+
exit 1
92+
fi
93+
done
8494
echo "Cadence ${TARGET_DIR} op tests passed on ${XTENSA_CORE}."

‎.github/workflows/_xtensa_test.yml‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Reusable: build + run the Cadence Xtensa op-level gtest tests for one core on
22
# the Instruction Set Simulator (xt-run). Mirrors _xtensa_build.yml's native
33
# OIDC skeleton (running xt-run needs the same licensed toolchain),
4-
# then builds the gtest op-test ELF and runs it on the simulator. The runner
5-
# cross-compile and these op tests are separate build configs (the tests need
6-
# exceptions/RTTI that the runner build disables), so this is a self-contained
7-
# job rather than consuming the runner artifact.
4+
# then builds the gtest op-test ELF and runs it on the simulator. On backends
5+
# that opt in, test-cadence-xtensa.sh also builds and runs
6+
# cadence_op_registry_test, which checks that cadence_ops_lib registers every
7+
# op in the backend's functions yaml. The runner cross-compile and these op
8+
# tests are separate build configs (the tests need exceptions/RTTI that the
9+
# runner build disables), so this is a self-contained job rather than
10+
# consuming the runner artifact.
811
name: xtensa-test
912

1013
on:

‎backends/cadence/CMakeLists.txt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,40 @@ if(EXECUTORCH_BUILD_CADENCE_OP_TESTS
155155
"${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators/tests/CMakeLists.txt"
156156
)
157157
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators/tests)
158+
159+
# Op-registry gtest: links cadence_ops_lib the way cadence_executor_runner
160+
# does and checks that every op codegen selected from the backend's functions
161+
# yaml is in the kernel registry after runtime_init(). The op list is read at
162+
# build time from cadence_ops_lib's selected_operators.yaml. gtest comes from
163+
# the op-test dir above.
164+
get_target_property(_cadence_ops_lib_dir cadence_ops_lib BINARY_DIR)
165+
set(_selected_ops_yaml
166+
${_cadence_ops_lib_dir}/cadence_ops_lib/selected_operators.yaml
167+
)
168+
set(_selected_ops_h
169+
${CMAKE_CURRENT_BINARY_DIR}/cadence_ops_lib_selected_ops.h
170+
)
171+
add_custom_command(
172+
OUTPUT ${_selected_ops_h}
173+
COMMAND
174+
${PYTHON_EXECUTABLE}
175+
${CMAKE_CURRENT_SOURCE_DIR}/utils/gen_selected_ops_header.py
176+
--oplist-yaml=${_selected_ops_yaml} --output-path=${_selected_ops_h}
177+
DEPENDS ${_selected_ops_yaml}
178+
${CMAKE_CURRENT_SOURCE_DIR}/utils/gen_selected_ops_header.py
179+
)
180+
add_executable(
181+
cadence_op_registry_test cadence_op_registry_test.cpp ${_selected_ops_h}
182+
)
183+
target_compile_definitions(
184+
cadence_op_registry_test PRIVATE GTEST_HAS_PTHREAD=0
185+
)
186+
target_include_directories(
187+
cadence_op_registry_test PRIVATE ${_common_include_directories}
188+
${CMAKE_CURRENT_BINARY_DIR}
189+
)
190+
target_link_libraries(
191+
cadence_op_registry_test PRIVATE gtest gtest_main executorch
192+
cadence_ops_lib
193+
)
158194
endif()

‎backends/cadence/aot/BUCK‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ fbcode_target(_kind = executorch_generated_lib,
168168
"//executorch/backends/cadence/generic/operators:op_quantized_max_pool2d_nhwc",
169169
"//executorch/backends/cadence/generic/operators:op_quantized_mul",
170170
"//executorch/backends/cadence/generic/operators:op_quantized_relu",
171+
"//executorch/backends/cadence/generic/operators:op_avg_pool2d",
172+
"//executorch/backends/cadence/generic/operators:op_conv1d",
173+
"//executorch/backends/cadence/generic/operators:op_conv2d",
174+
"//executorch/backends/cadence/generic/operators:op_fully_connected",
175+
"//executorch/backends/cadence/generic/operators:op_softmax",
176+
"//executorch/backends/cadence/generic/operators:op_transposed_convolution",
177+
"//executorch/backends/cadence/generic/operators:op_transposed_im2row",
171178
"//executorch/kernels/portable:executorch_all_ops",
172179
"//executorch/kernels/portable:operators",
173180
],
@@ -741,3 +748,14 @@ fbcode_target(_kind = python_unittest,
741748
"//later:lib",
742749
],
743750
)
751+
752+
fbcode_target(_kind = python_unittest,
753+
name = "test_functions_yaml",
754+
srcs = [
755+
"tests/test_functions_yaml.py",
756+
],
757+
resources = glob(["functions*.yaml"]),
758+
deps = [
759+
"fbsource//third-party/pypi/pyyaml:pyyaml",
760+
],
761+
)

0 commit comments

Comments
 (0)