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

Generate: add model class validation #18902

Merged
merged 3 commits into from
Sep 13, 2022

Conversation

gante
Copy link
Member

@gante gante commented Sep 6, 2022

What does this PR do?

Fixes #18210

This PR adds model class validation at the start of generate (all model classes inherit GenerationMixin, but few can use generate()). It also adds an exception that attempts to redirect the users to the right classes.

Copy link
Collaborator

@sgugger sgugger 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 fixing!

@gante gante changed the title add model class validation Generate: add model class validation Sep 6, 2022
if not hasattr(self, "prepare_inputs_for_generation"):
model_class = self.__class__.__name__
raise TypeError(
f"The current model class ({model_class}) is not compatible with `.generate()`, as it doesn't have a ",
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) Wondering if it could make sense to make a really nifty error message here where we check if any class of the model type of the given class in in one of the auto classes below - maybe over-engineered though

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Sounds good! Wondering if it could make sense to make the error message a bit more detailed E.g. if someone uses BertModel and gets an error that one should use one of the following AutoModel classes, I'm not sure how helpful this is). Could be super cool that if one uses BertModel and gets as an answer that BertForCausalLM supports generate it could be a nicer UX. But maybe over engineered.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Sep 6, 2022

The documentation is not available anymore as the PR was closed or merged.

@gante
Copy link
Member Author

gante commented Sep 6, 2022

@sgugger @patrickvonplaten I've requested a re-review of this PR. As per @patrickvonplaten's suggestion, the PR was upgraded to contain the exact class the user should use in the exception (as opposed to pointing to all generate-compatible auto classes).

In the process of building it, I've noticed that the previous version of this PR was incorrect anyways -- PT and TF had a default prepare_inputs_for_generation, so we couldn't rely on its existence. Only 1 model was using this default, so I removed it and implemented it in the missing model. The default prepare_inputs_for_generation was a public method, but since this PR blocks the use of generate() with classes that are not intended to be used with it anyways, removing the public method should have little impact. Nevertheless, it is a point to consider in the review!


Here's an example with the current version of the PR:

>>> from transformers import AutoModel
>>> model = AutoModel.from_pretrained("distilgpt2")
>>> model.generate("foo")
TypeError: The current model class (GPT2Model) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'GPT2LMHeadModel'}

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Looks good to me! I haven't tested thoroughly but from a few quick tests it seems to work well.

Thanks @gante!

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Oh missed the notification that re-requested the review. Still LGTM!

@gante gante merged commit 4bd36f1 into huggingface:main Sep 13, 2022
@gante gante deleted the class_validation_generate branch September 13, 2022 08:19
oneraghavan pushed a commit to oneraghavan/transformers that referenced this pull request Sep 26, 2022
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.

TFAutoModel does not work with gpt2 and .generate
5 participants