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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Native CUDA PyanNet segmentation: what `segmentation-3.0.onnx` and
//! `segmentation-3.0-b32.onnx` compute, without ONNX Runtime
//!
//! - waveform instance normalization, then SincNet: 80 generated band-pass filters
//!   (selected convolution, stride 10), `abs`, max pool, instance normalization and
//!   LeakyReLU, then two selected kernel or cuDNN convolutions, each followed by max pool,
//!   instance normalization and LeakyReLU (`segmentation_pool_norm`)
//! - four bidirectional LSTM layers through one selected Oxide or Library stack
//! - three selected dense kernel or cuBLAS layers with LeakyReLU, then log-softmax over the 7
//!   powerset classes
//!
//! [`CudaSegmentation`] holds the weights and one [`SegmentationWorkspace`] per batch
//! shape it has run, so buffers, plans and captured graphs are allocated once per
//! batch class and reused

mod dispatch;
mod graph;
mod kernels;
#[cfg(test)]
pub(super) use kernels::REQUIRED_KERNELS;
#[cfg(feature = "_cuda-libraries")]
mod rnn;
mod shape;
mod weights;

use cudarc::driver::CudaSlice;

use self::graph::CapturedGraph;
use self::kernels::{PoolNorm, RowLayout, SegmentationKernels};
#[cfg(feature = "_cuda-libraries")]
use self::rnn::{CudnnLstm, LstmPlan};
use self::shape::{
    CONV_KERNEL, FEATURES, HIDDEN, LEAKY_SLOPE, LINEAR, NORM_EPSILON, POOL, SINC_CHANNELS,
};
#[cfg(feature = "_cuda-libraries")]
use self::shape::{SINC_KERNEL, SINC_STRIDE};
use self::weights::{LstmLayer, SegmentationWeights};
#[cfg(feature = "_cuda-libraries")]
use super::CudaLstmAlgorithm;
use super::buffer::unfold_windows;
use super::candidate::{
    DenseSite, DenseSpec, LstmOxide, LstmProjOxide, Phases, SegConvCandidate, SegConvOxide,
    SegConvSite, SegConvSpec, SincCandidate, SincOutput, SincOxide,
};
use super::dense::DensePlan;
#[cfg(feature = "_cuda-libraries")]
use super::dnn::{ConvPlan, ConvPlanner};
use super::geometry::Conv2d;
use super::implementation::{
    AreaTarget, BoundaryId, LibraryNeed, MODEL_BATCHES, Selected, plan_selection,
};

/// The two Library-owned temporal convolutions after the Sinc producer
const CONV1: BoundaryId = BoundaryId::named("sincnet.conv1");
const CONV2: BoundaryId = BoundaryId::named("sincnet.conv2");
/// The cuBLAS input projections nested in the pinned PR #36 LSTM stack
const LSTM_INPUT_PROJECTION: BoundaryId = BoundaryId::named("lstm.stack.input_proj");
/// The three linear layers after the LSTM
const LINEAR_BOUNDARIES: [BoundaryId; 3] = [
    BoundaryId::named("linear0"),
    BoundaryId::named("linear1"),
    BoundaryId::named("linear2"),
];
use super::{CudaError, CudaMath, CudaRuntime, DeviceTensor, PtxTier, SafetensorsFile, Sgemm};
use super::{CudaLibrary, KernelModule};

use self::shape::CLASSES;
pub use self::shape::SegmentationShape;

/// How the segmentation forward pass runs
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SegmentationOptions {
    /// Precision of every cuBLAS, cuDNN convolution and cuDNN RNN call
    pub math: CudaMath,
    /// cuDNN RNN algorithm for the LSTM stack
    #[cfg(feature = "_cuda-libraries")]
    pub lstm_algo: CudaLstmAlgorithm,
    /// Capture the forward pass of each workspace as a CUDA graph on its first run and
    /// replay it afterwards
    pub cuda_graph: bool,
}

/// The segmentation model on one [`CudaRuntime`]: device weights, kernels and a
/// workspace per batch shape
///
/// Use it only with the runtime it was created on
#[derive(Debug)]
pub struct CudaSegmentation {
    workspaces: Vec<SegmentationWorkspace>,
    network: Network,
    /// the audio of the last [`Self::run_span`], grown on demand
    span: Option<CudaSlice<f32>>,
}

