Share and discover more about AI with social posts from the community.huggingface/OpenAi
safetensors
v0.4.4
Provides functions to read and write safetensors which aim to be safer than their PyTorch counterpart. The format is 8 bytes which is an unsized int, being the size of a JSON header, the JSON header refers the dtype the shape and data_offsets which are the offsets for the values in the rest of the file.
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
#tensorflow #pytorch #huggingface #tensors #safetensors GitHub - huggingface/safetensors: Simple, safe way to store and distribute tensors