speakrs 0.6.0

Fast Rust speaker diarization with pyannote-level accuracy and native CPU/CoreML/CUDA inference
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
//! Bounded channels-first cross-correlation

use std::num::NonZeroUsize;

use ndarray::{Array2, ArrayView2, ArrayView3, ArrayViewMut2, ArrayViewMut3, Axis, s};

use super::gemm::matmul;
use crate::inference::{InferenceError, TensorShapeError};

const TILE: usize = 1024;
const CONTEXT: &str = "CPU convolution";

#[derive(Debug, Clone, Copy)]
struct Kernel {
    input: NonZeroUsize,
    output: NonZeroUsize,
    kernel: NonZeroUsize,
    stride: NonZeroUsize,
    rows: usize,
}

impl Kernel {
    fn new(
        input: usize,
        output: usize,
        kernel: usize,
        stride: usize,
        rank: usize,
    ) -> Result<Self, TensorShapeError> {
        let input = nonzero(input)?;
        let output = nonzero(output)?;
        let kernel = nonzero(kernel)?;
        let stride = nonzero(stride)?;
        let mut rows = input.get();
        for _ in 0..rank {
            rows = product(&[rows, kernel.get()])?;
        }
        product(&[rows, output.get()])?;
        product(&[rows, TILE])?;
        product(&[output.get(), TILE])?;
        Ok(Self {
            input,
            output,
            kernel,
            stride,
            rows,
        })
    }
}

fn nonzero(value: usize) -> Result<NonZeroUsize, TensorShapeError> {
    NonZeroUsize::new(value)
        .filter(|value| value.get() <= isize::MAX as usize)
        .ok_or(TensorShapeError::InvalidDimension {
            context: CONTEXT,
            dimension: i64::try_from(value).unwrap_or(i64::MAX),
        })
}

fn product(values: &[usize]) -> Result<usize, TensorShapeError> {
    values.iter().try_fold(1usize, |result, value| {
        result
            .checked_mul(*value)
            .filter(|result| *result <= isize::MAX as usize)
            .ok_or(TensorShapeError::Overflow { context: CONTEXT })
    })
}

fn length(actual: usize, expected: usize) -> Result<(), TensorShapeError> {
    if actual == expected {
        return Ok(());
    }
    Err(TensorShapeError::LengthMismatch {
        context: CONTEXT,
        expected,
        actual,
    })
}

fn shape(actual: &[usize], expected: &[usize]) -> Result<(), TensorShapeError> {
    if actual == expected {
        return Ok(());
    }
    Err(TensorShapeError::ShapeMismatch {
        context: CONTEXT,
        expected: expected.to_vec(),
        actual: actual.to_vec(),
    })
}

fn axis_len(input: usize, kernel: Kernel, padding: usize) -> Result<usize, TensorShapeError> {
    nonzero(input)?;
    let padded = padding
        .checked_mul(2)
        .and_then(|pad| input.checked_add(pad))
        .filter(|value| *value <= isize::MAX as usize)
        .ok_or(TensorShapeError::Overflow { context: CONTEXT })?;
    if padded < kernel.kernel.get() {
        return Err(TensorShapeError::AxisMismatch {
            context: CONTEXT,
            axis: 0,
            expected: kernel.kernel.get(),
            actual: padded,
        });
    }
    Ok((padded - kernel.kernel.get()) / kernel.stride.get() + 1)
}

/// Valid non-zero one-dimensional convolution geometry
#[derive(Debug, Clone, Copy)]
pub(crate) struct Conv1dSpec(Kernel);

impl Conv1dSpec {
    /// Checks geometry and bounded workspace sizes before packing weights
    pub(crate) fn new(
        in_channels: usize,
        out_channels: usize,
        kernel: usize,
        stride: usize,
    ) -> Result<Self, TensorShapeError> {
        Ok(Self(Kernel::new(
            in_channels,
            out_channels,
            kernel,
            stride,
            1,
        )?))
    }

    /// Checks the unpadded output length and total output size
    pub(crate) fn output_len(self, input_len: usize) -> Result<usize, TensorShapeError> {
        let len = axis_len(input_len, self.0, 0)?;
        product(&[self.0.input.get(), input_len])?;
        product(&[self.0.output.get(), len])?;
        Ok(len)
    }
}

/// Packed immutable one-dimensional cross-correlation weights
#[derive(Debug)]
pub(crate) struct Conv1d {
    spec: Conv1dSpec,
    weight: Array2<f32>,
    bias: Option<Vec<f32>>,
}

