|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import copy |
| 16 | +import functools |
16 | 17 | import textwrap |
17 | 18 | from io import StringIO |
18 | 19 | from unittest.mock import patch |
|
37 | 38 | entropy_from_logits, |
38 | 39 | flush_left, |
39 | 40 | generate_model_card, |
| 41 | + get_callable_name, |
40 | 42 | get_peft_config, |
41 | 43 | hash_module, |
42 | 44 | nanstd, |
@@ -279,6 +281,36 @@ def test_create_peft_config_use_peft_true(self): |
279 | 281 | assert getattr(peft_config, arg) == value |
280 | 282 |
|
281 | 283 |
|
| 284 | +class TestGetCallableName(TrlTestCase): |
| 285 | + def test_function(self): |
| 286 | + def accuracy_reward(completions): |
| 287 | + return [0.0] * len(completions) |
| 288 | + |
| 289 | + assert get_callable_name(accuracy_reward) == "accuracy_reward" |
| 290 | + |
| 291 | + def test_partial(self): |
| 292 | + def reward(completions, threshold): |
| 293 | + return [0.0] * len(completions) |
| 294 | + |
| 295 | + assert get_callable_name(functools.partial(reward, threshold=0.5)) == "reward" |
| 296 | + |
| 297 | + def test_nested_partial(self): |
| 298 | + def reward(completions, threshold): |
| 299 | + return [0.0] * len(completions) |
| 300 | + |
| 301 | + assert get_callable_name(functools.partial(functools.partial(reward), threshold=0.5)) == "reward" |
| 302 | + |
| 303 | + def test_callable_instance(self): |
| 304 | + class LengthReward: |
| 305 | + def __call__(self, completions): |
| 306 | + return [0.0] * len(completions) |
| 307 | + |
| 308 | + assert get_callable_name(LengthReward()) == "LengthReward" |
| 309 | + |
| 310 | + def test_lambda(self): |
| 311 | + assert get_callable_name(lambda completions: [0.0] * len(completions)) == "<lambda>" |
| 312 | + |
| 313 | + |
282 | 314 | class TestNanStd(TrlTestCase): |
283 | 315 | def test_nanstd_ignores_nans(self): |
284 | 316 | x = torch.tensor([1.0, 2.0, 3.0, float("nan")]) |
|
0 commit comments