Safetensors 文档
Safetensors
您正在查看 main 版本,该版本需要从源码安装。如果您想要常规的 pip 安装,请查看最新的稳定版本 (v0.5.0-rc.0)。
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
Safetensors
Safetensors 是一种新的简单格式,用于安全地存储张量(相对于 pickle),并且仍然快速(零拷贝)。Safetensors 真的非常快 🚀。
安装
使用 pip
pip install safetensors
使用 conda
conda install -c huggingface safetensors
用法
加载张量
from safetensors import safe_open
tensors = {}
with safe_open("model.safetensors", framework="pt", device=0) as f:
for k in f.keys():
tensors[k] = f.get_tensor(k)
仅加载部分张量(在多 GPU 上运行时很有用)
from safetensors import safe_open
tensors = {}
with safe_open("model.safetensors", framework="pt", device=0) as f:
tensor_slice = f.get_slice("embedding")
vocab_size, hidden_dim = tensor_slice.get_shape()
tensor = tensor_slice[:, :hidden_dim]
保存张量
import torch
from safetensors.torch import save_file
tensors = {
"embedding": torch.zeros((2, 2)),
"attention": torch.zeros((2, 3))
}
save_file(tensors, "model.safetensors")
格式
假设您有一个名为 model.safetensors
的 safetensors 文件,那么 model.safetensors
将具有以下内部格式
特色项目
Safetensors 正被领先的 AI 企业广泛使用,例如 Hugging Face、EleutherAI、 和 StabilityAI。以下是正在使用 safetensors 的项目的不完全列表
- huggingface/transformers
- ml-explore/mlx
- huggingface/candle
- AUTOMATIC1111/stable-diffusion-webui
- Llama-cpp
- microsoft/TaskMatrix
- hpcaitech/ColossalAI
- huggingface/pytorch-image-models
- CivitAI
- huggingface/diffusers
- coreylowman/dfdx
- invoke-ai/InvokeAI
- oobabooga/text-generation-webui
- Sanster/lama-cleaner
- PaddlePaddle/PaddleNLP
- AIGC-Audio/AudioGPT
- brycedrennan/imaginAIry
- comfyanonymous/ComfyUI
- LianjiaTech/BELLE
- alvarobartt/safejax
- MaartenGr/BERTopic
- rachthree/safestructures
- justinchuby/onnx-safetensors