Model serialization formats (pickle, ONNX, TorchScript) in MLOps - Time & Space Complexity
When saving machine learning models, the time it takes depends on the format used. We want to understand how this saving time grows as the model size increases.
How does the time to serialize a model change when the model gets bigger?
Analyze the time complexity of the following code snippet.
import torch
import pickle
# Assume model is a trained PyTorch model
def save_pickle(model, path):
with open(path, 'wb') as f:
pickle.dump(model, f)
def save_torchscript(model, path):
scripted = torch.jit.script(model)
scripted.save(path)
def save_onnx(model, path, input_sample):
torch.onnx.export(model, input_sample, path)
This code saves a model using three formats: pickle, TorchScript, and ONNX.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the model's parameters and structure to serialize data.
- How many times: Each parameter and layer is processed once during serialization.
As the model size (number of parameters) grows, the time to save grows roughly in proportion.
| Input Size (number of parameters) | Approx. Operations |
|---|---|
| 10,000 | 10,000 operations |
| 100,000 | 100,000 operations |
| 1,000,000 | 1,000,000 operations |
Pattern observation: Doubling the model size roughly doubles the serialization time.
Time Complexity: O(n)
This means the time to save the model grows linearly with the model size.
[X] Wrong: "Serialization time is constant no matter the model size."
[OK] Correct: Larger models have more data to process, so saving takes more time.
Understanding how serialization time grows helps you design efficient model deployment pipelines and choose the right format for your needs.
"What if we used a streaming serialization method that writes data in chunks? How would the time complexity change?"