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

added warning to Trainer when label_names is not specified for PeftModel #32085

Merged
merged 6 commits into from
Feb 12, 2025

Conversation

MilkClouds
Copy link
Contributor

What does this PR do?

in Trainer, it automatically finds label_names with input arguments name of model.
But with model which is PeftModel, the input arguments are all hidden and we can't find the input argument's name automatically.

So, for the case then the user didn't specified label_names in TrainerArguments, I made warning message pop up.

p.s. it makes the change when I add call trainer.predict; if label_names it not set, there's no way I can get get label_ids from trainer.predict outcomes.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for adding this check - LGTM but let's get confirmation from @muellerzr

@@ -679,6 +679,11 @@ def __init__(
# returned to 0 every time flos need to be logged
self.current_flos = 0
self.hp_search_backend = None
if self.model.__class__.name__ == "PeftModel" and self.args.label_names is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this check is robust as the model could be e.g. PeftForCausalLM. Within this module we have _is_peft_model which I think we should use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, very good suggestion!

Copy link
Contributor

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Thanks, agree it makes sense but cc @BenjaminBossan here since it's peft :)

@muellerzr muellerzr requested a review from BenjaminBossan July 31, 2024 20:08
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for iterating on this!

If @BenjaminBossan is happy with it I think we can merge :)

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

Not sure if I fully understand the issue, does it have to do with some function signature inspection that fails on the wrapped model because it uses *args, **kwargs? Could you maybe provide a small example where this comes up?

@MilkClouds
Copy link
Contributor Author

MilkClouds commented Aug 1, 2024

Basically inside Trainer's init function, it automatically finds whether 'labels' argument exist in model class's forward function.

e.g. LlamaForCausalLM: have labels field in forward, so 'labels' is detected
e.g. PeftModel-wrapped LlamaForCausalLM: since PeftModel only have *args, **kwargs in forward's argument, it can't detect 'labels' of LlamaForCausalLM.

@BenjaminBossan
Copy link
Member

Thanks for clarifying, that makes sense. I had already shared this concern internally in the past (but got no response 😢):

Users are reporting issues with Trainer not showing evals when using PEFT models (e.g. huggingface/peft#1881). The core of the problem appear to functions like

that inspect the forward signature of the model class. As PEFT wraps the transformers model and uses mostly **kwargs in the signature, this check returns False even if the underlying model would return True.
I'm not sure how to resolve this. On the PEFT side, I could just add the arguments to the signature, but this can lead to false positives, as the base model may or may not actually support this. Therefore, it would be better if on the transformers side there would be a check if the model is a PEFT model, or if it has a base_model attribute, and then check the signature of the base model instead. This of course won't work if the model class is passed, it would require the model instance, so this would be a somewhat bigger change.

So I guess this PR helps a little bit but the fundamental problem still remains. Thus from my side, it's okay to merge with my suggestion applied.

Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@MilkClouds
Copy link
Contributor Author

Does it need any other consideration, @amyeroberts ?

@ArthurZucker
Copy link
Collaborator

I think we were waiting for the suggestion from @BenjaminBossan to be merged 🤗

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
@MilkClouds
Copy link
Contributor Author

Ah, I see. I missed the suggestion. I accepted the suggestion!

@ArthurZucker
Copy link
Collaborator

Now let's make sure make fixup and the rest of the CIs related are green!

@MilkClouds
Copy link
Contributor Author

Sorry for the late reply, I've forgotten and thought this PR was merged.
I ran make fixup and fixed some, but some of CI/CD fail until now.

@ArthurZucker
Copy link
Collaborator

Failing test is unrelated don't worry!

@ArthurZucker ArthurZucker merged commit 0cd5e2d into huggingface:main Feb 12, 2025
21 of 23 checks passed
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Feb 16, 2025
…del (huggingface#32085)

* feat: added warning to Trainer when label_names is not specified for PeftModel

* Update trainer.py

* feat: peft detectw ith `_is_peft_model`

* Update src/transformers/trainer.py

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* Applied formatting in trainer.py

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
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.

6 participants