Skip to content
Merged
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
24 changes: 23 additions & 1 deletion Libraries/MLXLMCommon/AttentionUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,29 @@ public func attentionWithCacheUpdate(
mask: mask
)
}
if let quantizedKVCache = cache as? QuantizedKVCacheProtocol {
if let turboCache = cache as? TurboQuantKVCache {
// TurboQuant path: two modes depending on useCompressedAttention flag
if turboCache.useCompressedAttention {
// Compressed-domain Metal kernels — no FP16 materialization
return turboCache.compressedAttention(
queries: queries, keys: keys, values: values,
scale: scale, mask: mask
)
} else {
// Dequant-first path: rotated FP16 cache + pre-rotated queries + MLX SDPA
let (cachedKeys, cachedValues) = turboCache.updateAndDequant(
keys: keys, values: values)
let rotatedQueries = turboCache.prepareQueries(queries)
let rotatedOutput = MLXFast.scaledDotProductAttention(
queries: rotatedQueries,
keys: cachedKeys,
values: cachedValues,
scale: scale,
mask: mask
)
return turboCache.inverseRotateOutput(rotatedOutput)
}
} else if let quantizedKVCache = cache as? QuantizedKVCacheProtocol {
let (quantizedKeys, quantizedValues) = quantizedKVCache.updateQuantized(
keys: keys, values: values)
return quantizedScaledDotProductAttention(
Expand Down
25 changes: 25 additions & 0 deletions Libraries/MLXLMCommon/KVCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,31 @@ public class KVCacheSimple: BaseKVCache, CustomDebugStringConvertible {
return trimmed
}

/// Convert to TurboQuant compressed cache.
///
/// Uses Algorithm 1 (WHT + Lloyd-Max codebook) for KV cache compression.
/// Supports asymmetric bit-widths: `keyBits` for keys, `valueBits` for values.
/// Set `keyBits: 0` for raw-K mode (FP16 keys, compressed values only).
///
/// - Parameters:
/// - bits: Default bit-width for both K and V (overridden by keyBits/valueBits)
/// - keyBits: Bit-width for key compression (0 = raw FP16, nil = use `bits`)
/// - valueBits: Bit-width for value compression (nil = use `bits`)
/// - Returns: A `TurboQuantKVCache` initialized with the current raw K/V data
public func toTurboQuantized(bits: Int = 4, keyBits: Int? = nil, valueBits: Int? = nil) -> TurboQuantKVCache {
let turboCache = TurboQuantKVCache(bits: bits, keyBits: keyBits, valueBits: valueBits)

if let keys = self.keys, let values = self.values, offset > 0 {
// Transfer raw K/V state — TurboQuantKVCache will compress on first decode
turboCache.state = [
keys[.ellipsis, ..<offset, 0...],
values[.ellipsis, ..<offset, 0...],
]
}

return turboCache
}

/// Convert to quantized cache for maximum efficiency
///
/// Use `updateQuantized()` and `quantizedScaledDotProductAttention()` for zero-overhead operation.
Expand Down
Loading