tinymind 0.1.4

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind C++ library, bit-exact with it
Documentation
# tinymind-rs

[![crates.io](https://img.shields.io/crates/v/tinymind.svg)](https://crates.io/crates/tinymind)
[![docs.rs](https://docs.rs/tinymind/badge.svg)](https://docs.rs/tinymind)
[![CI](https://github.com/danmcleran/tinymind-rs/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/danmcleran/tinymind-rs/actions/workflows/ci.yml)

tinymind-rs (crate `tinymind`) brings [tinymind](https://github.com/danmcleran/tinymind),
a header-only C++ library for neural networks on embedded systems, to Rust. Like
tinymind, it is free, open source software under the MIT license. The library is
`no_std` and allocation-free, and every ported path is verified bit-for-bit
against the C++ build: the XOR example's weights, training log and decision
surface are byte-identical to the C++ output.

Documentation, including the API reference: <https://danmcleran.github.io/tinymind-rs/>

## Status

Early. So far the port covers Q16.16 and Q8.8 arithmetic, ReLU, linear and
lookup-table tanh and sigmoid activations, trainable networks with any number of
hidden layers (SGD with momentum, Adam or RMSprop, optional gradient clipping), an inference-only
one-hidden-layer network that is just its weights, and int8
affine-quantized inference (the TFLite/CMSIS-NN model: requantizer, dense layer,
quantized ReLU, activation tables, and a softmax that is bit-exact with
TFLite's). The API is not stable yet: before 1.0, a minor version may break it.

The library is `no_std` by default. The `std` feature adds host-side calibration
helpers (`int8::calibration`) that turn float scales into the integer parameters
the int8 layers use.

The minimum supported Rust version is 1.85. CI builds the library for Cortex-M4F
(`thumbv7em-none-eabihf`) and Cortex-M0+ (`thumbv6m-none-eabi`).

## Use it

```toml
[dependencies]
tinymind = "0.1"
# For host-side int8 calibration (float scales to requantizers and tables):
# tinymind = { version = "0.1", features = ["std"] }
```

The API reference is on [docs.rs](https://docs.rs/tinymind).

To run a model trained in PyTorch, `tools/import_pytorch` in this repository
turns it into a Rust module (int8 weights, integer requantizers and a `forward`
function; `no_std`). See its README.

## Quick start

```bash
cargo run --release --example xor -- output                  # Q16.16, output files in ./output
cargo run --release --example xor -- --format q8.8 output    # the same network in Q8.8
cargo run --release --example iris -- output                 # iris species classifier (ReLU hidden layer)
cargo run --release --example energy_efficiency -- output    # energy-load regression (linear output, clipped gradients)
cargo run --release --example anomaly -- output              # anomaly detection: an autoencoder trained on the device
cargo run --release --features std --example int8_xor -- output   # PyTorch-trained int8 network
cargo run --release --features std --example imported_mlp    # two models written by the PyTorch importer
cargo test --all-features                                # unit, doc and golden tests against the C++ output
PYTHON=<python with numpy> scripts/compare-with-cpp.sh   # rebuild the C++ side and diff everything
```

The `xor` example takes an optional `--format q16.16|q8.8` (default Q16.16), an
optional output directory (default `output`), then an optional learning rate and
momentum. It writes `nn_fixed_xor.txt`, `xor_training.csv` and
`xor_decision_surface.csv` there and prints a summary on stdout. The `iris` example
takes an optional output directory and data file (default `data/iris.data`, the
UCI Iris dataset under CC BY 4.0; see `NOTICE`) and writes `iris_loss.csv`,
`iris_scatter.csv` and `iris_confusion.csv`. The `energy_efficiency` example
reads `data/ENB2012_data.csv` (the UCI Energy Efficiency dataset, CC BY 4.0) the
same way and writes `energy_loss.csv` and `energy_pred.csv`. The `anomaly`
example trains an 8-4-8 autoencoder on synthetic normal vibration windows and
flags faults by reconstruction error; the same code runs on the boards in
`bench/`. It takes an optional output directory and writes `anomaly_scores.csv`
there. The `int8_xor` example (feature
`std`) runs a PyTorch-trained, int8-quantized 2-4-1 network and writes
`xor_decision_surface.csv`.

`scripts/compare-with-cpp.sh` needs a tinymind checkout, `g++`, and a Python with
numpy for the PyTorch importer checks (set `PYTHON`, for example to a virtual
environment's interpreter). It looks in `../tinymind` unless `TINYMIND_DIR` is
set.

## What is ported

| Rust | C++ (tinymind) |
| --- | --- |
| `src/qformat.rs` | `cpp/qformat.hpp`: `QValue`, Truncate and Wrap policies, `i32` and `i16` storage (`Q16_16`, `Q8_8`); `Sqrt` (`SquareRootApproximation` from `cpp/adam.hpp`) |
| `src/activation.rs` | `cpp/activationFunctions.hpp`, `cpp/lookupTable.hpp`, `cpp/interpolate.hpp`: ReLU, linear, tanh, sigmoid |
| `src/lut.rs` | `cpp/lookupTables.cpp`: Q16.16 and Q8.8 tanh and sigmoid tables, generated by `scripts/extract-luts.py` |
| `src/neuralnet.rs` | `cpp/neuralnet.hpp`, `cpp/gradientClipping.hpp`: one-hidden-layer MLP (`MultilayerPerceptron`), SGD with momentum, Adam or RMSprop, optional gradient clipping, batch size 1; and its inference-only form (`InferencePerceptron`, `IsTrainable = false`) |
| `src/network.rs` | `cpp/neuralnet.hpp` (`NeuralNetwork<..., HiddenLayers<...>>`): any number of hidden layers as a tuple of `Dense` layers, same training |
| `src/optimizer.rs` | `cpp/adam.hpp`, `cpp/rmsprop.hpp`: the `Sgd` (default), `Adam` and `RmsProp` optimizers for both trainable networks |
| `src/rand.rs` | a re-implementation of glibc `rand()`, which the C++ examples use for weight init and sampling |
| `examples/xor.rs` | `examples/xor/xor.cpp` |
| `examples/iris.rs` | `examples/iris/iris.cpp` |
| `examples/energy_efficiency.rs` | `examples/energy_efficiency/energy_efficiency.cpp` |
| `examples/anomaly.rs` | no counterpart: an on-device anomaly-detection demo built from the ported parts |
| `src/int8.rs` | `cpp/qaffine.hpp`, `cpp/qdense.hpp`, `cpp/qpointwiseconv2d.hpp`, `cpp/qactivations.hpp`, `cpp/qsoftmax_fixedpoint.hpp`: requantizer, int8 dense layer (per-tensor and per-channel), quantized ReLU, activation tables, softmax |
| `src/int8/calibration.rs` | `cpp/include/qcalibration.hpp`, `cpp/qactivations.hpp`: host-side calibration (feature `std`) |
| `examples/int8_xor.rs` | `examples/pytorch_quant/xor/xor_quant.cpp` |
| `tools/import_pytorch/` | `apps/import_pytorch/tinymind_import.py`: the PyTorch importer, emitting Rust |
| `examples/imported_mlp.rs` | two models written by that importer, checked against tinymind's importer + C++ runtime |

Everything else in tinymind (floating-point networks, other Q formats, unsigned
and 8/64-bit storage, the other activations, recurrent layers, the int8 quantization tiers, SIMD
backends, and so on) is not ported.

## How equivalence is proven

- `tests/xor_golden.rs` runs the `xor` example in each format and compares its
  stdout and the two CSV files byte-for-byte with the goldens in
  `tests/golden/<format>/`. The 2.4-2.6 MB `nn_fixed_xor.txt` is checked by
  length, line count and a 64-bit FNV-1a hash instead of being stored. The goldens
  come from the C++ build: Q16.16 from the example as shipped, Q8.8 from the same
  example with both bit counts in `xornet.h` set to 8. `tests/iris_golden.rs`
  does the same for iris against `tests/golden/iris/`, `tests/energy_golden.rs`
  for energy_efficiency against `tests/golden/energy/`, and
  `tests/int8_xor_golden.rs` for the int8 example against `tests/golden/int8_xor/`,
  and `tests/imported_mlp_golden.rs` for the importer's models. `TINYMIND_REF`
  records the tinymind commit they were last verified against. The int8 and
  importer tests need the `std` feature: run `cargo test --all-features`.
- `tests/deep_mlp.rs` checks networks with one, two and three hidden layers
  against tinymind's `NeuralNetwork` (every weight after every training step,
  both formats), trained with SGD, Adam and RMSprop, with and without gradient
  clipping.
- `tests/anomaly_golden.rs` pins the anomaly example's output and hash. The
  demo has no tinymind counterpart, so this golden is the crate's own output, a
  regression check; its parts are each checked against tinymind above.
- `tests/inference_mlp.rs` checks the inference-only perceptron against
  tinymind's untrainable network (`IsTrainable = false`) with the same weights,
  in both formats and two shapes.
- `tests/softmax_tflite.rs` checks the int8 softmax against the TFLite Micro
  reference kernel's outputs (tinymind's unit-test goldens and sweep hashes), and
  `tests/softmax_sweep.rs` against tinymind's C++ softmax and its calibration
  over 504 calibrations and row lengths 1 to 511.
- `scripts/compare-with-cpp.sh` builds the C++ examples from a tinymind checkout
  (xor in both formats, iris, energy efficiency, int8 xor) with the flags their
  Makefiles use, plus the C++ programs in `scripts/cpp/` that exercise what no
  example does, runs both sides, and compares every output file (29 checks today, including
  that `data/iris.data` and `data/ENB2012_data.csv` are still tinymind's files). It exits nonzero if anything differs.
- `tests/qformat_oracle.rs`, ported from tinymind's qformat oracle, checks every
  `QValue` operation against an exact reference: edge values and a fixed-seed
  sample for nine formats, and every operand pair for Q8.8 (about 26 billion
  checks, run in CI with overflow checks on). A nightly job reruns the sample with
  a fresh seed.
- Kani proofs (`src/kani_proofs.rs`) check, for every input, that the
  `QValue` operations match their definitions (division: that it never panics),
  that tanh and sigmoid stay within their tables' range, that the int8
  requantizer's fast path equals gemmlowp's two-step rounding, and that the
  requantizer, the int8 dense layers, the tables and the softmax never panic.
- `ci/panic-never` is a Cortex-M firmware image that calls every public function
  and whose panic handler is an undefined symbol, so CI fails to link it if any
  panic path survives optimisation (M0+ and M4F, three optimisation levels).
- `scripts/extract-luts.py` generates `src/lut.rs` from tinymind's
  `cpp/lookupTables.cpp`; CI regenerates it and fails if the tables differ.
- CI runs all of these on every pull request (the `cpp-equivalence` job checks out
  tinymind at `TINYMIND_REF` for the compare script and the table check), and
  branch protection blocks merging unless they pass.

## License

MIT. See `LICENSE` and `NOTICE`.