Repository navigation
Group the optimizer components into a package - #4140
Conversation
|
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
left a comment
There was a problem hiding this comment.
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.
0a03aaf to
407f53c
Compare
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
left a comment
There was a problem hiding this comment.
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, | ||
| ) |
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Absolutely yeah, discussed offline that bc it's a large number of changes to the files, we can address in a separate PR
…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.
…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
…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.
Summary:
@tianyu-l asked for a dedicated folder for the optimizer components, and both
@fegin and @tianyu-l pointed out that
checkpoint_utils.pyis not an optimizermodule 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, andload_flat_optim_state_dictall 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.pyhere andmoves to
components/checkpointer/utils.pyin the next change, which is the onethat creates that folder, so it only ever moves once.
Nothing outside the package has to change its imports:
components/optimizer/__init__.pyre-exportsOptimizersContainer,ParamGroupConfig,default_adamw,register_moe_load_balancing_hook, andLRSchedulersContainer, sotorchtitan.components.optimizerkeeps working andthe scheduler now comes from the same short path as the container.
components/lr_scheduler.pyandcomponents/checkpoint_utils.pystay asforwarding 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.pypasses (68 tests) at this commit, in a venv matching the CPU CI job.
torchtitan.trainerandtorchtitan.components.optimizerboth import standaloneat this commit.
flake8andufmt(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.