Repository navigation
v16.9 — Calibration fixes: Conv+BatchNorm fusion and the int8 KWS example
v16.9 — Calibration fixes: Conv+BatchNorm fusion and the int8 KWS example
Two calibration fixes. One changes what the PyTorch importer exports for any model with a Conv2D directly followed by a BatchNorm2D; re-export such models to pick it up. The other makes the int8 keyword-spotting example produce real output. Nothing under cpp/ changed, so on-device code is the same as v16.8.
Fixes
- PyTorch importer: Conv2D + BatchNorm2D calibration (
apps/import_pytorch). When a BatchNorm follows a convolution, the importer folds the BN into the conv's weights and bias. The folded weights were correct, but the fused layer kept the convolution's own floatforward, and calibration observes each layer through that function. The fused layer's output scale was therefore fitted to the range before the BatchNorm, while the int8 layer outputs the values after it, so outputs either saturated or wasted resolution, depending on the BN's scale and shift. The fusedforwardnow runs the convolution and then the BatchNorm, with the same constants as the folded weights. A new numpy-only test file,apps/import_pytorch/test_import_pytorch.py, checks that the fused weights and the fusedforwardboth compute conv-then-BN and that calibration observes the post-BN range; the last two fail on the old importer (an observed minimum of -1.74 against -4.73 after the BN). Models without a Conv2D-then-BatchNorm2D pair are unaffected. Present since the importer was added. - int8 keyword-spotting example (
examples/kws_cortex_m_int8). The example gave every activation the same fixed scale, 1/127. Its synthetic weights shrink the signal several times over at each layer, so all ten int8 logits were 0 and the printed "predicted class" was the first of ten equal values. Each layer's activation scale is now fitted from a float reference pass over 32 calibration inputs withRangeObserver, and the runner prints the int8 logits next to the float model's: the largest difference is 0.6% of the float logits' range, and both pick the same class. The input, the weights and the reported weight bytes are unchanged.
Both were found while porting to tinymind-rs, whose parity tests compare every tensor with the C++.
Docs
- The example's README and the Pages walkthrough describe the per-layer calibration and show the new output.
- The importer README describes what the fused layer calibrates and how to run the new tests.
Documentation, architecture guides, and example gallery: https://danmcleran.github.io/tinymind/
Full changelog: v16.8...v16.9