Skip to content

Add repetition penalty warper #85

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

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/Generation/Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public extension Generation {
if config.topP < 1.0 {
logitsWarpers.append(TopPLogitsWarper(p: Float(config.topP)))
}
if config.repetitionPenalty != 1.0 {
logitsWarpers.append(RepetitionPenaltyWarper(penalty: config.repetitionPenalty))
}
return logitsWarpers
}
}
25 changes: 25 additions & 0 deletions Sources/TensorUtils/LogitsWarper/RepetitionPenaltyWarper.swift
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's throw here if penalty <= 0 (it must be strictly positive)

self.penalty = Float(penalty)
}

public func warp(indices: [Int], logits: [Float]) -> (indices: [Int], logits: [Float]) {
var logits = logits
for index in indices {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to find an accelerated version of this? (not critical right now, but good to keep in mind for the future)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use gather scatter, but didn't find scatter method in vDSP.

if logits[index] < 0 {
logits[index] *= penalty
} else {
logits[index] /= penalty
}
}

return (indices, logits)
}
}
30 changes: 30 additions & 0 deletions Tests/TensorUtilsTests/LogitsWarperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ final class LogitsWarperTests: XCTestCase {
XCTAssertEqual(result5.logits, [2, 1, 0], accuracy: accuracy)
}

func testRepetitionPenaltyWarper() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests! Did you verify results against the reference transformers implementation?

let indices = Array(0..<10)
let logits = indices.map({ Float($0) })

let result1 = RepetitionPenaltyWarper(penalty: 1.0)(indices, logits)
XCTAssertEqual(result1.indices, indices)
XCTAssertEqual(result1.logits, logits, accuracy: accuracy)

let result2 = RepetitionPenaltyWarper(penalty: 3.75)(indices, logits)
XCTAssertEqual(result2.indices, indices)
let logits2 = indices.map({ Float($0) / 3.75 })
XCTAssertEqual(result2.logits, logits2, accuracy: accuracy)

let result3 = RepetitionPenaltyWarper(penalty: 0.75)([0, 1, 2], [0.8108, 0.9954, 0.0119])
XCTAssertEqual(result3.indices, [0, 1, 2])
XCTAssertEqual(result3.logits, [1.0811, 1.3272, 0.0158], accuracy: 1e-4)

let result4 = RepetitionPenaltyWarper(penalty: 1.11)([2, 3, 4], [0.5029, 0.8694, 0.4765, 0.9967, 0.4190, 0.9158])
XCTAssertEqual(result4.indices, [2, 3, 4])
XCTAssertEqual(result4.logits, [0.5029, 0.8694, 0.4293, 0.8980, 0.3775, 0.9158], accuracy: 1e-4)

let result5 = RepetitionPenaltyWarper(penalty: 0.9)([0, 1, 2], [-0.7433, -0.4738, -0.2966])
XCTAssertEqual(result5.indices, [0, 1, 2])
XCTAssertEqual(result5.logits, [-0.6690, -0.4264, -0.2669], accuracy: 1e-4)

let result6 = RepetitionPenaltyWarper(penalty: 1.125)([3, 1, 2], [0.1674, 0.6431, 0.6780, 0.2755])
XCTAssertEqual(result6.indices, [3, 1, 2])
XCTAssertEqual(result6.logits, [0.1674, 0.5716, 0.6026, 0.2449], accuracy: 1e-4)
}

func testLogitsProcessor() {
let processor1 = LogitsProcessor(logitsWarpers: [])
let result1 = processor1([])
Expand Down