-
Notifications
You must be signed in to change notification settings - Fork 140
Re-implement Top-P sampling and LogitsWarper
using MLTensor
#274
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
#if canImport(CoreML) | ||
import CoreML | ||
|
||
/// Protocol for modifying logits before token sampling. | ||
/// | ||
/// Logits warpers can be used to apply various transformations to the logits | ||
/// distribution before sampling, such as temperature scaling, top-k filtering, | ||
/// top-p (nucleus) filtering, or repetition penalties. | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public protocol LogitsWarper { | ||
/// Warps (modifies) the logits before sampling. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: The input token sequence used for context-dependent warping | ||
/// - logits: The logits tensor to be modified | ||
/// - Returns: The modified logits tensor | ||
func warp(inputIds: MLTensor, logits: MLTensor) -> MLTensor | ||
|
||
/// Alternative call syntax for convenience. | ||
func callAsFunction(inputIds: MLTensor, logits: MLTensor) -> MLTensor | ||
} | ||
|
||
@available(macOS 15.0, iOS 18.0, *) | ||
public extension LogitsWarper { | ||
/// Default implementation of callAsFunction that delegates to warp. | ||
func callAsFunction(inputIds: MLTensor, logits: MLTensor) -> MLTensor { | ||
warp(inputIds: inputIds, logits: logits) | ||
} | ||
} | ||
|
||
/// A collection of logits warpers that processes logits sequentially. | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public struct LogitsProcessor { | ||
private let warpers: [LogitsWarper] | ||
|
||
/// Creates a new logits processor with the specified warpers. | ||
/// | ||
/// - Parameter warpers: Array of logits warpers to apply sequentially | ||
public init(warpers: [LogitsWarper] = []) { | ||
self.warpers = warpers | ||
} | ||
|
||
/// Applies all warpers sequentially to the logits. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: The input token sequence | ||
/// - logits: The logits tensor to process | ||
/// - Returns: The processed logits tensor | ||
public func process(inputIds: MLTensor, logits: MLTensor) -> MLTensor { | ||
var processedLogits = logits | ||
for warper in warpers { | ||
processedLogits = warper.warp(inputIds: inputIds, logits: processedLogits) | ||
} | ||
return processedLogits | ||
} | ||
|
||
/// Alternative call syntax for convenience. | ||
public func callAsFunction(inputIds: MLTensor, logits: MLTensor) -> MLTensor { | ||
process(inputIds: inputIds, logits: logits) | ||
} | ||
} | ||
|
||
#endif // canImport(CoreML) |
This file contains hidden or 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,44 @@ | ||
#if canImport(CoreML) | ||
import CoreML | ||
|
||
/// Logits warper that applies repetition penalty. | ||
/// | ||
/// Repetition penalty reduces the likelihood of generating tokens that have | ||
/// already appeared in the input sequence. This helps reduce repetitive text | ||
/// generation. | ||
/// | ||
/// - Note: Penalty > 1.0 penalizes repetition, penalty < 1.0 encourages it | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public struct RepetitionPenaltyWarper: LogitsWarper { | ||
/// The repetition penalty factor. | ||
public let penalty: Float | ||
|
||
/// Creates a new repetition penalty warper. | ||
/// | ||
/// - Parameter penalty: Penalty factor (must be > 0). Values > 1.0 penalize repetition. | ||
public init(penalty: Double) { | ||
precondition(penalty > 0, "Penalty must be strictly positive") | ||
self.penalty = Float(penalty) | ||
} | ||
|
||
/// Applies repetition penalty to tokens that appear in the input sequence. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: The input token sequence used to identify repeated tokens | ||
/// - logits: The logits tensor to modify | ||
/// - Returns: Logits with repetition penalty applied | ||
public func warp(inputIds: MLTensor, logits: MLTensor) -> MLTensor { | ||
if penalty == 1.0 { | ||
return logits | ||
} | ||
|
||
// TODO: Implement repetition penalty when MLTensor API allows for easier tensor updates | ||
// For now, we'll return the original logits to avoid compilation errors | ||
// This functionality will need to be implemented when tensor item access and update operations are available | ||
|
||
print("Warning: Repetition penalty is not yet implemented due to MLTensor API limitations") | ||
return logits | ||
} | ||
} | ||
|
||
#endif // canImport(CoreML) |
This file contains hidden or 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,37 @@ | ||
#if canImport(CoreML) | ||
import CoreML | ||
|
||
/// Logits warper that applies temperature scaling. | ||
/// | ||
/// Temperature scaling modifies the sharpness of the probability distribution: | ||
/// - Temperature < 1.0: Makes the distribution more concentrated (less random) | ||
/// - Temperature = 1.0: No change to the distribution | ||
/// - Temperature > 1.0: Makes the distribution more uniform (more random) | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public struct TemperatureLogitsWarper: LogitsWarper { | ||
/// The temperature value for scaling logits. | ||
public let temperature: Float | ||
|
||
/// Creates a new temperature logits warper. | ||
/// | ||
/// - Parameter temperature: Temperature value (must be > 0) | ||
public init(temperature: Double) { | ||
precondition(temperature > 0, "Temperature must be strictly positive") | ||
self.temperature = Float(temperature) | ||
} | ||
|
||
/// Applies temperature scaling to the logits. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: The input token sequence (unused by temperature warper) | ||
/// - logits: The logits tensor to scale | ||
/// - Returns: Temperature-scaled logits | ||
public func warp(inputIds: MLTensor, logits: MLTensor) -> MLTensor { | ||
if temperature == 1.0 { | ||
return logits | ||
} | ||
return logits / temperature | ||
} | ||
} | ||
|
||
#endif // canImport(CoreML) |
This file contains hidden or 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 |
---|---|---|
|
@@ -129,6 +129,7 @@ extension LanguageModel { | |
static let valueCache = "valueCache" | ||
// Output keys | ||
static let logits = "logits" | ||
// swift-format-ignore: DontRepeatTypeInStaticProperties | ||
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. Lint check failing despite disabling rule globally in |
||
static let presentKeys = "presentKeys" | ||
static let presentValues = "presentValues" | ||
} | ||
|
This file contains hidden or 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,70 @@ | ||
import CoreML | ||
import Testing | ||
|
||
@testable import Generation | ||
|
||
#if canImport(CoreML) | ||
@Suite("Logits Warper Tests") | ||
struct LogitsWarperTests { | ||
|
||
@Test("Temperature warper scaling") | ||
@available(macOS 15.0, iOS 18.0, *) | ||
func temperatureWarper() { | ||
let logits = MLTensor([[1.0, 2.0, 3.0]]) | ||
let inputIds = MLTensor([[1, 2]]) | ||
|
||
let tempWarper = TemperatureLogitsWarper(temperature: 2.0) | ||
let warpedLogits = tempWarper.warp(inputIds: inputIds, logits: logits) | ||
|
||
#expect(warpedLogits.shape == logits.shape) | ||
|
||
let identityWarper = TemperatureLogitsWarper(temperature: 1.0) | ||
let unchangedLogits = identityWarper.warp(inputIds: inputIds, logits: logits) | ||
#expect(unchangedLogits.shape == logits.shape) | ||
} | ||
|
||
@Test("LogitsProcessor with multiple warpers") | ||
@available(macOS 15.0, iOS 18.0, *) | ||
func logitsProcessor() { | ||
let logits = MLTensor([[1.0, 2.0, 3.0]]) | ||
let inputIds = MLTensor([[1, 2]]) | ||
|
||
let warpers: [LogitsWarper] = [ | ||
TemperatureLogitsWarper(temperature: 2.0) | ||
] | ||
|
||
let processor = LogitsProcessor(warpers: warpers) | ||
let processedLogits = processor.process(inputIds: inputIds, logits: logits) | ||
|
||
#expect(processedLogits.shape == logits.shape) | ||
} | ||
|
||
@Test("LogitsProcessor with no warpers") | ||
@available(macOS 15.0, iOS 18.0, *) | ||
func logitsProcessorEmpty() { | ||
let logits = MLTensor([[1.0, 2.0, 3.0]]) | ||
let inputIds = MLTensor([[1, 2]]) | ||
|
||
let processor = LogitsProcessor(warpers: []) | ||
let processedLogits = processor.process(inputIds: inputIds, logits: logits) | ||
|
||
#expect(processedLogits.shape == logits.shape) | ||
} | ||
|
||
@Test("Repetition penalty warper") | ||
@available(macOS 15.0, iOS 18.0, *) | ||
func repetitionPenaltyWarper() { | ||
let logits = MLTensor([[1.0, 2.0, 3.0]]) | ||
let inputIds = MLTensor([[0, 1]]) | ||
|
||
let repWarper = RepetitionPenaltyWarper(penalty: 1.2) | ||
let warpedLogits = repWarper.warp(inputIds: inputIds, logits: logits) | ||
|
||
#expect(warpedLogits.shape == logits.shape) | ||
|
||
let identityWarper = RepetitionPenaltyWarper(penalty: 1.0) | ||
let unchangedLogits = identityWarper.warp(inputIds: inputIds, logits: logits) | ||
#expect(unchangedLogits.shape == logits.shape) | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
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.
Strange that the lint check fails even when this is disabled. FWIW, I also can't reproduce the lint failure locally...