NeuralAmpModeler-rs 3.0.0

An opinionated, high-performance Neural Amp Modeler (NAM) client and core implementation in Rust for Linux/PipeWire and CLAP plugins.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Fábio Henrique de Lima Silva (fhl.bsb@gmail.com) All rights reserved.

use super::*;

/// Verifies that a 1D Convolution acting as an "Identity Kernel"
/// exactly reproduces the input at the output, without any modification.
///
/// The pedagogical importance of this is enormous: we use it to ensure that
/// the lowest-level SIMD primitives are not transposing, corrupting,
/// or dropping channels when accessing and storing values in memory (SoA/AoS).
#[test]
fn test_conv1d_identity_kernel() {
    // Create a 4x4 weight matrix (flattened to 16 floats).
    // By filling only the main diagonal with 1.0, we create an "Identity Kernel".
    let mut raw_weights = vec![0.0f32; 16];
    for i in 0..4 {
        raw_weights[i * 4 + i] = 1.0;
    }
    let mut weights =
        AlignedVec::new(16, 0.0f32).expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::transpose_conv1d_interleaved_4wide(
        &raw_weights,
        &mut weights,
        4, // IN
        4, // OUT
        1, // K
    );

    // Instantiate 1D Convolution without bias and with dilation 1 (linear processing).
    let conv = Conv1d::<4, 4, 1> {
        weights,
        bias: AlignedVec::from_vec(vec![0.0; 4])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: false,
        dilation: 1,
    };

    // Simulate a layer buffer (input) with sequential values.
    let layer_buffer = vec![1.0, 2.0, 3.0, 4.0];
    let mut block = vec![0.0; 4];

    // Manually invoke the SIMD routine optimized for AVX2.
    // The unsafe block is necessary because we access hardware intrinsic primitives.
    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 0, 1);
    }

    // Since the kernel is identity, the output should be a bit-perfect copy of the input.
    assert_eq!(block, vec![1.0, 2.0, 3.0, 4.0]);
}

/// Verifies the *Bias* addition functionality in Conv1D.
///
/// A layer equipped with Identity matrix weights plus a constant bias
/// should merely translate the numeric axis of the processed values. This attests
/// to the correct use of FMA (Fused Multiply-Add) with the `do_bias` flag.
#[test]
fn test_conv1d_with_bias() {
    // Again, we use an identity kernel to isolate the Bias effect.
    let mut raw_weights = vec![0.0f32; 16];
    for i in 0..4 {
        raw_weights[i * 4 + i] = 1.0;
    }
    let mut weights =
        AlignedVec::new(16, 0.0f32).expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::transpose_conv1d_interleaved_4wide(
        &raw_weights,
        &mut weights,
        4, // IN
        4, // OUT
        1, // K
    );

    // Configure the layer with Bias of 0.5 on all output channels.
    let conv = Conv1d::<4, 4, 1> {
        weights,
        bias: AlignedVec::from_vec(vec![0.5; 4])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: true, // Enable bias vector addition.
        dilation: 1,
    };

    let layer_buffer = vec![1.0, 2.0, 3.0, 4.0];
    let mut block = vec![0.0; 4];

    // The SIMD engine executes Fused Multiply-Add (FMA): (input * 1.0) + 0.5.
    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 0, 1);
    }

    // Verify that each element was correctly translated by the bias value.
    assert_eq!(block, vec![1.5, 2.5, 3.5, 4.5]);
}

