vyre-libs 0.6.3

vyre Category A library ecosystem - pure-IR compositions over vyre-ops hardware primitives
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! FFT-based circular convolution for complex F32 vectors.
//!
//! This is the H2 composition layer over the verified radix-2 FFT:
//! `FFT(a)`, `FFT(b)`, pointwise complex multiply, then inverse FFT
//! via `conj(FFT(conj(x))) / n`. Inputs and output are interleaved
//! complex buffers `[re0, im0, re1, im1, ...]` with length `2 * n`.

use std::sync::Arc;

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

use super::common::validate_complex_len;
use super::fft_radix2_complex;
use crate::region::wrap_anonymous;

const OP_ID: &str = "vyre-libs::math::fft::fft_convolve_circular_complex";
const FFT_OP_ID: &str = "vyre-libs::math::fft::fft_radix2";
const MULTIPLY_OP_ID: &str = "vyre-libs::math::fft::pointwise_complex_multiply_conjugate";
const SCALE_OP_ID: &str = "vyre-libs::math::fft::scale_conjugate_inverse";

/// Build a Program that computes the length-`n` circular convolution
/// of two interleaved complex F32 buffers using FFTs.
///
/// `signal_freq`, `kernel_freq`, and `product_freq` are explicit
/// workgroup scratch buffers of length `2 * n`. Keeping scratch in
/// the Program's buffer table makes the memory footprint visible to
/// validation and the megakernel planner.
///
/// # Errors
///
/// Returns `Err` when `n` is not a power of two, `n < 2`, `2 * n`
/// overflows `u32`, or any buffer names alias.
pub fn fft_convolve_circular_complex(
    signal: &str,
    kernel: &str,
    signal_freq: &str,
    kernel_freq: &str,
    product_freq: &str,
    output: &str,
    n: u32,
) -> Result<Program, String> {
    validate_names(&[
        signal,
        kernel,
        signal_freq,
        kernel_freq,
        product_freq,
        output,
    ])?;
    let elements = validate_complex_len(n, "fft_convolve_circular_complex")?;

    // Each FFT body is a registered composition boundary. The Region keeps
    // its Let bindings scoped while letting Gate 1 and print-composition see
    // this as reuse of `fft_radix2`, not inline FFT reinvention.
    let mut entry = Vec::new();
    entry.push(fft_region(signal, signal_freq, n)?);
    entry.push(fft_region(kernel, kernel_freq, n)?);
    entry.push(Node::Region {
        generator: Ident::from(MULTIPLY_OP_ID),
        // source_region: Some(...) marks this as a child composition so
        // the structural-budget gate stops counting nodes once it
        // descends below this Region. The `multiply + conjugate` step
        // is a self-contained sub-op of the FFT-convolve composition.
        source_region: Some(GeneratorRef {
            name: MULTIPLY_OP_ID.to_string(),
        }),
        body: Arc::new(multiply_and_conjugate_body(
            signal_freq,
            kernel_freq,
            product_freq,
            n,
        )),
    });
    entry.push(fft_region(product_freq, output, n)?);
    entry.push(Node::Region {
        generator: Ident::from(SCALE_OP_ID),
        source_region: Some(GeneratorRef {
            name: SCALE_OP_ID.to_string(),
        }),
        body: Arc::new(scale_conjugate_body(output, n)),
    });

    Ok(Program::wrapped(
        vec![
            BufferDecl::storage(signal, 0, BufferAccess::ReadOnly, DataType::F32)
                .with_count(elements),
            BufferDecl::storage(kernel, 1, BufferAccess::ReadOnly, DataType::F32)
                .with_count(elements),
            BufferDecl::workgroup(signal_freq, elements, DataType::F32),
            BufferDecl::workgroup(kernel_freq, elements, DataType::F32),
            BufferDecl::workgroup(product_freq, elements, DataType::F32),
            BufferDecl::output(output, 2, DataType::F32).with_count(elements),
        ],
        [1, 1, 1],
        vec![wrap_anonymous(OP_ID, entry)],
    )
    .with_entry_op_id(OP_ID))
}

fn validate_names(names: &[&str]) -> Result<(), String> {
    for (idx, name) in names.iter().enumerate() {
        if name.is_empty() {
            return Err(format!(
                "Fix: buffer name at position {idx} must not be empty."
            ));
        }
        if names[..idx].iter().any(|seen| seen == name) {
            return Err(format!(
                "Fix: fft_convolve_circular_complex requires distinct buffer names; `{name}` is reused."
            ));
        }
    }
    Ok(())
}

