Skip to content
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,8 @@ mlxOptions.topK = 40
mlxOptions.minP = 0.05
mlxOptions.repetitionPenalty = 1.1
mlxOptions.repetitionContextSize = 64
mlxOptions.presencePenalty = 1.5
mlxOptions.presenceContextSize = 64
options[custom: MLXLanguageModel.self] = mlxOptions

let response = try await session.respond(
Expand All @@ -1499,7 +1501,8 @@ By default, images are passed through without an explicit resize override
You can also set `additionalContext` to provide extra JSON template variables
for model-specific chat templates.

The sampling fields (`topP`, `topK`, `minP`, `repetitionPenalty`, `repetitionContextSize`)
The sampling fields (`topP`, `topK`, `minP`, `repetitionPenalty`, `repetitionContextSize`,
`presencePenalty`, `presenceContextSize`)
override the matching values from `GenerationOptions.sampling`.
Leave them `nil` to inherit those values,
or to use MLX's defaults when neither is set.
Expand Down
22 changes: 21 additions & 1 deletion Sources/AnyLanguageModel/Models/MLXLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ import Foundation
/// Set this to `nil` to use `20` for regular generation or `64` for structured generation.
public var repetitionContextSize: Int?

/// Additive penalty applied to tokens that appear in the recent context.
///
/// Set this to `nil` or `0` to use no presence penalty.
public var presencePenalty: Float?

/// Number of recent tokens considered by the presence penalty.
///
/// Set this to `nil` to use `20`.
public var presenceContextSize: Int?

/// Creates MLX-specific generation options.
///
/// - Parameters:
Expand All @@ -348,6 +358,8 @@ import Foundation
/// to neutralize the penalty explicitly.
/// - repetitionContextSize: Repetition-penalty token window. Defaults to `nil`
/// (`20` for regular generation or `64` for structured generation).
/// - presencePenalty: Additive presence penalty. Defaults to `nil` (disabled).
/// - presenceContextSize: Presence-penalty token window. Defaults to `nil` (`20`).
public init(
kvCache: KVCache,
userInputProcessing: UserInputProcessing?,
Expand All @@ -356,7 +368,9 @@ import Foundation
topK: Int? = nil,
minP: Float? = nil,
repetitionPenalty: Float? = nil,
repetitionContextSize: Int? = nil
repetitionContextSize: Int? = nil,
presencePenalty: Float? = nil,
presenceContextSize: Int? = nil
) {
self.kvCache = kvCache
self.additionalContext = additionalContext
Expand All @@ -366,6 +380,8 @@ import Foundation
self.minP = minP
self.repetitionPenalty = repetitionPenalty
self.repetitionContextSize = repetitionContextSize
self.presencePenalty = presencePenalty
self.presenceContextSize = presenceContextSize
}

/// Default MLX generation options used when none are provided at runtime.
Expand Down Expand Up @@ -1641,6 +1657,8 @@ import Foundation
minP: custom?.minP ?? 0.0,
repetitionPenalty: custom?.repetitionPenalty,
repetitionContextSize: custom?.repetitionContextSize ?? 20,
presencePenalty: custom?.presencePenalty,
presenceContextSize: custom?.presenceContextSize ?? 20,
seed: derived.seed
)
}
Expand All @@ -1661,6 +1679,8 @@ import Foundation
minP: custom?.minP ?? 0.0,
repetitionPenalty: custom?.repetitionPenalty ?? 1.1,
repetitionContextSize: custom?.repetitionContextSize ?? 64,
presencePenalty: custom?.presencePenalty,
presenceContextSize: custom?.presenceContextSize ?? 20,
seed: derived.seed
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,8 @@ struct GeminiCustomOptionsTests {
#expect(params.minP == 0)
#expect(params.repetitionPenalty == Float(1.1))
#expect(params.repetitionContextSize == 64)
#expect(params.presencePenalty == nil)
#expect(params.presenceContextSize == 20)
}

@Test(arguments: [false, true])
Expand Down Expand Up @@ -1175,7 +1177,9 @@ struct GeminiCustomOptionsTests {
topK: 0,
minP: 0.1,
repetitionPenalty: 1.0,
repetitionContextSize: 128
repetitionContextSize: 128,
presencePenalty: 1.5,
presenceContextSize: 64
)
let params = toStructuredGenerateParameters(options)
#expect(params.temperature == Float(0.7))
Expand All @@ -1184,6 +1188,8 @@ struct GeminiCustomOptionsTests {
#expect(params.minP == Float(0.1))
#expect(params.repetitionPenalty == 1.0)
#expect(params.repetitionContextSize == 128)
#expect(params.presencePenalty == Float(1.5))
#expect(params.presenceContextSize == 64)
}

@Test func codable() throws {
Expand Down
Loading