Skip to content

GRPOTrainer can crash with AttributeError for Callable reward_func.__name__ #3049

Open
@jamesbraza

Description

@jamesbraza

Reproduction

GRPOTrainer._prepare_inputs on the non-Module reward_func.__name__: https://github.com/huggingface/trl/blob/v0.15.2/trl/trainer/grpo_trainer.py#L658

The latent crash here is __name__ is not guaranteed to be present on a Callable, for example with unittest.mock.Mock:

>>> from unittest.mock import Mock
>>> Mock().__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jamesbraza/.pyenv/versions/3.12.8/lib/python3.12/unittest/mock.py", line 662, in __getattr__
    raise AttributeError(name)
AttributeError: __name__

A more robust implementation is:

reward_func_name = getattr(reward_func, '__name__', str(reward_func))

Happy to make a PR for this.

System Info

n/a

Checklist

  • I have checked that my issue isn't already filed (see open issues)
  • I have included my system information
  • Any code provided is minimal, complete, and reproducible (more on MREs)
  • Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
  • Any traceback provided is complete

Metadata

Metadata

Assignees

Labels

🏋 GRPORelated to GRPO🐛 bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions