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

本站现有博文321篇,共被浏览764589

本站已经建立2442天!

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