Uh oh!
There was an error while loading. Please reload this page.
Feat: Activation Checkpointing - #154
Conversation
d87926e to
ba8db8fCompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| @@ -1,12 +1,15 @@ | |||
| #include "infini_train/include/nn/modules/transformer/transformer.h" | |||
There was a problem hiding this comment.
之后扩展 recompute 其他功能(如selective)的话就不适合全放在transformer.cc里了,之后可以拆分activation_recompute.cc。本PR可以不做修改
There was a problem hiding this comment.
确实,这块确实没太想清楚,主要是 megatron 的实现里面也把很多重计算逻辑融在了 transformer 模型层,之后可以再讨论下
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
9cdf73c to
a38a4d3Comparea38a4d3 to
e594d9cCompare| // 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 { |
There was a problem hiding this comment.
本质上是需要一个 2*2=4 种情况的精细控制(with_grad/no_grad x 带引用计数建图/仅传递 requires_grad)。
也可以不单独实现一个 guard,而是给原先的 GradGuard 重载一个带参数 (比如 bool record_context = false) 的构造方式,让 GradGuard 本身能够覆盖这四种情况。
| try { | ||
| forward_fn(detached_inputs); | ||
| } catch (const StopRecomputeError &) { | ||
| // Early-stop: expected when all needed tensors are recomputed. |
e594d9c to
4e0e3ddCompare4e0e3dd to
0bf0c25Compare
背景
本 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 重计算