Skip to content

v16.0 — An exact QValue, an on-target benchmark vs TFLM, and 2.3x faster int8 CNNs

Choose a tag to compare

@danmcleran danmcleran released this 14 Oct 13:13
· 82 commits to master since this release
0688fa9

v16.0 — An exact QValue, an on-target benchmark vs TFLM, and 2.3x faster int8 CNNs

This release fixes every QValue defect found by a new differential oracle, which checks each QValue operation against an exact 128-bit reference. One of those fixes changes the API, so this is a breaking release: q += n, q -= n, q == n and q != n with an int no longer compile (see below). Three other fixes change behaviour at the edges of the range: the saturation maximum, division by zero, and 0 / 0.

tinymind also now has an on-target benchmark against TensorFlow Lite for Microcontrollers (TFLM) with CMSIS-NN and against emlearn, on a NUCLEO-F411RE (Cortex-M4F, 100 MHz). The int8 weights and requantizers come from the same TFLite model, so tinymind's outputs are bit-exact with TFLM's on the board. Over this release the keyword-spotting CNN went from 422,449 to 181,568 cycles at -O3: 3.2x the speed of TFLM with CMSIS-NN (576,368). Every performance change is bit-exact.

Breaking change: QValue integer operands (#196)

  • q += n, q -= n, q == n and q != n with an int, a raw word or any other non-QValue operand no longer compile. They read an int as raw LSBs while the free operators read it as a whole number, so q += 1 added one LSB but q + 1 added 1.0. Say which you mean:
    • whole number: q.addInteger(n), q.subtractInteger(n), q.compareInteger(n), or q += QValue(n, 0) / q == QValue(n, 0)
    • raw LSBs: q += QValue(raw), q.getValue() == raw
  • The free operators (q + n, q - n, q * n, q / n, q < n, ...) and q *= n, q /= n now treat an int as an exact whole number. Previously an int outside the fixed part's range wrapped first: for Q8.8, 1.0 < 200 was false and q / 256 divided by zero. Every in-range int gives the same result as before.

QValue fixes

A differential oracle (#190, #200) checks every public QValue operation against an exact 128-bit reference across 32 formats and all four rounding and saturation policies, with operation chains. A nightly seeded soak runs about 2 billion checks under ASan and UBSan (#203). Known defects are a ratchet that can only shrink, and none are left. What it found and what was fixed:

  • Narrow formats stay canonical (#188): formats narrower than their storage word (e.g. QValue<8, 16, true> in an int32_t) left stale bits above the format's width, so negative values read back as large positives.
  • Undefined behaviour (#190): WrapPolicy add/sub overflowed signed words of int width or wider; RoundUpPolicy's half-LSB mask was an int shift; fromParts overflowed for Q1.31 and Q1.63. All now well-defined and bit-identical for every format used in the tree.
  • True saturation maximum (#193), behaviour change: QFormatMaxValue() was the largest whole number (127.0 for Q8.8) instead of the true maximum (127.99609375). Under MinMaxSaturatePolicy, results above the largest whole number now keep their fraction, and a saturated Q8.8 result is 0x7FFF, not 0x7F00.
  • Saturating ++/-- on Q1.x and unsigned saturating add (#194): Q1.15 ++0.5 gave -0.5; an unsigned addend above the maximum wrapped instead of clamping.
  • Division by zero is defined (#205, #207), behaviour change: under WrapPolicy, q / 0 was an integer division by zero (SIGFPE on x86). Both policies now return the maximum for a positive numerator, the minimum for a negative one, and 0 for 0 / 0 (MinMaxSaturatePolicy used to return the minimum).

Benchmark: tinymind vs TFLM vs emlearn on Cortex-M4

  • benchmarks/framework_comparison_f411/ (#191) holds the board support, the DWT timing harness, one backend per framework and the model generator. Two models: an MLP (sklearn digits) and a KWS-style CNN (conv 3x3 → max-pool → depthwise → pointwise → global average pool → dense). make LEVEL=O3 run flashes and captures every image.
  • Results at -O3, cycles per inference: KWS CNN 181,568 (tinymind + SMLAD) vs 576,368 (TFLM CMSIS-NN); MLP 10,241 vs 14,030. tinymind's code and weights are 7-22x smaller than TFLM with CMSIS-NN.
  • make host-bench (#214) times the same models on an x86 host, scalar against each x86 SIMD backend the CPU supports. It fails if any build's outputs drift from the expected hashes.

Performance (all bit-exact)

  • Conv windows (#195, #202): the int8 conv gathers each kernel window of up to 64 bytes once and runs every filter over it, instead of one dot product per kernel tap. It now gathers a tile of windows (up to a row) and sweeps the tile filter by filter. The tile's stack budget is TINYMIND_CONV_TILE_MAX_BYTES (default 256).
  • Weights and requantizer held in registers (#210, #211): an int8 output store may alias the int8 weights, so GCC reloaded the weights after every output. Conv, pointwise and depthwise now copy each filter's weights (up to 64 bytes) into a local, and decode the requantizer once per filter into a form with the zero point folded into the rounding and a single SSAT/USAT clamp.
  • One rounding per requantize (#208): for the common case (multiplier >= 0, shift >= 2), gemmlowp's two roundings compose into one 64-bit rounding: an SMLAL and an ASR on Cortex-M4.
  • Depthwise and pointwise loops (#198): pointwise runs filter-outer, and depthwise runs channel-outer with the zero point folded into the bias.
  • -O2 robustness (#192, #199, #204): the SMLAD dot product folds the zero point with SXTAB16 and unrolls its block loop; the scalar dot product and the pooling window loops unroll too. With identical kernels the -O2 MLP used to range from 14.5k to 24.6k cycles depending on surrounding code. Now the CNN at -O2 is about 6% slower than at -O3 (192,965 vs 181,568 cycles).
  • x86 short dot products (#213): every x86 vector backend (AVX2, AVX-VNNI, AVX-512F, AVX-512 VNNI) routes dot products shorter than 16 elements to the unrolled scalar code. Below one block the vector paths did no vector work but still paid for their reductions. On an i7-11850H, KWS at -O2 with AVX2 went from 19.0 to 14.3 µs.

Docs

  • New Pages pages: the on-target benchmark write-up (#189) and a Releases page generated from GitHub Releases (#186).
  • The project name is now written as tinymind throughout the repository, matching its name: the README, the docs and the Pages site, code comments, and example plots (#215, #216, #217, #218).

Benchmark write-up: https://danmcleran.github.io/tinymind/benchmarks.html

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

Full changelog: v15.2...v16.0