EADST

Sharding and SafeTensors in Hugging Face Transformers

In the Hugging Face transformers library, managing large models efficiently is crucial, especially when working with limited disk space or specific file size requirements. Two key features that help with this are sharding and the use of SafeTensors.

Sharding

Sharding is the process of splitting a large model's weights into smaller files or "shards." This is particularly useful when dealing with large models that exceed file size limits or when you want to manage storage more effectively.

Usage

To shard a model during the saving process, you can use the max_shard_size parameter in the save_pretrained method. Here's an example:

# Save the model with sharding, setting the maximum shard size to 1GB
model.save_pretrained('./model_directory', max_shard_size="1GB")

In this example, the model's weights will be divided into multiple files, each not exceeding 1GB. This can make storage and transfer more manageable, especially when dealing with large-scale models.

SafeTensors

The safetensors library provides a new format for storing tensors in a safe and efficient way. Unlike traditional formats like PyTorch's .pt files, SafeTensors ensures that the tensor data cannot be accidentally executed as code, offering an additional layer of security. This is particularly important when sharing models across different systems or with the community.

Usage

To save a model using SafeTensors, simply specify the safe_serialization parameter when saving:

# Save the model using SafeTensors format
model.save_pretrained('./model_directory', safe_serialization=True)

This will create files with the .safetensors extension, ensuring the saved tensors are stored safely.

Combining Sharding and SafeTensors

You can combine both sharding and SafeTensors to save a large model securely and efficiently:

# Save the model with sharding and SafeTensors
model.save_pretrained('./model_directory', max_shard_size="1GB", safe_serialization=True)

This setup splits the model into shards, each in the SafeTensors format, offering both manageability and security.

Conclusion

By leveraging sharding and SafeTensors, Hugging Face transformers users can handle large models more effectively. Sharding helps manage file sizes, while SafeTensors ensures the safe storage of tensor data. These features are essential for anyone working with large-scale models, providing both practical and security benefits.

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
论文速读 Dataset Statistics v2ray hf 强化学习 Random LoRA Anaconda 阿里云 Plotly GGML Plate 净利润 Distillation COCO FlashAttention MD5 LLM NLP Pillow 多线程 WebCrawler BTC Firewall DeepStream XGBoost Attention uWSGI Password llama.cpp News ChatGPT CV Jetson RGB Crawler Use TensorRT DeepSeek JSON OpenAI Color Magnet Data API FP64 Hilton Shortcut Rebuttal Breakpoint 证件照 Datetime Food Web CEIR 搞笑 FastAPI 继承 CAM Bipartite Translation Llama Bin Hungarian logger Proxy Ptyhon Windows Agent scipy Freesound Quantization SAM 域名 Conda tqdm OCR Zip Paddle Pandas 飞书 transformers BeautifulSoup Baidu Bitcoin Cloudreve NLTK CTC Numpy 版权 顶会 Miniforge GoogLeNet 云服务器 SPIE InvalidArgumentError Python Animate VPN Mixtral icon CC Linux UI Safetensors Django LaTeX VGG-16 UNIX HaggingFace Website GIT Docker Card Tensor Bert Math Diagram SQL IndexTTS2 Augmentation EXCEL Qwen Google Pickle YOLO FP8 diffusers PyCharm 关于博主 财报 Video Domain printf Vmess mmap Vim git-lfs ResNet-50 Qwen2 Git API网关 TensorFlow Markdown Logo Permission git 算法题 Streamlit Pytorch Qwen2.5 Transformers RAR CSV Nginx PDB ms-swift CLAP Land CUDA VSCode 递归学习法 C++ 签证 TSV tar Knowledge Algorithm Jev Gemma HuggingFace SVR 多进程 Paper Sklearn 公式 GPTQ Quantize 第一性原理 PDF Disk Hotel Tracking Interview BF16 音频 Claude TTS OpenCV Heatmap AI PIP Excel Michelin FP32 Harness Template Review FP16 v0.dev 图形思考法 uwsgi NameSilo QWEN Input LLAMA Tiktoken LeetCode SQLite PyTorch Base64 论文 Image2Text RL Github WAN ModelScope 图标 Jupyter Clash torchinfo GPT4 ONNX Ubuntu Search XML 报税 腾讯云
站点统计

本站现有博文338篇,共被浏览966579次

本站已经建立2679天!

热门文章
文章归档
回到顶部