/// Tests the central concept of WaveNet: Dilated Convolutions.
///
/// Dilation inserts deterministic spacing in the historical data sampling.
/// Here we validate whether the mathematical offset in `Conv1d` correctly accesses
/// the delayed frames (with `dilation: 2`). This means skipping adjacent samples
/// and jumping windows within the Ring Buffer. The success of this test ensures the
/// correct construction of the architecture's overall *Receptive Field*.
#[test]
fn test_conv1d_dilation() {
    // Configure unit weights (1.0) to sum all inputs directly.
    let raw_weights = vec![1.0f32; 2 * 3 * 2];
    let mut weights =
        AlignedVec::new(24, 0.0f32).expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::transpose_conv1d_interleaved_4wide(
        &raw_weights,
        &mut weights,
        2, // IN
        2, // OUT
        3, // K
    );

    // Define dilation: 2. This will make the kernel "skip" one frame per tap.
    let conv = Conv1d::<2, 2, 3> {
        weights,
        bias: AlignedVec::from_vec(vec![0.0; 2])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: false,
        dilation: 2,
    };

    // Create a history of 6 frames (12 floats).
    // Layout: [F0, F1, F2, F3, F4, F5] -> [ (1,2), (10,20), (3,4), (30,40), (5,6), (0,0) ]
    let mut layer_buffer = vec![0.0; 6 * 2];
    layer_buffer[0] = 1.0;
    layer_buffer[1] = 2.0; // F0
    layer_buffer[2] = 10.0;
    layer_buffer[3] = 20.0; // F1 (Will be ignored by dilation 2)
    layer_buffer[4] = 3.0;
    layer_buffer[5] = 4.0; // F2
    layer_buffer[6] = 30.0;
    layer_buffer[7] = 40.0; // F3 (Will be ignored by dilation 2)
    layer_buffer[8] = 5.0;
    layer_buffer[9] = 6.0; // F4

    let mut block = vec![0.0; 2];

    // Process at frame index 4 (F4).
    // With K=3 and Dilation=2, the taps will be:
    // Tap 0: frame 4 - (2 * 2) = frame 0 -> [1.0, 2.0]
    // Tap 1: frame 4 - (2 * 1) = frame 2 -> [3.0, 4.0]
    // Tap 2: frame 4 - (2 * 0) = frame 4 -> [5.0, 6.0]
    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 4, 1);
    }

    // Expected total sum: 1+2 + 3+4 + 5+6 = 21.0.
    assert_eq!(block[0], 21.0);
    assert_eq!(block[1], 21.0);
}

/// Ensures that giant weights (100.0) multiplied by silence inputs (0.0)
/// will stay absolutely at zero, proving that the multiplication loop does not
/// introduce digital noise (*spontaneous DC offset*).
/// Then, activates *bias* mid-execution to prove that the runtime
/// switch is respected by the DSP primitive.
#[test]
fn test_conv1d_zero_input() {
    // Extremely high weights to test if any residual noise is amplified.
    let raw_weights = vec![100.0f32; 2 * 3 * 2];
    let mut weights =
        AlignedVec::new(24, 0.0f32).expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::transpose_conv1d_interleaved_4wide(
        &raw_weights,
        &mut weights,
        2, // IN
        2, // OUT
        3, // K
    );

    let mut conv = Conv1d::<2, 2, 3> {
        weights: weights.clone(),
        bias: AlignedVec::from_vec(vec![0.0; 2])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: false,
        dilation: 1,
    };

    // Buffer filled with zeros.
    let layer_buffer = vec![0.0; 4 * 2];
    let mut block = vec![0.0; 2];

    // First pass: Without bias, output should be absolute 0.0.
    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 2, 1);
    }

    assert_eq!(block, vec![0.0, 0.0]);

    // Second pass: Activate bias. The output should reflect exactly the injected bias.
    conv.do_bias = true;
    conv.bias = AlignedVec::from_vec(vec![7.5, 8.5])
        .expect("allocation should succeed for test-sized buffers");

    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 2, 1);
    }

    assert_eq!(block, vec![7.5, 8.5]);
}

