-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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: remove deprecated code due to Cache
and cache_position
being default
#31898
Conversation
@@ -689,13 +689,16 @@ def _update_model_kwargs_for_generation( | |||
dim=-1, | |||
) | |||
|
|||
if ( |
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.
TL;DR cache_position
now always exists, regardless of use_cache
# If we are about to go beyond the maximum cache length, we need to crop the input attention mask. | ||
if ( | ||
max_cache_length is not None | ||
and attention_mask is not None | ||
and cache_length + input_ids.shape[1] > max_cache_length | ||
): | ||
attention_mask = attention_mask[:, -max_cache_length:] |
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.
_update_causal_mask
handles the corner case this was originally meant to cover
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.
💘
@staticmethod | ||
def _reorder_cache(past_key_values, beam_idx): | ||
reordered_past = () | ||
for layer_past in past_key_values: | ||
reordered_past += ( | ||
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past), | ||
) | ||
return reordered_past |
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.
This was used with legacy caches only
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.
nice!
if inputs_embeds is not None: # Exception 1 | ||
input_ids = input_ids[:, -cache_position.shape[0] :] | ||
elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) | ||
input_ids = input_ids[:, cache_position] |
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.
This line holds the logic from end-to-end generate
compilation. The other two lines are exceptions to ensure we don't lose BC. The comment at the top should be MUCH clearer now.
cache_position=None, | ||
use_cache=True, | ||
**kwargs, | ||
self, input_ids, cache_position, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs |
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.
Note: cache_position
now is a mandatory input
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.
SOOOOO much cleaner
# If we are about to go beyond the maximum cache length, we need to crop the input attention mask. | ||
if ( | ||
max_cache_length is not None | ||
and attention_mask is not None | ||
and cache_length + input_ids.shape[1] > max_cache_length | ||
): | ||
attention_mask = attention_mask[:, -max_cache_length:] |
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.
💘
@staticmethod | ||
def _reorder_cache(past_key_values, beam_idx): | ||
reordered_past = () | ||
for layer_past in past_key_values: | ||
reordered_past += ( | ||
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past), | ||
) | ||
return reordered_past |
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.
nice!
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. |
DynamicCache
and cache_position
being defaultCache
and cache_position
being default
@ArthurZucker ready for a final check :) (tons of slow tests ran on my end, should be safe to merge) |
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.
LGTM then! i trust our tests for this, would be nice to see the results of the full suit! cc @ydshieh
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.
nice!
Do you want me to trigger a full (GitHub Action) CI for this PR during this weekend (before merge)? |
yeah would be nice ! |
OK will do (once today's CI run is over) |
@ydshieh please ping me when the run is over 🤗 btw, there are MANY broken tests in the list of models changed in this PR (on |
The run (triggered for this PR) is likely to be over tomorrow morning (if I trigger it this evening). I will let you know in any case. |
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
CI is running here |
slow CI looks good (same issues as in |
What does this PR do?
prepare_inputs_for_generation
on models usingCache
_reorder_cache
function on models usingCache
Slow tests run (and passing / same failures as in
main
):Cache
integration tests)RUN_SLOW=1 py.test -vv tests/utils/test_cache_utils.py
generate
integration tests)RUN_SLOW=1 py.test -vv tests/generation/test_utils.py
llama
)RUN_SLOW=1 py.test -vv tests/models/llama/test_modeling_llama.py
mixtral
)RUN_SLOW=1 py.test -vv tests/models/mixtral/test_modeling_mixtral.py