#[cfg(all(test, feature = "_cuda-libraries"))]
pub use test_support::SegmentationTensor;

/// Buffers, convolution plans, LSTM plan and captured graph for one batch shape
#[derive(Debug)]
pub struct SegmentationWorkspace {
    shape: SegmentationShape,
    sinc: SincPlan,
    conv1: ConvStage,
    conv2: ConvStage,
    /// cuDNN workspace shared by the three convolutions, sized for the largest
    conv_workspace: CudaSlice<u8>,
    lstm: LstmStage,
    dense: [DensePlan; 3],
    tensors: Tensors,
    graph: Option<CapturedGraph>,
}

/// One owner for the Sinc producer
#[derive(Debug)]
enum SincPlan {
    #[cfg(feature = "_cuda-libraries")]
    Library(ConvPlan),
    Oxide(SincOxide),
}

impl SincPlan {
    fn workspace_bytes(&self) -> usize {
        match self {
            #[cfg(feature = "_cuda-libraries")]
            Self::Library(plan) => plan.workspace_bytes(),
            Self::Oxide(_) => 0,
        }
    }
}

/// One implementation owner for a raw temporal convolution
#[derive(Debug)]
enum ConvStage {
    Oxide(SegConvOxide),
    #[cfg(feature = "_cuda-libraries")]
    Library(ConvPlan),
}

impl ConvStage {
    fn new(
        runtime: &CudaRuntime,
        boundary: BoundaryId,
        spec: Conv2d,
        weight: &CudaSlice<f32>,
    ) -> Result<Self, CudaError> {
        let site = if boundary == CONV1 {
            SegConvSite::Conv1
        } else {
            SegConvSite::Conv2
        };
        let selected = plan_selection(
            runtime,
            boundary,
            spec.batch,
            spec.math,
            #[cfg(all(test, feature = "_cuda-libraries"))]
            None,
        )?;
        if let Selected::Oxide(token) = selected {
            let shape = SegConvSpec::new(site, spec.batch, spec.math).map_err(|error| {
                CudaError::Unsupported {
                    context: "temporal convolution",
                    reason: error.to_string(),
                }
            })?;
            if let Some(plan) = token.segconv(runtime, shape, spec, weight)? {
                return Ok(Self::Oxide(plan));
            }
        }
        LibraryNeed::new(
            boundary,
            spec.batch,
            spec.math,
            AreaTarget::for_area(runtime, boundary.area())?,
            CudaLibrary::Cudnn,
        )
        .prepare(runtime)?;
        #[cfg(feature = "_cuda-libraries")]
        return Ok(Self::Library(ConvPlanner::new(runtime)?.plan(spec)?));
        #[cfg(not(feature = "_cuda-libraries"))]
        Err(CudaError::LibraryUnavailable {
            library: CudaLibrary::Cudnn,
        })
    }

    fn workspace_bytes(&self) -> usize {
        match *self {
            Self::Oxide(_) => 0,
            #[cfg(feature = "_cuda-libraries")]
            Self::Library(ref plan) => plan.workspace_bytes(),
        }
    }

    fn forward(
        &self,
        runtime: &CudaRuntime,
        workspace: &mut cudarc::driver::CudaViewMut<'_, u8>,
        input: &cudarc::driver::CudaView<'_, f32>,
        weight: &cudarc::driver::CudaView<'_, f32>,
        output: &mut cudarc::driver::CudaViewMut<'_, f32>,
    ) -> Result<(), CudaError> {
        #[cfg(not(feature = "_cuda-libraries"))]
        let _ = (workspace, weight);
        match *self {
            Self::Oxide(ref plan) => plan.enqueue(input, output, &Phases::new(), runtime),
            #[cfg(feature = "_cuda-libraries")]
            Self::Library(ref plan) => plan.forward(workspace, input, weight, output),
        }
    }
}

/// One owner for the complete LSTM stack
#[derive(Debug)]
enum LstmStage {
    Projected {
        candidate: Box<LstmProjOxide>,
        rows: usize,
    },
    #[cfg(feature = "_cuda-libraries")]
    Library(LstmPlan),
    Oxide {
        candidate: Box<LstmOxide>,
        rows: usize,
    },
}

