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

本站现有博文329篇,共被浏览858999

本站已经建立2567天!

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