bunsen 0.40.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
//! The `FastConformer`'s depthwise-striding subsampling.
use burn::{
    Tensor,
    config::Config,
    module::Module,
    nn::{
        Linear,
        LinearConfig,
        PaddingConfig2d,
        conv::Conv2dConfig,
    },
    tensor::Device,
};

use crate::{
    blocks::conv::{
        ConvBlock2d,
        ConvBlock2dConfig,
    },
    burner::{
        module::{
            ModuleInit,
            Rekey,
        },
        tensor::{
            permute_data,
            reshape_data,
        },
    },
    errors::{
        BunsenResult,
        ConstraintError,
        Rule,
    },
    ops::transformers::attention::length_mask,
};

/// How [`DwStridingSubsampling`] flattens its last convolution's output,
/// `[channels, n_freq]` per frame, into the vector its projection reads.
///
/// A layout choice with a loading consequence: the projection's weight
/// columns follow the flatten, so a checkpoint flattened the other way loads
/// through a key map that permutes them
/// ([`DwStridingSubsamplingMeta::reflatten_from`]).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FlattenOrder {
    /// Channels outer, frequencies inner.
    #[default]
    ChannelsFreqs,

    /// Frequencies outer, channels inner.
    FreqsChannels,
}

/// Common meta for [`DwStridingSubsampling`] and
/// [`DwStridingSubsamplingConfig`], and the key-map helpers that follow from
/// it.
pub trait DwStridingSubsamplingMeta {
    /// The mel channels it reads.
    fn n_mels(&self) -> usize;

    /// The convolution channel count.
    fn channels(&self) -> usize;

    /// The output width.
    fn d_model(&self) -> usize;

    /// The number of strided stages.
    fn n_stages(&self) -> usize;

    /// The time (and frequency) reduction: `2^n_stages`.
    fn factor(&self) -> usize {
        1 << self.n_stages()
    }

    /// The output length for an input of `len` frames.
    ///
    /// Each stage is a kernel-3, stride-2 convolution padded by one on each
    /// side, so it maps `len` to `(len - 1) / 2 + 1`.
    fn output_len(
        &self,
        len: usize,
    ) -> usize {
        (0..self.n_stages()).fold(len, |len, _| if len == 0 { 0 } else { (len - 1) / 2 + 1 })
    }

    /// The frequency bins left after subsampling.
    fn n_freq_out(&self) -> usize;

    /// How the last convolution's output is flattened for the projection.
    fn flatten(&self) -> FlattenOrder;

    /// A flat list of its layers' tensors, `{prefix}{k}.*` as a `PyTorch`
    /// `nn.Sequential` of them indexes them, onto its module paths, relative
    /// to it: `0` the stem's convolution, then each stage's depthwise and
    /// pointwise convolutions, the activations between them counted. An
    /// activation's index, or one past the stages, is dropped; anything else
    /// is kept as it is.
    fn sequential_rekey(
        &self,
        prefix: &'static str,
    ) -> Rekey {
        let n_stages = self.n_stages();
        Rekey::keep_rest().custom(move |name| {
            let (k, param) = name.strip_prefix(prefix)?.split_once('.')?;
            let module = k.parse().ok().and_then(|k| dw_striding_module(k, n_stages));
            Some(module.map(|module| format!("{module}.{param}")))
        })
    }

    /// `rekey`, and, when `from`, the order a checkpoint flattens in, is not
    /// this one's, the projection's weight (`linear.weight`) permuted to
    /// match: in `PyTorch`'s `[out, in]`, `[d_model, a * b]` to
    /// `[d_model, b * a]`.
    fn reflatten_from(
        &self,
        from: FlattenOrder,
        rekey: Rekey,
    ) -> Rekey {
        if from == self.flatten() {
            return rekey;
        }
        let (c, f) = (self.channels(), self.n_freq_out());
        let (a, b) = match from {
            FlattenOrder::ChannelsFreqs => (c, f),
            FlattenOrder::FreqsChannels => (f, c),
        };
        rekey.transform("linear.weight", move |data| {
            let d = data.shape().num_elements() / (a * b);
            let data = reshape_data(data, vec![d, a, b]);
            reshape_data(permute_data(data, &[0, 2, 1]), vec![d, b * a])
        })
    }
}

/// Config for [`DwStridingSubsampling`].
///
/// Implements [`DwStridingSubsamplingMeta`].
#[derive(Config, Debug)]
pub struct DwStridingSubsamplingConfig {
    /// The feature (mel) count of the input.
    pub n_mels: usize,

    /// The convolution channel count.
    pub channels: usize,

