Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
29c1255
feat(kvcache): spec 041 phase 1.2 — rotating affine cache + sliding-w…
ekryski May 13, 2026
b9a0f0a
fix(affine-sdpa): auto-strategy prefers .flash for sliding-window masks
ekryski May 13, 2026
9d6a2d8
feat(turbo-kv): route sinks models through compressed β kernel (spec …
ekryski May 13, 2026
cf21379
test(turbo-kv): regression test for β + sinks contiguous-K/V bug
ekryski May 13, 2026
bc1ca95
fix(turbo-kv): route sinks-using models through dequant-SDPA-with-sin…
ekryski May 13, 2026
34b5de2
feat(turbo-kv): DC-bias correction + hybrid sliding-FP16 policy for G…
ekryski May 14, 2026
ed7cb66
feat(turbo-kv): default `useBias: true` on GPT-OSS-20B turbo* caches
ekryski May 14, 2026
88e2272
docs(plan + code): GPT-OSS sinks shipped + comment audit
ekryski May 14, 2026
af166f5
bench(2026-05-13): A-path + B-path archives across 13 models × 6 KV c…
ekryski May 14, 2026
335d5f3
feat(turbo-kv): TurboFlash is now the unconditional A path; dequant S…
ekryski May 14, 2026
4a0ce3a
bench(2026-05-13): clean A-path + B-path archives across full 156-cel…
ekryski May 14, 2026
5826d69
docs(kv-cache-types): drop stale "needs rotating-buffer-aware affine …
ekryski May 14, 2026
56715c8
refactor(kv-cache): replace `maxSize` + `architecturalSlidingWindow` …
ekryski May 14, 2026
c3fd511
feat(kv-cache): honor user maxKVSize on .affine — rotating affine cac…
ekryski May 14, 2026
8255557
docs(kv-cache): refresh stale claims to reflect specs 020/040/041 shi…
ekryski May 14, 2026
c28a9a3
docs(kv-cache): fix stale "TurboQuant not trimmable" claim
ekryski May 14, 2026
bc68b53
refactor(kv-cache): thread prefillStep uniformly across all three cac…
ekryski May 15, 2026
12f5595
udpdating kv cache documentation layout
ekryski May 15, 2026
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
6 changes: 3 additions & 3 deletions Libraries/MLXLLM/Models/AfMoE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,12 @@ public class AfMoEModel: Module, LLMModel, KVCacheDimensionProvider {
// Create cache based on layer type (sliding-window or unbounded). When
// `compressionAlgorithm == .affine(...)`, every attention layer is
// constructed directly as an AffineQuantizedKVCache (no runtime swap).
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return layerUsesSliding.map { usesSliding in
makeAttentionCache(
parameters: parameters,
maxSize: usesSliding ? slidingWindow : nil,
affineStep: affineStep)
slidingWindow: usesSliding ? slidingWindow : nil,
prefillStep: prefillStep)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions Libraries/MLXLLM/Models/BaichuanM1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,14 @@ public class BaichuanM1Model: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.enumerated().map { (i, _) in
let isSWA = configuration.slidingWindowLayers.contains(i)
let convCache = SSMStateCache()
let kvCache = makeAttentionCache(
parameters: parameters,
maxSize: isSWA ? configuration.slidingWindow : nil,
affineStep: affineStep,
architecturalSlidingWindow: isSWA)
slidingWindow: isSWA ? configuration.slidingWindow : nil,
prefillStep: prefillStep)
return CacheList(convCache, kvCache)
}
}
Expand Down
11 changes: 6 additions & 5 deletions Libraries/MLXLLM/Models/Exaone4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,14 @@ public class Exaone4Model: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters? = nil) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { layer in
let isSliding = layer.attention.isLocal
let maxSize: Int? = isSliding ? configuration.slidingWindow : nil
let slidingWindow: Int? =
layer.attention.isLocal ? configuration.slidingWindow : nil
return makeAttentionCache(
parameters: parameters, maxSize: maxSize, affineStep: affineStep,
architecturalSlidingWindow: isSliding)
parameters: parameters,
slidingWindow: slidingWindow,
prefillStep: prefillStep)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/FalconH1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,11 @@ public class FalconH1Model: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [any KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { _ in
CacheList(
SSMStateCache(),
makeAttentionCache(parameters: parameters, affineStep: affineStep))
makeAttentionCache(parameters: parameters, prefillStep: prefillStep))
}
}
}
Expand Down
44 changes: 31 additions & 13 deletions Libraries/MLXLLM/Models/GPTOSS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,9 @@ public class GPTOSSModel: Module, LLMModel, KVCacheDimensionProvider {
private let configuration: GPTOSSConfiguration
@ModuleInfo(key: "lm_head") var lmHead: Linear

/// GPT-OSS uses attention sinks. The α-path runs sinks-bearing attention
/// without crashing (dequant V → standard SDPA fallback) but at
/// `valueBits=2` the output is silently incoherent — Harmony channel
/// markers (`<|channel|>`/`<|message|>`) never appear, so downstream
/// parsers see empty content. See issue #171. Coherent compressed-domain
/// support tracked in #130 (the path-B sinks β-path stack).
public var supportsTurboQuantization: Bool { false }
/// Coherent on `--kv turbo*` via the bias-correction + hybrid
/// sliding-FP16 policy in `newCache(...)` below. Closes #171 / #130.
public var supportsTurboQuantization: Bool { true }

public init(_ config: GPTOSSConfiguration) {
self.configuration = config
Expand Down Expand Up @@ -527,22 +523,44 @@ public class GPTOSSModel: Module, LLMModel, KVCacheDimensionProvider {

public func newCache(parameters: GenerateParameters?) -> [any KVCache] {
var caches: [KVCache] = []
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
// GPT-OSS turbo* policy (spec 041 phase 1.1 follow-up):
// - Full-attention layers: TurboQuant with DC-bias correction
// (`useBias: true`) — model's K/V projections have `bias=True`,
// so the rotated vectors carry a structured DC offset the
// zero-mean Lloyd-Max codebook can't represent.
// - Sliding-window layers (128-token cap): raw FP16 — the codec's
// per-token error compounds in the sinks softmax; the FP16
// fallback adds ~1.5 MB across all sliding layers vs the
// full-attention layers' 8K-token compressed caches.
//
// `--kv affine*` and `--kv none` keep both layer types on their
// default cache (no silent algorithm substitution).
let isTurboScheme: Bool
if case .turbo = parameters?.compressionAlgorithm {
isTurboScheme = true
} else {
isTurboScheme = false
}
for lt in model.layerTypes {
if lt == "full_attention" {
// keep: 4 preserves attention-sink tokens for full-attention layers.
// No `slidingWindow` — function reads the user's
// `parameters?.maxKVSize` internally as the budget cap.
caches.append(makeAttentionCache(
parameters: parameters,
maxSize: parameters?.maxKVSize,
keep: 4,
affineStep: affineStep))
prefillStep: prefillStep,
useBias: isTurboScheme))
} else if isTurboScheme {
caches.append(StandardKVCache(
maxSize: configuration.slidingWindow, keep: 0))
} else {
caches.append(makeAttentionCache(
parameters: parameters,
maxSize: configuration.slidingWindow,
slidingWindow: configuration.slidingWindow,
keep: 0,
affineStep: affineStep,
architecturalSlidingWindow: true))
prefillStep: prefillStep))
}
}
return caches
Expand Down
9 changes: 4 additions & 5 deletions Libraries/MLXLLM/Models/Gemma3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,17 @@ public class Gemma3TextModel: Module, LLMModel {
var caches = [KVCache]()
let slidingWindow = config.slidingWindow
let slidingWindowPattern = config.slidingWindowPattern
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize

for i in 0 ..< config.hiddenLayers {
let isGlobalLayer = (i % slidingWindowPattern == slidingWindowPattern - 1)
let cache = makeAttentionCache(
parameters: parameters,
maxSize: isGlobalLayer ? nil : slidingWindow,
affineStep: affineStep,
architecturalSlidingWindow: !isGlobalLayer)
slidingWindow: isGlobalLayer ? nil : slidingWindow,
prefillStep: prefillStep)
// For global layers (unbounded StandardKVCache), bump the step
// size for long-sequence efficiency. Affine-quantized caches
// pick up `affineStep` via the constructor above.
// pick up `prefillStep` via the constructor above.
if isGlobalLayer, let standard = cache as? StandardKVCache {
standard.step = 1024
}
Expand Down
17 changes: 8 additions & 9 deletions Libraries/MLXLLM/Models/Gemma3nText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -734,31 +734,30 @@ public class Gemma3nLanguageModel: Module {
// conformer), so `defaultPrefillStepSize` isn't in scope. The
// outer `Gemma3nTextModel` uses the protocol default (1024) —
// hardcode the matching affine step here.
let affineStep = 1024
let prefillStep = 1024
// KV-sharing in Gemma 3n. Spec 041 phase 5 follow-up: shared
// reader layers in `Gemma3nAttention.callAsFunction` now route
// through `quantizedScaledDotProductAttention` when the donor is
// an `AffineQuantizedKVCache` — no dequant, no compression loss.
// `forceRawKV: false` keeps the affine compression on donor
// layers. (Sliding-window donors still fall back via
// `architecturalSlidingWindow: true` since affine has no rotating
// buffer; closing that gap needs spec 041 Phase 1.2 / 1.3 work.)
// layers. Sliding-window donors use the spec 041 phase 1.2
// rotating-window affine cache (selected by `slidingWindow != nil`).
_ = config.numKvSharedLayers > 0 // intentional: previously gated forceRawKV
for i in 0 ..< firstKvSharedLayerIdx {
let layerType = layerTypes[i]
switch layerType {
case "full_attention":
caches.append(
makeAttentionCache(
parameters: parameters, affineStep: affineStep,
parameters: parameters, prefillStep: prefillStep,
forceRawKV: false))
case "sliding_attention":
caches.append(
makeAttentionCache(
parameters: parameters, maxSize: slidingWindow,
affineStep: affineStep,
forceRawKV: false,
architecturalSlidingWindow: true))
parameters: parameters,
slidingWindow: slidingWindow,
prefillStep: prefillStep,
forceRawKV: false))
default:
fatalError("Unknown layer type: \(layerType) for layer \(i)")
}
Expand Down
21 changes: 7 additions & 14 deletions Libraries/MLXLLM/Models/Gemma4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ public class Gemma4TextModel: Module, LLMModel, KVCacheDimensionProvider {
public func newCache(parameters: GenerateParameters? = nil) -> [KVCache] {
var caches = [KVCache]()

let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize

// Compute the donor set first. Gemma 4 LLM (this file) routes shared
// reader layers through `Gemma4TextAttention`'s quantised donor path
Expand All @@ -1294,21 +1294,14 @@ public class Gemma4TextModel: Module, LLMModel, KVCacheDimensionProvider {
}

for (idx, layerType) in config.layerTypes.enumerated() {
let maxSize: Int?
let isSliding: Bool
if layerType == "full_attention" {
maxSize = parameters?.maxKVSize
isSliding = false
} else {
maxSize = config.slidingWindow
isSliding = true
}
let slidingWindow: Int? =
(layerType == "full_attention") ? nil : config.slidingWindow
caches.append(
makeAttentionCache(
parameters: parameters, maxSize: maxSize,
affineStep: affineStep,
forceRawKV: false, // spec 041 phase 5: reader path handles quantised donors.
architecturalSlidingWindow: isSliding))
parameters: parameters,
slidingWindow: slidingWindow,
prefillStep: prefillStep,
forceRawKV: false)) // spec 041 phase 5: reader path handles quantised donors.
_ = idx
}

Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/GraniteMoeHybrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ public class GraniteMoeHybridModel: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return configuration.layerTypes.map { layerType in
if layerType == "mamba" {
return SSMStateCache()
} else {
return makeAttentionCache(parameters: parameters, affineStep: affineStep)
return makeAttentionCache(parameters: parameters, prefillStep: prefillStep)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/Jamba.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ public class JambaModel: Module, LLMModel, KVCacheDimensionProvider {
@ModuleInfo(key: "lm_head") var lmHead: Linear?

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { layer in
if layer.isAttn {
return makeAttentionCache(parameters: parameters, affineStep: affineStep)
return makeAttentionCache(parameters: parameters, prefillStep: prefillStep)
} else {
// Jamba's Mamba mixer uses a per-step parallel scan whose
// Spec 040 shipped Mamba state-replay via
Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/LFM2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public class LFM2Model: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return (0 ..< configuration.hiddenLayers).map { layerIdx in
if configuration.fullAttnIdxs.contains(layerIdx) {
makeAttentionCache(parameters: parameters, affineStep: affineStep)
makeAttentionCache(parameters: parameters, prefillStep: prefillStep)
} else {
SSMStateCache()
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/LFM2MoE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ public class LFM2MoEModel: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return (0 ..< configuration.hiddenLayers).map { layerIdx in
if configuration.fullAttnIdxs.contains(layerIdx) {
makeAttentionCache(parameters: parameters, affineStep: affineStep)
makeAttentionCache(parameters: parameters, prefillStep: prefillStep)
} else {
SSMStateCache()
}
Expand Down
10 changes: 5 additions & 5 deletions Libraries/MLXLLM/Models/MiMoV2Flash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,14 @@ public class MiMoV2FlashModel: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { layer in
let isSliding = layer.isSlidingWindow
let slidingWindow: Int? =
layer.isSlidingWindow ? configuration.slidingWindowSize : nil
return makeAttentionCache(
parameters: parameters,
maxSize: isSliding ? configuration.slidingWindowSize : nil,
affineStep: affineStep,
architecturalSlidingWindow: isSliding)
slidingWindow: slidingWindow,
prefillStep: prefillStep)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Libraries/MLXLLM/Models/Mistral3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ public class Mistral3TextModel: Module, LLMModel, KVCacheDimensionProvider {
/// Per-layer cache: sliding-attention layers get a windowed
/// `StandardKVCache`; full-attention layers get unbounded.
public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { layer in
let isSliding = layer.useSliding
let maxSize: Int? = isSliding ? args.slidingWindow : nil
let slidingWindow: Int? = layer.useSliding ? args.slidingWindow : nil
return makeAttentionCache(
parameters: parameters, maxSize: maxSize, affineStep: affineStep,
architecturalSlidingWindow: isSliding)
parameters: parameters,
slidingWindow: slidingWindow,
prefillStep: prefillStep)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Libraries/MLXLLM/Models/NemotronH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,7 @@ public class NemotronHModel: Module, LLMModel, KVCacheDimensionProvider, LoRAMod
// the boundary-skip set — hand to the standard factory.
caches.append(makeAttentionCache(
parameters: parameters,
maxSize: parameters?.maxKVSize,
affineStep: defaultPrefillStepSize))
prefillStep: defaultPrefillStepSize))
}
case .mlp, .moe:
continue // No cache needed for MLP/MoE layers
Expand Down
11 changes: 6 additions & 5 deletions Libraries/MLXLLM/Models/Olmo3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,15 @@ public class Olmo3Model: Module, LLMModel, KVCacheDimensionProvider {

public func newCache(parameters: GenerateParameters) -> [KVCache] {
var caches: [KVCache] = []
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
for layerType in args.layerTypes {
let isSliding = (layerType != "full_attention")
let maxSize: Int? = isSliding ? args.slidingWindow : nil
let slidingWindow: Int? =
(layerType != "full_attention") ? args.slidingWindow : nil
caches.append(
makeAttentionCache(
parameters: parameters, maxSize: maxSize, affineStep: affineStep,
architecturalSlidingWindow: isSliding))
parameters: parameters,
slidingWindow: slidingWindow,
prefillStep: prefillStep))
}
return caches
}
Expand Down
6 changes: 4 additions & 2 deletions Libraries/MLXLLM/Models/Qwen35.swift
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,12 @@ public class Qwen35TextModel: Module, LLMModel, KVCacheDimensionProvider {
// that the user opted to keep uncompressed. Either way, hand
// off to the standard factory (raw `StandardKVCache` for .none,
// `AffineQuantizedKVCache` for .affine).
// No `slidingWindow` — Qwen 3.5 attention layers are
// full-attention; the user's `parameters?.maxKVSize` is read
// internally by `makeAttentionCache` as the budget cap.
return makeAttentionCache(
parameters: parameters,
maxSize: parameters?.maxKVSize,
affineStep: defaultPrefillStepSize)
prefillStep: defaultPrefillStepSize)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Libraries/MLXLLM/Models/Qwen3Next.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,12 @@ public class Qwen3NextModel: Module, LLMModel, KVCacheDimensionProvider {
}

public func newCache(parameters: GenerateParameters?) -> [KVCache] {
let affineStep = defaultPrefillStepSize
let prefillStep = defaultPrefillStepSize
return model.layers.map { layer in
if layer.isLinear {
return SSMStateCache()
}
return makeAttentionCache(parameters: parameters, affineStep: affineStep)
return makeAttentionCache(parameters: parameters, prefillStep: prefillStep)
}
}

Expand Down
Loading