-
Notifications
You must be signed in to change notification settings - Fork 87
feat(transformers): add ut for generation #1210
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
Open
wcrzlh
wants to merge
3
commits into
mindspore-lab:master
Choose a base branch
from
wcrzlh:generation_ut_pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -317,6 +317,28 @@ class GenerateBeamEncoderDecoderOutput(ModelOutput): | |
| past_key_values: Optional[Tuple[Tuple[Tuple[ms.Tensor]]]] = None | ||
|
|
||
|
|
||
| # TODO (joao): remove the equivalent classes and typing shortcuts below in v5 | ||
| # Equivalent classes (kept for retrocompatibility purposes) | ||
| GreedySearchDecoderOnlyOutput = GenerateDecoderOnlyOutput | ||
| ContrastiveSearchDecoderOnlyOutput = GenerateDecoderOnlyOutput | ||
| SampleDecoderOnlyOutput = GenerateDecoderOnlyOutput | ||
|
|
||
| ContrastiveSearchEncoderDecoderOutput = GenerateEncoderDecoderOutput | ||
| GreedySearchEncoderDecoderOutput = GenerateEncoderDecoderOutput | ||
| SampleEncoderDecoderOutput = GenerateEncoderDecoderOutput | ||
|
|
||
| BeamSearchDecoderOnlyOutput = GenerateBeamDecoderOnlyOutput | ||
| BeamSampleDecoderOnlyOutput = GenerateBeamDecoderOnlyOutput | ||
|
|
||
| BeamSearchEncoderDecoderOutput = GenerateBeamEncoderDecoderOutput | ||
| BeamSampleEncoderDecoderOutput = GenerateBeamEncoderDecoderOutput | ||
|
|
||
| GreedySearchOutput = Union[GreedySearchEncoderDecoderOutput, GreedySearchDecoderOnlyOutput] | ||
| SampleOutput = Union[SampleEncoderDecoderOutput, SampleDecoderOnlyOutput] | ||
| BeamSearchOutput = Union[BeamSearchEncoderDecoderOutput, BeamSearchDecoderOnlyOutput] | ||
| BeamSampleOutput = Union[BeamSampleEncoderDecoderOutput, BeamSampleDecoderOnlyOutput] | ||
| ContrastiveSearchOutput = Union[ContrastiveSearchEncoderDecoderOutput, ContrastiveSearchDecoderOnlyOutput] | ||
|
|
||
| # Typing shortcuts | ||
| GenerateNonBeamOutput = Union[GenerateDecoderOnlyOutput, GenerateEncoderDecoderOutput] | ||
| GenerateBeamOutput = Union[GenerateBeamDecoderOnlyOutput, GenerateBeamEncoderDecoderOutput] | ||
|
|
@@ -2482,7 +2504,7 @@ def heal_tokens(self, input_ids: ms.Tensor, tokenizer: Optional["PreTrainedToken | |
| # assumption: leading/trailing whitespace is not meaningful, so the prompts are | ||
| # stripped before re-tokenizing to desensitize generation to whitespace artefacts | ||
| prompts = [p.strip() for p in tokenizer.batch_decode(input_ids, skip_special_tokens=True)] | ||
| input_ids = ms.Tensor( | ||
| input_ids = ms.tensor( | ||
| tokenizer( | ||
| prompts, | ||
| return_tensors="np", | ||
|
|
@@ -2491,7 +2513,14 @@ def heal_tokens(self, input_ids: ms.Tensor, tokenizer: Optional["PreTrainedToken | |
| ) | ||
|
|
||
| # replace bos with pad to not condition healing on it | ||
| input_ids = ops.where(input_ids == bos_token_id, pad_token_id, input_ids) | ||
| input_ids = mint.where(input_ids == bos_token_id, pad_token_id, input_ids) | ||
|
|
||
| """ | ||
| the latter code assumes the input_ids is not empty, | ||
| input_id has to be checked if contains elements | ||
| """ | ||
| if input_ids.numel() == 0: | ||
| return input_ids | ||
|
|
||
| tail_ids = input_ids[:, -1].tolist() | ||
|
|
||
|
|
@@ -2502,11 +2531,18 @@ def heal_tokens(self, input_ids: ms.Tensor, tokenizer: Optional["PreTrainedToken | |
|
|
||
| for batch_idx, (tail_id, tail_tok) in enumerate(zip(tail_ids, tail_toks)): | ||
| batch_ids = input_ids[batch_idx] | ||
| if ops.all(batch_ids == pad_token_id).item(): | ||
| if mint.all(batch_ids == pad_token_id).item(): | ||
| continue # skip empty sequences (all pad ids) | ||
|
|
||
| # apply bias for alternatives (extensions) to the tail token | ||
| seq_bias = {(alt_tok,): 10.0 for alt_tok in vocab_trie.values(prefix=tail_tok)} | ||
| """ | ||
| seq_bias key has to be tuple with int so have to use | ||
| tokenizer function to convert str to int | ||
| """ | ||
|
Comment on lines
+2538
to
+2541
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. This multiline string is used as a comment. For better readability and to avoid potential runtime effects of docstrings, it's recommended to use standard # The `seq_bias` key must be a tuple of integers (token IDs), so we use the
# tokenizer function to convert the string token to an integer ID. |
||
| seq_bias = { | ||
| (tokenizer.convert_tokens_to_ids(alt_tok),): 10.0 for alt_tok in vocab_trie.extensions(prefix=tail_tok) | ||
| } | ||
|
|
||
| if len(seq_bias) == 1: | ||
| continue # skip if there are no token alternatives to heal with | ||
|
|
||
|
|
@@ -3321,3 +3357,59 @@ def _beam_search( | |
| ) | ||
| else: | ||
| return sequences | ||
|
|
||
|
|
||
| def _speculative_sampling( | ||
| candidate_input_ids, | ||
| candidate_logits, | ||
| candidate_length, | ||
| new_logits, | ||
| is_done_candidate, | ||
| ): | ||
| """ | ||
| Applies sampling as in the speculative decoding paper (https://arxiv.org/pdf/2211.17192.pdf, algorithm 1). Returns | ||
| the selected tokens, as well as the number of candidate matches. | ||
|
|
||
| NOTE: Unless otherwise stated, the variable names match those in the paper. | ||
| """ | ||
| new_candidate_input_ids = candidate_input_ids[:, -candidate_length:] | ||
| # Gets the probabilities from the logits. q_i and p_i denote the assistant and model probabilities of the tokens | ||
| # selected by the assistant, respectively. | ||
| q = mint.nn.functional.softmax(candidate_logits, dim=-1) | ||
| q_i = q[:, mint.arange(candidate_length), new_candidate_input_ids].squeeze((0, 1)) | ||
| p = mint.nn.functional.softmax(new_logits, dim=-1) | ||
| p_i = p[:, mint.arange(candidate_length), new_candidate_input_ids].squeeze((0, 1)) | ||
| probability_ratio = p_i / q_i | ||
|
|
||
| # When probability_ratio > 1 (i.e. q_i(x) < p_i(x), or "assistant probability of the candidate token is smaller | ||
| # than the model probability for the same token"), keep the token. Otherwise reject with p = 1 - probability_ratio | ||
| # (= keep with p = probability_ratio). Keep all the tokens until the first rejection | ||
| r_i = mint.rand_like(probability_ratio) | ||
| is_accepted = r_i <= probability_ratio | ||
| n_matches = ((~is_accepted).cumsum(dim=-1) < 1).sum() # this is `n` in algorithm 1 | ||
|
|
||
| # Ensure we don't generate beyond max_len or an EOS token (not in algorithm 1, but needed for correct behavior) | ||
| if is_done_candidate and n_matches == candidate_length: | ||
| # Output length is assumed to be `n_matches + 1`. Since we won't generate another token with the target model | ||
| # due to acceptance on EOS we fix `n_matches` | ||
| n_matches -= 1 | ||
| valid_tokens = new_candidate_input_ids[:, : n_matches + 1] | ||
| else: | ||
| # Next token selection: if there is a rejection, adjust the distribution from the main model before sampling. | ||
| gamma = candidate_logits.shape[1] | ||
| p_n_plus_1 = p[:, n_matches, :] | ||
| if n_matches < gamma: | ||
| q_n_plus_1 = q[:, n_matches, :] | ||
| p_prime = mint.clamp((p_n_plus_1 - q_n_plus_1), min=0) | ||
| p_prime.div_(p_prime.sum()) | ||
| else: | ||
| p_prime = p_n_plus_1 | ||
| t = mint.squeeze(mint.multinomial(p_prime, num_samples=1), dim=1)[None, :] | ||
|
|
||
| # The selected tokens include the matches (if any) plus the next sampled tokens | ||
| if n_matches > 0: | ||
| valid_tokens = mint.cat((new_candidate_input_ids[:, :n_matches], t), dim=-1) | ||
| else: | ||
| valid_tokens = t | ||
|
|
||
| return valid_tokens, n_matches | ||
Oops, something went wrong.
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.
This multiline string is used as a comment. For better readability and to avoid potential runtime effects of docstrings, it's recommended to use standard
#comments.