#[derive(Debug)]
struct Tensors {
    input: DeviceTensor,
    wave_norm: DeviceTensor,
    sinc: DeviceTensor,
    /// `[batch, 80, pool0]` written by a Sinc candidate that pools, allocated only for one
    sinc_pooled: Option<DeviceTensor>,
    stage0: DeviceTensor,
    conv1: DeviceTensor,
    stage1: DeviceTensor,
    conv2: DeviceTensor,
    lstm_input: DeviceTensor,
    lstm_output: DeviceTensor,
    linear0: DeviceTensor,
    linear1: DeviceTensor,
    output: DeviceTensor,
}

/// The weights and kernels; separate from the workspaces so a forward pass can
/// borrow both
#[derive(Debug)]
struct Network {
    options: SegmentationOptions,
    kernels: SegmentationKernels,
    #[cfg(feature = "_cuda-libraries")]
    lstm: Option<CudnnLstm>,
    /// Waveform normalization scale and shift, one value each
    wav_norm: [CudaSlice<f32>; 2],
    /// `[80, 1, 1, 251]`
    sinc_filters: CudaSlice<f32>,
    /// Learned convolution weights `[60, c, 1, 5]` and biases `[60]`
    convs: [[CudaSlice<f32>; 2]; 2],
    /// Scale and shift of the three SincNet normalizations
    norms: [[CudaSlice<f32>; 2]; 3],
    /// `MatMul` weights `[in, out]` and biases of the three linear layers
    linear: [[CudaSlice<f32>; 2]; 3],
    /// Zero bias for the stages without a convolution bias
    zero_bias: CudaSlice<f32>,
    /// Host LSTM weights for candidate plans
    lstm_weights: Vec<LstmLayer>,
}

impl CudaSegmentation {
    /// Powerset classes per output frame (3 speakers, at most 2 active)
    pub const CLASSES: usize = CLASSES;

    /// Uploads the weights of `segmentation-3.0` (or its `-b32` export) from a
    /// safetensors file keyed by ONNX initializer names
    pub fn new(
        runtime: &CudaRuntime,
        weights: &SafetensorsFile,
        options: SegmentationOptions,
    ) -> Result<Self, CudaError> {
        Ok(Self {
            network: Network::new(runtime, &SegmentationWeights::load(weights)?, options)?,
            workspaces: Vec::new(),
            span: None,
        })
    }

    /// The PTX tier the segmentation kernels were loaded from
    pub fn kernel_tier(&self) -> PtxTier {
        self.network.kernels.tier()
    }

    /// The workspace for `batch` windows of `samples` samples, allocated on first use
    pub fn workspace(
        &mut self,
        runtime: &CudaRuntime,
        batch: usize,
        samples: usize,
    ) -> Result<&mut SegmentationWorkspace, CudaError> {
        let index = self.workspace_index(runtime, batch, samples)?;
        Ok(&mut self.workspaces[index])
    }

    /// Uploads `batch` windows (`[batch, samples]`, `samples = input.len() / batch`),
    /// runs the model and returns the `[batch, frames, 7]` powerset log-probabilities
    pub fn run(
        &mut self,
        runtime: &CudaRuntime,
        batch: usize,
        input: &[f32],
    ) -> Result<Vec<f32>, CudaError> {
        let samples = input.len().checked_div(batch).unwrap_or(0);
        if samples * batch != input.len() {
            return Err(CudaError::BufferLength {
                context: "segmentation input (batch * samples)",
                expected: samples * batch,
                actual: input.len(),
            });
        }

        let index = self.workspace_index(runtime, batch, samples)?;
        let workspace = &mut self.workspaces[index];
        workspace.upload_input(runtime, input)?;
        self.network.forward(runtime, workspace)?;
        workspace.download_output(runtime)
    }

