vyre-primitives 0.6.1

Compositional primitives for vyre - marker types (always on) + Tier 2.5 LEGO substrate (feature-gated per domain).
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
//! Block-diagonal inverse for K-FAC natural gradient.
//!
//! Input is a block-diagonal matrix (vector of blocks each n×n),
//! output is the block-diagonal inverse.
//!
//! Fulfils the "otherwise dense solve" primitive fallback for natural
//! gradient optimization in `vyre-nn`.

use std::sync::Arc;

use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};

/// Canonical op id.
pub const OP_ID: &str = "vyre-primitives::math::kfac_block_inverse";

/// Block-diagonal inverse builder.
///
/// Inverts `num_blocks` matrices of size `n x n` in parallel.
/// Assumes matrices are non-singular and well-conditioned (e.g. PD Fisher blocks).
#[must_use]
pub fn kfac_block_inverse(
    blocks_out: &str,
    blocks_in: &str,
    scratch: &str,
    num_blocks: u32,
    n: u32,
) -> Program {
    if num_blocks == 0 {
        return crate::invalid_output_program(
            OP_ID,
            blocks_out,
            DataType::F32,
            "Fix: kfac_block_inverse requires num_blocks > 0, got 0.".to_string(),
        );
    }
    if n == 0 {
        return crate::invalid_output_program(
            OP_ID,
            blocks_out,
            DataType::F32,
            "Fix: kfac_block_inverse requires n > 0, got 0.".to_string(),
        );
    }
    let Some(block_cells) = n.checked_mul(n) else {
        return crate::invalid_output_program(
            OP_ID,
            blocks_out,
            DataType::F32,
            format!("Fix: kfac_block_inverse n*n overflows u32 for n={n}."),
        );
    };
    let Some(total_cells) = num_blocks.checked_mul(block_cells) else {
        return crate::invalid_output_program(
            OP_ID,
            blocks_out,
            DataType::F32,
            format!("Fix: kfac_block_inverse num_blocks*n*n overflows u32: {num_blocks}*{n}*{n}."),
        );
    };

    let t = Expr::InvocationId { axis: 0 };
    let n_expr = Expr::u32(n);

    // Each thread t handles one block
    let mut iter_body = Vec::new();

    let offset = |i: Expr, j: Expr| {
        Expr::add(
            Expr::mul(t.clone(), Expr::mul(n_expr.clone(), n_expr.clone())),
            Expr::add(Expr::mul(i, n_expr.clone()), j),
        )
    };

    // 1. Copy block to scratch and initialize blocks_out to identity
    iter_body.push(Node::loop_for(
        "i",
        Expr::u32(0),
        n_expr.clone(),
        vec![Node::loop_for(
            "j",
            Expr::u32(0),
            n_expr.clone(),
            vec![
                Node::let_bind("idx", offset(Expr::var("i"), Expr::var("j"))),
                Node::store(
                    scratch,
                    Expr::var("idx"),
                    Expr::load(blocks_in, Expr::var("idx")),
                ),
                Node::store(
                    blocks_out,
                    Expr::var("idx"),
                    Expr::select(
                        Expr::eq(Expr::var("i"), Expr::var("j")),
                        Expr::f32(1.0),
                        Expr::f32(0.0),
                    ),
                ),
            ],
        )],
    ));

    // 2. Gauss-Jordan Elimination (no pivoting, assumes PSD)
    iter_body.push(Node::loop_for(
        "i",
        Expr::u32(0),
        n_expr.clone(),
        vec![
            // pivot = scratch[i, i]
            Node::let_bind(
                "pivot",
                Expr::load(scratch, offset(Expr::var("i"), Expr::var("i"))),
            ),
            // scale row i
            Node::loop_for(
                "j",
                Expr::u32(0),
                n_expr.clone(),
                vec![
                    Node::let_bind("idx_ij", offset(Expr::var("i"), Expr::var("j"))),
                    Node::store(
                        scratch,
                        Expr::var("idx_ij"),
                        Expr::div(Expr::load(scratch, Expr::var("idx_ij")), Expr::var("pivot")),
                    ),
                    Node::store(
                        blocks_out,
                        Expr::var("idx_ij"),
                        Expr::div(
                            Expr::load(blocks_out, Expr::var("idx_ij")),
                            Expr::var("pivot"),
                        ),
                    ),
                ],
            ),
            // eliminate other rows
            Node::loop_for(
                "k",
                Expr::u32(0),
                n_expr.clone(),
                vec![Node::if_then(
                    Expr::ne(Expr::var("k"), Expr::var("i")),
                    vec![
                        Node::let_bind(
                            "factor",
                            Expr::load(scratch, offset(Expr::var("k"), Expr::var("i"))),
                        ),
                        Node::loop_for(
                            "jj",
                            Expr::u32(0),
                            n_expr.clone(),
                            vec![
                                Node::let_bind("idx_kj", offset(Expr::var("k"), Expr::var("jj"))),
                                Node::let_bind("idx_ij", offset(Expr::var("i"), Expr::var("jj"))),
                                Node::store(
                                    scratch,
                                    Expr::var("idx_kj"),
                                    Expr::sub(
                                        Expr::load(scratch, Expr::var("idx_kj")),
                                        Expr::mul(
                                            Expr::var("factor"),
                                            Expr::load(scratch, Expr::var("idx_ij")),
                                        ),
                                    ),
                                ),
                                Node::store(
                                    blocks_out,
                                    Expr::var("idx_kj"),
                                    Expr::sub(
                                        Expr::load(blocks_out, Expr::var("idx_kj")),
                                        Expr::mul(
                                            Expr::var("factor"),
                                            Expr::load(blocks_out, Expr::var("idx_ij")),
                                        ),
                                    ),
                                ),
                            ],
                        ),
                    ],
                )],
            ),
        ],
    ));

    let entry = vec![Node::if_then(
        Expr::lt(t.clone(), Expr::u32(num_blocks)),
        iter_body,
    )];

    Program::wrapped(
        vec![
            BufferDecl::storage(blocks_out, 0, BufferAccess::ReadWrite, DataType::F32)
                .with_count(total_cells),
            BufferDecl::storage(blocks_in, 1, BufferAccess::ReadOnly, DataType::F32)
                .with_count(total_cells),
            BufferDecl::storage(scratch, 2, BufferAccess::ReadWrite, DataType::F32)
                .with_count(total_cells),
        ],
        [256, 1, 1],
        vec![Node::Region {
            generator: Ident::from(OP_ID),
            source_region: None,
            body: Arc::new(entry),
        }],
    )
}

