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: Mistral/Mixtral FA2 cache fix when going beyond the context window #28037

Merged
merged 8 commits into from
Dec 14, 2023

Conversation

gante
Copy link
Member

@gante gante commented Dec 14, 2023

What does this PR do?

The FA2 code path was indexing the Cache object incorrectly. This PR fixes it.

Fixes #27985


NOTE: tests/models/mistral/test_modeling_mistral.py::MistralIntegrationTest::test_model_7b_long_prompt (slow test) was failing on main, but it was not popping up in our daily slow CI 🤔 because of that, this issue flew under the radar. It is passing now.

Edit: the test was not run because we are skipping FA2 tests (@require_flash_attn). @ydshieh is on it :)

@@ -385,11 +385,16 @@ def forward(

if past_key_value is not None:
Copy link
Member Author

Choose a reason for hiding this comment

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

context: when use_cache is True, past_key_value is now a Cache object even if it is an empty cache (previously it was None).

As such, with a slicing window, we need to check whether the cache has contents before attempting to slice, as we can't slice None.

@@ -400,8 +405,6 @@ def forward(
f" {past_key.shape}"
)

past_key_value = (past_key, past_value)
Copy link
Member Author

@gante gante Dec 14, 2023

Choose a reason for hiding this comment

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

past_key_value is now a Cache instance that is updated in place with the .update() function (L413 in the updated file). We don't need to set it.

@gante gante requested a review from amyeroberts December 14, 2023 14:07
slicing_tokens = 1 - self.config.sliding_window

past_key = past_key_value[0]
past_value = past_key_value[1]
past_key = past_key_value[self.layer_idx][0]
Copy link
Member

Choose a reason for hiding this comment

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

self.layer_idx is guaranteed to be defined here, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, good catch! Going to add an appropriate exception.

@tomaarsen
Copy link
Member

This indeed seems to address what was described here, well done!

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

Looking forward to the FA2 tests being run 😅

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

@ydshieh
Copy link
Collaborator

ydshieh commented Dec 14, 2023

As @younesbelkada mentioined and in the official site

Ampere, Ada, or Hopper GPUs (e.g., A100, RTX 3090, RTX 4090, H100). Support for Turing GPUs (T4, RTX 2080) is coming soon, please use FlashAttention 1.x for Turing GPUs for now.

nothing we can't do unless we run on a different machine

@gante gante merged commit 388fd31 into huggingface:main Dec 14, 2023
18 checks passed
@gante gante deleted the mixtral_cache branch December 14, 2023 14:52
iantbutler01 pushed a commit to BismuthCloud/transformers that referenced this pull request Dec 16, 2023
staghado pushed a commit to staghado/transformers that referenced this pull request Jan 15, 2024
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.

KeyError: 'Cache only has 0 layers, attempted to access layer with index 0'
5 participants