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
//! A Sortformer, streamed: chunks, look-ahead, and the speaker cache.
use std::sync::Arc;

use burn::{
    Tensor,
    module::Module,
};

use super::{
    SilenceFill,
    SortformerStreamingConfig,
    SpeakerCache,
    blocks::{
        Sortformer,
        SortformerMeta,
    },
};
use crate::{
    contracts::unpack_shape_contract,
    errors::{
        BunsenError,
        BunsenResult,
    },
    kits::speech::{
        asr_driver::StreamingFeatures,
        diarization::{
            SpeakerActivity,
            SpeakerActivityStream,
        },
        nemo::{
            NemoMelFeatures,
            NemoMelFrontEnd,
            NemoMelFrontEndMeta,
        },
    },
};

/// A Sortformer over a stream of mel frames: the model run a chunk at a time
/// with its context, against a [`SpeakerCache`], `NeMo`'s sync streaming.
///
/// Mel frames go in through [`push`](Self::push) in pieces of any size. Each
/// step covers `chunk_len` encoder frames of mels; once those and the
/// look-ahead's are in, the step pre-encodes its window (the left context's
/// mels, the chunk's, the look-ahead's), runs the model over the cache, the
/// FIFO and that window, and its chunk's frames come out, final.
/// [`finish`](Self::finish) runs what is left, the last chunks with what
/// look-ahead there is. However the frames are split across calls, the output
/// is the same.
///
/// The output is at the model's output rate: one frame per
/// `subsampling_factor / output_upsampling` mel frames.
#[derive(Clone, Debug)]
pub struct SortformerMelStream {
    model: Arc<Sortformer>,

    cache: SpeakerCache,

    /// Mel frames from `mels_start` on, `[frames, n_mels]`: the next step's
    /// left context and everything after.
    mels: Option<Tensor<2>>,

    /// The stream's mel frame `mels` starts at.
    mels_start: usize,

    /// The mel frame the next step's chunk starts at.
    next_chunk: usize,

    /// Mel frames taken in.
    mel_frames: usize,

    /// Output frames emitted.
    emitted: usize,

    finished: bool,
}

impl SortformerMelStream {
    /// A stream through `model` under `config`. The cache's silence slots
    /// hold the model's learned silence embedding when it has one
    /// (`Nemotron-3-Diarization`), and a running mean of the silent frames
    /// otherwise (Sortformer v2).
    ///
    /// # Errors
    /// As [`SortformerStreamingConfig::check`].
    pub fn new(
        model: Arc<Sortformer>,
        config: SortformerStreamingConfig,
    ) -> BunsenResult<Self> {
        let silence = match &model.silence {
            Some(silence) => SilenceFill::Learned(silence.val()),
            None => SilenceFill::running_mean(model.d_model(), &model.projection.weight.device()),
        };
        let cache = SpeakerCache::new(config, model.n_speakers(), silence)?;
        Ok(Self {
            model,
            cache,
            mels: None,
            mels_start: 0,
            next_chunk: 0,
            mel_frames: 0,
            emitted: 0,
            finished: false,
        })
    }

    /// The model.
    pub fn model(&self) -> &Arc<Sortformer> {
        &self.model
    }

    /// The speaker cache.
    pub fn cache(&self) -> &SpeakerCache {
        &self.cache
    }

    /// The mel frames per output frame.
    pub fn mels_per_output(&self) -> usize {
        self.model.subsampling_factor() / self.model.output_upsampling()
    }

    /// Takes mel frames in, and returns the output frames they finalize.
    ///
    /// # Arguments
    /// * `mels` - `[frames, n_mels]`, following the frames of the previous
    ///   call.
    ///
    /// # Returns
    /// `frames * n_speakers` probabilities, frame-major.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal) after
    /// [`finish`](Self::finish).
    pub fn push(
        &mut self,
        mels: Tensor<2>,
    ) -> BunsenResult<Vec<f32>> {
        if self.finished {
            return Err(BunsenError::illegal(
                "the stream has ended; nothing more can be fed",
            ));
        }
        let [frames] = unpack_shape_contract!(
            ["frames", "n_mels"],
            &mels.dims(),
            &["frames"],
            &[("n_mels", self.model.n_mels())],
        );
        if frames == 0 {
            return Ok(Vec::new());
        }
        self.mel_frames += frames;
        self.mels = cat_present(self.mels.take(), Some(mels));

        let c = self.cache.config().clone();
        let factor = self.model.subsampling_factor();
        let (chunk, look_ahead) = (c.chunk_len * factor, c.right_context * factor);
        let mut out = Vec::new();
        while self.next_chunk + chunk + look_ahead <= self.mel_frames {
            out.extend(self.step(self.next_chunk + chunk, look_ahead)?);
        }
        Ok(out)
    }

