Repository navigation
Rename activation checkpointing policies to activation checkpoint configs - #5102
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: 75426ea Pull-Request: #5102
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: 9064cb0 Pull-Request: #5102
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: a11fee9 Pull-Request: #5102
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: d734d5e Pull-Request: #5102
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #5102 * #5101 * #4893 * __->__ #5131 ---- * Pin spmd_types to a main commit instead of the 0.2.5 release. It includes the fixes torch_remat activation checkpointing needs under tensor parallelism: metadata queries such as `stride()` and `storage_offset()` are no longer type checked, so saving a Partial region works, and `assert_type_like` / `assert_local_type_like` overrides keyed by a process group replace the source's entry for that axis instead of conflicting. * `_AuxLossInjection.spmd_typecheck` types its output with `rules.output` and declares `aux_loss`, which is only saved for backward, with `rules.ignore`, satisfying the new check that a hook accounts for every tensor argument. * spmd_types now ships type stubs: narrow a just-registered parameter or buffer before `assert_type`, and drop a `no_typecheck` suppression Pyrefly no longer needs.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #5102 * #5101 * __->__ #4893 * #5131 ---- Needs https://www.internalfb.com/diff/D123762808 This moves SelectiveAC to use torch remat. In future, we want to get rid of SelectiveAC as well and just have one RegionAC (or a better name) with possibly some good defaults for models. Currently, we keep the `SelectiveAC` name because its used at many places and there is some future work to remove it entirely. * SelectiveAC is now a fixed torch_remat policy: it saves every model-declared region except the routed-expert `w13`/`w2` grouped matmuls, and recomputes everything else. It shares the `_RematAC` base with RegionAC, which stays the way to choose a different policy; SelectiveAC can no longer be subclassed. * The operator-level save policy and `force_recompute_mm_shapes_by_fqns` are removed. The graph trainer keeps its own copy of the operator save set. * `configure_remat_regions` gains `save_all_except`, which SelectiveAC uses: it suits starting from no AC and recomputing only a few regions. * FullAC is unchanged and still uses PyTorch checkpointing. * The SAC + FlexInnerAttention restriction under SPMD typechecking is removed. <img width="1933" height="833" alt="image" src="https://github.com/user-attachments/assets/5937fbba-5e72-45db-acad-e6602e8b2340" /> <img width="1939" height="844" alt="image" src="https://github.com/user-attachments/assets/f8932afb-5f9c-4fc9-8c95-d0046af341f8" />
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: e756268 Pull-Request: #5102
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs applied with one function, since the policy objects held nothing beyond their config: * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `apply_activation_remat(model, config)` replaces `config.build(dump_folder=...).apply(model)`. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. No behavior change; call sites, recipes, tests and docs are renamed. ghstack-source-id: bd6ef42 Pull-Request: #5102
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0) (oldest at bottom): * #5102 * __->__ #5101 ---- * FullAC is now a fixed RegionAC preset with `save_regions=[]`, so every activation-checkpointing policy runs on torch_remat and the `torch.utils.checkpoint` path is removed. Measured on Qwen3 MoE and DeepSeek V3 at EP4, activation and peak memory are unchanged, steps are 5-6 ms faster (routing decisions are retained instead of replayed), and losses are bitwise identical. * Replay no longer re-runs forward side effects under FullAC: the aux-loss metric and the expert token counts are counted once. The `// 2` correction for FullAC token counts is removed. * Flux checkpoints its blocks with torch_remat instead of `checkpoint_wrapper`. * The dynamo LRU-cache workaround for PyTorch checkpointing with PP and Flex is removed. * FullAC now requires `preserve_rng_state=False` and rejects `debug=True`, like the other policies.
[ghstack-poisoned]
…figs The policies are plain configs applied with one function, since the policy objects held nothing beyond their config. User-facing names say "activation checkpoint"; torch_remat stays the implementation: * `ActivationCheckpointConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationCheckpointConfig` (formerly `FullAC.Config`) and `DefaultActivationCheckpointConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationCheckpointConfig` is the way to pin a policy. * `apply_activation_checkpoint(model, config)` replaces `config.build(dump_folder=...).apply(model)`. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. * The `ActivationCheckpointingConfig` type alias is removed: it was the union of these configs and None, so annotations use `ActivationCheckpointConfig | None`. The trainer field keeps its name, `activation_checkpoint`. No behavior change; call sites, recipes, tests and docs are renamed, including tests that landed after the policies were introduced (`test_aux_loss.py`, `test_remat.py`'s saved view-output test, and the `alphabet_sort` RL test recipe). ghstack-source-id: 03415c3 Pull-Request: #5102
| activation_checkpoint={ | ||
| "none": None, | ||
| "selective": SelectiveAC.Config(), | ||
| "full": FullAC.Config(), | ||
| "selective": DefaultActivationCheckpointConfig(), | ||
| "full": FullActivationCheckpointConfig(), | ||
| }[activation_checkpoint_mode], |
There was a problem hiding this comment.
shall we migrate this away from string usage as well?
| parallelism: ParallelismConfig, | ||
| local_compile_regions: list[str], | ||
| ac_config: ActivationCheckpointingConfig | None, | ||
| ac_config: ActivationCheckpointConfig | None, |
There was a problem hiding this comment.
do you think it's OK to do
| ac_config: ActivationCheckpointConfig | None, | |
| remat: ActivationRematConfig | None, |
or
| ac_config: ActivationCheckpointConfig | None, | |
| activation_remat: ActivationRematConfig | None, |
to be consistent with TrainingEngine
| checkpointer: CheckpointManager.Config | None = None | ||
| activation_checkpoint: ActivationCheckpointingConfig = field( | ||
| default_factory=SelectiveAC.Config | ||
| activation_checkpoint: ActivationCheckpointConfig | None = field( |
There was a problem hiding this comment.
| activation_checkpoint: ActivationCheckpointConfig | None = field( | |
| activation_remat: ActivationRematConfig | None = field( |
| class ActivationCheckpointing(Configurable): | ||
| """Base class for activation checkpointing policies. | ||
| @dataclass(kw_only=True, slots=True) | ||
| class ActivationCheckpointConfig: |
There was a problem hiding this comment.
| class ActivationCheckpointConfig: | |
| class ActivationRematConfig: |
I've heard many complaints about "activation checkpointing" as a bad name, as in "oh you applied checkpointing, but why you are having less memory usage?" -- "because you used to save (checkpoint) everything!". Now that we are using torch_remat, let's just migrate from the bad name?
There was a problem hiding this comment.
Hmm, so claude did change it to ActivationRematConfig first and I asked it to change it back to use the word "checkpoint" because that is more common word.
I've heard many complaints about "activation checkpointing" as a bad name, as in "oh you applied checkpointing, but why you are having less memory usage?"
Do you really hear this complain? The whole point of AC is to actually reduce memory footprint.
There was a problem hiding this comment.
The whole point of AC, as a mechanism, is to reduce memory yes. But the name is confusing
- people thought turning on activation checkpointing saves more (because you checkpoint)
- but reality is it saves less (because you save everything, not just the checkpoint)
Let me ask you the reverse question. Why'd Ed / jax name it remat, not checkpointing?
There was a problem hiding this comment.
My guess that Ed chose "remat" is to avoid confusion with distributed checkpointing, not because of the confusion arising from activation checkpointing.
Jax seems to have aliased names - https://docs.jax.dev/en/latest/301/remat.html with jax.checkpoint being more common, and having the same semantics as PyTorch (if you apply checkpoint, you actually save memory).
I think the reason for aliasing with "jax.remat" is same - avoid confusion with DCP.
I can buy that we should rename it to Remat to avoid the confusion with DCP.
There was a problem hiding this comment.
also I think no AC vs. selective AC vs. full AC is bad naming
- no -> selective -> full, seems to suggesting checkpointing more, but it's less
There was a problem hiding this comment.
I agree with the last ladder argument. I have found myself getting confused with FullAC.
I will switch to remat versions.
| ) | ||
|
|
||
|
|
||
| def _check_fixed_policy( |
There was a problem hiding this comment.
this can be a method of the config?
| ActivationCheckpointingConfig = ( | ||
| SelectiveAC.Config | RegionAC.Config | FullAC.Config | None | ||
| ) | ||
| def apply_activation_checkpoint( |
There was a problem hiding this comment.
I think it's better to make this a method of the config, e.g. activation_remat.apply(model). The main reason is encapsulation -- unlikely, but one can imagine replacing our torch_remat AC mechanism and have their own 3rdPartyACConfig and override this method.
| SelectiveAC.Config | RegionAC.Config | FullAC.Config | None | ||
| ) | ||
| def apply_activation_checkpoint( | ||
| model: nn.Module, |
There was a problem hiding this comment.
type should be torchtitan Model?
[ghstack-poisoned]
The policies are plain configs that apply themselves, since the policy objects held nothing beyond their config. The API says "remat" (activation rematerialization, also known as activation checkpointing): the presets then read in the direction they act -- "full remat" recomputes the most -- and the names no longer collide with distributed checkpointing (the trainer's `checkpointer`). * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `config.apply(model)` replaces `config.build(dump_folder=...).apply(model)`; `model` is a torchtitan `Module` whose `layers` are the transformer blocks. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. The fixed-preset check is a config method. * The trainer field `activation_checkpoint` is renamed `activation_remat`, and the `ac_config` parameters of the parallelize and pipeline functions follow it. The `ActivationCheckpointingConfig` alias is removed: it was the union of these configs and None, so annotations use `ActivationRematConfig | None`. * The module moves from `torchtitan/distributed/activation_checkpoint.py` to `torchtitan/distributed/activation_remat.py`. * graph_trainer's minimal-trainer test helper takes an `activation_remat` config instead of a mode string. No behavior change; call sites, recipes, tests and docs are updated, including tests that landed after the policies were introduced (`test_aux_loss.py`, `test_remat.py`'s saved view-output test, and the `alphabet_sort` RL test recipe). ghstack-source-id: 536ac3c Pull-Request: #5102
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs that apply themselves, since the policy objects held nothing beyond their config. The API says "remat" (activation rematerialization, also known as activation checkpointing): the presets then read in the direction they act -- "full remat" recomputes the most -- and the names no longer collide with distributed checkpointing (the trainer's `checkpointer`). * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `config.apply(model)` replaces `config.build(dump_folder=...).apply(model)`; `model` is a torchtitan `Module` whose `layers` are the transformer blocks. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. The fixed-preset check is a config method. * The trainer field `activation_checkpoint` is renamed `activation_remat`, and the `ac_config` parameters of the parallelize and pipeline functions follow it. The `ActivationCheckpointingConfig` alias is removed: it was the union of these configs and None, so annotations use `ActivationRematConfig | None`. * The module moves from `torchtitan/distributed/activation_checkpoint.py` to `torchtitan/distributed/activation_remat.py`. * graph_trainer's minimal-trainer test helper takes an `activation_remat` config instead of a mode string. No behavior change; call sites, recipes, tests and docs are updated, including tests that landed after the policies were introduced (`test_aux_loss.py`, `test_remat.py`'s saved view-output test, and the `alphabet_sort` RL test recipe). ghstack-source-id: 26e216d Pull-Request: #5102
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs that apply themselves, since the policy objects held nothing beyond their config. The API says "remat" (activation rematerialization, also known as activation checkpointing): the presets then read in the direction they act -- "full remat" recomputes the most -- and the names no longer collide with distributed checkpointing (the trainer's `checkpointer`). * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `config.apply(model)` replaces `config.build(dump_folder=...).apply(model)`; `model` is a torchtitan `Module` whose `layers` are the transformer blocks. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. The fixed-preset check is a config method. * The trainer field `activation_checkpoint` is renamed `activation_remat`, and the `ac_config` parameters of the parallelize and pipeline functions follow it. The `ActivationCheckpointingConfig` alias is removed: it was the union of these configs and None, so annotations use `ActivationRematConfig | None`. * The module moves from `torchtitan/distributed/activation_checkpoint.py` to `torchtitan/distributed/activation_remat.py`. * graph_trainer's minimal-trainer test helper takes an `activation_remat` config instead of a mode string. No behavior change; call sites, recipes, tests and docs are updated, including tests that landed after the policies were introduced (`test_aux_loss.py`, `test_remat.py`'s saved view-output test, the transformers backend's HF MoE remat test, and the `alphabet_sort` RL test recipe). ghstack-source-id: ea133eb Pull-Request: #5102
…re consumers (#5374) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0) (oldest at bottom): * #5102 * __->__ #5374 The default remat policy saves the Titan MoE's last region: `combine` with EP, `shared_add` with shared experts, or `tp_output_reduction` with TP and no sequence parallelism. A saved region is skipped during checkpoint replay and its output is a placeholder unless a consumer declares that it needs the data. Titan blocks consume the MoE output with the retained `ffn_residual` add, which replay also skips, but HF decoder layers read it with bare ops that replay re-runs: Qwen3 MoE's `residual + hidden_states`, and Gemma4's post-feedforward norm. transformers_modeling_backend_moe_fsdp+tp+ep+cp has failed in backward since #4893: RuntimeError: layers.3::mlp.routed_experts.token_dispatcher.combine.out is a placeholder for the output of remat.region 'layers.3::mlp.routed_experts.token_dispatcher.combine' (recompute=False) `_HFBatchedMoE.forward` is the boundary where the output leaves Titan code, so it declares the output with `remat.recompute_needs_tensor`, as REMAT.md prescribes for consumers outside the owning module. The output is now kept from the forward to the backward, which a bare HF consumer requires. The backend wraps HF linears and attention without Titan regions, so the MoE output is the only saved Titan output that HF code reads. Test: a tiny HF DeepSeek-V3 model through the real probe, swap and module conversion path, under the default policy. Its shared experts make the MoE output the saved `shared_add` output, so it reproduces the failure on one GPU without EP; with the fix, loss and gradients match no checkpointing bitwise. transformers_modeling_backend_moe_fsdp+tp+ep+cp (8 GPUs) now passes, and over 5 seeded deterministic steps its loss and grad_norm match a run without activation checkpointing bitwise. Fixes #5363
[ghstack-poisoned]
[ghstack-poisoned]
The policies are plain configs that apply themselves, since the policy objects held nothing beyond their config. The API says "remat" (activation rematerialization, also known as activation checkpointing): the presets then read in the direction they act -- "full remat" recomputes the most -- and the names no longer collide with distributed checkpointing (the trainer's `checkpointer`). * `ActivationRematConfig` (formerly `RegionAC.Config`) saves regions matching `save_regions` except those matching `recompute_regions`. * `FullActivationRematConfig` (formerly `FullAC.Config`) and `DefaultActivationRematConfig` (formerly `SelectiveAC.Config`, the trainer default) are fixed presets of it. The default preset's regions may change as measurements improve; an explicit `ActivationRematConfig` is the way to pin a policy. * `config.apply(model)` replaces `config.build(dump_folder=...).apply(model)`; `model` is a torchtitan `Module` whose `layers` are the transformer blocks. Per-block saved-tensor hooks, previously a `RegionAC.get_saved_tensors_hooks` override, are passed as its `saved_tensors_hooks` callable. The fixed-preset check is a config method. * The trainer field `activation_checkpoint` is renamed `activation_remat`, and the `ac_config` parameters of the parallelize and pipeline functions follow it. The `ActivationCheckpointingConfig` alias is removed: it was the union of these configs and None, so annotations use `ActivationRematConfig | None`. * The module moves from `torchtitan/distributed/activation_checkpoint.py` to `torchtitan/distributed/activation_remat.py`. * graph_trainer's minimal-trainer test helper takes an `activation_remat` config instead of a mode string. No behavior change; call sites, recipes, tests and docs are updated, including tests that landed after the policies were introduced (`test_aux_loss.py`, `test_remat.py`'s saved view-output test, the transformers backend's HF MoE remat test, and the `alphabet_sort` RL test recipe). ghstack-source-id: 5680e36 Pull-Request: #5102
Stack from ghstack (oldest at bottom):
The policies are plain configs applied with one function, since the policy
objects held nothing beyond their config. User-facing names say "activation
checkpoint"; torch_remat stays the implementation:
ActivationCheckpointConfig(formerlyRegionAC.Config) saves regionsmatching
save_regionsexcept those matchingrecompute_regions.FullActivationCheckpointConfig(formerlyFullAC.Config) andDefaultActivationCheckpointConfig(formerlySelectiveAC.Config, thetrainer default) are fixed presets of it. The default preset's regions may
change as measurements improve; an explicit
ActivationCheckpointConfigisthe way to pin a policy.
apply_activation_checkpoint(model, config)replacesconfig.build(dump_folder=...).apply(model). Per-block saved-tensor hooks,previously a
RegionAC.get_saved_tensors_hooksoverride, are passed as itssaved_tensors_hookscallable.ActivationCheckpointingConfigtype alias is removed: it was the unionof these configs and None, so annotations use
ActivationCheckpointConfig | None. The trainer field keeps its name,activation_checkpoint.No behavior change; call sites, recipes, tests and docs are renamed, including
tests that landed after the policies were introduced (
test_aux_loss.py,test_remat.py's saved view-output test, and thealphabet_sortRL testrecipe).