Here is a source file from a macOS app. Read it and then summarize what it does, list its main types, and point out anything that looks fragile.

// ToshLLM - run LLMs locally on Intel Macs with AMD GPUs
// Copyright (C) 2026 Engelbert Delgado <engeldlgado@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later

import Foundation
import Metal

extension Notification.Name {
    /// Posted when a fresh engine process has launched (KV slots are empty).
    static let engineDidStart = Notification.Name("toshEngineDidStart")
}

struct GPUDevice: Identifiable, Hashable {
    let index: Int
    let name: String
    let vramMB: Int
    var isExternal: Bool = false   // eGPU (MTLDeviceLocation.external)
    var isIntegrated: Bool = false // iGPU (MTLDevice.isLowPower); never auto-selected
    /// Metal peer group: 0 when the card has no Infinity Fabric link, shared with
    /// the other members when it has one.
    var peerGroupID: UInt64 = 0
    var peerCount: Int = 0
    /// Metal exposes bf16 only from the Metal 3 family up; older cards abort on a bf16 weight.
    var supportsBF16: Bool = true
    var id: Int { index }
    /// Rounded, since Metal reports a working set a little off the nominal size.
    var vramGB: Int { Int((Double(vramMB) / 1024).rounded()) }
}

enum DynamicMoeAutoRoute: Equatable {
    case cache
    case normalDense
    case normalFitsVRAM
    case normalInsufficientRAM
    case normalUnsupportedGPU
    case normalMissingModel
    case normalSplitOrRouter
    case normalMissingMetadata
    case normalInsufficientVRAM
    case normalNoCacheBenefit
    case normalOversizedHostBank
}

struct DynamicMoeModelInfo: Equatable {
    let layerCount: Int
    let expertCount: Int
    let activeExpertCount: Int
}

struct DynamicMoeSlotPlan: Equatable {
    let model: DynamicMoeModelInfo
    /// Smallest cache that can hold every expert selected by one decoded token.
    let minimumSlots: Int
    /// Largest valid K for this architecture, regardless of memory pressure.
    let maximumSlots: Int
    /// Largest K estimated to fit after fixed weights, runtime and transfer buffers.
    let recommendedMaximumSlots: Int
    let automaticSlots: Int
    let estimatedBytesPerSlot: UInt64
    let estimatedFixedVRAMBytes: UInt64

    func clamped(_ slots: Int) -> Int {
        min(max(slots, minimumSlots), maximumSlots)
    }

    func estimatedVRAMBytes(slots: Int) -> UInt64 {
        estimatedFixedVRAMBytes + UInt64(clamped(slots)) * estimatedBytesPerSlot
    }
}

