turbospark-runtime 0.3.0

The raw-completion prefill+decode loop: wires a LogitProducer, the tokenizer, and the selection sampler into one token generation loop.
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
//! MiniMax Music 3 runner. Uses the portable pipeline with packed resident
//! Metal weights, typed attention, and device vocoder convolutions.
use audio::music::minimax_music3::backend::{
    AttentionShape, ComputeBackend, ConvShape, DeviceBuffer, DeviceWeight, RopeShape, Sequence,
    Slot, WeightData, WeightEncoding,
};
use audio::music::minimax_music3::precision::{bf16_silu_table, DType};
use audio::music::minimax_music3::{
    Control, GenerateRequest, Generation, Model, ModelConfig, Music3Precision, Progress,
    SamplingTrace, StageTimings, TextGenerateRequest,
};
use audio::{Result, SpeechError};
use gpu::{
    Music3Buffer, Music3DType, Music3Device, Music3Encoding, Music3Sequence, Music3Slot,
    Music3Table, Music3Weight,
};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::time::{Duration, Instant};

type Cancellation = Rc<RefCell<Option<Arc<AtomicBool>>>>;
fn checkpoint(cancel: &Cancellation) -> Result<()> {
    if cancel
        .borrow()
        .as_ref()
        .is_some_and(|flag| flag.load(Ordering::Acquire))
    {
        Err(SpeechError::Input {
            why: "audio job cancelled".into(),
        })
    } else {
        Ok(())
    }
}

