-
Notifications
You must be signed in to change notification settings - Fork 4.4k
rl: add durable rollout bank for completed groups #6352
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
lauradang
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
lauradang:laurad/rollout-bank-complete-groups
base: main
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c9a310
rl: add durable rollout bank for completed groups
lauradang 6384b49
Merge branch 'main' into laurad/rollout-bank-complete-groups
lauradang 6e3d3be
rl: apply black formatting to rollout bank tests
lauradang 1d3fe94
Merge branch 'main' into laurad/rollout-bank-complete-groups
lauradang 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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
|
|
||
| import numpy as np | ||
|
|
||
| from .registry import get_agent_class | ||
| from ..types import GroupsPerEnv | ||
| from .api import ( | ||
| AgentBaseModel, | ||
| ContrastiveRollout, | ||
|
|
@@ -21,6 +21,7 @@ | |
| RolloutGenerator, | ||
| RolloutRequest, | ||
| ) | ||
| from .registry import get_agent_class | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -189,9 +190,53 @@ async def get_reward_rollouts(self, request: RolloutRequest) -> list[Rollout]: | |
| all_rollouts_lists = await asyncio.gather(*tasks) | ||
| return [rollout for rollouts in all_rollouts_lists for rollout in rollouts] | ||
|
|
||
| def _env_ids(self) -> list[str]: | ||
| """Per-agent env_ids (aligned with ``self.agents``).""" | ||
| env_ids = [] | ||
| for i, (a, config) in enumerate(zip(self.agents, self.agent_configs)): | ||
| env_id = getattr(a, "env_id", None) | ||
| if not env_id and not config.evaluation_only: | ||
| raise ValueError( | ||
| f"Active agent {i} ({type(a).__name__}) has no env_id; it is " | ||
| f"required to weight-balance restored rollout-bank groups by env. " | ||
| f"Set env_id for every non-evaluation agent in the environment config." | ||
| ) | ||
| env_ids.append(env_id or f"agent_{i}") | ||
| return env_ids | ||
|
|
||
| def env_group_targets(self, total_count: int) -> GroupsPerEnv: | ||
| """Per-env group counts for a full batch of ``total_count`` groups. | ||
|
|
||
| The weight split (``_distribute_counts``) keyed by env_id and summed over | ||
| any agents that share an env_id — the whole-batch target used to | ||
| weight-balance injected restored rollout-bank groups. Living here, beside | ||
| ``_distribute_counts``/``_env_ids``, keeps this target and the generator's | ||
| actual split identical. Counts always sum to ``total_count`` | ||
| (weight-proportional, remainder to the largest fractional parts), and | ||
| agents that share an env_id are merged into one entry. | ||
| """ | ||
| target: GroupsPerEnv = {} | ||
| for eid, c in zip(self._env_ids(), self._distribute_counts(total_count)): | ||
| target[eid] = target.get(eid, 0) + c | ||
| return target | ||
|
|
||
| async def get_grouped_rollouts(self, request: GroupedRolloutRequest): | ||
| """Distribute grouped rollouts across sub-agents according to weights.""" | ||
| agent_groups = self._distribute_counts(request.num_groups) | ||
| override: GroupsPerEnv | None = request.num_groups_per_env | ||
| if override is not None: | ||
| # Explicit per-env counts (e.g. the residual after injecting restored | ||
| # rollout-bank groups). Route each env's count to its agent(s). | ||
| env_ids = self._env_ids() | ||
| unknown = set[Any](override) - set(env_ids) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with this |
||
| if unknown: | ||
| raise ValueError( | ||
| f"num_groups_per_env references unknown env_id(s) {sorted(unknown)}; " | ||
| f"known env_ids: {sorted(set(env_ids))}. Check that the envs defined " | ||
| f"in this run are consistent with the restored rollouts." | ||
| ) | ||
| agent_groups = [override.get(eid, 0) for eid in env_ids] | ||
| else: | ||
| agent_groups = self._distribute_counts(request.num_groups) | ||
| if request.submission_granularity == "B": | ||
| # In BATCH mode, pgt counts local batches in flight. agent_groups already | ||
| # splits each batch by weight, so copy pgt to every active agent. | ||
|
|
@@ -202,8 +247,13 @@ async def get_grouped_rollouts(self, request: GroupedRolloutRequest): | |
| else: | ||
| # In GROUP/ROLLOUT mode, pgt counts fine-grained work units, so split it by weight. | ||
| agent_pgts = self._distribute_counts(self.parallel_generation_tasks) | ||
| agent_slots = self._distribute_counts(request.num_groups, distribute_remainder=False) | ||
| agent_slots = np.array(agent_slots) / np.gcd.reduce(agent_slots) | ||
| if override is not None: | ||
| # With an explicit per-env override, slots must follow the residual | ||
| # shape, not a weight split of num_groups. | ||
| raw_slots = list(agent_groups) | ||
| else: | ||
| raw_slots = self._distribute_counts(request.num_groups, distribute_remainder=False) | ||
| agent_slots = np.array(raw_slots) / np.gcd.reduce(raw_slots) | ||
|
|
||
| # Snapshot the distribution for observability. Read back by rl_utils | ||
| # during per-iteration metric logging. | ||
|
|
@@ -242,6 +292,7 @@ async def get_grouped_rollouts(self, request: GroupedRolloutRequest): | |
| f"Agent of type {type(agent)} does not support grouped rollouts" | ||
| ) | ||
| agent.parallel_generation_tasks = pgt | ||
| agent._rollout_bank = self._rollout_bank | ||
| agent_request = GroupedRolloutRequest( | ||
| num_groups=num_groups, | ||
| streaming=request.streaming, | ||
|
|
||
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.
Do single-environment setups currently set
env_id? If so, they would fail this check, when they shouldn't have to because single-env routing is trivial.