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
//! Moonshine decoder block
//!
//! Masked MHA self-attention (RoPE) + cross-attention (no RoPE) + SiLU gated MLP FFN.
//! Pre-LayerNorm (no bias) before each sub-layer with residual connections.

use crate::error::WhisperResult;
use crate::model::lfm2::gqa::{GqaConfig, GroupedQueryAttention};
use crate::model::lfm2::layer::LayerNormNoBias;
use crate::model::lfm2::mlp::GatedMlpFfn;
use crate::model::lfm2::rope::RotaryEmbedding;
use crate::model::LayerKVCache;

/// Single Moonshine decoder transformer block
///
/// Architecture:
/// 1. Pre-LayerNorm → masked MHA self-attention (with RoPE) → residual
/// 2. Pre-LayerNorm → MHA cross-attention (Q from decoder, KV from encoder) → residual
/// 3. Pre-LayerNorm → SiLU gated MLP FFN → residual
///
/// Uses `LayerNorm(bias=False)` matching the HuggingFace Moonshine implementation.
#[derive(Debug, Clone)]
pub struct MoonshineDecoderBlock {
    /// Pre-self-attention layer normalization (weight-only, no bias)
    pub ln1: LayerNormNoBias,
    /// Masked MHA self-attention (causal, with RoPE)
    pub self_attn: GroupedQueryAttention,
    /// Pre-cross-attention layer normalization (weight-only, no bias)
    pub ln_cross: LayerNormNoBias,
    /// MHA cross-attention (Q from decoder, KV from encoder, no RoPE, no causal mask)
    pub cross_attn: GroupedQueryAttention,
    /// Pre-FFN layer normalization (weight-only, no bias)
    pub ln2: LayerNormNoBias,
    /// Gated MLP feed-forward network (fc1[→2x] → SiLU(gate)*value → fc2)
    pub ffn: GatedMlpFfn,
}

impl MoonshineDecoderBlock {
    /// Create a new Moonshine decoder block
    ///
    /// # Arguments
    /// * `d_model` - Hidden dimension (288 for tiny, 416 for base)
    /// * `n_q_heads` - Number of query attention heads (8)
    /// * `n_kv_heads` - Number of key-value heads (8, MHA)
    /// * `intermediate_size` - FFN intermediate dimension (4x d_model)
    ///
    /// # Errors
    /// Returns error if attention or MLP config validation fails
    pub fn new(
        d_model: usize,
        n_q_heads: usize,
        n_kv_heads: usize,
        intermediate_size: usize,
    ) -> WhisperResult<Self> {
        let head_dim = d_model / n_q_heads;

        // Self-attention: causal (masked) with RoPE
        let self_attn_config = GqaConfig {
            hidden_size: d_model,
            num_q_heads: n_q_heads,
            num_kv_heads: n_kv_heads,
            head_dim,
            causal: true,
            dropout: 0.0,
            pad_head_dim_to: Some(8),
        };

        // Cross-attention: not causal (decoder attends to all encoder positions)
        let cross_attn_config = GqaConfig {
            hidden_size: d_model,
            num_q_heads: n_q_heads,
            num_kv_heads: n_kv_heads,
            head_dim,
            causal: false,
            dropout: 0.0,
            pad_head_dim_to: Some(8),
        };

        Ok(Self {
            ln1: LayerNormNoBias::new(d_model),
            self_attn: GroupedQueryAttention::new(self_attn_config)?,
            ln_cross: LayerNormNoBias::new(d_model),
            cross_attn: GroupedQueryAttention::new(cross_attn_config)?,
            ln2: LayerNormNoBias::new(d_model),
            ffn: GatedMlpFfn::new(d_model, intermediate_size)?,
        })
    }

