|
| 1 | +# Copyright 2023-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 utils_rl.extract_answer (CPU-only). |
| 16 | +
|
| 17 | +Covers the two-part contract of the boxed-extraction change: |
| 18 | + 1. `\\boxed{N}` is extracted (with/without <answer> tags, nested LaTeX, |
| 19 | + multiple boxed, whitespace, negatives, and answer-tag scoping). |
| 20 | + 2. Legacy plain-text answers inside the solution tags still work, so |
| 21 | + existing recipes that do not emit `\\boxed` are unaffected. |
| 22 | +""" |
| 23 | + |
| 24 | +import unittest |
| 25 | +from types import SimpleNamespace |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from maxtext.trainers.post_train.rl import utils_rl |
| 30 | + |
| 31 | +pytestmark = [pytest.mark.post_training] |
| 32 | + |
| 33 | + |
| 34 | +def _make_config(): |
| 35 | + """Minimal config carrying the solution/reasoning tokens extract_answer reads.""" |
| 36 | + return SimpleNamespace( |
| 37 | + reasoning_start_token="<reasoning>", |
| 38 | + reasoning_end_token="</reasoning>", |
| 39 | + solution_start_token="<answer>", |
| 40 | + solution_end_token="</answer>", |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class ExtractAnswerTest(unittest.TestCase): |
| 45 | + """Verify boxed extraction and legacy-fallback behavior of extract_answer.""" |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + super().setUp() |
| 49 | + self.config = _make_config() |
| 50 | + |
| 51 | + # ---- boxed extraction ---- |
| 52 | + |
| 53 | + @pytest.mark.cpu_only |
| 54 | + def test_boxed_inside_answer_tags(self): |
| 55 | + got = utils_rl.extract_answer("<reasoning>2+2</reasoning><answer>\\boxed{4}</answer>", self.config) |
| 56 | + self.assertEqual(got, "4") |
| 57 | + |
| 58 | + @pytest.mark.cpu_only |
| 59 | + def test_boxed_without_answer_tags(self): |
| 60 | + got = utils_rl.extract_answer("the result is \\boxed{42}", self.config) |
| 61 | + self.assertEqual(got, "42") |
| 62 | + |
| 63 | + @pytest.mark.cpu_only |
| 64 | + def test_boxed_nested_latex(self): |
| 65 | + """Brace-balanced scan keeps the full nested LaTeX content.""" |
| 66 | + got = utils_rl.extract_answer("<answer>\\boxed{\\frac{1}{2}}</answer>", self.config) |
| 67 | + self.assertEqual(got, "\\frac{1}{2}") |
| 68 | + |
| 69 | + @pytest.mark.cpu_only |
| 70 | + def test_multiple_boxed_returns_last(self): |
| 71 | + got = utils_rl.extract_answer("first \\boxed{1} then \\boxed{99}", self.config) |
| 72 | + self.assertEqual(got, "99") |
| 73 | + |
| 74 | + @pytest.mark.cpu_only |
| 75 | + def test_boxed_strips_whitespace(self): |
| 76 | + got = utils_rl.extract_answer("<answer>\\boxed{ 7 }</answer>", self.config) |
| 77 | + self.assertEqual(got, "7") |
| 78 | + |
| 79 | + @pytest.mark.cpu_only |
| 80 | + def test_boxed_negative(self): |
| 81 | + got = utils_rl.extract_answer("answer: \\boxed{-3}", self.config) |
| 82 | + self.assertEqual(got, "-3") |
| 83 | + |
| 84 | + @pytest.mark.cpu_only |
| 85 | + def test_answer_tag_scopes_over_reasoning_boxed(self): |
| 86 | + """A boxed value in <reasoning> must not win over the one in <answer>.""" |
| 87 | + resp = "<reasoning>maybe \\boxed{1}</reasoning><answer>\\boxed{8}</answer>" |
| 88 | + self.assertEqual(utils_rl.extract_answer(resp, self.config), "8") |
| 89 | + |
| 90 | + @pytest.mark.cpu_only |
| 91 | + def test_scoping_follows_configured_solution_tokens(self): |
| 92 | + """Scoping uses solution_start/end_token, not a hardcoded <answer> tag.""" |
| 93 | + config = SimpleNamespace( |
| 94 | + reasoning_start_token="<reasoning>", |
| 95 | + reasoning_end_token="</reasoning>", |
| 96 | + solution_start_token="<sol>", |
| 97 | + solution_end_token="</sol>", |
| 98 | + ) |
| 99 | + resp = "<reasoning>maybe \\boxed{1}</reasoning><sol>\\boxed{8}</sol>" |
| 100 | + self.assertEqual(utils_rl.extract_answer(resp, config), "8") |
| 101 | + |
| 102 | + # ---- legacy fallback (no boxed) ---- |
| 103 | + |
| 104 | + @pytest.mark.cpu_only |
| 105 | + def test_legacy_plain_answer_in_tags(self): |
| 106 | + """A plain-text answer inside <answer> tags (no boxed) still extracts.""" |
| 107 | + got = utils_rl.extract_answer("<reasoning>work</reasoning><answer>42</answer>", self.config) |
| 108 | + self.assertEqual(got, "42") |
| 109 | + |
| 110 | + @pytest.mark.cpu_only |
| 111 | + def test_legacy_last_answer_wins(self): |
| 112 | + got = utils_rl.extract_answer("<answer>1</answer> ... <answer>5</answer>", self.config) |
| 113 | + self.assertEqual(got, "5") |
| 114 | + |
| 115 | + # ---- no answer ---- |
| 116 | + |
| 117 | + @pytest.mark.cpu_only |
| 118 | + def test_no_answer_returns_fallback_constant(self): |
| 119 | + got = utils_rl.extract_answer("I have no idea", self.config) |
| 120 | + self.assertEqual(got, utils_rl.FALLBACK_ANSWER) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + unittest.main() |
0 commit comments