Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microtensor

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.

rust deps license


Why

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 principleslinear is y = x·Wᵀ + b matching PyTorch's nn.Linear weight layout [out, in]; relu and a numerically-stable softmax round it out.
  • Round-tripsserialize_f32 writes 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.

Try it

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 values

Or containerized:

docker build -t microtensor . && docker run --rm microtensor   # runs the demo

Library

use 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);

Scope (deliberately small)

  • 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.

Tests

cargo test          # 4 integration tests + 1 doctest
cargo clippy -- -D warnings
cargo fmt --check

The 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

About

Load .safetensors and run a forward pass from scratch in Rust — zero dependencies.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages