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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import torch

import numpy as np
import torch.nn as nn
import onnxruntime as ort
import torch.nn.functional as F

from einops import rearrange
from torch.nn.utils.parametrizations import weight_norm

sys.path.append(os.getcwd())
os.environ["LRU_CACHE_CAPACITY"] = "3"

from infer.lib.predictors.FCPE.wav2mel import Wav2Mel
from infer.lib.predictors.FCPE.encoder import EncoderLayer, ConformerNaiveEncoder
from infer.lib.predictors.FCPE.utils import batch_interp_with_replacement_detach, decrypt_model, DotDict

@torch.no_grad()
def cent_to_f0(cent):
    return 10 * 2 ** (cent / 1200)

@torch.no_grad()
def f0_to_cent(f0):
    return 1200 * (f0 / 10).log2()

@torch.no_grad()
def latent2cents_decoder(cent_table, y, threshold = 0.05, mask = True):
    if str(y.device).startswith("privateuseone"): 
        cent_table = cent_table.cpu()
        y = y.cpu()

    B, N, _ = y.size()
    ci = cent_table[None, None, :].expand(B, N, -1)
    rtn = (ci * y).sum(dim=-1, keepdim=True) / y.sum(dim=-1, keepdim=True)  

    if mask:
        confident = y.max(dim=-1, keepdim=True)[0]
        confident_mask = torch.ones_like(confident)
        confident_mask[confident <= threshold] = float("-INF")
        rtn = rtn * confident_mask

    return rtn

@torch.no_grad()
def latent2cents_local_decoder(cent_table, out_dims, y, threshold = 0.05, mask = True):
    if str(y.device).startswith("privateuseone"): 
        cent_table = cent_table.cpu()
        y = y.cpu()

    B, N, _ = y.size()
    ci = cent_table[None, None, :].expand(B, N, -1)
    confident, max_index = y.max(dim=-1, keepdim=True)

    local_argmax_index = torch.arange(0, 9).to(max_index.device) + (max_index - 4)
    local_argmax_index[local_argmax_index < 0] = 0
    local_argmax_index[local_argmax_index >= out_dims] = out_dims - 1

    y_l = y.gather(-1, local_argmax_index)
    rtn = (ci.gather(-1, local_argmax_index) * y_l).sum(dim=-1, keepdim=True) / y_l.sum(dim=-1, keepdim=True) 

    if mask:
        confident_mask = torch.ones_like(confident)
        confident_mask[confident <= threshold] = float("-INF")
        rtn = rtn * confident_mask

    return rtn

def cents_decoder(cent_table, y, confidence, threshold = 0.05, mask=True):
    if str(y.device).startswith("privateuseone"): 
        cent_table = cent_table.cpu()
        y = y.cpu()

    B, N, _ = y.size()
    rtn = (cent_table[None, None, :].expand(B, N, -1) * y).sum(dim=-1, keepdim=True) / y.sum(dim=-1, keepdim=True)

    if mask:
        confident = y.max(dim=-1, keepdim=True)[0]
        confident_mask = torch.ones_like(confident)
        confident_mask[confident <= threshold] = float("-INF")
        rtn = rtn * confident_mask

    return (rtn, confident) if confidence else rtn

def cents_local_decoder(cent_table, y, n_out, confidence, threshold = 0.05, mask=True):
    if str(y.device).startswith("privateuseone"): 
        cent_table = cent_table.cpu()
        y = y.cpu()

    B, N, _ = y.size()
    confident, max_index = y.max(dim=-1, keepdim=True)
    local_argmax_index = (torch.arange(0, 9).to(max_index.device) + (max_index - 4)).clamp(0, n_out - 1)
    y_l = y.gather(-1, local_argmax_index)
    rtn = (cent_table[None, None, :].expand(B, N, -1).gather(-1, local_argmax_index) * y_l).sum(dim=-1, keepdim=True) / y_l.sum(dim=-1, keepdim=True)

    if mask:
        confident_mask = torch.ones_like(confident)
        confident_mask[confident <= threshold] = float("-INF")
        rtn = rtn * confident_mask

    return (rtn, confident) if confidence else rtn

class PCmer(nn.Module):
    def __init__(
        self, 
        num_layers, 
        num_heads, 
        dim_model, 
        dim_keys, 
        dim_values, 
        residual_dropout, 
        attention_dropout
    ):
        super().__init__()
        self.num_layers = num_layers
        self.num_heads = num_heads
        self.dim_model = dim_model
        self.dim_values = dim_values
        self.dim_keys = dim_keys
        self.residual_dropout = residual_dropout
        self.attention_dropout = attention_dropout
        self._layers = nn.ModuleList([EncoderLayer(self) for _ in range(num_layers)])

    def forward(self, phone, mask=None):
        for layer in self._layers:
            phone = layer(phone, mask)

        return phone

