Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Keep Mac weights on the GPU as zero-copy mappings; document Metal memory

Gabriele Lozupone88236f1af15c
diff --git a/crates/qls-core/src/process.rs b/crates/qls-core/src/process.rs
index df13b01..f85e54d 100644
--- a/crates/qls-core/src/process.rs
+++ b/crates/qls-core/src/process.rs
@@ -43,20 +43,29 @@ pub fn build_server_args(cfg: &AppConfig, profile: &ModelProfile) -> Vec<String>
// Unified memory: "system RAM" is the GPU's memory too, so a CPU copy only costs.
placement = TextEncoderPlacement::Gpu;
}
+ if metal && placement == TextEncoderPlacement::Gpu {
+ // Weights stay on the Metal device as zero-copy mappings of the model files (needs
+ // the params on their compute device). Without `--auto-fit off` auto-fit may put the
+ // text encoder in "RAM": on a Mac a second, staged copy in the same memory.
+ a.extend(["--backend".into(), dev.clone(), "--auto-fit".into(), "off".into()]);
+ }
match placement {
TextEncoderPlacement::Ram => {
// Weights in RAM, compute on the GPU. Explicit --params-backend disables
// auto-fit, so DiT/VAE weights live on their compute device.
a.extend(["--backend".into(), dev.clone(), "--params-backend".into(), "te=cpu".into()]);
}
+ TextEncoderPlacement::Gpu if metal => {}
TextEncoderPlacement::Gpu => a.extend(["--backend".into(), dev.clone()]),
TextEncoderPlacement::Cpu => a.extend(["--backend".into(), format!("diffusion={dev},vae={dev},te=cpu")]),
TextEncoderPlacement::Auto => a.extend(["--backend".into(), format!("diffusion={dev},vae={dev}"), "--auto-fit".into(), "on".into()]),
TextEncoderPlacement::Disk => a.extend(["--backend".into(), dev.clone(), "--params-backend".into(), "disk".into()]),
}
if metal {
- // Weights mapped from the files instead of copied (fixed for Metal upstream in #2037,
- // part of the pinned build): about 10 GB less peak memory on Apple Silicon.
+ // Weights mapped from the files instead of copied (Metal support upstream in #2037,
+ // part of the pinned build). ggml keeps Metal buffers wired while in use (residency
+ // sets, 3 min keep-alive), so macOS cannot page them out: what does not fit under
+ // Metal's working-set limit must be released instead (Disk placement, 16 GB Macs).
a.push("--mmap".into());
}
if cfg.flash_attention {
@@ -851,7 +860,7 @@ mod tests {
let mut cfg = AppConfig::default();
cfg.gpu_backend = crate::config::GpuBackend::Metal;
let s = build_server_args(&cfg, cfg.profile()).join(" ");
- assert!(s.contains("--backend metal --mmap --fa"), "RAM placement runs everything on the GPU: {s}");
+ assert!(s.contains("--backend metal --auto-fit off --mmap --fa"), "RAM placement runs everything on the GPU: {s}");
assert!(!s.contains("--params-backend") && !s.contains("--sage-attn"));
assert!(s.contains("--mmap") && s.contains("--eager-load"));
cfg.text_encoder_placement = TextEncoderPlacement::Disk;
diff --git a/docs/platforms.md b/docs/platforms.md
index 5eb2915..013dbc0 100644
--- a/docs/platforms.md
+++ b/docs/platforms.md
@@ -53,8 +53,42 @@ pass. Kokon uses CFG 1, which is one pass per step.
| **Unified memory** | 24 GB (Fast/Balanced, up to ~1.5K) | 32 GB+ (Quality 2K); 48 GB+ gets the 8-bit model |
| **Disk** | 15 GB free | 25 GB on the internal SSD |
-16 GB Macs can run it only in the lightest setup: weights streamed from disk, 1 MP, much
-slower. It should be offered as "not supported, try at your own risk" rather than blocked.
+16 GB Macs can run it only in the lightest setup: 1 MP, with each model's weights loaded when
+it runs and released afterwards. That costs only a few seconds per image; the GPU compute is
+what makes it slow. It should be offered as "not supported, try at your own risk" rather than
+blocked.
+
+### Why macOS paging does not help here (verified in the source, 2026-10-09)
+
+macOS compresses and swaps idle memory, and with `--mmap` model files are file-backed pages
+it can drop and re-read. That is why ~10 GB LLMs run well on 16 GB Macs. GPU memory is
+different:
+
+- ggml's Metal backend puts every buffer, mapped files included, in a residency set and keeps
+ it **wired** for 3 minutes after the last GPU use (ggml `ggml-metal-device.m`, heartbeat
+ thread). macOS cannot evict, compress or swap wired pages.
+- Metal's working-set limit (`recommendedMaxWorkingSetSize`, about 11–12 GiB on 16 GB,
+ about 17–18 GiB on 24 GB) is the real ceiling. 10.7 GB of weights, plus several GB of
+ activations at 1 MP, plus about 1 GB of prefix cache does not fit under it on 16 GB.
+- `--params-backend disk` releases each model's weights after it runs, so the text encoder
+ (6.2 GB) is gone during sampling. It disables the zero-copy mapping
+ (`ModelManager::can_mmap_storage` needs `ResidencyMode::ParamBackend`), but the reload
+ reads from the file cache or the SSD in seconds.
+- qwen-image-studio (same engine build, M5 16 GB) saw 4.3 GB of swap and slower steps with
+ everything resident; disk mode peaked at 10–12 GB.
+- Raising the limit with `sudo sysctl iogpu.wired_limit_mb` is undocumented, resets on
+ reboot, and has caused kernel panics on a 24 GB M4 Pro. Not recommended.
+
+Launch flags chosen:
+
+- **16 GB:** `--backend metal --params-backend disk --mmap --fa`, no `--eager-load`.
+- **24 GB and up:** `--backend metal --auto-fit off --mmap --fa`. All weights stay on the GPU
+ as zero-copy mappings; `--auto-fit off` stops auto-fit from staging the text encoder in
+ "RAM", which on a Mac is a second copy in the same memory.
+
+The Neural Engine and the ray-tracing cores are not used by any of these runtimes. The
+per-GPU-core "neural accelerators" exist only from M5 (Metal 4 tensor API, which ggml
+enables only on M5). What matters is GPU cores and memory bandwidth.
### Memory math
@@ -71,13 +105,17 @@ VAE 0.25 GB ≈ 10.7 GB.
### Expected speed
-These are **estimates, not measured**. They scale the M5 result by GPU size and are for
-1 MP, 20 steps, CFG 1.
+These are **estimates, not measured**, for 1 MP, 20 steps, CFG 1. They are derived from the
+M5 16 GB measurement (about 38 s per model pass at 1024², with the same engine build) and
+llama.cpp compute ratios. Our test machines: M2 Pro 16 GB (19-core GPU, 200 GB/s) about
+15–25 s per step, 6–9 min per image; M4 Pro 24 GB (20-core, 273 GB/s) about 12–18 s per
+step, 4.5–6.5 min. The 16-core M4 Pro is close to the M2 Pro. Benchmark both before setting
+the Mac presets' time hints.
| Chip class | GPU cores | Bandwidth | Per image (rough) |
| --- | --- | --- | --- |
| M1–M5 (base) | 7–10 | 68–153 GB/s | 10–15 min |
-| Pro | 14–20 | 150–273 GB/s | 4–6 min |
+| Pro | 14–20 | 150–273 GB/s | 4.5–9 min |
| Max | 30–40 | 300–546 GB/s | 2–3 min |
| Ultra | 48–80 | ~800 GB/s | 1–2 min |