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: 2 SIMD groups per tile on every M5, chosen by GPU family

Gabriele Lozupone200f3d5feb53
diff --git a/engine-mac/README.md b/engine-mac/README.md
index 85d1ff4..71b8de5 100644
--- a/engine-mac/README.md
+++ b/engine-mac/README.md
@@ -442,13 +442,15 @@ correctness test that runs on any Mac (`PerChipTests`). Only the M2 Pro has meas
`time-dit` third evaluation 3.64 → 2.99 s; test set 1024² 33.3 → 28.9 s, 1344×768 33.7 → 27.7 s, edit 36.2 →
31.3 s, faster in every round; bench velocity error 4.4 · 10⁻⁴ (3.0 · 10⁻⁴ with it off in the same run, 4.1 ·
10⁻⁴ the run before). 2 SIMD groups of 32 rows, 8 of 8 rows or tiles of 32 keys were 1.7–3× slower.
-- **2 SIMD groups per `na_matmul` tile on the M5 Pro** (`KOKON_NA_SG2=0` off, `=1` on elsewhere). With 4, the MLP's
+- **2 SIMD groups per `na_matmul` tile on the M5's neural accelerators** (Apple10 GPUs, `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
+ 37.2 → 34.4 s, 1344×768 37.5 → 34.9 s, edit 40.2 → 39.0 s; velocity error 4.1 · 10⁻⁴ both. Chosen by capability,
+ as the attention below: the M5 and M5 Max have the same accelerators per core (not measured there). On the M2 Pro's
+ shader cores 2 are 7–10 % slower on every DiT shape (2048 × 12288 × 4096 32.8 → 35.9 ms), so M1–M4 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 on
every chip, as their error does not depend on the chip. M2 Pro: one 1792 × 4096 × 4096 tile 10.1 ms in fp16, 7.0 ms
diff --git a/engine-mac/Sources/KokonEngine/Bench.swift b/engine-mac/Sources/KokonEngine/Bench.swift
index cffab33..047dad9 100644
--- a/engine-mac/Sources/KokonEngine/Bench.swift
+++ b/engine-mac/Sources/KokonEngine/Bench.swift
@@ -27,8 +27,8 @@ 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 NeuralAccelerators.simdgroups(device: device) == 2 {
+ paths.append(("those products on 2 SIMD groups per tile (M5)", "KOKON_NA_SG2", "0"))
}
}
if NeuralAttention.selected(device: device), NeuralAttention.make(device: device) != nil {
diff --git a/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift b/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift
index b8fcd84..952a44b 100644
--- a/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift
+++ b/engine-mac/Sources/KokonEngine/NeuralAccelerators.swift
@@ -22,20 +22,28 @@ public final class NeuralAccelerators: @unchecked Sendable {
struct Params { var m, n, k: UInt32 }
- /// **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
+ /// **2 SIMD groups per tile on the M5's neural accelerators.** 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 {
+ /// macOS 27.2, median of 12). Chosen by capability, as `NeuralAttention`: Apple10 GPUs (the M5, M5 Pro and M5
+ /// Max, the same accelerators per core; the M5 and M5 Max not measured). On the shader cores of older chips 2 are
+ /// slower: M2 Pro 19-core, every DiT shape 7–10 % (2048 × 12288 × 4096 32.8 → 35.9 ms, 4096³ 21.5 → 23.4), so they
+ /// keep 4. `KOKON_NA_SG2=0` / `=1`.
+ static func simdgroups(gpuFamily: Int, setting: String?) -> Int {
switch setting {
case "0": return 4
case "1": return 2
- default: return chip.contains("M5 Pro") ? 2 : 4
+ default: return gpuFamily >= 10 ? 2 : 4
}
}
+ /// This GPU's choice (`simdgroups(gpuFamily:setting:)`).
+ static func simdgroups(device: MTLDevice) -> Int {
+ simdgroups(gpuFamily: device.supportsFamily(.apple10) ? 10 : 0,
+ setting: ProcessInfo.processInfo.environment["KOKON_NA_SG2"])
+ }
+
/// 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? {
@@ -45,8 +53,7 @@ public final class NeuralAccelerators: @unchecked Sendable {
}
do {
let library = try metal4Library(device: device, file: "na_matmul.metal")
- let groups = simdgroups ?? NeuralAccelerators.simdgroups(chip: Chip.current.name,
- setting: ProcessInfo.processInfo.environment["KOKON_NA_SG2"])
+ let groups = simdgroups ?? NeuralAccelerators.simdgroups(device: device)
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)
diff --git a/engine-mac/Tests/KokonEngineTests/PerChipTests.swift b/engine-mac/Tests/KokonEngineTests/PerChipTests.swift
index 83e5e1f..d7b6e4b 100644
--- a/engine-mac/Tests/KokonEngineTests/PerChipTests.swift
+++ b/engine-mac/Tests/KokonEngineTests/PerChipTests.swift
@@ -48,17 +48,17 @@ final class PerChipTests: XCTestCase {
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]) }
+ // On the M5 Pro and the M2 Pro the two were the same to the bit; elsewhere only the error above is required.
+ if device.supportsFamily(.apple10) { XCTAssertEqual(results[0], results[1]) }
}
- /// Which chips get 2 SIMD groups per tile: the M5 Pro only, unless the setting says otherwise.
+ /// Which GPUs get 2 SIMD groups per tile: Apple10 (the M5's), 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)
+ XCTAssertEqual(NeuralAccelerators.simdgroups(gpuFamily: 10, setting: nil), 2)
+ XCTAssertEqual(NeuralAccelerators.simdgroups(gpuFamily: 10, setting: "0"), 4)
+ for family in [7, 8, 9] { // M1–M4
+ XCTAssertEqual(NeuralAccelerators.simdgroups(gpuFamily: family, setting: nil), 4, "Apple\(family)")
+ XCTAssertEqual(NeuralAccelerators.simdgroups(gpuFamily: family, setting: "1"), 2, "Apple\(family)")
}
}
diff --git a/engine-mac/docs/mac-engine.md b/engine-mac/docs/mac-engine.md
index 59ad84d..25d39b6 100644
--- a/engine-mac/docs/mac-engine.md
+++ b/engine-mac/docs/mac-engine.md
@@ -1276,7 +1276,7 @@ any Mac.**
|---|---|---|---|
| **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` |
| **The target's attention on Metal 4 tensor operations** (`na_flash_attention`: Q·Kᵀ and P·V with MPP's `matmul2d`, the softmax on its row reductions; same packed K and V and the same fp16 arithmetic as `flash_attention_packed`) | GPUs of the Apple10 family with Metal 4 (the M5's neural accelerators), by capability | not selected (M5 Pro: the attention at 1024² 60.0 → 28.1 ms, the same error; a step 3.64 → 2.99 s, 1024² image 33.3 → 28.9 s) | `KOKON_NA_ATTENTION=0/1` |
-| **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` |
+| **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) | Apple10 GPUs (M5, M5 Pro, M5 Max) | not selected; forced, 7–10 % slower on every DiT shape (2048 × 12288 × 4096 32.8 → 35.9 ms) (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` |
@@ -1456,7 +1456,7 @@ All are environment variables read once at start. Defaults are what users run.
| `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_ATTENTION` | on Apple10 GPUs with Metal 4 | `0`: the engine's packed attention; `1`: force it on any Metal 4 GPU |
-| `KOKON_NA_SG2` | on the M5 Pro | `0`/`1`: 2 SIMD groups per `na_matmul` tile instead of 4 |
+| `KOKON_NA_SG2` | on Apple10 GPUs (the M5's) | `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 |