Run this model with HF Jobs

Run AlexeyNe/kitten-tts-2-mini-coreml on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/AlexeyNe/kitten-tts-2-mini-coreml. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/AlexeyNe/kitten-tts-2-mini-coreml on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
File size: 3,380 Bytes
e66a8be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Генератор бакетами: фраза лежит в начале бакета, `valid` кадров x_pre её
# настоящие. Нормализация (InstanceNorm) считается по ним одним, перед каждой
# свёрткой хвост обнуляется, обратный STFT нормируется по маске — результат
# равен генератору ровно по фразе. GEN_GAIN — множитель громкости (Kokoro в
# Aloudy звучит как ONNX-экспорт: torch.istft × 1,5).
import torch, torch.nn as nn, torch.nn.functional as F
import os
import network as ek
GAIN = float(os.environ.get('GEN_GAIN', '1'))

def masked_norm(x, m, inv_n):
    # inv_n is folded in before the sum: no sum or count ever exceeds fp16 range.
    mean = (x * (m * inv_n)).sum(-1, keepdim=True)
    var = ((x - mean) ** 2 * (m * inv_n)).sum(-1, keepdim=True)
    return (x - mean) / torch.sqrt(var + 1e-5)

def adain(mod, x, s, m, n):
    gamma, beta = mod.fc(s).view(1, -1, 1).chunk(2, 1)
    return (1 + gamma) * masked_norm(x, m, n) + beta

def snake(x, a):
    return x + (1 / a) * torch.sin(a * x) ** 2

def resblock(rb, x, s, m, n):
    for c1, c2, n1, n2, a1, a2 in zip(rb.convs1, rb.convs2, rb.adain1, rb.adain2, rb.alpha1, rb.alpha2):
        xt = c1(snake(adain(n1, x, s, m, n), a1) * m)
        xt = c2(snake(adain(n2, xt, s, m, n), a2) * m)
        x = xt + x
    return x

def istft_masked(spec, phase, m):
    z = torch.cat([spec * torch.cos(phase), spec * torch.sin(phase)], 1) * m
    y = F.conv_transpose1d(z, ek.ISTFT_W, stride=ek.HOP)
    env = F.conv_transpose1d(m, ek.ENV_W, stride=ek.HOP)
    y = y / env.clamp(min=1e-3)
    return y[:, :, ek.NFFT // 2: -(ek.NFFT // 2)]

class MaskedGenerator(nn.Module):
    def forward(self, x_pre, ref_s, har, valid):
        g = ek.net['decoder'].generator; s = ref_s[:, :128]
        N = x_pre.shape[-1]
        m0 = (torch.arange(N, dtype=torch.int32) < valid).float().view(1, 1, N)   # valid: int32, exact for any bucket
        m1 = m0.unsqueeze(-1).expand(1, 1, N, 10).reshape(1, 1, 10 * N)
        m2 = torch.cat([torch.ones(1, 1, 1), m0.unsqueeze(-1).expand(1, 1, N, 60).reshape(1, 1, 60 * N)], -1)
        n1 = 0.1 / valid.float()                                  # 1 / (10 · valid)
        n2 = (1.0 / 60) / (valid.float() + 1.0 / 60)              # 1 / (60 · valid + 1)
        har = har * m2
        x = x_pre * m0
        # stage 0: x_pre -> 10x
        x = F.leaky_relu(x, 0.1)
        src = resblock(g.noise_res[0], g.noise_convs[0](har), s, m1, n1)
        x = g.ups[0](x * m0) + src
        xs = None
        for j in range(g.num_kernels):
            y = resblock(g.resblocks[j], x, s, m1, n1); xs = y if xs is None else xs + y
        x = xs / g.num_kernels
        # stage 1: 10x -> 60x + 1
        x = F.leaky_relu(x, 0.1)
        src = resblock(g.noise_res[1], g.noise_convs[1](har), s, m2, n2)
        x = g.reflection_pad(g.ups[1](x * m1)) + src
        xs = None
        for j in range(g.num_kernels):
            y = resblock(g.resblocks[g.num_kernels + j], x, s, m2, n2); xs = y if xs is None else xs + y
        x = xs / g.num_kernels
        x = g.conv_post(F.leaky_relu(x) * m2)
        spec = ek.SPEC(x[:, :ek.K, :]); phase = torch.sin(x[:, ek.K:, :])
        return istft_masked(spec, phase, m2) * GAIN