-
Notifications
You must be signed in to change notification settings - Fork 511
send step level advantages to prime rl #941
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
eligotts
wants to merge
3
commits into
main
Choose a base branch
from
eli/step-level-adv
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from verifiers import Rubric | ||
| from verifiers.types import ( | ||
| AssistantMessage, | ||
| RolloutInput, | ||
| State, | ||
| TrajectoryStep, | ||
| TrajectoryStepTokens, | ||
| UserMessage, | ||
| ) | ||
| from verifiers.utils.save_utils import states_to_outputs | ||
|
|
||
|
|
||
| def _make_step( | ||
| *, | ||
| completion_ids: list[int], | ||
| advantage: float | None, | ||
| ) -> TrajectoryStep: | ||
| completion_length = len(completion_ids) | ||
| return TrajectoryStep( | ||
| prompt=[UserMessage(content="q")], | ||
| completion=[AssistantMessage(content="a")], | ||
| response=MagicMock(), | ||
| tokens=TrajectoryStepTokens( | ||
| prompt_ids=[1], | ||
| prompt_mask=[0], | ||
| completion_ids=completion_ids, | ||
| completion_mask=[1] * completion_length, | ||
| completion_logprobs=[-0.1] * completion_length, | ||
| overlong_prompt=False, | ||
| is_truncated=False, | ||
| routed_experts=None, | ||
| ), | ||
| reward=None, | ||
| advantage=advantage, | ||
| is_truncated=False, | ||
| trajectory_id="trajectory-1", | ||
| extras={}, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_score_group_populates_step_completion_advantages_from_state_advantage(): | ||
| def reward_by_length(completion, **kwargs): | ||
| return float(len(str(completion))) | ||
|
|
||
| rubric = Rubric(funcs=[reward_by_length], weights=[1.0]) | ||
|
|
||
| state_a = State( | ||
| input=RolloutInput( | ||
| prompt=[UserMessage(content="prompt-a")], | ||
| answer="", | ||
| task="task", | ||
| example_id=0, | ||
| ) | ||
| ) | ||
| state_a["completion"] = "a" | ||
| state_a["trajectory"] = [_make_step(completion_ids=[11, 12], advantage=None)] | ||
| state_a["timing"] = { | ||
| "generation_ms": 0.0, | ||
| "scoring_ms": 0.0, | ||
| "total_ms": 0.0, | ||
| "start_time": 0.0, | ||
| } | ||
|
|
||
| state_b = State( | ||
| input=RolloutInput( | ||
| prompt=[UserMessage(content="prompt-b")], | ||
| answer="", | ||
| task="task", | ||
| example_id=1, | ||
| ) | ||
| ) | ||
| state_b["completion"] = "bbb" | ||
| state_b["trajectory"] = [_make_step(completion_ids=[21, 22, 23], advantage=None)] | ||
| state_b["timing"] = { | ||
| "generation_ms": 0.0, | ||
| "scoring_ms": 0.0, | ||
| "total_ms": 0.0, | ||
| "start_time": 0.0, | ||
| } | ||
|
|
||
| await rubric.score_group([state_a, state_b]) | ||
|
|
||
| step_a = state_a["trajectory"][0] | ||
| step_b = state_b["trajectory"][0] | ||
|
|
||
| assert state_a["advantage"] == -1.0 | ||
| assert state_b["advantage"] == 1.0 | ||
| assert step_a["advantage"] == state_a["advantage"] | ||
| assert step_b["advantage"] == state_b["advantage"] | ||
| assert step_a["completion_advantages"] == [-1.0, -1.0] | ||
| assert step_b["completion_advantages"] == [1.0, 1.0, 1.0] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_score_group_preserves_existing_step_advantage_for_completion_advantages(): | ||
| def constant_reward(completion, **kwargs): | ||
| return 1.0 | ||
|
|
||
| rubric = Rubric(funcs=[constant_reward], weights=[1.0]) | ||
|
|
||
| state = State( | ||
| input=RolloutInput( | ||
| prompt=[UserMessage(content="prompt")], | ||
| answer="", | ||
| task="task", | ||
| example_id=0, | ||
| ) | ||
| ) | ||
| state["completion"] = "answer" | ||
| state["trajectory"] = [_make_step(completion_ids=[31, 32], advantage=0.25)] | ||
| state["timing"] = { | ||
| "generation_ms": 0.0, | ||
| "scoring_ms": 0.0, | ||
| "total_ms": 0.0, | ||
| "start_time": 0.0, | ||
| } | ||
|
|
||
| await rubric.score_group([state]) | ||
|
|
||
| step = state["trajectory"][0] | ||
| assert state["advantage"] == 0.0 | ||
| assert step["advantage"] == 0.25 | ||
| assert step["completion_advantages"] == [0.25, 0.25] | ||
|
|
||
|
|
||
| def test_states_to_outputs_includes_use_verifiers_advantages(make_state): | ||
| state = make_state() | ||
| state["use_verifiers_advantages"] = True | ||
|
|
||
| output = states_to_outputs([state], state_columns=["use_verifiers_advantages"])[0] | ||
|
|
||
| assert output["use_verifiers_advantages"] is True |
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
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
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.
RubricGroup produces incorrect completion_advantages after aggregation
Medium Severity
When
RubricGroup.score_groupis used, each sub-rubric'sscore_groupcalls_populate_step_completion_advantageswith that sub-rubric's partial advantage. Since step advantages are only set whenNone, the first sub-rubric's partial advantage "sticks" on the steps andcompletion_advantages, whileRubricGroupnever recomputes them from the final aggregated reward. The result iscompletion_advantagesreflecting only one sub-rubric's contribution rather than the total.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.
this was already the implementation before, the rubric group only does aggregated trajectory level rewards, if someone wants to do step level advantages they will probably have a single reward function in a rubric that does this. probably a cleaner way to get around this but not sure if this is worth fixing right now