    /// Ends the input, and returns the output frames left: the last chunks
    /// run with what look-ahead there is, the last partial stack padded, and
    /// the output cut at the last mel frame.
    ///
    /// Idempotent.
    ///
    /// # Errors
    /// As the model.
    pub fn finish(&mut self) -> BunsenResult<Vec<f32>> {
        if self.finished {
            return Ok(Vec::new());
        }
        self.finished = true;

        let c = self.cache.config().clone();
        let factor = self.model.subsampling_factor();
        let mut out = Vec::new();
        while self.next_chunk < self.mel_frames {
            let end = (self.next_chunk + c.chunk_len * factor).min(self.mel_frames);
            let look_ahead = (c.right_context * factor).min(self.mel_frames - end);
            out.extend(self.step(end, look_ahead)?);
        }

        // The output ends with the last mel frame, not with its stack.
        let total = self.mel_frames.div_ceil(self.mels_per_output());
        let s = self.model.n_speakers();
        let over = self.emitted.saturating_sub(total);
        out.truncate(out.len() - over.min(out.len() / s) * s);
        self.emitted -= over;
        Ok(out)
    }

    /// Output frames emitted so far.
    pub fn emitted(&self) -> usize {
        self.emitted
    }

    /// One step, `NeMo`'s `forward_streaming_step`: the chunk's mels
    /// `[next_chunk, end)` with up to the configured left context before them
    /// and `look_ahead` mels after, pre-encoded together; the model over the
    /// cache, the FIFO and that window; the chunk's output out, its
    /// embeddings into the FIFO.
    fn step(
        &mut self,
        end: usize,
        look_ahead: usize,
    ) -> BunsenResult<Vec<f32>> {
        let factor = self.model.subsampling_factor();
        let left_context = self.cache.config().left_context * factor;
        let start = self.next_chunk;
        let left = left_context.min(start);
        let mels = self.mels.clone().expect("mel frames are pending");
        let window = mels.slice_dim(
            0,
            start - left - self.mels_start..end + look_ahead - self.mels_start,
        );
        let (embs, _) = self.model.pre_encode(window.unsqueeze_dim(0), None);
        let embs = embs.squeeze_dim::<2>(0);
        let n_embs = embs.dims()[0];
        // `NeMo`'s `lc_enc` and `rc_enc`.
        let (left_enc, right_enc) = (
            (left as f64 / factor as f64).round() as usize,
            look_ahead.div_ceil(factor),
        );
        let chunk = n_embs - left_enc - right_enc;
        let chunk_embs = embs.clone().slice_dim(0, left_enc..left_enc + chunk);

        let cached = self.cache.cached();
        let n_cached = cached.as_ref().map_or(0, |c| c.dims()[0]);
        let input = cat_present(cached, Some(embs)).expect("the window is present");
        let probs = self.model.forward_pre_encoded(input.unsqueeze_dim(0), None);

        let up = self.model.output_upsampling();
        let s = self.model.n_speakers();
        let first = n_cached + left_enc;
        let out: Vec<f32> = probs
            .output
            .slice_dim(1, first * up..(first + chunk) * up)
            .into_data()
            .try_into_vec_as::<f32>()
            .expect("read back f32 probabilities");
        // The cache scores the cached frames and the chunk's, its context's
        // left out.
        let step: Vec<f32> = Tensor::cat(
            vec![
                probs.encoder_rate.clone().slice_dim(1, 0..n_cached),
                probs.encoder_rate.slice_dim(1, first..first + chunk),
            ],
            1,
        )
        .into_data()
        .try_into_vec_as::<f32>()
        .expect("read back f32 probabilities");
        self.cache.update(&step, chunk_embs)?;
        self.emitted += out.len() / s;

        // Keep what the next step's left context needs, and after.
        self.next_chunk = end;
        let keep_from = end.saturating_sub(left_context).max(self.mels_start);
        let n_mels = mels_len(&self.mels);
        self.mels = (keep_from - self.mels_start < n_mels).then(|| {
            self.mels
                .take()
                .expect("mel frames are pending")
                .slice_dim(0, keep_from - self.mels_start..n_mels)
        });
        self.mels_start = keep_from;
        Ok(out)
    }
}

