Sub topic 14
Choosing a Model Format: Safety, Portability, and Speed
Assume you know Python classes, trained model weights, and basic PyTorch use. This chapter focuses on choosing a storage format after training. The format affects security, interoperability, loading speed, and whether the file can be used for training or only inference.
Start with the model artifact
A model artifact can contain more than numeric weights. It may include the computation graph, layer configuration, tokenizer data, optimizer state, training progress, and framework-specific objects.
The safest choice depends on what must be stored:
- Weights only: Use
safetensorswhen the consumer can rebuild the model architecture. - Local inference: Use GGUF when working with llama.cpp-compatible language models.
- Cross-framework inference: Use ONNX when supported operators and runtimes meet your needs.
- Full framework training state: Use a framework-native checkpoint, but handle it as executable input if it uses pickle or custom objects.
Compare the common formats
| Format | Main use | Loading safety | Portability | Typical strength |
|---|---|---|---|---|
| safetensors | Tensor weights | Data-only loading; no Python object execution | Good across Python tensor tools | Simple and fast weight loading |
| GGUF | Quantized local inference | Structured tensor and metadata format | Strong in its supported ecosystem | CPU-friendly inference and quantization |
| ONNX | Portable inference graphs | No Python pickle, but runtimes interpret graph operators | Broad runtime support | Moving inference between frameworks |
| PyTorch checkpoint | Training or PyTorch use | May execute pickle code unless restricted | Mostly PyTorch-specific | Complete training state |
| Keras or SavedModel | TensorFlow/Keras use | Depends on configuration and custom objects | Strongest in TensorFlow tooling | Framework-native deployment |
No format wins every category. safetensors stores tensor data and metadata, but it does not normally store the Python class needed to construct a model. GGUF can package architecture metadata and quantized tensors, but it is not a general training checkpoint. ONNX can represent a computation graph, but custom operators reduce portability.
Use a decision process
Ask these questions in order:
- Will another framework consume the model? Choose ONNX if the graph uses supported operators. Choose safetensors if the other tool only needs weights.
- Will the model run locally with limited memory? Consider GGUF and an appropriate quantization level.
- Must you resume training? Preserve optimizer state, scheduler state, and configuration in a trusted framework checkpoint.
- Do you control the producer and consumer? A private PyTorch checkpoint may be practical. A third-party checkpoint deserves stricter handling.
- Can the consumer rebuild the architecture? If not, a weights-only file is incomplete without a trusted configuration and model definition.
This flow separates safety from compatibility. A safe file can still fail because the architecture, tensor names, data types, or operators do not match.
Practical recommendation
Keep an original trusted training checkpoint for recovery, then export a deployment artifact for each target environment. Prefer safetensors for ordinary tensor weights. Use weights_only=True when loading compatible PyTorch files, and inspect unknown pickle streams with pickletools rather than loading them.
Exercise
You need to publish only neural-network weights for users who may use PyTorch or another tensor library. Which format is the best default, and what limitation must you document?
Show answer
Use safetensors. Document the model architecture, tensor names, configuration, and code needed to rebuild the model because the file does not contain an executable Python model class.