cortiq-engine 0.8.32

Portable inference runtime for the CMF model format, with no ML framework underneath: runs on CPU, and on GPU (Vulkan / Metal / DX12) with the `gpu` feature; tokenizer, chat templates and dynamic per-skill weight overlay.
Documentation
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! CPU forward-pass primitives: RMS-norm, SiLU, sparse SwiGLU FFN.

use crate::pool::Pool;
use cortiq_core::types::NormStyle;

/// SiLU activation function.
#[inline(always)]
/// tanh-approximated GELU (Gemma's GeGLU gate; HF `gelu_pytorch_tanh`).
/// Exact (erf) GELU, `0.5·x·(1 + erf(x/√2))` — HF `hidden_act = "gelu"`.
/// erf by Abramowitz–Stegun 7.1.26 (|error| ≤ 1.5e-7), the same formula the
/// GPU kernels use (WGSL and MSL have no erf).
#[inline]
pub fn gelu_erf(x: f32) -> f32 {
    0.5 * x * (1.0 + erf_f32(x * std::f32::consts::FRAC_1_SQRT_2))
}

/// erf(x) by Abramowitz–Stegun 7.1.26, |error| ≤ 1.5e-7.
#[inline]
pub fn erf_f32(x: f32) -> f32 {
    let a = x.abs();
    let t = 1.0 / (1.0 + 0.327_591_1 * a);
    let y = 1.0
        - (((((1.061_405_4 * t - 1.453_152_1) * t + 1.421_413_8) * t - 0.284_496_74) * t
            + 0.254_829_6)
            * t)
            * (-a * a).exp();
    if x < 0.0 { -y } else { y }
}

pub fn gelu_tanh(x: f32) -> f32 {
    const C: f32 = 0.797_884_6; // √(2/π)
    0.5 * x * (1.0 + (C * (x + 0.044_715 * x * x * x)).tanh())
}

pub fn silu(x: f32) -> f32 {
    x / (1.0 + (-x).exp())
}

/// MiniMax M3's SwiGLU-OAI gated activation.
///
/// The upstream contract is deliberately not ordinary `silu(gate) * up`:
///
/// ```text
/// g = min(gate, limit)                // only an upper clamp
/// u = clamp(up, -limit, limit)
/// out = g * sigmoid(alpha * g) * (u + beta)
/// ```
///
/// MiniMax M3.1 uses `alpha = 1.702`, `beta = 1.0`, and `limit = 7.0`.
/// `None` is useful for source variants which explicitly omit the clamp.
/// Keeping this scalar reference here lets all execution paths share the
/// exact asymmetric clamp rule before an accelerator kernel is admitted.
#[inline]
pub fn swiglu_oai(gate: f32, up: f32, alpha: f32, beta: f32, limit: Option<f32>) -> f32 {
    let (gate, up) = match limit {
        Some(limit) => (gate.min(limit), up.clamp(-limit, limit)),
        None => (gate, up),
    };
    gate * (1.0 / (1.0 + (-alpha * gate).exp())) * (up + beta)
}

/// RMS normalization with explicit weight semantics.
///
/// - `NormStyle::Qwen`  (Llama family): `x̂ · w`
/// - `NormStyle::Gemma`:                `x̂ · (1 + w)`
///
/// Applying the wrong style corrupts every normalization in the
/// forward pass — the style comes from the model arch, never guessed.
pub fn rms_norm(input: &[f32], weight: &[f32], eps: f64, style: NormStyle) -> Vec<f32> {
    let mut out = vec![0.0f32; input.len()];
    rms_norm_into(input, weight, eps, style, &mut out);
    out
}

/// `rms_norm` writing into a caller-owned buffer — the decode hot path
/// calls this twice per layer; the returning variant allocated each time.
pub fn rms_norm_into(input: &[f32], weight: &[f32], eps: f64, style: NormStyle, out: &mut [f32]) {
    let n = input.len();
    debug_assert_eq!(out.len(), n);
    let mean_sq: f64 = input.iter().map(|&x| (x as f64) * (x as f64)).sum::<f64>() / n as f64;
    let inv_rms = 1.0 / (mean_sq + eps).sqrt() as f32;
    match style {
        NormStyle::Qwen => {
            for (o, (&x, &w)) in out.iter_mut().zip(input.iter().zip(weight)) {
                *o = x * inv_rms * w;
            }
        }
        NormStyle::Gemma => {
            for (o, (&x, &w)) in out.iter_mut().zip(input.iter().zip(weight)) {
                *o = x * inv_rms * (1.0 + w);
            }
        }
    }
}