/// Manual matrix cross-product test (Dot Product)
/// against an expected result pre-calculated by a scientist.
///
/// Provides absolute guarantee that the `process_block` routine optimized for
/// x86-64 hardware handles perfectly summations containing concurrent positive
/// and negative values and weights, validating the correctness of mathematical calculations.
#[test]
fn test_conv1d_known_output() {
    // Heterogeneous weight matrix to validate channel cross-product (Dot Product).
    // Structure: OUT=2, K=2, IN=2. Total 8 weights.
    let raw_weights = vec![
        0.5, 1.5, // out0, in0, k0 and k1
        1.0, 2.0, // out0, in1, k0 and k1
        -0.5, -1.5, // out1, in0, k0 and k1
        -1.0, -2.0, // out1, in1, k0 and k1
    ];
    let mut weights =
        AlignedVec::new(16, 0.0f32).expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::transpose_conv1d_interleaved_4wide(
        &raw_weights,
        &mut weights,
        2, // IN
        2, // OUT
        2, // K
    );

    let conv = Conv1d::<2, 2, 2> {
        weights,
        bias: AlignedVec::from_vec(vec![1.0, -1.0])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: true,
        dilation: 1,
    };

    // Layer buffer with 2 frames: F0=(2.0, 3.0), F1=(4.0, 5.0).
    let layer_buffer = vec![2.0, 3.0, 4.0, 5.0];
    let mut block = vec![0.0; 2];

    // Process at frame index 1 (F1).
    // out0 = bias[0] + dot(F0, w[out0,k0]) + dot(F1, w[out0,k1])
    //      = 1.0 + (2*0.5 + 3*1.0) + (4*1.5 + 5*2.0) = 1.0 + 4.0 + 16.0 = 21.0
    // out1 = bias[1] + dot(F0, w[out1,k0]) + dot(F1, w[out1,k1])
    //      = -1.0 + (2*-0.5 + 3*-1.0) + (4*-1.5 + 5*-2.0) = -1.0 - 4.0 - 16.0 = -21.0
    unsafe {
        conv.process_block::<crate::math::common::Avx2Math>(&layer_buffer, &mut block, 1, 1);
    }

    assert_eq!(block[0], 21.0);
    assert_eq!(block[1], -21.0);
}

