Safetensors is a Python library and file format for securely and efficiently storing and distributing deep learning tensors. It provides a safer, zero-copy alternative to pickle-based serialization, emphasizing speed, security, and ease of use. The library is actively maintained by Hugging Face, with its latest version being 0.7.0, and has a frequent release cadence, often aligning with new tensor datatype support or framework integrations.
pip install safetensorsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to save and load PyTorch tensors using the `safetensors.torch` API. It creates a dictionary of dummy tensors, saves them to a `.safetensors` file, then loads them back, and finally cleans up the file.
Ensure tensors are properly aligned or handle `MisalignedByte` exceptions. Be aware of how FP4/FP6 types are represented and accessed.
Always use the `safetensors` library's provided `safe_open` or `load_file` functions to ensure consistent and secure JSON header parsing. Avoid external JSON parsers for `.safetensors` headers.
Be mindful of potential shape changes when working with PyTorch's `float4_e2m1fn_x2` and `safetensors`. Explicitly check tensor shapes after loading.
Update calls to `load_file` or `safe_open` to include the `framework` argument, e.g., `with safe_open('model.safetensors', framework='pt', device='cpu') as f:`.Ensure that `torch` is installed in your environment. If running in a container, add `pip install torch` (or a specific version) to your Dockerfile or setup script. For specific PyTorch versions and CUDA support, refer to the official PyTorch installation instructions.
Ensure that PyTorch is correctly installed in the environment. For `pip`, use `pip install torch`. If running in a container, add `RUN pip install torch` to your Dockerfile or ensure the base image includes it. Verify the Python environment's paths.
pip install safetensors
Verify the file's integrity and ensure it was genuinely created and saved in the safetensors format. Do not rename non-safetensors files to .safetensors.
safetensors.save_file(tensors_dict, 'path/to/your/file.safetensors')
Ensure the data passed to `save_file` is a dictionary, where keys are strings and values are actual tensors (e.g., NumPy arrays, PyTorch tensors, TensorFlow tensors).
import numpy as np
import safetensors
data_to_save = {"my_tensor": np.zeros((10, 10), dtype=np.float32)}
safetensors.save_file(data_to_save, "model.safetensors")