|
| 1 | +# Copyright 2020-2025 The HuggingFace Team. All rights reserved. |
| 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 | +# http://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 | +import unittest |
| 16 | + |
| 17 | +from trl.rewards import think_format_reward |
| 18 | + |
| 19 | + |
| 20 | +class ThinkFormatRewardTester(unittest.TestCase): |
| 21 | + def test_valid_format(self): |
| 22 | + completions = [ |
| 23 | + "<think>This is my reasoning.</think>This is my answer.", # Simple, one-line reasoning |
| 24 | + "<think>\nThis is my reasoning.\n</think>\nThis is my answer.", # Multiline reasoning |
| 25 | + "<think>\nThis is\nmy reasoning.\n</think>\nThis is my answer.", # Multiline reasoning |
| 26 | + "<think>\nThis is <some tag> my reasoning.</think>\nThis is my answer.", # Reasoning including other tags |
| 27 | + "<think></think>\nThis is my answer.", # Empty reasoning |
| 28 | + ] |
| 29 | + completions = [[{"content": completion}] for completion in completions] |
| 30 | + expected_rewards = [1.0, 1.0, 1.0, 1.0, 1.0] # All should be valid |
| 31 | + rewards = think_format_reward(completions) |
| 32 | + self.assertEqual(rewards, expected_rewards) |
| 33 | + |
| 34 | + def test_invalid_format(self): |
| 35 | + completions = [ |
| 36 | + "<think>\nThis is my reasoning.\nThis is my answer.", # No closing </think> |
| 37 | + "<think>This is my reasoning.\nThis is my answer.", # No closing </think> |
| 38 | + "This is my reasoning. This is my answer.", # No <think> tags |
| 39 | + "This is my reasoning.\nThis is my answer.", # No <think> tags |
| 40 | + "This is my reasoning.</think>\nThis is my answer.", # No opening <think> |
| 41 | + "This is my reasoning.</think>This is my answer.", # No opening <think> |
| 42 | + "This<think>is my reasoning.</think>\nThis is my answer.", # <think> tag in the middle |
| 43 | + "<think>This is<think>my reasoning.</think></think>This is my answer.", # Nested <think> tags |
| 44 | + "<think>This is</think>\nmy\n<think>reasoning.</think>\nThis is my answer.", # Multiline <think> |
| 45 | + ] |
| 46 | + completions = [[{"content": completion}] for completion in completions] |
| 47 | + expected_rewards = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # All should be invalid |
| 48 | + rewards = think_format_reward(completions) |
| 49 | + self.assertEqual(rewards, expected_rewards) |
| 50 | + |
| 51 | + def test_mixed_format(self): |
| 52 | + completions = [ |
| 53 | + "<think>This is my reasoning.</think>This is my answer.", # Valid |
| 54 | + "<think>\nThis is my reasoning.\n</think>\nThis is my answer.", # Valid |
| 55 | + "<think>This is my reasoning.\nThis is my answer.", # Invalid |
| 56 | + "This is my reasoning. This is my answer.", # Invalid |
| 57 | + ] |
| 58 | + completions = [[{"content": completion}] for completion in completions] |
| 59 | + expected_rewards = [1.0, 1.0, 0.0, 0.0] |
| 60 | + rewards = think_format_reward(completions) |
| 61 | + self.assertEqual(rewards, expected_rewards) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + unittest.main() |
0 commit comments