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

本站现有博文332篇,共被浏览882340

本站已经建立2587天!

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