Skip to content

Commit 6d4fd9a

Browse files
authored
[megatron] fix: respect use_distributed_optimizer in config (verl-project#4392)
### What does this PR do? Currently `use_distributed_optimizer` is hardcoded as `optim_args`, which is unexpected since `use_distributed_optimizer` is a config for `megatron`. ### Checklist Before Starting - [X] Search for similar PRs. Paste at least one query link here: ... - [X] Format the PR title as `[{modules}] {type}: {description}` (This will be checked by the CI) - `{modules}` include `fsdp`, `megatron`, `sglang`, `vllm`, `rollout`, `trainer`, `ci`, `training_utils`, `recipe`, `hardware`, `deployment`, `ray`, `worker`, `single_controller`, `misc`, `perf`, `model`, `algo`, `env`, `tool`, `ckpt`, `doc`, `data` - If this PR involves multiple modules, separate them with `,` like `[megatron, fsdp, doc]` - `{type}` is in `feat`, `fix`, `refactor`, `chore`, `test` - If this PR breaks any API (CLI arguments, config, function signature, etc.), add `[BREAKING]` to the beginning of the title. - Example: `[BREAKING][fsdp, megatron] feat: dynamic batching` ### Test > For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc. ### API and Usage Example > Demonstrate how the API changes if any, and provide usage example(s) if possible. ```python # Add code snippet or script demonstrating how to use this ``` ### Design & Code Changes > Demonstrate the high-level design if this PR is complex, and list the specific changes. ### Checklist Before Submitting > [!IMPORTANT] > Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review. - [X] Read the [Contribute Guide](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md). - [X] Apply [pre-commit checks](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting): `pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=always` - [X] Add / Update [the documentation](https://github.com/volcengine/verl/tree/main/docs). - [X] Add unit or end-to-end test(s) to [the CI workflow](https://github.com/volcengine/verl/tree/main/.github/workflows) to cover all the code. If not feasible, explain why: ... - [X] Once your PR is ready for CI, send a message in [the `ci-request` channel](https://verl-project.slack.com/archives/C091TCESWB1) in [the `verl` Slack workspace](https://join.slack.com/t/verl-project/shared_invite/zt-3855yhg8g-CTkqXu~hKojPCmo7k_yXTQ). (If not accessible, please try [the Feishu group (飞书群)](https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=772jd4f1-cd91-441e-a820-498c6614126a).) Signed-off-by: Hollow Man <hollowman@opensuse.org>
1 parent 7ca9d8a commit 6d4fd9a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

verl/utils/megatron/optimizer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
from verl.utils.logger import print_rank_0
2222

2323

24-
def init_megatron_optim_config(optim_config: dict, fp16: bool = False) -> OptimizerConfig:
24+
def init_megatron_optim_config(
25+
optim_config: dict, use_distributed_optimizer: bool = True, fp16: bool = False
26+
) -> OptimizerConfig:
2527
optim_args = {
2628
"optimizer": optim_config.optimizer,
2729
"lr": optim_config.lr,
2830
"min_lr": optim_config.min_lr,
2931
"clip_grad": optim_config.clip_grad,
3032
"weight_decay": optim_config.weight_decay,
31-
"use_distributed_optimizer": True,
33+
"use_distributed_optimizer": use_distributed_optimizer,
3234
}
3335
if fp16:
3436
optim_args.update(

verl/workers/engine/megatron/transformer_impl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ def _build_optimizer(self):
262262
init_megatron_optim_config,
263263
)
264264

265-
optim_config_megatron = init_megatron_optim_config(self.optimizer_config, self.param_dtype == torch.float16)
265+
optim_config_megatron = init_megatron_optim_config(
266+
self.optimizer_config,
267+
use_distributed_optimizer=self.engine_config.use_distributed_optimizer,
268+
fp16=self.param_dtype == torch.float16,
269+
)
266270
optimizer = get_megatron_optimizer(model=self.module, config=optim_config_megatron)
267271
register_megatron_training_hooks(self.module, optimizer)
268272
return optimizer

verl/workers/megatron_workers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ def _build_model_optimizer(
463463

464464
# TODO: add more optimizer args into config
465465
if self._is_actor:
466-
optim_config_megatron = init_megatron_optim_config(optim_config, fp16=self.dtype == torch.float16)
466+
optim_config_megatron = init_megatron_optim_config(
467+
optim_config,
468+
use_distributed_optimizer=wrap_config.use_distributed_optimizer,
469+
fp16=self.dtype == torch.float16,
470+
)
467471
actor_optimizer = get_megatron_optimizer(model=actor_module, config=optim_config_megatron)
468472
actor_optimizer_scheduler = get_megatron_optimizer_param_scheduler(
469473
optimizer=actor_optimizer, config=optim_config
@@ -1118,7 +1122,11 @@ def _build_critic_model_optimizer(
11181122
print_model_size(critic_module[0])
11191123

11201124
# TODO: add more optimizer args into config
1121-
optim_config_megatron = init_megatron_optim_config(optim_config, fp16=self.dtype == torch.float16)
1125+
optim_config_megatron = init_megatron_optim_config(
1126+
optim_config,
1127+
use_distributed_optimizer=wrap_config.use_distributed_optimizer,
1128+
fp16=self.dtype == torch.float16,
1129+
)
11221130
critic_optimizer = get_megatron_optimizer(model=critic_module, config=optim_config_megatron)
11231131
critic_optimizer_scheduler = get_megatron_optimizer_param_scheduler(
11241132
optimizer=critic_optimizer, config=optim_config

0 commit comments

Comments
 (0)