PyTorch Model Serialization is the process of saving and loading PyTorch models, tensors, and training state to or from a file or stream. It commonly uses Python object serialisation or tensor state dictionaries, which allows models to be persisted, shared, and restored for inference or further training.
What PyTorch model serialization is used for
PyTorch model serialization is how training artefacts are written to disk or a stream and later restored. It lets teams persist model weights, optimizer state, and other training context so they can resume training, reproduce experiments, or load a model for inference without rebuilding it from scratch.
In practice, serialization is what turns an in-memory model into something portable across jobs, machines, and deployment stages. That portability is useful, but it also means the serialized object becomes a security-sensitive artifact if it is shared, stored, or loaded across trust boundaries.
Common serialization patterns in PyTorch
PyTorch supports more than one pattern. The safest and most common approach is saving a model’s state dictionary, which captures parameters without embedding the full Python object graph. Full-object serialization can be more convenient for quick experiments, but it binds the save file more tightly to Python class definitions and execution environment assumptions.
That distinction matters because model files are not all equally portable or equally safe. State dictionaries are generally easier to inspect and reason about, while full object pickles can hide more application logic inside the saved artifact and create stronger coupling to the code that produced it.
For practitioners, the key question is not only “can I save this model?” but “what exactly am I persisting?” Weights, optimizer state, scheduler state, metadata, and code references each have different operational and security implications.
Why model serialization can create security risk
Serialized model files often move between research notebooks, shared storage, CI jobs, and production systems. That movement makes them part of the software supply chain, especially when models, checkpoints, or pretrained artefacts are downloaded from third-party sources or reused across teams.
Because some Python serialization mechanisms can execute code during deserialization, loading an untrusted model artifact can become a code execution risk rather than a simple data import. Even when no code execution occurs, a tampered checkpoint can still corrupt model behaviour, poison training continuation, or leak embedded secrets and environment details.
For a real example of the kind of exposure this can create, NHIMG’s PyTorch torchtriton supply chain attack 2022 shows how a malicious package related to the PyTorch ecosystem was used to exfiltrate credentials and environment secrets.
How to handle serialized models safely
Serialized artefacts should be treated as untrusted until provenance is known. That means keeping an inventory of where checkpoints come from, validating downloads against expected sources, and being cautious about loading files from public repositories, shared buckets, or ad hoc transfers.
When possible, prefer serialization formats and loading paths that reduce the chance of arbitrary object execution, and separate model data from application code. It is also prudent to restrict who can publish, replace, or load production model artefacts, because write access to the artifact store is effectively a deployment control.
Strong surrounding controls help as well. Access limits on artifact storage, integrity checks, malware scanning, isolated runtime environments, and careful dependency management all reduce the chance that a serialized model becomes an attack vehicle.
For broader control thinking, NIST SP 800-53 Rev 5 Security and Privacy Controls helps organisations map model artefact handling to access control, integrity, audit, and configuration safeguards, while SLSA provides a useful lens for provenance and integrity of software artefacts.
Practically, teams should also consider whether a model checkpoint contains anything beyond weights, such as secrets, tokens, or environment metadata, before they share or archive it.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
NIST SP 800-53 Rev 5 and SLSA set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev 5 | AC-6 — Least Privilege | Serialized model handling needs restricted artifact access. |
| AU-9 — Protection of Audit Information | Model loading and artifact changes benefit from tamper-evident records. | |
| SI-7 — Software, Firmware, and Information Integrity | Serialized artefacts require integrity verification before loading. | |
| Recommendation — Restrict checkpoint read/write permissions to approved build and deployment roles. Protect model artifact logs so changes and loads remain reviewable and tamper-evident. Verify model file integrity before deserializing or promoting it into production. | ||
| SLSA | Supply-chain Levels for Software Artifacts | Serialized models are supply-chain artefacts that need provenance assurance. |
| Recommendation — Track model artifact provenance and require trusted build or publication paths before use. | ||
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 18, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org