    /// Incremental cached forward pass for a single token (WAPR-MOONSHINE-002)
    ///
    /// Processes only the new token embedding, appending K/V to cache and attending
    /// to all cached positions. O(n) per token instead of O(n²) full recompute.
    ///
    /// # Arguments
    /// * `x` - Single token hidden state `[d_model]`
    /// * `encoder_out` - Encoder output `[enc_seq_len * d_model]`
    /// * `enc_seq_len` - Encoder sequence length
    /// * `position` - Current decode position (for RoPE offset)
    /// * `rope` - Rotary position embedding
    /// * `self_attn_cache` - Self-attention KV cache for this layer (kv_dim width)
    /// * `cross_attn_cache` - Cross-attention KV cache for this layer (kv_dim width)
    /// * `cross_attn_cached` - Whether cross-attention K/V are already cached
    ///
    /// # Returns
    /// Output hidden state `[d_model]`
    ///
    /// # Errors
    /// Returns error if dimensions are invalid or cache overflows
    #[allow(clippy::too_many_arguments)]
    pub fn forward_cached(
        &self,
        x: &[f32],
        encoder_out: &[f32],
        enc_seq_len: usize,
        position: usize,
        rope: &RotaryEmbedding,
        self_attn_cache: &mut LayerKVCache,
        cross_attn_cache: &mut LayerKVCache,
        cross_attn_cached: bool,
    ) -> WhisperResult<Vec<f32>> {
        let d_model = self.self_attn.config.hidden_size;

        // 1. Pre-norm → self-attention with RoPE + KV cache
        let normed = self.ln1.forward(x, 1)?;
        let (q, k_new, v_new) = self
            .self_attn
            .project_qkv_single(&normed, Some(rope), position)?;

        // Append new K, V to self-attention cache
        self_attn_cache.append(&k_new, &v_new)?;

        // Attend to all cached K/V
        let k_full = self_attn_cache.get_key();
        let v_full = self_attn_cache.get_value();
        let cache_len = self_attn_cache.len();
        let attn_out = self
            .self_attn
            .attention_cached(&q, k_full, v_full, cache_len)?;
        let attn_out = self.self_attn.output_projection(&attn_out);

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

        // 2. Pre-norm → cross-attention with cached encoder K/V
        let normed_cross = self.ln_cross.forward(&residual, 1)?;

        let cross_out = if !cross_attn_cached || cross_attn_cache.is_empty() {
            // First token: project encoder output to K/V and cache
            let (k_enc, v_enc) = self.cross_attn.project_kv(encoder_out, enc_seq_len);
            cross_attn_cache.append(&k_enc, &v_enc)?;

            let q_cross = self.cross_attn.project_q(&normed_cross);
            let attn_out =
                self.cross_attn
                    .attention_cached(&q_cross, &k_enc, &v_enc, enc_seq_len)?;
            self.cross_attn.output_projection(&attn_out)
        } else {
            // Subsequent tokens: reuse cached encoder K/V
            let k_cached = cross_attn_cache.get_key();
            let v_cached = cross_attn_cache.get_value();
            let cached_enc_len = cross_attn_cache.len();

            let q_cross = self.cross_attn.project_q(&normed_cross);
            let attn_out =
                self.cross_attn
                    .attention_cached(&q_cross, k_cached, v_cached, cached_enc_len)?;
            self.cross_attn.output_projection(&attn_out)
        };

        // Residual connection
        add_vectors_inplace(&mut residual, &cross_out);

        // 3. Pre-norm → SiLU MLP FFN → residual
        let normed2 = self.ln2.forward(&residual, 1)?;
        let ffn_out = self.ffn.forward(&normed2, 1)?;
        add_vectors_inplace(&mut residual, &ffn_out);

        debug_assert_eq!(residual.len(), d_model);
        Ok(residual)
    }

    /// Forward pass through decoder block
    ///
    /// # Arguments
    /// * `x` - Decoder input tensor [dec_seq_len, d_model]
    /// * `encoder_out` - Encoder output tensor [enc_seq_len, d_model]
    /// * `dec_seq_len` - Decoder sequence length
    /// * `enc_seq_len` - Encoder sequence length
    /// * `rope` - Rotary position embedding (for self-attention only)
    ///
    /// # Returns
    /// Output tensor [dec_seq_len, d_model]
    pub fn forward(
        &self,
        x: &[f32],
        encoder_out: &[f32],
        dec_seq_len: usize,
        enc_seq_len: usize,
        rope: &RotaryEmbedding,
    ) -> WhisperResult<Vec<f32>> {
        // 1. Masked self-attention with RoPE + residual
        let normed = self.ln1.forward(x, dec_seq_len)?;
        let self_attn_out = self
            .self_attn
            .forward_with_rope(&normed, dec_seq_len, Some(rope))?;
        let mut residual = add_vectors(x, &self_attn_out);

        // 2. Cross-attention (Q from decoder, KV from encoder) + residual
        let normed_cross = self.ln_cross.forward(&residual, dec_seq_len)?;
        let cross_attn_out = self.cross_attn.forward_cross_attention(
            &normed_cross,
            encoder_out,
            dec_seq_len,
            enc_seq_len,
        )?;
        add_vectors_inplace(&mut residual, &cross_attn_out);

        // 3. FFN + residual
        let normed2 = self.ln2.forward(&residual, dec_seq_len)?;
        let ffn_out = self.ffn.forward(&normed2, dec_seq_len)?;
        add_vectors_inplace(&mut residual, &ffn_out);

        Ok(residual)
    }

