Gemma 4 26B-A4B inference in ~2 GB of RAM on any M-series MacBook
Swift apple-silicongemmagemma4gemma4-26b-a4bgpgpullmllm-inferencelocal-aimacosmetalon-device-aion-device-llmswift
import Metal
/// Produces next-token logits for the `Generator`. The production
/// implementation is `RealForwardRunner`; tests use scripted logits so decode
/// behavior stays independent of the kernel stack.
public protocol LogitProducer: AnyObject, Sendable {
/// Clear any per-generation state, such as KV cache.
func reset()
/// Run one token at `position`, leaving FP16 logits in `logits`.
func produce(token: Int32, position: Int, into logits: MTLBuffer) async throws
}
public protocol ContinuableLogitProducer: LogitProducer {
var continuationPosition: Int { get }
func prepareForContinuation(expectedPosition: Int) throws
}
protocol ContextWindowReporting: Sendable {
var maxContext: Int { get }
}
public enum PrefillOutputMode: Sendable, Equatable {
case logits
case greedyIfAvailable
}
public enum PrefillSeed: Sendable, Equatable {
case logitsWritten
case greedyToken(UInt32)
}
public struct PrefillResult: Sendable, Equatable {
public let newPosition: Int
public let seed: PrefillSeed
public init(newPosition: Int, seed: PrefillSeed) {
self.newPosition = newPosition
self.seed = seed
}
}
protocol ChunkedPrefillRunner: LogitProducer {
/// Prefill a prompt slice using the chunked production runtime.
func prefillChunked(tokens: ArraySlice<Int32>,
startPosition: Int,
outputMode: PrefillOutputMode,
config: PrefillRuntimeConfig,
into logits: MTLBuffer,
onProgress: (Int) -> Void) async throws -> PrefillResult
}
protocol MultimodalPrefillRunner: LogitProducer {
func prefillMultimodal(input: MultimodalPrefillInput,
startPosition: Int,
outputMode: PrefillOutputMode,
config: PrefillRuntimeConfig,
into logits: MTLBuffer,
onProgress: (Int) -> Void) async throws -> PrefillResult
}