/// CPU reference.
#[cfg(any(test, feature = "cpu-parity"))]
#[must_use]
pub fn cpu_ref(blocks_in: &[f32], num_blocks: u32, n: u32) -> Vec<f32> {
    let n = n as usize;
    let mut out = Vec::new();
    let mut mat = Vec::new();
    let mut inv = Vec::new();
    cpu_ref_into(
        blocks_in, num_blocks, n as u32, &mut out, &mut mat, &mut inv,
    );
    out
}

/// CPU reference using caller-owned output and per-block scratch buffers.
///
/// Uses flat `n*n` scratch matrices rather than allocating nested vectors for
/// every block. `out` is overwritten with one inverse block per input block.
#[cfg(any(test, feature = "cpu-parity"))]
pub fn cpu_ref_into(
    blocks_in: &[f32],
    num_blocks: u32,
    n: u32,
    out: &mut Vec<f32>,
    mat: &mut Vec<f32>,
    inv: &mut Vec<f32>,
) {
    let n = n as usize;
    out.clear();
    out.resize(blocks_in.len(), 0.0);
    let Some(block_cells) = n.checked_mul(n) else {
        panic!(
            "kfac_block_inverse CPU oracle n={n} overflows block cell count. Fix: shard K-FAC blocks before parity comparison."
        );
    };
    mat.clear();
    mat.resize(block_cells, 0.0);
    inv.clear();
    inv.resize(block_cells, 0.0);
    for b in 0..num_blocks as usize {
        let block_offset = b * block_cells;
        for i in 0..n {
            for j in 0..n {
                let idx = i * n + j;
                mat[idx] = blocks_in[block_offset + idx];
                inv[idx] = if i == j { 1.0 } else { 0.0 };
            }
        }
        // Gauss-Jordan
        for i in 0..n {
            let pivot = mat[i * n + i];
            for j in 0..n {
                mat[i * n + j] /= pivot;
                inv[i * n + j] /= pivot;
            }
            for k in 0..n {
                if k != i {
                    let factor = mat[k * n + i];
                    for j in 0..n {
                        mat[k * n + j] -= factor * mat[i * n + j];
                        inv[k * n + j] -= factor * inv[i * n + j];
                    }
                }
            }
        }
        for i in 0..n {
            for j in 0..n {
                let idx = i * n + j;
                out[block_offset + idx] = inv[idx];
            }
        }
    }
}

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

    #[test]
    fn test_cpu_ref_1x1() {
        let blocks_in = vec![2.0];
        let out = cpu_ref(&blocks_in, 1, 1);
        assert_eq!(out, vec![0.5]);
    }

    #[test]
    fn test_cpu_ref_multi_block() {
        let blocks_in = vec![2.0, 0.0, 0.0, 2.0, 4.0, 0.0, 0.0, 4.0];
        let out = cpu_ref(&blocks_in, 2, 2);
        assert_eq!(out, vec![0.5, 0.0, 0.0, 0.5, 0.25, 0.0, 0.0, 0.25]);
    }

    #[test]
    fn test_cpu_ref_3x3_diag() {
        let blocks_in = vec![1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 4.0];
        let out = cpu_ref(&blocks_in, 1, 3);
        assert_eq!(out, vec![1.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.25]);
    }

    #[test]
    fn test_cpu_ref_large_blocks() {
        let n = 2;
        let num_blocks = 64;
        let mut blocks_in = vec![0.0; num_blocks * n * n];
        for b in 0..num_blocks {
            blocks_in[b * 4] = 2.0;
            blocks_in[b * 4 + 3] = 2.0;
        }
        let out = cpu_ref(&blocks_in, num_blocks as u32, n as u32);
        assert_eq!(out[0], 0.5);
        assert_eq!(out[out.len() - 1], 0.5);
    }

    #[test]
    fn test_cpu_ref_asymmetric_values() {
        let blocks_in = vec![2.0, 1.0, 1.0, 2.0];
        // det = 4 - 1 = 3. Inv = 1/3 * [[2, -1], [-1, 2]] = [[0.666, -0.333], [-0.333, 0.666]]
        let out = cpu_ref(&blocks_in, 1, 2);
        assert!((out[0] - 0.6666667).abs() < 1e-6);
        assert!((out[1] - (-0.3333333)).abs() < 1e-6);
    }

    #[test]
    fn cpu_ref_into_reuses_output_and_flat_scratch() {
        let blocks_in = vec![2.0, 0.0, 0.0, 2.0, 4.0, 0.0, 0.0, 4.0];
        let mut out = Vec::with_capacity(16);
        let mut mat = Vec::with_capacity(8);
        let mut inv = Vec::with_capacity(8);
        out.extend_from_slice(&[99.0; 12]);
        mat.extend_from_slice(&[77.0; 6]);
        inv.extend_from_slice(&[55.0; 6]);
        let out_capacity = out.capacity();
        let mat_capacity = mat.capacity();
        let inv_capacity = inv.capacity();

        cpu_ref_into(&blocks_in, 2, 2, &mut out, &mut mat, &mut inv);

        assert_eq!(out, vec![0.5, 0.0, 0.0, 0.5, 0.25, 0.0, 0.0, 0.25]);
        assert_eq!(mat.len(), 4);
        assert_eq!(inv.len(), 4);
        assert_eq!(out.capacity(), out_capacity);
        assert_eq!(mat.capacity(), mat_capacity);
        assert_eq!(inv.capacity(), inv_capacity);

        cpu_ref_into(&[2.0], 1, 1, &mut out, &mut mat, &mut inv);
        assert_eq!(out, vec![0.5]);
        assert_eq!(mat, vec![1.0]);
        assert_eq!(inv, vec![0.5]);
        assert_eq!(out.capacity(), out_capacity);
        assert_eq!(mat.capacity(), mat_capacity);
        assert_eq!(inv.capacity(), inv_capacity);
    }

    #[test]
    fn test_parity_2x2() {
        let blocks_in = vec![4.0, 0.0, 0.0, 2.0];
        let p = kfac_block_inverse("bo", "bi", "s", 1, 2);

        let expected_out = cpu_ref(&blocks_in, 1, 2);

        use vyre_reference::reference_eval;
        use vyre_reference::value::Value;

        let to_value = |data: &[f32]| {
            let bytes = crate::wire::pack_f32_slice(data);
            Value::Bytes(Arc::from(bytes))
        };

        let inputs = vec![
            to_value(&[0.0; 4]),  // bo
            to_value(&blocks_in), // bi
            to_value(&[0.0; 4]),  // s
        ];

        let results = reference_eval(&p, &inputs).expect("Fix: interpreter failed");
        let actual_bytes = results[0].to_bytes();
        let actual_out: Vec<f32> = actual_bytes
            .chunks_exact(4)
            .map(|c| f32::from_le_bytes(c.try_into().unwrap()))
            .collect();

        for (a, b) in actual_out.iter().zip(expected_out.iter()) {
            assert!((a - b).abs() < 1e-6);
        }
    }

    #[test]
    fn program_declares_three_buffers() {
        let p = kfac_block_inverse("bo", "bi", "s", 4, 4);
        assert_eq!(p.buffers().len(), 3);
    }

    #[test]
    fn rejects_zero_num_blocks_with_trap() {
        let p = kfac_block_inverse("bo", "bi", "s", 0, 4);
        assert!(p.stats().trap());
    }

    #[test]
    fn rejects_zero_n_with_trap() {
        let p = kfac_block_inverse("bo", "bi", "s", 4, 0);
        assert!(p.stats().trap());
    }
}