bunsen 0.36.0

bunsen is a batteries included common library for burn
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! # The audio front end a checkpoint was trained with.
//!
//! Whisper's log-mels are a grid fixed in time — a 25 ms
//! window every 10 ms — computed at 16 kHz and floored 8 dB under each
//! window's maximum. A checkpoint records none of that; it is the convention of
//! the pipeline that trained it. [`WhisperFrontEndConfig`] declares it on the
//! model, defaulting to upstream's, so every sample-domain number is
//! derived from it rather than written down, and a checkpoint trained
//! differently can say so. The mel options and the packaging it drives
//! live with the driver, as methods on it.

use burn::{
    Tensor,
    config::Config,
    prelude::Backend,
};

use crate::{
    burner::module::ModuleInit,
    errors::{
        BunsenError,
        BunsenResult,
    },
    kits::speech::whisper::driver::{
        PerWindow,
        StreamClampPolicy,
        support::drop_last_frame,
    },
    ops::signal::perceptive_audio::{
        AffineCompress,
        PerceptiveAudioConverter,
        PerceptiveAudioConverterOptions,
    },
};

/// The audio front end a checkpoint's log-mels were computed with.
///
/// The grid is in time; [`hop`](Self::hop) and [`n_fft`](Self::n_fft) put
/// it on samples at [`sample_rate`](Self::sample_rate).
#[derive(Config, Debug, PartialEq)]
pub struct WhisperFrontEndConfig {
    /// The sample rate, in Hz.
    #[config(default = "16_000")]
    pub sample_rate: usize,

    /// The hop between mel frames, in milliseconds. One timestamp step over
    /// the encoder's stride
    /// ([`AUDIO_ENCODER_STRIDE`](super::AUDIO_ENCODER_STRIDE)), so that one
    /// encoder position is one timestamp token.
    #[config(default = "10")]
    pub hop_ms: usize,

    /// The analysis window, in milliseconds.
    #[config(default = "25")]
    pub window_ms: usize,

    /// The dynamic range kept under each window's maximum, in dB:
    /// log-mels further below the maximum are floored to it
    /// before packaging.
    #[config(default = "8.0")]
    pub range_clamp_db: f64,
}

impl WhisperFrontEndConfig {
    /// The hop, in samples.
    pub fn hop(&self) -> usize {
        self.sample_rate * self.hop_ms / 1000
    }

    /// The analysis window, in samples: the FFT length.
    pub fn n_fft(&self) -> usize {
        self.sample_rate * self.window_ms / 1000
    }

    /// Checks that the grid falls on whole samples.
    ///
    /// # Errors
    /// [`BunsenError::Invalid`] if the rate, hop or window is zero, or the
    /// rate does not put the hop and the window on whole samples. At the
    /// default 10 ms and 25 ms that is any rate not a multiple of 200 Hz.
    pub fn validate(&self) -> BunsenResult<()> {
        if self.sample_rate == 0 || self.hop_ms == 0 || self.window_ms == 0 {
            return Err(BunsenError::Invalid(format!(
                "a front end needs a rate, a hop and a window; got {} Hz, {} ms, {} ms",
                self.sample_rate, self.hop_ms, self.window_ms,
            )));
        }
        for (what, ms) in [("hop", self.hop_ms), ("window", self.window_ms)] {
            if !(self.sample_rate * ms).is_multiple_of(1000) {
                return Err(BunsenError::Invalid(format!(
                    "a {ms} ms {what} is not a whole number of samples at {} Hz",
                    self.sample_rate,
                )));
            }
        }
        Ok(())
    }

    /// Initialize a mel converter for this front end.
    ///
    /// # Arguments
    /// * `n_mels` - the encoder's mel channel count, from the checkpoint.
    /// * `device` - the device to initialize the mel converter on.
    ///
    /// # Errors
    /// See [`validate`](PerceptiveAudioConverterOptions::validate) and
    /// [`to_vec_filterbank`](PerceptiveAudioConverterOptions::try_to_filterbank_vec).
    pub fn try_init_audio_converter<B: Backend>(
        &self,
        n_mels: usize,
        device: &B::Device,
    ) -> BunsenResult<PerceptiveAudioConverter<B>> {
        self.mel_converter_options(n_mels)?.try_init(device)
    }