fn device_error(error: gpu::GpuError) -> SpeechError {
    SpeechError::Unsupported {
        why: format!("Music 3 Metal: {error}"),
    }
}
/// Intermediate values retain their logical dtype even though storage is f32.
pub struct Music3Stage<'a> {
    pub name: &'a str,
    pub values: &'a [f32],
    pub dtype: DType,
    pub shape: &'a [usize],
}
type Observer = Rc<RefCell<Option<Box<dyn FnMut(Music3Stage<'_>)>>>>;
/// Wall time and call count of one device operation at one shape, as the
/// pipeline sees it (buffer setup, dispatch, wait, and readback included).
#[derive(Debug, Clone, PartialEq)]
pub struct DispatchStat {
    pub op: &'static str,
    /// Operation geometry: linear is `[rows, input, output]`, attention is
    /// `[queries, keys, heads]`, norms are `[rows, cols]`, and so on.
    pub shape: Vec<usize>,
    pub calls: u64,
    pub total: Duration,
}

type Profile = Rc<RefCell<HashMap<(&'static str, Vec<usize>), (u64, Duration)>>>;

fn record(profile: &Profile, op: &'static str, shape: &[usize], started: Instant) {
    let mut table = profile.borrow_mut();
    let entry = table.entry((op, shape.to_vec())).or_default();
    entry.0 += 1;
    entry.1 += started.elapsed();
}

struct Backend {
    device: Music3Device,
    observer: Observer,
    cancellation: Cancellation,
    profile: Profile,
    /// BF16 SiLU of every 16-bit pattern, uploaded on first use.
    silu: RefCell<Option<Rc<Music3Table>>>,
}

impl Backend {
    fn silu_table(&self) -> Result<Rc<Music3Table>> {
        if let Some(table) = self.silu.borrow().as_ref() {
            return Ok(table.clone());
        }
        let table = Rc::new(
            self.device
                .table(&bf16_silu_table())
                .map_err(device_error)?,
        );
        *self.silu.borrow_mut() = Some(table.clone());
        Ok(table)
    }
}

/// A persistent Metal buffer (the AR KV cache) behind [`DeviceBuffer`].
struct KvBuffer(Music3Buffer);
impl DeviceBuffer for KvBuffer {
    fn write(&self, first: usize, data: &[f32]) -> Result<()> {
        self.0.write(first, data).map_err(device_error)
    }
    fn read(&self, first: usize, len: usize) -> Result<Vec<f32>> {
        self.0.read(first, len).map_err(device_error)
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// A Metal [`Sequence`]: one command buffer per step. Cancellation is
/// checked at every recorded operation and before commit; a cancelled
/// sequence is dropped uncommitted. One `dispatch_profile` row per finish.
struct MetalSequence<'a> {
    backend: &'a Backend,
    inner: Music3Sequence,
    slots: Vec<Music3Slot>,
    op: &'static str,
    shape: Vec<usize>,
    started: Instant,
}

impl MetalSequence<'_> {
    fn slot(&self, slot: Slot) -> Result<Music3Slot> {
        self.slots
            .get(slot.0)
            .copied()
            .ok_or_else(|| SpeechError::Input {
                why: "Music 3 sequence slot does not exist".into(),
            })
    }
    fn push(&mut self, result: std::result::Result<Music3Slot, gpu::GpuError>) -> Result<Slot> {
        self.slots.push(result.map_err(device_error)?);
        Ok(Slot(self.slots.len() - 1))
    }
}

fn require_bf16(op: &str, dtypes: &[DType]) -> Result<()> {
    if dtypes.iter().all(|d| *d == DType::Bf16) {
        Ok(())
    } else {
        Err(SpeechError::Unsupported {
            why: format!("Music 3 Metal sequence {op} reproduces BF16 rounding only"),
        })
    }
}

impl Sequence for MetalSequence<'_> {
    fn upload(&mut self, data: &[f32]) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let result = self.inner.upload(data);
        self.push(result)
    }
    fn bind(&mut self, buffer: &dyn DeviceBuffer) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let buffer =
            buffer
                .as_any()
                .downcast_ref::<KvBuffer>()
                .ok_or_else(|| SpeechError::Unsupported {
                    why: "Music 3 Metal sequence needs a Metal-resident buffer".into(),
                })?;
        let result = self.inner.bind(&buffer.0);
        self.push(result)
    }
    fn view(&mut self, slot: Slot, first: usize, len: usize) -> Result<Slot> {
        let slot = self.slot(slot)?;
        let result = self.inner.view(slot, first, len);
        self.push(result)
    }
    fn copy(&mut self, src: Slot, dst: Slot) -> Result<()> {
        checkpoint(&self.backend.cancellation)?;
        let (src, dst) = (self.slot(src)?, self.slot(dst)?);
        self.inner.copy(src, dst).map_err(device_error)
    }
    fn rope(&mut self, x: Slot, s: RopeShape, d: DType) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let x = self.slot(x)?;
        let result = self.inner.rope(
            x,
            s.batch,
            s.seq,
            s.heads,
            s.dim,
            s.offset,
            s.theta,
            dtype(d),
        );
        self.push(result)
    }
    fn linear(
        &mut self,
        x: Slot,
        weight: &dyn DeviceWeight,
        bias: Option<&[f32]>,
        rows: usize,
        input_dim: usize,
        output_dim: usize,
        d: DType,
    ) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let weight = weight
            .as_any()
            .and_then(|w| w.downcast_ref::<Weight>())
            .ok_or_else(|| SpeechError::Unsupported {
                why: "Music 3 Metal sequence needs a Metal-resident weight".into(),
            })?;
        let x = self.slot(x)?;
        let result = self
            .inner
            .linear(x, &weight.0, bias, rows, input_dim, output_dim, dtype(d));
        self.push(result)
    }
    fn layer_norm(
        &mut self,
        x: Slot,
        w: &[f32],
        bias: Option<&[f32]>,
        rows: usize,
        cols: usize,
        eps: f32,
        d: DType,
    ) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let x = self.slot(x)?;
        let result = self.inner.layer_norm(x, w, bias, rows, cols, eps, dtype(d));
        self.push(result)
    }
    fn partial_rope(
        &mut self,
        x: Slot,
        cos: Slot,
        sin: Slot,
        len: usize,
        heads: usize,
        head_dim: usize,
        rotary: usize,
        d: DType,
    ) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        require_bf16("partial rotary", &[d])?;
        let (x, cos, sin) = (self.slot(x)?, self.slot(cos)?, self.slot(sin)?);
        let result = self
            .inner
            .partial_rope_bf16(x, cos, sin, len, heads, head_dim, rotary);
        self.push(result)
    }
    fn gated_silu(&mut self, x: Slot, rows: usize, half: usize, d: DType) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        require_bf16("gated SiLU", &[d])?;
        let table = self.backend.silu_table()?;
        let x = self.slot(x)?;
        let result = self.inner.gated_silu_bf16(x, rows, half, &table);
        self.push(result)
    }
    fn rms_norm(
        &mut self,
        x: Slot,
        w: &[f32],
        rows: usize,
        cols: usize,
        eps: f32,
        d: DType,
    ) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let x = self.slot(x)?;
        let result = self.inner.rms_norm(x, w, rows, cols, eps, dtype(d));
        self.push(result)
    }
    fn attention(
        &mut self,
        q: Slot,
        k: Slot,
        v: Slot,
        s: AttentionShape,
        d: DType,
    ) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        let (q, k, v) = (self.slot(q)?, self.slot(k)?, self.slot(v)?);
        let result = self.inner.attention(
            q,
            k,
            v,
            s.batch,
            s.queries,
            s.keys,
            s.heads,
            s.kv_heads,
            s.dim,
            s.kv_time_major,
            s.causal,
            s.offset,
            dtype(d),
        );
        self.push(result)
    }
    fn add_round(&mut self, a: Slot, b: Slot, d: DType) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        require_bf16("residual add", &[d])?;
        let (a, b) = (self.slot(a)?, self.slot(b)?);
        let result = self.inner.add_round_bf16(a, b);
        self.push(result)
    }
    fn swiglu(&mut self, g: Slot, u: Slot, gate: DType, fused: DType) -> Result<Slot> {
        checkpoint(&self.backend.cancellation)?;
        require_bf16("SwiGLU", &[gate, fused])?;
        let table = self.backend.silu_table()?;
        let (g, u) = (self.slot(g)?, self.slot(u)?);
        let result = self.inner.swiglu_bf16(g, u, &table);
        self.push(result)
    }
    fn finish(self: Box<Self>, read: &[Slot]) -> Result<Option<Vec<Vec<f32>>>> {
        checkpoint(&self.backend.cancellation)?;
        let read = read
            .iter()
            .map(|slot| self.slot(*slot))
            .collect::<Result<Vec<_>>>()?;
        let Self {
            backend,
            inner,
            op,
            shape,
            started,
            ..
        } = *self;
        let result = inner.finish(&read).map_err(device_error);
        record(&backend.profile, op, &shape, started);
        result
    }
}
fn dtype(d: DType) -> Music3DType {
    match d {
        DType::F32 => Music3DType::F32,
        DType::F16 => Music3DType::F16,
        DType::Bf16 => Music3DType::Bf16,
    }
}
struct Weight(Music3Weight, Cancellation, Profile);
impl ComputeBackend for Backend {
    fn load_weight(&self, data: WeightData<'_>) -> Result<Rc<dyn DeviceWeight>> {
        let encoding = match data.encoding {
            WeightEncoding::F32 => Music3Encoding::F32,
            WeightEncoding::F16 => Music3Encoding::F16,
            WeightEncoding::Bf16 => Music3Encoding::Bf16,
            WeightEncoding::Affine { bits, group_size } => {
                Music3Encoding::Affine { bits, group_size }
            }
            WeightEncoding::MxFp4 => Music3Encoding::MxFp4,
            WeightEncoding::MxFp8 => Music3Encoding::MxFp8,
            WeightEncoding::NvFp4 => Music3Encoding::NvFp4,
        };
        let weight = self
            .device
            .load_weight(
                data.shape,
                data.bytes,
                encoding,
                data.scales,
                data.offsets,
                data.block_scales,
            )
            .map_err(|error| SpeechError::Tensor {
                name: data.name.into(),
                why: error.to_string(),
            })?;
        Ok(Rc::new(Weight(
            weight,
            self.cancellation.clone(),
            self.profile.clone(),
        )))
    }
    fn attention(
        &self,
        q: &[f32],
        k: &[f32],
        v: &[f32],
        s: AttentionShape,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        let started = Instant::now();
        let out = self
            .device
            .attention_typed(
                q,
                k,
                v,
                s.batch,
                s.queries,
                s.keys,
                s.heads,
                s.kv_heads,
                s.dim,
                s.kv_time_major,
                s.causal,
                s.offset,
                dtype(d),
            )
            .map_err(device_error);
        record(
            &self.profile,
            "attention",
            &[s.batch, s.queries, s.keys, s.heads, s.dim],
            started,
        );
        out
    }
    fn rms_norm(
        &self,
        x: &[f32],
        w: &[f32],
        rows: usize,
        cols: usize,
        eps: f32,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        let started = Instant::now();
        let out = self
            .device
            .rms_norm(x, w, rows, cols, eps, dtype(d))
            .map_err(device_error);
        record(&self.profile, "rms_norm", &[rows, cols], started);
        out
    }
    fn layer_norm(
        &self,
        x: &[f32],
        w: &[f32],
        bias: Option<&[f32]>,
        rows: usize,
        cols: usize,
        eps: f32,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        let started = Instant::now();
        let out = self
            .device
            .layer_norm(x, w, bias, rows, cols, eps, dtype(d))
            .map_err(device_error);
        record(&self.profile, "layer_norm", &[rows, cols], started);
        out
    }
    fn rope(&self, x: &[f32], s: RopeShape, d: DType) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        let started = Instant::now();
        let out = self
            .device
            .rope(
                x,
                s.batch,
                s.seq,
                s.heads,
                s.dim,
                s.offset,
                s.theta,
                dtype(d),
            )
            .map_err(device_error);
        record(
            &self.profile,
            "rope",
            &[s.batch, s.seq, s.heads, s.dim],
            started,
        );
        out
    }
    fn normal_from_uniform(&self, x: &[f32], d: DType) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        self.device
            .normal_from_uniform(x, dtype(d))
            .map_err(device_error)
    }
    fn rotary_tables(&self, seq: usize, dim: usize, theta: f32) -> Result<(Vec<f32>, Vec<f32>)> {
        checkpoint(&self.cancellation)?;
        self.device
            .rotary_tables(seq, dim, theta)
            .map_err(device_error)
    }
    fn snake(
        &self,
        x: &[f32],
        alpha: &[f32],
        channels: usize,
        frames: usize,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.cancellation)?;
        let started = Instant::now();
        let out = self
            .device
            .snake(x, alpha, channels, frames, dtype(d))
            .map_err(device_error);
        record(&self.profile, "snake", &[channels, frames], started);
        out
    }
    fn trace(&self, stage: &str, data: &[f32], d: DType, shape: &[usize]) {
        if let Some(f) = self.observer.borrow_mut().as_mut() {
            f(Music3Stage {
                name: stage,
                values: data,
                dtype: d,
                shape,
            });
        }
    }
    fn sequence(&self, op: &'static str, shape: &[usize]) -> Option<Box<dyn Sequence + '_>> {
        // Trace observers need host values between operations, so observed
        // runs keep the per-operation path.
        if self.observer.borrow().is_some() {
            return None;
        }
        Some(Box::new(MetalSequence {
            backend: self,
            inner: self.device.sequence(op),
            slots: Vec::new(),
            op,
            shape: shape.to_vec(),
            started: Instant::now(),
        }))
    }
    fn buffer(&self, len: usize) -> Option<Result<Rc<dyn DeviceBuffer>>> {
        Some(
            self.device
                .buffer(len)
                .map(|buffer| Rc::new(KvBuffer(buffer)) as Rc<dyn DeviceBuffer>)
                .map_err(device_error),
        )
    }
}
impl DeviceWeight for Weight {
    fn linear(
        &self,
        input: &[f32],
        bias: Option<&[f32]>,
        rows: usize,
        inn: usize,
        out: usize,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.1)?;
        let started = Instant::now();
        let result = self
            .0
            .linear_typed(input, bias, rows, inn, out, dtype(d))
            .map_err(device_error);
        record(&self.2, "linear", &[rows, inn, out], started);
        result
    }
    fn embedding(&self, ids: &[i32], width: usize) -> Result<Vec<f32>> {
        checkpoint(&self.1)?;
        let started = Instant::now();
        let result = self.0.embedding(ids, width).map_err(device_error);
        record(&self.2, "embedding", &[ids.len(), width], started);
        result
    }
    fn convolution(
        &self,
        input: &[f32],
        bias: Option<&[f32]>,
        s: ConvShape,
        d: DType,
    ) -> Result<Vec<f32>> {
        checkpoint(&self.1)?;
        let started = Instant::now();
        let result = self
            .0
            .convolution_typed(
                input,
                bias,
                s.input_channels,
                s.output_channels,
                s.kernel,
                s.stride,
                s.padding,
                s.dilation,
                s.transpose,
                dtype(d),
            )
            .map_err(device_error);
        record(
            &self.2,
            if s.transpose {
                "conv_transpose"
            } else {
                "conv"
            },
            &[s.input_channels, s.output_channels, s.kernel],
            started,
        );
        result
    }
    fn as_any(&self) -> Option<&dyn std::any::Any> {
        Some(self)
    }
}

