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: on the M5 Pro, the GPU's products on 2 SIMD groups per tile (#49)
Cesare Davide Pace5a5f3a9d55e6
diff --git a/engine-mac/README.md b/engine-mac/README.mdindex 8809260..b34b64f 100644--- a/engine-mac/README.md+++ b/engine-mac/README.md@@ -429,6 +429,17 @@ correctness test that runs on any Mac (`PerChipTests`). Only the M2 Pro has measrun on the GPU's neural accelerators, before it on the shader cores). Its source compiles at run time on macOS 27,so it is on wherever Metal 4 runs (`KOKON_NA=0` off). M2 Pro: 6.3 TFLOP/s against MPS's 5.6 at 1536 × 4096 × 4096,bit for bit the same result (`check-na`); per evaluation at 1024², 3 alternated rounds: 7.57 → 7.48 s.+ **On the M5 Pro they are the neural accelerators**, and MPS reaches them too: 31.4 against 32.2 TFLOP/s at+ 1536 × 4096 × 4096, the same result to the bit, against at most ~10 for its 20 GPU cores without them; MLX 0.32+ reaches 28–31 on the DiT's shapes. So `KOKON_NA=0` does not turn them off; it was ahead because of the tile below.+- **2 SIMD groups per `na_matmul` tile on the M5 Pro** (`KOKON_NA_SG2=0` off, `=1` on elsewhere). With 4, the MLP's+ out (2048 × 12288 × 4096) took 9.6 ms against MPS's 6.7 and 4096 × 4096 × 4096 6.0 against 4.4 (`check-na`, best+ of 5); with 2, 7.0 and 4.4, the same result to the bit. A sweep of tiles 32–128 and 1–8 SIMD groups found no+ shape faster than ~32 TFLOP/s fp16, the units' ceiling. M5 Pro 20-core, 24 GB, macOS 27.2, 3 alternated rounds,+ median (min–max): `time-dit` third evaluation 4.21 (4.19–4.27) → 3.86 (3.84–4.16) s (MPS 3.89); test set 1024²+ 37.2 → 34.4 s, 1344×768 37.5 → 34.9 s, edit 40.2 → 39.0 s; velocity error 4.1 · 10⁻⁴ both. Not measured on the M5+ or M5 Max, which keep 4. int8 × int8 runs at ~62 TOP/s there, twice fp16, but needs int8 activations (the Neural+ Engine's int8 error, chapter 29); half × int8 weights is slower than fp16.- **int8 products on the Neural Engine** (libnnc's M3/M4 path; `KOKON_ANE_INT8=1`, int8 weights and rows). Opt-in onevery chip, as their error does not depend on the chip. M2 Pro: one 1792 × 4096 × 4096 tile 10.1 ms in fp16, 7.0 msin int8 (`check-ane8`); per evaluation 7.42 → 7.00 s; but the velocity at 896² goes from 7.6e-4 to 4.1e-2 (one int8 scale per 4 096 weights where Q4_K has one per 32;diff --git a/engine-mac/Sources/KokonEngine/Bench.swift b/engine-mac/Sources/KokonEngine/Bench.swiftindex 643b4a9..d4fd365 100644--- a/engine-mac/Sources/KokonEngine/Bench.swift+++ b/engine-mac/Sources/KokonEngine/Bench.swift@@ -27,6 +27,9 @@ public enum Bench {}if NeuralAccelerators.selected(device: device), NeuralAccelerators.make(device: device) != nil {paths.append(("GPU products on Metal 4 tensor operations (the M5's neural accelerators; shader cores before)", "KOKON_NA", "0"))+ if NeuralAccelerators.simdgroups(chip: chip.name, setting: ProcessInfo.processInfo.environment["KOKON_NA_SG2"]) == 2 {+ paths.append(("those products on 2 SIMD groups per tile (M5 Pro)", "KOKON_NA_SG2", "0"))+ }}if Coexecutor.flushReleaseSelected(context) {paths.append(("each product released to the AMX and the Neural Engine at once (this Mac's signals arrive late)", "KOKON_FLUSH_READY", "0"))diff --git a/engine-mac/Sources/KokonEngine/Checks.swift b/engine-mac/Sources/KokonEngine/Checks.swiftindex b1dea1d..65a74df 100644--- a/engine-mac/Sources/KokonEngine/Checks.swift+++ b/engine-mac/Sources/KokonEngine/Checks.swift@@ -295,19 +295,24 @@ extension Checks {}return best}- let tNA = try run { try na.encode(a: (a, 0), b: (b, 0), c: (c1, 0), m: m, n: n, k: k, commands: $0) }let tMPS = try run {product.encode(commandBuffer: $0,leftMatrix: MPSMatrix(buffer: a, descriptor: MPSMatrixDescriptor(rows: m, columns: k, rowBytes: k * 2, dataType: .float16)),rightMatrix: MPSMatrix(buffer: b, descriptor: MPSMatrixDescriptor(rows: k, columns: n, rowBytes: n * 2, dataType: .float16)),resultMatrix: MPSMatrix(buffer: c2, descriptor: MPSMatrixDescriptor(rows: m, columns: n, rowBytes: n * 4, dataType: .float32)))}- let x = c1.contents().assumingMemoryBound(to: Float.self), y = c2.contents().assumingMemoryBound(to: Float.self)- var diff = 0.0, norm = 0.0- for i in 0..<(m * n) { diff += Double(x[i] - y[i]) * Double(x[i] - y[i]); norm += Double(y[i]) * Double(y[i]) }let flops = 2.0 * Double(m) * Double(n) * Double(k)- print(String(format: "%d×%d×%d: na_matmul %.2f ms (%.1f TFLOP/s), MPS %.2f ms (%.1f TFLOP/s), relative difference %.2e",- m, k, n, tNA * 1e3, flops / tNA / 1e12, tMPS * 1e3, flops / tMPS / 1e12, (diff / norm).squareRoot()))+ // Both tile variants (`NeuralAccelerators.simdgroups`), this chip's choice marked.+ for groups in [4, 2] {+ guard let variant = NeuralAccelerators.make(device: context.device, simdgroups: groups) else { continue }+ let tNA = try run { try variant.encode(a: (a, 0), b: (b, 0), c: (c1, 0), m: m, n: n, k: k, commands: $0) }+ let x = c1.contents().assumingMemoryBound(to: Float.self), y = c2.contents().assumingMemoryBound(to: Float.self)+ var diff = 0.0, norm = 0.0+ for i in 0..<(m * n) { diff += Double(x[i] - y[i]) * Double(x[i] - y[i]); norm += Double(y[i]) * Double(y[i]) }+ print(String(format: "%d×%d×%d: na_matmul, %d SIMD groups%@ %.2f ms (%.1f TFLOP/s), MPS %.2f ms (%.1f TFLOP/s), relative difference %.2e",+ m, k, n, groups, groups == na.simdgroups ? " (selected)" : "", tNA * 1e3, flops / tNA / 1e12,+ tMPS * 1e3, flops / tMPS / 1e12, (diff / norm).squareRoot()))+ }}}diff --git a/engine-mac/Sources/KokonEngine/Kernels/metal4/na_matmul.metal b/engine-mac/Sources/KokonEngine/Kernels/metal4/na_matmul.metalindex 31914f8..bed9451 100644--- a/engine-mac/Sources/KokonEngine/Kernels/metal4/na_matmul.metal+++ b/engine-mac/Sources/KokonEngine/Kernels/metal4/na_matmul.metal@@ -10,13 +10,15 @@ using namespace mpp::tensor_ops;// kernel is written for Kokon.//// A and B fp16, C fp32, row-major and dense; m and n multiples of 64 (the DiT pads every product to 64 rows, its-// widths are multiples of 4 096). One threadgroup of 4 SIMD groups per 64 × 64 tile of C, the whole K in one run.-// Tensor extents list the innermost dimension first: A is (k, m), B (n, k), C (n, m).+// widths are multiples of 4 096). One threadgroup per 64 × 64 tile of C, the whole K in one run, on 4 SIMD groups+// (`na_matmul`) or 2 (`na_matmul_sg2`, the M5 Pro's: `NeuralAccelerators.simdgroups`). Tensor extents list the+// innermost dimension first: A is (k, m), B (n, k), C (n, m).struct NAParams {uint m, n, k;};+template <int SIMDGROUPS>kernel void na_matmul(device half *A [[buffer(0)]], device half *B [[buffer(1)]], device float *C [[buffer(2)]],constant NAParams &p [[buffer(3)]], uint2 tg [[threadgroup_position_in_grid]]) {auto a = tensor<device half, dextents<int32_t, 2>, tensor_inline>(A, dextents<int32_t, 2>(p.k, p.m));@@ -24,9 +26,14 @@ kernel void na_matmul(device half *A [[buffer(0)]], device half *B [[buffer(1)]]auto c = tensor<device float, dextents<int32_t, 2>, tensor_inline>(C, dextents<int32_t, 2>(p.n, p.m));constexpr auto descriptor = matmul2d_descriptor(64, 64, dynamic_length_v<int>, false, false, false,matmul2d_descriptor::mode::multiply);- matmul2d<descriptor, execution_simdgroups<4>> op;+ matmul2d<descriptor, execution_simdgroups<SIMDGROUPS>> op;auto mA = a.slice(0, int(tg.y) * 64);auto mB = b.slice(int(tg.x) * 64, 0);auto mC = c.slice(int(tg.x) * 64, int(tg.y) * 64);op.run(mA, mB, mC);}++template [[host_name("na_matmul")]] kernel void na_matmul<4>(device half *, device half *, device float *,+ constant NAParams &, uint2);+template [[host_name("na_matmul_sg2")]] kernel void na_matmul<2>(device half *, device half *, device float *,+ constant NAParams &, uint2);diff --git a/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift b/engine-mac/Sources/KokonEngine/NeuralAccelerators.swiftindex 875bbb0..3f5ebfc 100644--- a/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift+++ b/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift@@ -9,17 +9,36 @@ import Metal/// Selected for the split products' GPU rows wherever Metal 4 runs (fp16 A and weights, the defaults), MPS when the/// source does not compile or with `KOKON_NA=0`. On the M2 Pro's shader cores it beats MPS too: 6.3 against 5.6/// TFLOP/s at 1536 × 4096 × 4096, bit for bit the same result (`check-na`), and per evaluation at 1024², 3 alternated-/// rounds of 4, 7.57 → 7.48 s (2026-10-25). Not yet run on an M5, M1, M3 or M4; the attention stays on the engine's-/// own kernels.+/// rounds of 4, 7.57 → 7.48 s (2026-10-25). On the M5 Pro they are the neural accelerators: 31 TFLOP/s at+/// 1536 × 4096 × 4096, against at most ~10 the 20 GPU cores could do without them; MPS reaches them too (32 TFLOP/s,+/// the same result to the bit), and so does MLX (28–31). Not yet run on an M1, M3 or M4; the attention stays on the+/// engine's own kernels.public final class NeuralAccelerators: @unchecked Sendable {let pipeline: MTLComputePipelineState+ /// SIMD groups per 64 × 64 tile: 4 (`na_matmul`), or 2 (`na_matmul_sg2`) where `simdgroups` picks it.+ public let simdgroups: Int/// Why `make` returned nil, for the bench's report.nonisolated(unsafe) public private(set) static var unavailable: String?struct Params { var m, n, k: UInt32 }- /// Compiles the kernel; nil without Metal 4 or when the compiler refuses the source.- public static func make(device: MTLDevice) -> NeuralAccelerators? {+ /// **2 SIMD groups per tile on the M5 Pro.** With 4, a tile's K loop ran 20–26 % slower than MPS where it is+ /// long or the rows are many: the MLP's out (2048 × 12288 × 4096) 10.2 ms against MPS's 7.9, 4096 × 4096 × 4096+ /// 6.0 against 4.5. With 2 every DiT shape is at the matrix units' ceiling (~32 TFLOP/s fp16, MPS's and MLX's+ /// too): 7.1 and 4.5 ms, the same result to the bit (a sweep of tiles 32–128 and 1–8 SIMD groups; M5 Pro 20-core,+ /// macOS 27.2, median of 12). Not measured on the M5, M5 Max or the shader cores of older chips, which keep 4.+ /// `KOKON_NA_SG2=0` / `=1`.+ static func simdgroups(chip: String, setting: String?) -> Int {+ switch setting {+ case "0": return 4+ case "1": return 2+ default: return chip.contains("M5 Pro") ? 2 : 4+ }+ }++ /// Compiles the kernel; nil without Metal 4 or when the compiler refuses the source. `simdgroups`: nil for this+ /// chip's choice.+ public static func make(device: MTLDevice, simdgroups: Int? = nil) -> NeuralAccelerators? {guard #available(macOS 26, *), device.supportsFamily(.metal4) else {unavailable = "no Metal 4 on this GPU or macOS"return nil@@ -31,15 +50,21 @@ public final class NeuralAccelerators: @unchecked Sendable {options.languageVersion = .version4_0options.mathMode = .safelet library = try device.makeLibrary(source: try String(contentsOf: url, encoding: .utf8), options: options)- guard let function = library.makeFunction(name: "na_matmul") else { throw MetalContext.Failure.kernel("na_matmul not found") }- return NeuralAccelerators(pipeline: try device.makeComputePipelineState(function: function))+ let groups = simdgroups ?? NeuralAccelerators.simdgroups(chip: Chip.current.name,+ setting: ProcessInfo.processInfo.environment["KOKON_NA_SG2"])+ let name = groups == 2 ? "na_matmul_sg2" : "na_matmul"+ guard let function = library.makeFunction(name: name) else { throw MetalContext.Failure.kernel("\(name) not found") }+ return NeuralAccelerators(pipeline: try device.makeComputePipelineState(function: function), simdgroups: groups)} catch {unavailable = "the kernel did not compile: \(error.localizedDescription)"return nil}}- init(pipeline: MTLComputePipelineState) { self.pipeline = pipeline }+ init(pipeline: MTLComputePipelineState, simdgroups: Int) {+ self.pipeline = pipeline+ self.simdgroups = simdgroups+ }/// Whether the split products' GPU rows go through them on this device.public static func selected(device: MTLDevice) -> Bool {@@ -67,7 +92,7 @@ public final class NeuralAccelerators: @unchecked Sendable {var p = Params(m: UInt32(m), n: UInt32(n), k: UInt32(k))e.setBytes(&p, length: MemoryLayout<Params>.stride, index: 3)e.dispatchThreadgroups(MTLSize(width: n / 64, height: m / 64, depth: 1),- threadsPerThreadgroup: MTLSize(width: pipeline.threadExecutionWidth * 4, height: 1, depth: 1))+ threadsPerThreadgroup: MTLSize(width: pipeline.threadExecutionWidth * simdgroups, height: 1, depth: 1))e.endEncoding()}}diff --git a/engine-mac/Sources/kokon-engine-mac/main.swift b/engine-mac/Sources/kokon-engine-mac/main.swiftindex 0d93f46..3d624ff 100644--- a/engine-mac/Sources/kokon-engine-mac/main.swift+++ b/engine-mac/Sources/kokon-engine-mac/main.swift@@ -36,7 +36,8 @@ kokon-engine-mac: Kokon's Apple Silicon engine (in development)a few minutes: this Mac's paths, speed at 1024², accuracy against the fp32 path;prints a report to paste in a GitHub issue (Kokon's model files by default)check-ane8 the M3/M4 path (int8 Neural Engine program) against the fp16 one- check-na the M5 path (Metal 4 tensor operations) against MPS on one product+ check-na [m k n] the M5 path (Metal 4 tensor operations) against MPS on one product+ (default 1536 4096 4096), both tile variantsprobe this Mac's matrix throughput: GPU, AMX, Neural Engine, togetherprobe-attention the target's attention at 1024² on the GPU, the Neural Engine, the CPU"""@@ -204,7 +205,12 @@ do {case "check-ane8":try Checks.aneInt8()case "check-na":- try Checks.neuralAccelerators()+ let shape = arguments.dropFirst().compactMap { Int($0) }+ if shape.count == 3 {+ try Checks.neuralAccelerators(m: shape[0], k: shape[1], n: shape[2])+ } else {+ try Checks.neuralAccelerators()+ }case "probe":try Probe.run()case "probe-attention":diff --git a/engine-mac/Tests/KokonEngineTests/PerChipTests.swift b/engine-mac/Tests/KokonEngineTests/PerChipTests.swiftindex 5aac247..da460de 100644--- a/engine-mac/Tests/KokonEngineTests/PerChipTests.swift+++ b/engine-mac/Tests/KokonEngineTests/PerChipTests.swift@@ -8,10 +8,11 @@ import XCTest/// for (`kokon-engine-mac bench`), their results do not.final class PerChipTests: XCTestCase {/// `na_matmul` (the M5's neural accelerators; shader cores on older Metal 4 GPUs) against a product in double of- /// the same fp16 operands, on shapes with several tiles along m and n and a K that is not a power of two.+ /// the same fp16 operands, on shapes with several tiles along m and n and a K that is not a power of two: both+ /// tile variants (4 SIMD groups, and the M5 Pro's 2), forced on whatever chip runs the test.func testNeuralAcceleratorProduct() throws {let device = try XCTUnwrap(MTLCreateSystemDefaultDevice())- guard let na = NeuralAccelerators.make(device: device) else {+ guard NeuralAccelerators.make(device: device) != nil else {throw XCTSkip("Metal 4 tensor operations unavailable: \(NeuralAccelerators.unavailable ?? "?")")}let context = try MetalContext()@@ -25,21 +26,40 @@ final class PerChipTests: XCTestCase {let ab = try context.scratch(a.count * 2, label: "A"), bb = try context.scratch(b.count * 2, label: "B")ab.contents().copyMemory(from: a, byteCount: a.count * 2)bb.contents().copyMemory(from: b, byteCount: b.count * 2)- let cb = try context.scratch(m * n * 4, label: "C")- let commands = context.queue.makeCommandBuffer()!- try na.encode(a: (ab, 0), b: (bb, 0), c: (cb, 0), m: m, n: n, k: k, commands: commands)- try MetalContext.finish(commands, "na_matmul")- let c = cb.contents().assumingMemoryBound(to: Float.self)- var worst = 0.0, scale = 0.0+ var exact = [Double](repeating: 0, count: m * n), scale = 0.0for i in 0..<m {for j in 0..<n {var s = 0.0for l in 0..<k { s += Double(a[i * k + l]) * Double(b[l * n + j]) }- worst = max(worst, abs(Double(c[i * n + j]) - s))+ exact[i * n + j] = sscale = max(scale, abs(s))}}- XCTAssertLessThan(worst / scale, 1e-5, "fp16 products accumulated in fp32")+ var results: [[Float]] = []+ for groups in [4, 2] {+ let na = try XCTUnwrap(NeuralAccelerators.make(device: device, simdgroups: groups))+ XCTAssertEqual(na.simdgroups, groups)+ let cb = try context.scratch(m * n * 4, label: "C")+ let commands = context.queue.makeCommandBuffer()!+ try na.encode(a: (ab, 0), b: (bb, 0), c: (cb, 0), m: m, n: n, k: k, commands: commands)+ try MetalContext.finish(commands, "na_matmul")+ let c = Array(UnsafeBufferPointer(start: cb.contents().assumingMemoryBound(to: Float.self), count: m * n))+ let worst = zip(c, exact).map { abs(Double($0) - $1) }.max() ?? 0+ XCTAssertLessThan(worst / scale, 1e-5, "\(groups) SIMD groups: fp16 products accumulated in fp32")+ results.append(c)+ }+ // On the M5 Pro the two were the same to the bit; elsewhere only the error above is required.+ if Chip.current.name.contains("M5 Pro") { XCTAssertEqual(results[0], results[1]) }+ }++ /// Which chips get 2 SIMD groups per tile: the M5 Pro only, unless the setting says otherwise.+ func testNeuralAcceleratorTileSelection() {+ XCTAssertEqual(NeuralAccelerators.simdgroups(chip: "Apple M5 Pro", setting: nil), 2)+ XCTAssertEqual(NeuralAccelerators.simdgroups(chip: "Apple M5 Pro", setting: "0"), 4)+ for chip in ["Apple M5", "Apple M5 Max", "Apple M4 Pro", "Apple M2 Pro", "Apple M1"] {+ XCTAssertEqual(NeuralAccelerators.simdgroups(chip: chip, setting: nil), 4, chip)+ XCTAssertEqual(NeuralAccelerators.simdgroups(chip: chip, setting: "1"), 2, chip)+ }}func testChipNames() {diff --git a/engine-mac/chips.md b/engine-mac/chips.mdindex 09e99df..3848274 100644--- a/engine-mac/chips.md+++ b/engine-mac/chips.md@@ -8,7 +8,7 @@ Before working on a chip, look here and in [docs/mac-engine.md](docs/mac-engine.| Chip | Mac (model, cooling) | Memory | macOS | Paths on by default | 1024² text to image | Velocity error (default) | Source ||---|---|---|---|---|---|---|---|| M2 Pro, 19-core GPU | MacBook Pro (Mac14,10), fans | 16 GB | 27 | Metal 4 products (7.57 → 7.48 s per evaluation); direct attention off (142 ms vs 91) | ~64–67 s; 1 MP edit ~106 s, peak 5 GB | ~6–7 · 10⁻⁴ | [README.md](README.md), [docs/mac-engine.md](docs/mac-engine.md) chapters 26, 28, 30 |-| M5 Pro, 20-core GPU | MacBook Pro 16" (Mac17,8), fans | 24 GB | 26.6.2 | Metal 4 products; each product released at once (signals arrive late: macOS 26) | ~40 s (test set, median of 3; master 49.0 s); edit ~43 s, peak 4.3–4.6 GB | 3.1 · 10⁻⁴ (bench, vs the engine's fp32 path) | [README.md](README.md) "Per-chip paths", [docs/mac-engine.md](docs/mac-engine.md) chapters 13, 26 |+| M5 Pro, 20-core GPU | MacBook Pro 16" (Mac17,8), fans | 24 GB | 27.2 | Metal 4 products on 2 SIMD groups per tile; each product released at once (signals arrive late: still on macOS 27.2) | 34.4 s (test set, median of 3, macOS 27.2; 37.2 s with 4 SIMD groups); edit 39.0 s, peak 5.2–5.3 GB | 4.1 · 10⁻⁴ (bench, vs the engine's fp32 path) | [README.md](README.md) "Per-chip paths", [docs/mac-engine.md](docs/mac-engine.md) chapters 13, 26 |## Per-chip notes@@ -26,4 +26,11 @@ Before working on a chip, look here and in [docs/mac-engine.md](docs/mac-engine.With it, test set (3 alternated rounds, median): 1024² 49.0 → 39.6 s, 1344×768 48.3 → 39.9 s, edit 51.6 → 43.0 s.- With the release fixed, `bench` still shows `KOKON_NA=0` (3.92 s per step) and `KOKON_ATTENTION_DIRECT=0` (3.86 s)ahead of the default (4.08 s): one run each. Direct attention is off on every chip since 0.1.1.-- `KOKON_ANE_INT8=1` works here (1.7 · 10⁻²).+- `KOKON_ANE_INT8=1` works here (1.7 · 10⁻²; 1.6 · 10⁻² on macOS 27.2).+- The GPU's products run on the neural accelerators with MPS as with `na_matmul` (~32 TFLOP/s fp16 either way, MLX+ 28–31). `na_matmul` was behind on long K and many rows with 4 SIMD groups per tile; with 2 (`KOKON_NA_SG2`) a step+ at 1024² 4.21 → 3.86 s, level with MPS (3.89), test set 1024² 37.2 → 34.4 s (3 alternated rounds, macOS 27.2).+- On macOS 27.2 the late signal is still measured here (`KOKON_FLUSH_READY` selected), unlike the other Macs on 27.+- The balancer keeps about 2 600 of 4 096 rows on the GPU, 1 280 on the Neural Engine and 192–256 on the AMX, with+ either tile: its remembered shares do not tell the GPU paths apart, so whether a faster GPU should take more rows+ is not measured yet.diff --git a/engine-mac/docs/mac-engine.md b/engine-mac/docs/mac-engine.mdindex 7f10b02..9673388 100644--- a/engine-mac/docs/mac-engine.md+++ b/engine-mac/docs/mac-engine.md@@ -1275,6 +1275,7 @@ any Mac.**| Path | Chosen | M2 Pro result | Switch ||---|---|---|---|| **GPU rows on Metal 4 tensor operations** (`na_matmul`, MetalPerformancePrimitives' `matmul2d`): on the M5 the GPU cores' matrix units, before it the shader cores | wherever Metal 4 runs | 6.3 vs MPS 5.6 TFLOP/s, bit for bit the same; an evaluation 7.57 → 7.48 s | `KOKON_NA=0` |+| **2 SIMD groups per `na_matmul` tile** (4 elsewhere): with 4 the MLP's out (K 12 288) and 4 096-row products ran 20–26 % below MPS on the M5 Pro's neural accelerators; with 2 every DiT shape is at their ~32 TFLOP/s fp16 ceiling (MPS's and MLX's too) | M5 Pro only | not selected (M5 Pro: a step at 1024² 4.21 → 3.86 s, 1024² image 37.2 → 34.4 s, the same result to the bit) | `KOKON_NA_SG2=0/1` || **Attention reading K/V from memory** (`flash_attention_direct`) | off on every chip (since 0.1.1) | slower on every Mac measured (M2 Pro 142 ms vs 91; M3 Max, M4 Pro, M4 Max, M5 Pro: 8–18 % per step) | `KOKON_ATTENTION_DIRECT=1` || **int8 products on the Neural Engine** (libnnc's M3/M4 path) | opt-in on every chip | a tile 10.1 → 7.0 ms, but 15–50× the error | `KOKON_ANE_INT8=1` || **Each product released at once** (`Coexecutor.release`: a wait on `ready` right after its signal, so the CPU sees it before the GPU's own rows) | where signals arrive late, measured at every start (`signalArrivesLate`: M5 Pro on macOS 26.6, M2 Max on 26.2) | M5 Pro: an evaluation 5.50–5.89 → 4.44–4.46 s; where on time it costs ~2.5 % (M2 Pro, macOS 27.2) | `KOKON_FLUSH_READY=0/1` |@@ -1453,6 +1454,7 @@ All are environment variables read once at start. Defaults are what users run.| `KOKON_ANE_ATTENTION` | off | `1`: Neural Engine heads too || `KOKON_ANE_INT8` | off | `1`: int8 weights and rows on the Neural Engine || `KOKON_NA` | on with Metal 4 | `0`: MPS for the GPU's rows; `1`: force Metal 4 tensor ops |+| `KOKON_NA_SG2` | on the M5 Pro | `0`/`1`: 2 SIMD groups per `na_matmul` tile instead of 4 || `KOKON_WEIGHTS` | fp16 | `fp32`: unpack weights to fp32 (checks) || `KOKON_ACTIVATIONS` | fp16 | `fp32`: fp32 inputs to the GPU's and AMX's products (checks) || `KOKON_ATTENTION` | fp16 packed kernel | `fp32`: fp32 kernel; `graph`: MPSGraph; `unpacked`: no K/V packing |