Skip to content

Group the optimizer components into a package - #4140

Merged
ivy-zhou merged 1 commit into
pytorch:mainfrom
ivy-zhou:pr4140
Aug 17, 2026
Merged

ivy-zhou merged 1 commit into
pytorch:mainfrom
ivy-zhou:pr4140

Conversation

@ivy-zhou

@ivy-zhou ivy-zhou commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
@tianyu-l asked for a dedicated folder for the optimizer components, and both
@fegin and @tianyu-l pointed out that checkpoint_utils.py is not an optimizer
module even though three of its four public functions are optimizer-only.

Split it on that line and group the result:

components/optimizer.py -> components/optimizer/optimizer.py
components/lr_scheduler.py -> components/optimizer/lr_scheduler.py
components/checkpoint_utils.py:optimizer state helpers
-> components/optimizer/utils.py

init_optim_state, get_flat_optim_state_dict, and load_flat_optim_state_dict
all take a torch.optim.Optimizer, so they move. canonical_fqn(name: str, ...)
does not: it strips activation-checkpoint wrapper segments from a dotted FQN and
is a checkpointing concern. It stays in components/checkpoint_utils.py here and
moves to components/checkpointer/utils.py in the next change, which is the one
that creates that folder, so it only ever moves once.

Nothing outside the package has to change its imports:

  • components/optimizer/__init__.py re-exports OptimizersContainer,
    ParamGroupConfig, default_adamw, register_moe_load_balancing_hook, and
    LRSchedulersContainer, so torchtitan.components.optimizer keeps working and
    the scheduler now comes from the same short path as the container.
  • components/lr_scheduler.py and components/checkpoint_utils.py stay as
    forwarding modules for their old names.

EMA has no module today, so there is nothing to move for it yet.

Test Plan:
pytest tests/unit_tests/test_checkpoint.py tests/unit_tests/test_state_dict_keys.py tests/unit_tests/test_lr_scheduler.py tests/unit_tests/test_optimizer_param_groups.py
passes (68 tests) at this commit, in a venv matching the CPU CI job.

torchtitan.trainer and torchtitan.components.optimizer both import standalone
at this commit. flake8 and ufmt (pinned black 22.12.0 / usort 1.0.5, matching
.pre-commit-config.yaml) are clean on the changed files.


Stack created with Sapling. Best reviewed with ReviewStack.

@ivy-zhou

Copy link
Copy Markdown
Contributor Author

cc: @tianyu-l I'm also happy to move this into an optimizer specific dir if you want? I just wanted to make this because it was getting a bit confusing what should actually go in the components/checkpointer folder.

@tianyu-l tianyu-l left a comment

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.

I'm also happy to move this into an optimizer specific dir if you want?

Yeah I think we can move lr scheduler, optimizer, and maybe ema into a dedicated folder. Not urgent though.

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.

only concern is #4141 (comment)

@fegin fegin left a comment •

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.

canonical_fqn is also used by rl trainer.py. And it is not related to optimizer. Just saying that this function doesn't fit into optimizer_utils naming

@ivy-zhou ivy-zhou changed the title Rename checkpoint_utils to optimizer_utils Group the optimizer components into a package Aug 14, 2026
@ivy-zhou
ivy-zhou force-pushed the pr4140 branch 2 times, most recently from 0a03aaf to 407f53c Compare August 14, 2026 19:33
@ivy-zhou
ivy-zhou marked this pull request as ready for review August 14, 2026 20:02
Summary:
@tianyu-l asked for a dedicated folder for the optimizer components, and both
@fegin and @tianyu-l pointed out that `checkpoint_utils.py` is not an optimizer
module even though three of its four public functions are optimizer-only.

Split it on that line and group the result:

  components/optimizer.py                 -> components/optimizer/optimizer.py
  components/lr_scheduler.py              -> components/optimizer/lr_scheduler.py
  components/checkpoint_utils.py:optimizer state helpers
                                          -> components/optimizer/utils.py

