Safetensors documentation
Safetensors
Memulai
🤗 SafetensorsPerbandingan KecepatanBerbagi Tensor di PyTorchParsing MetadataKonversi bobot ke safetensors
API
You are viewing main version, which requires installation from source. If you'd like
regular pip install, checkout the latest stable version (v0.5.0-rc.0).
Safetensors
Safetensors adalah format baru yang sederhana untuk menyimpan tensor secara aman (sebagai alternatif pickle) dan tetap cepat (zero-copy). Safetensors benar-benar cepat 🚀.
Instalasi
dengan pip:
pip install safetensorsdengan conda:
conda install -c conda-forge safetensorsPenggunaan
Muat tensor
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)Memuat hanya sebagian dari tensor (menarik saat dijalankan di beberapa 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]Simpan tensor
import torch
from safetensors.torch import save_file
tensors = {
"embedding": torch.zeros((2, 2)),
"attention": torch.zeros((2, 3))
}
save_file(tensors, "model.safetensors")Format
Misalkan Anda memiliki file safetensors bernama model.safetensors, maka model.safetensors akan memiliki format internal berikut:
Proyek Unggulan
Safetensors digunakan secara luas oleh perusahaan AI terkemuka, seperti Hugging Face, EleutherAI, dan StabilityAI. Berikut adalah daftar proyek yang menggunakan safetensors (tidak lengkap):
- 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