impl Conv1d {
    /// Validates and packs `[out,in,kernel]` weights in row-major order
    pub(crate) fn new(
        spec: Conv1dSpec,
        weight: Vec<f32>,
        bias: Option<Vec<f32>>,
    ) -> Result<Self, TensorShapeError> {
        let kernel = spec.0;
        length(weight.len(), kernel.output.get() * kernel.rows)?;
        if let Some(bias) = &bias {
            length(bias.len(), kernel.output.get())?;
        }
        let weight = Array2::from_shape_vec((kernel.output.get(), kernel.rows), weight)
            .map_err(|_| TensorShapeError::Overflow { context: CONTEXT })?;
        Ok(Self { spec, weight, bias })
    }

    /// Writes channels/time output using private bounded scratch
    pub(crate) fn forward(
        &self,
        input: ArrayView2<'_, f32>,
        mut output: ArrayViewMut2<'_, f32>,
        workspace: &mut ConvWorkspace,
    ) -> Result<(), InferenceError> {
        let kernel = self.spec.0;
        let out_len = self.spec.output_len(input.ncols())?;
        shape(input.shape(), &[kernel.input.get(), input.ncols()])?;
        shape(output.shape(), &[kernel.output.get(), out_len])?;
        workspace.prepare(kernel.rows, kernel.output.get());
        for start in (0..out_len).step_by(TILE) {
            let count = TILE.min(out_len - start);
            for channel in 0..kernel.input.get() {
                let source = input.row(channel);
                for offset in 0..kernel.kernel.get() {
                    let mut target = workspace
                        .columns
                        .row_mut(channel * kernel.kernel.get() + offset);
                    let first = start * kernel.stride.get() + offset;
                    if kernel.stride.get() == 1
                        && let Some(source) = source.as_slice()
                    {
                        target
                            .as_slice_mut()
                            .ok_or(InferenceError::NonContiguousBuffer { context: CONTEXT })?
                            [..count]
                            .copy_from_slice(&source[first..first + count]);
                    } else {
                        for index in 0..count {
                            target[index] = source[first + index * kernel.stride.get()];
                        }
                    }
                }
            }
            let columns = workspace.columns.slice(s![..kernel.rows, ..count]);
            let mut tile = output.slice_mut(s![.., start..start + count]);
            matmul(self.weight.view(), columns, tile.view_mut(), 0.0)?;
            if let Some(bias) = &self.bias {
                for (mut row, bias) in tile.axis_iter_mut(Axis(0)).zip(bias) {
                    row.mapv_inplace(|value| value + bias);
                }
            }
        }
        Ok(())
    }
}

/// Valid square two-dimensional convolution geometry
#[derive(Debug, Clone, Copy)]
pub(crate) struct Conv2dSpec {
    kernel: Kernel,
    padding: usize,
}

impl Conv2dSpec {
    /// Checks geometry, padding and bounded workspace sizes
    pub(crate) fn new(
        in_channels: usize,
        out_channels: usize,
        kernel: usize,
        stride: usize,
        padding: usize,
    ) -> Result<Self, TensorShapeError> {
        product(&[padding, 2])?;
        Ok(Self {
            kernel: Kernel::new(in_channels, out_channels, kernel, stride, 2)?,
            padding,
        })
    }

    /// Checks height/width and input/output tensor products
    pub(crate) fn output_shape(
        self,
        height: usize,
        width: usize,
    ) -> Result<(usize, usize), TensorShapeError> {
        let output = (
            axis_len(height, self.kernel, self.padding)?,
            axis_len(width, self.kernel, self.padding)?,
        );
        product(&[self.kernel.input.get(), height, width])?;
        product(&[self.kernel.output.get(), output.0, output.1])?;
        Ok(output)
    }
}

/// Packed immutable two-dimensional cross-correlation weights and bias
#[derive(Debug)]
pub(crate) struct Conv2d {
    spec: Conv2dSpec,
    weight: Array2<f32>,
    bias: Vec<f32>,
}

impl Conv2d {
    /// Validates and packs `[out,in,kernel,kernel]` weights in row-major order
    pub(crate) fn new(
        spec: Conv2dSpec,
        weight: Vec<f32>,
        bias: Vec<f32>,
    ) -> Result<Self, TensorShapeError> {
        let kernel = spec.kernel;
        length(weight.len(), kernel.output.get() * kernel.rows)?;
        length(bias.len(), kernel.output.get())?;
        let weight = Array2::from_shape_vec((kernel.output.get(), kernel.rows), weight)
            .map_err(|_| TensorShapeError::Overflow { context: CONTEXT })?;
        Ok(Self { spec, weight, bias })
    }