/// A converted MiniMax Music 3 checkpoint, resident on the Metal device.
/// Each request creates fresh AR caches and flow overlap state.
pub struct Music3Runner {
    model: Model,
    device: Music3Device,
    observer: Observer,
    cancellation: Cancellation,
    profile: Profile,
}
impl Music3Runner {
    pub fn open(path: &Path) -> Result<Self> {
        Self::open_with_precision(path, Music3Precision::Checkpoint)
    }
    pub fn open_with_precision(path: &Path, precision: Music3Precision) -> Result<Self> {
        let device = Music3Device::new().map_err(device_error)?;
        let observer = Rc::new(RefCell::new(None));
        let cancellation = Rc::new(RefCell::new(None));
        let profile = Profile::default();
        let backend = Rc::new(Backend {
            device: device.clone(),
            observer: observer.clone(),
            cancellation: cancellation.clone(),
            profile: profile.clone(),
            silu: RefCell::new(None),
        });
        let model = Model::load_converted_with_backend_and_precision(path, backend, precision)?;
        Ok(Self {
            model,
            device,
            observer,
            cancellation,
            profile,
        })
    }
    pub fn set_trace_observer(&self, observer: impl FnMut(Music3Stage<'_>) + 'static) {
        *self.observer.borrow_mut() = Some(Box::new(observer));
    }
    pub fn clear_trace_observer(&self) {
        *self.observer.borrow_mut() = None;
    }
    pub fn config(&self) -> &ModelConfig {
        self.model.config()
    }
    /// Opt in to reusing the flow unconditional CFG branch between steps.
    /// The default (`FlowGuidance::EXACT`) is the reference trajectory.
    pub fn set_flow_guidance(
        &self,
        guidance: audio::music::minimax_music3::FlowGuidance,
    ) -> Result<()> {
        self.model.set_flow_guidance(guidance)
    }
    /// Per-operation call counts and wall time since the last reset,
    /// slowest total first. Weight upload during `open` is not included.
    pub fn dispatch_profile(&self) -> Vec<DispatchStat> {
        let mut stats: Vec<DispatchStat> = self
            .profile
            .borrow()
            .iter()
            .map(|((op, shape), (calls, total))| DispatchStat {
                op,
                shape: shape.clone(),
                calls: *calls,
                total: *total,
            })
            .collect();
        stats.sort_by_key(|stat| std::cmp::Reverse(stat.total));
        stats
    }
    pub fn reset_dispatch_profile(&self) {
        self.profile.borrow_mut().clear();
    }
    pub fn resident_weight_bytes(&self) -> usize {
        self.device.resident_weight_bytes()
    }
    /// Stops at a native compute boundary; the caller retains its permit until return.
    pub fn generate_text_cancellable(
        &self,
        request: &TextGenerateRequest,
        cancel: Arc<AtomicBool>,
    ) -> Result<Generation> {
        self.generate_text_controlled(request, cancel, |_| Control::Continue)
    }

    /// Atomic cancellation remains independent of callbacks and native model state.
    pub fn generate_text_controlled(
        &self,
        request: &TextGenerateRequest,
        cancel: Arc<AtomicBool>,
        mut on_progress: impl FnMut(Progress) -> Control,
    ) -> Result<Generation> {
        *self.cancellation.borrow_mut() = Some(cancel);
        struct Reset(Cancellation);
        impl Drop for Reset {
            fn drop(&mut self) {
                self.0.borrow_mut().take();
            }
        }
        let _reset = Reset(self.cancellation.clone());
        checkpoint(&self.cancellation)?;
        let result = self.model.generate_text_with_progress(request, |event| {
            if checkpoint(&self.cancellation).is_err() {
                Control::Cancel
            } else {
                on_progress(event)
            }
        })?;
        checkpoint(&self.cancellation)?;
        result
            .map(|(generation, _)| generation)
            .ok_or_else(|| SpeechError::Input {
                why: "audio job cancelled".into(),
            })
    }
    pub fn generate_text(&self, request: &TextGenerateRequest) -> Result<Generation> {
        self.model.generate_text(request)
    }
    /// [`Self::generate_text`] plus a per-stage wall-time breakdown.
    pub fn generate_text_timed(
        &self,
        request: &TextGenerateRequest,
    ) -> Result<(Generation, StageTimings)> {
        self.model.generate_text_timed(request)
    }
    /// [`Self::generate_text_timed`] with milestone callbacks. Return
    /// [`Control::Cancel`] to stop at the next frame or chunk boundary; the
    /// result is then `Ok(None)` and the runner is ready for the next request.
    pub fn generate_text_with_progress(
        &self,
        request: &TextGenerateRequest,
        on_progress: impl FnMut(Progress) -> Control,
    ) -> Result<Option<(Generation, StageTimings)>> {
        self.model.generate_text_with_progress(request, on_progress)
    }
    pub fn generate(&self, request: &GenerateRequest) -> Result<Generation> {
        self.model.generate(request)
    }
    /// Decode precomputed AR frames, useful for independent stage parity.
    pub fn run_flow(
        &self,
        hiddens: &[f32],
        frames: usize,
        steps: usize,
        seed: u64,
    ) -> Result<Vec<f32>> {
        self.model.run_flow(hiddens, frames, steps, seed)
    }
    /// Replay supplied channel-major noise chunks for independent stage probes.
    pub fn run_flow_with_noise(
        &self,
        hiddens: &[f32],
        frames: usize,
        steps: usize,
        noise_chunks: &[Vec<f32>],
    ) -> Result<Vec<f32>> {
        self.model
            .run_flow_with_noise(hiddens, frames, steps, noise_chunks)
    }
    pub fn generate_frame_hiddens(
        &self,
        ids: &[i32],
        frames: usize,
        seed: u64,
    ) -> Result<(Vec<f32>, Vec<Vec<i32>>)> {
        self.model.generate_frame_hiddens(ids, frames, seed)
    }
    pub fn generate_frame_hiddens_traced(
        &self,
        ids: &[i32],
        frames: usize,
        seed: u64,
        observe: impl FnMut(SamplingTrace<'_>),
    ) -> Result<(Vec<f32>, Vec<Vec<i32>>)> {
        self.model
            .generate_frame_hiddens_traced(ids, frames, seed, observe)
    }
}