1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! MSL kernel sources for the **FLUX.2 VAE decoder** per-op f32 GPU primitives.
//!
//! These four element/structural kernels accelerate the pure-CPU VAE decode of
//! the FLUX.2 image model (the convs are ~60 % of the VAE FLOPs at 512×512, and
//! the CPU path is im2col-memory-bound). They are purely additive — no existing
//! kernel is touched — and each is parity-validated against the CPU reference in
//! `oxibonsai-image::vae` (`conv.rs`, `norm.rs`, `ops.rs`).
//!
//! All buffers are flat NCHW `[C, H, W]` f32 (batch 1), matching the CPU stack.
//!
//! | Kernel | Op | CPU reference |
//! |-------------------------|----|---------------|
//! | `im2col_f32` | im2col patch extraction `[rows, kH·kW·C_in]` in `(kH,kW,C_in)` order | `conv.rs::build_im2col` |
//! | `groupnorm_f32` | GroupNorm (32 groups, eps 1e-6, per-channel affine) | `norm.rs::forward_inplace` |
//! | `silu_f32` | element-wise `x / (1 + exp(-x))` | `ops.rs::silu_inplace` / `math::silu` |
//! | `upsample_nearest_f32` | nearest ×2 `[C,H,W] → [C,2H,2W]` | `ops.rs::upsample_nearest2x` |
//!
//! The `im2col_f32` output feeds the existing parity-clean `gemm_f32_simdgroup`
//! (the `(kH,kW,C_in)` element order is exactly the MLX conv-weight
//! `[C_out, kH, kW, C_in]` flattening, so the GEMM dot is correct with the
//! weight passed directly as `W = [N=C_out, K=kH·kW·C_in]`).
// ═══════════════════════════════════════════════════════════════════════════
// im2col (k≥3) — patch extraction in (kH, kW, C_in) order
// ═══════════════════════════════════════════════════════════════════════════
/// im2col patch extraction for a **tile of output rows**.
///
/// Produces `patches[local_row * patch_dim + j]` for output spatial rows
/// `[row_start, row_start + tile_rows)`, where `patch_dim = k·k·C_in` and the
/// element index `j` decomposes as `(kh·k + kw)·C_in + ci` — the **`(kh, kw,
/// ci)` order that matches the MLX conv weight `[C_out, kH, kW, C_in]`
/// flattening** (the critical correctness invariant, mirroring
/// `oxibonsai-image::vae::conv::build_im2col`). Zero padding: an out-of-range
/// (padded) source position contributes `0`.
///
/// Buffers:
/// - buffer(0) = `input` (f32, NCHW `[C_in, H·W]` == `input[ci*H*W + ih*W + iw]`)
/// - buffer(1) = `patches` (f32, `[tile_rows, patch_dim]`, written)
/// - buffer(2) = `c_in` (u32)
/// - buffer(3) = `h` (u32)
/// - buffer(4) = `w` (u32)
/// - buffer(5) = `k` (u32, kernel edge; square)
/// - buffer(6) = `pad` (u32)
/// - buffer(7) = `w_out` (u32)
/// - buffer(8) = `row_start` (u32, first output spatial index of this tile)
/// - buffer(9) = `n_elems` (u32, `tile_rows * patch_dim`, the grid bound)
///
/// Dispatch: `[ceil(n_elems / 256), 1, 1]` threadgroups, `[256, 1, 1]` threads
/// (one thread per patch element).
pub const MSL_IM2COL_F32: &str = r#"
#include <metal_stdlib>
using namespace metal;
kernel void im2col_f32(
device const float* input [[buffer(0)]],
device float* patches [[buffer(1)]],
constant uint& c_in [[buffer(2)]],
constant uint& h [[buffer(3)]],
constant uint& w [[buffer(4)]],
constant uint& k [[buffer(5)]],
constant uint& pad [[buffer(6)]],
constant uint& w_out [[buffer(7)]],
constant uint& row_start [[buffer(8)]],
constant uint& n_elems [[buffer(9)]],
uint gid [[thread_position_in_grid]])
{
if (gid >= n_elems) { return; }
const uint patch_dim = k * k * c_in;
const uint local_row = gid / patch_dim; // 0..tile_rows-1
const uint j = gid % patch_dim; // 0..patch_dim-1
// Output spatial position (global, across the full H_out*W_out plane).
const uint out_idx = row_start + local_row;
const uint oh = out_idx / w_out;
const uint ow = out_idx % w_out;
// Decompose j = (kh*k + kw)*c_in + ci → (kh, kw, ci).
const uint kc = k * c_in;
const uint kh = j / kc;
const uint rem = j % kc;
const uint kw = rem / c_in;
const uint ci = rem % c_in;
// Padded source coordinates (same geometry as the CPU build_im2col).
const uint ih_p = oh + kh;
const uint iw_p = ow + kw;
float v = 0.0f;
if (ih_p >= pad && ih_p < h + pad && iw_p >= pad && iw_p < w + pad) {
const uint ih = ih_p - pad;
const uint iw = iw_p - pad;
v = input[(ulong)ci * (ulong)h * (ulong)w + (ulong)ih * (ulong)w + (ulong)iw];
}
patches[gid] = v;
}
"#;
// ═══════════════════════════════════════════════════════════════════════════
// GroupNorm — 32 groups, eps 1e-6, per-channel affine (one threadgroup/group)
// ═══════════════════════════════════════════════════════════════════════════
/// PyTorch-compatible GroupNorm, in place on NCHW `[C, H·W]`.
///
/// One **threadgroup per group** (`num_groups`, 32 in the VAE). Each group spans
/// `gs = C / num_groups` contiguous channels × `hw` spatial positions; the
/// per-group population mean / variance are reduced over all `gs·hw` elements,
/// then `(x - mean) / sqrt(var + eps)` is scaled by the per-channel affine
/// `weight[c]` / `bias[c]`.
///
/// **Numerics.** The CPU reference (`norm.rs`) accumulates the mean and variance
/// in `f64`. Metal has no `double`, so this kernel uses **Kahan-compensated f32
/// summation** within each thread's strided slice, followed by a plain f32 tree
/// reduction of the 256 partials. Over the VAE's group sizes (up to `gs·hw =
/// 3·512·512 ≈ 786 k` elements) this keeps the relative error of the sum /
/// sum-of-squares at ~`1e-6`–`1e-7`, so the normalized output matches the f64
/// reference to ≪ `1e-4` (the parity gate), and the end-to-end `vae_parity`
/// cosine stays ≥ 0.999. The variance is the classic two-pass form
/// (`mean` first, then `Σ(x-mean)²`) to mirror `norm.rs` exactly.
///
/// Buffers:
/// - buffer(0) = `x` (f32, NCHW `[C, hw]`, read-write / in-place)
/// - buffer(1) = `weight` (f32, `[C]` per-channel scale)
/// - buffer(2) = `bias` (f32, `[C]` per-channel shift)
/// - buffer(3) = `channels` (u32, `C`)
/// - buffer(4) = `hw` (u32, `H·W`)
/// - buffer(5) = `num_groups` (u32)
/// - buffer(6) = `eps` (f32)
///
/// Dispatch: `[num_groups, 1, 1]` threadgroups, `[256, 1, 1]` threads.
pub const MSL_GROUPNORM_F32: &str = r#"
#include <metal_stdlib>
using namespace metal;
constant constexpr uint GN_THREADS = 256u;
kernel void groupnorm_f32(
device float* x [[buffer(0)]],
device const float* weight [[buffer(1)]],
device const float* bias [[buffer(2)]],
constant uint& channels [[buffer(3)]],
constant uint& hw [[buffer(4)]],
constant uint& num_groups [[buffer(5)]],
constant float& eps [[buffer(6)]],
uint ggid [[threadgroup_position_in_grid]],
uint lid [[thread_index_in_threadgroup]])
{
const uint g = ggid; // group index (one threadgroup/group)
if (g >= num_groups) { return; }
const uint gs = channels / num_groups; // channels per group
const ulong group_elems = (ulong)gs * (ulong)hw;
const ulong base = (ulong)(g * gs) * (ulong)hw;
threadgroup float red[GN_THREADS];
threadgroup float mean_sh;
threadgroup float inv_std_sh;
// ── Pass 1: Kahan-compensated partial sum over this thread's stride. ──
float sum = 0.0f;
float comp = 0.0f; // Kahan running compensation
for (ulong i = lid; i < group_elems; i += GN_THREADS) {
float val = x[base + i];
float y = val - comp;
float t = sum + y;
comp = (t - sum) - y;
sum = t;
}
red[lid] = sum;
threadgroup_barrier(mem_flags::mem_threadgroup);
// Tree reduction of the 256 partials (plain f32 — only 8 levels).
for (uint stride = GN_THREADS / 2u; stride > 0u; stride >>= 1u) {
if (lid < stride) { red[lid] += red[lid + stride]; }
threadgroup_barrier(mem_flags::mem_threadgroup);
}
if (lid == 0u) {
mean_sh = red[0] / (float)group_elems;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
const float mean = mean_sh;
// ── Pass 2: Kahan-compensated partial sum of (x - mean)^2. ──
float vsum = 0.0f;
float vcomp = 0.0f;
for (ulong i = lid; i < group_elems; i += GN_THREADS) {
float d = x[base + i] - mean;
float dv = d * d;
float y = dv - vcomp;
float t = vsum + y;
vcomp = (t - vsum) - y;
vsum = t;
}
red[lid] = vsum;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = GN_THREADS / 2u; stride > 0u; stride >>= 1u) {
if (lid < stride) { red[lid] += red[lid + stride]; }
threadgroup_barrier(mem_flags::mem_threadgroup);
}
if (lid == 0u) {
float var = red[0] / (float)group_elems;
inv_std_sh = 1.0f / sqrt(var + eps);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
const float inv_std = inv_std_sh;
// ── Pass 3: normalize + per-channel affine, in place. ──
// Element i (within the group) belongs to channel c0 + i/hw.
const uint c0 = g * gs;
for (ulong i = lid; i < group_elems; i += GN_THREADS) {
const uint ci = (uint)(i / (ulong)hw); // local channel 0..gs-1
const uint c = c0 + ci;
const float wgt = weight[c];
const float bia = bias[c];
x[base + i] = (x[base + i] - mean) * inv_std * wgt + bia;
}
}
"#;
// ═══════════════════════════════════════════════════════════════════════════
// SiLU — element-wise x / (1 + exp(-x))
// ═══════════════════════════════════════════════════════════════════════════
/// Element-wise SiLU (`x · sigmoid(x) = x / (1 + exp(-x))`), in place.
///
/// Mirrors `oxibonsai-image::math::silu` exactly. Buffers:
/// - buffer(0) = `x` (f32, read-write / in-place)
/// - buffer(1) = `n` (u32, element count)
///
/// Dispatch: `[ceil(n / 256), 1, 1]` threadgroups, `[256, 1, 1]` threads.
pub const MSL_SILU_F32: &str = r#"
#include <metal_stdlib>
using namespace metal;
kernel void silu_f32(
device float* x [[buffer(0)]],
constant uint& n [[buffer(1)]],
uint gid [[thread_position_in_grid]])
{
if (gid >= n) { return; }
const float v = x[gid];
x[gid] = v / (1.0f + exp(-v));
}
"#;
// ═══════════════════════════════════════════════════════════════════════════
// Upsample nearest ×2 — [C, H, W] → [C, 2H, 2W]
// ═══════════════════════════════════════════════════════════════════════════
/// Nearest-neighbour ×2 upsample of an NCHW buffer `[C, H, W] → [C, 2H, 2W]`
/// (each pixel repeated 2× along H and W). Mirrors
/// `oxibonsai-image::vae::ops::upsample_nearest2x`.
///
/// Buffers:
/// - buffer(0) = `input` (f32, `[C, H·W]`)
/// - buffer(1) = `output` (f32, `[C, 2H·2W]`, written)
/// - buffer(2) = `c` (u32)
/// - buffer(3) = `h` (u32)
/// - buffer(4) = `w` (u32)
/// - buffer(5) = `n_out` (u32, `C·2H·2W`, the grid bound)
///
/// Dispatch: `[ceil(n_out / 256), 1, 1]` threadgroups, `[256, 1, 1]` threads
/// (one thread per output element).
pub const MSL_UPSAMPLE_NEAREST_F32: &str = r#"
#include <metal_stdlib>
using namespace metal;
kernel void upsample_nearest_f32(
device const float* input [[buffer(0)]],
device float* output [[buffer(1)]],
constant uint& c [[buffer(2)]],
constant uint& h [[buffer(3)]],
constant uint& w [[buffer(4)]],
constant uint& n_out [[buffer(5)]],
uint gid [[thread_position_in_grid]])
{
if (gid >= n_out) { return; }
const uint w_out = w * 2u;
const uint hw_out = h * 2u * w_out; // output elements per channel
const uint ch = gid / hw_out;
const uint r = gid % hw_out;
const uint ho = r / w_out;
const uint wo = r % w_out;
const uint hh = ho / 2u;
const uint ww = wo / 2u;
output[gid] = input[(ulong)ch * (ulong)h * (ulong)w + (ulong)hh * (ulong)w + (ulong)ww];
}
"#;