Safetensors 文档
Safetensors
您正在查看的是需要从源码安装的 主分支 版本。如果您希望通过 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