any-tts 0.1.1

A Rust TTS library with Candle backends and runtime adapters for modern open TTS models
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
//! Kokoro prosody predictor.
//!
//! Predicts duration, F0 (fundamental frequency), and noise parameters
//! from encoded text features and style embeddings.
//!
//! Architecture from StyleTTS2:
//! - DurationEncoder: LSTM + AdaLayerNorm
//! - ProsodyPredictor: DurationEncoder + duration/F0/noise prediction heads

use candle_core::{Device, Result, Tensor};
use candle_nn::VarBuilder;

use crate::layers::conv::{AdaIn1d, AdaLayerNorm, Conv1d, ConvTranspose1d, LinearNorm};
use crate::layers::lstm::Lstm;

fn scalar_like(tensor: &Tensor, value: f32) -> Result<Tensor> {
    Tensor::new(value, tensor.device())?.to_dtype(tensor.dtype())
}

fn scale_tensor(tensor: &Tensor, value: f32) -> Result<Tensor> {
    tensor.broadcast_mul(&scalar_like(tensor, value)?)
}

// ---------------------------------------------------------------------------
// Upsample helper (for Metal compatibility)
// ---------------------------------------------------------------------------

/// Upsample 1D using repeat (works on Metal unlike upsample_nearest1d).
/// Input: [batch, channels, length] → Output: [batch, channels, target_len]
fn upsample_1d_repeat(x: &Tensor, target_len: usize) -> Result<Tensor> {
    let (batch, channels, length) = x.dims3()?;
    if target_len == length {
        return Ok(x.clone());
    }
    // Compute scale factor (must be integer multiple for repeat)
    let scale = target_len / length;
    if scale * length == target_len && scale > 1 {
        // Exact integer upsample: [b, c, l] → [b, c, l, 1] → repeat → [b, c, l*scale]
        let x = x.unsqueeze(3)?;
        let x = x.repeat(&[1, 1, 1, scale])?;
        x.reshape((batch, channels, target_len))
    } else {
        // Fallback: move to CPU, upsample, move back
        let device = x.device().clone();
        let x_cpu = x.to_device(&Device::Cpu)?;
        let upsampled = x_cpu.upsample_nearest1d(target_len)?;
        upsampled.to_device(&device)
    }
}

/// LeakyReLU activation: max(x, x * negative_slope)
fn leaky_relu(x: &Tensor, negative_slope: f32) -> Result<Tensor> {
    let scaled = scale_tensor(x, negative_slope)?;
    x.maximum(&scaled)
}

// ---------------------------------------------------------------------------
// AdainResBlk1d — Residual block with AdaIN (used in F0/N prediction)
// ---------------------------------------------------------------------------

/// Residual block with Adaptive Instance Normalization.
pub struct AdainResBlk1d {
    conv1: Conv1d,
    conv2: Conv1d,
    norm1: AdaIn1d,
    norm2: AdaIn1d,
    /// Channel-matching 1×1 shortcut (when dim_in != dim_out).
    conv1x1: Option<Conv1d>,
    /// Depthwise transposed convolution used on the residual path when upsampling.
    pool: Option<ConvTranspose1d>,
    upsample: bool,
}

impl AdainResBlk1d {
    /// Load from VarBuilder.
    pub fn load(
        dim_in: usize,
        dim_out: usize,
        style_dim: usize,
        upsample: bool,
        vb: VarBuilder,
    ) -> Result<Self> {
        let conv1 = Conv1d::load(dim_in, dim_out, 3, 1, 1, 1, 1, true, vb.pp("conv1"))?;
        let conv2 = Conv1d::load(dim_out, dim_out, 3, 1, 1, 1, 1, true, vb.pp("conv2"))?;
        let norm1 = AdaIn1d::load(style_dim, dim_in, vb.pp("norm1"))?;
        let norm2 = AdaIn1d::load(style_dim, dim_out, vb.pp("norm2"))?;

        let conv1x1 = if dim_in != dim_out {
            Some(Conv1d::load(
                dim_in,
                dim_out,
                1,
                1,
                0,
                1,
                1,
                false,
                vb.pp("conv1x1"),
            )?)
        } else {
            None
        };

        // Upstream uses a depthwise ConvTranspose1d on the residual path.
        let pool = if upsample {
            Some(ConvTranspose1d::load(
                dim_in,
                dim_in,
                3,
                2,
                1,
                1,
                dim_in,
                true,
                vb.pp("pool"),
            )?)
        } else {
            None
        };

        Ok(Self {
            conv1,
            conv2,
            norm1,
            norm2,
            conv1x1,
            pool,
            upsample,
        })
    }

    fn shortcut(&self, x: &Tensor) -> Result<Tensor> {
        let mut out = x.clone();
        if self.upsample {
            let (_b, _c, length) = out.dims3()?;
            out = upsample_1d_repeat(&out, length * 2)?;
        }
        if let Some(ref sc) = self.conv1x1 {
            out = sc.forward(&out)?;
        }
        Ok(out)
    }