    /// [`Self::run`] for windows of `samples` cut out of one stretch of audio
    ///
    /// Row `i` is `span[starts[i]..starts[i] + samples]`, clipped at the end of `span`
    /// and zero padded, which is how [`Self::run`] pads a short window; the samples
    /// that overlapping windows share are copied and uploaded once
    pub fn run_span(
        &mut self,
        runtime: &CudaRuntime,
        samples: usize,
        span: &[f32],
        starts: &[usize],
    ) -> Result<Vec<f32>, CudaError> {
        let index = self.workspace_index(runtime, starts.len(), samples)?;
        let stream = runtime.stream();
        if self
            .span
            .as_ref()
            .is_none_or(|device| device.len() < span.len())
        {
            self.span = Some(stream.alloc_zeros(span.len().max(1))?);
        }
        let device = self.span.as_mut().expect("span buffer allocated above");
        if !span.is_empty() {
            stream.memcpy_htod(span, &mut device.slice_mut(..span.len()))?;
        }

        let workspace = &mut self.workspaces[index];
        unfold_windows(
            stream,
            &device.slice(..span.len()),
            starts,
            samples,
            workspace.tensors.input.data_mut(),
        )?;
        self.network.forward(runtime, workspace)?;
        workspace.download_output(runtime)
    }

    /// Queues an eager pass on the existing tuner workspace without host transfers
    pub(crate) fn enqueue_for_tuning(
        &mut self,
        runtime: &CudaRuntime,
        batch: usize,
        samples: usize,
    ) -> Result<(), CudaError> {
        let index = self.workspace_index(runtime, batch, samples)?;
        self.network.enqueue(runtime, &mut self.workspaces[index])
    }

    fn workspace_index(
        &mut self,
        runtime: &CudaRuntime,
        batch: usize,
        samples: usize,
    ) -> Result<usize, CudaError> {
        let existing = self
            .workspaces
            .iter()
            .position(|workspace| workspace.matches(batch, samples));
        if let Some(index) = existing {
            return Ok(index);
        }

        let shape = SegmentationShape::new(batch, samples)?;
        self.workspaces
            .push(self.network.workspace(runtime, shape)?);
        Ok(self.workspaces.len() - 1)
    }
}

impl Network {
    fn new(
        runtime: &CudaRuntime,
        weights: &SegmentationWeights,
        options: SegmentationOptions,
    ) -> Result<Self, CudaError> {
        let mut needs = Vec::new();
        for batch in MODEL_BATCHES {
            for boundary in [dispatch::SINC, CONV1, CONV2, dispatch::LSTM] {
                let target = AreaTarget::for_area(runtime, boundary.area())?;
                let selected = plan_selection(
                    runtime,
                    boundary,
                    batch,
                    options.math,
                    #[cfg(all(test, feature = "_cuda-libraries"))]
                    None,
                )?;
                if matches!(selected, Selected::Library) {
                    needs.push(LibraryNeed::new(
                        boundary,
                        batch,
                        options.math,
                        target,
                        CudaLibrary::Cudnn,
                    ));
                    #[cfg(feature = "_cuda-libraries")]
                    if boundary == dispatch::LSTM
                        && options.lstm_algo == CudaLstmAlgorithm::PersistDynamic
                    {
                        needs.push(LibraryNeed::new(
                            boundary,
                            batch,
                            options.math,
                            target,
                            CudaLibrary::Nvrtc,
                        ));
                    }
                } else if boundary == dispatch::LSTM
                    && matches!(&selected, Selected::Oxide(token) if token.area() == KernelModule::Lstm)
                {
                    needs.push(LibraryNeed::new(
                        LSTM_INPUT_PROJECTION,
                        batch,
                        options.math,
                        target,
                        CudaLibrary::Cublas,
                    ));
                }
            }
            for boundary in LINEAR_BOUNDARIES {
                if matches!(
                    plan_selection(
                        runtime,
                        boundary,
                        batch,
                        options.math,
                        #[cfg(all(test, feature = "_cuda-libraries"))]
                        None
                    )?,
                    Selected::Library
                ) {
                    needs.push(LibraryNeed::new(
                        boundary,
                        batch,
                        options.math,
                        AreaTarget::for_area(runtime, KernelModule::Segdense)?,
                        CudaLibrary::Cublas,
                    ));
                }
            }
        }
        if super::driver_only() {
            for need in &needs {
                need.prepare(runtime)?;
            }
        }
        // selected handles precede model buffers because their allocations affect recurrence locality
        for need in &needs {
            need.prepare_handle(runtime)?;
        }
        let stream = runtime.stream();
        let upload = |values: &[f32]| stream.clone_htod(values);
        let pair = |first: &[f32], second: &[f32]| -> Result<[CudaSlice<f32>; 2], CudaError> {
            Ok([upload(first)?, upload(second)?])
        };

        let [norm0, norm1, norm2] = &weights.norms;
        let [linear0, linear1, linear2] = &weights.linear;
        Ok(Self {
            options,
            kernels: SegmentationKernels::load(runtime)?,
            #[cfg(feature = "_cuda-libraries")]
            lstm: None,
            wav_norm: pair(&weights.wav_norm.gamma, &weights.wav_norm.beta)?,
            sinc_filters: upload(&weights.sinc_filters)?,
            convs: [
                pair(&weights.conv1.weight, &weights.conv1.bias)?,
                pair(&weights.conv2.weight, &weights.conv2.bias)?,
            ],
            norms: [
                pair(&norm0.gamma, &norm0.beta)?,
                pair(&norm1.gamma, &norm1.beta)?,
                pair(&norm2.gamma, &norm2.beta)?,
            ],
            linear: [
                pair(&linear0.weight, &linear0.bias)?,
                pair(&linear1.weight, &linear1.bias)?,
                pair(&linear2.weight, &linear2.bias)?,
            ],
            zero_bias: stream.alloc_zeros(SINC_CHANNELS)?,
            lstm_weights: weights.lstm.to_vec(),
        })
    }