class CFNaiveMelPE(nn.Module):
    def __init__(
        self, 
        input_channels, 
        out_dims, 
        hidden_dims = 512, 
        n_layers = 6, 
        n_heads = 8, 
        f0_max = 1975.5, 
        f0_min = 32.70, 
        use_fa_norm = False, 
        conv_only = False, 
        conv_dropout = 0, 
        atten_dropout = 0, 
        use_harmonic_emb = False
    ):
        super().__init__()
        self.input_channels = input_channels
        self.out_dims = out_dims
        self.hidden_dims = hidden_dims
        self.n_layers = n_layers
        self.n_heads = n_heads
        self.f0_max = f0_max
        self.f0_min = f0_min
        self.use_fa_norm = use_fa_norm

        self.harmonic_emb = nn.Embedding(9, hidden_dims) if use_harmonic_emb else None
        self.input_stack = nn.Sequential(
            nn.Conv1d(
                input_channels, 
                hidden_dims, 
                3, 
                1, 
                1
            ), 
            nn.GroupNorm(
                4, 
                hidden_dims
            ), 
            nn.LeakyReLU(), 
            nn.Conv1d(
                hidden_dims, 
                hidden_dims, 
                3, 
                1, 
                1
            )
        )
        self.net = ConformerNaiveEncoder(
            num_layers=n_layers, 
            num_heads=n_heads, 
            dim_model=hidden_dims, 
            use_norm=use_fa_norm, 
            conv_only=conv_only, 
            conv_dropout=conv_dropout, 
            atten_dropout=atten_dropout
        )
        self.norm = nn.LayerNorm(hidden_dims)
        self.output_proj = weight_norm(
            nn.Linear(
                hidden_dims, 
                out_dims
            )
        )

        self.cent_table_b = torch.linspace(
            f0_to_cent(torch.Tensor([f0_min]))[0], 
            f0_to_cent(torch.Tensor([f0_max]))[0], 
            out_dims
        ).detach()
        self.gaussian_blurred_cent_mask_b = (
            1200 * torch.Tensor([self.f0_max / 10.]).log2()
        )[0].detach()

        self.register_buffer("cent_table", self.cent_table_b)
        self.register_buffer("gaussian_blurred_cent_mask", self.gaussian_blurred_cent_mask_b)

    def forward(self, x, _h_emb=None):
        x = self.input_stack(x.transpose(-1, -2)).transpose(-1, -2)

        if self.harmonic_emb is not None:
            if _h_emb is None:
                x += self.harmonic_emb(torch.LongTensor([0]).to(x.device))
            else:
                x += self.harmonic_emb(torch.LongTensor([int(_h_emb)]).to(x.device))

        return self.output_proj(self.norm(self.net(x))).sigmoid()

    @torch.no_grad()
    def infer(self, mel, decoder = "local_argmax", threshold = 0.05):
        latent = self.forward(mel)

        return cent_to_f0(
            (
                latent2cents_decoder(
                    self.cent_table, 
                    latent, 
                    threshold=threshold
                )
            ) if decoder == "argmax" else (
                latent2cents_local_decoder(
                    self.cent_table, 
                    self.out_dims, 
                    latent, 
                    threshold=threshold
                )
            )
        )

class FCPE_LEGACY(nn.Module):
    def __init__(
        self, 
        input_channel=128, 
        out_dims=360, 
        n_layers=12, 
        n_chans=512, 
        f0_max=1975.5, 
        f0_min=32.70, 
        confidence=False, 
        threshold=0.05, 
        use_input_conv=True
    ):
        super().__init__()
        self.n_out = out_dims
        self.f0_max = f0_max
        self.f0_min = f0_min
        self.confidence = confidence
        self.threshold = threshold
        self.use_input_conv = use_input_conv

        self.cent_table_b = torch.Tensor(
            np.linspace(
                f0_to_cent(torch.Tensor([f0_min]))[0], 
                f0_to_cent(torch.Tensor([f0_max]))[0], 
                out_dims
            )
        )
        self.register_buffer("cent_table", self.cent_table_b)

        self.stack = nn.Sequential(
            nn.Conv1d(
                input_channel, 
                n_chans, 
                3, 
                1, 
                1
            ), 
            nn.GroupNorm(
                4, 
                n_chans
            ), 
            nn.LeakyReLU(), 
            nn.Conv1d(
                n_chans, 
                n_chans, 
                3, 
                1, 
                1
            )
        )
        self.decoder = PCmer(
            num_layers=n_layers, 
            num_heads=8, 
            dim_model=n_chans, 
            dim_keys=n_chans, 
            dim_values=n_chans, 
            residual_dropout=0.1, 
            attention_dropout=0.1
        )
        self.norm = nn.LayerNorm(n_chans)
        self.dense_out = weight_norm(
            nn.Linear(
                n_chans, 
                self.n_out
            )
        )

    def forward(self, mel, return_hz_f0=False, cdecoder="local_argmax", output_interp_target_length=None):
        x = self.decoder(self.stack(mel.transpose(1, 2)).transpose(1, 2) if self.use_input_conv else mel)
        x = self.dense_out(self.norm(x)).sigmoid()

        x = cent_to_f0(
            (
                cents_decoder(
                    self.cent_table, 
                    x, 
                    self.confidence, 
                    threshold=self.threshold, 
                    mask=True
                )
            ) if cdecoder == "argmax" else (
                cents_local_decoder(
                    self.cent_table, 
                    x, 
                    self.n_out, 
                    self.confidence, 
                    threshold=self.threshold, 
                    mask=True
                )
            )
        )

        x = (1 + x / 700).log() if not return_hz_f0 else x

        if output_interp_target_length is not None: 
            x = F.interpolate(
                torch.where(x == 0, float("nan"), x).transpose(1, 2), 
                size=int(output_interp_target_length), 
                mode="linear"
            ).transpose(1, 2)

            x = torch.where(x.isnan(), float(0.0), x)

        return x

    def gaussian_blurred_cent(self, cents):
        B, N, _ = cents.size()

        return (
            -(self.cent_table[None, None, :].expand(B, N, -1) - cents).square() / 1250
        ).exp() * (cents > 0.1) & (
            cents < (1200.0 * np.log2(self.f0_max / 10.0))
        ).float()

