-
Notifications
You must be signed in to change notification settings - Fork 140
Logit warpers #273
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
Logit warpers #273
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
486e58f
inital
kashif e550fa4
formatting
kashif d6f1aee
added generation integration tests
kashif 55b534f
use MLTensor
kashif 75b9661
fix top-p and do-sample
kashif 8e4db8f
fix CI issue
kashif 4d012b9
undo changes to LanguageModel.swift
kashif 9492d47
use MLTensor
kashif 83164c2
Update Tests/GenerationTests/GenerationIntegrationTests.swift
kashif fff7a3c
Throws: If penalty is not strictly positive
kashif 9754942
remove unused selectNextTokenUsingTopKSampling
kashif d6a7526
make sure ordering of warpers is that of transformers
kashif aec446d
throw
kashif 501f192
add Min-P
kashif 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
Some comments aren't visible on the classic Files Changed page.
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
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,59 @@ | ||
#if canImport(CoreML) | ||
import CoreML | ||
|
||
/// Abstract base class for all logits processors that can be applied during generation. | ||
/// | ||
/// Logits processors modify the probability distribution over vocabulary tokens by transforming | ||
/// the raw logit scores produced by language models. This enables various sampling strategies | ||
/// such as temperature scaling, top-k/top-p filtering, and repetition penalties. | ||
/// | ||
/// Based on: https://github.com/huggingface/transformers/blob/main/src/transformers/generation/logits_process.py | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public protocol LogitsProcessor { | ||
/// Processes logits for next token prediction. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: Tensor of input token IDs with shape `[batch_size, sequence_length]` | ||
/// - scores: Tensor of raw logit scores with shape `[batch_size, vocab_size]` | ||
/// - Returns: Processed logits tensor with shape `[batch_size, vocab_size]` | ||
/// | ||
/// - Note: The `inputIds` parameter provides context for processors that need to examine | ||
/// the generated sequence (e.g., repetition penalty). Processors that don't need this | ||
/// context (e.g., temperature) can ignore it. | ||
func callAsFunction(_ inputIds: MLTensor, _ scores: MLTensor) async -> MLTensor | ||
} | ||
|
||
/// A list of logits processors that applies each processor sequentially. | ||
/// | ||
/// This class provides a convenient way to chain multiple logits processors together. | ||
/// Each processor is applied in order to the logits tensor, with the output of one | ||
/// processor becoming the input to the next. | ||
@available(macOS 15.0, iOS 18.0, *) | ||
public struct LogitsProcessorList { | ||
public var processors: [any LogitsProcessor] | ||
|
||
public init(processors: [any LogitsProcessor]) { | ||
self.processors = processors | ||
} | ||
|
||
/// Applies all logits processors sequentially to the input scores. | ||
/// | ||
/// - Parameters: | ||
/// - inputIds: Tensor of input token IDs with shape `[batch_size, sequence_length]` | ||
/// - scores: Tensor of raw logit scores with shape `[batch_size, vocab_size]` | ||
/// - Returns: Processed logits tensor with shape `[batch_size, vocab_size]` | ||
public func callAsFunction(_ inputIds: MLTensor, _ scores: MLTensor) async -> MLTensor { | ||
// Following transformers convention: all logits processing happens in Float32 | ||
// Cast to Float32 once at the start, process, then cast back to original type at the end | ||
let originalScalarType = scores.scalarType | ||
var processedScores = scores.scalarType == Float.self ? scores : scores.cast(to: Float.self) | ||
|
||
for processor in processors { | ||
processedScores = await processor(inputIds, processedScores) | ||
} | ||
|
||
// Cast back to original type if needed | ||
return originalScalarType == Float.self ? processedScores : processedScores.cast(to: originalScalarType) | ||
} | ||
} | ||
#endif |
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.
Uh oh!
There was an error while loading. Please reload this page.