Chapter 1: Neural Network Serialization Basics

Understanding the Fundamentals of Model Persistence

弘益人間 · Benefit All Humanity

1.1 Introduction to Neural Network Serialization

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.

Why Serialization Matters

1.2 Core Concepts of Model Formats

A neural network model consists of multiple components that must be preserved during serialization:

Model Architecture

The computational graph defining how data flows through layers. This includes:

Learned Parameters

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
}

Metadata and Configuration

Additional information needed for proper model usage:

1.3 Tensor Representation Fundamentals

Tensors are the fundamental data structure in neural networks. Understanding how they're represented in serialized formats is crucial:

Tensor Attributes

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

Data Type Considerations

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

1.4 Graph-Based Model Representation

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"]
}

Graph Optimization

Serialized formats often include optimized versions of the computational graph:

1.5 Format Categories and Trade-offs

Neural network formats can be categorized by their design goals and use cases:

Framework-Specific Formats

PyTorch (.pt, .pth): Python pickle-based, preserves Python objects

TensorFlow SavedModel: Protocol buffers, includes serving signatures

Keras (.h5): HDF5-based, hierarchical model structure

Interchange Formats

ONNX: Framework-agnostic, operator-based graph representation

CoreML: Apple ecosystem, optimized for iOS/macOS

TFLite: Mobile/edge deployment, optimized for size

Specialized Formats

GGUF: LLM quantization format for llama.cpp

SafeTensors: Security-focused tensor storage

GGML: C-based inference format

1.6 Serialization Challenges

Several technical challenges must be addressed when serializing neural networks:

Custom Operations

Models may use custom layers or operations not supported by standard formats. Solutions include:

Dynamic Shapes

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)

Numerical Precision

Ensuring numerical consistency across different hardware and frameworks:

1.7 WIA-AI-014 Standard Overview

The WIA-AI-014 Neural Network Format standard provides:

Core Principles

Standard Components

  1. Data Format Specification: Binary layout and encoding rules
  2. API Definitions: Language bindings for model I/O
  3. Conversion Protocols: Framework interoperability guidelines
  4. Validation Tools: Model verification and testing utilities

1.8 Best Practices for Model Serialization

Follow these guidelines when working with neural network formats:

Always Include Metadata

{
  "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
  }
}

Validate After Conversion

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}")

Version Control Models

Use semantic versioning and track model lineage:

1.9 Common Serialization Workflows

Typical workflows for model serialization in production:

Research to Production Pipeline

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

Multi-Platform Deployment

// 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)

1.10 Future Trends in Model Formats

The neural network format landscape continues to evolve:

Emerging Formats

Standardization Efforts

WIA-AI-014 contributes to broader AI standardization initiatives:

Chapter Summary

This chapter introduced the fundamentals of neural network serialization and model formats. Key takeaways include:

Review Questions

  1. What are the three main components that must be preserved when serializing a neural network model?
  2. Explain the difference between framework-specific and interchange formats. Provide examples of each.
  3. What are the key attributes of a tensor in a serialized format?
  4. How does quantization from FP32 to INT8 affect model size? Calculate the reduction for a 100MB model.
  5. What is operator fusion and why is it beneficial in serialized model graphs?
  6. Describe three challenges that arise when serializing neural networks with custom operations.
  7. What metadata should always be included with a serialized model and why?
  8. Explain the validation process after converting a model between formats.
  9. What are the main goals of the WIA-AI-014 standard?
  10. Describe a typical workflow for deploying a PyTorch model to multiple platforms (cloud, mobile, edge).
弘益人間 (Hongik Ingan) · Benefit All Humanity