File size: 10,165 Bytes
3b7b3fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""AmberNet language identification — standalone PyTorch, no NeMo dependency.

Faithful re-implementation of NeMo's EncDecSpeakerLabelModel as configured for
AmberNet (ContextNet-style separable conv encoder + squeeze-excite, x-vector
stats pooling head). Parameter names match the original .nemo checkpoint so the
weights load verbatim.
"""

import json
import math
import os

import torch
import torch.nn as nn
import torch.nn.functional as F

CONSTANT = 1e-5


class MaskedConv1d(nn.Module):
    """Conv1d that zeroes padded timesteps before convolving."""

    def __init__(self, in_ch, out_ch, kernel_size, padding=0, groups=1):
        super().__init__()
        self.conv = nn.Conv1d(in_ch, out_ch, kernel_size, padding=padding, groups=groups, bias=False)

    def forward(self, x, lens):
        mask = torch.arange(x.shape[-1], device=x.device)[None, :] < lens[:, None]
        return self.conv(x * mask.unsqueeze(1)), lens


class SqueezeExcite(nn.Module):
    def __init__(self, channels, reduction_ratio=8):
        super().__init__()
        self.fc = nn.Sequential(
            nn.Linear(channels, channels // reduction_ratio, bias=False),
            nn.ReLU(inplace=True),
            nn.Linear(channels // reduction_ratio, channels, bias=False),
        )

    def forward(self, x, lens):
        # Global context over valid timesteps only.
        mask = (torch.arange(x.shape[-1], device=x.device)[None, :] < lens[:, None]).unsqueeze(1)
        x = x * mask
        y = x.sum(dim=-1, keepdim=True) / mask.sum(dim=-1, keepdim=True).to(x.dtype)
        y = self.fc(y.transpose(1, -1)).transpose(1, -1)
        return x * torch.sigmoid(y), lens


def _conv_bn(in_ch, out_ch, kernel_size, separable):
    padding = (kernel_size - 1) // 2
    if separable:
        layers = [
            MaskedConv1d(in_ch, in_ch, kernel_size, padding=padding, groups=in_ch),
            MaskedConv1d(in_ch, out_ch, 1),
        ]
    else:
        layers = [MaskedConv1d(in_ch, out_ch, kernel_size, padding=padding)]
    return layers + [nn.BatchNorm1d(out_ch, eps=1e-3, momentum=0.1)]


class JasperBlock(nn.Module):
    def __init__(self, inplanes, planes, repeat, kernel_size, dropout, residual, separable=True, se=True):
        super().__init__()
        layers, inp = [], inplanes
        for _ in range(repeat - 1):
            layers += _conv_bn(inp, planes, kernel_size, separable)
            layers += [nn.ReLU(inplace=True), nn.Dropout(dropout)]
            inp = planes
        layers += _conv_bn(inp, planes, kernel_size, separable)
        if se:
            layers.append(SqueezeExcite(planes))
        self.mconv = nn.ModuleList(layers)
        self.res = nn.ModuleList([nn.ModuleList(_conv_bn(inplanes, planes, 1, separable=False))]) if residual else None
        self.mout = nn.Sequential(nn.ReLU(inplace=True), nn.Dropout(dropout))

    def forward(self, x, lens):
        out = x
        for layer in self.mconv:
            out, lens = layer(out, lens) if isinstance(layer, (MaskedConv1d, SqueezeExcite)) else (layer(out), lens)
        if self.res is not None:
            res = x
            for layer in self.res[0]:
                res, _ = layer(res, lens) if isinstance(layer, MaskedConv1d) else (layer(res), lens)
            out = out + res
        return self.mout(out), lens


class ConvASREncoder(nn.Module):
    def __init__(self, feat_in, jasper):
        super().__init__()
        blocks = []
        for cfg in jasper:
            blocks.append(
                JasperBlock(
                    feat_in,
                    cfg["filters"],
                    cfg["repeat"],
                    cfg["kernel"][0],
                    cfg["dropout"],
                    cfg["residual"],
                    cfg.get("separable", True),
                    cfg.get("se", True),
                )
            )
            feat_in = cfg["filters"]
        self.encoder = nn.ModuleList(blocks)

    def forward(self, x, lens):
        for block in self.encoder:
            x, lens = block(x, lens)
        return x, lens


class SpeakerDecoder(nn.Module):
    """x-vector stats pooling (mean+std) → embedding → classifier."""

    def __init__(self, feat_in, num_classes, emb_size):
        super().__init__()
        self.emb_layers = nn.ModuleList(
            [
                nn.Sequential(
                    nn.Linear(feat_in * 2, emb_size),
                    nn.BatchNorm1d(emb_size, affine=False, track_running_stats=True),
                    nn.ReLU(inplace=True),
                )
            ]
        )
        self.final = nn.Linear(emb_size, num_classes)

    def forward(self, x, lens):
        mask = (torch.arange(x.shape[-1], device=x.device)[None, :] < lens[:, None]).unsqueeze(1)
        x = x * mask
        mean = x.sum(dim=-1) / lens.unsqueeze(-1).to(x.dtype)
        std = (
            ((x - mean.unsqueeze(-1)) * mask).pow(2).sum(-1).div(lens.view(-1, 1) - 1).clamp(min=1e-10).sqrt()
        )
        pool = torch.cat([mean, std], dim=-1)
        layer = self.emb_layers[0]
        emb = layer[:2](pool)
        return self.final(layer(pool)), emb


class MelSpectrogram(nn.Module):
    """NeMo AudioToMelSpectrogramPreprocessor, inference path (no dither/augment)."""

    def __init__(self, sample_rate=16000, n_fft=512, win_length=400, hop_length=160, n_mels=80, preemph=0.97):
        super().__init__()
        self.n_fft, self.win_length, self.hop_length, self.preemph = n_fft, win_length, hop_length, preemph
        self.register_buffer("window", torch.hann_window(win_length, periodic=False))
        self.register_buffer("fb", torch.zeros(1, n_mels, n_fft // 2 + 1))
        self.register_buffer("stft_basis", torch.zeros(n_fft + 2, 1, n_fft), persistent=False)
        self.build_stft_basis()

    def build_stft_basis(self):
        """STFT as a strided conv: portable to every ONNX runtime, unlike the STFT op.

        Must be re-run after loading weights, since it is derived from `window`.
        """
        n_bins = self.n_fft // 2 + 1
        pad = (self.n_fft - self.win_length) // 2
        window = F.pad(self.window, (pad, self.n_fft - self.win_length - pad))
        angle = torch.outer(
            torch.arange(n_bins, dtype=torch.float64), torch.arange(self.n_fft, dtype=torch.float64)
        ) * (-2 * math.pi / self.n_fft)
        basis = torch.cat([torch.cos(angle), torch.sin(angle)]).float() * window
        self.stft_basis = basis.unsqueeze(1).to(self.stft_basis.device)

    def get_seq_len(self, seq_len):
        return torch.div(seq_len + self.n_fft // 2 * 2 - self.n_fft, self.hop_length, rounding_mode="floor").long()

    def forward(self, x, seq_len):
        out_len = self.get_seq_len(seq_len)
        time_mask = torch.arange(x.shape[1], device=x.device)[None, :] < seq_len[:, None]
        x = torch.cat((x[:, :1], x[:, 1:] - self.preemph * x[:, :-1]), dim=1) * time_mask

        x = F.pad(x, (self.n_fft // 2, self.n_fft // 2))  # equivalent to torch.stft(center=True)
        spec = F.conv1d(x.unsqueeze(1), self.stft_basis, stride=self.hop_length)
        real, imag = spec.chunk(2, dim=1)
        power = real.pow(2) + imag.pow(2)  # magnitude^2
        mel = torch.matmul(self.fb, power)
        mel = torch.log(mel + 2**-24)

        # per-feature normalization over valid frames
        valid = torch.arange(mel.shape[-1], device=mel.device)[None, :] < out_len[:, None]
        n = valid.sum(dim=1)[:, None]
        mean = torch.where(valid.unsqueeze(1), mel, torch.zeros_like(mel)).sum(-1) / n
        std = torch.sqrt(
            torch.where(valid.unsqueeze(1), mel - mean.unsqueeze(-1), torch.zeros_like(mel)).pow(2).sum(-1) / (n - 1.0)
        )
        mel = (mel - mean.unsqueeze(-1)) / (std + CONSTANT).unsqueeze(-1)
        return mel * valid.unsqueeze(1), out_len


class AmberNet(nn.Module):
    """Spoken language identification over 107 languages.

    forward(audio [B, N] float32 16 kHz, audio_len [B]) -> (logits [B, 107], embedding [B, 512])
    """

    def __init__(self, config):
        super().__init__()
        self.config = config
        self.labels = config["labels"]
        self.preprocessor = nn.Module()
        self.preprocessor.featurizer = MelSpectrogram(**config["preprocessor"])
        self.encoder = ConvASREncoder(config["encoder"]["feat_in"], config["encoder"]["jasper"])
        self.decoder = SpeakerDecoder(**config["decoder"])

    def load_state_dict(self, *args, **kwargs):
        result = super().load_state_dict(*args, **kwargs)
        self.preprocessor.featurizer.build_stft_basis()  # derived from the loaded window
        return result

    def forward(self, audio, audio_len):
        feats, feat_len = self.preprocessor.featurizer(audio, audio_len)
        enc, enc_len = self.encoder(feats, feat_len)
        return self.decoder(enc, enc_len)

    @torch.inference_mode()
    def classify(self, audio, audio_len=None, top_k=5):
        """Returns a list (per batch item) of (language, probability), most likely first."""
        if audio.ndim == 1:
            audio = audio.unsqueeze(0)
        if audio_len is None:
            audio_len = torch.full((audio.shape[0],), audio.shape[1], dtype=torch.long, device=audio.device)
        probs = self(audio, audio_len)[0].softmax(-1)
        top = probs.topk(min(top_k, len(self.labels)), dim=-1)
        return [
            [(self.labels[i], float(p)) for p, i in zip(row_p, row_i)]
            for row_p, row_i in zip(top.values, top.indices)
        ]

    @classmethod
    def from_pretrained(cls, path):
        """Load from a directory holding config.json + model.safetensors (or pytorch_model.bin)."""
        with open(os.path.join(path, "config.json")) as f:
            config = json.load(f)
        model = cls(config)
        safetensors_path = os.path.join(path, "model.safetensors")
        if os.path.exists(safetensors_path):
            from safetensors.torch import load_file

            state = load_file(safetensors_path)
        else:
            state = torch.load(os.path.join(path, "pytorch_model.bin"), map_location="cpu", weights_only=True)
        model.load_state_dict(state)
        return model.eval()