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

[generate] Fix encoder decoder models attention mask #36018

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/transformers/generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def prepare_inputs_for_generation(
model_inputs[input_ids_key] = input_ids.clone(memory_format=torch.contiguous_format)

# 4. Create missing `position_ids` on the fly
encoder_attention_mask = attention_mask if self.config.is_encoder_decoder else None
attention_mask = (
kwargs.pop("decoder_attention_mask", None) if self.config.is_encoder_decoder else attention_mask
)
Expand Down Expand Up @@ -480,6 +481,9 @@ def prepare_inputs_for_generation(
if attention_mask is not None:
model_inputs[attention_mask_key] = attention_mask

if encoder_attention_mask is not None:
model_inputs["attention_mask"] = encoder_attention_mask

# 7. Forward ALL kwargs that are uninitialized (e.g. `use_cache`).
for key, value in kwargs.items():
if key not in model_inputs:
Expand Down