Skip to content

Jacobi

Jacobi

A machine learning framework for Rust compiled via XLA/PJRT to CPUs, GPUs (CUDA, ROCm), and TPUs, with native AMX/SME2 and Metal acceleration on Apple Silicon.

$cargo add jacobi
use jacobi::prelude::*;

#[module]
struct Mlp {
    l1: Linear,
    l2: Linear,
}

impl Mlp {
    fn forward(&self, x: &Tensor) -> Tensor {
        self.l2.forward(&self.l1.forward(x).tanh())
    }
}

let step = jit(move |(mut m, mut o): (Mlp, AdamW<Mlp>)| {
    let (loss, grads) = value_and_grad(
        |p| nn::mse_loss(&p.forward(&x), &y),
        &m,
    );
    o.step(&mut m, &grads);
    (m, o, loss)
});

Benchmarks

1-1024-1024-1 tanh network for 200 full-batch steps on 2,048 points of sin(x) in f32 (median of 5 runs after 1 warm-up with identical initial weights and matching convergence 0.3586 -> 0.0133).

Framework Nvidia B300 Nvidia B300 CPU Nvidia B200 Nvidia B200 CPU Apple M5 Max GPU Apple M5 Max CPU
Jacobi 0.1.0 (jit + for_loop) 0.0139s 2.98s 0.0171s 2.66s 0.14s 1.13s
JAX 0.12.1 (jax.jit + fori_loop) 0.0147s 3.31s 0.0175s 2.67s N/A 1.22s
PyTorch (eager) 0.0856s 24.29s 0.1042s 2.73s 0.31s 2.23s
PyTorch (torch.compile) N/A N/A N/A N/A 0.31s 2.30s
MLX 0.32.5 N/A N/A N/A N/A 0.15s 3.86s

Random-weight architecture replicas benchmarked on live PleIAs/SYNTH next-token cross-entropy at 4,096 token context in FP8 across single-GPU, 8x H200 Expert Parallel, and 16x H200 Data + Expert Parallel clusters, plus batched RL Rollout (4,096-token prefill + 1,024-token decode).

Model Hardware Parallelism Mode Precision Batch Peak RAM (dev0 x N) Throughput 15-Step CE Loss Cost / 1T
Gemma-4-E4B (7.4B) 1x B200 180GB Single GPU Forward/Prefill FP8 16 22 GB 154,127 tok/s N/A $9k
Gemma-4-E4B (7.4B) 1x B200 180GB Single GPU Train FP8 8 155 GB 35,021 tok/s 12.83 -> 8.85 $40k
Gemma-4-E4B (7.4B) 1x B200 180GB Single GPU Rollout/Decode FP8 512 20 GB 30,999 tok/s N/A $45k
Inkling-2-Mini (26B) 8x H200 141GB Expert Parallel Forward/Prefill FP8 192 210 GB 356,044 tok/s N/A $22k
Inkling-2-Mini (26B) 8x H200 141GB Expert Parallel Train FP8 192 1,015 GB 105,420 tok/s 12.66 -> 8.32 $74k
Inkling-2-Mini (26B) 8x H200 141GB Expert Parallel Rollout/Decode FP8 2,048 150 GB 63,084 tok/s N/A $123k
DeepSeek-V4-Flash (283B) 16x H200 141GB Data + Expert Parallel Forward/Prefill FP8 64 744 GB 217,234 tok/s N/A $72k
DeepSeek-V4-Flash (283B) 16x H200 141GB Data + Expert Parallel Train (Rank-32 EfficientAdamW) FP8 80 2,115 GB 52,966 tok/s 12.57 -> 10.62 $294k
DeepSeek-V4-Flash (283B) 16x H200 141GB Data + Expert Parallel Rollout/Decode FP8 2,048 797 GB 42,924 tok/s N/A $362k

EfficientAdamW vs Full AdamW (Multi-Node RDMA, 16x H200, 4k Context, PleIAs/SYNTH)

