sonos/tract
Tiny, no-nonsense, self-contained, Tensorflow and ONNX inference
What is tract?
tract is Sonos’ open‑source neural‑network inference engine written in Rust. It can load models in ONNX, NNEF (with Sonos‑specific extensions called tract‑OPL), TensorFlow Lite and legacy TensorFlow‑1 formats, optimise them, and run them on a huge range of hardware – from tiny ARM micro‑controllers to desktop CPUs, Apple Metal GPUs, NVIDIA CUDA GPUs, and even in the browser via WebAssembly.
Key capabilities
| Capability | Details |
|---|---|
| Model formats | ONNX, NNEF (+ tract‑OPL), TFLite (legacy), TF‑1 frozen graphs. |
| Runtimes | CPU (hand‑rolled SIMD), Apple Metal, NVIDIA CUDA, WebAssembly. |
| Streaming / pulsification | Convert a batch model into a low‑latency “pulsified” version that processes fixed‑size chunks, ideal for wake‑word and streaming ASR. |
| Cross‑platform | Same TypedModel IR works on all targets; you can optimise on one device and ship the compiled model to another. |
| Python bindings | A tract PyPI package that mirrors the Rust API, letting Python users run the same engine. |
| Benchmarks | Nightly automated benchmarks on CPUs, ARM boards, RISC‑V, Apple/NVIDIA GPUs, and WASM, with public charts for speech, vision and LLM workloads. |
Typical use‑cases
- Embedded speech‑recognition (wake‑word detection, streaming ASR) on low‑power ARM chips.
- On‑device computer vision such as MobileNet, YOLO, face‑recognition.
- Running LLMs or diffusion models on desktop GPUs or Metal‑enabled Macs.
- Browser inference via WebAssembly for client‑side ML demos.
- Any scenario needing a tiny, self‑contained runtime – you ship only the core crates and the serialized NNEF/OPL model, no heavy framework dependencies.
Getting started (Rust)
# Cargo.toml
[dependencies]
tract = "0.20" # or latest version on crates.io
use tract::prelude::*;
fn main() -> TractResult<()> {
// Load an ONNX model
let model = tract::onnx()?.load("mobilenetv2-7.onnx")?.into_model()?;
// Choose a runtime (default = CPU SIMD)
let runtime = tract::runtime_for_name("default")?;
// Optimise & compile
let runnable = runtime.prepare(model)?;
// Run with a single input tensor (convert your data to a tract tensor first)
let result = runnable.run([input.tract()?])?;
println!("{:?}", result);
Ok(())
}
Python
pip install tract
import tract
model = tract.load_onnx('mobilenetv2-7.onnx')
model = model.optimize().into_runnable()
result = model.run(input_tensor)
How to ship a model
- Convert the original model to the intermediate NNEF/OPL format with the CLI:
tract model.onnx dump --nnef model.nnef.tgz - Deploy only the runtime crates you need (
tract-core,tract-nnef, plustract-onnx-oplortract-pulse-oplif required). This keeps the binary footprint tiny – no protobuf, no Python runtime.
Ecosystem & resources
- Crates:
tract-core,tract-onnx,tract-nnef,tract-linalg(CPU),tract-metal,tract-cuda,tract-pulse, etc. - Documentation:
doc/intro.md,doc/cli-recipe.md,doc/overview.md(streaming), plus generated API docs on docs.rs. - Examples: ready‑to‑run demos for MobileNet, TFLite, causal LLMs, speech‑recognition, Stable Diffusion, YOLO, WebAssembly, etc.
- Blog post: Sonos engineering blog explains the design and optimisation strategies.
- Benchmarks: live benchmark dashboard at https://sonos.github.io/tract/bench/.
License
Dual‑licensed under Apache 2.0 or MIT (your choice). Some generated protobuf files from TensorFlow and ONNX retain their original licenses.
tract gives developers a fast, portable, and production‑ready way to run modern neural‑network models on anything from a microcontroller to a high‑end GPU, with special support for streaming inference needed in voice‑activated products.
Related
- Project
- Project
- Project
- Project