    fn residual(&self, x: &Tensor, s: &Tensor) -> Result<Tensor> {
        let mut out = self.norm1.forward(x, s)?;
        out = leaky_relu(&out, 0.2)?;
        if let Some(ref pool) = self.pool {
            out = pool.forward(&out)?;
        }
        out = self.conv1.forward(&out)?;
        out = self.norm2.forward(&out, s)?;
        out = leaky_relu(&out, 0.2)?;
        self.conv2.forward(&out)
    }

    /// Forward pass.
    /// `x`: [batch, channels, length]
    /// `s`: [batch, style_dim]
    pub fn forward(&self, x: &Tensor, s: &Tensor) -> Result<Tensor> {
        let residual = self.shortcut(x)?;
        let out = self.residual(x, s)?;
        let combined = out.add(&residual)?;
        scale_tensor(&combined, std::f32::consts::FRAC_1_SQRT_2)
    }

    /// Whether this block upsamples.
    pub fn upsample_type(&self) -> &str {
        if self.upsample {
            "nearest"
        } else {
            "none"
        }
    }
}

// ---------------------------------------------------------------------------
// DurationEncoder — LSTM + AdaLayerNorm for duration encoding
// ---------------------------------------------------------------------------

/// Duration encoder: LSTM layers interleaved with AdaLayerNorm.
pub struct DurationEncoder {
    lstms: Vec<Lstm>,
    ada_norms: Vec<AdaLayerNorm>,
    sty_dim: usize,
}

impl DurationEncoder {
    /// Load from VarBuilder.
    pub fn load(
        sty_dim: usize,
        d_model: usize,
        nlayers: usize,
        vb: VarBuilder,
        _device: &Device,
    ) -> Result<Self> {
        let mut lstms = Vec::with_capacity(nlayers);
        let mut ada_norms = Vec::with_capacity(nlayers);

        for i in 0..nlayers {
            let lstm = Lstm::load(
                1,                 // num_layers
                d_model + sty_dim, // input_size
                d_model / 2,       // hidden_size
                true,              // bidirectional → output = d_model
                vb.pp("lstms").pp((i * 2).to_string()),
            )?;
            lstms.push(lstm);

            let norm =
                AdaLayerNorm::load(sty_dim, d_model, vb.pp("lstms").pp((i * 2 + 1).to_string()))?;
            ada_norms.push(norm);
        }

        Ok(Self {
            lstms,
            ada_norms,
            sty_dim,
        })
    }

    /// Forward pass.
    ///
    /// `x`: [batch, channels, seq_len] — text features (transposed)
    /// `style`: [batch, style_dim] — style embedding
    /// `text_lengths`: [batch] — sequence lengths
    /// `mask`: [batch, seq_len] — True for padded positions
    ///
    /// Returns: [batch, seq_len, d_model + sty_dim]
    pub fn forward(
        &self,
        x: &Tensor,
        style: &Tensor,
        _text_lengths: &Tensor,
        mask: &Tensor,
    ) -> Result<Tensor> {
        let (batch, _channels, seq_len) = x.dims3()?;

        // x: [batch, channels, seq_len] → [seq_len, batch, channels]
        let mut x = x.permute((2, 0, 1))?;

        // style: [batch, sty_dim] → [seq_len, batch, sty_dim]
        let s = style
            .unsqueeze(0)?
            .broadcast_as((seq_len, batch, self.sty_dim))?;

        // Concatenate x and style along last dim
        x = Tensor::cat(&[&x, &s], 2)?;

        // Apply mask: [batch, seq_len] → [seq_len, batch, 1]
        let mask_f = mask.to_dtype(x.dtype())?.transpose(0, 1)?.unsqueeze(2)?;
        let inv_mask = mask_f.neg()?.add(&Tensor::ones_like(&mask_f)?)?;
        x = x.broadcast_mul(&inv_mask)?;

        // Transpose to [batch, seq_len, d_model + sty_dim]
        x = x.transpose(0, 1)?;

        // Run through LSTM + AdaLayerNorm pairs
        for (lstm, norm) in self.lstms.iter().zip(self.ada_norms.iter()) {
            // x: [batch, seq_len, d_model + sty_dim] → LSTM → [batch, seq_len, d_model]
            x = lstm.forward(&x)?;

            // Apply AdaLayerNorm: [batch, seq_len, d_model]
            x = norm.forward(&x, style)?;

            // Re-concatenate style
            let s_batch = style
                .unsqueeze(1)?
                .broadcast_as((batch, seq_len, self.sty_dim))?;
            x = Tensor::cat(&[&x, &s_batch], 2)?;

            // Apply mask
            let batch_mask = mask.unsqueeze(2)?.to_dtype(x.dtype())?;
            let inv = batch_mask.neg()?.add(&Tensor::ones_like(&batch_mask)?)?;
            x = x.broadcast_mul(&inv)?;
        }

        Ok(x)
    }
}

// ---------------------------------------------------------------------------
// ProsodyPredictor — Duration + F0 + Noise prediction
// ---------------------------------------------------------------------------

