Export and Serving¶
Jacobi supports saving weights for training restarts, exporting standalone inference graphs for deployment, and serving compiled models over HTTP.
Safetensors Checkpoints¶
Every #[module] saves and reloads PyTorch-compatible .safetensors files using dotted parameter paths such as l1.w and l1.b, preserving native BF16, F16, FP8, I16, I8, and F32 dtypes without upcasting.
To store custom __metadata__ fields (such as step count or model config JSON), use io::safetensors::save_with_metadata(&model, metadata, "checkpoint.safetensors")? and inspect them later without loading tensor data via io::safetensors::read_metadata("checkpoint.safetensors")?.
For multi-file checkpoints, io::safetensors::load_sharded("model.safetensors.index.json") loads all shards into a single map, and io::safetensors::load_from_map(&model, &map) hydrates any #[module].
ONNX Export¶
Export any #[module] forward pass to a self-contained binary .onnx file (ONNX IR v9, Opset 18) with named weight initializers and no extra dependencies.
let sample_x = Tensor::randn(&[1, 4]);
model.export_onnx(&sample_x, |m, x| m.forward(&x), "model.onnx")?;
The exported file runs directly in ONNX Runtime, TensorRT, CoreML, OpenVINO, and ONNX Runtime Web. If you want the serialized protobuf bytes in memory instead of writing to disk, call io::onnx::trace_export_onnx(&model, &sample_x, |m, x| m.forward(&x))?.
StableHLO MLIR Export¶
For small models, export a single standalone .mlir file with weights baked in.
For large models such as LLMs, keep weights in binary .safetensors and export a compact graph-only .mlir file (around 226 KB for a 24-layer Qwen2.5-0.5B decode step) via io::export::export_mlir_with_weights.
use jacobi::io::export;
export::export_mlir_with_weights(
&model,
&sample_x,
|m, x| m.forward(&x),
"model.mlir",
"model.safetensors",
)?;
To inspect or compile MLIR text in memory, call io::export::trace_export_mlir or io::export::trace_export_mlir_external.
HTTP Inference Server and Ecosystem Interop¶
Serve either a standalone .mlir file or an external-weight .mlir + .safetensors pair over HTTP JSON (GET /, GET /health, POST /predict). Weights upload to the accelerator once at startup and stay resident in device memory with zero per-request copies. By default the server binds to loopback (127.0.0.1) and rejects request bodies larger than 64 MiB. Use serve_mlir_on, serve_mlir_with_weights_on, or JACOBI_SERVE_HOST when you intentionally want to bind another interface.
use jacobi::io::serve;
// Standalone baked .mlir (binds 127.0.0.1:8080 by default)
serve::serve_mlir("model.mlir", 8080)?;
// Large model with external .safetensors weights
serve::serve_mlir_with_weights("model.mlir", "model.safetensors", 8080)?;
// Explicit interface binding
serve::serve_mlir_on("model.mlir", "0.0.0.0", 8080)?;
For in-process serving without HTTP overhead, load directly into io::serve::MlirRunner::load_with_weights("model.mlir", "model.safetensors")? and call .run(&feeds)?.
If you want to deploy a Jacobi-trained LLM into vLLM or SGLang, export standard Hugging Face .safetensors checkpoints (preserving native BF16, F16, or FP8 weights alongside config.json and tokenizer.json) rather than .mlir, because vLLM and SGLang load PyTorch/Hugging Face safetensors weights directly into their own PagedAttention and FlashInfer runtimes. Export .mlir when targeting XLA/PJRT, IREE, or TPU runtimes, and .onnx when targeting ONNX Runtime, TensorRT, or CoreML.