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

Add position_ids in XLMRobertaXLForCausalLM.prepare_inputs_for_generation #35044

Merged
merged 4 commits into from
Jan 6, 2025

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Dec 2, 2024

What does this PR do?

The model component XLMRobertaXLEmbeddings will perform

position_ids = create_position_ids_from_input_ids(input_ids, self.padding_idx, past_key_values_length)

if position_ids is not passed to the model (say XLMRobertaXLForCausalLM).

However, during the decoding, even if past_key_values_length is passed, the create_position_ids_from_input_ids will compute the wrong position_ids when the input_ids is only the single last token. This is because create_position_ids_from_input_ids relies the whole input_ids in order to get the number of padding tokens:

mask = input_ids.ne(padding_idx).int()

(since past_key_values_length doesn't contain this information.)

In general, we need to prepare position_ids in prepare_inputs_for_generation, like what has been done in

GenerationMixin.prepare_inputs_for_generation

This PR adds this for XLMRobertaXLForCausalLM (whose prepare_inputs_for_generation is already overwritten from the parent class)


As a bonus:

tests/models/xlm_roberta_xl/test_modeling_xlm_roberta_xl.py::XLMRobertaXLModelTest::test_assisted_decoding_matches_greedy_search_1_same

is also no longer flaky (running 10000 times) while it was failing 5% of the time before this PR.

@ydshieh ydshieh changed the title fix Add position_ids in XLMRobertaXLForCausalLM.prepare_inputs_for_generation Dec 2, 2024
@ydshieh ydshieh requested a review from zucchini-nlp December 2, 2024 15:15
@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

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Oh yeah, we have some inconsistencies with how position ids are created in models. I've been trying to create position ids in all models in the same way, but it got too big and complicated. We'll need to come back to position ids later at some point

For now LGTM, thanks!

@ydshieh ydshieh requested a review from ArthurZucker December 3, 2024 12:57
Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Looks good to me, let's cleanup a bit before merging 😉

@ydshieh ydshieh force-pushed the fix_xlmrobertaxl_position_ids branch from 25ed3ba to 962cf42 Compare January 6, 2025 14:11
@ydshieh ydshieh merged commit 203e978 into main Jan 6, 2025
17 checks passed
@ydshieh ydshieh deleted the fix_xlmrobertaxl_position_ids branch January 6, 2025 15:10
AlanPonnachan pushed a commit to AlanPonnachan/transformers that referenced this pull request Jan 7, 2025
…eration` (huggingface#35044)

* fix

* fix

* cleanup

* style

---------

Co-authored-by: ydshieh <[email protected]>
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.

4 participants