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] Fix encoder decoder models attention mask #36018

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

eustlb
Copy link
Contributor

@eustlb eustlb commented Feb 3, 2025

What does this PR do?

#35852 introduced a bug where the attention mask is lost for encoder-decoder models.
This PR implements a naïve fix.

cf #36020

@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.

@eustlb eustlb changed the title [generate] handle encoder attention mask [generate] Fix encoder decoder models attention mask Feb 3, 2025
@eustlb eustlb requested a review from zucchini-nlp February 3, 2025 16:41
Copy link
Member

@zucchini-nlp zucchini-nlp 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 noticing it's broken, should've triggered some slow tests before merging!

I agree that we'd need to pass both attention masks for encoder-decoder models if available. And the attention mask in prepare_inputs should come from decoder for further manipulations no top

Overall LGTM but would be cleaner if we could pass encoder-mask from within kwargs in step 7, imo. CC @gante for one more opinion on generation

@eustlb
Copy link
Contributor Author

eustlb commented Feb 3, 2025

agree that having a encoder_mask would be cleaner, but it diverges a bit with the current convention in the codebase where encoder-decoder model forward's use attention_mask for the encoder and decoder_attention_mask otherwise. It is also how it is currently documents:

Ad hoc parametrization of `generation_config` and/or additional model-specific kwargs that will be
forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder
specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*.

@eustlb eustlb requested a review from gante February 4, 2025 10:24
Copy link
Member

@gante gante left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for the fix 🙏

P.S.: can we somehow add a fast test to ensure this doesn't happen again? This is a pretty basic feature, and should be covered by fast tests

@gante
Copy link
Member

gante commented Feb 5, 2025

@zucchini-nlp As @eustlb wrote, historically the naming convention is for the attention_mask to attend to the input (back when there were no multimodal models) 👀

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.

4 participants