| title | Introduction |
|---|
import Image from 'next/image'; import { Callout, Card, Cards, Steps, Code } from 'nextra/components'; import Ort from '../components/Ort'; import { CRATE_VERSION } from '../constants';
is an open-source Rust binding for ONNX Runtime.
{CRATE_VERSION}. This version is production-ready (just not API stable) and we recommend new & existing projects use it.
makes it easy to deploy your machine learning models to production via ONNX Runtime, a hardware-accelerated inference engine. With + ONNX Runtime, you can run almost any ML model (including ResNet, YOLOv8, BERT, LLaMA) on almost any hardware, often far faster than PyTorch, and with the added bonus of Rust's efficiency.
ONNX is an interoperable neural network specification. Your ML framework of choice -- PyTorch, TensorFlow, Keras, PaddlePaddle, etc. -- turns your model into an ONNX graph comprised of basic operations like MatMul or Add. This graph can then be converted into a model in another framework, or inferenced directly with ONNX Runtime.
Converting a neural network to a graph representation like ONNX opens the door to more optimizations and broader accelerator support. ONNX Runtime can significantly improve the inference speed/latency of most models and enable hardware acceleration with NVIDIA CUDA & TensorRT, Intel OpenVINO, Qualcomm QNN, Huawei CANN, and much more.
is the Rust gateway to ONNX Runtime, allowing you to infer your ONNX models via an easy-to-use and ergonomic API. Many commercial, open-source, & research projects use in some pretty serious production scenarios to boost inference performance:
- Bloop's semantic code search feature is powered by .
- SurrealDB's powerful SurrealQL query language supports calling ML models, including ONNX graphs through .
- Google's Magika file type detection library is powered by .
- Wasmtime, an open-source WebAssembly runtime, supports ONNX inference for the WASI-NN standard via .
rust-bertimplements many ready-to-use NLP pipelines in Rust à la Hugging Face Transformers with bothtch& backends.- Supabase's edge functions AI compatibility is powered by .
If you have a supported platform (and you probably do), installing couldn't be any simpler! Just add it to your Cargo dependencies:
[dependencies]
ort = "=2.0.0-rc.12"Your model will need to be converted to an ONNX graph before you can use it.
- The awesome folks at Hugging Face have a guide to export 🤗 Transformers models to ONNX with 🤗 Optimum.
- For any PyTorch model:
torch.onnx - For
scikit-learnmodels:sklearn-onnx - For TensorFlow, Keras, TFlite, & TensorFlow.js:
tf2onnx - For PaddlePaddle:
Paddle2ONNX
Once you've got a model, load it in by creating a Session:
use ort::session::{builder::GraphOptimizationLevel, Session};
let mut model = Session::builder()?
.with_optimization_level(GraphOptimizationLevel::Level3)?
.with_intra_threads(4)?
.commit_from_file("yolov8m.onnx")?;Prepare your inputs, then run() the session to perform inference.
let outputs = model.run(ort::inputs!["image" => image])?;
let predictions = outputs["output0"].try_extract_array::<f32>()?;
...There are some more useful examples in the repo!
Use execution providers to enable hardware acceleration in your app and unlock the full power of your GPU or NPU.
Control where and when inputs/outputs end up with IoBinding to maximize I/O efficiency.
Deploy your application to WASM with 's tract or candle backends.
We'd love to see what you've made with ! Show off your project in GitHub Discussions or on our Discord.
