Skip to content

Refactor activation checkpointing into a policy class hierarchy - #3674

Merged
tianyu-l merged 1 commit into
mainfrom
flexible_ac
Jun 15, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
flexible_ac

Conversation

@tianyu-l

Copy link
Copy Markdown
Contributor

What

Replace the flat ActivationCheckpointConfig (with a mode string) with a Configurable policy hierarchy in torchtitan/distributed/activation_checkpoint.py:

  • ActivationCheckpointing (base) + FullAC / SelectiveAC / MemoryBudgetAC, each a Configurable with a nested Config and an apply(model) method. The old apply_ac free function (and _apply_full_ac / _apply_op_sac / _apply_ac_to_transformer_block) are removed; callsites now do ac_config.build(...).apply(model).
  • The Trainer field becomes a tyro subcommand union ActivationCheckpointingConfig (selective / full / memory-budget / none), where None disables AC (replacing mode="none").
  • Users tune the per-op SAC save set by subclassing SelectiveAC and overriding get_save_ops(), then assigning their Config in a Python config function — no registry, no string indirection.

Why

Activation checkpointing is a hardware / memory-budget knob (like ParallelismConfig), not a model property. Keeping it in the Trainer config lets users mix-and-match AC policies with parallelism per hardware setup, while the class hierarchy gives each policy exactly its relevant fields and a clean, typed extension point for customizing save_ops — the original motivation (the per-op SAC save set was previously hardcoded and identical for all models).

Other changes

  • Remove model_compile_enabled from the AC path; the memory-budget-requires-compile check moves to Trainer.Config.__post_init__ (and the forge engine).
  • Drop the early_stop config field (universally False); hardcode early_stop=False at the ptd_checkpoint_wrapper calls.
  • RL trainer now defaults to SelectiveAC.Config.
  • Rename per_op_sac_force_recompute_mm_shapes_by_fqns → force_recompute_mm_shapes_by_fqns and _get_save_ops → _get_default_save_ops.

CLI behavior change

Switching/disabling AC from the CLI is now a tyro subcommand:

... activation-checkpoint:full
... activation-checkpoint:none
... activation-checkpoint:selective --activation-checkpoint.determinism-check none

Overriding an AC scalar requires naming the subcommand first (top-level flags must precede the subcommand token). Policy selection in Python config_registry functions is unaffected. Integration tests updated accordingly.

Validation

  • pre-commit run --all-files passes (flake8, ufmt, pyrefly, pydoclint, codespell, …).
  • AC unit tests pass (tests/unit_tests/test_activation_checkpoint.py).
  • Numerics: llama3 debugmodel, 10 steps, --debug.seed 42 --debug.deterministic — loss and grad_norm are bitwise-identical before vs. after (pure refactor).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 15, 2026
@tianyu-l
tianyu-l requested a review from shuhuayu June 15, 2026 07:17
Replace the flat `ActivationCheckpointConfig` (with a `mode` string) with a
`Configurable` policy hierarchy living next to its logic in
`torchtitan/distributed/activation_checkpoint.py`:

- `ActivationCheckpointing` (base) + `FullAC` / `SelectiveAC` /
  `MemoryBudgetAC`, each a `Configurable` with a nested `Config` and an
  `apply(model)` method. The old `apply_ac` free function and its helpers
  are removed; callsites now do `ac_config.build(...).apply(model)`.
- The Trainer field becomes a tyro subcommand union
  `ActivationCheckpointingConfig` (`selective` / `full` / `memory-budget` /
  `none`), where `None` disables AC (replacing `mode="none"`).
- Users customize the per-op SAC save set by subclassing `SelectiveAC` and
  overriding `get_save_ops()`, then assigning their `Config` in a Python
  config function -- no registry, no string indirection.

Why: AC policy is a hardware/memory-budget knob (like `ParallelismConfig`),
not a model property, so it stays in the Trainer config and composes freely
with parallelism. The hierarchy gives each policy exactly its relevant
fields and a clean, typed extension point for tuning `save_ops`.

Other changes:
- Remove `model_compile_enabled` from the AC path; the
  memory-budget-requires-compile check moves to `Trainer.Config.__post_init__`
  (and forge engine).
- Drop the `early_stop` config field (universally `False`); hardcode
  `early_stop=False` at the `ptd_checkpoint_wrapper` calls.
