-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize(utils): move custom processors into model (#419)
- Loading branch information
Showing
6 changed files
with
50 additions
and
49 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import torch | ||
import torch.nn.functional as F | ||
|
||
|
||
class CustomRepetitionPenaltyLogitsProcessorRepeat(): | ||
|
||
def __init__(self, penalty: float, max_input_ids, past_window): | ||
if not isinstance(penalty, float) or not (penalty > 0): | ||
raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}") | ||
|
||
self.penalty = penalty | ||
self.max_input_ids = max_input_ids | ||
self.past_window = past_window | ||
|
||
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: | ||
|
||
input_ids = input_ids[:, -self.past_window:] | ||
freq = F.one_hot(input_ids, scores.size(1)).sum(1) | ||
freq[self.max_input_ids:] = 0 | ||
alpha = self.penalty**freq | ||
scores = scores.contiguous() | ||
scores = torch.where(scores < 0, scores*alpha, scores/alpha) | ||
|
||
return scores | ||
|
||
class CustomRepetitionPenaltyLogitsProcessor(): | ||
|
||
def __init__(self, penalty: float, max_input_ids, past_window): | ||
if not isinstance(penalty, float) or not (penalty > 0): | ||
raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}") | ||
|
||
self.penalty = penalty | ||
self.max_input_ids = max_input_ids | ||
self.past_window = past_window | ||
|
||
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: | ||
|
||
input_ids = input_ids[:, -self.past_window:] | ||
score = torch.gather(scores, 1, input_ids) | ||
_score = score.detach().clone() | ||
score = torch.where(score < 0, score * self.penalty, score / self.penalty) | ||
score[input_ids>=self.max_input_ids] = _score[input_ids>=self.max_input_ids] | ||
scores.scatter_(1, input_ids, score) | ||
|
||
return scores |
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
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