[trainer] feat: enable model engine based critic#4507
[trainer] feat: enable model engine based critic#4507wuxibin89 merged 18 commits intoverl-project:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the critic to use the model engine-based TrainingWorker, removing the specialized CriticWorker. This is a significant and positive simplification of the architecture. The changes across the trainer, configurations, and tests are largely consistent with this goal. However, I've identified two critical bugs in verl/workers/engine_workers.py where a .cpu() call on a None value can occur, leading to worker crashes. I've also found a high-severity issue with a misleading assertion message that could hinder debugging. Addressing these points will improve the robustness of the new implementation.
verl/workers/engine_workers.py
Outdated
| else: | ||
| final_output = None | ||
| return final_output | ||
| return final_output.cpu() |
There was a problem hiding this comment.
Potential AttributeError here. final_output can be None if self.engine.is_mp_src_rank_with_outputs() is False (line 285), which will cause None.cpu() to raise an error. The call to .cpu() should be conditional to handle cases where final_output is None.
| return final_output.cpu() | |
| return final_output.cpu() if final_output is not None else None |
verl/workers/engine_workers.py
Outdated
| output = DataProto(batch=None, meta_info={"metrics": metrics}) | ||
|
|
||
| return output | ||
| return final_output.cpu() |
There was a problem hiding this comment.
Potential AttributeError here. final_output can be None if self.engine.is_mp_src_rank_with_outputs() is False (line 322), which will cause None.cpu() to raise an error. The call to .cpu() should be conditional to handle cases where final_output is None.
| return final_output.cpu() | |
| return final_output.cpu() if final_output is not None else None |
| assert mini_batch_size % self.engine.get_data_parallel_size() == 0, ( | ||
| f"Got {batch_size_per_dp=} and {num_mini_batch=}" | ||
| ) |
There was a problem hiding this comment.
The assertion message here is incorrect due to what appears to be a copy-paste error. It references batch_size_per_dp and num_mini_batch, which are from the other if branch. This can be misleading when debugging. The message should reference mini_batch_size and self.engine.get_data_parallel_size().
| assert mini_batch_size % self.engine.get_data_parallel_size() == 0, ( | |
| f"Got {batch_size_per_dp=} and {num_mini_batch=}" | |
| ) | |
| assert mini_batch_size % self.engine.get_data_parallel_size() == 0, ( | |
| f"Got {mini_batch_size=} and data_parallel_size={self.engine.get_data_parallel_size()}" | |
| ) |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request is a significant refactoring that enables the critic to use the model engine-based TrainingWorker, unifying worker implementations and simplifying the trainer logic. The introduction of train_mini_batch in TrainingWorker is a good move to centralize mini-batch handling. I've identified a critical bug in this new method related to handling asynchronous results, which could lead to a runtime error. Additionally, there's a high-severity issue with a metric key typo that would cause incorrect logging. I've provided code suggestions for both issues. Overall, this is a valuable structural improvement, and addressing these points will make it ready for merging.
verl/workers/engine_workers.py
Outdated
| actor_output = self.train_batch(mini_batch_td) | ||
| output_lst.append(actor_output) | ||
|
|
||
| actor_output = [tu.get(output, "metrics") for output in output_lst] |
There was a problem hiding this comment.
It appears there's a bug in how results from train_batch are processed. The train_batch method is decorated with blocking=False, which means it returns a future. The output_lst is therefore a list of futures. However, you are attempting to process this list of futures directly with tu.get without first resolving them (e.g., using .get() or ray.get()). This will likely cause a runtime error as tu.get expects a TensorDict, not a future. You should resolve the futures before processing their results.
| actor_output = [tu.get(output, "metrics") for output in output_lst] | |
| actor_output = [tu.get(output.get(), "metrics") for output in output_lst] |
verl/trainer/ppo/ray_trainer.py
Outdated
| output = tu.get(output, "metrics") | ||
| output = rename_dict(output, "critic/") | ||
| # modify key name | ||
| output["perf/critic/actor"] = output.pop("critic/mfu") |
There was a problem hiding this comment.
There seems to be a typo in the metric key. You are renaming critic/mfu to perf/critic/actor. This is likely a copy-paste error from _update_actor and should probably be perf/mfu/critic. The current key perf/critic/actor is confusing and will conflict with the actor's MFU metric, leading to incorrect logging.
| output["perf/critic/actor"] = output.pop("critic/mfu") | |
| output["perf/mfu/critic"] = output.pop("critic/mfu") |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the PPO trainer to use a generic TrainingWorker for the critic model, instead of a specialized CriticWorker. This is a significant and positive architectural change that improves code reuse and maintainability. The core logic for handling mini-batch updates has been encapsulated within the TrainingWorker, simplifying the main trainer loop. The changes also include updates to tests to reflect the new API, and additions to model registries to support Llama-based token classification models for the critic. My review found one issue related to configuration handling that could lead to incorrect behavior.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
### What does this PR do? Due to fused worker design and ActorRolloutRef is a async Ray Actor, we have to temporarily implement `train_mini_batch` API for TrainingWorker and use it to implement update_actor and update_critic In this PR, we - Completely remove CriticWorker in engine and directly use TrainingWorker - Implement e2e ppo process using model engine - Results on char count task matches with legacy worker implementation - Note that currently megatron backend for model engine value model is not supported until mbridge officially supports value model save/load <img width="365" height="267" alt="image" src="https://github.com/user-attachments/assets/4edafe71-6929-4a3a-84fa-d653cad826e0" /> - orange: ppo with legacy worker - blue: ppo with model engine worker - white: grpo with model engine worker ### Checklist Before Starting - [ ] Search for similar PRs. Paste at least one query link here: ... - [ ] 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. - [ ] Read the [Contribute Guide](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md). - [ ] 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` - [ ] Add / Update [the documentation](https://github.com/volcengine/verl/tree/main/docs). - [ ] 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: ... - [ ] 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).) --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
### What does this PR do? Due to fused worker design and ActorRolloutRef is a async Ray Actor, we have to temporarily implement `train_mini_batch` API for TrainingWorker and use it to implement update_actor and update_critic In this PR, we - Completely remove CriticWorker in engine and directly use TrainingWorker - Implement e2e ppo process using model engine - Results on char count task matches with legacy worker implementation - Note that currently megatron backend for model engine value model is not supported until mbridge officially supports value model save/load <img width="365" height="267" alt="image" src="https://github.com/user-attachments/assets/4edafe71-6929-4a3a-84fa-d653cad826e0" /> - orange: ppo with legacy worker - blue: ppo with model engine worker - white: grpo with model engine worker ### Checklist Before Starting - [ ] Search for similar PRs. Paste at least one query link here: ... - [ ] 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. - [ ] Read the [Contribute Guide](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md). - [ ] 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` - [ ] Add / Update [the documentation](https://github.com/volcengine/verl/tree/main/docs). - [ ] 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: ... - [ ] 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).) --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
What does this PR do?
Due to fused worker design and ActorRolloutRef is a async Ray Actor, we have to temporarily implement
train_mini_batchAPI for TrainingWorker and use it to implement update_actor and update_criticIn this PR, we
Checklist Before Starting
[{modules}] {type}: {description}(This will be checked by the CI){modules}includefsdp,megatron,sglang,vllm,rollout,trainer,ci,training_utils,recipe,hardware,deployment,ray,worker,single_controller,misc,perf,model,algo,env,tool,ckpt,doc,data,like[megatron, fsdp, doc]{type}is infeat,fix,refactor,chore,test[BREAKING]to the beginning of the title.[BREAKING][fsdp, megatron] feat: dynamic batchingTest
API and Usage Example
# Add code snippet or script demonstrating how to use thisDesign & Code Changes
Checklist Before Submitting
Important
Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.
pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=alwaysci-requestchannel in theverlSlack workspace. (If not accessible, please try the Feishu group (飞书群).)