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

本站现有博文336篇,共被浏览947188

本站已经建立2657天!

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