    /// Build the [`PerceptiveAudioConverterOptions`] for this frontend.
    ///
    /// The grid is fixed in time &mdash; a `window_ms` periodic Hann window
    /// every `hop_ms` &mdash; and the rate puts it on samples. The mel
    /// scale, its normalisation and the log compression are
    /// [`PerceptiveAudioConverterOptions`]' defaults, so at the default front
    /// end the result is that struct's default entirely.
    ///
    /// # Arguments
    /// * `n_mels` - the encoder's mel channel count, from the checkpoint.
    ///
    /// # Errors
    /// As [`validate`](Self::validate): the rate must put the grid on whole
    /// samples.
    pub fn mel_converter_options(
        &self,
        n_mels: usize,
    ) -> BunsenResult<PerceptiveAudioConverterOptions> {
        self.validate()?;
        Ok(PerceptiveAudioConverterOptions::default()
            .with_sample_rate(self.sample_rate)
            .with_n_fft(self.n_fft())
            .with_hop(self.hop())
            .with_n_mels(n_mels))
    }

    /// Packages one window of log-mels into encoder input, against
    /// a reference.
    ///
    /// Floors every value at `range_clamp_db` below its row's `reference`,
    /// applies the `(log + 4) / 4` affine tail, and transposes to the
    /// encoder's channels-first layout. Where the reference comes from is a
    /// [`StreamClampPolicy`]'s decision; this only applies it.
    ///
    /// # Arguments
    /// * `window` - `[batch, frames, n_mels]` log-mels.
    /// * `reference` - `[batch]`, the maximum each row is floored against, in
    ///   the post-log domain.
    ///
    /// # Returns
    /// `[batch, n_mels, frames]`.
    pub fn package_window<B: Backend>(
        &self,
        window: Tensor<B, 3>,
        reference: Tensor<B, 1>,
    ) -> Tensor<B, 3> {
        let [batch, _, _] = window.dims();
        assert_eq!(reference.dims(), [batch], "one reference per batch row");

        // Each row against its own reference: the batch is independent
        // streams, and one row must not see another's peak.
        let floor = reference
            .sub_scalar(self.range_clamp_db)
            .reshape([batch, 1, 1])
            .expand(window.dims());

        // `[batch, frames, n_mels]`
        let floored = window.max_pair(floor);

        AffineCompress::default()
            .apply(floored)
            // `[batch, n_mels, frames]`
            .swap_dims(1, 2)
    }

    /// Packages a joined log-mel spectrogram into encoder input, whole.
    ///
    /// [`drop_last_frame`], then [`package_window`](Self::package_window)
    /// against each row's own maximum ([`PerWindow`]) &mdash; which, when the
    /// window is the whole clip, is upstream's
    /// `maximum(log_spec, log_spec.max() - 8.0)` exactly.
    ///
    /// Apply this **once**, to the whole spectrogram. The reference reduces
    /// over what it is given, so packaging each streamed chunk separately
    /// would floor every chunk against its own maximum and match nothing. A
    /// stream that cannot wait for the whole clip packages per window with a
    /// [`StreamClampPolicy`] instead.
    ///
    /// # Arguments
    /// * `joined` - `[batch, frames, n_mels]`, the concatenated output of a
    ///   [`MelConversionContext`](crate::ops::signal::perceptive_audio::PerceptiveAudioConversionContext)
    ///   including its `finish` tail.
    ///
    /// # Returns
    /// `[batch, n_mels, frames - 1]`.
    ///
    /// # Panics
    /// If `frames` is less than 2. One frame leaves nothing after the
    /// trailing frame is dropped, and the clamp has no maximum to reduce
    /// over.
    pub fn package_mels<B: Backend>(
        &self,
        joined: Tensor<B, 3>,
    ) -> Tensor<B, 3> {
        let window = drop_last_frame(joined);
        let reference = PerWindow.reference(window.clone());
        self.package_window(window, reference)
    }
}