    /// Forward pass with activation probing
    ///
    /// Same logic as [`forward()`](Self::forward) but records activation snapshots
    /// at each sub-layer boundary.
    ///
    /// # Arguments
    /// * `x` - Decoder input tensor [dec_seq_len, d_model]
    /// * `encoder_out` - Encoder output tensor [enc_seq_len, d_model]
    /// * `dec_seq_len` - Decoder sequence length
    /// * `enc_seq_len` - Encoder sequence length
    /// * `rope` - Rotary position embedding
    /// * `block_idx` - Block index for checkpoint naming
    /// * `probe` - Activation probe for recording snapshots
    #[allow(clippy::too_many_arguments)]
    pub fn forward_probed(
        &self,
        x: &[f32],
        encoder_out: &[f32],
        dec_seq_len: usize,
        enc_seq_len: usize,
        rope: &RotaryEmbedding,
        block_idx: usize,
        probe: &mut crate::probe::ActivationProbe,
    ) -> WhisperResult<Vec<f32>> {
        let d_model = self.self_attn.config.hidden_size;
        let prefix = format!("decoder.block_{block_idx}");

        // 1. Masked self-attention with RoPE + residual
        let normed = self.ln1.forward(x, dec_seq_len)?;
        probe.record(
            &format!("{prefix}.ln1_out"),
            &normed,
            &[dec_seq_len, d_model],
        );

        let self_attn_out = self
            .self_attn
            .forward_with_rope(&normed, dec_seq_len, Some(rope))?;
        probe.record(
            &format!("{prefix}.self_attn_out"),
            &self_attn_out,
            &[dec_seq_len, d_model],
        );

        let mut residual = add_vectors(x, &self_attn_out);
        probe.record(
            &format!("{prefix}.residual_1"),
            &residual,
            &[dec_seq_len, d_model],
        );

        // 2. Cross-attention (Q from decoder, KV from encoder) + residual
        let normed_cross = self.ln_cross.forward(&residual, dec_seq_len)?;
        probe.record(
            &format!("{prefix}.ln_cross_out"),
            &normed_cross,
            &[dec_seq_len, d_model],
        );

        let cross_attn_out = self.cross_attn.forward_cross_attention(
            &normed_cross,
            encoder_out,
            dec_seq_len,
            enc_seq_len,
        )?;
        probe.record(
            &format!("{prefix}.cross_attn_out"),
            &cross_attn_out,
            &[dec_seq_len, d_model],
        );

        add_vectors_inplace(&mut residual, &cross_attn_out);
        probe.record(
            &format!("{prefix}.residual_2"),
            &residual,
            &[dec_seq_len, d_model],
        );

        // 3. FFN + residual
        let normed2 = self.ln2.forward(&residual, dec_seq_len)?;
        probe.record(
            &format!("{prefix}.ln2_out"),
            &normed2,
            &[dec_seq_len, d_model],
        );

        let ffn_out = self.ffn.forward(&normed2, dec_seq_len)?;
        probe.record(
            &format!("{prefix}.ffn_out"),
            &ffn_out,
            &[dec_seq_len, d_model],
        );

        add_vectors_inplace(&mut residual, &ffn_out);
        probe.record(
            &format!("{prefix}.residual_3"),
            &residual,
            &[dec_seq_len, d_model],
        );

        Ok(residual)
    }
}

