# Safetensors

Safetensors 是 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 于 2022 年 9 月创建的一种张量序列化格式：文件在一个小小的 JSON 头部之后存放原始张量数据，不含任何可执行代码路径，因此加载模型文件无法运行任意代码，数据也可以直接从磁盘映射，无需复制。

| 项目 | 内容 |
|---|---|
| 创建 | 2022 年 9 月 22 日，由 Hugging Face 创建，Apache-2.0 许可 <sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> |
| 文件布局 | 8 字节小端头部长度、JSON 头部、原始张量字节缓冲区 <sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> |
| 头部上限 | 100MB，用以防止借超大 JSON 实施拒绝服务攻击 <sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> |
| 厂商报告的加载速度 | BLOOM 在 8 块 GPU 上：用 PyTorch 权重需 10 分钟，降至 45 秒；CPU 加载比 pickle 快约 100 倍 <sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup><sup> • </sup><sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup> |
| 安全审计 | Trail of Bits，2023 年 5 月 23 日公布；未发现可导致任意代码执行的关键漏洞 <sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup> |
| 治理 | 2026 年 4 月 8 日加入 Linux 基金会旗下的 PyTorch 基金会 <sup>[3](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)</sup> |
| 采用情况 | Hugging Face 称其为 Hugging Face Hub 上模型分发的默认格式，被涵盖所有模态的数万个模型使用 <sup>[3](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)</sup> |

## safetensors 是什么

这种格式只存张量，别无他物。文件开头是一个 8 字节无符号小端 64 位整数，给出头部的大小 N，随后是 N 字节的 JSON UTF-8 头部，且必须以 `{` 字符（0x7B）开头。头部把每个张量名称映射到它的 dtype、shape 和 data_offsets，还可以包含一个特殊的 `__metadata__` 键，其中只允许存放字符串到字符串的值；那里不允许任意 JSON。头部之后是张量数据的原始字节缓冲区。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup>

实现刻意做得很小。这个库用 Rust 写成，大约 400 行代码，项目将其与另一种数值存储格式 HDF5 约 21 万行的体量作对比。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> 代码仓库创建于 2022 年 9 月 22 日，采用 Apache-2.0 许可。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup>

## 它解决的问题：基于 pickle 的检查点

PyTorch 惯用的 `.bin` 检查点底层用的是 pickle，而 pickle 天生不安全：一个伪装成模型的恶意文件，在加载时就能让攻击者完全控制用户的电脑。<sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup> Safetensors 是纯数据格式，加载过程中不执行任何代码。

这种格式还约束了文件结构。字节缓冲区必须被完整索引、不留空洞，这就杜绝了多格式文件（即同时也能按另一种格式解析的文件）的构造，重复的键也不允许出现。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> 头部被限制在 100MB 以内，以防解析超大 JSON 成为拒绝服务的入口；格式还保证各张量的地址互不重叠，因此加载时占用的内存绝不会超过文件大小。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup>

## 格式如何工作

在字节缓冲区内部，张量按小端、C 顺序（行优先）存储，不带跨步；张量在序列化前会先打包成紧凑布局。<sup>[4](https://docs.rs/safetensors/latest/safetensors/)</sup> 张量的偏移量在头部中明确给出。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup>

这种布局带来了零拷贝加载：文件被内存映射，张量直接从磁盘使用，而不复制到新的内存分配中；也支持惰性加载，即按需读取单个权重。<sup>[3](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)</sup> 有一项限制与亚字节数据类型有关：只要某个操作会触发非对齐读取，Rust 软件包就会报错，因为对小于一字节的数据类型来说，对齐与寻址都很棘手。<sup>[4](https://docs.rs/safetensors/latest/safetensors/)</sup>

## 数据一览

最引人注目的性能数字来自厂商报告。Hugging Face 称，对 [BLOOM](https://www.edgepedia.org/zh/bloom) 而言，使用 safetensors 把在 8 块 GPU 上加载该模型的时间从常规 PyTorch 权重的 10 分钟缩短到 45 秒。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup> 同一项目还报告，CPU 惰性加载比 pickle 快约 100 倍。<sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup>

与替代方案相比，项目的 README 指出，safetensors 原生支持 bfloat16 和 FP8，而 ONNX 所用的 Protobuf 底层有 2GB 的硬性文件大小上限，NumPy 的 npz 格式不支持 bfloat16，且易受 zip 炸弹攻击。<sup>[1](https://github.com/huggingface/safetensors?tab=readme-ov-file)</sup>

## 采用与治理

支持覆盖主流 Python 机器学习框架：PyTorch、[TensorFlow](https://www.edgepedia.org/zh/tensorflow)、JAX、PaddlePaddle 和 NumPy。<sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup> 在 Trail of Bits 完成外部安全审计之后，采用速度加快；该审计由 Hugging Face 与 [EleutherAI](https://www.edgepedia.org/zh/eleutherai)、[Stability AI](https://www.edgepedia.org/zh/stability-ai) 合作，于 2023 年 5 月 23 日公布，完整报告已公开。审计未发现可导致任意代码执行的关键安全漏洞；它指出了规范中的不精确之处以及缺失的校验，正是这些让多格式文件得以存在，相关问题已修复。<sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup> 经过这次验证，safetensors 在 `transformers` 库中成为默认安装项，EleutherAI 在开发 GPT-NeoX 支持的过程中也在其 LM Evaluation Harness 中加入了支持。<sup>[2](https://huggingface.co/blog/safetensors-security-audit)</sup>

到 2026 年 4 月，Hugging Face 已将 safetensors 描述为 [Hugging Face Hub](https://www.edgepedia.org/zh/hugging-face-hub) 上模型分发的默认格式，被涵盖所有模态的数万个模型使用。<sup>[3](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)</sup> 2026 年 4 月 8 日，该项目作为基金会托管项目加入 [Linux 基金会](https://www.edgepedia.org/zh/linux-foundation)旗下的 PyTorch 基金会，与 [DeepSpeed](https://www.edgepedia.org/zh/deepspeed)、Helion、Ray、vLLM 以及 PyTorch 本身并列；商标、代码仓库与治理权现归 Linux 基金会，Hugging Face 的两位核心维护者 Luc Georges 与 Daniël de Kok 进入技术指导委员会。<sup>[3](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)</sup> 独立分析指出，正式的 MAINTAINERS.md 名单上仍然只有 de Kok 和 Georges 两人，均来自 Hugging Face，而新的 GOVERNANCE.md 确立了基于共识的维护者模式、一条申诉途径，以及面向未来维护者的提名渠道。<sup>[5](https://ainewssilo.com/articles/safetensors-pytorch-foundation-weight-layer)</sup>

## References

1. [huggingface/safetensors GitHub repository README](https://github.com/huggingface/safetensors?tab=readme-ov-file)
2. [Safetensors audited as really safe and becoming the default (Hugging Face blog, May 23, 2023)](https://huggingface.co/blog/safetensors-security-audit)
3. [Safetensors is Joining the PyTorch Foundation (Hugging Face blog, April 8, 2026)](https://huggingface.co/blog/safetensors-joins-pytorch-foundation)
4. [safetensors Rust crate documentation](https://docs.rs/safetensors/latest/safetensors/)
5. [Safetensors joins PyTorch Foundation weight stack (AI News Silo)](https://ainewssilo.com/articles/safetensors-pytorch-foundation-weight-layer)

---
*Initially written Sep 17, 2026 · Edited: Oct 11, 2026; Oct 15, 2026; Oct 21, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Model families and named models › Open-weight ecosystem, formats, and licensing

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/safetensors

English original: https://www.edgepedia.org/safetensors