Model & Hardware Optimizer State / GPU Peak HBM / GPU Throughput 15-Step CE Loss
DeepSeek-V4-Flash 16L (106B, 16x H200 DP=2 x EP=8, batch 64) Full AdamW 62.56 GB 120.00 GB 57,865 tok/s 12.59 -> 8.15
DeepSeek-V4-Flash 16L (106B, 16x H200 DP=2 x EP=8, batch 64) Rank-32 EfficientAdamW (gap=50, scale=2.5) 0.60 GB (99.0% saved) 58.88 GB 140,018 tok/s (2.42x) 12.59 -> 7.76
DeepSeek-V4-Flash 43L (283B, 16x H200) Full AdamW (EP=16, batch 16 min) >141 GB OOM OOM N/A
DeepSeek-V4-Flash 43L (283B, 16x H200 DP=2 x EP=8, batch 80) Rank-32 EfficientAdamW (gap=50, scale=2.5) 1.59 GB (99.0% saved) 132.19 GB 52,966 tok/s 12.57 -> 10.62

Peak RAM is device-0 peak HBM x GPU count for symmetric SPMD shards. Cost / 1T uses $5.00/GPU-hr (B200) and $3.50/GPU-hr (H200).

Full hardware benchmarks and MFU analysis →

Zero runtime setup

Run cargo add jacobi and go. PJRT plugins download automatically on first use, with no protoc or libclang required.

Hardware backends →

use jacobi::prelude::*;

fn main() -> Result<()> {
    println!("running on {}", backend_name()?);
    let x = Tensor::randn(&[4, 8]);
    println!("mean = {:.4}", x.mean().item()?);
    Ok(())
}

Composable transforms

Combine jit, grad, jvp, hvp, vmap, checkpoint, custom_vjp, for_loop, scan, cond, and switch across plain Rust functions and structs, with automatic buffer donation and async dispatch.

Transforms guide →

// Checkpointed reverse-mode differentiation without unrolling
let dwh = grad(|w| {
    let (h_final, _ys) = scan(&xs, h0.clone(), |h, x_t| {
        let next = (h.matmul(&w) + x_t).tanh();
        (next.clone(), next)
    });
    h_final.sum()
}, &wh);

PyTorch ergonomics

Define models with #[module], Linear, Conv1d/2d/3d, ConvTranspose1d/2d, MultiheadAttention, RoPE, KvCache, BatchNorm2d, GroupNorm, LayerNorm, stateful AdamW and Muon, Ema, and implicit device RNG.

Models and optimizers →

#[module]
struct Block {
    norm: RmsNorm,
    attn: MultiheadAttention,
    proj: Linear,
    heads: usize, // plain config fields pass through automatically
}

manual_seed(42);
let mut opt = AdamW::new(&model, 1e-3).weight_decay(1e-2);

Mixed precision and quantization

Use jit_autocast for BF16 and FP16, train with dynamically scaled FP8 and FP4 matmuls, or store and multiply quantized INT8 and UINT8 tensors with i32 accumulation.

Mixed precision →

let train_step = jit_autocast(Dtype::BF16, move |(mut m, mut o)| {
    let (loss, grads) = value_and_grad(|p| p.loss(&x, &y), &m);
    o.step(&mut m, &grads);
    (m, o, loss)
});

// Or scaled FP8 / FP4 layers
let h = nn::fp8_linear(&self.fc1, &x).relu();

Hugging Face native

Built on the official Rust tokenizers, safetensors, and hf-hub crates, plus Hugging Face Datasets streaming and memory-mapped checkpoint loading.

Hugging Face guide →

let tok = Tokenizer::from_pretrained("gpt2")?;
let ids = tok.encode_tensor("Hello from Jacobi!")?;

let weights = hub::load_safetensors("Qwen/Qwen2.5-0.5B")?;
let ds = hub::load_dataset("roneneldan/TinyStories", "default", "train");
let tokens = ds.tokenize_column(&tok, "text", 0, 100)?;

Export and notebooks

Save PyTorch-compatible .safetensors, export standalone .onnx or StableHLO .mlir, or run interactive notebooks with inline SVG plots in VS Code.

Export and serving →

model.save("model.safetensors")?;
model.export_onnx(&sample_x, |m, x| m.forward(&x), "model.onnx")?;

// Renders an inline SVG chart in VS Code and JupyterLab
Figure::new().title("Loss").plot(&steps, &losses, None)?

Documentation

Start Here

Core Guide

Ecosystem and Examples

  • Hugging FacePretrained tokenizers, safetensors, and Datasets.
  • Export and ServingSafetensors, ONNX, StableHLO MLIR, and HTTP serving.
  • ExamplesComplete MLP, CNN, ViT, Mini GPT, and benchmark walkthroughs.
  • API ReferenceComplete reference for tensors, transforms, layers, optimizers, runtime, and llms.txt / llms-full.txt.