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.

Mac engine: fp16 VAE measured and dropped (0.3 s per image for 250x the error)

Gabriele Lozupone150b42ed9a18
diff --git a/engine-mac/README.md b/engine-mac/README.md
index 0220816..b898eef 100644
--- a/engine-mac/README.md
+++ b/engine-mac/README.md
@@ -200,8 +200,10 @@ that macOS 27's run-time compiler refuses (libnnc ships prebuilt metallibs for i
**VAE decoder** (`check-vae`, against `tools/reference_vae.py`, diffusers fp32): within 1.1e-5; 1024² in 2.75 s. Its
graph builds and compiles in 0.3 s, but its first run at a size costs ~3.7 s more, GPU time (~1 s of CPU): it cannot
hide under the DiT, so the pipeline keeps its VAE between images and a long-lived engine pays it once per size.
-fp16 convolutions (norms and attention kept fp32) were wrong (2.7e-1, an overflow) and 5.7× slower (MPSGraph tries to
-move them to the Neural Engine): the VAE stays fp32.
+fp16 up blocks (norms and the mid block kept fp32) are wrong (2.7e-1) and 3–11× slower only when MPSGraph may place
+them on the Neural Engine (optimization level 1, its default); at level 0 they stay on the GPU and are right but
+gain little: all five 2.43 s against 2.77 at 1024², for 2.7e-3 of diffusers instead of 1.1e-5 (up to ~19 levels off).
+The VAE stays fp32 (2026-10-25).
Credits: the Neural Engine program technique and the GPU + AMX + Neural Engine split come from
[Siliconed](https://github.com/gwenn-ha-dev/Siliconed) (Apache-2.0), written again here.
diff --git a/engine-mac/Sources/KokonEngine/QwenVAE.swift b/engine-mac/Sources/KokonEngine/QwenVAE.swift
index d5e97d9..5cff9af 100644
--- a/engine-mac/Sources/KokonEngine/QwenVAE.swift
+++ b/engine-mac/Sources/KokonEngine/QwenVAE.swift
@@ -38,8 +38,10 @@ public final class QwenVAE {
/// One compiled graph per latent size, compiled to the end (`waitForCompilationCompletion`): 2.7 s at 1024² (M2
/// Pro), mostly the shader compiler, which otherwise ran inside the first decode (6.4 s against 2.75 s; with it
/// up front the first decode is 4.15 s). Beside the DiT it slows the steps far more than it saves: the pipeline
- /// keeps its VAE between images, and an idle engine can prepare ahead. fp16 convolutions (norms and attention in
- /// fp32) were wrong (2.7e-1: an overflow) and 5.7× slower, MPSGraph trying to move them to the Neural Engine: the
+ /// keeps its VAE between images, and an idle engine can prepare ahead. fp16 up blocks (norms and the mid block in
+ /// fp32) are wrong (2.7e-1) and 3–11× slower only at optimization level 1, where MPSGraph places them on the Neural
+ /// Engine; at level 0 they stay on the GPU and are right, but save little: all five in fp16 2.43 s against 2.77 at
+ /// 1024², the first two 2.65, for 2.2–2.7e-3 of diffusers instead of 1.1e-5 (M2 Pro, `check-vae`, 2026-10-25). The
/// VAE stays fp32.
private var executables: [String: MPSGraphExecutable] = [:]
private let lock = NSLock()