axonml-onnx
Overview
axonml-onnx provides ONNX (Open Neural Network Exchange) import and export functionality for the AxonML machine learning framework. It enables interoperability with PyTorch, TensorFlow, and other ML frameworks by supporting ONNX model loading and saving up to opset version 17.
Features
- ONNX Import - Load ONNX models and convert to executable AxonML representation
- ONNX Export - Export AxonML models to ONNX format for deployment
- Operator Support - Implementations for 40+ common ONNX operators including activations, math, and neural network ops
- Protobuf Parsing - Full ONNX protobuf structure support with JSON fallback for testing
- Graph Execution - Execute imported models with automatic operator dispatch
- Feedforward Export - Helper utilities for exporting simple feedforward networks
- Error Handling - Comprehensive error types for debugging import/export issues
Modules
| Module | Description |
|---|---|
parser |
ONNX file parsing and model import functionality |
export |
OnnxExporter builder and export utilities |
model |
OnnxModel representation for inference |
operators |
ONNX operator implementations (Relu, MatMul, Conv, etc.) |
proto |
ONNX protobuf structure definitions |
error |
Error types for ONNX operations |
Supported Operators
| Category | Operators |
|---|---|
| Activations | Relu, Sigmoid, Tanh, Softmax, LeakyRelu, Gelu |
| Math | Add, Sub, Mul, Div, MatMul, Gemm, Sqrt, Pow, Exp, Log |
| Shape | Reshape, Transpose, Flatten, Squeeze, Unsqueeze, Concat, Gather |
| Reduction | ReduceSum, ReduceMean, ReduceMax |
| Neural Network | Conv*, MaxPool*, AveragePool*, BatchNormalization, Dropout |
| Comparison | Equal, Greater, Less, Clip |
| Utility | Constant, Identity, Cast, Shape |
* Partial implementation - basic structure only
Usage
Add the dependency to your Cargo.toml:
[]
= "0.1.0"
Importing an ONNX Model
use ;
use Tensor;
use HashMap;
// Import model from file
let model = import_onnx?;
// Inspect model
println!;
println!;
println!;
// Run inference
let mut inputs = new;
let input_tensor = from_vec.unwrap;
inputs.insert;
let outputs = model.forward?;
let output = outputs.get.unwrap;
Importing from Bytes
use import_onnx_bytes;
let bytes = read?;
let model = import_onnx_bytes?;
Exporting a Model
use ;
use TensorDataType;
use Tensor;
use HashMap;
// Create exporter
let mut exporter = new
.with_producer
.with_doc_string;
// Add input
exporter.add_input;
// Add nodes (operators)
exporter.add_node;
// Add output
exporter.add_output;
// Export to file
exporter.export?;
// Or export to bytes
let bytes = exporter.to_bytes?;
Adding Weights to Export
use ;
use TensorDataType;
use Tensor;
let mut exporter = new;
// Add input
exporter.add_input;
// Add weight initializer
let weights = from_vec.unwrap;
exporter.add_initializer;
// Add bias initializer
let bias = from_vec.unwrap;
exporter.add_initializer;
// Add Gemm node with attributes
let mut attrs = new;
attrs.insert;
exporter.add_node;
// Add output
exporter.add_output;
Exporting Feedforward Networks
use export_feedforward;
use Tensor;
// Define layers (in_features, out_features)
let layers = vec!;
// Prepare weights and biases
let weights = vec!;
let biases = vec!;
let exporter = export_feedforward?;
exporter.export?;
Working with Attributes
use AttributeValue;
use HashMap;
let mut attrs = new;
// Float attribute
attrs.insert;
// Integer attribute
attrs.insert;
// Integer array attribute
attrs.insert;
// Float array attribute
attrs.insert;
exporter.add_node;
Error Handling
use ;
match import_onnx
Constants
use ;
println!; // 17
println!; // 8
Tests
Run the test suite:
License
Licensed under either of:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.