-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Changes from 7 commits
aacffa8
70cc502
564750c
f038066
3f24fb0
d4867f7
cf7cc6d
fbfb543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,11 +385,16 @@ def forward( | |
|
||
if past_key_value is not None: | ||
# Activate slicing cache only if the config has a value `sliding_windows` attribute | ||
if getattr(self.config, "sliding_window", None) is not None and kv_seq_len > self.config.sliding_window: | ||
cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0 | ||
if ( | ||
getattr(self.config, "sliding_window", None) is not None | ||
and kv_seq_len > self.config.sliding_window | ||
and cache_has_contents | ||
): | ||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, good catch! Going to add an appropriate exception. |
||
past_value = past_key_value[self.layer_idx][1] | ||
|
||
past_key = past_key[:, :, slicing_tokens:, :].contiguous() | ||
past_value = past_value[:, :, slicing_tokens:, :].contiguous() | ||
|
@@ -400,8 +405,6 @@ def forward( | |
f" {past_key.shape}" | ||
) | ||
|
||
past_key_value = (past_key, past_value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if attention_mask is not None: | ||
attention_mask = attention_mask[:, slicing_tokens:] | ||
attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1) | ||
|
There was a problem hiding this comment.
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 aCache
object even if it is an empty cache (previously it wasNone
).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
.