# 激活检查点

**激活检查点**（Activation checkpointing，也称**梯度检查点**，gradient checkpointing）是一种用计算换内存的训练技术：在前向传播过程中，指定区域内的中间激活值不被存储以供反向传播使用，而是被丢弃，在反向传播时通过在每个检查点分段上重新运行前向计算来重物化这些激活值。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup><sup> • </sup><sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup> 两个名称指的是同一种技术，可互换使用。<sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup>

| 关键事实 | 详情 |
|---|---|
| 机制 | 被检查点函数内部的中间张量在前向传播后被释放，除非保留供反向传播使用；函数的输入和返回的输出按需保持可用，函数在反向传播期间被重新调用来重新生成梯度所需的内容。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup> |
| 理论成本 | 将 n 个单元分为 k 个等长分段，内存为 O(n/k + k)；设 k = √n 得到 O(2√n)，相对于模型深度呈亚线性。<sup>[3](https://arxiv.org/pdf/1604.06174)</sup> |
| 计算成本 | 额外一次前向传播；由于反向传播的开销大约是前向传播的两倍，这大约增加 33% 的计算量，而 Transformer 模型实测的挂钟时间开销约为 5-10%。<sup>[3](https://arxiv.org/pdf/1604.06174)</sup><sup> • </sup><sup>[4](https://fairscale.readthedocs.io/en/stable/deep_dive/activation_checkpointing.html)</sup><sup> • </sup><sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup> |
| 完全重计算与选择性重计算 | 完全重计算可能增加 30-40% 的计算时间；选择性重计算为 GPT-3 和 MT-NLG 节省了 70% 和 65% 的激活内存，仅带来 2.7% 和 1.6% 的 FLOPs 开销。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> |
| 内存节省 | 检查点技术最多可削减 80% 的训练内存，允许在相同硬件上使用更大的批次或输入。<sup>[6](https://ar5iv.labs.arxiv.org/html/1808.00079)</sup> |
| 规模化结果 | 结合序列并行与选择性重计算，一个 5300 亿参数的 GPT-3 风格模型在 2,240 块 NVIDIA A100 GPU 上训练，达到了 54.2% 的模型浮点运算利用率，比完全重计算基线 42.1% 快 29%。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> |
| 框架 | PyTorch `torch.utils.checkpoint`、DeepSpeed、NVIDIA NeMo/Megatron、Hugging Face Transformers、FairScale、torchtune 和 Axolotl 均实现了该技术。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup><sup> • </sup><sup>[7](https://deepspeed.readthedocs.io/en/stable/activation-checkpointing.html)</sup><sup> • </sup><sup>[8](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)</sup> |

## 存储什么与重计算什么

在标准的完全检查点中，被检查点区域内的操作不为反向传播保存中间张量；该区域的输入被保留，而返回的输出按需保持可用。在反向传播期间，梯度计算所需的中间激活值通过第二次运行该函数来重物化。<sup>[9](https://pytorch.org/blog/activation-checkpointing-techniques/)</sup> 在层序列的表述中，该方法仅保留一组层中第一层的输入，并在反向传播时按需重计算中间输出。<sup>[10](https://www.usenix.org/conference/atc24/presentation/yuan)</sup> AWS SageMaker 的模型并行实现在模块粒度上行为相同：在前向传播结束时，模块的输入和输出留在内存中，而模块内部的中间张量被释放。<sup>[11](https://www.aws.amazon.com/sagemaker/latest/dg/model-parallel-extended-features-pytorch-activation-checkpointing.html)</sup>

**随机数生成器状态。** 在反向传播期间重新运行前向分段会使随机数生成器等持久状态推进到超出未使用检查点时的位置，这对于 dropout 等非确定性层很重要。PyTorch 默认在每个检查点处暂存并恢复随机数生成器状态，以保持 dropout 等随机行为相对于非检查点传播的一致性；如果不需要确定性，`preserve_rng_state=False` 可省略此操作，以适度降低性能开销。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup> FairScale 的实现同样按 dropout 层的需要保存前向和反向传播的随机数生成器状态。<sup>[4](https://fairscale.readthedocs.io/en/stable/deep_dive/activation_checkpointing.html)</sup>

## 变体与粒度

**完全（朴素）检查点**对每一层或整个模型进行检查点，仅存储分段边界。**分段或块级检查点**将序列模型划分为若干段并分别设置检查点；除最后一段外，所有分段不存储中间激活值，仅存储其输入。<sup>[12](https://github.com/pytorch/pytorch/blob/main/torch/utils/checkpoint.py)</sup> [NVIDIA NeMo](https://www.edgepedia.org/zh/nvidia-nemo) 将此暴露为 `recompute_method=uniform`，通过 `recompute_num_layers` 设置每个块的 Transformer 层数，同时提供 `recompute_method=full` 用于完全重计算。<sup>[8](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)</sup> [Hugging Face Transformers](https://www.edgepedia.org/zh/hugging-face-transformers) 指出，完全检查点会重计算每个可检查点的层，而内存有裕量的运行可以减少检查点的层数，以部分内存节省换取速度。<sup>[13](https://huggingface.co/docs/transformers/grad_checkpointing)</sup>

**选择性激活检查点（SAC）** 提供更精细的控制：不是选择整层，而是将单个操作分类为保存、重计算或卸载。PyTorch 的指令为 `{MUST,PREFER}_SAVE`（前向传播期间保存输出，不重计算）、`{MUST,PREFER}_RECOMPUTE` 和 `{MUST,PREFER}_CPU_OFFLOAD`（保存后卸载到 CPU，反向传播时重新加载）。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup><sup> • </sup><sup>[12](https://github.com/pytorch/pytorch/blob/main/torch/utils/checkpoint.py)</sup> 其原理在于，不同操作的重计算成本相对于其所占内存差异巨大：在 Transformer 中，softmax、dropout 和 qkv 点积注意力的输入具有随序列长度二次增长的内存复杂度，但它们的重计算成本小于线性投影层。<sup>[8](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)</sup> PyTorch 工程师观察到，仅重计算逐点操作即可减少 50% 的内存，其中注意力是重计算成本最高的操作，通常最后重计算。<sup>[9](https://pytorch.org/blog/activation-checkpointing-techniques/)</sup> NeMo 的选择性模式保存注意力、集合通信和部分矩阵乘法等昂贵操作，同时重计算较廉价的操作；它专为稠密 Transformer 设计，通常对使用专家并行的混合专家模型无帮助，后者更适合完全检查点。<sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup>

## 数据说话

奠基性分析来自 2016 年的论文《用亚线性内存成本训练深度网络》（*Training Deep Nets with Sublinear Memory Cost*），该论文提出丢弃中间结果并通过额外一次前向传播来恢复它们。将 n 个单元分为 k 个等长分段，内存成本为 O(n/k + k)；当 k = √n 时为 O(2√n)，相对于深度呈亚线性。由于反向传播的耗时几乎是前向传播的两倍，额外一次前向传播仅略微减慢训练——当反向传播耗时是前向传播的两倍时，约增加 33%。<sup>[3](https://arxiv.org/pdf/1604.06174)</sup> FairScale 引用了相同的约 33% 数字，并指出其回报：更小的激活存储允许更大的批次大小，从而提高净吞吐量。<sup>[4](https://fairscale.readthedocs.io/en/stable/deep_dive/activation_checkpointing.html)</sup> 随后一项关于最优检查点搜索的研究报告称，最多可削减 80% 的训练内存，时间开销适中，约为 30-50%，足以在相同 GPU 上将输入图像尺寸翻倍或将批次大小增至四倍。<sup>[6](https://ar5iv.labs.arxiv.org/html/1808.00079)</sup>

实测数字因配置而异，可靠来源给出了不同的范围。Megatron 风格的 MLSys 2023 研究报告称，Transformer 层的完全重计算可能引入高达 30-40% 的计算时间开销，<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> 而 NeMo 报告称完全层重计算每 Transformer 层约增加 30% 的计算量，但 Transformer 模型整体实际挂钟时间开销约为 5-10%。<sup>[8](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)</sup><sup> • </sup><sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup> 选择性重计算大幅缩小了成本：1.3 毫秒，即 11.9 毫秒反向传播基线的 11%，而完全层重计算为 7.6 毫秒，即 64%，与序列并行结合时仅为 4%。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> 在内存方面，梯度检查点在 GPU 上为每个检查点层保留一个激活值，在长序列长度下大致为层数 × 序列长度 × 隐藏维度 × 每元素字节数。<sup>[13](https://huggingface.co/docs/transformers/grad_checkpointing)</sup>

## 框架实现

**PyTorch** 提供 `torch.utils.checkpoint`，通过 `use_reentrant` 选择两种实现；文档推荐 `use_reentrant=False`。<sup>[1](https://docs.pytorch.org/docs/stable/checkpoint.md)</sup> 非重入版本在所有需要的中间激活值重新生成后立即停止重计算，而重入检查点在反向传播期间总是完整地重计算整个函数。<sup>[12](https://github.com/pytorch/pytorch/blob/main/torch/utils/checkpoint.py)</sup> `checkpoint_sequential` 处理序列模型的分段情况，选择性 API 则按操作暴露保存/重计算/卸载指令。<sup>[12](https://github.com/pytorch/pytorch/blob/main/torch/utils/checkpoint.py)</sup>

**DeepSpeed** 提供激活检查点 API，涵盖模型并行下跨 GPU 的激活分区、CPU 检查点以及减少内存消耗和碎片的连续内存优化。<sup>[7](https://deepspeed.readthedocs.io/en/stable/activation-checkpointing.html)</sup><sup> • </sup><sup>[14](https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/runtime/activation_checkpointing/checkpointing.py)</sup> **NVIDIA NeMo/Megatron** 暴露完全、块级和选择性重计算，并在 Transformer Engine 中使用 [FlashAttention](https://www.edgepedia.org/zh/flashattention) 时强制启用自注意力重计算；选择性模式也可在不使用 FlashAttention 的情况下通过设置 `recompute_method=selective` 来使用。<sup>[8](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)</sup> **Hugging Face Transformers** 文档介绍了层数调优和卸载选项。<sup>[13](https://huggingface.co/docs/transformers/grad_checkpointing)</sup> **FairScale** 和 **torchtune** 构建在 PyTorch 的机制之上，<sup>[4](https://fairscale.readthedocs.io/en/stable/deep_dive/activation_checkpointing.html)</sup> **Axolotl** 则用自己的默认值对其进行了封装。<sup>[15](https://docs.axolotl.ai/docs/gradient_checkpointing.html)</sup>

## 与其他省内存技术的比较

**激活卸载**将激活值移至 CPU 内存，并在反向传播时将其取回，通过 `torch.autograd.graph.saved_tensors_hooks()` 实现；torchtune 在可用时使用多个 CUDA 流，将额外的主机-设备通信与计算重叠。<sup>[16](https://meta-pytorch.org/torchtune/stable/tutorials/memory_optimizations.html)</sup> torchtune 建议仅在同时启用激活检查点时进行卸载，此时仅卸载被检查点的张量。<sup>[16](https://meta-pytorch.org/torchtune/stable/tutorials/memory_optimizations.html)</sup> Axolotl 的 `hidden_states` 模式进一步缩小了传输范围：它保持检查点启用，仅将解码器层的检查点输入移至 CPU，避免了完全激活卸载的 CPU 内存和带宽成本。<sup>[15](https://docs.axolotl.ai/docs/gradient_checkpointing.html)</sup> Hugging Face 同样提供卸载选项，将检查点激活值保存在锁页主机内存中，代价是设备到主机及主机到设备的拷贝，建议仅在运行无法容纳时使用。<sup>[13](https://huggingface.co/docs/transformers/grad_checkpointing)</sup>

**梯度累积**是目标为更大的有效批次时的替代方案：torchtune 指出，当至少一个样本能放入 GPU 时，通过累积梯度人为增大批次可以比激活检查点等以内存换速度的权衡带来更快的训练。<sup>[16](https://meta-pytorch.org/torchtune/stable/tutorials/memory_optimizations.html)</sup> 检查点还可与其他内存技术组合：NeMo 将其描述为与内存高效损失函数（如 Linear-Cut 交叉熵）以及使用 FSDP 的参数分片结合时尤为强大，典型目标是在保持吞吐量不变的情况下增大批次大小或序列长度。<sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup>

## 在大规模训练与并行中的应用

检查点与模型并行直接交互，因为每种并行方案都会改变给定设备必须持有的激活值。将选择性激活重计算与序列并行结合，可将激活内存降至原来的五分之一，并消除完全重计算 90% 以上的计算开销。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> 在大规模上，同一研究在 2,240 块 NVIDIA A100 GPU 上训练了一个 5300 亿参数的 GPT-3 风格模型，模型浮点运算利用率达 54.2%，比完全重计算达到的 42.1% 快 29%，在 220 亿到 1 万亿参数的配置中，端到端迭代时间改善了 29.0-32.1%。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> 一篇关于最优混合并行下高效激活重物化的 2024 年 USENIX ATC 论文，在 256 块 NVIDIA H800 GPU 上以 32,768 上下文窗口训练 1750 亿参数的 Llama 类模型，将模型浮点运算利用率从 32.3% 提升至 42.7%。<sup>[10](https://www.usenix.org/conference/atc24/presentation/yuan)</sup>

实用指导从这些数字中得出：理想情况下，仅对足够让给定模型并行配置在设备内存约束内训练所需的激活值设置检查点。<sup>[5](https://arxiv.org/pdf/2205.05198)</sup> 选择性检查点是否比完全检查点更快取决于模型、序列长度和 `torch.compile` 设置，因此 NVIDIA 建议在相同编译设置下对两种模式进行基准测试。<sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup> 平台约束也很重要：在 SageMaker 的模型并行设置中，被检查点的模块树必须位于一个流水线并行分区内，且 PyTorch 原生检查点 API 与 `smdistributed.modelparallel` 不兼容。<sup>[11](https://www.aws.amazon.com/sagemaker/latest/dg/model-parallel-extended-features-pytorch-activation-checkpointing.html)</sup>

## 近期进展

选择性激活检查点从库项目进入了 PyTorch 核心。SAC 最初由弗朗西斯科·马萨（Francisco Massa）及其同事在 xformers 仓库中实现，初始核心版本由威尔·冯（Will Feng）及其同事引入；它在 2024 年 PyTorch 大会上作为新 API 展示。<sup>[9](https://pytorch.org/blog/activation-checkpointing-techniques/)</sup><sup> • </sup><sup>[17](https://static.sched.com/hosted_files/pytorch2024/59/New%20Activation%20Checkpointing%20APIs%20in%20PyTorch.pdf)</sup> 编译器集成随之而来：`torch.compile`（PyTorch 2.0）在联合前向-反向图上使用最小割/最大流分区器来最小化为反向传播保存的张量数量，并提供内存预算 API，自动以帕累托最优策略应用 SAC，其中预算为 0 时行为等同于普通激活检查点，为 1 时等同于默认 `torch.compile`。<sup>[9](https://pytorch.org/blog/activation-checkpointing-techniques/)</sup>

工具也变得更加自动化和更能感知边缘情况。NeMo 提供 `NEMO_SELECTIVE_AC_TRACE=1` 来记录每个操作的 SAVE、RECOMPUTE 或 ALTERNATE 决策，而 Gemma4 等 KV 共享模型会自动回退到子模块检查点，因为注意力无法通过 [KV 缓存](https://www.edgepedia.org/zh/kv-cache)重计算。<sup>[2](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)</sup> 基于策略的方案的学术工作仍在继续：2024 年 IEEE 论文 MSCH 提出了基于微批次的选择性激活检查点，将重计算隐藏起来以高效训练大语言模型。<sup>[18](https://ieeexplore.ieee.org/document/10670415/)</sup> Axolotl 的选择性模式默认保存注意力操作的输出并重计算廉价操作，在长上下文下以每层额外一个隐藏状态大小的张量为代价，挽回了大部分检查点带来的减速，SDPA 和 flash-attention 在调度器层面匹配，未注册的自定义内核则照常重计算。<sup>[15](https://docs.axolotl.ai/docs/gradient_checkpointing.html)</sup>

## References

1. [torch.utils.checkpoint, PyTorch documentation](https://docs.pytorch.org/docs/stable/checkpoint.md)
2. [Use Gradient (Activation) Checkpointing, NVIDIA NeMo AutoModel](https://docs.nvidia.com/nemo/automodel/latest/development/gradient-checkpointing)
3. [Training Deep Nets with Sublinear Memory Cost (Chen et al., 2016), arXiv](https://arxiv.org/pdf/1604.06174)
4. [Enhanced Activation Checkpointing, FairScale documentation](https://fairscale.readthedocs.io/en/stable/deep_dive/activation_checkpointing.html)
5. [Reducing Activation Recomputation in Large Transformer Models (MLSys 2023), arXiv](https://arxiv.org/pdf/2205.05198)
6. [Optimal Gradient Checkpoint Search for Arbitrary Computation Graphs, arXiv](https://ar5iv.labs.arxiv.org/html/1808.00079)
7. [Activation Checkpointing, DeepSpeed documentation](https://deepspeed.readthedocs.io/en/stable/activation-checkpointing.html)
8. [Activation Recomputation, NVIDIA NeMo Framework User Guide](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/features/optimizations/activation_recomputation.html)
9. [Current and New Activation Checkpointing Techniques in PyTorch, PyTorch blog](https://pytorch.org/blog/activation-checkpointing-techniques/)
10. [Accelerating the Training of Large Language Models using Efficient Activation Rematerialization and Optimal Hybrid Parallelism, USENIX ATC 2024](https://www.usenix.org/conference/atc24/presentation/yuan)
11. [Activation Checkpointing, AWS SageMaker documentation](https://www.aws.amazon.com/sagemaker/latest/dg/model-parallel-extended-features-pytorch-activation-checkpointing.html)
12. [torch/utils/checkpoint.py, PyTorch source](https://github.com/pytorch/pytorch/blob/main/torch/utils/checkpoint.py)
13. [Gradient checkpointing, Hugging Face Transformers documentation](https://huggingface.co/docs/transformers/grad_checkpointing)
14. [deepspeed/runtime/activation_checkpointing/checkpointing.py, DeepSpeed source](https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/runtime/activation_checkpointing/checkpointing.py)
15. [Gradient Checkpointing, Activation Offloading, and Layer Offloading, Axolotl documentation](https://docs.axolotl.ai/docs/gradient_checkpointing.html)
16. [Memory Optimization Overview, torchtune documentation](https://meta-pytorch.org/torchtune/stable/tutorials/memory_optimizations.html)
17. [New Activation Checkpointing APIs in PyTorch, PyTorch Conference 2024 slides](https://static.sched.com/hosted_files/pytorch2024/59/New%20Activation%20Checkpointing%20APIs%20in%20PyTorch.pdf)
18. [MSCH: Microbatch-Based Selective Activation Checkpointing With Recomputation Hidden for Efficient Training of LLM Models, IEEE](https://ieeexplore.ieee.org/document/10670415/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/activation-checkpointing

English original: https://www.edgepedia.org/activation-checkpointing