/// Fused residual addition and RMSNorm into a caller-owned buffer:
/// `h += delta`, then `out = rms_norm(h, weight)`.
/// This avoids a separate pass over `h` for the residual addition and sum of squares.
pub fn add_rmsnorm_fused_into(
    h: &mut [f32],
    delta: &[f32],
    weight: &[f32],
    eps: f64,
    style: NormStyle,
    out: &mut [f32],
) {
    let n = h.len();
    debug_assert_eq!(delta.len(), n);
    debug_assert_eq!(out.len(), n);
    let mut mean_sq = 0.0f64;
    for (hv, &dv) in h.iter_mut().zip(delta) {
        let val = *hv + dv;
        *hv = val;
        mean_sq += (val as f64) * (val as f64);
    }
    mean_sq /= n as f64;
    let inv_rms = 1.0 / (mean_sq + eps).sqrt() as f32;
    match style {
        NormStyle::Qwen => {
            for (o, (&x, &w)) in out.iter_mut().zip(h.iter().zip(weight)) {
                *o = x * inv_rms * w;
            }
        }
        NormStyle::Gemma => {
            for (o, (&x, &w)) in out.iter_mut().zip(h.iter().zip(weight)) {
                *o = x * inv_rms * (1.0 + w);
            }
        }
    }
}

/// Sparse SwiGLU FFN: compute only `active_indices` neurons.
///
/// `out = down_projᵀ[·, active] · (silu(gate_proj[active, ·]·h) ⊙ up_proj[active, ·]·h)`
///
/// With a full index list this is bit-identical to the dense path —
/// masking is an execution schedule, not an approximation.
pub fn sparse_ffn_forward(
    hidden_states: &[f32],
    gate_proj_full: &[f32], // [intermediate_size, hidden_size]
    up_proj_full: &[f32],   // [intermediate_size, hidden_size]
    down_proj_full: &[f32], // [hidden_size, intermediate_size]
    hidden_size: usize,
    intermediate_size: usize,
    active_indices: &[u16],
    pool: Option<&Pool>,
) -> Vec<f32> {
    let n_active = active_indices.len();
    if n_active == 0 {
        return vec![0.0; hidden_states.len()];
    }

    let seq_len = hidden_states.len() / hidden_size;
    let mut output = vec![0.0f32; seq_len * hidden_size];

    for s in 0..seq_len {
        let h = &hidden_states[s * hidden_size..(s + 1) * hidden_size];

        // Gather active rows of gate/up, fuse silu(gate)·up. Each active
        // neuron is independent → row-parallel, bit-identical to serial.
        let mut act = vec![0.0f32; n_active];
        let neuron_act = |ai: usize| -> f32 {
            let idx = active_indices[ai];
            let row = idx as usize * hidden_size;
            if row + hidden_size > gate_proj_full.len() {
                return 0.0;
            }
            let mut gate_sum = 0.0f32;
            let mut up_sum = 0.0f32;
            for k in 0..hidden_size {
                gate_sum += gate_proj_full[row + k] * h[k];
                up_sum += up_proj_full[row + k] * h[k];
            }
            silu(gate_sum) * up_sum
        };
        match pool {
            Some(pool) if n_active >= 256 => {
                let act_ptr = SendMut(act.as_mut_ptr());
                pool.run(&move |widx, n| {
                    let chunk = n_active.div_ceil(n);
                    let start = widx * chunk;
                    let end = (start + chunk).min(n_active);
                    for ai in start..end {
                        // SAFETY: disjoint index ranges per worker.
                        unsafe { *act_ptr.at(ai) = neuron_act(ai) };
                    }
                });
            }
            _ => {
                for (ai, dst) in act.iter_mut().enumerate() {
                    *dst = neuron_act(ai);
                }
            }
        }

        // Scatter through the active columns of down_proj.
        let out = &mut output[s * hidden_size..(s + 1) * hidden_size];
        for (ai, &idx) in active_indices.iter().enumerate() {
            let val = act[ai];
            if val.abs() < 1e-12 {
                continue;
            }
            for k in 0..hidden_size {
                out[k] += down_proj_full[k * intermediate_size + idx as usize] * val;
            }
        }
    }

    output
}