/// Full prosody predictor: predicts duration, F0, and noise.
pub struct ProsodyPredictor {
    /// Duration encoder (LSTM + AdaLayerNorm).
    pub text_encoder: DurationEncoder,
    /// LSTM for duration refinement.
    pub lstm: Lstm,
    /// Duration projection (to max_dur classes).
    pub duration_proj: LinearNorm,
    /// Shared LSTM for F0 and noise.
    pub shared: Lstm,
    /// F0 prediction blocks.
    pub f0_blocks: Vec<AdainResBlk1d>,
    /// F0 projection to single channel.
    pub f0_proj: Conv1d,
    /// Noise prediction blocks.
    pub n_blocks: Vec<AdainResBlk1d>,
    /// Noise projection to single channel.
    pub n_proj: Conv1d,
}

impl ProsodyPredictor {
    /// Load from VarBuilder.
    pub fn load(
        style_dim: usize,
        d_hid: usize,
        nlayers: usize,
        max_dur: usize,
        vb: VarBuilder,
        device: &Device,
    ) -> Result<Self> {
        let text_encoder =
            DurationEncoder::load(style_dim, d_hid, nlayers, vb.pp("text_encoder"), device)?;

        // LSTM for duration
        let lstm = Lstm::load(1, d_hid + style_dim, d_hid / 2, true, vb.pp("lstm"))?;

        let duration_proj =
            LinearNorm::load(d_hid, max_dur, vb.pp("duration_proj").pp("linear_layer"))?;

        // Shared LSTM for F0/N
        let shared = Lstm::load(1, d_hid + style_dim, d_hid / 2, true, vb.pp("shared"))?;

        // F0 prediction: 2 AdainResBlk1d blocks + Conv1d projection
        let f0_0 = AdainResBlk1d::load(d_hid, d_hid, style_dim, false, vb.pp("F0").pp("0"))?;
        let f0_1 = AdainResBlk1d::load(
            d_hid,
            d_hid / 2,
            style_dim,
            true, // upsample
            vb.pp("F0").pp("1"),
        )?;
        let f0_2 =
            AdainResBlk1d::load(d_hid / 2, d_hid / 2, style_dim, false, vb.pp("F0").pp("2"))?;
        let f0_proj = Conv1d::load(d_hid / 2, 1, 1, 1, 0, 1, 1, true, vb.pp("F0_proj"))?;

        // Noise prediction: same structure as F0
        let n_0 = AdainResBlk1d::load(d_hid, d_hid, style_dim, false, vb.pp("N").pp("0"))?;
        let n_1 = AdainResBlk1d::load(
            d_hid,
            d_hid / 2,
            style_dim,
            true, // upsample
            vb.pp("N").pp("1"),
        )?;
        let n_2 = AdainResBlk1d::load(d_hid / 2, d_hid / 2, style_dim, false, vb.pp("N").pp("2"))?;
        let n_proj = Conv1d::load(d_hid / 2, 1, 1, 1, 0, 1, 1, true, vb.pp("N_proj"))?;

        Ok(Self {
            text_encoder,
            lstm,
            duration_proj,
            shared,
            f0_blocks: vec![f0_0, f0_1, f0_2],
            f0_proj,
            n_blocks: vec![n_0, n_1, n_2],
            n_proj,
        })
    }

    /// Predict durations from encoded text features.
    ///
    /// `d`: [batch, seq_len, d_hid + style_dim] — output from DurationEncoder
    /// `s`: [batch, style_dim] — style embedding (unused here; already fused into `d`)
    ///
    /// Returns: [batch, seq_len] — sigmoid duration sums per token
    pub fn predict_duration(&self, d: &Tensor, _s: &Tensor) -> Result<Tensor> {
        // Upstream DurationEncoder already returns d_hid + style_dim features.
        let x = self.lstm.forward(d)?;

        // Duration projection: [batch, seq_len, d_hid] → [batch, seq_len, max_dur]
        let dur = self.duration_proj.forward(&x)?;

        // Sigmoid → sum over last axis → [batch, seq_len]
        let dur = candle_nn::ops::sigmoid(&dur)?;
        dur.sum(2) // Sum over max_dur dimension
    }

    /// Predict F0 and noise from aligned features.
    ///
    /// `x`: [batch, d_hid + style_dim, aligned_len]
    /// `s`: [batch, style_dim]
    ///
    /// Returns: (f0, noise) — both [batch, aligned_len*2] (upsampled)
    pub fn f0_n_predict(&self, x: &Tensor, s: &Tensor) -> Result<(Tensor, Tensor)> {
        // Upstream alignment features already include the concatenated style channels.
        let x_t = x.transpose(1, 2)?;
        let shared_out = self.shared.forward(&x_t)?;

        // F0 path
        let mut f0 = shared_out.transpose(1, 2)?; // [batch, d_hid, seq_len]
        for block in &self.f0_blocks {
            f0 = block.forward(&f0, s)?;
        }
        let f0 = self.f0_proj.forward(&f0)?.squeeze(1)?; // [batch, aligned_len*2]

        // Noise path
        let mut n = shared_out.transpose(1, 2)?;
        for block in &self.n_blocks {
            n = block.forward(&n, s)?;
        }
        let n = self.n_proj.forward(&n)?.squeeze(1)?; // [batch, aligned_len*2]

        Ok((f0, n))
    }
}