Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatInference/Sources/MLXSupport.swift3// Description: Facts read from the MLX libraries at run time: allocator memory and the architectures this build can actually load.4import Foundation5import LocalChatCore6import MLX7import MLXEmbedders8import MLXLLM9import MLXLMCommon10import MLXVLM1112enum MLXMemoryProbe {13 static func read() -> (active: Int64, cache: Int64, peak: Int64)? {14 #if targetEnvironment(simulator)15 return nil16 #else17 let s = MLX.Memory.snapshot()18 return (Int64(s.activeMemory), Int64(s.cacheMemory), Int64(s.peakMemory))19 #endif20 }21 static func clearCache() {22 #if !targetEnvironment(simulator)23 MLX.Memory.clearCache()24 #endif25 }26}2728public enum MLXSupport {29 /// Limits the MLX buffer cache: a large cache speeds up generation on a Mac and gets an iPhone app killed.30 public static func configureMemory(for device: DeviceProfile) {31 #if !targetEnvironment(simulator)32 switch device.platform {33 case .mac: MLX.Memory.cacheLimit = Int(min(Int64(2) << 30, device.physicalMemoryBytes / 16))34 default: MLX.Memory.cacheLimit = 48 << 2035 }36 #endif37 }3839 public static func memorySnapshot() -> (active: Int64, cache: Int64, peak: Int64)? { MLXMemoryProbe.read() }40}41