    /// Allocates the buffers and plans for one batch shape
    fn workspace(
        &mut self,
        runtime: &CudaRuntime,
        shape: SegmentationShape,
    ) -> Result<SegmentationWorkspace, CudaError> {
        let batch = shape.batch;
        let math = self.options.math;
        let conv = |[in_channels, out_channels]: [usize; 2], input, kernel, stride| Conv2d {
            batch,
            in_channels,
            out_channels,
            input: [1, input],
            kernel: [1, kernel],
            padding: [0, 0],
            stride: [1, stride],
            dilation: [1, 1],
            math,
        };
        let stream = runtime.stream();
        let tensor = |dims: &[usize]| DeviceTensor::zeros(stream, dims);
        let tensors = Tensors {
            input: tensor(&[batch, 1, shape.samples])?,
            wave_norm: tensor(&[batch, 1, shape.samples])?,
            sinc: tensor(&[batch, SINC_CHANNELS, shape.sinc])?,
            sinc_pooled: None,
            stage0: tensor(&[batch, SINC_CHANNELS, shape.pool0])?,
            conv1: tensor(&[batch, FEATURES, shape.conv1])?,
            stage1: tensor(&[batch, FEATURES, shape.pool1])?,
            conv2: tensor(&[batch, FEATURES, shape.conv2])?,
            lstm_input: tensor(&[batch, shape.frames, FEATURES])?,
            lstm_output: tensor(&[batch, shape.frames, 2 * HIDDEN])?,
            linear0: tensor(&[batch, shape.frames, LINEAR[0][1]])?,
            linear1: tensor(&[batch, shape.frames, LINEAR[1][1]])?,
            output: tensor(&[batch, shape.frames, CLASSES])?,
        };

        let sinc_selected = plan_selection(
            runtime,
            dispatch::SINC,
            batch,
            math,
            #[cfg(all(test, feature = "_cuda-libraries"))]
            None,
        )?;
        let lstm_selected = plan_selection(
            runtime,
            dispatch::LSTM,
            batch,
            math,
            #[cfg(all(test, feature = "_cuda-libraries"))]
            None,
        )?;
        let sinc = self.plan_sinc(runtime, shape, sinc_selected)?;
        let conv1 = ConvStage::new(
            runtime,
            CONV1,
            conv([SINC_CHANNELS, FEATURES], shape.pool0, CONV_KERNEL, 1),
            &self.convs[0][0],
        )?;
        let conv2 = ConvStage::new(
            runtime,
            CONV2,
            conv([FEATURES, FEATURES], shape.pool1, CONV_KERNEL, 1),
            &self.convs[1][0],
        )?;
        let workspace_bytes = sinc
            .workspace_bytes()
            .max(conv1.workspace_bytes())
            .max(conv2.workspace_bytes());
        let mut tensors = tensors;
        if matches!(sinc, SincPlan::Oxide(_)) {
            tensors.sinc_pooled = pooled_tensor(runtime, shape)?;
        }
        // keep convolution scratch before recurrence state; allocation order affects replay latency
        let conv_workspace = stream.alloc_zeros(workspace_bytes.max(1))?;
        let lstm = self.plan_lstm(runtime, shape, lstm_selected)?;
        let dense = [
            DenseSite::Linear0,
            DenseSite::Linear1,
            DenseSite::Classifier,
        ]
        .into_iter()
        .enumerate()
        .map(|(index, site)| {
            let spec =
                DenseSpec::new(site, batch, math).map_err(|error| CudaError::Unsupported {
                    context: "dense shape",
                    reason: error.to_string(),
                })?;
            DensePlan::new(
                runtime,
                spec,
                shape.frames,
                &self.linear[index][0],
                &self.linear[index][1],
            )
        })
        .collect::<Result<Vec<_>, CudaError>>()?
        .try_into()
        .expect("three dense sites");
        Ok(SegmentationWorkspace {
            shape,
            sinc,
            conv1,
            conv2,
            conv_workspace,
            lstm,
            dense,
            tensors,
            graph: None,
        })
    }