fn fft_region(input: &str, output: &str, n: u32) -> Result<Node, String> {
    Ok(Node::Region {
        generator: Ident::from(FFT_OP_ID),
        source_region: Some(GeneratorRef {
            name: FFT_OP_ID.to_string(),
        }),
        body: Arc::new(fft_radix2_complex(input, output, n)?.into_entry_vec()),
    })
}

fn multiply_and_conjugate_body(
    signal_freq: &str,
    kernel_freq: &str,
    product_freq: &str,
    n: u32,
) -> Vec<Node> {
    let mut body = Vec::with_capacity(n as usize * 10);
    for k in 0..n {
        let base = 2 * k;
        let ar_name = format!("a_re_{k}");
        let ai_name = format!("a_im_{k}");
        let br_name = format!("b_re_{k}");
        let bi_name = format!("b_im_{k}");
        let prod_re_name = format!("prod_re_{k}");
        let prod_im_name = format!("prod_im_{k}");
        body.push(Node::let_bind(
            ar_name.clone(),
            Expr::load(signal_freq, Expr::u32(base)),
        ));
        body.push(Node::let_bind(
            ai_name.clone(),
            Expr::load(signal_freq, Expr::u32(base + 1)),
        ));
        body.push(Node::let_bind(
            br_name.clone(),
            Expr::load(kernel_freq, Expr::u32(base)),
        ));
        body.push(Node::let_bind(
            bi_name.clone(),
            Expr::load(kernel_freq, Expr::u32(base + 1)),
        ));
        let ar = Expr::var(ar_name);
        let ai = Expr::var(ai_name);
        let br = Expr::var(br_name);
        let bi = Expr::var(bi_name);
        body.push(Node::let_bind(
            prod_re_name.clone(),
            Expr::sub(
                Expr::mul(ar.clone(), br.clone()),
                Expr::mul(ai.clone(), bi.clone()),
            ),
        ));
        body.push(Node::let_bind(
            prod_im_name.clone(),
            Expr::add(Expr::mul(ar, bi), Expr::mul(ai, br)),
        ));
        body.push(Node::store(
            product_freq,
            Expr::u32(base),
            Expr::var(prod_re_name),
        ));
        body.push(Node::store(
            product_freq,
            Expr::u32(base + 1),
            Expr::negate(Expr::var(prod_im_name)),
        ));
    }
    body
}

fn scale_conjugate_body(output: &str, n: u32) -> Vec<Node> {
    scale_conjugate_body_from(output, output, n)
}

fn scale_conjugate_body_from(input: &str, output: &str, n: u32) -> Vec<Node> {
    let inv_n = Expr::f32(1.0 / n as f32);
    let zero_epsilon = Expr::f32(1.0e-6);
    let mut body = Vec::with_capacity(n as usize * 6);
    for k in 0..n {
        let base = 2 * k;
        let re_name = format!("ifft_re_{k}");
        let im_name = format!("ifft_im_{k}");
        let scaled_re_name = format!("ifft_scaled_re_{k}");
        let scaled_im_name = format!("ifft_scaled_im_{k}");
        body.push(Node::let_bind(
            re_name.clone(),
            Expr::load(input, Expr::u32(base)),
        ));
        body.push(Node::let_bind(
            im_name.clone(),
            Expr::load(input, Expr::u32(base + 1)),
        ));
        body.push(Node::let_bind(
            scaled_re_name.clone(),
            Expr::mul(Expr::var(re_name), inv_n.clone()),
        ));
        body.push(Node::let_bind(
            scaled_im_name.clone(),
            Expr::mul(Expr::negate(Expr::var(im_name)), inv_n.clone()),
        ));
        body.push(Node::store(
            output,
            Expr::u32(base),
            Expr::select(
                Expr::lt(
                    Expr::abs(Expr::var(scaled_re_name.clone())),
                    zero_epsilon.clone(),
                ),
                Expr::f32(0.0),
                Expr::var(scaled_re_name),
            ),
        ));
        body.push(Node::store(
            output,
            Expr::u32(base + 1),
            Expr::select(
                Expr::lt(
                    Expr::abs(Expr::var(scaled_im_name.clone())),
                    zero_epsilon.clone(),
                ),
                Expr::f32(0.0),
                Expr::var(scaled_im_name),
            ),
        ));
    }
    body
}