- RL trainer now defaults to `SelectiveAC.Config`.
- Rename `per_op_sac_force_recompute_mm_shapes_by_fqns` ->
  `force_recompute_mm_shapes_by_fqns` and `_get_save_ops` ->
  `_get_default_save_ops`.

CLI note: switching/disabling is now `activation-checkpoint:{full,none,
memory-budget}`; overriding an AC scalar requires naming the subcommand
first (e.g. `activation-checkpoint:selective
--activation-checkpoint.determinism-check none`). Policy selection in Python
config functions is unaffected.

Validation: pre-commit passes; AC unit tests pass; llama3 debugmodel with
`--debug.seed 42 --debug.deterministic` produces bitwise-identical loss and
grad_norm before vs. after (pure refactor).
Annotated[SelectiveAC.Config, tyro.conf.subcommand("selective")]
| Annotated[FullAC.Config, tyro.conf.subcommand("full")]
| Annotated[MemoryBudgetAC.Config, tyro.conf.subcommand("memory-budget")]
| Annotated[None, tyro.conf.subcommand("none")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can also override it to be None via CLI activation-checkpointing:none?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes -- activation-checkpoint:none.
That's the benefit of this code snippet.

@tianyu-l
tianyu-l merged commit c5d93d1 into main Jun 15, 2026
28 of 32 checks passed
@tianyu-l
tianyu-l deleted the flexible_ac branch June 15, 2026 19:27
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jun 24, 2026
… sync)

Upstream c5d93d1 replaced the flat ActivationCheckpointConfig with a
Configurable policy hierarchy: FullAC / SelectiveAC / MemoryBudgetAC /
None (None disables AC, replacing mode="none"). Callsites change from
apply_ac(model, cfg, ...) to cfg.build(...).apply(model). The old
ActivationCheckpointConfig symbol is gone from torchtitan.config, so
ezpz could not import until replayed.

Replayed:
- agpt/parallelize.py + moe/parallelize.py: ac_config type ->
  ActivationCheckpointingConfig; the "if ac_config.mode != none:
  apply_ac(...)" guard becomes "if ac_config is not None:
  ac_config.build(dump_folder=...).apply(model)".
- agpt/config_registry.py: ActivationCheckpointConfig(mode=full) ->
  FullAC.Config(); the agpt() wrapper's activation_checkpoint_mode
  knob now maps none->None, full->FullAC.Config().
- moe/config_registry.py: the moe() wrapper maps none->None,
  full->FullAC.Config(), selective->MoeSelectiveAC.Config().
- moe/activation_checkpoint.py: rewrote the _get_save_ops monkey-patch
  (broken by the refactor's _get_save_ops -> _get_default_save_ops
  rename and apply_ac removal) as MoeSelectiveAC, a clean SelectiveAC
  subclass overriding get_save_ops() to drop
  _c10d_functional.all_to_all_single -- the extension point the new
  API was designed for. Same effect: forces recompute of the EP a2a
  on backward, avoiding PR14's dangling AsyncCollectiveTensor save.

Smoke (job 12469466): agpt_debugmodel with FullAC and moe_debugmodel
with MoeSelectiveAC both ran 2 deterministic steps rc=0 (see prior
commit for losses).
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jun 24, 2026
…ytorch#3674)

PR pytorch#3674 made activation_checkpoint a tyro subcommand union, so the old
`--activation_checkpoint.mode=full` flag no longer parses (tyro errors
with 'Unrecognized options'). The replacement is the positional token
`activation-checkpoint:full`, which must come AFTER every --flag.

Updated 7 scripts:
- benchmark_80b, bitwise_sync_check, _smoke_agpt_2b_hf_n2, smoke_agpt_tp2:
  drop the --activation_checkpoint.mode=full flag, append
  activation-checkpoint:full as the last launch arg.
- run_scaling_study_aurora: map the ac_mode var (none/full/selective)
  to an activation-checkpoint:<policy> token, appended last.
- submit_agpt_80b_aurora_venv_failover: map ACKPT_MODE the same way,
  append after "$@".
- submit_agpt_20b: comment-only example updated.

These are launcher scripts (separate from the model-code replay in
fa6f068 / bb38b95). bash -n clean on all 7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants