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.
TypeScript ai-photo-editorcomfyui-alternativegenerative-fillimage-editingimage-generationinpaintinglinuxlocal-aimacosmcpmcp-servernano-banana-alternativeofflinephoto-editorprivacyqwen-imageqwen-image-2-1stable-diffusion-cppvulkanwindows
Mac engine: sd.cpp's noise for a seed
Gabriele Lozupone2288854a1f97
diff --git a/engine-mac/README.md b/engine-mac/README.mdindex df6b4e9..0359e0e 100644--- a/engine-mac/README.md+++ b/engine-mac/README.md@@ -76,8 +76,17 @@ processor) and saves its noise; `check-generate` starts the engine from that noi256², 2026-10-25: each step's latent within 6.7e-6 relative, the image within 1.0e-5, 97 of 262 144 8-bit valuesdiffer by 1. **First 1024² image (M2 Pro, `generate`): 100.7 s in all**, text encoder 1.7 s, 8 steps of 11.4–12.9 s,-VAE 5.4 s with its graph build; peak memory 4.1 GB (Kokon's sd.cpp: ~4.5 min). The seed's noise is the engine's-own (SplitMix64 + Box–Muller), not sd.cpp's: the same seed gives a different image than Kokon today.+VAE 5.4 s with its graph build; peak memory 4.1 GB (Kokon's sd.cpp: ~4.5 min).++**The seed's noise is sd.cpp's** (`Noise.swift`: its default Philox generator, the latent filled in the same+order), so a seed starts from the same latent as in Kokon today. Bit for bit against sd.cpp's own code compiled as+Kokon's macOS engine is (Apple clang, Release: `x · a + b` fused), 8 seeds × 263 144 values; without the fused+multiply-add 0.7 % of the values differ in the last bit:++```bash+clang++ -std=c++17 -O3 -I <stable-diffusion.cpp>/src tools/philox_reference.cpp -o philox && ./philox > philox.bin+.build/release/kokon-engine-mac check-noise philox.bin+```## First measurements (M2 Pro 19-core GPU, 16 GB, macOS 27, 2026-10-25, `probe`)diff --git a/engine-mac/Sources/KokonEngine/Noise.swift b/engine-mac/Sources/KokonEngine/Noise.swiftnew file mode 100644index 0000000..15daaf6--- /dev/null+++ b/engine-mac/Sources/KokonEngine/Noise.swift@@ -0,0 +1,54 @@+import Foundation++/// **The starting noise, as sd.cpp makes it**, so a seed gives the same latent as Kokon's sd.cpp engine.+///+/// sd.cpp's default generator (`CUDA_RNG`, `src/core/rng_philox.hpp` on the fork's `kokon` branch, a port of+/// AUTOMATIC1111's imitation of `torch.randn` on CUDA): value i of a draw is Philox 4×32-10 on the counter+/// (offset, 0, i, 0) with the key (seed low 32 bits, seed high 32 bits), its first two words through one+/// Box–Muller branch (`sin`), in fp32. The offset grows by one per draw; the first image of a seed is draw 0.+///+/// sd.cpp fills the latent `{W, H, C, 1}` (W contiguous), so value i lands at channel i / (h·w), cell i % (h·w):+/// the engine's `[64, h, w]` order. For a flow model the start is the noise itself (`latent · (1 − σ) + noise · σ`+/// with an empty latent and σ = 1).+public enum Noise {+ static let m0: UInt64 = 0xD251_1F53, m1: UInt64 = 0xCD9E_8D57+ static let w0: UInt32 = 0x9E37_79B9, w1: UInt32 = 0xBB67_AE85+ static let twoPow32Inv: Float = 2.3283064e-10+ static let twoPow32Inv2Pi: Float = twoPow32Inv * 6.2831855++ /// `PhiloxRNG(seed).randn(count)` after `offset` earlier draws.+ public static func philox(seed: UInt64, count: Int, offset: UInt32 = 0) -> [Float] {+ let key0 = UInt32(truncatingIfNeeded: seed), key1 = UInt32(truncatingIfNeeded: seed >> 32)+ var out = [Float](repeating: 0, count: count)+ out.withUnsafeMutableBufferPointer { buffer in+ nonisolated(unsafe) let out = buffer.baseAddress!+ let chunks = max(1, min(64, count / 4096))+ DispatchQueue.concurrentPerform(iterations: chunks) { chunk in+ for i in (chunk * count / chunks)..<((chunk + 1) * count / chunks) {+ var (c0, c1, c2, c3) = (offset, UInt32(0), UInt32(truncatingIfNeeded: i), UInt32(0))+ var (k0, k1) = (key0, key1)+ for round in 0..<10 {+ let v1 = UInt64(c0) &* m0, v2 = UInt64(c2) &* m1+ c0 = UInt32(truncatingIfNeeded: v2 >> 32) ^ c1 ^ k0+ c1 = UInt32(truncatingIfNeeded: v2)+ c2 = UInt32(truncatingIfNeeded: v1 >> 32) ^ c3 ^ k1+ c3 = UInt32(truncatingIfNeeded: v1)+ if round < 9 { k0 &+= w0; k1 &+= w1 }+ }+ out[i] = boxMuller(Float(c0), Float(c1))+ }+ }+ }+ return out+ }++ /// sd.cpp's `box_muller`: `u`, `v` centred in their 2⁻³² bins, `sqrt(−2 ln u) · sin(v)`, fp32 throughout.+ /// `x · a + b` is one fused multiply-add, as Apple clang compiles it by default (`-ffp-contract=on`), and so+ /// Kokon's macOS engine (kokon.yml: CMake Release, no float flags): separate roundings change the last bit of+ /// some values. Checked bit for bit against rng_philox.hpp built that way (`tools/philox_reference.cpp`).+ @inline(__always) static func boxMuller(_ x: Float, _ y: Float) -> Float {+ let u = (twoPow32Inv / 2).addingProduct(x, twoPow32Inv)+ let v = (twoPow32Inv2Pi / 2).addingProduct(y, twoPow32Inv2Pi)+ return sqrtf(-2 * logf(u)) * sinf(v)+ }+}diff --git a/engine-mac/Sources/KokonEngine/QwenPipeline.swift b/engine-mac/Sources/KokonEngine/QwenPipeline.swiftindex 863c84d..3b7ba7c 100644--- a/engine-mac/Sources/KokonEngine/QwenPipeline.swift+++ b/engine-mac/Sources/KokonEngine/QwenPipeline.swift@@ -6,7 +6,7 @@ import UniformTypeIdentifiers/// **Qwen-Image 2.1 Turbo, text to image**: `QwenImage21Pipeline.__call__` (diffusers 0.42) with the engine's parts.////// prompt ─ QwenTextEncoder ─ conditioning [T, 4096] (the encoder's file is unmapped before the DiT loads)-/// noise [64, h, w] ─ packed [h·w, 64] ─ 8 × { v = DiT(x, σᵢ); x += (σᵢ₊₁ − σᵢ) · v } flow Euler+/// sd.cpp's noise for the seed [64, h, w] ─ packed [h·w, 64] ─ 8 × { v = DiT(x, σᵢ); x += (σᵢ₊₁ − σᵢ) · v } flow Euler/// x ─ unpacked [64, h, w] ─ QwenVAE ─ RGBA [4, 16h, 16w] in [−1, 1] ─ 8-bit RGBA////// The sigmas are Turbo's (`sample_sigmas` of the checkpoint, the ones Kokon already sends to sd.cpp); the@@ -61,7 +61,7 @@ public final class QwenPipeline {log(String(format: "text encoder: %d tokens in %.2f s", rows, timings["encode"]!))// 2. Noise, packed: token y·w + x holds the 64 channels of cell (y, x).- let start = noise ?? QwenPipeline.gaussian(seed: seed, count: channels * h * w)+ let start = noise ?? Noise.philox(seed: seed, count: channels * h * w) // sd.cpp's, value for valueguard start.count == channels * h * w else {throw QwenSequence.Failure.layout("noise of \(start.count) values for a \(channels)×\(h)×\(w) latent")}@@ -114,29 +114,6 @@ public final class QwenPipeline {return out}- /// Standard normal values for `seed`: SplitMix64 bits, Box–Muller in double precision.- /// Not sd.cpp's generator nor torch's: the same seed does not give the same noise as they do.- public static func gaussian(seed: UInt64, count: Int) -> [Float] {- var state = seed- func next() -> UInt64 {- state &+= 0x9E37_79B9_7F4A_7C15- var z = state- z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9- z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB- return z ^ (z >> 31)- }- func uniform() -> Double { (Double(next() >> 11) + 0.5) / Double(1 << 53) } // (0, 1)- var out = [Float](repeating: 0, count: count)- var i = 0- while i < count {- let r = (-2 * Foundation.log(uniform())).squareRoot(), angle = 2 * Double.pi * uniform()- out[i] = Float(r * cos(angle))- if i + 1 < count { out[i + 1] = Float(r * sin(angle)) }- i += 2- }- return out- }-// MARK: - output/// RGBA `[4, H, W]` in [−1, 1] → interleaved 8-bit RGBA, as VaeImageProcessor.postprocess (`(x / 2 + 0.5)`diff --git a/engine-mac/Sources/KokonEngine/TextChecks.swift b/engine-mac/Sources/KokonEngine/TextChecks.swiftindex 886fd32..6bb16f1 100644--- a/engine-mac/Sources/KokonEngine/TextChecks.swift+++ b/engine-mac/Sources/KokonEngine/TextChecks.swift@@ -91,3 +91,22 @@ extension Checks {print("wrote \(png)")}}++extension Checks {+ /// The seed's noise against sd.cpp's own generator (`tools/philox_reference.cpp`): every value, bit for bit.+ public static func noise(dump: String) throws {+ let data = try Data(contentsOf: URL(fileURLWithPath: dump))+ let want = data.withUnsafeBytes { Array($0.bindMemory(to: Float.self)) }+ let seeds: [UInt64] = [0, 1, 42, 711_449_798, 4001, 0xFFFF_FFFF, 0x0123_4567_89AB_CDEF, .max]+ var offset = 0, differ = 0, worst: Float = 0+ for seed in seeds {+ let got = Noise.philox(seed: seed, count: 262_144) + Noise.philox(seed: seed, count: 1000, offset: 1)+ for (i, g) in got.enumerated() where g.bitPattern != want[offset + i].bitPattern {+ differ += 1; worst = max(worst, abs(g - want[offset + i]))+ }+ offset += got.count+ }+ print("noise: \(offset - differ) of \(offset) values bit-identical to sd.cpp's" + (differ > 0 ? ", largest |Δ| \(worst)" : ""))+ if differ > 0 || offset != want.count { throw GGUFFile.Failure.unsupported("noise differs from sd.cpp's") }+ }+}diff --git a/engine-mac/Sources/kokon-engine-mac/main.swift b/engine-mac/Sources/kokon-engine-mac/main.swiftindex 70a7b2c..0436ea3 100644--- a/engine-mac/Sources/kokon-engine-mac/main.swift+++ b/engine-mac/Sources/kokon-engine-mac/main.swift@@ -13,6 +13,7 @@ kokon-engine-mac: Kokon's Apple Silicon engine (in development)check-text <encoder.gguf> <reference dir> the text encoder against tools/reference_text_encoder.pycheck-generate <encoder.gguf> <dit.gguf> <vae.safetensors> <reference dir>text to image against tools/reference_generate.py, from its noise+ check-noise <philox.bin> the seed's noise against sd.cpp's generator (tools/philox_reference.cpp)generate <encoder.gguf> <dit.gguf> <vae.safetensors> "<prompt>" [--size WxH] [--seed N] [--out file.png]text to image, Turbo's 8 steps (default 1024x1024, seed 42, out.png)probe this Mac's matrix throughput: GPU, AMX, Neural Engine, together@@ -90,6 +91,9 @@ do {try QwenPipeline.writePNG(QwenPipeline.pixels(image, width: size.width, height: size.height),width: size.width, height: size.height, to: out)print("wrote \(out) (\(size.width)×\(size.height), seed \(seed))")+ case "check-noise":+ guard arguments.count == 2 else { fail(usage) }+ try Checks.noise(dump: arguments[1])case "probe":try Probe.run()default:diff --git a/engine-mac/Tests/KokonEngineTests/QwenPipelineTests.swift b/engine-mac/Tests/KokonEngineTests/QwenPipelineTests.swiftindex f5a1b44..0fbc911 100644--- a/engine-mac/Tests/KokonEngineTests/QwenPipelineTests.swift+++ b/engine-mac/Tests/KokonEngineTests/QwenPipelineTests.swift@@ -13,10 +13,22 @@ final class QwenPipelineTests: XCTestCase {XCTAssertEqual(QwenPipeline.unpack(tokens, height: h, width: w), latent)}- func testNoiseIsStandardNormalAndSeeded() {- let a = QwenPipeline.gaussian(seed: 7, count: 200_001)- XCTAssertEqual(a, QwenPipeline.gaussian(seed: 7, count: 200_001))- XCTAssertNotEqual(Array(a.prefix(8)), Array(QwenPipeline.gaussian(seed: 8, count: 8)))+ func testNoiseIsSdCppsBitForBit() {+ // Values of sd.cpp's PhiloxRNG built as Kokon's macOS engine (tools/philox_reference.cpp): the first four+ // and the last of a 1024² latent's draw, then the first of the next draw.+ let cases: [(UInt64, [UInt32], UInt32)] = [+ (42, [0x3e46ace2, 0x400a53f3, 0xbe302e09, 0x3f595c00, 0x3fe8a248], 0x3e0e7a13),+ (711_449_798, [0x3f0c5c5e, 0x3e28992a, 0x3e0ac701, 0xbf825663, 0x3e9eeab0], 0xbfc42d4a),+ ]+ for (seed, want, next) in cases {+ let a = Noise.philox(seed: seed, count: 262_144)+ XCTAssertEqual([a[0], a[1], a[2], a[3], a[262_143]].map(\.bitPattern), want, "seed \(seed)")+ XCTAssertEqual(Noise.philox(seed: seed, count: 1, offset: 1)[0].bitPattern, next, "seed \(seed), next draw")+ }+ }++ func testNoiseIsStandardNormal() {+ let a = Noise.philox(seed: 7, count: 200_001)let mean = a.reduce(0, +) / Float(a.count)let variance = a.reduce(0) { $0 + ($1 - mean) * ($1 - mean) } / Float(a.count)XCTAssertEqual(mean, 0, accuracy: 0.01)diff --git a/engine-mac/tools/philox_reference.cpp b/engine-mac/tools/philox_reference.cppnew file mode 100644index 0000000..1c91a3b--- /dev/null+++ b/engine-mac/tools/philox_reference.cpp@@ -0,0 +1,20 @@+// sd.cpp's default noise generator (PhiloxRNG, src/core/rng_philox.hpp of the fork's `kokon` branch), compiled+// as Kokon's macOS engine is (Apple clang, Release, default float contraction), for `kokon-engine-mac check-noise`.+// Writes raw little-endian floats: for each seed below, a 1024² latent's draw (262 144) then the next draw (1 000).+//+// clang++ -std=c++17 -O3 -I <stable-diffusion.cpp>/src engine-mac/tools/philox_reference.cpp -o philox+// ./philox > philox.bin+#include <cstdio>+#include <cstdlib>+#include <memory>+#include "core/rng_philox.hpp"+int main(int argc, char** argv) {+ uint64_t seeds[] = {0, 1, 42, 711449798ull, 4001ull, 0xFFFFFFFFull, 0x123456789ABCDEFull, 18446744073709551615ull};+ for (uint64_t s : seeds) {+ PhiloxRNG rng(s);+ auto a = rng.randn(262144); // 1024²: 64 × 64 × 64+ auto b = rng.randn(1000); // the next draw (offset 1)+ fwrite(a.data(), 4, a.size(), stdout);+ fwrite(b.data(), 4, b.size(), stdout);+ }+}