Burn Store
Advanced model storage and serialization for the Burn deep learning framework
A comprehensive storage library for Burn that enables efficient model serialization, cross-framework interoperability, and advanced tensor management.
Features
Core Capabilities
- Burnpack Format - Native Burn format with CBOR metadata, memory-mapped loading, ParamId persistence for stateful training, and no-std support
- SafeTensors Format - Industry-standard format for secure and efficient tensor serialization
- PyTorch Support - Direct loading of PyTorch .pth/.pt files with automatic weight transformation
- Zero-Copy Loading - Memory-mapped files and lazy tensor materialization for optimal performance
- Cross-Framework Support - Seamless PyTorch ↔ Burn model conversion with automatic adaptations
- Flexible Filtering - Load/save specific model subsets with regex, exact paths, or custom predicates
- Tensor Remapping - Rename tensors during load/save for framework compatibility
- No-std Support - Burnpack and SafeTensors formats available in embedded and WASM environments
Advanced Features
- Framework Adapters - Automatic weight transposition and parameter renaming for PyTorch compatibility
- Lazy Transformations - Chain tensor transformations without materializing intermediate data
- Partial Loading - Continue loading even when some tensors are missing
- Custom Metadata - Attach version info, training details, or other metadata to saved models
Quick Start
Basic Save and Load
Burnpack (Native Format)
use ;
// Save a model with metadata
let mut store = from_file
.metadata
.metadata;
model.save_into?;
// Load a model (automatically memory-mapped when available)
let mut store = from_file;
model.load_from?;
Performance: Burnpack provides faster loading times and reduced memory overhead compared to other formats.
Training State Persistence: Burnpack automatically preserves parameter identifiers (ParamId) for stateful training continuation.
SafeTensors
use ;
// Save a model
let mut store = from_file;
model.save_into?;
// Load a model
let mut store = from_file;
model.load_from?;
Filtering Tensors
// Save only encoder layers
let mut store = from_file
.with_regex
.metadata;
model.save_into?;
// Load with multiple filter patterns (OR logic)
let mut store = from_file
.with_regex // Include encoder tensors
.with_regex // OR include any bias tensors
.with_full_path; // OR include specific tensor
model.load_from?;
PyTorch Interoperability
use ;
// Load PyTorch .pth file directly (PyTorchToBurnAdapter is applied automatically)
// Note: skip_enum_variants defaults to true for PytorchStore
let mut store = from_file
.with_top_level_key // Access nested state dict
.allow_partial; // Skip unknown tensors
burn_model.load_from?;
// Load PyTorch model from SafeTensors
let mut store = from_file
.with_from_adapter // Auto-transpose linear weights
.skip_enum_variants // Handle enum variant name differences
.allow_partial; // Skip unknown PyTorch tensors
burn_model.load_from?;
// Save Burn model for PyTorch (with enum variant skipping)
let mut store = from_file
.with_to_adapter // Convert back to PyTorch format
.skip_enum_variants; // Omit enum variants for PyTorch compatibility
burn_model.save_into?;
Contiguous Layer Index Mapping
When loading PyTorch models that use nn.Sequential with mixed layer types (e.g., Conv2d + ReLU),
the layer indices may be non-contiguous because only some layers have parameters:
# PyTorch model with non-contiguous indices
=
Burn models typically expect contiguous indices (fc.0, fc.1, fc.2). The map_indices_contiguous
feature automatically maps non-contiguous indices to contiguous ones:
// PytorchStore: map_indices_contiguous is ON by default
let mut store = from_file;
// fc.0 -> fc.0, fc.2 -> fc.1, fc.4 -> fc.2
// Disable if your model already has contiguous indices
let mut store = from_file
.map_indices_contiguous;
// SafetensorsStore: map_indices_contiguous is OFF by default
let mut store = from_file
.map_indices_contiguous; // Enable for PyTorch-exported safetensors
Tensor Name Remapping
// Simple pattern-based remapping
let mut store = from_file
.with_key_remapping // old_model.X -> new_model.X
.with_key_remapping // X.gamma -> X.weight
.with_key_remapping; // X.beta -> X.bias
// Complex remapping with KeyRemapper
use KeyRemapper;
let remapper = new
.add_pattern? // h.0 -> layer0
.add_pattern?; // attn -> attention
let mut store = from_file
.remap;
// Combining with PyTorch loading
let mut store = from_file
.with_key_remapping // Remove model. prefix
.with_key_remapping; // norm1 -> norm_1
Memory Operations
// Burnpack: Save to memory buffer
let mut store = from_bytes
.with_regex
.metadata;
model.save_into?;
let bytes = store.get_bytes?;
// Burnpack: Load from memory buffer (no-std compatible)
let mut store = from_bytes
.allow_partial;
let result = model.load_from?;
// SafeTensors: Memory operations
let mut store = from_bytes
.with_regex;
model.save_into?;
let bytes = store.get_bytes?;
println!;
if !result.missing.is_empty
Both BurnpackStore and SafetensorsStore support no-std environments when using byte operations
Zero-Copy Loading
For embedded models and large model files, zero-copy loading avoids unnecessary memory allocations by directly referencing the source data instead of copying it.
Embedded Models (Static Data)
use ;
// Embed model weights in the binary at compile time
static MODEL_DATA: & = include_bytes!;
// Zero-copy loading - data stays in binary's .rodata section
let mut store = from_static;
model.load_from?;
The from_static() constructor automatically enables zero-copy mode. Tensor data is sliced directly
from the embedded bytes without heap allocation.
File-Based Zero-Copy
// Memory-mapped file with zero-copy tensor slicing
let mut store = from_file
.zero_copy; // Enable zero-copy slicing
model.load_from?;
When zero_copy(true) is set, the memory-mapped file is wrapped in bytes::Bytes via
from_owner(), enabling O(1) slicing operations.
In-Memory Zero-Copy
use ;
// Create shared bytes for zero-copy
let data: = load_model_bytes;
let shared = from;
let bytes = from_shared;
// Load with zero-copy enabled
let mut store = from_bytes
.zero_copy;
model.load_from?;
When to Use Zero-Copy
| Scenario | Recommendation |
|---|---|
Embedded models (include_bytes!) |
Use from_static() (auto-enabled) |
| Large model files | Use from_file().zero_copy(true) |
| Repeated loading from same bytes | Use from_bytes().zero_copy(true) |
| One-time load, release memory after | Use default (copy mode) |
Note: Zero-copy keeps the source data alive as long as any tensor references it. Use copy mode (default) if you need to release the source file/memory immediately after loading.
Model Surgery and Partial Operations
Burn Store enables sophisticated model surgery operations for selectively loading, saving, and transferring parts of models.
Direct Model-to-Model Transfer
use ;
// Direct transfer - all compatible tensors
let snapshots = model1.collect;
let result = model2.apply;
// Selective transfer with filtering
let filter = new.with_regex;
let snapshots = model1.collect;
let result = model2.apply;
// Transfer with path transformation
let mut snapshots = model1.collect;
for snapshot in &mut snapshots
model2.apply;
Partial Loading and Exports
// Export only specific layers
let mut store = from_file
.with_regex;
model.save_into?;
// Load with missing tensors allowed
let mut store = from_file
.allow_partial;
let result = model.load_from?;
println!;
Merging Multiple Models
// Merge weights from different sources
let mut merged = Vecnew;
merged.extend;
// Add encoder from specialized model
let encoder_filter = new.with_regex;
merged.extend;
// Apply merged weights
target_model.apply;
// Alternative: Sequential loading from files
let mut base_store = from_file;
model.load_from?;
let mut encoder_store = from_file
.with_regex
.allow_partial;
model.load_from?; // Overlays encoder weights
Complete Example: Migrating PyTorch Models
use ;
// Load directly from PyTorch .pth file (automatic PyTorchToBurnAdapter)
// Note: skip_enum_variants defaults to true for PytorchStore
let mut store = from_file
// Access the state dict
.with_top_level_key
// Only load transformer layers
.with_regex
// Rename layer structure to match Burn model
.with_key_remapping
// Rename attention layers
.with_key_remapping
// Handle missing tensors gracefully
.allow_partial;
let mut model = new;
let result = model.load_from?;
println!;
if !result.errors.is_empty
// Save the migrated model in SafeTensors format
let mut save_store = from_file
.metadata
.metadata;
model.save_into?;
Advanced Usage
Direct Tensor Access
All stores provide methods to directly access tensor snapshots without loading into a model. This is useful for inspection, debugging, selective processing, or building custom loading pipelines.
use ;
// Works with any store type
let mut store = from_file;
// let mut store = SafetensorsStore::from_file("model.safetensors");
// let mut store = PytorchStore::from_file("model.pth");
// List all tensor names (ordered)
let names = store.keys?;
println!;
for name in &names
// Get all tensors as a BTreeMap (cached for repeated access)
let snapshots = store.get_all_snapshots?;
for in snapshots
// Get a specific tensor by name
if let Some = store.get_snapshot?
Use Cases
- Model Inspection: Examine tensor shapes, dtypes, and names without full model instantiation
- Selective Loading: Build custom pipelines that only load specific tensors
- Debugging: Verify tensor values and compare across different model files
- Format Conversion: Read tensors from one format and write to another
Custom Filtering with Predicates
// Custom filter function
let mut store = from_file
.with_predicate;
Working with Containers
// Filter based on container types (Linear, Conv2d, etc.)
let mut store = from_file
.with_predicate;
Handling Load Results
let result = model.load_from?;
// Detailed result information
println!;
println!;
println!;
println!;
if !result.errors.is_empty
Benchmarks
Loading Benchmarks
# Generate model files first (one-time setup)
# Run unified loading benchmark with default backend (NdArray CPU)
# Run with specific backend
# Run with multiple backends
Saving Benchmarks
Compares 3 saving methods: BurnpackStore, NamedMpkFileRecorder, and SafetensorsStore.
# Run unified saving benchmark with default backend (NdArray CPU)
# Run with specific backend
# Run with multiple backends
API Overview
Builder Methods
The stores provide a fluent API for configuration:
Filtering
with_regex(pattern)- Filter by regex patternwith_full_path(path)- Include specific tensorwith_full_paths(paths)- Include multiple specific tensorswith_predicate(fn)- Custom filter logicmatch_all()- Include all tensors (no filtering)
Remapping
with_key_remapping(from, to)- Regex-based tensor renamingremap(KeyRemapper)- Complex remapping rules
Adapters
with_from_adapter(adapter)- Loading transformationswith_to_adapter(adapter)- Saving transformations
Configuration
metadata(key, value)- Add custom metadata (Burnpack and SafeTensors)allow_partial(bool)- Continue on missing tensorsvalidate(bool)- Toggle validationskip_enum_variants(bool)- Skip enum variant names in paths for PyTorch compatibilitymap_indices_contiguous(bool)- Map non-contiguous layer indices to contiguous (default:truefor PyTorch,falsefor SafeTensors)with_top_level_key(key)- Access nested dict in PyTorch filesoverwrite(bool)- Allow overwriting existing files (Burnpack)zero_copy(bool)- Enable zero-copy tensor slicing (Burnpack)
Direct Tensor Access
keys()- Get ordered list of all tensor namesget_all_snapshots()- Get all tensors as a BTreeMap (cached)get_snapshot(name)- Get a specific tensor by name
Inspecting Burnpack Files
Generate and examine a sample file:
|
The example creates a sample model and outputs inspection commands for examining the binary format.
License
This project is dual-licensed under MIT and Apache-2.0.