Understanding the Fundamentals of Model Persistence
弘益人間 · Benefit All Humanity
Neural network serialization is the process of converting trained models from their in-memory representations into persistent formats that can be stored, transferred, and loaded across different systems and frameworks. This fundamental capability enables the entire AI/ML ecosystem - from research to production deployment.
Without standardized serialization formats, each framework would create incompatible model files, preventing model sharing, deployment flexibility, and interoperability. The WIA-AI-014 standard addresses this critical need by defining universal formats and conversion protocols.
A neural network model consists of multiple components that must be preserved during serialization:
The computational graph defining how data flows through layers. This includes:
The weights and biases learned during training. These are typically stored as multi-dimensional tensors:
// Example parameter structure
{
"layer1.weight": Tensor[64, 3, 7, 7], // Conv2D weights
"layer1.bias": Tensor[64], // Conv2D bias
"layer2.weight": Tensor[128, 64, 3, 3],
"layer2.bias": Tensor[128],
// ... additional layers
}
Additional information needed for proper model usage:
Tensors are the fundamental data structure in neural networks. Understanding how they're represented in serialized formats is crucial:
| Attribute | Description | Example |
|---|---|---|
shape |
Dimensions of the tensor | [1, 3, 224, 224] |
dtype |
Data type of elements | float32, int8, bfloat16 |
layout |
Memory layout | NCHW, NHWC |
data |
Raw tensor values | Binary blob or array |
Different data types offer trade-offs between precision, memory, and performance:
// Common tensor data types
FP32 (float32): 32-bit floating point (standard training)
FP16 (float16): 16-bit floating point (mixed precision)
BF16 (bfloat16): 16-bit brain float (Google TPU)
INT8: 8-bit integer (quantized inference)
INT4: 4-bit integer (extreme quantization)
// Example tensor in different precisions
FP32: 4 bytes/element → 100MB model
FP16: 2 bytes/element → 50MB model
INT8: 1 byte/element → 25MB model
Modern neural networks are represented as computational graphs, where nodes represent operations and edges represent data flow:
// Simplified graph structure
{
"nodes": [
{
"id": "input",
"op": "Input",
"shape": [1, 3, 224, 224]
},
{
"id": "conv1",
"op": "Conv2D",
"inputs": ["input"],
"attributes": {
"kernel_size": [7, 7],
"stride": [2, 2],
"padding": [3, 3]
}
},
{
"id": "relu1",
"op": "ReLU",
"inputs": ["conv1"]
}
],
"outputs": ["relu1"]
}
Serialized formats often include optimized versions of the computational graph:
Neural network formats can be categorized by their design goals and use cases:
PyTorch (.pt, .pth): Python pickle-based, preserves Python objects
TensorFlow SavedModel: Protocol buffers, includes serving signatures
Keras (.h5): HDF5-based, hierarchical model structure
ONNX: Framework-agnostic, operator-based graph representation
CoreML: Apple ecosystem, optimized for iOS/macOS
TFLite: Mobile/edge deployment, optimized for size
GGUF: LLM quantization format for llama.cpp
SafeTensors: Security-focused tensor storage
GGML: C-based inference format
Several technical challenges must be addressed when serializing neural networks:
Models may use custom layers or operations not supported by standard formats. Solutions include:
Some models accept variable input sizes or use dynamic control flow:
// Dynamic input example
input_shape = [-1, 3, -1, -1] // Batch and spatial dims can vary
// Dynamic control flow
if condition:
x = branch_a(x)
else:
x = branch_b(x)
Ensuring numerical consistency across different hardware and frameworks:
The WIA-AI-014 Neural Network Format standard provides:
Follow these guidelines when working with neural network formats:
{
"model_name": "ResNet50-ImageNet",
"version": "1.0.0",
"framework": "pytorch",
"framework_version": "2.0.0",
"created_date": "2025-01-15",
"input_shape": [1, 3, 224, 224],
"input_preprocessing": {
"normalize": true,
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225]
},
"output_labels": "imagenet_1k.txt",
"accuracy": {
"top1": 0.761,
"top5": 0.931
}
}
Always verify that converted models produce identical outputs:
# Python validation example
import numpy as np
# Test with random input
test_input = np.random.randn(1, 3, 224, 224).astype(np.float32)
# Get outputs from both models
output_original = original_model(test_input)
output_converted = converted_model(test_input)
# Check numerical difference
diff = np.abs(output_original - output_converted)
max_diff = np.max(diff)
mean_diff = np.mean(diff)
assert max_diff < 1e-5, f"Max difference: {max_diff}"
print(f"✓ Validation passed: max_diff={max_diff:.2e}")
Use semantic versioning and track model lineage:
Typical workflows for model serialization in production:
1. Train model in preferred framework (PyTorch/TensorFlow)
2. Export to framework-specific format (.pt, SavedModel)
3. Convert to ONNX for framework independence
4. Optimize for target deployment (quantization, pruning)
5. Convert to deployment format (TFLite, CoreML, GGUF)
6. Validate accuracy and performance
7. Deploy to production with monitoring
// Single source model deployed to multiple targets
Source: PyTorch .pt model
├── Cloud: ONNX → TensorRT (NVIDIA GPU)
├── Mobile: ONNX → CoreML (iOS)
├── Edge: ONNX → TFLite (Raspberry Pi)
└── Browser: ONNX → ONNX.js (WebAssembly)
The neural network format landscape continues to evolve:
WIA-AI-014 contributes to broader AI standardization initiatives:
This chapter introduced the fundamentals of neural network serialization and model formats. Key takeaways include: