Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatCore/Sources/AppleDevices.swift3// Description: Memory classes of Apple devices and the share of RAM a model may use on each, so models can be proposed per device (not only for the device in hand).4import Foundation56public struct AppleDeviceClass: Sendable, Hashable, Identifiable, Codable {7 public enum Family: String, Sendable, Codable, CaseIterable { case iPhone, iPad, mac8 public var title: String { switch self { case .iPhone: "iPhone"; case .iPad: "iPad"; case .mac: "Mac" } }9 public var symbol: String { switch self { case .iPhone: "iphone"; case .iPad: "ipad"; case .mac: "macbook" } }10 }11 public var family: Family12 public var memoryGB: Int13 public var examples: String // indicative, non-exhaustive; the class is defined by its memory14 public var id: String { "\(family.rawValue)-\(memoryGB)" }15 public var title: String { "\(family.title) · \(memoryGB) GB" }1617 public init(family: Family, memoryGB: Int, examples: String) { self.family = family; self.memoryGB = memoryGB; self.examples = examples }1819 public var physicalMemoryBytes: Int64 { Int64(memoryGB) * 1_073_741_824 }2021 /// Same rule as `DeviceProfile.usableModelMemoryBytes`: unified memory is shared with every app on a Mac; iOS ends an app well below physical RAM.22 public var usableModelMemoryBytes: Int64 {23 switch family {24 case .mac: Int64(Double(physicalMemoryBytes) * (memoryGB >= 32 ? 0.75 : 0.65))25 case .iPhone, .iPad: Int64(Double(physicalMemoryBytes) * 0.55)26 }27 }2829 public func fit(estimatedMemoryBytes need: Int64?) -> ModelFit {30 guard let need else { return .unknown }31 let usable = usableModelMemoryBytes32 if need > usable { return .tooLarge }33 return Double(need) > Double(usable) * 0.7 ? .tight : .comfortable34 }3536 public static let all: [AppleDeviceClass] = [37 .init(family: .iPhone, memoryGB: 6, examples: "iPhone 14 Pro, iPhone 15"),38 .init(family: .iPhone, memoryGB: 8, examples: "iPhone 15 Pro, iPhone 16, iPhone 17"),39 .init(family: .iPhone, memoryGB: 12, examples: "iPhone 17 Pro, iPhone 17 Pro Max, iPhone Air"),40 .init(family: .iPad, memoryGB: 8, examples: "iPad Air (M2, M3), iPad mini (A17 Pro), iPad Pro M4 256 / 512 GB"),41 .init(family: .iPad, memoryGB: 12, examples: "iPad Pro M5 256 / 512 GB"),42 .init(family: .iPad, memoryGB: 16, examples: "iPad Pro M4 / M5 1 TB and 2 TB"),43 .init(family: .mac, memoryGB: 8, examples: "MacBook Air M1 – M3 base"),44 .init(family: .mac, memoryGB: 16, examples: "MacBook Air, Mac mini, MacBook Pro base"),45 .init(family: .mac, memoryGB: 24, examples: "MacBook Air / Pro, Mac mini"),46 .init(family: .mac, memoryGB: 32, examples: "MacBook Pro, Mac Studio"),47 .init(family: .mac, memoryGB: 48, examples: "MacBook Pro M4 / M5 Pro and Max"),48 .init(family: .mac, memoryGB: 64, examples: "MacBook Pro Max, Mac Studio"),49 .init(family: .mac, memoryGB: 96, examples: "Mac Studio M3 Ultra"),50 .init(family: .mac, memoryGB: 128, examples: "MacBook Pro M4 / M5 Max, Mac Studio"),51 .init(family: .mac, memoryGB: 192, examples: "Mac Studio M2 Ultra"),52 ]5354 /// The class closest to a measured device (same family, nearest memory not above the real one).55 public static func matching(_ device: DeviceProfile) -> AppleDeviceClass {56 let family: Family = switch device.platform { case .mac: .mac; case .iPad: .iPad; case .iPhone, .simulator: .iPhone }57 let gb = Int((device.memoryGB).rounded())58 let candidates = all.filter { $0.family == family }59 return candidates.last { $0.memoryGB <= gb } ?? candidates.first ?? all[0]60 }61}62