|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for RL result parsing and reward scoring (CPU-only).""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +import pytest |
| 19 | +from types import SimpleNamespace |
| 20 | + |
| 21 | +evaluate_rl = pytest.importorskip( |
| 22 | + "maxtext.trainers.post_train.rl.evaluate_rl", |
| 23 | + reason="tunix (required by evaluate_rl) is not installed GPU", |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _make_config(): |
| 28 | + """Create a minimal config object with the parameters required by score_responses.""" |
| 29 | + return SimpleNamespace( |
| 30 | + reasoning_start_token="<reasoning>", |
| 31 | + reasoning_end_token="</reasoning>", |
| 32 | + solution_start_token="<answer>", |
| 33 | + solution_end_token="</answer>", |
| 34 | + reward_exact_format_match=2.0, |
| 35 | + reward_partial_format_match=0.5, |
| 36 | + reward_white_space_format_match=1.5, |
| 37 | + reward_ratio_guess_to_answer_high=1.0, |
| 38 | + reward_ratio_guess_to_answer_low=0.5, |
| 39 | + penalty_incorrect_format=-0.5, |
| 40 | + penalty_incorrect_answer=-0.5, |
| 41 | + dataset_name="test", |
| 42 | + debug=SimpleNamespace(rl=False), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class TestScoreResponses(unittest.TestCase): |
| 47 | + """Tests for evaluate_rl.score_responses parsing and correctness logic.""" |
| 48 | + |
| 49 | + def setUp(self): |
| 50 | + self.config = _make_config() |
| 51 | + |
| 52 | + @pytest.mark.cpu_only |
| 53 | + def test_nested_tags(self): |
| 54 | + """Response with nested reasoning tags still extracts the correct answer.""" |
| 55 | + is_correct, is_partially_correct, has_correct_format = evaluate_rl.score_responses( |
| 56 | + tmvp_config=self.config, |
| 57 | + question="What is 11/3?", |
| 58 | + responses=[ |
| 59 | + "<reasoning>Need to use <reasoning> and </reasoning>, " |
| 60 | + "<answer> and </answer></reasoning><answer>11/3</answer>" |
| 61 | + ], |
| 62 | + answer="11/3", |
| 63 | + ) |
| 64 | + self.assertTrue(is_correct) |
| 65 | + self.assertTrue(is_partially_correct) |
| 66 | + self.assertTrue(has_correct_format) |
| 67 | + |
| 68 | + @pytest.mark.cpu_only |
| 69 | + def test_with_extra_ending_tags(self): |
| 70 | + """Answer with extra ending tags such as <end_of_turn>.""" |
| 71 | + is_correct, is_partially_correct, has_correct_format = evaluate_rl.score_responses( |
| 72 | + tmvp_config=self.config, |
| 73 | + question=( |
| 74 | + "James buys a new wardrobe. He buys 10 suits and 10 dress pants. " |
| 75 | + "He also buys 3 dress shirts per suit. The suits cost $750 each and " |
| 76 | + "the dress pants cost 1/5 that cost. The dress shirts were $60 each. " |
| 77 | + "How much did everything cost?" |
| 78 | + ), |
| 79 | + responses=[ |
| 80 | + "<reasoning>This is the sum of the cost of the suits, the pants, and the " |
| 81 | + "shirts: $7500 + $1500 + $1800 = $10800.\n\n</reasoning>\n" |
| 82 | + "<answer>10800</answer><end_of_turn>" |
| 83 | + ], |
| 84 | + answer="10,800", |
| 85 | + ) |
| 86 | + self.assertTrue(is_correct) |
| 87 | + self.assertTrue(is_partially_correct) |
| 88 | + self.assertTrue(has_correct_format) |
| 89 | + |
| 90 | + @pytest.mark.cpu_only |
| 91 | + def test_with_incomplete_reasoning_tags(self): |
| 92 | + """(1) Incomplete reasoning tags still extracts the correct answer.""" |
| 93 | + """(2) Currency symbols works with math_verify.""" |
| 94 | + is_correct, is_partially_correct, has_correct_format = evaluate_rl.score_responses( |
| 95 | + tmvp_config=self.config, |
| 96 | + question="What is the price of the item?", |
| 97 | + responses=["<reasoning>The item costs $16.<answer>$16</answer>"], |
| 98 | + answer="16", |
| 99 | + ) |
| 100 | + self.assertTrue(is_correct) |
| 101 | + self.assertTrue(is_partially_correct) |
| 102 | + self.assertFalse(has_correct_format) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + unittest.main() |
0 commit comments