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

Fixed wrong trigger for warning #971

Merged
merged 1 commit into from
Nov 15, 2023
Merged

Conversation

zabealbe
Copy link
Contributor

@zabealbe zabealbe commented Nov 8, 2023

Fixes #972

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

func.__code__.co_varnames was used to count the function arguments for formatting_func. This code actually counted the function variables rather than function parameters.
@lvwerra lvwerra requested a review from younesbelkada November 9, 2023 14:03
@lvwerra
Copy link
Member

lvwerra commented Nov 9, 2023

Great catch! Would you mind adding a test for this as well if you have time?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch ! Thanks @zabealbe

@younesbelkada younesbelkada merged commit 28bdb6a into huggingface:main Nov 15, 2023
lapp0 pushed a commit to lapp0/trl that referenced this pull request May 10, 2024
func.__code__.co_varnames was used to count the function arguments for formatting_func. This code actually counted the function variables rather than function parameters.
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 this pull request may close these issues.

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