    fn forward(
        &self,
        runtime: &CudaRuntime,
        workspace: &mut SegmentationWorkspace,
    ) -> Result<(), CudaError> {
        if !self.options.cuda_graph {
            return self.enqueue(runtime, workspace);
        }

        if workspace.graph.is_none() {
            let graph = CapturedGraph::capture(runtime, || self.enqueue(runtime, workspace))?;
            workspace.graph = Some(graph);
        }

        match &workspace.graph {
            Some(graph) => graph.launch(),
            None => Ok(()),
        }
    }

    /// Queues every layer of one forward pass on the runtime's stream
    fn enqueue(
        &self,
        runtime: &CudaRuntime,
        workspace: &mut SegmentationWorkspace,
    ) -> Result<(), CudaError> {
        let shape = workspace.shape;
        let batch = shape.batch;
        let SegmentationWorkspace {
            sinc,
            conv1,
            conv2,
            conv_workspace,
            lstm,
            dense,
            tensors: t,
            ..
        } = workspace;
        let mut conv_workspace = conv_workspace.as_view_mut();
        let norm = |channels: usize, in_len: usize, pool: usize| PoolNorm {
            batch,
            channels,
            in_len,
            pool,
            abs_input: false,
            slope: LEAKY_SLOPE,
            epsilon: NORM_EPSILON,
            layout: RowLayout::channels_first(channels, in_len / pool),
        };
        let k = &self.kernels;

        // waveform instance normalization: no pooling and no activation
        let wave = PoolNorm {
            slope: 1.0,
            ..norm(1, shape.samples, 1)
        };
        let [gamma, beta] = &self.wav_norm;
        let zero_bias = &self.zero_bias;
        k.pool_norm(
            runtime,
            wave,
            t.input.data(),
            zero_bias,
            gamma,
            beta,
            t.wave_norm.data_mut(),
        )?;

        runtime.record_boundary(dispatch::SINC, batch, self.options.math, || {
            self.sinc_forward(
                runtime,
                shape,
                sinc,
                &mut conv_workspace,
                dispatch::SincIo {
                    input: t.wave_norm.data(),
                    raw: t.sinc.data_mut(),
                    pooled: t.sinc_pooled.as_mut().map(DeviceTensor::data_mut),
                    stage0: t.stage0.data_mut(),
                },
            )?;
            Ok(())
        })?;

        runtime.record_boundary(CONV1, batch, self.options.math, || {
            // the convolution bias is added inside the pooling kernel
            let [weight, bias] = &self.convs[0];
            conv1.forward(
                runtime,
                &mut conv_workspace,
                &t.stage0.data().as_view(),
                &weight.as_view(),
                &mut t.conv1.data_mut().as_view_mut(),
            )?;
            let stage1 = norm(FEATURES, shape.conv1, POOL);
            let [gamma, beta] = &self.norms[1];
            k.pool_norm(
                runtime,
                stage1,
                t.conv1.data(),
                bias,
                gamma,
                beta,
                t.stage1.data_mut(),
            )?;
            Ok(())
        })?;

        runtime.record_boundary(CONV2, batch, self.options.math, || {
            let [weight, bias] = &self.convs[1];
            conv2.forward(
                runtime,
                &mut conv_workspace,
                &t.stage1.data().as_view(),
                &weight.as_view(),
                &mut t.conv2.data_mut().as_view_mut(),
            )?;
            // the last stage writes the batch-major `[batch, frames, 60]` LSTM input
            let stage2 = PoolNorm {
                layout: RowLayout::time_major_rows(FEATURES, shape.frames),
                ..norm(FEATURES, shape.conv2, POOL)
            };
            let [gamma, beta] = &self.norms[2];
            k.pool_norm(
                runtime,
                stage2,
                t.conv2.data(),
                bias,
                gamma,
                beta,
                t.lstm_input.data_mut(),
            )?;
            Ok(())
        })?;

        runtime.record_boundary(dispatch::LSTM, batch, self.options.math, || {
            self.lstm_forward(runtime, lstm, t.lstm_input.data(), t.lstm_output.data_mut())?;
            Ok(())
        })?;

        let rows = batch * shape.frames;
        let gemm = |index: usize| Sgemm {
            math: self.options.math,
            ..Sgemm::new(rows, LINEAR[index][1], LINEAR[index][0])
        };
        runtime.record_boundary(LINEAR_BOUNDARIES[0], batch, self.options.math, || {
            dense[0].enqueue_slice(
                runtime,
                t.lstm_output.data(),
                t.linear0.data_mut(),
                |output| {
                    let [weight, bias] = &self.linear[0];
                    runtime.sgemm(gemm(0), t.lstm_output.data(), weight, output)?;
                    k.bias_leaky(runtime, bias, LEAKY_SLOPE, output)?;

                    Ok(())
                },
            )?;
            Ok(())
        })?;

        runtime.record_boundary(LINEAR_BOUNDARIES[1], batch, self.options.math, || {
            dense[1].enqueue_slice(runtime, t.linear0.data(), t.linear1.data_mut(), |output| {
                let [weight, bias] = &self.linear[1];
                runtime.sgemm(gemm(1), t.linear0.data(), weight, output)?;
                k.bias_leaky(runtime, bias, LEAKY_SLOPE, output)?;

                Ok(())
            })?;
            Ok(())
        })?;

        runtime.record_boundary(LINEAR_BOUNDARIES[2], batch, self.options.math, || {
            dense[2].enqueue_slice(runtime, t.linear1.data(), t.output.data_mut(), |output| {
                let [weight, bias] = &self.linear[2];
                runtime.sgemm(gemm(2), t.linear1.data(), weight, output)?;
                k.bias_log_softmax(runtime, bias, output)?;
                Ok(())
            })?;
            Ok(())
        })?;

        Ok(())
    }
}

impl SegmentationWorkspace {
    fn matches(&self, batch: usize, samples: usize) -> bool {
        self.shape.batch == batch && self.shape.samples == samples
    }

    /// Copies `[batch, samples]` waveforms into the input buffer
    pub fn upload_input(&mut self, runtime: &CudaRuntime, input: &[f32]) -> Result<(), CudaError> {
        self.tensors.input.copy_from_host(runtime.stream(), input)
    }

    /// Copies the `[batch, frames, 7]` log-probabilities back; waits for the stream
    pub fn download_output(&self, runtime: &CudaRuntime) -> Result<Vec<f32>, CudaError> {
        self.tensors.output.download(runtime.stream())
    }
}

#[cfg(all(test, feature = "_cuda-libraries"))]
mod test_support;

/// The pooled Sinc tensor, for a candidate whose declared output is pooled
fn pooled_tensor(
    runtime: &CudaRuntime,
    shape: SegmentationShape,
) -> Result<Option<DeviceTensor>, CudaError> {
    if SincOxide::OUTPUT != SincOutput::Pooled {
        return Ok(None);
    }

    let dims = [shape.batch, SINC_CHANNELS, shape.pool0];
    Ok(Some(DeviceTensor::zeros(runtime.stream(), &dims)?))
}