How to Strip Unused Operators and Kernels at Compile Time
By default, ESP-DL registers all operator modules and links all operator kernels for the target chip. When compile-time operator and kernel stripping (CONFIG_ESPDL_COMPILE_STRIP) is enabled, the build system inspects the models or requirement manifests declared by the project during CMake configuration, generates only the required operator registration entries and kernel selection table rows, and lets the linker discard unreferenced module implementations and assembly symbols via --gc-sections — significantly reducing firmware Flash footprint.
Note
The compile-time stripping specification is currently at the 0.1 preview stage:
Module-level stripping (
dl_module_includes.inc/dl_module_register.inc) covers all operator modules.Fine-grained kernel-level stripping (table-driven
dl_conv_select.incand future select tables) currently coversConv/Gemm/MatMul(theconvbase layer). Support will gradually expand to additional operators.
How It Works
Compile-time stripping uses a two-phase declarative pipeline:
Declaration phase (each component’s
CMakeLists.txt): Any application or model component includescmake/strip.cmakeafteridf_component_register()and callsesp_dl_strip_add(...)to register unencrypted.espdlmodels and/or.req.ymlrequirement manifests (for offline-resolved models, encrypted models, or handwritten module/base requirements); file types are detected automatically by extension.Resolution & finalization phase (automatically run at the end of configuration via
esp_dl_strip_finalize): Once every component’sCMakeLists.txthas finished executing, the build system automatically invokes:esp-dl/spec/resolve.py: resolves all declared.espdlmodels into canonical.req.ymlfiles.esp-dl/spec/merge.py(invoked byesp-dl/spec/generate.py): strictly validates and computes the union across all.req.ymlinputs.esp-dl/spec/generate.py: generates the following artifacts under theesp-dlbuild directory (typicallybuild/esp-idf/esp-dl/orbuild/esp-idf/espressif__esp-dl/):dl_compile_req.yml: the final effective merged requirement manifest for this build (useful for auditing and troubleshooting).dl_module_includes.incanddl_module_register.inc: header includes and deserialization registrations for only the required operators.dl_conv_select.inc: lookup table rows for only the required convolution kernels.
During linking, unreferenced C++ operators and assembly kernels in their per-symbol sections are automatically garbage-collected by the linker.
Note
When CONFIG_ESPDL_COMPILE_STRIP=n (the default), the build system generates the full module registry and full kernel selection tables. Any esp_dl_strip_add() declarations in CMakeLists.txt have no stripping effect (declared file paths and extensions are still validated at configure time), making it easy to toggle between full and stripped builds without editing CMake files.
Quick Start (Automatic Stripping from .espdl Models)
For most projects loading unencrypted .espdl models directly, automatic stripping takes only three steps.
Enable compile-time stripping in Kconfig
Run
idf.py menuconfigand enable:Component config ---> ESP-DL ---> [*] Enable compile-time operator and kernel strippingOr add the option directly to your project’s
sdkconfig.defaults:CONFIG_ESPDL_COMPILE_STRIP=y
Declare model dependencies in your component’s
CMakeLists.txtInclude
cmake/strip.cmakeafteridf_component_register()and callesp_dl_strip_add(...)(accepts one or more.espdl/.req.ymlfiles in a single call):idf_component_register( SRCS "app_main.cpp" INCLUDE_DIRS "." ) # Works with both ESP Component Registry (espressif__esp-dl) and local component (esp-dl) idf_build_get_property(build_components BUILD_COMPONENTS) if("espressif__esp-dl" IN_LIST build_components) idf_component_get_property(espdl_dir espressif__esp-dl COMPONENT_DIR) elseif("esp-dl" IN_LIST build_components) idf_component_get_property(espdl_dir esp-dl COMPONENT_DIR) endif() include(${espdl_dir}/cmake/strip.cmake) esp_dl_strip_add("${CMAKE_CURRENT_LIST_DIR}/models/mobilenet_v2.espdl")
If your project contains multiple models (even across multiple components), call
esp_dl_strip_add(...)in each component’sCMakeLists.txt; the build system automatically computes the union of all declarations.Note
Every declared
.espdl(and.req.yml) must match the active build targetIDF_TARGET. When a.espdlmodel carries target metadata that does not matchIDF_TARGET(or a.req.ymlomitsIDF_TARGET),spec/resolve.pyorspec/generate.pyaborts configure with a target mismatch error; legacy.espdlmodels without embedded target metadata emit a configure-timeWARNINGand skip the compatibility check. When a project ships per-chip models, guard registrations byIDF_TARGETinCMakeLists.txt:idf_build_get_property(target IDF_TARGET) if(target STREQUAL "esp32p4" OR target STREQUAL "esp32s31") esp_dl_strip_add("${CMAKE_CURRENT_LIST_DIR}/models/model_p4.espdl") elseif(target STREQUAL "esp32s3") esp_dl_strip_add("${CMAKE_CURRENT_LIST_DIR}/models/model_s3.espdl") else() esp_dl_strip_add("${CMAKE_CURRENT_LIST_DIR}/models/model_c.espdl") endif()
Build and verify the stripped result
Run the standard ESP-IDF build commands:
idf.py set-target esp32p4 idf.py build
After building, inspect the results:
build/esp-idf/esp-dl/dl_compile_req.yml(orbuild/esp-idf/espressif__esp-dl/dl_compile_req.yml) to review the effective operator and kernel set.Run
idf.py sizeoridf.py size-componentsto compare Flash usage before and after enabling stripping.
Advanced Usage
When your project uses encrypted models, loads models from external storage, or needs to manually declare C++ modules (modules) or base-layer kernels (bases), declare a .req.yml file and register it in CMakeLists.txt via esp_dl_strip_add(...). .espdl and .req.yml files can be freely mixed in the same project or even the same esp_dl_strip_add(...) call and are merged automatically.
Generating .req.yml Offline with spec/resolve.py
Offline resolution into .req.yml is recommended in the following scenarios:
The
.espdlmodel is encrypted with AES-128 (so plaintext keys do not need to be passed into CMake).The model lives in external storage (such as an SD card, standalone flash partition, or OTA payload) rather than in the source repository.
You want the model’s operator/kernel requirements tracked as a human-readable file in version control for code review.
esp-dl/spec/resolve.py resolves .espdl models into canonical .req.yml files:
# 1. Auto-detect supported target chips from unencrypted .espdl model(s)
python3 esp-dl/spec/resolve.py \
--espdl model_a.espdl model_b.espdl \
-o my_models.req.yml
# 2. Resolve an AES-128 encrypted .espdl (--key accepts a .key file, 32-char hex, or "0x01, 0x02, ...")
python3 esp-dl/spec/resolve.py \
--espdl encrypted_model.espdl \
--key path/to/aes.key \
-o encrypted_model.req.yml
# 3. Restrict output to specific target chip(s) (comma-separated)
python3 esp-dl/spec/resolve.py \
--espdl model_a.espdl \
--target esp32p4,esp32s31 \
-o p4_model.req.yml
Then register the generated .req.yml in CMakeLists.txt:
include(${espdl_dir}/cmake/strip.cmake)
esp_dl_strip_add("${CMAKE_CURRENT_LIST_DIR}/my_models.req.yml")
Merging Multiple .req.yml Files Offline with spec/merge.py
When multiple encrypted models use different AES keys and are resolved separately, or when you want to combine model manifests with handwritten operator manifests into a single distributable file, use esp-dl/spec/merge.py:
# 1. Merge multiple .req.yml files (defaults to the intersection of supported targets, grouped by ISA)
python3 esp-dl/spec/merge.py \
model_a.req.yml model_b.req.yml custom_ops.req.yml \
-o merged.req.yml
# 2. Filter and merge for specific target chip(s)
python3 esp-dl/spec/merge.py \
model_a.req.yml custom_ops.req.yml \
--target esp32p4 \
-o merged_p4.req.yml
Handwriting .req.yml for Direct C++ Module or Base Calls
If your code constructs C++ modules directly (for example dl::module::Conv, dl::module::Softmax) or invokes base-layer APIs directly (for example dl::base::Conv) without a corresponding .espdl file, write a .req.yml manifest and include it via esp_dl_strip_add(...):
spec_version: "0.1"
targets:
esp32p4, esp32s31:
modules:
# Omitting kernels keeps all legal kernels (Relu has no table-driven kernels)
Relu:
register: true
# Register Conv and keep all legal Conv kernels
Conv:
register: true
kernels: all
# Keep only a specific 1x1 kernel without registering Gemm in ModuleCreator
Gemm:
register: false
kernels:
- dl_esp32p4_s8_conv_1x1
bases:
# Keep a specific conv base kernel without registering any Module
conv:
- dl_esp32p4_s16_conv_3x3
If you simply want all legal kernels and module registration enabled, a compact list shorthand is also supported:
spec_version: "0.1"
targets:
esp32s3:
modules:
- Conv
- Relu
- Reshape
bases:
conv: all
.req.yml Format Reference
Canonical Example
Canonical .req.yml files emitted by spec/resolve.py and spec/merge.py group chips by ISA compile group (for example, esp32p4 and esp32s31 share identical kernel rules and assembly symbols):
spec_version: "0.1"
targets:
esp32p4, esp32s31:
modules:
Conv:
register: true
kernels:
- dl_esp32p4_s8_conv_1x1_bias_relu
- dl_esp32p4_s8_dwconv_3x3_pad_bias_relu
MatMul:
register: true
kernels: []
Relu:
register: true
kernels: []
bases:
conv:
- dl_esp32p4_s16_conv_1x1
The 5 Physical ISA Compile Groups
ESP-DL partitions all 9 supported chips into 5 physical ISA compile groups. Chips within the same compile group share identical ISA extensions, kernel sources, kernel symbol names, and kernel selection rules, so they can be combined under a single comma-separated key in targets to share one manifest:
Compile Group ( |
Architecture / ISA |
Kernel Symbol Prefixes |
Matching |
|---|---|---|---|
|
Xtensa LX6 + MAC16 |
|
|
|
Xtensa LX7 (no MAC16) |
|
|
|
RISC-V scalar (no PIE) |
|
|
|
Xtensa PIE V1 (TIE728) |
|
|
|
RISC-V PIE V2 |
|
|
Note
Why are there 3 model export groups in
esp-ppqbut 5 compile groups in ESP-DL? Whenesp-ppqexports a.espdlmodel, the model binary depends only on rounding mode and weight layout, so all non-PIE chips share the sametarget="c"model. At ESP-DL compile and link time, however,esp32(Xtensa + MAC16),esp32s2(Xtensa without MAC16), and RISC-V scalar chips (esp32c2/esp32c3/esp32c5/esp32c6) have distinct ISA capabilities and scalar kernels. Consequently, runningspec/resolve.pyon atarget="c"model emits separate sections for these 3 scalar compile groups, keeping manifests forward-compatible as ISA-specific kernels evolve.Grouping guideline when handwriting
.req.yml: The Kernel Symbol Prefixes column lists the ISA-level symbol families belonging to each compile group; each individual operator only registers the subset it implements (for example,convcurrently usesdl_xtensa_mac16_*+dl_c_*onesp32,dl_tie728_*onesp32s3, anddl_esp32p4_*onesp32p4/esp32s31). Prefer generating manifests viaspec/resolve.pyso every symbol is guaranteed valid for the target. If an entry lists explicit kernel symbol names (such asdl_esp32p4_*ordl_xtensa_mac16_*), only combine chips belonging to the same compile group under that key and define separate keys for different groups. If an entry only declares modules withkernels: all(or omitskernels), you may freely list chips across multiple groups under one comma-separated key — each chip automatically expands to its own legal kernel set during configuration.
Field Semantics and Validation Rules
spec/merge.py (and spec/generate.py during build configuration) enforce the following strict validation rules:
Field / Syntax |
Semantics & Validation Rules |
|---|---|
|
Required. For example |
|
Explicit chip target mapping. Keys must be concrete chip names or comma-separated chip lists (e.g., |
|
Boolean ( |
|
|
Unknown key check |
Module entries accept only |
|
Directly appends kernels to a table-driven base layer (currently |
CMake API Reference
The registration API is defined in ${espdl_dir}/cmake/strip.cmake and must be called after idf_component_register():
CMake Function |
Description |
|---|---|
|
Registers one or more unencrypted |
Troubleshooting & FAQ
Configure error after enabling
CONFIG_ESPDL_COMPILE_STRIP=y:no esp_dl_strip_add() declarations were registeredCause: Stripping is enabled, but no component in the project called
esp_dl_strip_add().Fix: Include
cmake/strip.cmakein your application or model component’sCMakeLists.txtand callesp_dl_strip_add(...). If you do not need stripping, setCONFIG_ESPDL_COMPILE_STRIP=n.
Runtime error:
Do not support <Op>, please implement and register it first.Cause: Model deserialization encountered an operator not registered in the generated
dl_module_register.inc.Fix: Inspect
build/esp-idf/<esp-dl>/dl_compile_req.ymlto check whether a.espdlmodel was omitted or whether a handwritten.req.ymlsetregister: falsefor that operator.
Runtime error:
no Conv kernel for packed key 0x....Cause: Executing
Conv/Gemm/MatMulrequested a kernel key not present in the generateddl_conv_select.inc.Fix:
If you invoke
dl::base::Convor handwritten C++dl::module::Convdirectly (without a corresponding.espdl), add the requiredbases.convormodules.Conventries to a.req.ymlfile and register it withesp_dl_strip_add(...).If a model was replaced or re-quantized, re-run
idf.py build(CMake automatically reconfigures when a declared.espdlchanges; if you use an offline.req.yml, re-runspec/resolve.pyand, if applicable,spec/merge.py).
Is it an error if
MatMulor other operators showkernels: []in the generated.req.yml?No.
kernels: []is expected in two cases:The operator has no table-driven kernel selection table (e.g.,
Relu,Softmax,Reshape).An S8/S16
MatMulnode uses theinput1_layout=native_knnative path instead of the quantized Conv 1x1 lookup table, so no Conv kernels need to be pulled in.
Warning when resolving legacy
.espdlfiles:WARNING: ... model has no 'targets'/'target' metadataOlder
.espdlexports may not contain target metadata. This warning only skips static target-compatibility validation and does not affect operator/kernel extraction. Re-export the model with a neweresp-ppqversion, or pass--targetexplicitly when invokingspec/resolve.pyoffline.