AI candidate selection: rerank with a Traditional Chinese language model (macOS)

The converter's K best paths are rescored with CKIP gpt2-tiny-chinese (GPL-3.0,
character-level, ~12M parameters, 22 MB as float16):
score = lexicon log-prob + 0.6 x LM log-prob, with the recently committed text
as context. Inference is plain Accelerate (Apple Silicon and Intel).

Modes off / saver (K=3, 15 ms) / standard (K=10, 40 ms) / quality (K=30, 120 ms)
in the input menu. A rerank over its time budget falls back to the lexicon; three
in a row pause AI for a minute. With auto-pause on, AI pauses when the Mac is hot
or the CPU is busy and drops to saver in Low Power Mode. The model loads in the
background and is not loaded at all when off.

Evaluation (training/lm/eval_rerank.py): chat 96.6% -> 97.5% chars,
Wikipedia 92.9% -> 94.7% chars (about 25-30% fewer wrong characters).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 16:33:58 +08:00
co-authored by Claude Opus 5.5
parent acafd8f96e
commit fd87da8061
16 changed files with 1653 additions and 2 deletions
+97
View File
@@ -0,0 +1,97 @@
import Foundation
import AutoZhuyinCore
/// "AI 選字" settings, model loading and system-load detection for the Mac app.
enum AI {
static let modeKey = "AIMode"
static let autoPauseKey = "AIAutoPause"
static let reranker: Reranker = {
let saved = UserDefaults.standard.string(forKey: modeKey).flatMap(Reranker.Mode.init(rawValue:))
let r = Reranker(mode: saved ?? .standard)
r.systemState = { SystemMonitor.shared.state() }
return r
}()
/// The model file is missing from the app bundle (built without scripts/fetch-lm.sh).
private(set) static var modelMissing = false
private static var loading = false
static var autoPause: Bool {
get { UserDefaults.standard.object(forKey: autoPauseKey) as? Bool ?? true }
set { UserDefaults.standard.set(newValue, forKey: autoPauseKey); SystemMonitor.shared.invalidate() }
}
static func setMode(_ mode: Reranker.Mode) {
reranker.mode = mode
UserDefaults.standard.set(mode.rawValue, forKey: modeKey)
if mode == .off {
reranker.model = nil // free the memory
} else {
loadModelIfNeeded()
}
}
/// Loads the model in the background (only when AI is on).
static func loadModelIfNeeded() {
guard reranker.mode != .off, reranker.model == nil, !loading, !modelMissing else { return }
guard let bin = Bundle.main.url(forResource: "lm", withExtension: "bin"),
let vocab = Bundle.main.url(forResource: "lm-vocab", withExtension: "txt") else {
modelMissing = true
return
}
loading = true
DispatchQueue.global(qos: .utility).async {
let start = Date()
let model = try? GPT2LanguageModel(modelURL: bin, vocabURL: vocab)
NSLog("AutoZhuyin: language model %@ in %.0f ms", model == nil ? "failed" : "loaded",
Date().timeIntervalSince(start) * 1000)
DispatchQueue.main.async {
loading = false
if model == nil { modelMissing = true }
if reranker.mode != .off { reranker.model = model }
}
}
}
static var statusText: String {
if modelMissing { return "AI 模型未安裝" }
switch reranker.status {
case .off: return "AI 選字已關閉"
case .loading: return "AI 模型載入中…"
case .active:
return SystemMonitor.shared.state() == .reduced ? "AI 使用中(省電:低耗電模式)" : "AI 使用中"
case .pausedBusy: return "AI 已暫停:系統忙碌或過熱"
case .pausedSlow: return "AI 已暫停:運算太慢,一分鐘後再試"
}
}
}
/// Decides when AI should step back: paused when the Mac is hot or the CPU is busy,
/// reduced (fewest candidates) in Low Power Mode. Checked at most every 2 seconds.
final class SystemMonitor {
static let shared = SystemMonitor()
private var cached: (time: Date, state: Reranker.SystemState) = (.distantPast, .normal)
/// CPU load (1-minute average per core) above which AI pauses.
var busyLoad = 0.9
func invalidate() { cached.time = .distantPast }
func state() -> Reranker.SystemState {
if Date().timeIntervalSince(cached.time) < 2 { return cached.state }
cached = (Date(), compute())
return cached.state
}
private func compute() -> Reranker.SystemState {
guard AI.autoPause else { return .normal }
let info = ProcessInfo.processInfo
if info.thermalState == .serious || info.thermalState == .critical { return .paused }
var load = [Double](repeating: 0, count: 3)
if getloadavg(&load, 3) > 0, load[0] / Double(max(1, info.activeProcessorCount)) > busyLoad {
return .paused
}
if info.isLowPowerModeEnabled { return .reduced }
return .normal
}
}
+26 -1
View File
@@ -50,7 +50,11 @@ enum Shared {
@objc(AutoZhuyinInputController)
final class InputController: IMKInputController {
private let engine = CompositionEngine(classifier: Shared.classifier)
private lazy var engine: CompositionEngine = {
let e = CompositionEngine(classifier: Shared.classifier)
e.reranker = AI.reranker
return e
}()
private var shiftTap = ShiftTapDetector()
// MARK: - Key events
@@ -182,10 +186,31 @@ final class InputController: IMKInputController {
override func menu() -> NSMenu! {
let menu = NSMenu()
let modes: [(Reranker.Mode, String, Selector)] = [
(.off, "AI 選字:關閉", #selector(aiOff(_:))),
(.saver, "AI 選字:省電", #selector(aiSaver(_:))),
(.standard, "AI 選字:標準", #selector(aiStandard(_:))),
(.quality, "AI 選字:高品質", #selector(aiQuality(_:))),
]
for (mode, title, action) in modes {
let item = menu.addItem(withTitle: title, action: action, keyEquivalent: "")
item.state = AI.reranker.mode == mode ? .on : .off
}
let pause = menu.addItem(withTitle: "系統忙碌時自動暫停 AI", action: #selector(toggleAutoPause(_:)), keyEquivalent: "")
pause.state = AI.autoPause ? .on : .off
let status = menu.addItem(withTitle: AI.statusText, action: nil, keyEquivalent: "")
status.isEnabled = false
menu.addItem(.separator())
menu.addItem(withTitle: "清除學習紀錄", action: #selector(clearLearned(_:)), keyEquivalent: "")
return menu
}
@objc private func aiOff(_ sender: Any?) { AI.setMode(.off) }
@objc private func aiSaver(_ sender: Any?) { AI.setMode(.saver) }
@objc private func aiStandard(_ sender: Any?) { AI.setMode(.standard) }
@objc private func aiQuality(_ sender: Any?) { AI.setMode(.quality) }
@objc private func toggleAutoPause(_ sender: Any?) { AI.autoPause.toggle() }
@objc private func clearLearned(_ sender: Any?) {
Shared.classifier.learned.clear()
Shared.classifier.history.clear()
+1
View File
@@ -22,5 +22,6 @@ else {
Shared.server = server
Shared.candidates = IMKCandidates(server: server, panelType: kIMKSingleColumnScrollingCandidatePanel)
_ = Shared.classifier // load the lexicon now rather than on the first keystroke
AI.loadModelIfNeeded() // "AI 選字" language model, in the background
NSApplication.shared.run()
@@ -63,6 +63,11 @@ public final class CompositionEngine {
public private(set) var pendingBacktick = false
static let holdMinScore = -3.5
/// "AI 選字" (language-model reranking); nil = lexicon only.
public var reranker: Reranker?
/// The last characters committed, used as context for the reranker.
private var recentText = ""
private var previousMode: InputMode?
private var previousCommitTime = Date.distantPast
/// How long the previous segment's mode keeps influencing the next one.
@@ -186,9 +191,16 @@ public final class CompositionEngine {
private func conversion(of seg: Segment, final: Bool) -> (segments: [ConvertedSegment], pending: String) {
let (syl, pending) = convertibleSyllables(seg, final: final)
let fixedHere = (fixed[seg.range.lowerBound] ?? [:]).filter { $0.key.upperBound <= syl.count }
if let reranker, let chosen = reranker.choose(converter, syl, fixed: fixedHere, context: rerankContext) {
return (chosen, pending)
}
return (converter.convert(syl, fixed: fixedHere), pending)
}
private var rerankContext: String {
now().timeIntervalSince(previousCommitTime) < contextWindow ? recentText : ""
}
// MARK: - Cursor
/// Absolute key index after each syllable of a Zhuyin segment.
@@ -542,6 +554,7 @@ public final class CompositionEngine {
let segs = given ?? segments
let text = segs.map { render($0, final: true) }.joined()
rememberPicks(segs)
recentText = String((rerankContext + text).suffix(20))
if let forced = forcedMode {
classifier.learned.record(String(keys), as: forced)
}
+26
View File
@@ -61,6 +61,32 @@ public struct Converter {
return segments.reversed()
}
/// The `k` best paths (log10 score, segments), best first — for language-model reranking.
public func kBest(_ syllables: [Syllable], fixed: [Range<Int>: String] = [:], k: Int) -> [(score: Double, segments: [ConvertedSegment])] {
let n = syllables.count
guard n > 0, k > 0 else { return [] }
let maxLen = max(1, lexicon.maxPhraseLength)
typealias Path = (score: Double, segments: [ConvertedSegment])
var beams: [[Path]] = Array(repeating: [], count: n + 1)
beams[0] = [(0, [])]
for j in 1...n {
var cand: [Path] = []
for len in 1...min(maxLen, j) {
let i = j - len
guard !beams[i].isEmpty else { continue }
let range = i..<j
for (text, score) in options(for: range, syllables: syllables, fixed: fixed) {
for path in beams[i] {
cand.append((path.score + score, path.segments + [ConvertedSegment(range: range, text: text)]))
}
}
}
cand.sort { $0.score > $1.score }
beams[j] = Array(cand.prefix(k))
}
return beams[n]
}
private func options(for range: Range<Int>, syllables: [Syllable], fixed: [Range<Int>: String]) -> [(String, Double)] {
if let text = fixed[range] { return [(text, 0)] }
// A span may not partially overlap or swallow a fixed span.
+242
View File
@@ -0,0 +1,242 @@
import Accelerate
import Foundation
/// Scores Chinese text: natural-log probability of `text` following `context`.
public protocol LanguageModel: AnyObject {
func logProb(_ text: String, context: String) -> Double
}
/// GPT-2 forward pass (CPU, Accelerate) for the character-level CKIP gpt2-tiny-chinese
/// model (GPL-3.0), stored by training/lm/export_lm.py as float16 ("AZLM" format).
/// Weights are expanded to float32 in memory (~46 MB for gpt2-tiny).
public final class GPT2LanguageModel: LanguageModel {
public enum LoadError: Error { case badFormat(String) }
let nLayer: Int, nHead: Int, d: Int, vocabSize: Int
let eps: Float
private var weights: [Float] = []
private var offsets: [String: Int] = [:]
private var vocab: [Character: Int32] = [:]
static let cls: Int32 = 101, unk: Int32 = 100
/// Longest input scored (context + text); longer contexts are cut from the left.
public var maxTokens = 64
public init(modelURL: URL, vocabURL: URL) throws {
let data = try Data(contentsOf: modelURL, options: .mappedIfSafe)
guard data.count > 8, data.prefix(4) == Data("AZLM".utf8) else { throw LoadError.badFormat("magic") }
let headerLength = Int(data[4]) | Int(data[5]) << 8 | Int(data[6]) << 16 | Int(data[7]) << 24
guard let header = try JSONSerialization.jsonObject(with: data.subdata(in: 8..<(8 + headerLength))) as? [String: Any],
let nLayer = header["n_layer"] as? Int, let nHead = header["n_head"] as? Int,
let d = header["n_embd"] as? Int, let vocabSize = header["vocab"] as? Int,
let eps = header["eps"] as? Double, let tensors = header["tensors"] as? [[Any]]
else { throw LoadError.badFormat("header") }
self.nLayer = nLayer; self.nHead = nHead; self.d = d; self.vocabSize = vocabSize; self.eps = Float(eps)
for t in tensors {
guard let name = t[0] as? String, let offset = t[2] as? Int else { throw LoadError.badFormat("tensor") }
offsets[name] = offset
}
let start = 8 + headerLength
let count = (data.count - start) / 2
weights = [Float](repeating: 0, count: count)
try data.withUnsafeBytes { (raw: UnsafeRawBufferPointer) in
guard let base = raw.baseAddress?.advanced(by: start) else { throw LoadError.badFormat("data") }
try weights.withUnsafeMutableBufferPointer { out in
var src = vImage_Buffer(data: UnsafeMutableRawPointer(mutating: base), height: 1,
width: vImagePixelCount(count), rowBytes: count * 2)
var dst = vImage_Buffer(data: out.baseAddress, height: 1,
width: vImagePixelCount(count), rowBytes: count * 4)
guard vImageConvert_Planar16FtoPlanarF(&src, &dst, 0) == kvImageNoError else {
throw LoadError.badFormat("float16")
}
}
}
var index: Int32 = 0
for line in try String(contentsOf: vocabURL, encoding: .utf8).split(separator: "\n", omittingEmptySubsequences: false) {
if line.count == 1, let c = line.first, vocab[c] == nil { vocab[c] = index }
index += 1
}
for name in ["wte.weight", "wpe.weight", "ln_f.weight"] where offsets[name] == nil {
throw LoadError.badFormat("missing \(name)")
}
}
func tokens(_ text: String) -> [Int32] { text.map { vocab[$0] ?? GPT2LanguageModel.unk } }
public func logProb(_ text: String, context: String) -> Double {
let target = tokens(text)
guard !target.isEmpty else { return 0 }
var ctx = tokens(context)
if ctx.count + target.count + 1 > maxTokens { ctx = Array(ctx.suffix(max(0, maxTokens - target.count - 1))) }
let input = [GPT2LanguageModel.cls] + ctx + target.dropLast()
let h = hidden(input) // [T, d]
return weights.withUnsafeBufferPointer { w in
logSoftmaxScore(h, rows: input.count, from: ctx.count, targets: target, w: w)
}
}
// MARK: - Forward pass
private func w(_ name: String) -> Int { offsets[name]! }
/// Final hidden states [T, d] (row-major).
func hidden(_ ids: [Int32]) -> [Float] {
let T = ids.count, d = self.d
var x = [Float](repeating: 0, count: T * d)
weights.withUnsafeBufferPointer { W in
let wte = w("wte.weight"), wpe = w("wpe.weight")
for t in 0..<T {
vDSP_vadd(W.baseAddress! + wte + Int(ids[t]) * d, 1, W.baseAddress! + wpe + t * d, 1,
&x[t * d], 1, vDSP_Length(d))
}
for l in 0..<nLayer {
let p = "h.\(l)."
var h = layerNorm(x, rows: T, p + "ln_1", W)
let qkv = linear(h, rows: T, inDim: d, outDim: 3 * d, p + "attn.c_attn", W)
let a = attention(qkv, T: T)
var proj = linear(a, rows: T, inDim: d, outDim: d, p + "attn.c_proj", W)
GPT2LanguageModel.add(&x, proj)
h = layerNorm(x, rows: T, p + "ln_2", W)
var f = linear(h, rows: T, inDim: d, outDim: 4 * d, p + "mlp.c_fc", W)
geluNew(&f)
proj = linear(f, rows: T, inDim: 4 * d, outDim: d, p + "mlp.c_proj", W)
GPT2LanguageModel.add(&x, proj)
}
x = layerNorm(x, rows: T, "ln_f", W)
}
return x
}
private func layerNorm(_ x: [Float], rows: Int, _ name: String, _ W: UnsafeBufferPointer<Float>) -> [Float] {
let d = self.d
var out = [Float](repeating: 0, count: x.count)
let g = W.baseAddress! + w(name + ".weight"), b = W.baseAddress! + w(name + ".bias")
for r in 0..<rows {
var mean: Float = 0, sd: Float = 0
x.withUnsafeBufferPointer { xp in
out.withUnsafeMutableBufferPointer { op in
let src = xp.baseAddress! + r * d, dst = op.baseAddress! + r * d
vDSP_normalize(src, 1, dst, 1, &mean, &sd, vDSP_Length(d))
// vDSP_normalize divides by the population std without eps; redo with eps.
let scale = sd / (sd * sd + eps).squareRoot()
var s = scale
vDSP_vsmul(dst, 1, &s, dst, 1, vDSP_Length(d))
vDSP_vma(dst, 1, g, 1, b, 1, dst, 1, vDSP_Length(d))
}
}
}
return out
}
/// x [rows, inDim] · W [inDim, outDim] + bias.
private func linear(_ x: [Float], rows: Int, inDim: Int, outDim: Int, _ name: String,
_ W: UnsafeBufferPointer<Float>) -> [Float] {
var out = [Float](repeating: 0, count: rows * outDim)
vDSP_mmul(x, 1, W.baseAddress! + w(name + ".weight"), 1, &out, 1,
vDSP_Length(rows), vDSP_Length(outDim), vDSP_Length(inDim))
let bias = W.baseAddress! + w(name + ".bias")
out.withUnsafeMutableBufferPointer { op in
for r in 0..<rows {
vDSP_vadd(op.baseAddress! + r * outDim, 1, bias, 1, op.baseAddress! + r * outDim, 1, vDSP_Length(outDim))
}
}
return out
}
/// Causal multi-head self-attention. qkv is [T, 3d] (q | k | v).
private func attention(_ qkv: [Float], T: Int) -> [Float] {
let d = self.d, hd = d / nHead
let scale = 1 / Float(hd).squareRoot()
var out = [Float](repeating: 0, count: T * d)
var scores = [Float](repeating: 0, count: T)
qkv.withUnsafeBufferPointer { q in
out.withUnsafeMutableBufferPointer { o in
for head in 0..<nHead {
for i in 0..<T {
let qi = q.baseAddress! + i * 3 * d + head * hd
var maxScore = -Float.infinity
for j in 0...i {
var s: Float = 0
vDSP_dotpr(qi, 1, q.baseAddress! + j * 3 * d + d + head * hd, 1, &s, vDSP_Length(hd))
scores[j] = s * scale
maxScore = max(maxScore, scores[j])
}
var sum: Float = 0
for j in 0...i { scores[j] = expf(scores[j] - maxScore); sum += scores[j] }
let dst = o.baseAddress! + i * d + head * hd
for j in 0...i {
var a = scores[j] / sum
vDSP_vsma(q.baseAddress! + j * 3 * d + 2 * d + head * hd, 1, &a, dst, 1, dst, 1, vDSP_Length(hd))
}
}
}
}
}
return out
}
/// x += y (same length).
private static func add(_ x: inout [Float], _ y: [Float]) {
x.withUnsafeMutableBufferPointer { xp in
vDSP_vadd(xp.baseAddress!, 1, y, 1, xp.baseAddress!, 1, vDSP_Length(xp.count))
}
}
/// gelu_new: 0.5·x·(1 + tanh(√(2/π)·(x + 0.044715·x³))).
private func geluNew(_ x: inout [Float]) {
let n = x.count
var inner = [Float](repeating: 0, count: n)
x.withUnsafeMutableBufferPointer { xb in
inner.withUnsafeMutableBufferPointer { ib in
let xp = xb.baseAddress!, ip = ib.baseAddress!, len = vDSP_Length(n)
var c3: Float = 0.044715, one: Float = 1, k = (2 / Float.pi).squareRoot(), half: Float = 0.5
vDSP_vsq(xp, 1, ip, 1, len) // x²
vDSP_vsmsa(ip, 1, &c3, &one, ip, 1, len) // 1 + 0.044715·x²
vDSP_vmul(ip, 1, xp, 1, ip, 1, len) // x + 0.044715·x³
vDSP_vsmul(ip, 1, &k, ip, 1, len)
var count = Int32(n)
vvtanhf(ip, ip, &count)
vDSP_vsadd(ip, 1, &one, ip, 1, len) // 1 + tanh(…)
vDSP_vmul(ip, 1, xp, 1, xp, 1, len)
vDSP_vsmul(xp, 1, &half, xp, 1, len)
}
}
}
/// Sum over target positions of log softmax(h · wteᵀ)[target]. Row `from + i` predicts target i.
private func logSoftmaxScore(_ h: [Float], rows T: Int, from start: Int, targets: [Int32],
w W: UnsafeBufferPointer<Float>) -> Double {
let d = self.d, V = vocabSize
let rowsUsed = T - start
// hᵀ for the rows we need: [d, rowsUsed]
var hT = [Float](repeating: 0, count: d * rowsUsed)
h.withUnsafeBufferPointer { hp in
vDSP_mtrans(hp.baseAddress! + start * d, 1, &hT, 1, vDSP_Length(d), vDSP_Length(rowsUsed))
}
// logitsᵀ [V, rowsUsed] = wte [V, d] · hᵀ [d, rowsUsed]
var logitsT = [Float](repeating: 0, count: V * rowsUsed)
vDSP_mmul(W.baseAddress! + w("wte.weight"), 1, hT, 1, &logitsT, 1,
vDSP_Length(V), vDSP_Length(rowsUsed), vDSP_Length(d))
var logits = [Float](repeating: 0, count: V * rowsUsed) // [rowsUsed, V]
vDSP_mtrans(logitsT, 1, &logits, 1, vDSP_Length(rowsUsed), vDSP_Length(V))
var total = 0.0
var expBuf = [Float](repeating: 0, count: V)
logits.withUnsafeMutableBufferPointer { lp in
expBuf.withUnsafeMutableBufferPointer { eb in
let e = eb.baseAddress!
for i in 0..<rowsUsed {
let row = lp.baseAddress! + i * V
var m: Float = 0
vDSP_maxv(row, 1, &m, vDSP_Length(V))
var negM = -m
vDSP_vsadd(row, 1, &negM, e, 1, vDSP_Length(V))
var n = Int32(V)
vvexpf(e, e, &n)
var sum: Float = 0
vDSP_sve(e, 1, &sum, vDSP_Length(V))
total += Double(row[Int(targets[i])] - m) - Double(logf(sum))
}
}
}
return total
}
}
+90
View File
@@ -0,0 +1,90 @@
import Foundation
/// "AI 選字": reranks the converter's best paths with a language model.
///
/// score = lexicon score (log10 → ln) + λ · LM log-probability
///
/// It never blocks typing for long: each rerank has a time budget, and a rerank that runs
/// over it is dropped (the plain lexicon result is used). Three overruns in a row pause
/// AI for a minute. `systemState` lets the app pause or reduce it when the machine is
/// busy, hot or saving power.
public final class Reranker {
public enum Mode: String, CaseIterable {
case off, saver, standard, quality
/// How many paths are reranked, and the time budget for one rerank.
var k: Int { [.saver: 3, .standard: 10, .quality: 30][self] ?? 0 }
var budget: TimeInterval { [.saver: 0.015, .standard: 0.04, .quality: 0.12][self] ?? 0 }
}
/// What the system allows right now (decided by the app).
public enum SystemState { case normal, reduced, paused }
public enum Status: Equatable {
case off, loading, active, pausedBusy, pausedSlow
}
public var mode: Mode { didSet { cache.removeAll() } }
/// nil while loading (or if the model file is missing).
public var model: LanguageModel? { didSet { cache.removeAll() } }
public var lambda = 0.6
public var systemState: () -> SystemState = { .normal }
public var now: () -> Date = Date.init
public private(set) var status: Status = .off
private var overruns = 0
private var pausedUntil = Date.distantPast
private var cache: [String: [ConvertedSegment]] = [:]
static let cacheLimit = 512
static let pauseAfterOverruns = 3
static let slowPause: TimeInterval = 60
public init(mode: Mode = .standard, model: LanguageModel? = nil) {
self.mode = mode
self.model = model
}
/// The mode actually used now (after the system state), or nil when AI is not used.
var effectiveMode: Mode? {
guard mode != .off else { status = .off; return nil }
guard model != nil else { status = .loading; return nil }
if now() < pausedUntil { status = .pausedSlow; return nil }
switch systemState() {
case .paused: status = .pausedBusy; return nil
case .reduced: status = .active; return .saver
case .normal: status = .active; return mode
}
}
/// The reranked conversion, or nil to use the plain lexicon result.
func choose(_ converter: Converter, _ syllables: [Syllable], fixed: [Range<Int>: String],
context: String) -> [ConvertedSegment]? {
guard let mode = effectiveMode, let model, !syllables.isEmpty else { return nil }
let key = "\(mode.rawValue)|\(context)|\(syllables.map(\.reading).joined(separator: "-"))|"
+ fixed.sorted { $0.key.lowerBound < $1.key.lowerBound }.map { "\($0.key)=\($0.value)" }.joined(separator: ",")
if let hit = cache[key] { return hit }
let paths = converter.kBest(syllables, fixed: fixed, k: mode.k)
guard paths.count > 1 else { return paths.first?.segments }
let start = now()
var best: (score: Double, segments: [ConvertedSegment])?
for path in paths {
let text = path.segments.map(\.text).joined()
let score = path.score * log(10) + lambda * model.logProb(text, context: context)
if best == nil || score > best!.score { best = (score, path.segments) }
if now().timeIntervalSince(start) > mode.budget {
overruns += 1
if overruns >= Reranker.pauseAfterOverruns {
pausedUntil = now().addingTimeInterval(Reranker.slowPause)
overruns = 0
status = .pausedSlow
}
return nil
}
}
overruns = 0
if cache.count >= Reranker.cacheLimit { cache.removeAll() }
cache[key] = best?.segments
return best?.segments
}
}