Skip to content

v16.1 — Compile-time decision trees: emlearn's speed on a Cortex-M0+, a 3 KB forest, and a tree importer

Choose a tag to compare

@danmcleran danmcleran released this 18 Oct 01:07
· 73 commits to master since this release
a92cc8e

v16.1 — Compile-time decision trees: emlearn's speed on a Cortex-M0+, a 3 KB forest, and a tree importer

On a Cortex-M0+ (NUCLEO-L031K6), tinymind's run-time tree walk was 3x slower than emlearn's generated if/else forest. This release closes that gap and goes past it on size, then adds an importer so trained scikit-learn, XGBoost and LightGBM models reach these trees directly.

Compile-time decision trees (#223, #226)

  • QStaticDecisionTree / QStaticGBDT run a decision tree or boosted ensemble from a compile-time node pool (a constexpr QTreeNode array in a type), under an evaluation policy:
    • QTreeInlineEval expands every node at compile time into a compare against an immediate threshold and a branch. The pool itself is never stored.
    • QTreeCompactEval walks a packed copy built at compile time: decision nodes only, 4 bytes each, in pre-order so the walk needs no cycle guard, and a deduplicated leaf table in the narrowest type that holds the values.
    • QTreeWalkEval walks the original 12-byte QTreeNode pool at run time.
  • All three give the same result for every input. The model is validated when it compiles: a split on a feature the input does not have, a child, root or class index outside the pool, or a cyclic tree is a build error rather than an out-of-bounds read.
  • 10-tree random forest on the L031 at -O3 (cycles, code + weights):
Cycles Code + weights
tinymind run-time walk 2,524 12.9 KB
tinymind compact 1,649 3.1 KB
tinymind inline (uint8 features) 845 7.9 KB
emlearn inline (generated if/else) 841 8.3 KB
emlearn loadable 6,409 7.1 KB

Inline ties emlearn's generated code (and beats it at -O2, 872 vs 917 cycles), in less flash at -O3 and less RAM (112 vs 184 B). Compact is the footprint option: 3.0 KB at -O2, half of emlearn's smallest build.

Tree-ensemble importer (#227)

  • apps/import_trees converts scikit-learn decision trees, random forests, extra trees and gradient boosting, XGBoost (gbtree) and LightGBM models, classifiers or regressors, into a C++14 header of these pools.
  • Integer features reproduce every split exactly; float features go onto a calibrated per-feature int8 / uint8 / int16 grid. XGBoost's strict x < t splits are converted separately from the x <= t of the other two.
  • The base score is measured from the model's own outputs instead of parsed from library metadata, which also checks the tree parsing against the library.
  • verify() compiles the generated header with a host compiler and checks every policy bit-exact against the importer's integer reference. 31 tests, including g++ and clang at C++14 and C++17 and a freestanding Cortex-M0+ build.

Benchmarks on a Cortex-M0+ (#219, #220, #221, #225)

  • The framework comparison now also runs on a NUCLEO-L031K6: Cortex-M0+ at 32 MHz, 32 KB flash, 8 KB SRAM, no FPU, DSP extension or hardware divide. The M0+ has no DWT, so a SysTick-based 32-bit cycle counter stands in, checked on the board.
  • Only tinymind fits the networks: no TFLM image links into 32 KB. tinymind's int8 MLP runs in 35,963 cycles (1.1 ms) and its int8 KWS CNN in 746,112 (23 ms), both bit-exact with the host and the F411. emlearn's MLP, which is float only, takes 707k cycles in software float.
  • The integer random forest above is the int8-against-int8 comparison with emlearn.
  • The benchmark page now has a Cortex-M0+ section, and the harness pins each run to one ST-LINK by serial number, so a run cannot flash one board and read the other.

C++14, enforced (#222)

  • C++14 was the stated minimum, but every build ran at the compiler default (C++17), and FFT1D / QFFT1D failed to link at C++14. That is fixed, tinymind_platform.hpp now stops a pre-C++14 build with an #error, and a new required CI job, "C++14 language floor", builds every header at -std=c++14 -pedantic-errors and builds and runs the test suites and examples at C++14.

Tooling

  • Builds that are meant to fail to compile (the prerequisite and qtree model checks) no longer appear as errors in the clang-tidy report (#224).
  • The benchmark Makefile rebuilds images when a library header changes; before, a run could re-measure a stale binary.

Documentation, architecture guides, and example gallery: https://danmcleran.github.io/tinymind/

Full changelog: v16.0...v16.1