struct ServerSettings {
    var serverBinary: String
    var modelPath: String
    var port: Int
    var ngl: Int
    var ncmoe: Int
    var ctx: Int
    var contextAutomatic: Bool = false
    var threads: Int
    var flashAttn: String      // auto | on | off
    var noMmap: Bool
    var jinja: Bool
    var vramReserveMB: Int
    var gpuIndex: Int          // -1 = system default
    /// Explicit set of physical GPUs to split across (2+ entries). Overrides
    /// `gpuIndex` and the all/N `multiGPU` split.
    var gpuList: [Int] = []
    var extraArgs: String
    /// Serve /v1/embeddings (--embeddings). llama-server restricts the process
    /// to embedding use, so it's meant for a dedicated embedding-model server.
    var embeddings: Bool = false
    var agentToolsEnabled: Bool = false
    var uiMcpProxy: Bool = false
    /// `--tools-runtime` target (`docker:image`, `podman:image`, `ssh:host`...). Empty
    /// runs the tools in the app's own environment, which is the engine default.
    var toolsRuntime: String = ""
    var cacheTypeK: String     // f16 | q8_0 | q5_x | q4_x | iq4_nl
    var cacheTypeV: String
    var mlock: Bool
    /// Host-RAM prompt cache cap in MiB (0 disables). The engine's own 8192 default
    /// grows with use and pushes a big model into swap, degrading speed per session.
    var cacheRAM: Int = 2048
    /// Emit reasoning inline in `content` (<think>…) instead of the separate
    /// `reasoning_content` field, for external clients that ignore the latter.
    var reasoningInline: Bool = false
    /// Server slots (0 = engine auto). With 1, requests queue instead of competing
    /// for the GPU, and a prefill aborted by a client timeout stays in the slot so
    /// the retry resumes where it left off.
    var parallelSlots: Int = 1
    var apiKeyEnabled: Bool = false
    /// Serve beyond loopback and advertise with Bonjour. Off by default: it opens
    /// the API to the whole local network, so it wants `apiKeyEnabled` alongside.
    var localNetworkDiscovery: Bool = false
    /// Legacy profile field kept for decoding older saved settings. MTP is now
    /// selected automatically from model capability and expert offload.
    var specMTP: Bool = false
    /// Route attention through the AMD Metal Flash-Attention kernel (TOSH_FA_AMD).
    var faAmd: Bool = true
    /// MoE-offload prefill boost: overlap expert uploads with compute
    /// (GGML_SCHED_PREFETCH_EXPERTS) and keep CPU experts unpacked so their
    /// matmuls can offload (GGML_CPU_NO_REPACK).
    var prefetchExperts: Bool = true
    /// Prefill micro-batch (--ubatch-size); 0 leaves the engine's 512. Only worth
    /// raising with experts on CPU: each micro-batch pays one expert upload over
    /// the bus, so a long prompt pays it once per batch instead of once per token.
    /// Costs VRAM linearly (~0.5 GiB per 512 on a 35B), so it is not a default.
    var ubatch: Int = 0
    /// Experimental bounded-VRAM expert cache. Compiled into the bundled engine,
    /// but completely inert unless this persisted user choice is enabled.
    var dynamicMoe: Bool = false
    var dynamicMoeSlots: Int = 8
    var dynamicMoePrefetch: Int = 4
    var dynamicMoePolicy: String = "cache" // cache | auto
    /// Router mode (`--models-preset`): one process auto-loads/unloads whichever
    /// model a request's "model" field names, instead of the fixed `modelPath`.
    var routerMode: Bool = false
    /// Models the router keeps loaded at once (LRU); 1 is safest on a single GPU.
    var routerModelsMax: Int = 1
    /// Persist each conversation's KV cache to disk (`--slot-save-path`). Needs the
    /// AMD FA kernel: without it the V cache is transposed and save/restore copies
    /// it row-by-row, unusably slow.
    var persistCache: Bool = false
    /// EXPERIMENTAL: split one model across all detected GPUs. Unvalidated on
    /// AMD/Metal: cross-GPU copies take a different path than the staging the
    /// patch covers and could corrupt or hang.
    var multiGPU: Bool = false
    /// How many GPUs to split across when `multiGPU` is on. 0 = all detected.
    /// Fewer GPUs trade prompt speed for generation speed (less cross-card sync).
    var multiGPUCount: Int = 0
    /// How the split divides the model (`--split-mode`): `layer` gives each GPU
    /// whole layers, `tensor` splits every matmul so both work on the same token,
    /// which costs one allreduce per layer and only pays off on a big model.
    var splitMode: String = "layer"
    /// GPUs per tensor-parallel group when splitting by tensors. 0 or 1 means one
    /// group with every card, which is the plain tensor split. A smaller group
    /// splits tensors inside it and layers between groups, which keeps most of the
    /// prompt speed without paying the per-layer wait across every card.
    var splitGroupSize: Int = 0
    /// Hand off activations between GPUs with shared Metal events instead of
    /// draining both queues on every copy (TOSH_MGPU_EVENTS). Inert on a layer
    /// split; on a tensor split it is most of the generation speed.
    var mgpuEvents: Bool = true
    /// When two split GPUs share a Metal peer group (Infinity Fabric Link, e.g. a
    /// W6800X/Vega II Duo), copy activations die-to-die instead of via host
    /// (TOSH_MGPU_PEER). Worth 16% of the prefill, and only safe alongside mgpuEvents.
    var mgpuPeer: Bool = true
    /// Force VRAM-resident (private) Metal buffers. The backend forces shared ones
    /// for external GPUs, which streams weights over Thunderbolt every op; this
    /// covers the default-GPU case, where the app can't tell macOS picked an eGPU.
    var forcePrivateBuffers: Bool = false
    /// Reuse shifted KV chunks across mid-prompt divergences (agent edits, a
    /// stripped <think>). Fast but approximate, hence user-toggleable.
    var cacheReuse: Bool = true
    /// Load the multimodal projector (mmproj) for vision-capable models. Off skips it
    /// so a vision model runs text-only and frees the VRAM the image encoder would use.
    var loadVision: Bool = true
    /// Cap on the tokens one image may take. 0 keeps the model's own limit; lowering it
    /// shrinks the vision encoder's attention quadratically, which is what rescues a
    /// card that cannot allocate that buffer.
    var imageMaxTokens: Int = 0
    /// llama-bench workload sizes: prompt tokens (-p → ppN), generated tokens
    /// (-n → tgN) and context depth (-d, tokens already in the KV cache before
    /// measuring). Benchmark-only; llama-server ignores them.
    var benchPP: Int = 512
    var benchTG: Int = 128
    var benchDepth: Int = 0

