Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Red control only: the policy surface the prompt-limit tests call, without
// the change. The setting is parsed, but no chip gets a limit and the
// LaunchAgent does not forward the key.

import Foundation

@testable import ProviderCore

extension MTPAutomaticVerificationPolicy {
enum GemmaDraftPromptLimit: Equatable {
case chipDefault
case off
case tokens(Int)

init(_ raw: String?) {
let value = raw?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() ?? ""
if ["0", "off", "false", "no"].contains(value) {
self = .off
} else if let tokens = Int(value), tokens > 0 {
self = .tokens(tokens)
} else {
self = .chipDefault
}
}
}

static let gemmaMaxDraftPromptTokensEnvironmentKey = "DARKBLOOM_GEMMA_MTP_MAX_PROMPT_TOKENS"
static let gemmaDraftPromptLimit = GemmaDraftPromptLimit(
ProcessInfo.processInfo.environment[gemmaMaxDraftPromptTokensEnvironmentKey])
static let gemmaM5UltraMaxDraftPromptTokens = 16384

static func maxDraftPromptTokens(
modelID: String?, chipName: String? = nil, hasBenchmarkVerificationOverride: Bool = false,
twoDrafts: Bool? = nil, limit: GemmaDraftPromptLimit? = nil
) -> Int? {
nil
}
}
61 changes: 61 additions & 0 deletions provider-swift/Tests/ProviderCoreTests/Config/MTPConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,67 @@ struct MTPConfigKeyTests {
modelID: gemma, chipName: "Apple M5 Ultra", twoDrafts: true) == (enabled ? [2, 0] : [2, 2, 0]))
}

/// Under the two-draft policy on M5 Ultra, a Gemma QAT prompt longer than
/// 16,384 tokens decodes without drafts; `DARKBLOOM_GEMMA_MTP_MAX_PROMPT_TOKENS`
/// moves the limit or, with `0`, removes it. No other chip, model or
/// policy gets a limit, whatever the setting.
@Test func gemmaQATLongPromptsOnM5UltraDecodeWithoutDrafts() {
typealias Limit = MTPAutomaticVerificationPolicy.GemmaDraftPromptLimit
let gemma = MTPAcceptancePolicy.gemmaQATModelID
func limit(
_ chip: String, _ setting: Limit, model: String? = MTPAcceptancePolicy.gemmaQATModelID,
twoDrafts: Bool = true, benchmark: Bool = false
) -> Int? {
MTPAutomaticVerificationPolicy.maxDraftPromptTokens(
modelID: model, chipName: chip, hasBenchmarkVerificationOverride: benchmark,
twoDrafts: twoDrafts, limit: setting)
}
#expect(MTPAutomaticVerificationPolicy.gemmaM5UltraMaxDraftPromptTokens == 16384)
#expect(limit("Apple M5 Ultra", .chipDefault) == 16384)
#expect(limit("Apple M5 Ultra", .tokens(8192)) == 8192)
#expect(limit("Apple M5 Ultra", .off) == nil)
for setting in [Limit.chipDefault, .off, .tokens(2048)] {
// The two-draft switch off, the benchmark override and other models.
#expect(limit("Apple M5 Ultra", setting, twoDrafts: false) == nil)
#expect(limit("Apple M5 Ultra", setting, benchmark: true) == nil)
for model in ["gemma-4-26b", "gemma-4-26b-8bit", "qwen3.6-35b-a3b-vl-mtp-mxfp8", nil]
as [String?]
{
#expect(limit("Apple M5 Ultra", setting, model: model) == nil)
}
// Every other chip, the other two-draft chips included.
for chip in ["Apple M5 Max", "Apple M3 Ultra", "Apple M5 Pro", "Apple M5", "Apple M4 Max",
"Apple M4 Ultra", "Apple M2 Ultra", "Apple M1 Max", "Unknown"]
{
#expect(limit(chip, setting) == nil, "\(chip)")
}
}
// A call without the setting follows the read-once process value.
#expect(MTPAutomaticVerificationPolicy.maxDraftPromptTokens(
modelID: gemma, chipName: "Apple M5 Ultra", twoDrafts: true)
== limit("Apple M5 Ultra", MTPAutomaticVerificationPolicy.gemmaDraftPromptLimit))
}

@Test func gemmaDraftPromptLimitParsesTheEnvironment() {
typealias Limit = MTPAutomaticVerificationPolicy.GemmaDraftPromptLimit
for raw in [nil, "", " ", "abc", "-5", "4096.5", "1e4"] as [String?] {
#expect(Limit(raw) == .chipDefault, "\(raw ?? "nil")")
}
for raw in ["0", " 0 ", "OFF", "off", "false", "No"] {
#expect(Limit(raw) == .off, "\(raw)")
}
#expect(Limit("8192") == .tokens(8192))
#expect(Limit(" 2048 ") == .tokens(2048))
}

@Test func gemmaDraftPromptLimitRollbackIsForwarded() {
let key = MTPAutomaticVerificationPolicy.gemmaMaxDraftPromptTokensEnvironmentKey
#expect(key == "DARKBLOOM_GEMMA_MTP_MAX_PROMPT_TOKENS")
#expect(LaunchAgent.inferencePassthroughEnvKeys.contains(key))
#expect(LaunchAgent.passthroughEnvironment(from: [key: "0"])[key] == "0")
#expect(LaunchAgent.passthroughEnvironment(from: [:])[key] == nil)
}

@Test func gemmaM5UltraLoneRowRollbackIsForwarded() {
let key = MTPAutomaticVerificationPolicy.gemmaM5UltraLoneRowEnvironmentKey
#expect(key == "DARKBLOOM_GEMMA_MTP_M5_ULTRA_LONE_ROW")
Expand Down
Loading