    /// The output width.
    pub d_model: usize,

    /// The number of strided stages: 3 for 8x subsampling.
    #[config(default = "3")]
    pub n_stages: usize,

    /// How the last convolution's output is flattened for the projection.
    #[config(default = "FlattenOrder::ChannelsFreqs")]
    pub flatten: FlattenOrder,
}

impl DwStridingSubsamplingMeta for DwStridingSubsamplingConfig {
    fn n_mels(&self) -> usize {
        self.n_mels
    }

    fn channels(&self) -> usize {
        self.channels
    }

    fn d_model(&self) -> usize {
        self.d_model
    }

    fn n_stages(&self) -> usize {
        self.n_stages
    }

    fn n_freq_out(&self) -> usize {
        self.output_len(self.n_mels)
    }

    fn flatten(&self) -> FlattenOrder {
        self.flatten
    }
}

impl ModuleInit<DwStridingSubsampling> for DwStridingSubsamplingConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<DwStridingSubsampling> {
        if self.n_stages == 0 {
            return Err(ConstraintError::new(
                "DwStridingSubsamplingConfig",
                "n_stages",
                Rule::ZeroOrEmpty,
            )
            .into());
        }
        let c = self.channels;
        let strided = |c_in: usize, groups: usize| {
            Conv2dConfig::new([c_in, c], [3, 3])
                .with_stride([2, 2])
                .with_padding(PaddingConfig2d::Explicit(1, 1, 1, 1))
                .with_groups(groups)
        };
        let stem = ConvBlock2dConfig::new(strided(1, 1)).try_init(device)?;
        let mut stages = Vec::with_capacity(self.n_stages - 1);
        for _ in 1..self.n_stages {
            stages.push(DwStridingStage {
                depthwise: ConvBlock2dConfig::new(strided(c, c))
                    .with_act(None)
                    .try_init(device)?,
                pointwise: ConvBlock2dConfig::new(Conv2dConfig::new([c, c], [1, 1]))
                    .try_init(device)?,
            });
        }
        Ok(DwStridingSubsampling {
            stem,
            stages,
            linear: LinearConfig::new(c * self.n_freq_out(), self.d_model).init(device),
            n_mels: self.n_mels,
            flatten: self.flatten,
        })
    }
}

/// One later stage of [`DwStridingSubsampling`]: a strided depthwise
/// convolution, then a pointwise convolution and `ReLU`.
#[derive(Module, Debug)]
pub struct DwStridingStage {
    /// Kernel 3, stride 2, one group per channel; no activation.
    pub depthwise: ConvBlock2d,

    /// Kernel 1, then `ReLU`.
    pub pointwise: ConvBlock2d,
}

/// The `FastConformer`'s depthwise-striding subsampling (`dw_striding` in
/// `NeMo`): `n_stages` stride-2 convolutions over time and frequency, then a
/// projection of the flattened channels and remaining frequencies to
/// `d_model`.
///
/// The first stage is a full 3x3 convolution from one channel and `ReLU`; each
/// later one is a [`DwStridingStage`]. Rybakov et al. (2023), "Fast
/// Conformer".
///
/// Padding frames are zeroed after every convolution, so a batch of
/// different-length inputs computes what each would alone.
///
/// Built by [`DwStridingSubsamplingConfig`].
/// Implements [`DwStridingSubsamplingMeta`].
#[derive(Module, Debug)]
pub struct DwStridingSubsampling {
    /// The first stage: kernel 3, stride 2, `1 -> channels`, then `ReLU`.
    pub stem: ConvBlock2d,

    /// The later stages.
    pub stages: Vec<DwStridingStage>,

    /// `channels * n_freq_out -> d_model`.
    pub linear: Linear,

    /// The mel channels it reads: what `n_freq_out` rounds up from.
    #[module(skip)]
    pub n_mels: usize,

    /// How the last convolution's output is flattened for the projection.
    #[module(skip)]
    pub flatten: FlattenOrder,
}

impl DwStridingSubsamplingMeta for DwStridingSubsampling {
    fn n_mels(&self) -> usize {
        self.n_mels
    }

    fn channels(&self) -> usize {
        self.stem.conv.weight.dims()[0]
    }

    fn d_model(&self) -> usize {
        self.linear.weight.dims()[1]
    }

    fn n_stages(&self) -> usize {
        self.stages.len() + 1
    }

    fn n_freq_out(&self) -> usize {
        self.linear.weight.dims()[0] / self.channels()
    }

    fn flatten(&self) -> FlattenOrder {
        self.flatten
    }
}