/// The frames `mels` holds.
fn mels_len(mels: &Option<Tensor<2>>) -> usize {
    mels.as_ref().map_or(0, |m| m.dims()[0])
}

/// A Sortformer over a stream of samples: `NeMo`'s mel front end, streamed,
/// into a [`SortformerMelStream`], as a diarizer stream
/// ([`SpeakerActivityStream`]).
///
/// Its mel frames are the one-shot front end's, however the samples are
/// split, so its activity is the one a whole recording would give.
#[derive(Clone, Debug)]
pub struct SortformerStream {
    features: NemoMelFeatures,
    hop: usize,
    mels: SortformerMelStream,

    /// Samples taken in.
    samples: usize,

    /// Mel frames handed on.
    handed: usize,

    /// Mel frames from the front end that the samples so far do not yet
    /// show are valid, `[frames, n_mels]`.
    held: Option<Tensor<2>>,
}

impl SortformerStream {
    /// A stream through `front_end` and `model`, under `config`.
    ///
    /// # Errors
    /// As [`SortformerMelStream::new`];
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal) if the front end's
    /// mel count is not the model's.
    pub fn new(
        model: Arc<Sortformer>,
        front_end: NemoMelFrontEnd,
        config: SortformerStreamingConfig,
    ) -> BunsenResult<Self> {
        if front_end.n_mels() != model.n_mels() {
            return Err(BunsenError::illegal(format!(
                "SortformerStream: the front end makes {} mels, the model reads {}",
                front_end.n_mels(),
                model.n_mels()
            )));
        }
        let device = model.devices()[0].clone();
        let hop = front_end.hop();
        Ok(Self {
            features: NemoMelFeatures::new(front_end, device),
            hop,
            mels: SortformerMelStream::new(model, config)?,
            samples: 0,
            handed: 0,
            held: None,
        })
    }

    /// The mel-level stream underneath.
    pub fn mel_stream(&self) -> &SortformerMelStream {
        &self.mels
    }

    /// The samples in one output frame.
    pub fn frame_samples(&self) -> usize {
        self.hop * self.mels.mels_per_output()
    }

    /// The activity of the output frames in `probs`, after those emitted
    /// before them.
    fn activity(
        &self,
        probs: Vec<f32>,
    ) -> BunsenResult<SpeakerActivity> {
        let s = self.mels.model.n_speakers();
        let frames = probs.len() / s;
        let start = (self.mels.emitted() - frames) * self.frame_samples();
        SpeakerActivity::try_new(start, self.frame_samples(), s, probs)
    }

    /// Hands on the held mel frames below `limit` (a frame index).
    fn hand_on(
        &mut self,
        limit: usize,
    ) -> BunsenResult<Vec<f32>> {
        let Some(held) = self.held.take() else {
            return Ok(Vec::new());
        };
        let n = held.dims()[0];
        let take = limit.saturating_sub(self.handed).min(n);
        if take < n {
            self.held = Some(held.clone().slice_dim(0, take..n));
        }
        if take == 0 {
            return Ok(Vec::new());
        }
        self.handed += take;
        self.mels.push(held.slice_dim(0, 0..take))
    }

    fn hold(
        &mut self,
        frames: Option<Tensor<3>>,
    ) {
        if let Some(frames) = frames {
            self.held = cat_present(self.held.take(), Some(frames.squeeze_dim(0)));
        }
    }
}

impl SpeakerActivityStream for SortformerStream {
    fn push(
        &mut self,
        samples: &[f32],
    ) -> BunsenResult<SpeakerActivity> {
        self.samples += samples.len();
        let frames = self.features.push(samples)?;
        self.hold(frames);
        // `NeMo` keeps `samples / hop` mel frames: the samples so far vouch
        // for that many.
        let probs = self.hand_on(self.samples / self.hop)?;
        self.activity(probs)
    }

    fn finish(&mut self) -> BunsenResult<SpeakerActivity> {
        let frames = self.features.finish()?;
        self.hold(frames);
        let mut probs = self.hand_on(self.samples / self.hop)?;
        self.held = None;
        probs.extend(self.mels.finish()?);
        self.activity(probs)
    }
}

/// `a` then `b` along the frames, either possibly absent.
fn cat_present(
    a: Option<Tensor<2>>,
    b: Option<Tensor<2>>,
) -> Option<Tensor<2>> {
    match (a, b) {
        (Some(a), Some(b)) => Some(Tensor::cat(vec![a, b], 0)),
        (a, b) => a.or(b),
    }
}