-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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 on "cache position" for assisted generation #30068
Conversation
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. |
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, but a question: how does the new test differ from the existing one? (I may be missing something :) )
Related change: 82be569 |
cur_len = model_kwargs["inputs_embeds"].shape[1] | ||
else: | ||
cur_len = input_ids.shape[-1] | ||
model_kwargs["cache_position"] = torch.arange(past_length, cur_len, device=input_ids.device) |
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.
It was not working in case when past key values are passed into the "generate" in Jamba, because Jamba apparently makes attention mask shape based on cache position values. Tested all generation
tests + LLama/Jamba/Gemma tests, all are passing on my end
@@ -276,6 +278,14 @@ def reorder_cache(self, beam_idx: torch.LongTensor): | |||
device = self.ssm_states[layer_idx].device | |||
self.ssm_states[layer_idx] = self.ssm_states[layer_idx].index_select(0, beam_idx.to(device)) | |||
|
|||
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int: |
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 causing errors since cache at layer_id=0 is always length=0. Current code is a workaround, I could not find a better way for Jamba yet...
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 looks good to me 👍
4353bbc
to
c5d3c0f
Compare
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.
In general looks great to me 🙌 I haven't approved because there is something wrong with CI.
I've also added a few improvement suggestions
@@ -4663,20 +4676,21 @@ def _assisted_decoding( | |||
# we use this forward pass to also pick the subsequent logits in the original model. | |||
|
|||
# 2.1. Prepare the model inputs | |||
model_kwargs = _prepare_attention_mask( | |||
model_kwargs, candidate_input_ids.shape[1], self.config.is_encoder_decoder | |||
candidate_kwargs = copy.copy(model_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.
Do we need the copy? It was originally added before the whole assistant run was abstracted into AssistedCandidateGenerator
, which now holds its own set of kwargs.
If we don't need the copy, let's remove it and use model_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.
yes, if we remove copy if fails because the cache position is from prev model run, with all candidates. It works only if we discard all changes made by candidates, and extend cache position based on number of accepted tokens
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.
Makes sense! Thank you for walking me through it :D
@@ -276,6 +278,14 @@ def reorder_cache(self, beam_idx: torch.LongTensor): | |||
device = self.ssm_states[layer_idx].device | |||
self.ssm_states[layer_idx] = self.ssm_states[layer_idx].index_select(0, beam_idx.to(device)) | |||
|
|||
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int: |
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 looks good to me 👍
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.
CI is happy now :D
Let's sort the comments, then we can tag a core maintainer.
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.
Thank you for iterating 💛
@@ -4663,20 +4676,21 @@ def _assisted_decoding( | |||
# we use this forward pass to also pick the subsequent logits in the original model. | |||
|
|||
# 2.1. Prepare the model inputs | |||
model_kwargs = _prepare_attention_mask( | |||
model_kwargs, candidate_input_ids.shape[1], self.config.is_encoder_decoder | |||
candidate_kwargs = copy.copy(model_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.
Makes sense! Thank you for walking me through it :D
Co-authored-by: Joao Gante <joaofranciscocardosogante@gmail.com>
Failing test fixed by #30389 |
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.
Thanks for fixing!
Just a small comment about model-specific login in the mixin
assistant_model = model | ||
# test with the same assistant model or randomly init one | ||
# in the first case all candidate tokens are accepted, in the second none is accepted | ||
# case when some are accepted and some not is hard to reproduce, so let's hope this catches most errors :) |
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.
Does this mean we expect the second case to fail periodically?
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.
No, we do not expect the test to fail but we expect that candidates will be discarded. We had some problems with getting correct cache positions when all tokens are accepted vs not all. So it's better to check that it works in both cases
src/transformers/generation/utils.py
Outdated
elif "gptbigcode" in self.__class__.__name__.lower() or ( | ||
self.config.architectures is not None and "gptbigcode" in self.config.architectures[0].lower() | ||
): | ||
if self.config.multi_query: | ||
past_length = model_kwargs["past_key_values"][0].shape[1] | ||
else: | ||
past_length = model_kwargs["past_key_values"][0].shape[2] |
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.
Rather than have this model specific code in the mixin side - it would be cleaner for inidividual models to implement their own _get_initial_cache_position
wherever necessary
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.
Hmm, This one is special only fot GPT bigcode, but I think we can make special method in bigcode's modeling and fallback to the general method in all other model types.
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.
We can remove the gptbigcode logic here now
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.
oppps, removed
@amyeroberts can be re-reviewed, failing tests are due to connection errors and not related to the PR |
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.
Thanks for fixing!
What does this PR do?
Fixes #30042 and fixes #30068. As it was reported in the issue, assisted decoding generation does not match greedy. The problem was in assuming there is only one new token generated when "update_kwargs_for_generation". That does not affect other model kwargs, because assisted generation handles them internally. But for "cache position" we need to update it to the new length, which is "accepted num candidates".
I added a small test for assisted generation + cache position, passes for all models where "cache position" is supported. Tested if anything breaks when 0 candidates accepted or when all of them accepted, seems to work for me.
Btw, I want to notice again that making a universal "update_kwargs_for_generation" would be very nice, so that it does not assume one token being added. I can work on it as a new PR, maybe this can help our future refactoring a bit :)
Update: After a little explorationn I found that using only "update_model_kwargs" to get rid of extra function is feasible, but that needs two separate methods: for pre-fill and for decoding