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

本站现有博文324篇,共被浏览809833

本站已经建立2513天!

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