Load a .safetensors file and run a forward pass — from scratch, in Rust, with
zero dependencies. No PyTorch, no ndarray, not even serde: the safetensors
container is parsed by hand and the ops (linear, relu, softmax) are a few
loops over f32. It's the layer underneath a framework, made legible.
safetensors is Hugging Face's tensor format (itself written in Rust). Most code
reaches for a framework to read it and multiply a matrix. This crate does neither —
it shows the mechanics end to end:
- Hand-written container parser — reads the 8-byte header length, the JSON
header (via a ~200-line dependency-free JSON reader in
src/json.rs), and the raw little-endian tensor bytes. - Ops from first principles —
linearisy = x·Wᵀ + bmatching PyTorch'snn.Linearweight layout[out, in];reluand a numerically-stablesoftmaxround it out. - Round-trips —
serialize_f32writes the format too, so the test suite builds a tensor, serializes it, reads it back, and checks the bytes and the math.
Pairs with the sibling sift service: sift runs a model from Hugging
Face through the framework; microtensor reads the same kind of .safetensors
weights and does the forward pass by hand. Framework at the top, first principles
at the bottom.
cargo run -- demo
# serialized 160 bytes of safetensors
# x = [1.0, 2.0, 3.0]
# y=xWᵀ+b = [-1.9, 2.8]
# relu(y) = [0.0, 2.8]
# softmax = [0.0090133, 0.99098665]
cargo run -- model.safetensors # list tensors (name, dtype, shape)
cargo run -- model.safetensors weight # decode one: shape, mean, first valuesOr containerized:
docker build -t microtensor . && docker run --rm microtensor # runs the demouse microtensor::{SafeTensors, Tensor, linear, softmax};
let st = SafeTensors::from_file("model.safetensors")?;
let w = st.tensor("weight")?; // [out, in]
let b = st.tensor("bias")?; // [out]
let x = Tensor::vector(vec![1.0, 2.0, 3.0]);
let logits = linear(&x, &w, Some(&b))?; // [out]
let probs = softmax(&logits);- Decodes F32 / F64; F16/BF16 return a clear error (not implemented — this is a teaching-grade core, not a runtime).
- Ops:
linear(1-D and batched 2-D),relu,softmax. Easy to extend.
cargo test # 4 integration tests + 1 doctest
cargo clippy -- -D warnings
cargo fmt --checkThe from-scratch numerical core of a small polyglot portfolio — Go for networking, Python for high-level AI, TypeScript for test/QA, Rust here. — Nicholas Martins · github.com/nickmartins-lambda