#[cfg(test)]
mod tests {
    use burn::{
        prelude::TensorData,
        tensor::Tolerance,
    };

    use super::*;
    use crate::{
        kits::speech::whisper::blocks::{
            AUDIO_ENCODER_STRIDE,
            WhisperTokenLayoutConfig,
        },
        support::testing::{
            CpuBackend,
            assert_tensor_close_to_vec,
            default_device,
        },
    };

    type B = CpuBackend;

    #[test]
    fn test_defaults_are_upstreams() {
        let front_end = WhisperFrontEndConfig::new();
        assert_eq!(front_end.sample_rate, 16_000);
        assert_eq!((front_end.hop(), front_end.n_fft()), (160, 400));
        assert_eq!(front_end.range_clamp_db, 8.0);
        assert!(front_end.validate().is_ok());
    }

    #[test]
    fn test_grid_scales_with_the_rate() {
        let at = |rate: usize| WhisperFrontEndConfig::new().with_sample_rate(rate);

        assert_eq!((at(8_000).hop(), at(8_000).n_fft()), (80, 200));
        assert!(at(8_000).validate().is_ok());
        assert_eq!((at(48_000).hop(), at(48_000).n_fft()), (480, 1_200));

        assert!(at(44_100).validate().is_err(), "not a whole hop");
        assert!(at(0).validate().is_err());
        assert!(at(16_000).with_hop_ms(0).validate().is_err());
        assert!(
            at(16_000).with_window_ms(3).validate().is_ok(),
            "3 ms is 48 whole samples"
        );
        assert!(
            at(16_000)
                .with_hop_ms(3)
                .with_window_ms(7)
                .validate()
                .is_ok(),
            "any whole-sample grid passes; the 10 ms / 25 ms pairing is convention, not checked"
        );
        assert!(
            at(22_050).with_hop_ms(10).validate().is_err(),
            "220.5 samples"
        );
    }

    /// The default front end: what every packaging test pins.
    fn front_end() -> WhisperFrontEndConfig {
        WhisperFrontEndConfig::new()
    }

    fn package_mels(joined: Tensor<B, 3>) -> Tensor<B, 3> {
        front_end().package_mels(joined)
    }

    fn package_window(
        window: Tensor<B, 3>,
        reference: Tensor<B, 1>,
    ) -> Tensor<B, 3> {
        front_end().package_window(window, reference)
    }

    /// At the default front end the derived geometry is
    /// `MelConverterOptions`' own default; at another rate the 10 ms / 25 ms
    /// grid scales and nothing else moves.
    #[test]
    fn test_mel_options_derives_the_grid_from_the_rate() {
        let base = PerceptiveAudioConverterOptions::default();

        let opts = front_end().mel_converter_options(80).unwrap();
        assert_eq!(opts.sample_rate, base.sample_rate);
        assert_eq!(opts.n_mels, base.n_mels);
        assert_eq!(opts.n_fft, base.n_fft);
        assert_eq!(opts.hop, base.hop);

        let opts = front_end()
            .with_sample_rate(8_000)
            .mel_converter_options(128)
            .unwrap();
        assert_eq!(opts.sample_rate, 8_000);
        assert_eq!(opts.n_mels, 128);
        assert_eq!(opts.n_fft, 200);
        assert_eq!(opts.hop, 80);
        assert_eq!(opts.window, base.window);
        assert_eq!(opts.mel_scale, base.mel_scale);
        assert_eq!(opts.filter_norm, base.filter_norm);
        assert_eq!(opts.log_base, base.log_base);

        let opts = front_end()
            .with_sample_rate(48_000)
            .mel_converter_options(80)
            .unwrap();
        assert_eq!((opts.n_fft, opts.hop), (1_200, 480));

        let opts = front_end()
            .with_hop_ms(20)
            .with_window_ms(50)
            .mel_converter_options(80)
            .unwrap();
        assert_eq!((opts.n_fft, opts.hop), (800, 320));

        assert!(
            front_end()
                .with_sample_rate(44_100)
                .mel_converter_options(80)
                .is_err(),
            "not a whole hop"
        );
        assert!(
            front_end()
                .with_sample_rate(0)
                .mel_converter_options(80)
                .is_err()
        );
    }

