Skip to content

Refactor optimizer configuration - #4904

Merged
tianyu-l merged 1 commit into
mainfrom
optimizer
Sep 28, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
optimizer

Conversation

@tianyu-l

Copy link
Copy Markdown
Contributor

Summary

  • replace string-selected optimizer factories with configurable Adam, AdamW, and DistMuon classes
  • construct FlexShard DistMuon directly while keeping it independent of TorchTitan configuration
  • move MoE balancing hooks next to the MoE implementation

Testing

  • pre-commit run --all-files
  • focused CPU/GPU optimizer, checkpoint, scheduler, TorchFT, MoE, and DistMuon tests
  • exact one-step AdamW parity and fused BF16-moment smoke checks

Full pytest collection is blocked locally by installed PyTorch/torchao private API mismatches.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Sep 26, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 26, 2026
@tianyu-l
tianyu-l added this pull request to stack #4906 September 26, 2026 23:09
@tianyu-l
tianyu-l force-pushed the optimizer branch 2 times, most recently from ac3975b to ffa248a Compare September 27, 2026 02:24
@tianyu-l
tianyu-l removed this pull request from stack #4906 September 27, 2026 04:41
@tianyu-l
tianyu-l added this pull request to stack #4910 September 27, 2026 04:41

@wwwjn wwwjn 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.

This direction looks good to me , and learned a lot about optimizer

meta-codesync Bot pushed a commit to meta-pytorch/flex_shard that referenced this pull request Oct 8, 2026
…anges

Summary:
Recent TorchTitan syncs changed several APIs that `experiments/fb/flex_shard` depends on, so `flex_shard_tests` now fails at import time (`unittest` `loadTestsFromName` ImportError). This diff moves the fb FlexShard code to the new APIs, fixes the EP>1 DistMuon break from #4419, and ports the Qwen3 recipe.

- pytorch/torchtitan#4904 (optimizer config refactor): `build_dist_muon(...)` is replaced by the `DistMuon(...)` class, which takes the same arguments. `comm_free_adapter.build_local_dist_muon` now constructs `DistMuon` directly.
- pytorch/torchtitan#4660 (`components.optimizer` -> `components.optim`, `Trainer.Config.optim`):
  - Imports now come from `torchtitan.components.optim`.
  - DSV3 Muon configs set `config.optim.optimizer`.
  - `FlexShardDistMuonOptimizers` accepts the new `enable_cuda_graph` kwarg. It raises if the flag is set, the same way upstream `OptimizersContainer` rejects optimizers that can't be captured.
  - `_log_optimizer` now takes a pattern string.
- pytorch/torchtitan#4617: `ParallelismConfig` now lives in `torchtitan.config.parallelism`.
- pytorch/torchtitan#4905:
  - `ParallelDims` is now `ParallelismContext`, and the `parallel_dims=` kwarg is now `parallelism_context=`.
  - `get_spmd_context(...)` is replaced by `parallelism_context.activate_spmd()`.
  - The `efsdp` mesh axis is now `edp_shard`. This also covers the `partition_rank_axis_name` passed to the DistMuon bucket planner.
  - Tests build the context with `ParallelismContext.from_config(config, DistributedTopology(world_size=...))`.
- pytorch/torchtitan#4840: `BlockShard(block_size=R)` is now `BlockShard(block_sizes=(R,))`.
- pytorch/torchtitan#4878: the DSV3 `model_registry` now requires `enable_sp`. The FlexShard wrapper passes `enable_sp=False`. FlexShard rejects TP, and `False` keeps the shared-expert `w2` as a plain `Linear`, as it was before #4878.
- pytorch/torchtitan#4419 (EP without DTensor): `Module._parallelize` now leaves EP-sharded routed experts as plain local `nn.Parameter` shards. It records their layouts in each module's `_sharding_config` instead of returning DTensors. With EP>1, DistMuon failed with "Partitioned block parameters must be DTensors", and `dtensor_to_global_layout` no longer found anything to declare.
  - flex_shard `dist_muon/placement.py` and `storage_layout.py` now read the FlexShard `GlobalLayout` contract (`get_global_layout`) instead of DTensor placements. `get_dim0_block_partitions` no longer takes `partition_axis_name`.
  - The new `experiments/fb/flex_shard/spmd_layouts.py` `declare_spmd_global_layouts(model, mesh)` collects `_sharding_config.state_shardings` and declares them through `spmd_types_to_global_layout`. The DSV3 recipes and tests call it where they used to call `dtensor_to_global_layout`.
- EP>1 configs now need `training.disable_cuda_graphs`: upstream config validation rejects CUDA graphs with the all-to-all token dispatcher. `flex_shard_deepseek_v3_debug_muon` sets EP=2 after construction, so it skipped that check and would fail when launched from the CLI. It now disables CUDA graphs, as upstream EP configs do.
- Qwen3 recipe port. It was broken before these syncs, by `protocols.model_spec`, `FusedQKVLinear`, `components.metrics`, `tools.logging`, `default_adamw` and `set_spmd_backend`.
  - It follows the DSV3 pattern: `FlexShardQwen3Model(Qwen3Model)` with `Config.muon_pattern`, and `parallelize()` runs `parallelize_qwen3_muon` under `activate_spmd()`.
  - Configs are rebuilt on the current `Trainer.Config` fields with the same hyperparameters.
  - Q/K/V BlockShard plans use the fused `QKVLinear.wqkv`: one `head_dim`-row matrix per Q/K/V head.
  - `parallelize_qwen3_muon` uses `dp_shard`, always calls `_parallelize` (as DSV3 does), and declares the EP layouts.
- DSV3 `test_debug_muon_train_step` now disables autograd multithreading, as upstream `init_distributed` does. The SPMD mesh is thread-local, and FSDPTest skips `init_distributed`. So selective-AC recompute ran on an autograd device thread with no mesh set, the MoE token dispatch skipped EP there, and backward failed in `torch._grouped_mm` with "matrix batch sizes have to match". The upstream Trainer is not affected: the DSV3 debug model with EP=2 + SAC trains normally.

The `comm_free_adapter` change is the same as the one in D122391176, the auto-generated Test Blame fix. This diff also covers the other breakages in fb/flex_shard.

The DistMuon tests (DSV3 `test_shard0_owned_mixed` and `test_debug_muon_train_step`, and Qwen3 `test_debug_muon_train_step`) now get past every API break. They still fail on the fused `w13` weights, which the next diff (D122443668) fixes.

Reviewed By: weifengpy

Differential Revision: D122692766

Pull Request resolved: #50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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.

4 participants