-
Notifications
You must be signed in to change notification settings - Fork 316
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
Add optional r1-style thinking reward #551
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16da676
Add optional r1-style thinking reward
vwxyzjn ee26489
quick change
vwxyzjn 818ed86
test
vwxyzjn 8e1e1d9
Merge branch 'main' into add-optional-format-reward
vwxyzjn 90355d1
quick change
vwxyzjn 4be929e
push latest change
vwxyzjn 79192aa
fix
vwxyzjn f45c51d
tested it to work
vwxyzjn f828fca
Push changes
vwxyzjn b02dcb2
push
vwxyzjn 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 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 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 |
---|---|---|
|
@@ -139,7 +139,7 @@ def verify_flan_sample(model_output, ground_truth_answer): | |
|
||
def soft_format_reward_func(responses: list[str]) -> list[float]: | ||
"""Reward function that checks if the completion has a specific format.""" | ||
pattern = r"<reasoning>.*?</reasoning>\s*<answer>.*?</answer>" | ||
pattern = r".*?</think>\s*<answer>.*?</answer>" | ||
matches = [re.match(pattern, r, re.DOTALL) for r in responses] | ||
return [0.5 if match else 0.0 for match in matches] | ||
|
||
|
@@ -150,115 +150,9 @@ def strict_format_reward_func(responses: list[str]) -> list[float]: | |
matches = [re.match(pattern, r, re.DOTALL) for r in responses] | ||
return [1.0 if match else 0.0 for match in matches] | ||
|
||
def test_basic_valid(): | ||
response = """<think> | ||
The sky is blue | ||
</think> | ||
<answer> | ||
Blue | ||
</answer> | ||
""" | ||
result = strict_format_reward_func([response]) | ||
assert result == [1.0], f"Basic valid case failed, got {result}" | ||
print("✓ Basic valid case passed") | ||
|
||
def test_multiline_think(): | ||
response = """<think> | ||
The sky is blue because of Rayleigh scattering | ||
This is a second line of think | ||
And here's a third line | ||
</think> | ||
<answer> | ||
Blue | ||
</answer> | ||
""" | ||
result = strict_format_reward_func([response]) | ||
assert result == [1.0], f"Multiline think failed, got {result}" | ||
print("✓ Multiline think passed") | ||
|
||
def test_multiline_answer(): | ||
response = """<think> | ||
The sky is blue | ||
</think> | ||
<answer> | ||
Blue | ||
And also sometimes lighter blue | ||
And even white when cloudy | ||
</answer> | ||
""" | ||
result = strict_format_reward_func([response]) | ||
assert result == [1.0], f"Multiline answer failed, got {result}" | ||
print("✓ Multiline answer passed") | ||
|
||
def test_no_newlines(): | ||
response = "<think>The sky is blue</think><answer>Blue</answer>" | ||
result = strict_format_reward_func([response]) | ||
assert result == [0.0], f"No newlines case failed, got {result}" | ||
print("✓ No newlines case passed") | ||
|
||
def test_missing_final_newline(): | ||
response = """<think> | ||
The sky is blue | ||
</think> | ||
<answer> | ||
Blue | ||
</answer>""" # No final newline | ||
result = strict_format_reward_func([response]) | ||
assert result == [0.0], f"Missing final newline failed, got {result}" | ||
print("✓ Missing final newline passed") | ||
|
||
def test_extra_content(): | ||
response = """Extra content | ||
<think> | ||
The sky is blue | ||
</think> | ||
<answer> | ||
Blue | ||
</answer> | ||
""" | ||
result = strict_format_reward_func([response]) | ||
assert result == [0.0], f"Extra content case failed, got {result}" | ||
print("✓ Extra content case passed") | ||
|
||
def test_wrong_order(): | ||
response = """<answer> | ||
Blue | ||
</answer> | ||
<think> | ||
The sky is blue | ||
</think> | ||
""" | ||
result = strict_format_reward_func([response]) | ||
assert result == [0.0], f"Wrong order case failed, got {result}" | ||
print("✓ Wrong order case passed") | ||
|
||
def test_multiple_responses(): | ||
valid = """<think> | ||
First think | ||
</think> | ||
<answer> | ||
First answer | ||
</answer> | ||
""" | ||
invalid = "Invalid format" | ||
|
||
result = strict_format_reward_func([valid, invalid]) | ||
assert result == [1.0, 0.0], f"Multiple responses failed, got {result}" | ||
print("✓ Multiple responses passed") | ||
|
||
|
||
# debug code | ||
if __name__ == "__main__": | ||
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. @vwxyzjn lets add this to the tests check we have? |
||
print("Running tests...") | ||
test_basic_valid() | ||
test_multiline_think() | ||
test_multiline_answer() | ||
test_no_newlines() | ||
test_missing_final_newline() | ||
test_extra_content() | ||
test_wrong_order() | ||
test_multiple_responses() | ||
print("\nAll tests passed!") | ||
from datasets import load_dataset | ||
ds = load_dataset("ai2-adapt-dev/prompts_with_constraints_for_ground_truth") | ||
test_model_output = "<|assistant|>\nThe answer is $\\boxed{3.14}$" | ||
|
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.
Would agree with nathan that it feels like reward weight should be a param (and maybe even what pattern you are looking for?), to help with tuning reward stuff in the future.