    /// The default hop is one timestamp step per encoder position.
    #[test]
    fn test_hop_is_one_timestamp_step_per_encoder_position() {
        let hop_seconds = front_end().hop_ms as f64 / 1000.0;
        let step_seconds =
            WhisperTokenLayoutConfig::new().timestamp_step_seconds / AUDIO_ENCODER_STRIDE as f64;
        assert!((hop_seconds - step_seconds).abs() < 1e-12);
    }

    /// The clamp range is the front end's, not a constant.
    #[test]
    fn test_package_window_uses_the_configured_range() {
        let device = default_device();
        let window: Tensor<B, 3> =
            Tensor::from_data(TensorData::new(vec![0.0_f64, -20.0], [1, 2, 1]), &device);
        let reference: Tensor<B, 1> =
            Tensor::from_data(TensorData::new(vec![0.0_f64], [1]), &device);

        // A 4 dB range floors -20 at -4: affine `(v + 4) / 4` gives 0.
        assert_tensor_close_to_vec(
            &front_end()
                .with_range_clamp_db(4.0)
                .package_window(window, reference),
            &[1.0, 0.0],
            Tolerance::absolute(1e-6),
        );
    }

    /// Sweeps the shape parameters: for every accepted frame count, and
    /// across batch and mel widths, the output is `[batch, n_mels, frames -
    /// 1]`.
    #[test]
    fn test_package_mels_shape_over_a_range() {
        let device = default_device();

        for batch in 1..4 {
            for n_mels in 1..5 {
                for frames in 2..12 {
                    let joined: Tensor<B, 3> = Tensor::zeros([batch, frames, n_mels], &device);

                    assert_eq!(
                        package_mels(joined).dims(),
                        [batch, n_mels, frames - 1],
                        "batch {batch}, n_mels {n_mels}, frames {frames}",
                    );
                }
            }
        }
    }

    /// Sweeps the frame count against the invariant that matters: the dropped
    /// frame never reaches the clamp, so an outlier parked in it cannot move
    /// the reference for anything that survives.
    ///
    /// Every kept value is 0, so the clamp is inert and the affine takes them
    /// all to `(0 + 4) / 4`. If the trailing frame leaked through, its 99
    /// would become the maximum and floor everything else at 91.
    #[test]
    fn test_package_mels_ignores_the_dropped_frame_over_a_range() {
        let device = default_device();
        let n_mels = 2;

        for frames in 2..10 {
            let mut data = vec![0.0_f64; frames * n_mels];
            for slot in data.iter_mut().skip((frames - 1) * n_mels) {
                *slot = 99.0;
            }

            let joined: Tensor<B, 3> =
                Tensor::from_data(TensorData::new(data, [1, frames, n_mels]), &device);

            assert_tensor_close_to_vec(
                &package_mels(joined),
                &vec![1.0; n_mels * (frames - 1)],
                Tolerance::absolute(1e-6),
            );
        }
    }

    /// Too few frames panics with the reason, rather than deep inside a
    /// reduction over an empty tensor.
    #[test]
    #[should_panic(expected = "at least 2 frames")]
    fn test_package_mels_rejects_a_single_frame() {
        let device = default_device();
        let joined: Tensor<B, 3> = Tensor::zeros([1, 1, 4], &device);

        let _ = package_mels(joined);
    }

