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