class InferCFNaiveMelPE(torch.nn.Module):
    def __init__(
        self, 
        args, 
        state_dict
    ):
        super().__init__()
        self.model = CFNaiveMelPE(
            input_channels=args.mel.num_mels, 
            out_dims=args.model.out_dims, 
            hidden_dims=args.model.hidden_dims, 
            n_layers=args.model.n_layers, 
            n_heads=args.model.n_heads, 
            f0_max=args.model.f0_max, 
            f0_min=args.model.f0_min, 
            use_fa_norm=args.model.use_fa_norm, 
            conv_only=args.model.conv_only, 
            conv_dropout=args.model.conv_dropout, 
            atten_dropout=args.model.atten_dropout, 
            use_harmonic_emb=False
        )
        self.model.load_state_dict(state_dict)
        self.model.eval()
        self.register_buffer("tensor_device_marker", torch.tensor(1.0).float(), persistent=False)

    def forward(self, mel, decoder_mode = "local_argmax", threshold = 0.006):
        with torch.no_grad():
            mels = rearrange(torch.stack([mel], -1), "B T C K -> (B K) T C")
            f0s = rearrange(self.model.infer(mels, decoder=decoder_mode, threshold=threshold), "(B K) T 1 -> B T (K 1)", K=1)

        return f0s 

    def infer(
        self, 
        mel, 
        decoder_mode = "local_argmax", 
        threshold = 0.006, 
        f0_min = None, 
        f0_max = None, 
        interp_uv = False, 
        output_interp_target_length = None, 
        return_uv = False
    ):
        f0 = self.__call__(mel, decoder_mode, threshold)
        f0_for_uv = f0

        uv = (f0_for_uv < f0_min).type(f0_for_uv.dtype)
        f0 = f0 * (1 - uv)

        if interp_uv: 
            f0 = batch_interp_with_replacement_detach(
                uv.squeeze(-1).bool(), 
                f0.squeeze(-1)
            ).unsqueeze(-1)

        if f0_max is not None: f0[f0 > f0_max] = f0_max

        if output_interp_target_length is not None: 
            f0 = F.interpolate(
                torch.where(f0 == 0, float("nan"), f0).transpose(1, 2), 
                size=int(output_interp_target_length), 
                mode="linear"
            ).transpose(1, 2)

            f0 = torch.where(f0.isnan(), float(0.0), f0)

        if return_uv: return f0, F.interpolate(uv.transpose(1, 2), size=int(output_interp_target_length), mode="nearest").transpose(1, 2)
        else: return f0

