whisper-apr 0.3.3

WASM-first automatic speech recognition engine implementing OpenAI Whisper
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#![allow(clippy::expect_used)]
//! LFM2 Layer components
//!
//! Contains the layer-level building blocks for LFM2:
//! - `Lfm2Layer`: Single transformer layer (Conv or Attention)
//! - `RmsNorm`: RMS normalization
//! - `LoadStats`: Statistics for weight loading

use crate::error::{WhisperError, WhisperResult};
use crate::format::apr2::LayerType;

use super::conv::Conv1d;
use super::gqa::GroupedQueryAttention;
use super::rope::RotaryEmbedding;
use super::swiglu::SwiGluFfn;

/// Statistics from loading model weights
#[derive(Debug, Clone, Default)]
pub struct LoadStats {
    /// Number of tensors successfully loaded
    pub tensors_loaded: usize,
    /// Total parameters loaded
    pub params_loaded: usize,
}

impl std::fmt::Display for LoadStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} tensors, {} params",
            self.tensors_loaded, self.params_loaded
        )
    }
}

/// Single LFM2 layer (Conv or Attention)
#[derive(Debug)]
pub struct Lfm2Layer {
    /// Layer index
    pub layer_idx: usize,
    /// Layer type
    pub layer_type: LayerType,
    /// Pre-attention/conv normalization
    pub input_norm: RmsNorm,
    /// Post-attention/conv normalization
    pub post_attn_norm: RmsNorm,
    /// Attention (if attention layer)
    pub attention: Option<GroupedQueryAttention>,
    /// Convolution (if conv layer)
    pub conv: Option<Conv1d>,
    /// Feed-forward network
    pub ffn: SwiGluFfn,
}

impl Lfm2Layer {
    /// Create new layer
    ///
    /// # Errors
    /// Returns error if layer creation fails
    pub fn new(
        layer_idx: usize,
        layer_type: LayerType,
        hidden_size: usize,
        intermediate_size: usize,
        num_q_heads: usize,
        num_kv_heads: usize,
    ) -> WhisperResult<Self> {
        let input_norm = RmsNorm::new(hidden_size);
        let post_attn_norm = RmsNorm::new(hidden_size);

        let (attention, conv) = match &layer_type {
            LayerType::Attention { use_gqa } => {
                let gqa_config = super::gqa::GqaConfig {
                    hidden_size,
                    num_q_heads,
                    num_kv_heads: if *use_gqa { num_kv_heads } else { num_q_heads },
                    head_dim: hidden_size / num_q_heads,
                    causal: true,
                    dropout: 0.0,
                    pad_head_dim_to: None,
                };
                (Some(GroupedQueryAttention::new(gqa_config)?), None)
            }
            LayerType::Convolution {
                kernel_size,
                cache_len: _,
            } => {
                let conv_config = super::conv::Conv1dConfig {
                    channels: hidden_size,
                    kernel_size: *kernel_size as usize,
                    causal: true,
                    bias: false,
                };
                (None, Some(Conv1d::new_depthwise(conv_config)?))
            }
            LayerType::Ffn { activation: _ } => {
                // FFN-only layer (unusual but supported)
                (None, None)
            }
        };

        let ffn_config = super::swiglu::SwiGluConfig {
            hidden_size,
            intermediate_size,
            bias: false,
        };
        let ffn = SwiGluFfn::new(ffn_config)?;

        Ok(Self {
            layer_idx,
            layer_type,
            input_norm,
            post_attn_norm,
            attention,
            conv,
            ffn,
        })
    }

