axonml-serialize
Overview
axonml-serialize handles model state I/O for AxonML: named-parameter
StateDicts, Checkpoints with full training state, and format conversion
for PyTorch / ONNX interop. The native .axonml format is bincode-encoded
binary; JSON and SafeTensors (behind the safetensors feature) are also
supported. Format is detected from the file extension with magic-byte
fallback.
Features
- Multiple Formats — native bincode
.axonml,.json,.safetensors(feature-gated) - State Dictionaries —
StateDictwithfrom_module,insert,get,entries,keys,merge,filter_prefix,strip_prefix,add_prefix,remove,set_metadata/get_metadata,total_params,size_bytes,summary - Training Checkpoints —
Checkpoint+CheckpointBuilder+TrainingStatewith loss / val-loss / lr / custom metric history, best-metric tracking, epoch / step counters, ISO-8601 timestamp, config map - Format Detection —
detect_format(path)by extension,detect_format_from_bytes(bytes)by magic bytes;Format::is_binary,Format::supports_streaming,Format::extension,Format::name,Format::all - PyTorch Conversion —
from_pytorch_key,to_pytorch_key,pytorch_layer_mapping,convert_from_pytorch,transpose_linear_weights - ONNX Utilities —
to_onnx_shape/from_onnx_shape(dynamic batch dim handling),OnnxOpTypewithparse_op/as_str - High-Level API —
save_model(&model, path)/load_model(&model, path)(name-matched param load with positional fallback),save_state_dict/load_state_dict,save_checkpoint/load_checkpoint
Feature Flags
| Flag | Effect |
|---|---|
safetensors |
Enables .safetensors save/load (f32 / f16 / bf16 / f64 input), pulls safetensors = "0.3" and half |
Modules
| Module | Description |
|---|---|
state_dict |
TensorData, StateDictEntry, StateDict |
checkpoint |
Checkpoint, CheckpointBuilder, TrainingState |
format |
Format enum and detection helpers |
convert |
PyTorch / ONNX conversion utilities, OnnxOpType |
Usage
Add the dependency to your Cargo.toml:
[]
= "0.6.1"
# Or with SafeTensors:
= { = "0.6.1", = ["safetensors"] }
Saving and Loading Models
use ;
use Linear;
// Save a model (format detected from extension)
let model = new;
save_model?; // Binary format
save_model?; // JSON format
// save_model(&model, "model.safetensors")?; // Requires `safetensors` feature
// Inspect the state dict directly
let sd = load_state_dict?;
println!;
println!;
// Or load weights back into a model (name-matched, positional fallback)
let target = new;
let loaded = load_model?;
println!;
Working with State Dictionaries
use ;
// Create a state dictionary
let mut state_dict = new;
let weights = TensorData ;
state_dict.insert;
let bias = TensorData ;
state_dict.insert;
// Query the state dictionary
assert!;
println!;
// Filter / rename
let linear_params = state_dict.filter_prefix;
let stripped = state_dict.strip_prefix;
let prefixed = state_dict.add_prefix;
Training Checkpoints
use ;
// Track training state
let mut training_state = new;
training_state.record_loss;
training_state.record_loss;
training_state.record_val_loss;
training_state.record_lr;
training_state.record_metric;
training_state.update_best; // lower is better
training_state.next_epoch;
training_state.next_step;
// Average last N losses
let smoothed = training_state.avg_loss;
// Build checkpoint
let checkpoint = builder
.model_state
.optimizer_state
.training_state
.rng_state
.epoch
.global_step
.config
.config
.build;
// Save and load checkpoints (bincode)
save_checkpoint?;
let loaded = load_checkpoint?;
println!;
println!;
Format Detection
use ;
// Detect from file extension
assert_eq!;
assert_eq!;
assert_eq!; // default
// Detect from file contents
let bytes = b"{\"key\": \"value\"}";
let format = detect_format_from_bytes;
assert_eq!;
// Format properties
assert!;
assert!;
PyTorch Conversion
use ;
// Convert PyTorch key naming to AxonML
let key = from_pytorch_key;
// Convert entire state dictionary
let axonml_dict = convert_from_pytorch;
// Transpose linear weights if needed (PyTorch uses [out, in])
let transposed = transpose_linear_weights;
ONNX Shape Utilities
use ;
// Convert to ONNX shape (with dynamic batch)
let onnx_shape = to_onnx_shape;
assert_eq!;
// Convert from ONNX shape (replace -1 with default)
let shape = from_onnx_shape;
assert_eq!;
// ONNX operator name mapping
let op = parse_op;
assert_eq!;
State Dictionary Metadata
use StateDict;
let mut state_dict = new;
state_dict.set_metadata;
state_dict.set_metadata;
if let Some = state_dict.get_metadata
Tests
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.