RunNX
A minimal, mathematically verifiable ONNX runtime implementation in Rust.

Overview
Fast, fearless, and formally verified ONNX in Rust.
This project provides a minimal, educational ONNX runtime implementation focused on:
- Simplicity: Easy to understand and modify
- Verifiability: Formal mathematical verification using Why3 and property-based testing
- Performance: Efficient operations using ndarray
- Safety: Memory-safe Rust implementation with mathematical guarantees
Features
- ✅ Dual Format Support: JSON and binary ONNX protobuf formats
- ✅ Auto-detection: Automatic format detection based on file extension
- ✅ Basic tensor operations (
Add,Mul,MatMul,Conv,Relu,Sigmoid,Reshape,Transpose) - ✅ Formal mathematical specifications with Why3
- ✅ Property-based testing for mathematical correctness
- ✅ Runtime invariant verification
- ✅ Model loading and validation
- ✅ Inference execution
- ✅ Error handling and logging
- ✅ Benchmarking support
- ✅ Async support (optional)
- ✅ Command-line runner
- ✅ Comprehensive examples
Quick Start
Prerequisites
RunNX requires the Protocol Buffers compiler (protoc) to build:
# Ubuntu/Debian
# macOS
# Windows
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
Basic Usage
use ;
// Load a model (supports both JSON and ONNX binary formats)
let model = from_file?; // Auto-detects format
// Or explicitly:
// let model = Model::from_onnx_file("model.onnx")?; // Binary ONNX
// let model = Model::from_json_file("model.json")?; // JSON format
// Create input tensor
let input = from_array;
// Run inference
let outputs = model.run?;
// Get results
let result = outputs.get.unwrap;
println!;
Saving Models
use Model;
let model = /* ... create or load model ... */;
// Save in different formats
model.to_file?; // Auto-detects format from extension
model.to_onnx_file?; // Explicit binary ONNX format
model.to_json_file?; // Explicit JSON format
Command Line Usage
# Run inference on a model (supports both .onnx and .json files)
# Run with async support
Architecture
The runtime is organized into several key components:
Core Components
- Model: ONNX model representation and loading
- Graph: Computational graph with nodes and edges
- Tensor: N-dimensional array wrapper with type safety
- Operators: Implementation of ONNX operations
- Runtime: Execution engine with optimizations
File Format Support
RunNX supports both JSON and binary ONNX protobuf formats:
📄 JSON Format
- Human-readable: Easy to inspect and debug
- Text-based: Can be viewed and edited in any text editor
- Larger file size: More verbose due to text representation
- Extension:
.json
🔧 Binary ONNX Format
- Standard format: Official ONNX protobuf serialization
- Compact: Smaller file sizes due to binary encoding
- Interoperable: Compatible with other ONNX runtime implementations
- Extension:
.onnx
🎯 Auto-Detection
The Model::from_file() method automatically detects the format based on file extension:
.onnxfiles → Binary ONNX protobuf format.jsonfiles → JSON format- Other extensions → Attempts JSON parsing as fallback
For explicit control, use:
Model::from_onnx_file()for binary ONNX filesModel::from_json_file()for JSON files
Supported Operators
| Operator | Status | Notes |
|---|---|---|
Add |
✅ | Element-wise addition |
Mul |
✅ | Element-wise multiplication |
MatMul |
✅ | Matrix multiplication |
Conv |
✅ | 2D Convolution |
Relu |
✅ | Rectified Linear Unit |
Sigmoid |
✅ | Sigmoid activation |
Reshape |
✅ | Tensor reshaping |
Transpose |
✅ | Tensor transposition |
Examples
Format Compatibility Demo
use *;
Simple Linear Model
use ;
use Array2;
Model Loading and Inference
# Format compatibility demonstration
# Format conversion between JSON and ONNX binary
# Simple model operations
# Formal verification examples
# Tensor operations
use ;
use HashMap;
Performance
The runtime includes benchmarking capabilities:
# Run benchmarks
# Generate HTML reports
Example benchmark results:
- Basic operations: ~10-50 µs
- Small model inference: ~100-500 µs
- Medium model inference: ~1-10 ms
Formal Verification
RunNX includes comprehensive formal verification capabilities to ensure mathematical correctness:
🔬 Mathematical Specifications
The runtime includes formal specifications for all tensor operations using Why3:
(** Addition operation specification *)
function add_spec (a b: tensor) : tensor
requires { valid_tensor a /\ valid_tensor b }
requires { a.shape = b.shape }
ensures { valid_tensor result }
ensures { result.shape = a.shape }
ensures { forall i. 0 <= i < length result.data ->
result.data[i] = a.data[i] + b.data[i] }
🧪 Property-Based Testing
Automatic verification of mathematical properties:
use ;
// Test addition commutativity: a + b = b + a
let result1 = tensor_a.add_with_contracts?;
let result2 = tensor_b.add_with_contracts?;
assert_eq!;
// Test ReLU idempotency: ReLU(ReLU(x)) = ReLU(x)
let relu_once = tensor.relu_with_contracts?;
let relu_twice = relu_once.relu_with_contracts?;
assert_eq!;
🔍 Runtime Verification
Dynamic checking of invariants during execution:
use InvariantMonitor;
let monitor = new;
let result = tensor.add?;
// Verify numerical stability and bounds
assert!;
🎯 Verified Properties
The formal verification system proves:
- Addition: Commutativity, associativity, identity
- Matrix Multiplication: Associativity, distributivity
- ReLU: Idempotency, monotonicity, non-negativity
- Sigmoid: Boundedness (0, 1), monotonicity, symmetry
- Numerical Stability: Overflow/underflow prevention
📊 Running Formal Verification
# Install Why3 (optional, for complete formal proofs)
# Run all verification (tests + proofs)
# Run only property-based tests (no Why3 required)
# Run verification example
# Generate verification report
Development
Quick Development with Justfile
RunNX includes a Justfile with convenient shortcuts for common development tasks:
# Install just command runner (one time setup)
# Show all available commands
# Quick development cycle
# Code quality
# Documentation
# Benchmarks
# Formal verification
# CI simulation
Alternatively, if you don't have just installed, use the included shell script:
# Show all available commands
# Quick development cycle
Running Tests
# Run all tests
# or with just
# Run tests with logging
RUST_LOG=debug
# Run specific test
Building Documentation
# Build and open documentation
# or with just
# Build with private items
Contributing
We welcome contributions! Please follow our development quality standards:
- Fork the repository
- Create a feature branch
- Make your changes following our Development QA Guidelines
- Add tests and documentation
- Run quality checks:
./scripts/quality-check.sh - Commit your changes (pre-commit hooks will run automatically)
- Submit a pull request
Development Quality Assurance
RunNX uses automated quality assurance tools to maintain code quality:
- Pre-commit hooks: Automatically run formatting, linting, and tests before each commit
- Code formatting: Consistent style enforced by
rustfmt - Linting: Comprehensive checks with
clippy(warnings treated as errors) - Comprehensive testing: Unit tests, integration tests, property-based tests, and doc tests
- Build verification: Ensures all code compiles successfully
For detailed information, see Development QA Guidelines.
To run quality checks manually:
# Run all quality checks with auto-fixes
# Or run individual checks
License
This project is licensed under
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Acknowledgments
- ONNX - Open Neural Network Exchange format
- ndarray - Rust's
ndarraylibrary - Candle - Inspiration for some design patterns
Roadmap
✅ Completed
- Dual Format Support: Both JSON and binary ONNX protobuf formats
- Auto-detection: Automatic format detection based on file extension
- Core Operators: Add, Mul, MatMul, Conv, ReLU, Sigmoid, Reshape, Transpose
- Formal Verification: Mathematical specifications with Why3
- CLI Tool: Command-line runner for model inference
🚧 Planned
- Add more operators (Softmax, BatchNorm, etc.)
- GPU acceleration support
- Quantization support
- Model optimization passes
- WASM compilation target
- Python bindings