This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
Include padding mask in generation #2096
Merged
joecummings
merged 1 commit into
pytorch:main
from
joecummings:fix-diff-generation-batch
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ def _prepare_decoder_ids_for_generation( | |
| return torch.ones((batch_size, 1), dtype=torch.long, device=device) * pad_idx | ||
|
|
||
| def greedy_search( | ||
| self, input_ids: torch.Tensor, max_length: int, eos_idx: int, pad_idx: Optional[int] = None, **model_kwargs | ||
| self, input_ids: torch.Tensor, max_length: int, eos_idx: int, pad_idx: int, **model_kwargs | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does changing pas_idx from Optional to required break any call sites?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. Only being called from the entry point method atm. |
||
| ) -> torch.Tensor: | ||
| """Greedy search decoding for text generation. Takes the most likely next token every time. | ||
|
|
||
|
|
@@ -62,10 +62,11 @@ def greedy_search( | |
| Returns: | ||
| Batch of sequences decoded by greedy search. | ||
| """ | ||
| unfinished_sequences = torch.ones((input_ids.shape[0], 1), device=input_ids.device, dtype=torch.long) | ||
| unfinished_sequences = torch.ones((input_ids.shape[0]), device=input_ids.device, dtype=torch.long) | ||
|
|
||
| while True: | ||
| model_inputs = self.model.prepare_inputs_for_generation(input_ids, **model_kwargs) | ||
|
|
||
| if self.is_huggingface_model: | ||
| model_inputs["return_dict"] = True | ||
| model_inputs["output_hidden_states"] = True | ||
|
|
@@ -77,18 +78,16 @@ def greedy_search( | |
|
|
||
| # Calculate probabilities and take the most likely next token | ||
| probs = F.log_softmax(decoder_output[:, -1], dim=-1) | ||
| _, next_tokens = torch.topk(probs, 1) | ||
| next_tokens = torch.argmax(probs, dim=-1) | ||
|
|
||
| # For any finished sequences, padding idx should be the last token | ||
| if eos_idx is not None: | ||
| if pad_idx is not None: | ||
| next_tokens = next_tokens * unfinished_sequences + pad_idx * (1 - unfinished_sequences) | ||
| next_tokens = next_tokens * unfinished_sequences + pad_idx * (1 - unfinished_sequences) | ||
|
|
||
| # Append the next tokens to the previous tokens | ||
| input_ids = torch.cat([input_ids, next_tokens], dim=-1) | ||
| input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing as unsqueezing the last dim |
||
|
|
||
| if eos_idx is not None: | ||
| unfinished_sequences = unfinished_sequences.mul((next_tokens != eos_idx).long()) | ||
| # Update unfinished sequences count | ||
| unfinished_sequences = unfinished_sequences.mul((next_tokens != eos_idx)).long() | ||
|
|
||
| # Stop iterating once all sequences are finished or exceed the max_length | ||
| if unfinished_sequences.max() == 0 or len(input_ids[0]) >= max_length: | ||
|
|
@@ -128,8 +127,10 @@ def generate( | |
|
|
||
| if self.is_encoder_decoder: | ||
| encoder = self.model.get_encoder() | ||
| model_kwargs["encoder_outputs"] = encoder(inputs) | ||
| encoder_model_kwargs = {"src_key_padding_mask": inputs.eq(pad_idx)} | ||
| model_kwargs["encoder_outputs"] = encoder(inputs, **encoder_model_kwargs) | ||
| inputs = self._prepare_decoder_ids_for_generation(len(inputs), device=inputs.device, **model_kwargs) | ||
| model_kwargs["encoder_padding_mask"] = encoder_model_kwargs.pop("src_key_padding_mask") | ||
|
|
||
| if max_length is None: | ||
| # Too hard to try to figure out the exact max_seq_length for each model | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why do we do
generated_text_for_single_example[-1]instead ofgenerated_text_for_single_example[0]?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.
Was originally going to pass multiple through the second pass, but did not. Both get the same result though. -1 == 0