fn pointwise_complex_multiply_conjugate_program() -> Program {
    let n = 4;
    let elements = n * 2;
    Program::wrapped(
        vec![
            BufferDecl::storage("signal_freq", 0, BufferAccess::ReadOnly, DataType::F32)
                .with_count(elements),
            BufferDecl::storage("kernel_freq", 1, BufferAccess::ReadOnly, DataType::F32)
                .with_count(elements),
            BufferDecl::output("product_freq", 2, DataType::F32).with_count(elements),
        ],
        [1, 1, 1],
        vec![wrap_anonymous(
            MULTIPLY_OP_ID,
            multiply_and_conjugate_body("signal_freq", "kernel_freq", "product_freq", n),
        )],
    )
    .with_entry_op_id(MULTIPLY_OP_ID)
}

fn scale_conjugate_inverse_program() -> Program {
    let n = 4;
    let elements = n * 2;
    Program::wrapped(
        vec![
            BufferDecl::storage("product_freq", 0, BufferAccess::ReadOnly, DataType::F32)
                .with_count(elements),
            BufferDecl::output("output", 1, DataType::F32).with_count(elements),
        ],
        [1, 1, 1],
        vec![wrap_anonymous(
            SCALE_OP_ID,
            scale_conjugate_body_from("product_freq", "output", n),
        )],
    )
    .with_entry_op_id(SCALE_OP_ID)
}

fn fixture_f32_bytes(values: &[f32]) -> Vec<u8> {
    vyre_primitives::wire::pack_f32_slice(values)
}

fn pointwise_complex_multiply_conjugate_inputs() -> Vec<Vec<Vec<u8>>> {
    vec![vec![
        fixture_f32_bytes(&[1.0, 2.0, 3.0, 4.0, -1.0, 0.5, 0.0, -2.0]),
        fixture_f32_bytes(&[5.0, 6.0, -2.0, 1.0, 0.5, -1.0, 3.0, 0.0]),
    ]]
}

fn pointwise_complex_multiply_conjugate_expected() -> Vec<Vec<Vec<u8>>> {
    vec![vec![fixture_f32_bytes(&[
        -7.0, -16.0, -10.0, 5.0, 0.0, -1.25, 0.0, 6.0,
    ])]]
}

fn scale_conjugate_inverse_inputs() -> Vec<Vec<Vec<u8>>> {
    vec![vec![fixture_f32_bytes(&[
        4.0, -8.0, -12.0, 16.0, 0.0, 0.0, 2.0, -4.0,
    ])]]
}

fn scale_conjugate_inverse_expected() -> Vec<Vec<Vec<u8>>> {
    vec![vec![fixture_f32_bytes(&[
        1.0, 2.0, -3.0, -4.0, 0.0, 0.0, 0.5, 1.0,
    ])]]
}

inventory::submit! {
    crate::harness::OpEntry {
        id: MULTIPLY_OP_ID,
        build: pointwise_complex_multiply_conjugate_program,
        test_inputs: Some(pointwise_complex_multiply_conjugate_inputs),
        expected_output: Some(pointwise_complex_multiply_conjugate_expected),
        category: Some("math"),
    }
}

inventory::submit! {
    crate::harness::OpEntry {
        id: SCALE_OP_ID,
        build: scale_conjugate_inverse_program,
        test_inputs: Some(scale_conjugate_inverse_inputs),
        expected_output: Some(scale_conjugate_inverse_expected),
        category: Some("math"),
    }
}

