Commit ad2f500
Right-size operator registry from selective build metadata (#19118)
### Summary
The runtime kernel registry is a fixed-size static array whose default
capacity (MAX_KERNEL_NUM=2000) permanently occupies ~24 KiB of BSS on
32-bit machines even when only a handful of kernels are actually
registered. When selective build is active the exact set of kernels
needed by the model is already known at build time, so the registry can
be sized to fit.
A new codegen tool (gen_max_kernel_num.py) counts the (op, kernel_key)
tuples in selected_operators.yaml, adds the prim ops registered by
register_prim_ops.cpp, and writes the total into a generated header.
operator_registry.cpp picks the header up via __has_include. A
user-supplied -DMAX_KERNEL_NUM still takes precedence, and builds that
don't use selective build keep the 2000 default.
The below example demonstrates examples/selective_build/basic (two
selected ops) on a 64-bit machine. The registry's BSS footprint drops
from 48000 B (2000 slots) to 840 B (35 slots).
Fixes #18618
Example generated header:
executorch/runtime/kernel/selected_max_kernel_num.h
```
// @generated by executorch/codegen/tools/gen_max_kernel_num.py. Do not edit.
#pragma once
#define EXECUTORCH_SELECTED_MAX_KERNEL_NUM 27
```
### Test plan
```
# 1. Export a tiny model
python -m examples.portable.scripts.export --model_name="add_mul"
# 2. Auto-sized (no MAX_KERNEL_NUM flag)
cmake -DCMAKE_BUILD_TYPE=Release \
-DEXECUTORCH_SELECT_OPS_MODEL="./add_mul.pte" \
-B/tmp/sb_auto examples/selective_build/basic
cmake --build /tmp/sb_auto -j$(nproc)
# 3. User override
cmake -DCMAKE_BUILD_TYPE=Release \
-DEXECUTORCH_SELECT_OPS_MODEL="./add_mul.pte" \
-DMAX_KERNEL_NUM=100 \
-B/tmp/sb_pin examples/selective_build/basic
cmake --build /tmp/sb_pin -j$(nproc)
# 4. Baseline (no selective build)
cmake -DCMAKE_BUILD_TYPE=Release -B/tmp/sb_default .
cmake --build /tmp/sb_default --target executorch_core -j$(nproc)
```
```
echo "=== auto-sized ==="
cat /tmp/sb_auto/executorch/executorch_selected_kernels/executorch/runtime/kernel/selected_max_kernel_num.h
nm -S /tmp/sb_auto/executorch/CMakeFiles/executorch_core.dir/runtime/kernel/operator_registry.cpp.o \
| grep registered_kernels_data
/tmp/sb_auto/selective_build_test --model_path=./add_mul.pte 2>&1 | tail -1
echo "=== user override (MAX_KERNEL_NUM=100) ==="
find /tmp/sb_pin -name selected_max_kernel_num.h 2>/dev/null || echo "(no header generated — expected)"
nm -S /tmp/sb_pin/executorch/CMakeFiles/executorch_core.dir/runtime/kernel/operator_registry.cpp.o \
| grep registered_kernels_data
echo "=== baseline (no selective build) ==="
nm -S /tmp/sb_default/CMakeFiles/executorch_core.dir/runtime/kernel/operator_registry.cpp.o \
| grep registered_kernels_data
```
Expected output (BSS size is the 4-hex column after the address):
```
=== auto-sized ===
#define EXECUTORCH_SELECTED_MAX_KERNEL_NUM 27
0... 0000000000000288 b ...registered_kernels_data... # 648 B = 27 × 24
OutputX 0: tensor(sizes=[2, 2], [3., 3., 3., 3.])
=== user override (MAX_KERNEL_NUM=100) ===
(no header generated — expected)
0... 0000000000000960 b ...registered_kernels_data... # 2400 B = 100 × 24
=== baseline (no selective build) ===
0... 000000000000bb80 b ...registered_kernels_data... # 48000 B = 2000 × 24
```
Co-authored-by: Claude <noreply@anthropic.com>1 parent 922adad commit ad2f500
7 files changed
Lines changed: 552 additions & 2 deletions
File tree
- codegen/tools
- test
- docs/source
- examples/selective_build
- runtime/kernel
- tools/cmake
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1228 | 1228 | | |
1229 | 1229 | | |
1230 | 1230 | | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
1231 | 1257 | | |
1232 | 1258 | | |
1233 | 1259 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
0 commit comments