#[test]
fn test_conv1d_ch8_wide_interleaving() {
    use crate::math::common::AlignedVec;

    const CH: usize = 8;
    const K: usize = 2;

    let mut raw = vec![0.0f32; CH * K * CH];

    for out_c in 0..CH {
        for k in 0..K {
            for in_c in 0..CH {
                let idx = (out_c * CH + in_c) * K + k;
                raw[idx] = (out_c + 1) as f32 * 0.5;
            }
        }
    }

    let mut weights = AlignedVec::new(CH * K * CH, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::layout::transpose_conv1d_interleaved_8wide(
        &raw,
        &mut weights,
        CH,
        CH,
        K,
    );

    let conv = Conv1d::<CH, CH, K> {
        weights: weights.clone(),
        bias: AlignedVec::from_vec(vec![0.1f32; CH])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: true,
        dilation: 1,
    };

    let mut layer_buffer = vec![0.0f32; CH * 10];
    layer_buffer.fill(1.0);

    let mut simd_out = vec![0.0f32; CH];
    unsafe {
        conv.process_single_frame::<crate::math::common::Avx2Math>(&layer_buffer, &mut simd_out, 5);
    }

    let mut scalar_out = [0.0f32; CH];
    for (oc, s) in scalar_out.iter_mut().enumerate() {
        let mut sum = 0.1; // bias
        for k in 0..K {
            let offset = (k as isize) + 1 - (K as isize);
            let in_idx = ((5isize + offset) as usize) * CH;
            for ic in 0..CH {
                let w_idx = k * CH * CH + ic * CH + oc;
                sum += layer_buffer[in_idx + ic] * weights[w_idx];
            }
        }
        *s = sum;
    }

    for oc in 0..CH {
        let diff = (simd_out[oc] - scalar_out[oc]).abs();
        assert!(
            diff < 1e-5,
            "CH=8 ch{} mismatch: simd={}, scalar={}, diff={}",
            oc,
            simd_out[oc],
            scalar_out[oc],
            diff
        );
    }
}

#[test]
fn test_conv1d_ch16_wide_interleaving() {
    use crate::math::common::AlignedVec;

    const CH: usize = 16;
    const K: usize = 2;

    let mut raw = vec![0.0f32; CH * K * CH];

    for out_c in 0..CH {
        for k in 0..K {
            for in_c in 0..CH {
                let idx = (out_c * CH + in_c) * K + k;
                raw[idx] = (out_c + 1) as f32 * 0.5;
            }
        }
    }

    let mut weights = AlignedVec::new(CH * K * CH, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::layout::transpose_conv1d_interleaved_16wide(
        &raw,
        &mut weights,
        CH,
        CH,
        K,
    );

    let conv = Conv1d::<CH, CH, K> {
        weights: weights.clone(),
        bias: AlignedVec::from_vec(vec![0.1f32; CH])
            .expect("allocation should succeed for test-sized buffers"),
        do_bias: true,
        dilation: 1,
    };

    let mut layer_buffer = vec![0.0f32; CH * 10];
    layer_buffer.fill(1.0);

    let mut simd_out = vec![0.0f32; CH];
    unsafe {
        conv.process_single_frame::<crate::math::common::Avx2Math>(&layer_buffer, &mut simd_out, 5);
    }

    let mut scalar_out = [0.0f32; CH];
    for (oc, s) in scalar_out.iter_mut().enumerate() {
        let mut sum = 0.1; // bias
        for k in 0..K {
            let offset = (k as isize) + 1 - (K as isize);
            let in_idx = ((5isize + offset) as usize) * CH;
            for ic in 0..CH {
                let w_idx = k * CH * CH + ic * CH + oc;
                sum += layer_buffer[in_idx + ic] * weights[w_idx];
            }
        }
        *s = sum;
    }

    for oc in 0..CH {
        let diff = (simd_out[oc] - scalar_out[oc]).abs();
        assert!(
            diff < 1e-5,
            "CH=16 ch{} mismatch: simd={}, scalar={}, diff={}",
            oc,
            simd_out[oc],
            scalar_out[oc],
            diff
        );
    }
}

#[test]
fn test_4wide_to_16wide_transpose_correctness() {
    use crate::math::common::AlignedVec;

    const CH: usize = 16;
    const K: usize = 2;

    let mut raw = vec![0.0f32; CH * K * CH];
    for out_c in 0..CH {
        for k in 0..K {
            for in_c in 0..CH {
                let idx = (out_c * CH + in_c) * K + k;
                raw[idx] = (out_c + 1) as f32 * 0.5 + (k as f32) * 0.1;
            }
        }
    }

    let padded_4 = (CH.div_ceil(4)) * 4 * CH * K;
    let mut weights_4wide = AlignedVec::new(padded_4, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::layout::transpose_conv1d_interleaved_4wide(
        &raw,
        &mut weights_4wide,
        CH,
        CH,
        K,
    );

    let mut weights_16wide_via_4to16 = AlignedVec::new(CH * K * CH, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::layout::transpose_4wide_to_16wide(
        &weights_4wide,
        &mut weights_16wide_via_4to16,
        CH,
        CH,
        K,
    );

    let mut weights_16wide_direct = AlignedVec::new(CH * K * CH, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    crate::loader::dispatcher::wavenet::layout::transpose_conv1d_interleaved_16wide(
        &raw,
        &mut weights_16wide_direct,
        CH,
        CH,
        K,
    );

    for (i, (&a, &b)) in weights_16wide_via_4to16
        .iter()
        .zip(weights_16wide_direct.iter())
        .enumerate()
    {
        assert!(
            (a - b).abs() < 1e-6,
            "4wide→16wide vs direct→16wide mismatch at idx {i}: {a} vs {b}"
        );
    }
}

/// Verifies that `Conv1d::from_parts` panics when the weights buffer
/// is smaller than the SIMD-padded total required by the interleaved layout.
/// This hardening protects against silent UB caused by out-of-bounds reads
/// in the SIMD convolution kernels.
#[test]
#[should_panic(expected = "Conv1d weights buffer is too small")]
fn test_conv1d_from_parts_subdimensioned_weights() {
    use crate::loader::dispatcher::wavenet::layout::select_interleave_width;
    use crate::loader::dispatcher::wavenet::traits::ConvWeightsOutput;
    use crate::math::common::AlignedVec;

    const IN: usize = 4;
    const OUT: usize = 8;
    const K: usize = 2;
    let interleave_width = select_interleave_width(OUT);
    let num_blocks = OUT.div_ceil(interleave_width);
    let padded_total = num_blocks * interleave_width * IN * K;

    let undersized = padded_total / 2;
    let weights = AlignedVec::new(undersized, 0.0f32)
        .expect("allocation should succeed for test-sized buffers");
    let bias =
        AlignedVec::new(OUT, 0.0f32).expect("allocation should succeed for test-sized buffers");

    Conv1d::<IN, OUT, K>::from_parts(weights, bias, false, 1, IN, OUT, K);
}