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

Fix FA2 for models with HybridCache #35681

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

Fix FA2 for models with HybridCache #35681

wants to merge 7 commits into from

Conversation

Cyrilvallez
Copy link
Member

@Cyrilvallez Cyrilvallez commented Jan 13, 2025

What does this PR do?

As per the title. Models with HybridCache need to correctly slice the key/value states when using FA2 as inputs needs to be unpadded on the right as well (and the mask has shape [bs, seq_len]). It is currently broken and leads to garbage generation when using padding. This fixes it.

Also do not slice the mask during prefill if longer than the sliding window (I added a test for this case).

@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

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

For gemma2 it was suppose to work!
Not aligned with the removal of the attentoin mask slicing tho! Let's run slow test on the PR

Comment on lines -291 to -292
if attention_mask.shape[-1] <= 1: # when decoding
attention_mask = attention_mask[:, :, :, -self.sliding_window :]
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is super important ! Why is it removed?
I know it is counter intuitive, but _flash_attention_forward takes the attention mask to pad / unpad the input itds.
Thus you need the slicing otherwise this operation fails, see the blame !

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I was too fast on this one, the HybridCache behaves slightly differently than I remembered. There was still an issue in the slicing during prefill for FA2 though!

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.

3 participants