impl DwStridingSubsampling {
    /// Subsamples features.
    ///
    /// # Arguments
    /// * `features` - `[batch, n_mels, time]`.
    /// * `lengths` - each item's valid frame count, `batch` of them; `None`
    ///   means every item fills `time`.
    ///
    /// # Returns
    /// `[batch, output_len(time), d_model]`, and each item's output length.
    pub fn forward(
        &self,
        features: Tensor<3>,
        lengths: Option<&[usize]>,
    ) -> (Tensor<3>, Vec<usize>) {
        let [batch, _, time] = features.dims();
        let device = features.device();
        let mut lengths: Vec<usize> = match lengths {
            Some(lengths) => {
                assert_eq!(lengths.len(), batch, "one length per batch item");
                lengths.to_vec()
            }
            None => vec![time; batch],
        };
        let step = |lengths: &mut Vec<usize>| {
            for len in lengths.iter_mut() {
                *len = if *len == 0 { 0 } else { (*len - 1) / 2 + 1 };
            }
        };
        let masked = |x: Tensor<4>, lengths: &[usize]| {
            let t = x.dims()[2];
            x * length_mask(lengths, t, &device)
                .float()
                .reshape([batch as i32, 1, t as i32, 1])
        };

        // [batch, 1, time, n_mels]: time and frequency as image axes.
        let mut x = features.swap_dims(1, 2).unsqueeze_dim::<4>(1);
        x = self.stem.forward(x);
        step(&mut lengths);
        x = masked(x, &lengths);
        for stage in &self.stages {
            x = stage.depthwise.forward(x);
            step(&mut lengths);
            x = masked(x, &lengths);
            x = masked(stage.pointwise.forward(x), &lengths);
        }

        #[cfg(any(debug_assertions, test))]
        crate::contracts::assert_shape_contract_periodically!(
            ["batch", "channels", "time", "n_freq"],
            &x.dims(),
            &[
                ("batch", batch),
                ("channels", self.channels()),
                ("time", self.output_len(time)),
                ("n_freq", self.n_freq_out()),
            ],
        );
        let [_, c, t, f] = x.dims();
        let x = match self.flatten {
            FlattenOrder::ChannelsFreqs => x.swap_dims(1, 2).reshape([batch, t, c * f]),
            FlattenOrder::FreqsChannels => x.permute([0, 2, 3, 1]).reshape([batch, t, f * c]),
        };
        let y = self.linear.forward(x);

        #[cfg(any(debug_assertions, test))]
        crate::contracts::assert_shape_contract_periodically!(
            ["batch", "time", "d_model"],
            &y.dims(),
            &[
                ("batch", batch),
                ("time", self.output_len(time)),
                ("d_model", self.d_model()),
            ],
        );

        (y, lengths)
    }
}

/// The stem or stage module a flat `ModuleList` index names, under
/// [`DwStridingSubsampling`]'s layout: `0` is the stem's convolution; from
/// `2`, each stage is a depthwise convolution, a pointwise convolution and an
/// activation, in threes. `None` for an activation, or past the stages.
fn dw_striding_module(
    k: usize,
    n_stages: usize,
) -> Option<String> {
    match k {
        0 => Some("stem.conv".to_string()),
        k if k >= 2 && (k - 2) / 3 + 1 < n_stages => match (k - 2) % 3 {
            0 => Some(format!("stages.{}.depthwise.conv", (k - 2) / 3)),
            1 => Some(format!("stages.{}.pointwise.conv", (k - 2) / 3)),
            _ => None,
        },
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::support::testing::cpu_device;

    #[test]
    fn test_meta() {
        let config = DwStridingSubsamplingConfig::new(24, 4, 16);
        assert_eq!(config.factor(), 8);
        assert_eq!(config.n_freq_out(), 3);
        assert_eq!(config.output_len(53), 7);
        assert_eq!(config.output_len(37), 5);
        assert_eq!(config.output_len(8), 1);
        assert_eq!(config.output_len(0), 0);

        let module: DwStridingSubsampling = config.init(&cpu_device());
        assert_eq!(module.n_mels(), 24);
        assert_eq!(module.n_mels(), config.n_mels());
        assert_eq!(module.channels(), config.channels());
        assert_eq!(module.d_model(), config.d_model());
        assert_eq!(module.n_stages(), config.n_stages());
        assert_eq!(module.n_freq_out(), config.n_freq_out());

        let (y, lengths) =
            module.forward(Tensor::zeros([2, 24, 53], &cpu_device()), Some(&[53, 37]));
        assert_eq!(y.dims(), [2, 7, 16]);
        assert_eq!(lengths, [7, 5]);
    }
}