Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong trigger for warning "UserWarning: The passed formatting_func has more than one argument." #972

Closed
zabealbe opened this issue Nov 8, 2023 · 0 comments · Fixed by #971

Comments

@zabealbe
Copy link
Contributor

zabealbe commented Nov 8, 2023

In trl/trainer/utils.py the code func.__code__.co_varnames is being used to check if the user passed a formatting_func with more than 1 parameter. This code actually counts the function variables rather than function parameters.

For instance

def add_v1(a):
  return a + 1

def add_v2(a):
  b = a + 1
  return b

print("Number of parameters for add_v1 is", len(add_v1.__code__.co_varnames))
print("Number of parameters for add_v2 is", len(add_v2.__code__.co_varnames))

outputs

Number of parameters for add_v1 is 1
Number of parameters for add_v2 is 2

The proposed fix is to change the following:

formatting_func_signature = formatting_func.__code__.co_varnames
if len(formatting_func_signature) > 1:
    warnings.warn(
        "The passed formatting_func has more than one argument. Usually that function should have a single argument `example`"
        " which corresponds to the dictionary returned by each element of the dataset. Make sure you know what you are doing."
    )

to:

if formatting_func.__code__.co_argcount > 1:
    warnings.warn(
        "The passed formatting_func has more than one argument. Usually that function should have a single argument `example`"
        " which corresponds to the dictionary returned by each element of the dataset. Make sure you know what you are doing."
    )

Tested on python Python 2.7.5 and Python 3.6.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant