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 on "cache position" for assisted generation #30068

Merged
merged 9 commits into from
Apr 23, 2024

Conversation

zucchini-nlp
Copy link
Member

@zucchini-nlp zucchini-nlp commented Apr 5, 2024

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

@zucchini-nlp zucchini-nlp requested a review from gante April 5, 2024 11:59
@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
Member

@gante gante left a 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 :) )

@gante
Copy link
Member

gante commented Apr 18, 2024

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)
Copy link
Member Author

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:
Copy link
Member Author

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

Copy link
Member

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 👍

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Copy link
Member

@gante gante left a 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)
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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:
Copy link
Member

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 👍

Copy link
Member

@gante gante left a 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.

@zucchini-nlp zucchini-nlp requested a review from gante April 19, 2024 12:23
Copy link
Member

@gante gante left a 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)
Copy link
Member

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

@gante gante requested a review from amyeroberts April 22, 2024 10:12
Co-authored-by: Joao Gante <joaofranciscocardosogante@gmail.com>
@gante
Copy link
Member

gante commented Apr 22, 2024

Failing test fixed by #30389

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!

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 :)
Copy link
Collaborator

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?

Copy link
Member Author

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

Comment on lines 1290 to 1296
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]
Copy link
Collaborator

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

Copy link
Member Author

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.

Copy link
Collaborator

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

Copy link
Member Author

Choose a reason for hiding this comment

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

oppps, removed

@zucchini-nlp
Copy link
Member Author

@amyeroberts can be re-reviewed, failing tests are due to connection errors and not related to the PR

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!

@zucchini-nlp zucchini-nlp merged commit 77b59dc into huggingface:main Apr 23, 2024
21 checks passed
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.

Assisted generation output differs from greedy output
4 participants