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

本站现有博文339篇,共被浏览1001515次

本站已经建立2711天!

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