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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/transformers/models/xlm_roberta_xl/modeling_xlm_roberta_xl.py
Original file line number Diff line number Diff line change
@@ -1119,6 +1119,13 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, attenti
if attention_mask is None:
attention_mask = input_ids.new_ones(input_shape)

# Create missing `position_ids` on the fly
position_ids = None
if model_kwargs.get("position_ids") is None:
position_ids = create_position_ids_from_input_ids(
input_ids, padding_idx=self.config.pad_token_id
) # placed in kwargs for further processing (see below)

# cut decoder_input_ids if past_key_values is used
if past_key_values is not None:
past_length = past_key_values[0][0].shape[2]
@@ -1131,8 +1138,15 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, attenti
remove_prefix_length = input_ids.shape[1] - 1

input_ids = input_ids[:, remove_prefix_length:]

return {"input_ids": input_ids, "attention_mask": attention_mask, "past_key_values": past_key_values}
if position_ids is not None:
position_ids = position_ids[:, remove_prefix_length:]

return {
"input_ids": input_ids,
"attention_mask": attention_mask,
"position_ids": position_ids,
"past_key_values": past_key_values,
}

def _reorder_cache(self, past_key_values, beam_idx):
reordered_past = ()