tinymind-rs
tinymind-rs (crate tinymind) brings 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 what three of tinymind's examples use: Q16.16 and Q8.8 arithmetic, ReLU and lookup-table tanh and sigmoid activations, a trainable one-hidden-layer perceptron, and int8 affine-quantized inference (the TFLite/CMSIS-NN model: requantizer, dense layer, quantized ReLU and activation tables). 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
[]
= "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.
Quick start
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 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 and g++. 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) |
src/activation.rs |
cpp/activationFunctions.hpp, cpp/lookupTable.hpp, cpp/interpolate.hpp: ReLU, 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: one-hidden-layer MLP, SGD with momentum, batch size 1 |
src/rand.rs |
a re-implementation of glibc rand(), which the C++ example uses for weight init and sampling |
examples/xor.rs |
examples/xor/xor.cpp |
examples/iris.rs |
examples/iris/iris.cpp |
src/int8.rs |
cpp/qaffine.hpp, cpp/qdense.hpp, cpp/qactivations.hpp: requantizer, int8 dense layer, quantized ReLU, activation tables |
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 |
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.rsruns thexorexample in each format and compares its stdout and the two CSV files byte-for-byte with the goldens intests/golden/<format>/. The 2.4-2.6 MBnn_fixed_xor.txtis 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 inxornet.hset to 8.tests/iris_golden.rsdoes the same for iris againsttests/golden/iris/, andtests/int8_xor_golden.rsfor the int8 example againsttests/golden/int8_xor/.TINYMIND_REFrecords the tinymind commit they were last verified against.scripts/compare-with-cpp.shbuilds the C++ examples from a tinymind checkout (xor in both formats, iris, int8 xor) with the flags their Makefiles use, runs both programs, and runscmpon every output file. It also checks thatdata/iris.datais still tinymind's file. It exits nonzero if anything differs.tests/qformat_oracle.rs, ported from tinymind's qformat oracle, checks everyQValueoperation 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 theQValueoperations match their definitions (division: that it never panics) and that tanh and sigmoid stay within their tables' range. scripts/extract-luts.pygeneratessrc/lut.rsfrom tinymind'scpp/lookupTables.cpp; CI regenerates it and fails if the tables differ.- CI runs all of these on every pull request (the
cpp-equivalencejob checks out tinymind atTINYMIND_REFfor the compare script and the table check), and branch protection blocks merging unless they pass.
License
MIT. See LICENSE and NOTICE.