Home

Awesome

<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://huggingface.co/datasets/safetensors/assets/raw/main/banner-dark.svg"> <source media="(prefers-color-scheme: light)" srcset="https://huggingface.co/datasets/safetensors/assets/raw/main/banner-light.svg"> <img alt="Hugging Face Safetensors Library" src="https://huggingface.co/datasets/safetensors/assets/raw/main/banner-light.svg" style="max-width: 100%;"> </picture> <br/> <br/> </p>

Python Pypi Documentation Codecov Downloads

Rust Crates.io Documentation Codecov Dependency status

safetensors

Safetensors

This repository implements a new simple format for storing tensors safely (as opposed to pickle) and that is still fast (zero-copy).

Installation

Pip

You can install safetensors via the pip manager:

pip install safetensors

From source

For the sources, you need Rust

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Make sure it's up to date and using stable channel
rustup update
git clone https://github.com/huggingface/safetensors
cd safetensors/bindings/python
pip install setuptools_rust
pip install -e .

Getting started

import torch
from safetensors import safe_open
from safetensors.torch import save_file

tensors = {
   "weight1": torch.zeros((1024, 1024)),
   "weight2": torch.zeros((1024, 1024))
}
save_file(tensors, "model.safetensors")

tensors = {}
with safe_open("model.safetensors", framework="pt", device="cpu") as f:
   for key in f.keys():
       tensors[key] = f.get_tensor(key)

Python documentation

Format

Notes:

Yet another format ?

The main rationale for this crate is to remove the need to use pickle on PyTorch which is used by default. There are other formats out there used by machine learning and more general formats.

Let's take a look at alternatives and why this format is deemed interesting. This is my very personal and probably biased view:

FormatSafeZero-copyLazy loadingNo file size limitLayout controlFlexibilityBfloat16/Fp8
pickle (PyTorch)🗸🗸🗸
H5 (Tensorflow)🗸🗸🗸~~
SavedModel (Tensorflow)🗸🗸🗸🗸
MsgPack (flax)🗸🗸🗸🗸
Protobuf (ONNX)🗸🗸
Cap'n'Proto🗸🗸~🗸🗸~
Arrow??????
Numpy (npy,npz)🗸??🗸
pdparams (Paddle)🗸🗸🗸
SafeTensors🗸🗸🗸🗸🗸🗸

Main oppositions

Notes

Benefits

Since we can invent a new format we can propose additional benefits:

License: Apache-2.0