SPB Git

spb/forge-studio Public

The Instruments of LLM training — a native macOS cockpit for Forge. Train language models from scratch on Apple Silicon without a terminal.

Swift 95.7% Shell 4.3%
1.5 KB · 38 lines swift
Raw Blame History
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2//3// Chip name, core count and unified memory via sysctl — feeds the memory4// estimate badge in the New Run form.5import Foundation67enum SystemInfo {8    static func sysctlString(_ name: String) -> String? {9        var size = 010        guard sysctlbyname(name, nil, &size, nil, 0) == 0, size > 0 else { return nil }11        var buf = [CChar](repeating: 0, count: size)12        guard sysctlbyname(name, &buf, &size, nil, 0) == 0 else { return nil }13        return String(cString: buf)14    }1516    static func sysctlInt(_ name: String) -> Int64? {17        var value: Int64 = 018        var size = MemoryLayout<Int64>.size19        guard sysctlbyname(name, &value, &size, nil, 0) == 0 else { return nil }20        return value21    }2223    static var chipName: String { sysctlString("machdep.cpu.brand_string") ?? "Apple Silicon" }24    static var memoryBytes: Int64 { sysctlInt("hw.memsize") ?? 0 }25    static var cpuCores: Int { Int(sysctlInt("hw.ncpu") ?? 0) }2627    /// Rough training footprint: weights+grads+AdamW moments in f32 (4 copies28    /// of params × 4 bytes) + activation estimate per micro-batch.29    static func estimatedTrainingBytes(config: ForgeConfig) -> Int64 {30        let params = Int64(config.model.paramCount)31        let states = params * 4 * 432        let actPerToken = Int64(config.model.nLayers * config.model.dModel * 24)33        let activations = actPerToken34            * Int64(config.train.batchSize * config.model.contextLength)35        return states + activations36    }37}38