`init_optim_state`, `get_flat_optim_state_dict`, and `load_flat_optim_state_dict`
all take a `torch.optim.Optimizer`, so they move. `canonical_fqn(name: str, ...)`
does not: it strips activation-checkpoint wrapper segments from a dotted FQN and
is a checkpointing concern. It stays in `components/checkpoint_utils.py` here and
moves to `components/checkpointer/utils.py` in the next change, which is the one
that creates that folder, so it only ever moves once.

Nothing outside the package has to change its imports:

- `components/optimizer/__init__.py` re-exports `OptimizersContainer`,
  `ParamGroupConfig`, `default_adamw`, `register_moe_load_balancing_hook`, and
  `LRSchedulersContainer`, so `torchtitan.components.optimizer` keeps working and
  the scheduler now comes from the same short path as the container.
- `components/lr_scheduler.py` and `components/checkpoint_utils.py` stay as
  forwarding modules for their old names.

EMA has no module today, so there is nothing to move for it yet.

Test Plan:
`pytest tests/unit_tests/test_checkpoint.py tests/unit_tests/test_state_dict_keys.py
tests/unit_tests/test_lr_scheduler.py tests/unit_tests/test_optimizer_param_groups.py`
passes (68 tests) at this commit, in a venv matching the CPU CI job.

`torchtitan.trainer` and `torchtitan.components.optimizer` both import standalone
at this commit. `flake8` and `ufmt` (pinned black 22.12.0 / usort 1.0.5, matching
`.pre-commit-config.yaml`) are clean on the changed files.

@fegin fegin left a comment

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.

The BC-compatible shim doesn't seem worth the effort. Just change the downstream usages.

get_flat_optim_state_dict,
init_optim_state,
load_flat_optim_state_dict,
)

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.

Do we still need to re-import? Or should we just let the downstream usage to directly import optimizer.utils?

@@ -4,244 +4,10 @@
# This source code is licensed under the BSD-style license found in the

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.

Why do we still keep this file? Who is using this? If it is the torchtitan-package-internal usage, we should just change them to use the new path.

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.

Absolutely yeah, discussed offline that bc it's a large number of changes to the files, we can address in a separate PR

@ivy-zhou
ivy-zhou merged commit 970fbdd into pytorch:main Aug 17, 2026
20 of 21 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Aug 19, 2026
…imizer

Upstream pytorch#4172 deleted torchtitan/components/lr_scheduler.py. It had become a
re-export shim when pytorch#4140 grouped the optimizer components into a package, so
LRSchedulersContainer now lives in torchtitan.components.optimizer.

Four ezpz registries imported the deleted path: agpt, moe, and the two RL
config registries. Without this every one of them fails at import, which is
the same failure shape as pytorch#4045 (maybe_enable_async_tp) and pytorch#3996
(wire_meshes tp_mesh) -- upstream removes a path, ezpz overrides still name it.

components.optimizer itself survives as a package, so the five other ezpz
files importing default_adamw / OptimizersContainer / ParamGroupConfig need
no change.

Verified: 0 unresolvable torchtitan imports and 0 syntax errors across all
ezpz .py files on the merged tree.
ivy-zhou added a commit that referenced this pull request Aug 20, 2026
…4185)

Summary:
`components/checkpoint_utils.py` became a re-export shim when the
optimizer and
checkpointer components were grouped into packages. Unlike the
lr_scheduler
shim removed in #4172, this one forwarded to two different destinations
at
once, which is what made it worth deleting rather than keeping: reading
an
import of `checkpoint_utils` told you nothing about whether the symbol
was
optimizer plumbing or checkpointer plumbing.

Route each of the four importers to the module that actually defines the
symbol. `canonical_fqn` lives in `checkpointer/utils.py`;
`init_optim_state`,
`get_flat_optim_state_dict`, and `load_flat_optim_state_dict` live in
`optimizer/utils.py`. This also settles the naming objection fegin
raised on
#4140, that `canonical_fqn` does not belong under an optimizer-shaped
name --
its importer in the rl trainer now names the checkpointer package
directly.

