-
Notifications
You must be signed in to change notification settings - Fork 701
[WIP][rl] refactor grader and trainer generator actor #2244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wwwjn
wants to merge
7
commits into
gh/wwwjn/7/base
Choose a base branch
from
gh/wwwjn/7/head
base: gh/wwwjn/7/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3260c95
refactor scorer and trainer generator actor
wwwjn d2e0fef
Update on "[rl] refactor grader and trainer generator actor"
wwwjn 39b7d87
Update on "[rl] refactor grader and trainer generator actor"
wwwjn 60662f2
Update on "[rl] refactor grader and trainer generator actor"
wwwjn b83ff15
Update on "[rl] refactor grader and trainer generator actor"
wwwjn 38628ac
Update on "[rl] refactor grader and trainer generator actor"
wwwjn 7bbcec5
Update on "[WIP][rl] refactor grader and trainer generator actor"
wwwjn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import logging | ||
| from dataclasses import dataclass | ||
| from typing import Callable, List, Optional | ||
|
|
||
| import torch | ||
| from monarch.actor import Actor, endpoint | ||
| from torchtitan.experiments.rl.unified.job_config import JobConfig | ||
| from torchtitan.experiments.rl.vllm_compat.simple_rl import trivial_reward_function | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class TrajectoryData: | ||
| """ | ||
| Data from one generation batch. | ||
|
|
||
| Attributes: | ||
| policy_version: Version of policy that produced this batch | ||
| completions: List of completion strings | ||
| vllm_token_ids: List of token ID lists for each completion | ||
| vllm_token_log_probs: List of per-token log prob lists | ||
| prompt_token_ids: List of prompt token ID lists | ||
| expected_answers: List of expected answers for reward computation | ||
| rewards: Rewards for each completion (initialized to zeros, filled by Scorer) | ||
| """ | ||
|
|
||
| policy_version: int | ||
| completions: List[str] | ||
| vllm_token_ids: List[List[int]] | ||
| vllm_token_log_probs: List[List[float]] | ||
| prompt_token_ids: List[List[int]] | ||
| expected_answers: List[str] | ||
| rewards: torch.Tensor | ||
|
|
||
|
|
||
| class Scorer(Actor): | ||
|
||
| """ | ||
| Evaluates completions and assigns rewards to trajectory data. | ||
|
|
||
| The Scorer receives trajectory data from the Generator | ||
| and computes rewards using a reward function. Advantage computation | ||
| is done by the Trainer. | ||
|
|
||
| Args: | ||
| job_config: JobConfig dataclass containing all configuration | ||
| reward_fn: Optional custom reward function. If not provided, | ||
| uses trivial_reward_function from simple_rl. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| job_config: JobConfig, | ||
| reward_fn: Optional[Callable] = None, | ||
| ): | ||
| # Extract needed fields from job_config | ||
| self.group_size = job_config.rl.grpo_group_size | ||
|
|
||
| # Set reward function | ||
| self.reward_fn = reward_fn if reward_fn is not None else trivial_reward_function | ||
|
|
||
| logger.info(f"Scorer initialized with group_size={self.group_size}") | ||
|
|
||
| @endpoint | ||
| async def score(self, trajectory: TrajectoryData) -> TrajectoryData: | ||
| """ | ||
| Score a trajectory by computing rewards. | ||
|
|
||
| Args: | ||
| trajectory: Trajectory data (with or without rewards) | ||
|
|
||
| Returns: | ||
| TrajectoryData with computed rewards | ||
| """ | ||
| logger.info( | ||
| f"Scorer scoring trajectory (policy v{trajectory.policy_version})..." | ||
| ) | ||
|
|
||
| # Compute rewards using reward function | ||
| rewards = self.reward_fn( | ||
| trajectory.completions, | ||
| trajectory.expected_answers, | ||
| self.group_size, | ||
| ) | ||
|
|
||
| reward_mean = rewards.mean() | ||
| reward_std = rewards.std() | ||
|
|
||
| # Update trajectory with rewards | ||
| trajectory.rewards = rewards | ||
|
|
||
| logger.info( | ||
| f"Scorer finished scoring: " | ||
| f"reward_mean={reward_mean.item():.4f}, reward_std={reward_std.item():.4f}" | ||
| ) | ||
|
|
||
| return trajectory | ||
|
|
||
| @endpoint | ||
| async def set_reward_fn(self, reward_fn: Callable) -> None: | ||
| """ | ||
| Update the reward function. | ||
|
|
||
| Args: | ||
| reward_fn: New reward function to use | ||
| """ | ||
| self.reward_fn = reward_fn | ||
| logger.info("Scorer reward function updated") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we deprecated the name trajectory which is intrinsically ambiguous, but I don't know what we replace it by, Episode?