    /// One model served across several GPUs, either by the all/N toggle or by an
    /// explicit selection of at least two cards.
    var isSplitting: Bool { multiGPU || gpuList.count >= 2 }
    /// Guards against a stale or hand-edited value: llama.cpp only takes these two,
    /// and a tensor split the model cannot take falls back instead of aborting.
    var effectiveSplitMode: String {
        (splitMode == "tensor" && tensorSplitLimit.map { splitDeviceCount <= $0 } != false) ? "tensor" : "layer"
    }

    /// The group size to pass, or nil when it would be a no-op or an invalid split.
    var effectiveSplitGroupSize: Int? {
        let n = splitDeviceCount
        guard splitGroupSize > 1, splitGroupSize < n, n % splitGroupSize == 0 else { return nil }
        return splitGroupSize
    }

    /// GPUs the split will span, matching how the launch environment picks them.
    var splitDeviceCount: Int {
        if gpuList.count >= 2 { return gpuList.count }
        guard multiGPU else { return 1 }
        let gpus = ServerController.availableGPUs()
        let discrete = gpus.filter { !$0.isIntegrated }.count
        let limit = discrete > 0 ? discrete : gpus.count
        return max(2, multiGPUCount > 0 ? min(multiGPUCount, limit) : limit)
    }

    /// Layer counts per GPU that even out the bytes rather than the layer count.
    /// `--n-cpu-moe` empties the experts of the first N layers, and llama.cpp still hands
    /// out layers by index, so without this the last GPUs take every expert and overflow.
    var layerBalancedTensorSplit: [Int]? {
        guard isSplitting, effectiveSplitMode == "layer", ncmoe > 0 else { return nil }
        let devices = splitDeviceCount
        guard devices >= 2 else { return nil }
        let flags = GGUFMetadataCache.tensorFlags(at: modelPath)
        let expert = Double(flags.expertBytesPerLayer)
        let other = Double(flags.otherBytesPerLayer)
        guard expert > 0, other > 0,
              let blocks = Self.ggufUInt32("block_count", at: modelPath).map(Int.init),
              blocks >= devices else { return nil }

        let weights = (0..<blocks).map { $0 < ncmoe ? other : other + expert }
        let target = weights.reduce(0, +) / Double(devices)
        var counts = [Int](repeating: 0, count: devices)
        var dev = 0
        var acc = 0.0
        for (il, w) in weights.enumerated() {
            // leave at least one layer for each device still waiting
            let mustAdvance = blocks - il == devices - dev
            if dev < devices - 1 && (mustAdvance || (acc + w/2 > target && counts[dev] > 0)) {
                dev += 1
                acc = 0
            }
            counts[dev] += 1
            acc += w
        }
        return counts.allSatisfy { $0 > 0 } ? counts : nil
    }

    /// GPUs this model's experts can be divided among, nil when nothing limits it.
    var tensorSplitLimit: Int? { Self.tensorSplitLimit(forModel: modelPath) }

    /// The user asked for a tensor split the model cannot take; the run uses layers.
    var tensorSplitDowngraded: Bool {
        splitMode == "tensor" && isSplitting && effectiveSplitMode == "layer"
    }

    var isMultimodal: Bool { Self.mmprojPath(forModel: modelPath) != nil }
    /// Vision actually loaded (projector available AND the eye is on); slot
    /// persistence gates on this, not on mere availability.
    var visionLoaded: Bool { loadVision && isMultimodal }

    /// `--no-mmap` and `--mlock` both write the engine's load mode and the last one wins, so
    /// passing the pair let mlock decide whether mmap stayed on. Nil keeps the engine default.