The three state-dict helpers are imported from `optimizer.utils` rather
than
re-exported through `optimizer/__init__.py`. They are low-level DCP
plumbing
with two callers between them, not part of the package's public surface,
which
stays `OptimizersContainer`, `LRSchedulersContainer`,
`ParamGroupConfig`, and
`default_adamw`.

This is an import-path change only; no runtime behavior changes.

Test Plan:
`pytest tests/unit_tests/test_state_dict_keys.py
tests/unit_tests/test_checkpoint.py
tests/unit_tests/test_lr_scheduler.py
tests/unit_tests/test_optimizer_param_groups.py
tests/unit_tests/test_torch_checkpointing.py`: 77 passed, 4 subtests
passed.

`test_legacy_checkpoint_utils_imports`, which asserted the shim's
symbols were
identical to the submodule's, is dropped -- it cannot outlive the shim.

`test_legacy_checkpoint_utils_can_be_imported_first` is kept but
retargeted, as
`test_state_dict_helpers_can_be_imported_first`. It guards a real
property
rather than the shim: the `optimizer` and `checkpointer` package
`__init__`
files import from each other, so importing either leaf `utils` module
first in
a fresh interpreter must not close an import cycle. It now subtests both
leaf
modules instead of the single shim.

Also verified:
- No `checkpoint_utils` references remain anywhere in the repo, across
all file
  types, not only Python.
- `import torchtitan.components.checkpoint_utils` now raises
  `ModuleNotFoundError`.
- `ufmt` and `flake8 --config=.flake8` clean on the four changed files.

---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/pytorch/torchtitan/pull/4185).
* #4240
* #4230
* #4191
* #4190
* #4189
* #4188
* #4197
* #4187
* #4186
* __->__ #4185
* #4184
ivy-zhou added a commit that referenced this pull request Aug 20, 2026
…4241)

Summary:
`components/checkpoint_utils.py` became a re-export shim when the
optimizer and
checkpointer components were grouped into packages. Unlike the
lr_scheduler
shim removed in #4172, this one forwarded to two different destinations
at
once, which is what made it worth deleting rather than keeping: reading
an
import of `checkpoint_utils` told you nothing about whether the symbol
was
optimizer plumbing or checkpointer plumbing.

Route each of the four importers to the module that actually defines the
symbol. `canonical_fqn` lives in `checkpointer/utils.py`;
`init_optim_state`,
`get_flat_optim_state_dict`, and `load_flat_optim_state_dict` live in
`optimizer/utils.py`. This also settles the naming objection fegin
raised on
#4140, that `canonical_fqn` does not belong under an optimizer-shaped
name --
its importer in the rl trainer now names the checkpointer package
directly.

The three state-dict helpers are imported from `optimizer.utils` rather
than
re-exported through `optimizer/__init__.py`. They are low-level DCP
plumbing
with two callers between them, not part of the package's public surface,
which
stays `OptimizersContainer`, `LRSchedulersContainer`,
`ParamGroupConfig`, and
`default_adamw`.

This is an import-path change only; no runtime behavior changes.

Test Plan:
`pytest tests/unit_tests/test_state_dict_keys.py
tests/unit_tests/test_checkpoint.py
tests/unit_tests/test_lr_scheduler.py
tests/unit_tests/test_optimizer_param_groups.py
tests/unit_tests/test_torch_checkpointing.py`: 77 passed, 4 subtests
passed.

`test_legacy_checkpoint_utils_imports`, which asserted the shim's
symbols were
identical to the submodule's, is dropped -- it cannot outlive the shim.

`test_legacy_checkpoint_utils_can_be_imported_first` is kept but
retargeted, as
`test_state_dict_helpers_can_be_imported_first`. It guards a real
property
rather than the shim: the `optimizer` and `checkpointer` package
`__init__`
files import from each other, so importing either leaf `utils` module
first in
a fresh interpreter must not close an import cycle. It now subtests both
leaf
modules instead of the single shim.

Also verified:
- No `checkpoint_utils` references remain anywhere in the repo, across
all file
  types, not only Python.
- `import torchtitan.components.checkpoint_utils` now raises
  `ModuleNotFoundError`.
- `ufmt` and `flake8 --config=.flake8` clean on the four changed files.
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