Safetensors 文件

Safetensors

您正在檢視的是需要從原始碼安裝主分支 版本。如果您希望透過 pip 常規安裝,請檢視最新的穩定版本 (v0.5.0-rc.0)。
Hugging Face's logo
加入 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 FaceEleutherAI、 和 StabilityAI。以下是使用 safetensors 的部分專案列表

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.