    /// Forward pass through layer
    ///
    /// # Errors
    /// Returns error if forward fails
    pub fn forward(
        &self,
        hidden_states: &[f32],
        seq_len: usize,
        rope: &RotaryEmbedding,
        _position_ids: Option<&[usize]>,
    ) -> WhisperResult<Vec<f32>> {
        let _hidden_size = hidden_states.len() / seq_len;

        // Pre-norm
        let normed = self.input_norm.forward(hidden_states, seq_len)?;

        // Attention or Conv
        let attn_output = if let Some(ref attn) = self.attention {
            // Apply RoPE to Q, K in attention
            attn.forward_with_rope(&normed, seq_len, Some(rope))?
        } else if let Some(ref conv) = self.conv {
            conv.forward(&normed, seq_len, None)?
        } else {
            normed.clone()
        };

        // Residual connection
        let mut residual: Vec<f32> = hidden_states
            .iter()
            .zip(attn_output.iter())
            .map(|(h, a)| h + a)
            .collect();

        // Post-attention norm
        let normed2 = self.post_attn_norm.forward(&residual, seq_len)?;

        // FFN
        let ffn_output = self.ffn.forward(&normed2, seq_len)?;

        // Residual connection
        for (r, f) in residual.iter_mut().zip(ffn_output.iter()) {
            *r += f;
        }

        Ok(residual)
    }

    /// Number of parameters in this layer
    #[must_use]
    pub fn num_params(&self) -> usize {
        let norm_params = 2 * self.input_norm.weight.len();
        let attn_params = self
            .attention
            .as_ref()
            .map_or(0, |a| a.w_q.len() + a.w_k.len() + a.w_v.len() + a.w_o.len());
        let conv_params = self.conv.as_ref().map_or(0, Conv1d::num_params);
        let ffn_params = self.ffn.num_params();

        norm_params + attn_params + conv_params + ffn_params
    }

    /// Load weights for this layer from APR2 reader
    ///
    /// # Arguments
    /// * `reader` - APR2 reader
    /// * `layer_idx` - Layer index for tensor names
    ///
    /// # Errors
    /// Returns error if weight loading fails
    pub fn load_weights(
        &mut self,
        reader: &crate::format::Apr2Reader,
        layer_idx: usize,
    ) -> WhisperResult<LoadStats> {
        let mut stats = LoadStats::default();

        // Load layer norms
        try_load_tensor(
            reader,
            &format!("layers.{layer_idx}.ln1.weight"),
            &mut self.input_norm.weight,
            &mut stats,
        );
        try_load_tensor(
            reader,
            &format!("layers.{layer_idx}.ln2.weight"),
            &mut self.post_attn_norm.weight,
            &mut stats,
        );

        // Load attention weights (if attention layer)
        if let Some(ref mut attn) = self.attention {
            let prefix = format!("layers.{layer_idx}.attn");
            for (suffix, target) in [
                ("q.weight", &mut attn.w_q),
                ("k.weight", &mut attn.w_k),
                ("v.weight", &mut attn.w_v),
                ("o.weight", &mut attn.w_o),
            ] {
                try_load_tensor(reader, &format!("{prefix}.{suffix}"), target, &mut stats);
            }
        }

        // Load convolution weights (if conv layer)
        if let Some(ref mut conv) = self.conv {
            try_load_tensor(
                reader,
                &format!("layers.{layer_idx}.conv.weight"),
                &mut conv.weight,
                &mut stats,
            );
        }

        // Load FFN weights
        let ffn_prefix = format!("layers.{layer_idx}.ffn");
        for (suffix, target) in [
            ("gate.weight", &mut self.ffn.w_gate),
            ("up.weight", &mut self.ffn.w_up),
            ("down.weight", &mut self.ffn.w_down),
        ] {
            try_load_tensor(
                reader,
                &format!("{ffn_prefix}.{suffix}"),
                target,
                &mut stats,
            );
        }

        Ok(stats)
    }
}

/// Try to load a tensor from the reader into the target, updating stats on success
fn try_load_tensor(
    reader: &crate::format::Apr2Reader,
    name: &str,
    target: &mut Vec<f32>,
    stats: &mut LoadStats,
) {
    if let Ok(w) = reader.load_tensor_f32(name) {
        if w.len() == target.len() {
            stats.tensors_loaded += 1;
            stats.params_loaded += w.len();
            *target = w;
        }
    }
}