class FCPEInfer_LEGACY:
    def __init__(
        self, 
        configs, 
        model_path, 
        device=None, 
        dtype=torch.float32, 
        providers=None, 
        onnx=False, 
        f0_min=50, 
        f0_max=1100
    ):
        if device is None: device = "cuda" if torch.cuda.is_available() else "cpu"
        self.device = device
        self.dtype = dtype
        self.onnx = onnx
        self.f0_min = f0_min
        self.f0_max = f0_max
        self.wav2mel = Wav2Mel(device=self.device, dtype=self.dtype)

        if self.onnx:
            sess_options = ort.SessionOptions()
            sess_options.log_severity_level = 3
            self.model = ort.InferenceSession(decrypt_model(configs, model_path), sess_options=sess_options, providers=providers)
        else:
            ckpt = torch.load(model_path, map_location="cpu", weights_only=True)
            self.args = DotDict(ckpt["config"])
            model = FCPE_LEGACY(
                input_channel=self.args.model.input_channel, 
                out_dims=self.args.model.out_dims, 
                n_layers=self.args.model.n_layers, 
                n_chans=self.args.model.n_chans,  
                f0_max=self.f0_max, 
                f0_min=self.f0_min, 
                confidence=self.args.model.confidence
            )
            model.to(self.device).to(self.dtype)
            model.load_state_dict(ckpt["model"])
            model.eval()
            self.model = model

    @torch.no_grad()
    def __call__(self, audio, sr, threshold=0.05, p_len=None):
        if not self.onnx: self.model.threshold = threshold
        if not hasattr(self, "numpy_threshold") and self.onnx: self.numpy_threshold = np.array(threshold, dtype=np.float32)

        mel = self.wav2mel(audio=audio[None, :], sample_rate=sr).to(self.dtype)

        if self.onnx:
            return torch.as_tensor(
                self.model.run(
                    [self.model.get_outputs()[0].name], 
                    {
                        self.model.get_inputs()[0].name: mel.detach().cpu().numpy(), 
                        self.model.get_inputs()[1].name: self.numpy_threshold
                    }
                )[0], 
                dtype=self.dtype, 
                device=self.device
            )
        else: 
            return self.model(
                mel=mel, 
                return_hz_f0=True, 
                output_interp_target_length=p_len
            )

class FCPEInfer:
    def __init__(
        self, 
        configs, 
        model_path, 
        device=None, 
        dtype=torch.float32, 
        providers=None, 
        onnx=False, 
        f0_min=50, 
        f0_max=1100
    ):
        if device is None: device = "cuda" if torch.cuda.is_available() else "cpu"
        self.device = device
        self.dtype = dtype
        self.onnx = onnx
        self.f0_min = f0_min
        self.f0_max = f0_max
        self.wav2mel = Wav2Mel(device=self.device, dtype=self.dtype)

        if self.onnx:
            sess_options = ort.SessionOptions()
            sess_options.log_severity_level = 3
            self.model = ort.InferenceSession(decrypt_model(configs, model_path), sess_options=sess_options, providers=providers)
        else:
            ckpt = torch.load(model_path, map_location="cpu", weights_only=True)
            ckpt["config_dict"]["model"]["conv_dropout"] = ckpt["config_dict"]["model"]["atten_dropout"] = 0.0
            self.args = DotDict(ckpt["config_dict"])
            model = InferCFNaiveMelPE(self.args, ckpt["model"])
            self.model = model.to(device).to(self.dtype).eval()

    @torch.no_grad()
    def __call__(self, audio, sr, threshold=0.05, p_len=None):
        if not hasattr(self, "numpy_threshold") and self.onnx: self.numpy_threshold = np.array(threshold, dtype=np.float32)
        mel = self.wav2mel(audio=audio[None, :], sample_rate=sr).to(self.dtype)

        if self.onnx:
            return torch.as_tensor(
                self.model.run(
                    [self.model.get_outputs()[0].name], 
                    {
                        self.model.get_inputs()[0].name: mel.detach().cpu().numpy(), 
                        self.model.get_inputs()[1].name: self.numpy_threshold
                    }
                )[0], 
                dtype=self.dtype, 
                device=self.device
            ) 
        else: 
            return self.model.infer(
                mel, 
                threshold=threshold, 
                f0_min=self.f0_min, 
                f0_max=self.f0_max, 
                output_interp_target_length=p_len
            )

class FCPE:
    def __init__(
        self, 
        configs, 
        model_path, 
        hop_length=512, 
        f0_min=50, 
        f0_max=1100, 
        dtype=torch.float32, 
        device=None, 
        sample_rate=16000, 
        threshold=0.05, 
        providers=None, 
        onnx=False, 
        legacy=False
    ):
        self.model = FCPEInfer_LEGACY if legacy else FCPEInfer
        self.fcpe = self.model(configs, model_path, device=device, dtype=dtype, providers=providers, onnx=onnx, f0_min=f0_min, f0_max=f0_max)
        self.hop_length = hop_length
        self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
        self.threshold = threshold
        self.sample_rate = sample_rate
        self.dtype = dtype
        self.legacy = legacy

    def compute_f0(self, wav, p_len=None):
        x = torch.FloatTensor(wav).to(self.dtype).to(self.device)
        p_len = (x.shape[0] // self.hop_length) if p_len is None else p_len

        f0 = self.fcpe(x, sr=self.sample_rate, threshold=self.threshold, p_len=p_len)
        f0 = f0[:] if f0.dim() == 1 else f0[0, :, 0]

        if torch.all(f0 == 0): return f0.cpu().numpy() if p_len is None else np.zeros(p_len)
        return f0.cpu().numpy()