A PyTorch model's learned weights are separate from the Python code that defines its layers. Safetensors saves those weights as named tensors without pickling the model object.
Loading the weights requires an instance of the same model architecture, with matching parameter names and shapes. Comparing its output with the original model on a fixed input checks that the restored weights reproduce the original prediction.
A weights file does not contain the model class, preprocessing code, or optimizer state needed to resume training. Keep that code and any training checkpoint separately; a Safetensors export is useful for moving weights into an inference application.
Steps to save a PyTorch model with Safetensors:
- Activate the Python environment that contains PyTorch, using your own environment path.
$ source ~/venvs/torch-cpu/bin/activate
PyTorch must already be installed in this environment.
Related: How to install PyTorch with pip
Related: How to create a virtual environment for PyTorch - Install Safetensors and NumPy in the active environment.
$ python -m pip install safetensors numpy
The PyTorch Safetensors interface uses NumPy when saving tensors. A uv project has no pip in its environment, so use uv add safetensors numpy there instead. Tested with PyTorch 2.15.0 and Safetensors 0.8.0.
Related: How to install PyTorch with uv - Create a file named save_weights.py in your text editor with the model and fixed input.
- save_weights.py
import torch from torch import nn from safetensors.torch import load_file, save_file torch.manual_seed(0) model = nn.Linear(3, 2).eval() inputs = torch.tensor([[1.0, 2.0, 3.0]]) with torch.inference_mode(): original_output = model(inputs)
The small, untrained CPU model keeps the round trip reproducible without a dataset or download. For a trained model, replace the model line with your model in evaluation mode, and replace inputs with a batch it accepts. A model on a GPU needs inputs on the same device.
- Append the Safetensors export to save_weights.py.
weights_path = "model.safetensors" save_file(model.state_dict(), weights_path) print(f"Saved {weights_path}")
Running the script replaces an existing model.safetensors in the current directory. An unused output name avoids losing an earlier export.
The state dictionary contains parameters and persistent buffers. The file stores each tensor's name, data type, shape and bytes with no device, so the loading program chooses where the tensors go. save_file accepts only dense, contiguous tensors that do not share memory, and it raises an error otherwise. A non-contiguous tensor saves after .contiguous(), and tied weights need the model-aware API described in the Safetensors tensor-sharing guidance.
- Append the load operation for a fresh instance of the same model to save_weights.py.
restored = nn.Linear(3, 2) restored.load_state_dict(load_file(weights_path)) restored.eval()
The new instance has its own initial weights until loading replaces them. For a trained model, replace nn.Linear(3, 2) with the same model class, and move restored to the device that holds inputs. Loading raises an error on a missing key, an unexpected key or a different tensor shape. Evaluation mode keeps dropout and batch normalization in inference mode for architectures that use them.
Related: Safetensors PyTorch API - Append the output comparison to save_weights.py.
with torch.inference_mode(): restored_output = restored(inputs) matches = torch.equal(restored_output, original_output) print("Outputs match:", matches)
torch.equal compares every output value exactly, with no tolerance. A fresh model with no weights loaded prints False.
- Save save_weights.py in your text editor.
- Run save_weights.py to confirm the weights file restores the model's output.
$ python save_weights.py Saved model.safetensors Outputs match: True
True shows that the fresh model reproduces the original output from the saved weights. The model.safetensors file remains in the current directory for later loading with the same architecture.