-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[v1] Support allowed_token_ids in v1 Sampler #13210
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,8 @@ def forward( | |
|
|
||
| # Use float32 for the logits. | ||
| logits = logits.to(torch.float32) | ||
| # Apply allowed token ids. | ||
| logits = self.apply_allowed_token_ids(logits, sampling_metadata) | ||
| # Apply logits bias. | ||
| logits = self.apply_logits_bias(logits, sampling_metadata) | ||
| # Apply penalties (e.g., min_tokens, freq_penalties). | ||
|
|
@@ -184,11 +186,13 @@ def apply_penalties( | |
| if not sampling_metadata.no_penalties: | ||
| assert sampling_metadata.prompt_token_ids is not None | ||
| logits = apply_all_penalties( | ||
| logits, sampling_metadata.prompt_token_ids, | ||
| logits, | ||
| sampling_metadata.prompt_token_ids, | ||
| sampling_metadata.presence_penalties, | ||
| sampling_metadata.frequency_penalties, | ||
| sampling_metadata.repetition_penalties, | ||
| sampling_metadata.output_token_ids) | ||
| sampling_metadata.output_token_ids, | ||
| ) | ||
|
||
| return logits | ||
|
|
||
| def apply_min_p( | ||
|
|
@@ -226,3 +230,13 @@ def apply_logits_bias( | |
| for token_id, bias in logit_bias.items(): | ||
| logits[i, token_id] += bias | ||
| return logits | ||
|
|
||
| def apply_allowed_token_ids( | ||
| self, | ||
| logits: torch.Tensor, | ||
| sampling_metadata: SamplingMetadata, | ||
| ) -> torch.Tensor: | ||
| if sampling_metadata.allowed_token_ids_mask is not None: | ||
| logits.masked_fill_(sampling_metadata.allowed_token_ids_mask, | ||
| float("-inf")) | ||
| return logits | ||
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.
Thank you for adding this test!!