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

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

本站已经建立2584天!

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