/// Fused two-position sparse FFN: gate/up/down weight rows are streamed
/// from memory ONCE for both positions. Bit-identical to two single
/// calls (per-position accumulation order is unchanged).
#[allow(clippy::too_many_arguments)]
pub fn sparse_ffn_forward_pair(
    h1: &[f32],
    h2: &[f32],
    gate_proj_full: &[f32],
    up_proj_full: &[f32],
    down_proj_full: &[f32],
    hidden_size: usize,
    intermediate_size: usize,
    active_indices: &[u16],
    pool: Option<&Pool>,
) -> (Vec<f32>, Vec<f32>) {
    let n_active = active_indices.len();
    let mut out1 = vec![0.0f32; hidden_size];
    let mut out2 = vec![0.0f32; hidden_size];
    if n_active == 0 {
        return (out1, out2);
    }

    let mut act1 = vec![0.0f32; n_active];
    let mut act2 = vec![0.0f32; n_active];
    let neuron_pair = |ai: usize| -> (f32, f32) {
        let idx = active_indices[ai];
        let row = idx as usize * hidden_size;
        if row + hidden_size > gate_proj_full.len() {
            return (0.0, 0.0);
        }
        let (mut g1, mut u1, mut g2, mut u2) = (0.0f32, 0.0f32, 0.0f32, 0.0f32);
        for k in 0..hidden_size {
            let gw = gate_proj_full[row + k];
            let uw = up_proj_full[row + k];
            g1 += gw * h1[k];
            g2 += gw * h2[k];
            u1 += uw * h1[k];
            u2 += uw * h2[k];
        }
        (silu(g1) * u1, silu(g2) * u2)
    };
    match pool {
        Some(pool) if n_active >= 256 => {
            let a1 = SendMut(act1.as_mut_ptr());
            let a2 = SendMut(act2.as_mut_ptr());
            pool.run(&move |widx, n| {
                let chunk = n_active.div_ceil(n);
                let start = widx * chunk;
                let end = (start + chunk).min(n_active);
                for ai in start..end {
                    let (v1, v2) = neuron_pair(ai);
                    // SAFETY: disjoint index ranges per worker.
                    unsafe {
                        *a1.at(ai) = v1;
                        *a2.at(ai) = v2;
                    }
                }
            });
        }
        _ => {
            for ai in 0..n_active {
                let (v1, v2) = neuron_pair(ai);
                act1[ai] = v1;
                act2[ai] = v2;
            }
        }
    }

    // Down scatter: one pass over the active columns for both positions.
    for (ai, &idx) in active_indices.iter().enumerate() {
        let (v1, v2) = (act1[ai], act2[ai]);
        if v1.abs() < 1e-12 && v2.abs() < 1e-12 {
            continue;
        }
        for k in 0..hidden_size {
            let dw = down_proj_full[k * intermediate_size + idx as usize];
            out1[k] += dw * v1;
            out2[k] += dw * v2;
        }
    }
    (out1, out2)
}

#[derive(Clone, Copy)]
struct SendMut(*mut f32);
unsafe impl Send for SendMut {}
unsafe impl Sync for SendMut {}