/// Element-wise vector addition
fn add_vectors(a: &[f32], b: &[f32]) -> Vec<f32> {
    a.iter().zip(b.iter()).map(|(x, y)| x + y).collect()
}

/// Element-wise in-place vector addition
fn add_vectors_inplace(a: &mut [f32], b: &[f32]) {
    for (x, y) in a.iter_mut().zip(b.iter()) {
        *x += y;
    }
}

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

    #[test]
    fn test_moonshine_decoder_block_new() {
        // Moonshine tiny: d=288, 8 Q heads, 8 KV heads (MHA), 4x intermediate
        let block = MoonshineDecoderBlock::new(288, 8, 8, 1152);
        assert!(block.is_ok());
    }

    #[test]
    fn test_moonshine_decoder_block_forward_shape() {
        let block = MoonshineDecoderBlock::new(288, 8, 8, 1152).expect("block creation");
        let rope = RotaryEmbedding::new(crate::model::lfm2::rope::RopeConfig {
            head_dim: 40, // 288/8=36 padded to 40 (pad_head_dim_to=8)
            base: 10000.0,
            max_seq_len: 2048,
            rotary_dim: Some(32),
        })
        .expect("rope creation");

        let d_model = 288;
        let dec_seq_len = 3;
        let enc_seq_len = 7;

        let decoder_input = vec![0.1_f32; dec_seq_len * d_model];
        let encoder_output = vec![0.2_f32; enc_seq_len * d_model];

        let output = block
            .forward(
                &decoder_input,
                &encoder_output,
                dec_seq_len,
                enc_seq_len,
                &rope,
            )
            .expect("forward");
        assert_eq!(output.len(), dec_seq_len * d_model);
    }

    #[test]
    fn test_moonshine_decoder_block_forward_cached_shape() {
        let block = MoonshineDecoderBlock::new(288, 8, 8, 1152).expect("block creation");
        let rope = RotaryEmbedding::new(crate::model::lfm2::rope::RopeConfig {
            head_dim: 40, // 288/8=36 padded to 40 (pad_head_dim_to=8)
            base: 10000.0,
            max_seq_len: 2048,
            rotary_dim: Some(32),
        })
        .expect("rope creation");

        let d_model = 288;
        // With pad_head_dim_to=8, head_dim 36 pads to 40; kv_dim = 8 * 40 = 320
        let padded_kv_dim = 8 * 40;
        let enc_seq_len = 7;
        let max_tokens = 100;

        let encoder_output = vec![0.2_f32; enc_seq_len * d_model];

        // Simulate 3 decode steps
        let mut self_cache = LayerKVCache::new(padded_kv_dim, max_tokens);
        let mut cross_cache = LayerKVCache::new(padded_kv_dim, max_tokens);

        for pos in 0..3 {
            let x = vec![0.1_f32; d_model];
            let out = block
                .forward_cached(
                    &x,
                    &encoder_output,
                    enc_seq_len,
                    pos,
                    &rope,
                    &mut self_cache,
                    &mut cross_cache,
                    pos > 0,
                )
                .expect("forward_cached");
            assert_eq!(out.len(), d_model);
            assert!(out.iter().all(|v| v.is_finite()));
        }

        assert_eq!(self_cache.len(), 3);
        assert_eq!(cross_cache.len(), enc_seq_len);
    }

    #[test]
    fn test_moonshine_decoder_block_finite_output() {
        let block = MoonshineDecoderBlock::new(288, 8, 8, 1152).expect("block creation");
        let rope = RotaryEmbedding::new(crate::model::lfm2::rope::RopeConfig {
            head_dim: 40,
            base: 10000.0,
            max_seq_len: 2048,
            rotary_dim: Some(32),
        })
        .expect("rope creation");

        let d_model = 288;
        let dec_seq_len = 1;
        let enc_seq_len = 5;

        let decoder_input = vec![1.0_f32; dec_seq_len * d_model];
        let encoder_output = vec![0.5_f32; enc_seq_len * d_model];

        let output = block
            .forward(
                &decoder_input,
                &encoder_output,
                dec_seq_len,
                enc_seq_len,
                &rope,
            )
            .expect("forward");
        assert!(output.iter().all(|v| v.is_finite()));
    }
}