-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add repetition penalty warper * Float the penalty * Add penalty to logits warpers * Test repetition penalty
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 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
25 changes: 25 additions & 0 deletions
25
Sources/TensorUtils/LogitsWarper/RepetitionPenaltyWarper.swift
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,25 @@ | ||
import Foundation | ||
|
||
/// `RepetitionPenaltyWarper` prevents the repetition of previous tokens through a penalty. | ||
/// This penalty is applied at most once per token. | ||
/// https://github.com/huggingface/transformers/blob/main/src/transformers/generation/logits_process.py#L294 | ||
public struct RepetitionPenaltyWarper: LogitsWarper { | ||
public var penalty: Float | ||
|
||
public init(penalty: Double) { | ||
self.penalty = Float(penalty) | ||
} | ||
|
||
public func warp(indices: [Int], logits: [Float]) -> (indices: [Int], logits: [Float]) { | ||
var logits = logits | ||
for index in indices { | ||
if logits[index] < 0 { | ||
logits[index] *= penalty | ||
} else { | ||
logits[index] /= penalty | ||
} | ||
} | ||
|
||
return (indices, 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