Feat: Activation Checkpointing#154
Open
Chamberlain0w0 wants to merge 6 commits into
Open
Conversation
Chamberlain0w0
force-pushed
the
feat/activation_checkpointing
branch
from
June 3, 2026 08:45
d87926e to
ba8db8f
Compare
chen2021673
reviewed
Jun 9, 2026
| @@ -1,12 +1,15 @@ | |||
| #include "infini_train/include/nn/modules/transformer/transformer.h" | |||
Contributor
There was a problem hiding this comment.
之后扩展 recompute 其他功能(如selective)的话就不适合全放在transformer.cc里了,之后可以拆分activation_recompute.cc。本PR可以不做修改
Contributor
Author
There was a problem hiding this comment.
确实,这块确实没太想清楚,主要是 megatron 的实现里面也把很多重计算逻辑融在了 transformer 模型层,之后可以再讨论下
chen2021673
reviewed
Jun 9, 2026
chen2021673
reviewed
Jun 9, 2026
chen2021673
reviewed
Jun 9, 2026
Chamberlain0w0
force-pushed
the
feat/activation_checkpointing
branch
from
June 11, 2026 01:21
9cdf73c to
a38a4d3
Compare
Chamberlain0w0
force-pushed
the
feat/activation_checkpointing
branch
from
June 25, 2026 06:47
a38a4d3 to
e594d9c
Compare
Chamberlain0w0
commented
Jun 25, 2026
| // Used by non-reentrant checkpoint recomputation so downstream SetupContext | ||
| // calls see the same needs_input_grad_ pattern as the original forward, | ||
| // without wiring the recompute graph into the engine. | ||
| class PropagateRequiresGradGuard { |
Contributor
Author
There was a problem hiding this comment.
本质上是需要一个 2*2=4 种情况的精细控制(with_grad/no_grad x 带引用计数建图/仅传递 requires_grad)。
也可以不单独实现一个 guard,而是给原先的 GradGuard 重载一个带参数 (比如 bool record_context = false) 的构造方式,让 GradGuard 本身能够覆盖这四种情况。
Chamberlain0w0
commented
Jun 25, 2026
| try { | ||
| forward_fn(detached_inputs); | ||
| } catch (const StopRecomputeError &) { | ||
| // Early-stop: expected when all needed tensors are recomputed. |
Contributor
Author
Chamberlain0w0
force-pushed
the
feat/activation_checkpointing
branch
from
July 10, 2026 08:36
e594d9c to
4e0e3dd
Compare
Chamberlain0w0
force-pushed
the
feat/activation_checkpointing
branch
from
July 22, 2026 06:52
4e0e3dd to
0bf0c25
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

背景
本 PR 为 InfiniTrain 增加重计算能力,通过在反向传播时重新执行部分前向计算,减少训练过程中需要保留的中间激活,以计算开销换取显存占用下降。
整体配置与 Megatron-Core 的 Transformer activation recompute 语义保持一致。
设计
通用 Checkpoint 接口
新增
utils::checkpoint::Checkpoint(),对标torch.utils.checkpoint()。同时支持:Non-reentrant 模式通过 Saved Tensor Hooks 将前向过程中保存的中间 Tensor 替换为占位对象。反向传播真正读取这些 Tensor 时,再按需重新执行 checkpoint 区域的前向计算。
该实现包含以下机制:
requires_grad状态,避免错误连接原始计算图由于当前 autograd engine 在构图时会直接修改依赖计数,non-reentrant 重计算在
no_grad下执行,并新增PropagateRequiresGradGuard传递requires_grad和needs_input_grad元数据,避免临时重计算图污染原始反向图。Transformer 重计算策略
TransformerConfig新增以下配置:recompute_granularitynone:关闭重计算full:以完整 Transformer Layer 为重计算单位selective:预留配置,当前尚未实现recompute_methodnone:每个 Transformer Layer 独立 checkpointuniform:每连续recompute_num_layers层组成一个 checkpoint 区域block:仅 checkpoint 当前本地 chunk/stage 的前recompute_num_layers层recompute_num_layers:控制 uniform 分组大小或 block 覆盖层数未启用重计算或处于
no_grad场景时,Transformer 仍直接执行原有前向路径。主要修改
--activation_recompute--recompute_granularity--recompute_method--recompute_num_layersForward()调用调整为operator()当前限制
selectivegranularity 尚未实现,目前仅支持完整 Transformer Layer 重计算