Sub topic 2
Serialization: Turning Objects into Bytes and Back
Assume you know basic Python, classes, and tensors. You should also know that a trained model has learned weights, such as matrices and vectors, that must be stored between program runs.
Why Models Need Serialization
Serialization is the process of converting an in-memory object into bytes that can be saved or transmitted. Deserialization is the reverse process: reading bytes and rebuilding an object.
A Python program represents a tensor as an object with a data buffer, shape, data type, and device information. A file system stores bytes, not live Python objects. Serialization defines how those properties become a file format.
A model artifact is the saved result. It may contain:
- Model weights.
- The names and shapes of weight tensors.
- The model architecture or a reference to it.
- Optimizer state for continuing training.
- Training metadata, such as epoch number or configuration.
- Format and version information.
The artifact does not always contain all of these items. A weights-only file is smaller and usually requires source code that already defines the model architecture.
The Serialization Round Trip
The usual workflow is a round trip:
- Build or train objects in memory.
- Serialize selected objects into bytes.
- Store the bytes in a file or object store.
- Read the bytes later.
- Deserialize them into usable data.
Serialization is more than writing raw tensor values. The reader needs enough metadata to interpret those values correctly. For example, a two-dimensional array of 32-bit floating-point values needs its shape and data type. Without that information, the reader cannot reliably rebuild the tensor.
Formats Are Contracts
A serialization format is a contract between the writer and reader. The contract specifies the byte layout, metadata, supported types, and version behavior.
Some formats encode general programming-language objects. Python's pickle is an example. It can represent lists, dictionaries, class instances, and references to functions or classes. This flexibility makes it convenient, but it also means that deserialization may perform operations beyond copying data.
Other formats are designed around data structures. A tensor format might store only named numeric arrays and their metadata. Such a format is easier to inspect and support across languages, but it cannot automatically preserve every Python object.
| Format style | Typical contents | Main trade-off |
|---|---|---|
| General object serialization | Python objects and relationships | Convenient, but loading requires trust |
| Tensor-focused serialization | Named tensors and metadata | Safer and portable, but less flexible |
| Graph serialization | Computation graph and parameters | Better cross-framework deployment, with operator limits |
What Serialization Does Not Guarantee
Serialization preserves a representation, not necessarily the original execution environment. A file may fail to load because the required library version, class definition, device, or operator is missing. A successfully loaded file may also produce different results if the runtime or numerical libraries differ.
For long-lived or shared artifacts, record the format version, framework version, model configuration, tensor names, and expected input shape. Prefer formats whose contents can be validated before reconstruction.
Exercise
Why can a weights-only file be smaller and safer than a serialized Python model object?
Show answer
It stores named numeric tensors and metadata instead of arbitrary Python objects, so it contains less information and requires fewer executable reconstruction steps.