
Neural networks for embedded Rust
tinymind-rs (crate tinymind) is a Rust port of tinymind (C++), a C++ library for neural networks on microcontrollers. Every ported path produces the same bits as the C++ build (floating point: with std on a glibc host).
XOR example Iris example Energy example Anomaly detection Decision trees Q-learning maze Activity recognition Fine-tuning on the device Int8 inference API reference View on GitHub
What it is
no_stdand allocation-free. The library needs only Rust’scorelibrary, notstdoralloc, so it builds for bare-metal targets with no operating system. Two optional features add more:stdfor the host-side int8 calibration helpers, andlibmfor floating-point tanh, sigmoid, the float optimizers and the heads’ softmax withoutstd. Networks are plain fixed-size arrays sized by const generics: no heap, no global state.- Fixed point and floating point, as in tinymind (C++). Both libraries run networks in fixed point and in floating point. The fixed-point value types are Q-format integers (Q16.16 and Q8.8), int8 in the TFLite/CMSIS-NN affine model, and the int8, uint8 or int16 feature grids of the decision trees, so training and inference need no FPU, and activations come from lookup tables. The same networks run on
f32andf64, with tanh and sigmoid from the math library (featurestd, orlibmwithout std); withstdon a glibc host they train bit for bit like tinymind (C++)’sfloatanddoublenetworks. - No
unsafe, no reachable panics. The library is#![forbid(unsafe_code)], and CI links a Cortex-M firmware image that calls every public function and whose panic handler does not exist: any panic path left after optimisation would fail the build. - Bit-exact with C++. The port is checked byte for byte against tinymind (C++)’s own C++ build on every pull request. See Equivalence with tinymind (C++).
- Built for microcontrollers, checked on them. CI builds the library for Cortex-M4F (
thumbv7em-none-eabihf) and Cortex-M0+ (thumbv6m-none-eabi), and it has been run on an STM32F411 and an STM32L031: int8 inference and on-device training match the C++ bit for bit there, at about tinymind (C++)’s code size. See On-board results. Minimum Rust version: 1.85.
How the port is checked, and what Rust’s guarantees did and did not buy: Bit for bit.
Status
The port covers what twelve of tinymind (C++)’s examples use (XOR, iris, handwritten digits, energy efficiency, the Q-learning maze and its deep Q-network version, LSTM activity recognition and air-quality forecasting, gas sensor drift with on-device fine-tuning, an int8 XOR, int8 gradient-boosted trees and a keyword-spotting shaped int8 CNN), plus networks with several hidden layers, inference-only networks, floating-point (f32, f64) networks, the Adam and RMSprop optimizers, weight decay, learning-rate step decay, early stopping and scheduled sampling, the int8 softmax, convolution and pooling layers, residual addition, multiplication, padding and concatenation, int8 LSTM and GRU cells, 16-bit FFTs (complex and real-input), and tools that write Rust modules from PyTorch, ONNX, TFLite and tree models. It is a partial port of tinymind (C++), and every ported path is checked bit for bit against it:
| Ported | Not yet ported |
|---|---|
Q16.16 (i32) and Q8.8 (i16) arithmetic, Truncate and Wrap policies | Other Q formats, saturating and rounding policies |
| ReLU, linear, and tanh and sigmoid via lookup tables; int8 inference (requantizer, dense layers, 2D convolution (standard, depthwise and pointwise), pooling, elementwise addition and multiplication, padding, concatenation, quantized ReLU, int8 activation tables, softmax bit-exact with TFLite’s, LSTM and GRU cells, radix-2 FFTs on 16-bit samples, complex or real-input); quantized decision trees and gradient-boosted ensembles, with an importer for scikit-learn, XGBoost and LightGBM models; importers for PyTorch models, int8 ONNX models quantized by ONNX Runtime and int8 TFLite models | The other activations (capped and leaky ReLU, …) |
Trainable networks with up to seven hidden layers (SGD with momentum, Adam or RMSprop; optional gradient clipping, L2 weight decay and a step-decay learning-rate schedule; early stopping; scheduled sampling for recurrent networks; the class from the output logits, predicted_class); inference-only networks of the same depths (weights only, can live in flash) | The rest of the int8 family (attention, state-space cells), SIMD |
Floating-point (f32, f64) networks, trained with SGD or tinymind (C++)’s floating-point Adam and RMSprop | |
Q-learning (QLearner) over a Q table or a deep Q-network (QNetwork), with tinymind (C++)’s maze and deep Q-network maze examples | |
LSTM networks with one hidden layer (LstmNetwork, in fixed or floating point), with tinymind (C++)’s activity-recognition and air-quality forecasting examples | Trainable GRU and Elman networks, recurrent networks with more than one hidden layer |
Head-only fine-tuning on the device: a trainable dense head (DenseHead) and a nearest-class-mean head (PrototypeHead) on top of a frozen model, with tinymind (C++)’s gas sensor drift example |
The crate is on crates.io; the API is not stable yet (before 1.0, a minor version may break it). tinymind (C++) itself is the full library; this port grows by porting slices of it.
Quick start
git clone https://github.com/danmcleran/tinymind-rs
cd tinymind-rs
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
cargo run --release --example optical_digits -- output # handwritten digit classifier
cargo run --release --example energy_efficiency -- output # building energy-load regression
cargo run --release --example anomaly -- output # autoencoder trained on the device flags faults
cargo run --release --features std --example int8_xor -- output # PyTorch-trained int8 network
cargo run --release --features std --example imported_mlp # models written by the PyTorch importer
cargo run --release --features std --example imported_cnn # convolutional models written by the PyTorch importer
cargo run --release --features std --example imported_lstm # int8 LSTM sequence models written by the PyTorch importer
cargo run --release --example imported_onnx # int8 models quantized by ONNX Runtime, via the ONNX importer
cargo run --release --features std --example gbdt_tabular_int8 -- output # int8 gradient-boosted trees
cargo run --release --features std --example kws_cortex_m_int8 # keyword-spotting shaped int8 CNN
cargo run --release --example imported_trees # scikit-learn trees via the tree importer
cargo run --release --example maze -- output # Q-learning: find the way out of a six-room maze
cargo run --release --example dqn_maze -- output # the same maze, learned by a deep Q-network
cargo run --release --example har_activity -- output # LSTM: classify accelerometer windows into four activities
cargo run --release --example air_quality -- output # LSTM: forecast the next hour of a pollutant series
cargo run --release --example gas_sensor_drift -- output # sensor drift, then on-device fine-tuning from a few samples
cargo test --all-features # unit, doc and golden tests
To use the library from another project:
[dependencies]
tinymind = "0.3.8"
# Host-side int8 calibration, and f32/f64 tanh, sigmoid, AdamFloat, RmsPropFloat, DenseHead softmax:
# tinymind = { version = "0.3.8", features = ["std"] }
# The same float math without std (for example on a Cortex-M4F):
# tinymind = { version = "0.3.8", features = ["libm"] }
The API reference starts with a complete example that trains XOR; it runs as a test on every build.