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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Fábio Henrique de Lima Silva (fhl.bsb@gmail.com) All rights reserved.
//! Neural Inference Architectures (Brain Engines) module for NAM-rs.
//!
//! This module contains the acoustic brains of the program: neural networks that have learned how,
//! for example, a real amplifier or pedal distorts and colors a guitar sound.
/// A2 architecture (v0.6+): FiLM, gating, head1x1, bottleneck, multi-array cascades.
/// Slimmable model container: multi-size bundles with quality-threshold based dispatch.
/// ConvNet feed-forward architecture.
/// Linear FIR model: dot product of input history with learned weights + bias.
/// Linear FFT model: frequency-domain overlap-save FIR convolution kernel.
/// LSTM recurrent architecture: configurable layers × hidden units, gate-level SIMD acceleration.
/// Slimmable channel-slicing dispatcher for WaveNet quality-tier transitions.
/// WaveNet dilated convolution architecture: Standard, Lite, Feather, Nano, Dynamic variants.
/// NamModel trait implementation for StaticModel (dispatch methods).
// =============================================================================
// Sealed Pattern — Prevents external implementations of NamModel
// =============================================================================
// =============================================================================
// Trait NamModel — Public Contract
// =============================================================================
/// Interface for all neural network model architectures in `NeuralAmpModeler-rs`.
///
/// `NamModel` defines the operational contract for acoustic neural inference
/// engines (WaveNet A1/A2, LSTM, ConvNet, Linear FIR/FFT, and Slimmable containers).
///
/// # Lifecycle & Execution Flow
///
/// 1. **Off-RT Instantiation & Prewarming:**
/// Models are constructed outside the real-time audio thread via [`loader::load_and_build_model`](crate::loader::load_and_build_model)
/// or concrete architecture constructors. During instantiation, weights are packed into
/// 64-byte aligned SIMD structures (`AlignedVec<f32>`), internal history state buffers are
/// allocated, and [`prewarm`](NamModel::prewarm) is executed to prime dilated convolution
/// buffers or recurrent states.
///
/// 2. **Real-Time Audio Hot-Path Processing:**
/// The DAW audio callback or standalone audio loop invokes [`process`](NamModel::process) on
/// each audio quantum (block of `f32` samples). Execution strictly guarantees:
/// - **Zero Heap Allocations:** No `Box`, `Vec`, `String`, or dynamic allocation occurs during `process`.
/// - **Zero Mutex Locks / Blocking I/O:** No locks, condition variables, file I/O, or logging.
/// - **Deterministic Real-Time Bounds:** SIMD inner loops (AVX2 / AVX-512) execute within
/// sub-millisecond deadlines.
///
/// 3. **State Resets & Buffer Reallocations:**
/// When sample rates or maximum buffer sizes change, the control thread invokes [`reset`](NamModel::reset)
/// or [`set_max_buffer_size`](NamModel::set_max_buffer_size). Re-allocations happen off-RT,
/// preserving zero-allocation guarantees during subsequent audio callbacks.
///
/// 4. **Swapping & GC Deallocation Cascade:**
/// When models or quality tiers are swapped dynamically, old model instances are transferred via an
/// SPSC channel to an off-RT Garbage Collector (`GcProducer`), ensuring deallocation drops
/// happen off the audio thread.
///
/// # Thread Safety & Trait Sealing
///
/// `NamModel` requires `Send + Sync`, enabling safe cross-thread transfer and multi-threaded host dispatch.
/// The trait is sealed via `sealed::Sealed` to restrict public implementations to this crate, enabling
/// static dispatch via [`StaticModel`].
// ── API Return Type Policy ────────────────────────────────────────────────────
// Methods returning collections of model configuration (not audio samples) use:
// • Box<[T]> when the set is fixed-size and immutable after model load.
// • Vec<T> only when the set is dynamic and caller-growable (justify inline).
// All collection-returning methods are off-RT only; document this in their
// doc-comments with the "# Allocation note" section.
/// Wrapper enum for trained model variants.
/// Enables static dispatch of DSP calls to the concrete variant, avoiding vtable overhead.
///
/// Named `StaticModel` because all variants are compile-time-fixed geometries.
/// The legacy "Dynamic" mode (arbitrary geometry at runtime) has been retired.
pub use clone_condition_dsp;