Expand description
§tflite-c-rs
A small, safe Rust wrapper around the TensorFlow Lite C API that resolves
the shared library at runtime with libloading instead of linking
against it at build time. The crate ships no native code, no build.rs,
no -sys crate — it dynamically opens a TFLite shared object that you
(or your platform package manager) provide separately, then offers a
tidy, RAII-driven API on top of the resolved C symbols.
§Why runtime dynamic loading?
The TensorFlow Lite C library is famously awkward to link statically:
its build system is Bazel-only, the released prebuilt binaries are
platform-specific, and cross-compilation against it tends to drag the
entire TensorFlow source tree along for the ride. For many real-world
deployments — embedded Linux, edge accelerators, vendor-shipped
NPU stacks — the path of least resistance is to ship the vendor’s
prebuilt .so/.dylib/.dll and pick it up at runtime.
That is exactly what this crate does. It uses libloading to dlopen
the TFLite C library, caches the symbols it needs, and exposes a small
safe surface (Model, InterpreterOptions, Interpreter, Tensor,
ExternalDelegate) on top of it.
§Quick start
use tflite_c::{TfLiteLibrary, Model, InterpreterOptions, Interpreter};
let lib = TfLiteLibrary::load_default()?;
let model = Model::from_file("model.tflite", lib.clone())?;
let mut options = InterpreterOptions::new(lib.clone());
options.num_threads(2);
let mut interp = Interpreter::new(model, options)?;
// Fill input 0 with whatever bytes your model expects.
interp.input_mut(0)?.data_mut()?.fill(0);
interp.invoke()?;
let out = interp.output(0)?;
let probs = out.to_vec_f32()?;
println!("first prob: {}", probs[0]);§Threading
Interpreter is Send + Sync, but TensorFlow Lite’s C interpreter is
not thread-safe in itself. The standard pattern is to share via
Arc<Mutex<Interpreter>> (or Arc<parking_lot::Mutex<Interpreter>>) and
serialize every call into a given interpreter.
§Relationship to the upstream C API
Type and function names that begin with TfLite… come from the upstream
header c_api.h
and the external-delegate header
c_api_experimental.h.
This crate covers the minimum set required to load a model, run
inference, and read results — enough for v0.0.1.
Re-exports§
pub use crate::error::Error;pub use crate::error::Result;pub use crate::ffi::TfLiteQuantizationParams;pub use crate::ffi::TfLiteStatus;pub use crate::ffi::TfLiteType;pub use crate::interpreter::Interpreter;pub use crate::interpreter::InterpreterOptions;pub use crate::library::ExternalDelegate;pub use crate::library::ExternalDelegateBuilder;pub use crate::library::TfLiteLibrary;pub use crate::model::Model;pub use crate::tensor::Tensor;pub use crate::tensor::TensorMut;
Modules§
- error
- Error type returned by fallible operations in this crate.
- ffi
- Raw FFI types and function-signature aliases for the TensorFlow Lite C API.
- interpreter
- Safe wrappers around
TfLiteInterpreterOptionsandTfLiteInterpreter. - library
- Runtime loader for the TensorFlow Lite C shared library.
- model
- Loaded
TfLiteModelhandle. - tensor
- Borrowed views into a
crate::Interpreter’s input and output tensors.