/// RMS Normalization
#[derive(Debug, Clone)]
pub struct RmsNorm {
    /// Learnable scale parameter
    pub weight: Vec<f32>,
    /// Epsilon for numerical stability
    pub eps: f32,
}

impl RmsNorm {
    /// Create new RMSNorm layer
    #[must_use]
    pub fn new(hidden_size: usize) -> Self {
        Self {
            weight: vec![1.0; hidden_size],
            eps: 1e-5,
        }
    }

    /// Forward pass
    ///
    /// # Errors
    /// Returns error if dimensions are invalid
    pub fn forward(&self, hidden_states: &[f32], seq_len: usize) -> WhisperResult<Vec<f32>> {
        let hidden_size = self.weight.len();

        if hidden_states.len() != seq_len * hidden_size {
            return Err(WhisperError::Model(format!(
                "hidden_states length {} != seq_len * hidden_size ({})",
                hidden_states.len(),
                seq_len * hidden_size
            )));
        }

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

        let chunks_in = hidden_states.chunks_exact(hidden_size);
        let chunks_out = output.chunks_exact_mut(hidden_size);

        #[cfg(not(feature = "parallel"))]
        {
            for (x, out_slice) in chunks_in.zip(chunks_out) {
                crate::simd::optimized::rms_norm_into(x, &self.weight, self.eps, out_slice);
            }
        }

        #[cfg(feature = "parallel")]
        {
            use rayon::prelude::*;
            chunks_in
                .zip(chunks_out)
                .par_bridge()
                .for_each(|(x, out_slice)| {
                    crate::simd::optimized::rms_norm_into(x, &self.weight, self.eps, out_slice);
                });
        }

        Ok(output)
    }
}

/// Layer Normalization (weight-only, no bias)
///
/// Used by Moonshine: `nn.LayerNorm(hidden_size, bias=False)`.
/// Computes `(x - mean(x)) / sqrt(var(x) + eps) * weight`.
/// Unlike RMSNorm, this subtracts the mean before normalizing.
#[derive(Debug, Clone)]
pub struct LayerNormNoBias {
    /// Learnable scale parameter (gamma)
    pub weight: Vec<f32>,
    /// Epsilon for numerical stability
    pub eps: f32,
}

impl LayerNormNoBias {
    /// Create new LayerNorm (no bias) layer
    #[must_use]
    pub fn new(hidden_size: usize) -> Self {
        Self {
            weight: vec![1.0; hidden_size],
            eps: 1e-5,
        }
    }

