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 檔案,那麼該檔案將具有以下內部格式
精選專案
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