    /// The other degenerate count: zero frames would slice `0..-1`.
    #[test]
    #[should_panic(expected = "at least 2 frames")]
    fn test_package_mels_rejects_zero_frames() {
        let device = default_device();
        let joined: Tensor<B, 3> = Tensor::zeros([1, 0, 4], &device);

        let _ = package_mels(joined);
    }

    /// The trailing frame is dropped **before** the clamp takes its
    /// reference, so an outlier in it cannot set the floor for everything
    /// else. That ordering is the part worth pinning.
    #[test]
    fn test_package_mels_clamps_after_dropping_the_frame() {
        let device = default_device();

        // One row, one mel, three frames. The last is an outlier that would
        // dominate the maximum if it survived to the clamp.
        let joined: Tensor<B, 3> = Tensor::from_data(
            TensorData::new(vec![0.0_f64, -20.0, 99.0], [1, 3, 1]),
            &device,
        );

        // Kept `[0, -20]`; max 0, so the floor is -8 and -20 lifts to it.
        // Affine `(v + 4) / 4` then gives `[1.0, -1.0]`.
        assert_tensor_close_to_vec(
            &package_mels(joined),
            &[1.0, -1.0],
            Tolerance::absolute(1e-6),
        );
    }

    /// **I4.** The split is behaviour-preserving: trim, then package against
    /// each row's own maximum, is `package_mels` to the bit &mdash; on rows
    /// with different peaks, so that the per-row reference is exercised.
    #[test]
    fn test_split_packaging_equals_package_mels() {
        let device = default_device();
        let (batch, frames, n_mels) = (3, 7, 4);

        let data: Vec<f64> = (0..batch * frames * n_mels)
            .map(|k| ((k * 37) % 23) as f64 - 15.0 + (k / (frames * n_mels)) as f64 * 3.0)
            .collect();
        let joined: Tensor<B, 3> =
            Tensor::from_data(TensorData::new(data, [batch, frames, n_mels]), &device);

        let whole = package_mels(joined.clone());

        let window = drop_last_frame(joined);
        let split = package_window(window.clone(), PerWindow.reference(window));

        assert_eq!(split.dims(), [batch, n_mels, frames - 1]);
        split.to_data().assert_eq(&whole.to_data(), true);
    }

    /// The reference is per row: raising one row's reference floors that row
    /// harder and leaves the others alone.
    #[test]
    fn test_package_window_floors_each_row_against_its_reference() {
        let device = default_device();

        // Two rows, two frames, one mel: `[0, -20]` in each row.
        let window: Tensor<B, 3> = Tensor::from_data(
            TensorData::new(vec![0.0_f64, -20.0, 0.0, -20.0], [2, 2, 1]),
            &device,
        );

        // Row 0 against its own maximum (0): floor -8. Row 1 against a
        // reference of 12, as if something louder had been heard: floor 4,
        // which lifts both of its values.
        let reference: Tensor<B, 1> =
            Tensor::from_data(TensorData::new(vec![0.0_f64, 12.0], [2]), &device);

        assert_tensor_close_to_vec(
            &package_window(window, reference),
            &[1.0, -1.0, 2.0, 2.0],
            Tolerance::absolute(1e-6),
        );
    }

    /// A reference below the window's own maximum does not clip the peak:
    /// values above the floor pass through unchanged, whatever the reference.
    #[test]
    fn test_package_window_never_clips_above_the_floor() {
        let device = default_device();
        let window: Tensor<B, 3> =
            Tensor::from_data(TensorData::new(vec![4.0_f64, 0.0], [1, 2, 1]), &device);
        let reference: Tensor<B, 1> =
            Tensor::from_data(TensorData::new(vec![0.0_f64], [1]), &device);

        // Floor at -8; nothing is below it, so it is the affine alone.
        assert_tensor_close_to_vec(
            &package_window(window, reference),
            &[2.0, 1.0],
            Tolerance::absolute(1e-6),
        );
    }
}