    /// Forward pass
    ///
    /// # Errors
    /// Returns error if dimensions are invalid
    pub fn forward(&self, hidden_states: &[f32], seq_len: usize) -> WhisperResult<Vec<f32>> {
        let hidden_size = self.weight.len();

        if hidden_states.len() != seq_len * hidden_size {
            return Err(WhisperError::Model(format!(
                "LayerNormNoBias input length {} != seq_len * hidden_size ({})",
                hidden_states.len(),
                seq_len * hidden_size
            )));
        }

        let mut output = vec![0.0f32; hidden_states.len()];
        let dummy_bias = vec![0.0; hidden_size]; // LayerNormNoBias has no bias

        let chunks_in = hidden_states.chunks_exact(hidden_size);
        let chunks_out = output.chunks_exact_mut(hidden_size);

        #[cfg(not(feature = "parallel"))]
        {
            for (x, out_slice) in chunks_in.zip(chunks_out) {
                crate::simd::optimized::layer_norm_into(
                    x,
                    &self.weight,
                    &dummy_bias,
                    self.eps,
                    out_slice,
                );
            }
        }

        #[cfg(feature = "parallel")]
        {
            use rayon::prelude::*;
            chunks_in
                .zip(chunks_out)
                .par_bridge()
                .for_each(|(x, out_slice)| {
                    crate::simd::optimized::layer_norm_into(
                        x,
                        &self.weight,
                        &dummy_bias,
                        self.eps,
                        out_slice,
                    );
                });
        }

        Ok(output)
    }
}

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

    #[test]
    fn test_rmsnorm_shape() {
        let rms = RmsNorm::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = rms.forward(&input, 1).expect("forward");
        assert_eq!(output.len(), 4);
        assert!(output.iter().all(|v| v.is_finite()));
    }

    #[test]
    fn test_rmsnorm_multi_seq() {
        let rms = RmsNorm::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let output = rms.forward(&input, 2).expect("forward");
        assert_eq!(output.len(), 8);
    }

    #[test]
    fn test_rmsnorm_dim_error() {
        let rms = RmsNorm::new(4);
        let bad = vec![1.0, 2.0, 3.0]; // wrong size
        assert!(rms.forward(&bad, 1).is_err());
    }

    #[test]
    fn test_layernorm_nobias_shape() {
        let ln = LayerNormNoBias::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = ln.forward(&input, 1).expect("forward");
        assert_eq!(output.len(), 4);
        assert!(output.iter().all(|v| v.is_finite()));
    }

    #[test]
    fn test_layernorm_nobias_zero_mean() {
        // LayerNorm subtracts mean, so output should have near-zero mean
        let ln = LayerNormNoBias::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = ln.forward(&input, 1).expect("forward");
        let mean: f32 = output.iter().sum::<f32>() / 4.0;
        assert!(
            mean.abs() < 1e-5,
            "LayerNorm output mean should be ~0, got {mean}"
        );
    }

    #[test]
    fn test_rmsnorm_nonzero_mean() {
        // RMSNorm does NOT subtract mean, so output mean is generally nonzero
        let rms = RmsNorm::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = rms.forward(&input, 1).expect("forward");
        let mean: f32 = output.iter().sum::<f32>() / 4.0;
        // RMSNorm preserves the sign pattern, mean should NOT be zero
        assert!(
            mean.abs() > 0.1,
            "RMSNorm output mean should be nonzero, got {mean}"
        );
    }

    #[test]
    fn test_layernorm_nobias_differs_from_rmsnorm() {
        // LayerNorm and RMSNorm should produce different outputs for non-zero-mean inputs
        let ln = LayerNormNoBias::new(4);
        let rms = RmsNorm::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0]; // mean = 2.5 (nonzero)

        let ln_out = ln.forward(&input, 1).expect("ln forward");
        let rms_out = rms.forward(&input, 1).expect("rms forward");

        let diff: f32 = ln_out
            .iter()
            .zip(rms_out.iter())
            .map(|(a, b)| (a - b).abs())
            .sum();
        assert!(
            diff > 0.01,
            "LayerNorm and RMSNorm should differ for non-zero-mean input"
        );
    }

    #[test]
    fn test_layernorm_nobias_multi_seq() {
        let ln = LayerNormNoBias::new(4);
        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let output = ln.forward(&input, 2).expect("forward");
        assert_eq!(output.len(), 8);
    }

    #[test]
    fn test_layernorm_nobias_dim_error() {
        let ln = LayerNormNoBias::new(4);
        let bad = vec![1.0, 2.0, 3.0]; // wrong size
        assert!(ln.forward(&bad, 1).is_err());
    }

    #[test]
    fn test_layernorm_nobias_unit_variance() {
        // With weight=1 and no bias, output should have unit variance
        let ln = LayerNormNoBias::new(4);
        let input = vec![1.0, 3.0, 5.0, 7.0];
        let output = ln.forward(&input, 1).expect("forward");
        let mean: f32 = output.iter().sum::<f32>() / 4.0;
        let variance: f32 = output.iter().map(|v| (v - mean) * (v - mean)).sum::<f32>() / 4.0;
        assert!(
            (variance - 1.0).abs() < 0.01,
            "LayerNorm output variance should be ~1.0, got {variance}"
        );
    }
}