-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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 repetition_penalty aligned with huggingface #866
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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 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 |
---|---|---|
|
@@ -21,7 +21,7 @@ class Sampler(nn.Module): | |
1. Discard the hidden states that are not used for sampling (i.e., all | ||
tokens except the final one in each prompt). | ||
2. Compute the logits for the next tokens. | ||
3. Apply presence and frequency penalties. | ||
3. Apply presence, frequency and repetition penalties. | ||
4. Apply temperature scaling. | ||
5. Apply top-p and top-k truncation. | ||
6. Sample the next tokens. | ||
|
@@ -54,12 +54,14 @@ def forward( | |
# Apply presence and frequency penalties. | ||
output_tokens = _get_output_tokens(input_metadata) | ||
assert len(output_tokens) == logits.shape[0] | ||
presence_penalties, frequency_penalties = _get_penalties( | ||
input_metadata) | ||
presence_penalties, frequency_penalties, repetition_penalties = \ | ||
_get_penalties(input_metadata) | ||
assert len(presence_penalties) == logits.shape[0] | ||
assert len(frequency_penalties) == logits.shape[0] | ||
logits = _apply_penalties(logits, output_tokens, presence_penalties, | ||
frequency_penalties, self.vocab_size) | ||
assert len(repetition_penalties) == logits.shape[0] | ||
logits = _apply_penalties(input_metadata, logits, output_tokens, | ||
presence_penalties, frequency_penalties, | ||
repetition_penalties, self.vocab_size) | ||
|
||
# Apply temperature scaling. | ||
temperatures = _get_temperatures(input_metadata) | ||
|
@@ -108,19 +110,23 @@ def _get_penalties( | |
# Collect the presence and frequency penalties. | ||
presence_penalties: List[float] = [] | ||
frequency_penalties: List[float] = [] | ||
repetition_penalties: List[float] = [] | ||
for i, seq_group in enumerate(input_metadata.seq_groups): | ||
seq_ids, sampling_params = seq_group | ||
p = sampling_params.presence_penalty | ||
f = sampling_params.frequency_penalty | ||
r = sampling_params.repetition_penalty | ||
if i < input_metadata.num_prompts: | ||
# A prompt input. | ||
presence_penalties.append(p) | ||
frequency_penalties.append(f) | ||
repetition_penalties.append(r) | ||
else: | ||
# A generation token. | ||
presence_penalties += [p] * len(seq_ids) | ||
frequency_penalties += [f] * len(seq_ids) | ||
return presence_penalties, frequency_penalties | ||
repetition_penalties += [r] * len(seq_ids) | ||
return presence_penalties, frequency_penalties, repetition_penalties | ||
|
||
|
||
def _get_output_tokens(input_metadata: InputMetadata) -> List[List[int]]: | ||
|
@@ -143,10 +149,12 @@ def _get_output_tokens(input_metadata: InputMetadata) -> List[List[int]]: | |
|
||
|
||
def _apply_penalties( | ||
input_metadata: InputMetadata, | ||
logits: torch.Tensor, | ||
output_tokens: List[List[int]], | ||
presence_penalties: List[float], | ||
frequency_penalties: List[float], | ||
repetition_penalties: List[float], | ||
vocab_size: int, | ||
) -> torch.Tensor: | ||
num_seqs = logits.shape[0] | ||
|
@@ -162,30 +170,61 @@ def _apply_penalties( | |
indices.append(i) | ||
|
||
# Return early if all sequences have zero penalties. | ||
if not indices: | ||
return logits | ||
|
||
bin_counts = [] | ||
for i in indices: | ||
bin_counts.append(np.bincount(output_tokens[i], minlength=vocab_size)) | ||
bin_counts = np.stack(bin_counts, axis=0) | ||
bin_counts = torch.from_numpy(bin_counts).to(dtype=logits.dtype, | ||
device=logits.device) | ||
|
||
frequency_penalties = [frequency_penalties[i] for i in indices] | ||
frequency_penalties = torch.tensor(frequency_penalties, | ||
dtype=logits.dtype, | ||
device=logits.device) | ||
presence_penalties = [presence_penalties[i] for i in indices] | ||
presence_penalties = torch.tensor(presence_penalties, | ||
dtype=logits.dtype, | ||
device=logits.device) | ||
|
||
# We follow the definition in OpenAI API. | ||
# Refer to https://platform.openai.com/docs/api-reference/parameter-details | ||
logits[indices] -= frequency_penalties.unsqueeze(dim=1) * bin_counts | ||
presence_mask = (bin_counts > 0.0).to(dtype=logits.dtype) | ||
logits[indices] -= presence_penalties.unsqueeze(dim=1) * presence_mask | ||
if indices: | ||
bin_counts = [] | ||
for i in indices: | ||
bin_counts.append( | ||
np.bincount(output_tokens[i], minlength=vocab_size)) | ||
bin_counts = np.stack(bin_counts, axis=0) | ||
bin_counts = torch.from_numpy(bin_counts).to(dtype=logits.dtype, | ||
device=logits.device) | ||
|
||
frequency_penalties = [frequency_penalties[i] for i in indices] | ||
frequency_penalties = torch.tensor(frequency_penalties, | ||
dtype=logits.dtype, | ||
device=logits.device) | ||
presence_penalties = [presence_penalties[i] for i in indices] | ||
presence_penalties = torch.tensor(presence_penalties, | ||
dtype=logits.dtype, | ||
device=logits.device) | ||
# We follow the definition in OpenAI API. | ||
# Refer to | ||
# https://platform.openai.com/docs/api-reference/parameter-details | ||
logits[indices] -= frequency_penalties.unsqueeze(dim=1) * bin_counts | ||
presence_mask = (bin_counts > 0.0).to(dtype=logits.dtype) | ||
logits[indices] -= presence_penalties.unsqueeze(dim=1) * presence_mask | ||
else: | ||
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. why |
||
# repetition penalty aligned with huggingface transformers | ||
for i, seq_group in enumerate(input_metadata.seq_groups): | ||
r = repetition_penalties[i] | ||
if r == 1.0: | ||
continue | ||
seq_ids, _ = seq_group | ||
if i < input_metadata.num_prompts: | ||
# A prompt input. | ||
# NOTE: While the prompt input usually has no output tokens, | ||
# it may have output tokens in the case of recomputation. | ||
seq_id = seq_ids[0] | ||
seq_data = input_metadata.seq_data[seq_id] | ||
token_ids = seq_data.get_token_ids() | ||
token_ids = torch.tensor(token_ids, | ||
dtype=torch.int64, | ||
device=logits.device) | ||
score = torch.gather(logits[i], 0, token_ids) | ||
score = torch.where(score < 0, score * r, score / r) | ||
logits[i].scatter_(0, token_ids, score) | ||
else: | ||
# A generation token. | ||
for seq_id in seq_ids: | ||
seq_data = input_metadata.seq_data[seq_id] | ||
token_ids = seq_data.get_token_ids() | ||
token_ids = torch.tensor(token_ids, | ||
dtype=torch.int64, | ||
device=logits.device) | ||
score = torch.gather(logits[i], 0, token_ids) | ||
score = torch.where(score < 0, score * r, score / r) | ||
logits[i].scatter_(0, token_ids, score) | ||
|
||
return logits | ||
|
||
|
||
|
This file contains 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
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 comment is misleading now?