    /// Writes channels/height/width output, with no activation or residual fusion
    pub(crate) fn forward(
        &self,
        input: ArrayView3<'_, f32>,
        mut output: ArrayViewMut3<'_, f32>,
        workspace: &mut ConvWorkspace,
    ) -> Result<(), InferenceError> {
        let kernel = self.spec.kernel;
        let (height, width) = self.spec.output_shape(input.shape()[1], input.shape()[2])?;
        shape(
            input.shape(),
            &[kernel.input.get(), input.shape()[1], input.shape()[2]],
        )?;
        shape(output.shape(), &[kernel.output.get(), height, width])?;
        workspace.prepare(kernel.rows, kernel.output.get());
        for start in (0..height * width).step_by(TILE) {
            let count = TILE.min(height * width - start);
            self.gather(input, width, start, count, workspace)?;
            // contiguous outputs use their native channel stride without a tile copy
            if let Some(flat) = output.as_slice_mut() {
                use ndarray::ShapeBuilder;
                let tile = ArrayViewMut2::from_shape(
                    (kernel.output.get(), count).strides((height * width, 1)),
                    &mut flat[start..],
                )
                .map_err(|source| InferenceError::OutputArray {
                    context: CONTEXT,
                    source,
                })?;
                matmul(
                    self.weight.view(),
                    workspace.columns.slice(s![..kernel.rows, ..count]),
                    tile,
                    0.0,
                )?;
                for (channel, bias) in self.bias.iter().enumerate() {
                    let first = channel * height * width + start;
                    for value in &mut flat[first..first + count] {
                        *value += bias;
                    }
                }
                continue;
            }

            let tile = workspace
                .result
                .slice_mut(s![..kernel.output.get(), ..count]);
            matmul(
                self.weight.view(),
                workspace.columns.slice(s![..kernel.rows, ..count]),
                tile,
                0.0,
            )?;
            for channel in 0..kernel.output.get() {
                for local in 0..count {
                    let position = start + local;
                    output[[channel, position / width, position % width]] =
                        workspace.result[[channel, local]] + self.bias[channel];
                }
            }
        }
        Ok(())
    }

    fn gather(
        &self,
        input: ArrayView3<'_, f32>,
        out_width: usize,
        start: usize,
        count: usize,
        workspace: &mut ConvWorkspace,
    ) -> Result<(), InferenceError> {
        let kernel = self.spec.kernel;
        let height = input.shape()[1];
        let width = input.shape()[2];
        let padding = self.spec.padding as isize;
        for channel in 0..kernel.input.get() {
            let plane = input.index_axis(Axis(0), channel);
            for ky in 0..kernel.kernel.get() {
                for kx in 0..kernel.kernel.get() {
                    let row = (channel * kernel.kernel.get() + ky) * kernel.kernel.get() + kx;
                    let mut target = workspace.columns.row_mut(row);
                    let target = &mut target
                        .as_slice_mut()
                        .ok_or(InferenceError::NonContiguousBuffer { context: CONTEXT })?[..count];
                    let mut local = 0;
                    while local < count {
                        let position = start + local;
                        let y =
                            (position / out_width * kernel.stride.get() + ky) as isize - padding;
                        let x =
                            (position % out_width * kernel.stride.get() + kx) as isize - padding;
                        let span = (out_width - position % out_width).min(count - local);
                        let destination = &mut target[local..local + span];
                        local += span;
                        if y < 0 || y >= height as isize {
                            destination.fill(0.0);
                            continue;
                        }
                        let source = plane.row(y as usize);
                        if kernel.stride.get() == 1
                            && let Some(source) = source.as_slice()
                        {
                            let left = (-x).max(0).min(span as isize) as usize;
                            let right = (width as isize - x).max(0).min(span as isize) as usize;
                            destination[..left].fill(0.0);
                            destination[right..].fill(0.0);
                            if right > left {
                                let first = (x + left as isize) as usize;
                                destination[left..right]
                                    .copy_from_slice(&source[first..first + right - left]);
                            }
                            continue;
                        }
                        for (index, value) in destination.iter_mut().enumerate() {
                            let sx = x + (index * kernel.stride.get()) as isize;
                            *value = if sx >= 0 && sx < width as isize {
                                source[sx as usize]
                            } else {
                                0.0
                            };
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

/// Reusable private im2col and strided-output tiles, each bounded to1024 columns
#[derive(Debug, Default)]
pub(crate) struct ConvWorkspace {
    columns: Array2<f32>,
    result: Array2<f32>,
}

impl ConvWorkspace {
    /// Creates empty scratch that grows only when a convolution needs more rows
    pub(crate) fn new() -> Self {
        Self::default()
    }

    fn prepare(&mut self, rows: usize, outputs: usize) {
        if self.columns.nrows() < rows {
            self.columns = Array2::zeros((rows, TILE));
        }
        if self.result.nrows() < outputs {
            self.result = Array2::zeros((outputs, TILE));
        }
    }
}

#[cfg(test)]
mod tests;