impl SendMut {
    /// See pool::SendMut::at — captures the Sync wrapper, not the field.
    #[inline]
    fn at(self, i: usize) -> *mut f32 {
        unsafe { self.0.add(i) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_silu() {
        assert!((silu(0.0) - 0.0).abs() < 1e-6);
        assert!((silu(1.0) - 0.7310586).abs() < 1e-4);
    }

    #[test]
    fn rms_norm_qwen_multiplies_by_w() {
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let weight = vec![1.0; 4]; // identity weight in Qwen semantics
        let out = rms_norm(&input, &weight, 1e-6, NormStyle::Qwen);
        let rms = (30.0_f64 / 4.0).sqrt() as f32;
        assert!((out[0] - 1.0 / rms).abs() < 1e-4);

        // w = 2 doubles the output — x̂·w, not x̂·(1+w).
        let out2 = rms_norm(&input, &[2.0; 4], 1e-6, NormStyle::Qwen);
        assert!((out2[0] - 2.0 / rms).abs() < 1e-4);
    }

    #[test]
    fn rms_norm_gemma_adds_one() {
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let weight = vec![0.0; 4]; // identity weight in Gemma semantics
        let out = rms_norm(&input, &weight, 1e-6, NormStyle::Gemma);
        let rms = (30.0_f64 / 4.0).sqrt() as f32;
        assert!((out[0] - 1.0 / rms).abs() < 1e-4);
    }

    #[test]
    fn minimax_gemma_1p_rms_norm_uses_weight_plus_one() {
        // MiniMax M3 applies Gemma-1p to its residual, Q/K and index-Q/K
        // norms. In particular a stored -1 weight must zero that lane; a
        // usual RMSNorm (`x-hat * weight`) would instead flip its sign.
        let input = [3.0f32, 4.0];
        let weight = [-1.0f32, 0.5];
        let expected_rstd = 1.0 / ((12.5f64 + 1e-6).sqrt() as f32);
        let expected = [0.0, 4.0 * expected_rstd * 1.5];

        let got = rms_norm(&input, &weight, 1e-6, NormStyle::Gemma);
        let mut into = [f32::NAN; 2];
        rms_norm_into(&input, &weight, 1e-6, NormStyle::Gemma, &mut into);

        for ((actual, scratch), want) in got.iter().zip(into).zip(expected) {
            assert!((actual - want).abs() < 1e-6, "{actual} != {want}");
            assert!((scratch - want).abs() < 1e-6, "{scratch} != {want}");
        }
    }

    #[test]
    fn minimax_swiglu_oai_has_asymmetric_gate_clamp() {
        const ALPHA: f32 = 1.702;
        const BETA: f32 = 1.0;
        const LIMIT: f32 = 7.0;

        let sigmoid = |x: f32| 1.0 / (1.0 + (-x).exp());
        let positive = swiglu_oai(10.0, 10.0, ALPHA, BETA, Some(LIMIT));
        let expected_positive = 7.0 * sigmoid(ALPHA * 7.0) * 8.0;
        assert!((positive - expected_positive).abs() < 1e-5);

        // The gate is *not* lower-clamped: only up is symmetric. This
        // catches the tempting but wrong `gate.clamp(-limit, limit)` port.
        let negative = swiglu_oai(-10.0, -10.0, ALPHA, BETA, Some(LIMIT));
        let expected_negative = -10.0 * sigmoid(-10.0 * ALPHA) * -6.0;
        assert!((negative - expected_negative).abs() < 1e-10);
        let symmetric_gate = -7.0 * sigmoid(-7.0 * ALPHA) * -6.0;
        assert!((negative - symmetric_gate).abs() > 1e-6);

        let unclamped = swiglu_oai(10.0, -10.0, ALPHA, BETA, None);
        let expected_unclamped = 10.0 * sigmoid(10.0 * ALPHA) * -9.0;
        assert!((unclamped - expected_unclamped).abs() < 1e-5);
    }

    #[test]
    fn test_sparse_ffn_full_active() {
        let hidden = 4;
        let inter = 8;
        let h = vec![1.0f32; hidden];
        let gate = vec![0.1f32; inter * hidden];
        let up = vec![0.1f32; inter * hidden];
        let down = vec![0.1f32; hidden * inter];
        let active: Vec<u16> = (0..inter as u16).collect();

        let out = sparse_ffn_forward(&h, &gate, &up, &down, hidden, inter, &active, None);
        assert_eq!(out.len(), hidden);
        assert!(out.iter().all(|&v| v.abs() > 1e-6));
    }

    #[test]
    fn test_sparse_ffn_half_active() {
        let hidden = 4;
        let inter = 8;
        let h = vec![1.0f32; hidden];
        let gate = vec![0.1f32; inter * hidden];
        let up = vec![0.1f32; inter * hidden];
        let down = vec![0.1f32; hidden * inter];

        let full: Vec<u16> = (0..inter as u16).collect();
        let half: Vec<u16> = (0..inter as u16 / 2).collect();

        let out_full = sparse_ffn_forward(&h, &gate, &up, &down, hidden, inter, &full, None);
        let out_half = sparse_ffn_forward(&h, &gate, &up, &down, hidden, inter, &half, None);

        let mag_full: f32 = out_full.iter().map(|v| v.abs()).sum();
        let mag_half: f32 = out_half.iter().map(|v| v.abs()).sum();
        assert!(mag_half < mag_full);
        assert!(mag_half > 0.0);
    }
}