inventory::submit! {
    crate::harness::OpEntry {
        id: OP_ID,
        build: || fft_convolve_circular_complex(
            "signal",
            "kernel",
            "signal_freq",
            "kernel_freq",
            "product_freq",
            "output",
            4,
        ).unwrap_or_else(|_| unreachable!("Fix: catalog fixture uses valid power-of-two buffers.")),
        test_inputs: Some(|| {
            vec![vec![
                crate::test_support::byte_pack::f32_bytes(&[1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0]),
                crate::test_support::byte_pack::f32_bytes(&[1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
            ]]
        }),
        expected_output: Some(|| {
            vec![vec![crate::test_support::byte_pack::f32_bytes(&[5.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0])]]
        }),
        category: Some("math"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::byte_pack::f32_bytes;
    use vyre_reference::value::Value;

    fn decode(bytes: &[u8]) -> Vec<f32> {
        bytes
            .chunks_exact(4)
            .map(|c| f32::from_le_bytes(c.try_into().unwrap()))
            .collect()
    }

    fn run(signal: &[f32], kernel: &[f32], n: u32) -> Vec<f32> {
        let prog = fft_convolve_circular_complex(
            "signal",
            "kernel",
            "signal_freq",
            "kernel_freq",
            "product_freq",
            "output",
            n,
        )
        .expect("Fix: build");
        let byte_len = (2 * n as usize) * 4;
        let outputs = vyre_reference::reference_eval(
            &prog,
            &[
                Value::from(f32_bytes(signal)),
                Value::from(f32_bytes(kernel)),
                Value::from(vec![0u8; byte_len]),
            ],
        )
        .expect("Fix: fft_convolve_circular_complex must execute in the reference interpreter.");
        decode(
            &outputs
                .last()
                .expect("Fix: output buffer must be returned after scratch buffers.")
                .to_bytes(),
        )
    }

    fn naive_circular_complex(signal: &[f32], kernel: &[f32], n: usize) -> Vec<f32> {
        let mut out = vec![0.0_f32; 2 * n];
        for k in 0..n {
            let mut re = 0.0_f32;
            let mut im = 0.0_f32;
            for j in 0..n {
                let rhs = (k + n - j) % n;
                let ar = signal[2 * j];
                let ai = signal[2 * j + 1];
                let br = kernel[2 * rhs];
                let bi = kernel[2 * rhs + 1];
                re += ar * br - ai * bi;
                im += ar * bi + ai * br;
            }
            out[2 * k] = re;
            out[2 * k + 1] = im;
        }
        out
    }

    #[test]
    fn fft_convolve_circular_real_fixture_matches_reference() {
        let signal = [1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0];
        let kernel = [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        let actual = run(&signal, &kernel, 4);
        let expected = naive_circular_complex(&signal, &kernel, 4);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!((a - e).abs() <= 1.0e-4, "lane {i}: {a} != {e}");
        }
    }

    #[test]
    fn fft_convolve_circular_complex_fixture_matches_reference() {
        let signal = [1.0, 1.0, 0.0, -1.0, 2.0, 0.5, -3.0, 0.25];
        let kernel = [0.5, -1.0, 2.0, 0.0, -1.0, 0.5, 0.25, 1.0];
        let actual = run(&signal, &kernel, 4);
        let expected = naive_circular_complex(&signal, &kernel, 4);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!((a - e).abs() <= 1.0e-4, "lane {i}: {a} != {e}");
        }
    }

    #[test]
    fn fft_convolve_rejects_non_power_of_two() {
        let err = fft_convolve_circular_complex("a", "b", "af", "bf", "pf", "out", 6)
            .expect_err("non-power-of-two must fail");
        assert!(err.contains("power of two"));
    }

    #[test]
    fn fft_convolve_rejects_aliasing_buffers() {
        let err = fft_convolve_circular_complex("a", "b", "af", "bf", "af", "out", 4)
            .expect_err("duplicate scratch name must fail");
        assert!(err.contains("distinct buffer names"));
    }

    // ------------------------------------------------------------------
    // Adversarial fixtures exposing real gaps
    // ------------------------------------------------------------------

    /// NaN in the signal must propagate to the convolution output.
    #[test]
    fn fft_convolve_nan_input_propagates() {
        let mut signal = vec![0.0_f32; 8];
        signal[0] = f32::NAN;
        let kernel = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        let actual = run(&signal, &kernel, 4);
        assert!(
            actual.iter().any(|&v| v.is_nan()),
            "convolution with NaN signal must produce at least one NaN component"
        );
    }

    /// Inf in the signal propagates, but because the complex multiply
    /// includes `Inf * 0 = NaN` in the imaginary part, the output becomes
    /// NaN rather than pure Inf. This is IEEE-754 correct but adversarial.
    #[test]
    fn fft_convolve_inf_input_produces_nan_or_inf() {
        let mut signal = vec![0.0_f32; 8];
        signal[0] = f32::INFINITY;
        let kernel = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        let actual = run(&signal, &kernel, 4);
        assert!(
            actual.iter().any(|&v| v.is_nan() || v.is_infinite()),
            "convolution with Inf signal must produce NaN or Inf; got {:?}",
            actual
        );
    }
}