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: read unsloth's Qwen-Image 2.1 Q4_K_M (fused gate_up)

Gabriele Lozupone0b2dc9b50dfc
diff --git a/engine-mac/README.md b/engine-mac/README.md
index 0359e0e..7d1efa6 100644
--- a/engine-mac/README.md
+++ b/engine-mac/README.md
@@ -48,6 +48,9 @@ python3 -m venv ~/kokon-bench/venv-oracle && ~/kokon-bench/venv-oracle/bin/pip i
```
256² text-to-image, σ 0.7, 2026-10-25: every stage within 2e-5 relative of diffusers fp32 (velocity 6e-6).
+unsloth's Qwen-Image 2.1 Turbo Q4_K_M (the file Kokon will ship with this engine; ComfyUI layout, each block's MLP
+`gate_up` fused, read as two views): velocity within 7.2e-5, layers 0–1 within 2.4e-6, the gap growing in the last
+layers (not understood yet).
**The text encoder against transformers.** The reference tokenizes with the processor of the official
Qwen/Qwen-Image-2.1-Turbo repository (`processor/` and `text_encoder/config.json`, a few MB) and runs transformers'
diff --git a/engine-mac/Sources/KokonEngine/QwenDiT.swift b/engine-mac/Sources/KokonEngine/QwenDiT.swift
index 5a475cf..8a770ab 100644
--- a/engine-mac/Sources/KokonEngine/QwenDiT.swift
+++ b/engine-mac/Sources/KokonEngine/QwenDiT.swift
@@ -277,11 +277,29 @@ public final class QwenDiT {
func elapsed(since start: UInt64) -> Double { Double(DispatchTime.now().uptimeNanoseconds - start) * 1e-9 }
+ /// The file's Linear weight `name` (without the prefix). Files in ComfyUI's layout (unsloth's GGUFs) fuse each
+ /// block's `img_mlp.gate_layer` and `img_mlp.proj` into one `img_mlp.gate_up` `[2 · 12288, 4096]`, gate rows
+ /// first (diffusers' single_file_utils and sd.cpp's qwen_image_2_1.hpp split it the same way): each half is
+ /// then a view of the fused tensor's rows, a quantized row being contiguous, so nothing is copied.
+ func weight(_ name: String) throws -> GGUFTensor {
+ if let t = file.tensors[QwenDiT.prefix + name] { return t }
+ for (part, half) in [("img_mlp.gate_layer.weight", 0), ("img_mlp.proj.weight", 1)] where name.hasSuffix(part) {
+ let fused = try file.tensor(QwenDiT.prefix + name.dropLast(part.count) + "img_mlp.gate_up.weight")
+ guard let type = fused.type, fused.dims.count == 2, fused.rows % 2 == 0 else {
+ throw GGUFFile.Failure.unsupported("\(fused.name): \(fused.dims)")
+ }
+ let rows = fused.rows / 2
+ return GGUFTensor(name: QwenDiT.prefix + name, dims: [fused.columns, rows], typeID: fused.typeID,
+ offset: fused.offset + half * rows * type.bytes(count: fused.columns))
+ }
+ return try file.tensor(QwenDiT.prefix + name)
+ }
+
/// `C[m, n] = A[m, k] · W` with W the file's Linear `[n, k]`, unpacked to `[k, n]` fp32 in a reserve first.
/// `aRow`, `cRow`: first row of A and C in their slices.
func linear(_ name: String, a: Arena.Slice, aRow: Int = 0, c: Arena.Slice, m: Int, cRow: Int = 0,
commands: MTLCommandBuffer) throws {
- let tensor = try file.tensor(QwenDiT.prefix + name)
+ let tensor = try weight(name)
let (kk, nn) = (tensor.columns, tensor.rows)
let m = QwenDiT.padded(m)
guard (aRow + m) * kk * 4 <= a.bytes, (cRow + m) * nn * 4 <= c.bytes else {
diff --git a/engine-mac/tools/reference_dit.py b/engine-mac/tools/reference_dit.py
index 94b96b4..b11093e 100644
--- a/engine-mac/tools/reference_dit.py
+++ b/engine-mac/tools/reference_dit.py
@@ -37,6 +37,12 @@ class Weights:
self.tensors = {t.name.removeprefix(PREFIX): t for t in self.reader.tensors}
def get(self, name):
+ if name not in self.tensors and name.endswith(("img_mlp.gate_layer.weight", "img_mlp.proj.weight")):
+ # ComfyUI's layout (unsloth's GGUFs): one gate_up tensor, gate rows first, as diffusers' single-file
+ # loader splits it.
+ base = name.rsplit("img_mlp.", 1)[0]
+ gate, up = self.get(base + "img_mlp.gate_up.weight").chunk(2, dim=0)
+ return (gate if name.endswith("gate_layer.weight") else up).contiguous()
t = self.tensors[name]
values = gguf.quants.dequantize(t.data, t.tensor_type).astype(np.float32)
shape = [int(d) for d in reversed(t.shape)] # ggml ne order -> torch order