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

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

本站已经建立2687天!

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