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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import Tuple, List, Optional

class RMSNorm(nn.Module):
    def __init__(self, dimension: int, eps: float = 1e-5):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(dimension))
        self.eps = eps

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        input_float = input.half()
        variance = input_float.pow(2).mean(dim=1, keepdim=True)
        input_norm = input_float * torch.rsqrt(variance + self.eps)
        return (input_norm * self.weight.unsqueeze(0).unsqueeze(-1)).type_as(input)


class RotaryEmbedding(nn.Module):
    def __init__(self, dim: int, max_position_embeddings: int = 2048, base: int = 10000):
        super().__init__()
        self.dim = dim
        self.max_position_embeddings = max_position_embeddings
        self.base = base
        
        inv_freq = 1. / (self.base ** (torch.arange(0, self.dim, 2).half() / self.dim))
        self.register_buffer('inv_freq', inv_freq)
        
        self._set_cos_sin_cache(
            seq_len=max_position_embeddings,
            device=self.inv_freq.device,
            dtype=torch.get_default_dtype()
        )
    
    def _set_cos_sin_cache(self, seq_len: int, device: torch.device, dtype: torch.dtype):
        self.max_seq_len_cached = seq_len
        t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
        
        freqs = torch.einsum('i,j->ij', t, self.inv_freq)
        emb = torch.cat((freqs, freqs), dim=-1)
        
        self.register_buffer('cos_cached', emb.cos()[None, None, :, :].to(dtype), persistent=False)
        self.register_buffer('sin_cached', emb.sin()[None, None, :, :].to(dtype), persistent=False)
    
    def forward(self, x: torch.Tensor, seq_len: int) -> Tuple[torch.Tensor, torch.Tensor]:
        if seq_len > self.max_seq_len_cached:
            self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
        
        return (
            self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
            self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
        )


def rotate_half(x: torch.Tensor) -> torch.Tensor:
    x1 = x[..., : x.shape[-1] // 2]
    x2 = x[..., x.shape[-1] // 2 :]
    return torch.cat((-x2, x1), dim=-1)


class RoformerLayer(nn.Module):
    def __init__(
        self, 
        feature_dim: int, 
        num_heads: int = 8,
        max_seq_len: int = 10000,
        dropout: float = 0.0,
        mlp_ratio: float = 4.0,
        rope_base: int = 10000
    ):
        super().__init__()
        assert feature_dim % num_heads == 0, "feature_dim must be divisible by num_heads"

        self.feature_dim = feature_dim
        self.num_heads = num_heads
        self.head_dim = feature_dim // num_heads

        self.rotary_emb = RotaryEmbedding(self.head_dim, max_position_embeddings=max_seq_len, base=rope_base)
        self.dropout = dropout

        self.input_norm = RMSNorm(feature_dim)
        self.qkv_proj = nn.Linear(feature_dim, feature_dim * 3, bias=False)
        self.output_proj = nn.Linear(feature_dim, feature_dim, bias=False)

        mlp_hidden_dim = int(feature_dim * mlp_ratio)
        self.mlp_norm = RMSNorm(feature_dim)
        self.mlp_up = nn.Linear(feature_dim, mlp_hidden_dim * 2, bias=False)
        self.mlp_down = nn.Linear(mlp_hidden_dim, feature_dim, bias=False)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B, N, T = x.shape
        x_residual = x
        x_norm = self.input_norm(x).transpose(1, 2)

        qkv = self.qkv_proj(x_norm)
        qkv = qkv.view(B, T, 3, self.num_heads, self.head_dim)
        qkv = qkv.permute(2, 0, 3, 1, 4)
        Q, K, V = qkv[0], qkv[1], qkv[2]

        cos, sin = self.rotary_emb(Q, seq_len=T)
        Q = (Q * cos) + (rotate_half(Q) * sin)
        K = (K * cos) + (rotate_half(K) * sin)

        attn_output = F.scaled_dot_product_attention(
            Q, K, V, dropout_p=self.dropout if self.training else 0.0, is_causal=False
        )

        attn_output = attn_output.permute(0, 2, 1, 3).contiguous().view(B, T, N)
        attn_output = self.output_proj(attn_output).transpose(1, 2)

        x = x_residual + attn_output

        x_residual = x
        x_norm = self.mlp_norm(x).transpose(1, 2)

        mlp_out = self.mlp_up(x_norm)
        gate, values = mlp_out.chunk(2, dim=-1)
        mlp_out = F.silu(gate) * values
        mlp_out = self.mlp_down(mlp_out)

        output = x_residual + mlp_out.transpose(1, 2)

        return output

class Roformer(nn.Module):
    def __init__(self, input_size, hidden_size, num_head=8, theta=10000, window=10000, 
                 input_drop=0., attention_drop=0., causal=True):
        super().__init__()

        self.input_size = input_size
        self.hidden_size = hidden_size // num_head
        self.num_head = num_head
        self.theta = theta
        self.window = window
        cos_freq, sin_freq = self._calc_rotary_emb()
        self.register_buffer("cos_freq", cos_freq)
        self.register_buffer("sin_freq", sin_freq)
        
        self.attention_drop = attention_drop
        self.causal = causal
        self.eps = 1e-5

        self.input_norm = RMSNorm(self.input_size)
        self.input_drop = nn.Dropout(p=input_drop)
        self.weight = nn.Conv1d(self.input_size, self.hidden_size*self.num_head*3, 1, bias=False)
        self.output = nn.Conv1d(self.hidden_size*self.num_head, self.input_size, 1, bias=False)

        self.MLP = nn.Sequential(RMSNorm(self.input_size),
                                 nn.Conv1d(self.input_size, self.input_size*8, 1, bias=False),
                                 nn.SiLU()
                                )
        self.MLP_output = nn.Conv1d(self.input_size*4, self.input_size, 1, bias=False)

    def _calc_rotary_emb(self):
        freq = 1. / (self.theta ** (torch.arange(0, self.hidden_size, 2)[:(self.hidden_size // 2)] / self.hidden_size))
        freq = freq.reshape(1, -1)
        pos = torch.arange(0, self.window).reshape(-1, 1)
        cos_freq = torch.cos(pos*freq)
        sin_freq = torch.sin(pos*freq)
        cos_freq = torch.stack([cos_freq]*2, -1).reshape(self.window, self.hidden_size)
        sin_freq = torch.stack([sin_freq]*2, -1).reshape(self.window, self.hidden_size)

        return cos_freq, sin_freq
    
    def _add_rotary_emb(self, feature, pos):
        N = feature.shape[-1]

        feature_reshape = feature.reshape(-1, N)
        pos = min(pos, self.window-1)
        cos_freq = self.cos_freq[pos]
        sin_freq = self.sin_freq[pos]
        reverse_sign = torch.from_numpy(np.asarray([-1, 1])).to(feature.device).type(feature.dtype)
        feature_reshape_neg = (torch.flip(feature_reshape.reshape(-1, N//2, 2), [-1]) * reverse_sign.reshape(1, 1, 2)).reshape(-1, N)
        feature_rope = feature_reshape * cos_freq.unsqueeze(0) + feature_reshape_neg * sin_freq.unsqueeze(0)
    
        return feature_rope.reshape(feature.shape)

    def _add_rotary_sequence(self, feature):
        T, N = feature.shape[-2:]
        feature_reshape = feature.reshape(-1, T, N)

        cos_freq = self.cos_freq[:T]
        sin_freq = self.sin_freq[:T]
        reverse_sign = torch.from_numpy(np.asarray([-1, 1])).to(feature.device).type(feature.dtype)
        feature_reshape_neg = (torch.flip(feature_reshape.reshape(-1, N//2, 2), [-1]) * reverse_sign.reshape(1, 1, 2)).reshape(-1, T, N)
        feature_rope = feature_reshape * cos_freq.unsqueeze(0) + feature_reshape_neg * sin_freq.unsqueeze(0)
    
        return feature_rope.reshape(feature.shape)
    
    def forward(self, input):
        B, _, T = input.shape

        weight = self.weight(self.input_drop(self.input_norm(input))).reshape(B, self.num_head, self.hidden_size*3, T).transpose(-2,-1)
        Q, K, V = torch.split(weight, self.hidden_size, dim=-1)
        Q_rot = self._add_rotary_sequence(Q)
        K_rot = self._add_rotary_sequence(K)

        attention_output = F.scaled_dot_product_attention(Q_rot.contiguous(), K_rot.contiguous(), V.contiguous(), dropout_p=self.attention_drop, is_causal=self.causal)  # B, num_head, T, N
        attention_output = attention_output.transpose(-2,-1).reshape(B, -1, T)
        output = self.output(attention_output) + input

        gate, z = self.MLP(output).chunk(2, dim=1)
        output = output + self.MLP_output(F.silu(gate) * z)

        return output

class ConvBlock(nn.Module):
    def __init__(self, channels: int, kernel_size: int, dilation: int, expansion: int = 4):
        super().__init__()
        padding = (kernel_size - 1) * dilation // 2

        self.dwconv = nn.Conv1d(
            channels, channels, kernel_size, padding=padding, dilation=dilation, groups=channels
        )
        self.norm = RMSNorm(channels)
        self.pwconv1 = nn.Conv1d(channels, channels * expansion, 1)
        self.act = nn.GLU(dim=1)
        self.pwconv2 = nn.Conv1d(channels * expansion // 2, channels, 1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.dwconv(x)
        x = self.norm(x)
        x = self.pwconv1(x)
        x = self.act(x)
        x = self.pwconv2(x)
        return x


class ICB(nn.Module):
    def __init__(self, channels: int, kernel_size: int = 7, dilation: int = 1, layer_scale_init_value: float = 1e-6):
        super().__init__()
        self.block1 = ConvBlock(channels, kernel_size, 1, )
        self.block2 = ConvBlock(channels, kernel_size, dilation)
        self.block3 = ConvBlock(channels, kernel_size, 1)
        self.gamma = nn.Parameter(layer_scale_init_value * torch.ones((channels)), requires_grad=True)
        
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        residual = x
        x = self.block1(x)
        x = self.block2(x)
        x = self.block3(x)
        return x * self.gamma.unsqueeze(0).unsqueeze(-1) + residual


class BSNet(nn.Module):
    def __init__(
        self, 
        feature_dim: int, 
        kernel_size: int, 
        dilation_rate: int, 
        num_heads: int,
        max_bands: int = 512,
        band_rope_base: int = 10000,
        layer_scale_init_value: float = 1e-6
    ):
        super().__init__()
        self.band_net = Roformer(feature_dim, feature_dim, num_head=num_heads, window=max_bands, causal=False)

        self.seq_net = ICB(
            feature_dim, 
            kernel_size=kernel_size, 
            dilation=dilation_rate, 
            layer_scale_init_value=layer_scale_init_value
        )
        
    def forward(self, input: torch.Tensor) -> torch.Tensor:
        B, nband, N, T = input.shape
        
        band_input = input.permute(0, 3, 2, 1).reshape(B * T, N, nband)
        band_output = self.band_net(band_input)
        band_output = band_output.view(B, T, N, nband).permute(0, 3, 2, 1)
        
        seq_input = band_output.reshape(B * nband, N, T)
        seq_output = self.seq_net(seq_input)
        output = seq_output.view(B, nband, N, T)
        
        return output

class Renaissance(nn.Module):
    def __init__(
        self,
        n_freqs: int = 2049,
        feature_dim: int = 128,
        layer: int = 9,
        sample_rate: int = 48000,
        dilation_start_layer: int = 3,
        n_bands: int = 80,
        num_heads: int = 16,
        max_seq_len: int = 10000,
        band_rope_base: int = 10000,
        temporal_rope_base: int = 10000
    ):
        super().__init__()
        self.enc_dim = n_freqs
        self.feature_dim = feature_dim
        self.eps = 1e-7
        self.dilation_start_layer = dilation_start_layer
        self.n_bands = n_bands
        self.sr = sample_rate
        self.max_seq_len = max_seq_len
        self.band_rope_base = band_rope_base
        self.temporal_rope_base = temporal_rope_base

        self.band_width = self._generate_mel_bandwidths()
        self.nband = len(self.band_width)
        assert self.enc_dim == sum(self.band_width), "Mel band splitting failed to cover all frequencies."

        self._build_feature_extractor()
        self._build_main_network(layer, num_heads)
        self._build_output_synthesis()

        self.apply(self._init_weights)

    def _init_weights(self, module):
        if isinstance(module, (nn.Linear, nn.Conv1d)):
            nn.init.kaiming_normal_(module.weight, mode='fan_out', nonlinearity='relu')
            if module.bias is not None:
                nn.init.zeros_(module.bias)

    def _generate_mel_bandwidths(self) -> List[int]:
        def hz_to_mel(hz): return 2595 * np.log10(1 + hz / 700.)
        def mel_to_hz(mel): return 700 * (10**(mel / 2595) - 1)

        min_freq, max_freq = 0.1, self.sr / 2
        min_mel, max_mel = hz_to_mel(min_freq), hz_to_mel(max_freq)

        mel_points = np.linspace(min_mel, max_mel, self.n_bands + 1)
        hz_points = mel_to_hz(mel_points)
        
        bin_width = self.sr / 2 / self.enc_dim
        bw = np.round(np.diff(hz_points) / bin_width).astype(int)
        
        bw = np.maximum(1, bw)
        
        remainder = self.enc_dim - np.sum(bw)
        if remainder != 0:
            sorted_indices = np.argsort(bw)
            op = 1 if remainder > 0 else -1
            indices_to_adjust = sorted_indices if op == 1 else sorted_indices[::-1]
            
            for i in range(abs(remainder)):
                idx = indices_to_adjust[i % len(indices_to_adjust)]
                if bw[idx] + op > 0:
                    bw[idx] += op
        
        if np.sum(bw) != self.enc_dim:
            bw[-1] += self.enc_dim - np.sum(bw)
            
        return bw.tolist()

    def _build_feature_extractor(self):
        self.feature_extractor_layers = nn.ModuleList([
            nn.Sequential(RMSNorm(bw * 2 + 1), nn.Conv1d(bw * 2 + 1, self.feature_dim, 1))
            for bw in self.band_width
        ])
    
    def _build_main_network(self, num_layers, num_heads):
        self.net = nn.ModuleList()
        max_bands = max(512, self.nband * 2)

        layer_scale_init = 1e-6
        
        for i in range(num_layers):
            dilation = min(2 ** max(0, i - self.dilation_start_layer + 1), 4)
            self.net.append(BSNet(
                self.feature_dim, 
                kernel_size=7, 
                dilation_rate=dilation, 
                num_heads=num_heads, 
                max_bands=max_bands, 
                band_rope_base=self.band_rope_base,
                layer_scale_init_value=layer_scale_init
            ))

    def _build_output_synthesis(self):
        self.output_layers = nn.ModuleList([
            nn.Sequential(
                RMSNorm(self.feature_dim),
                nn.Conv1d(self.feature_dim, self.feature_dim * 2, 1),
                nn.SiLU(),
                nn.Conv1d(self.feature_dim * 2, bw * 4, kernel_size=1),
                nn.GLU(dim=1),
            ) for bw in self.band_width
        ])

    def spec_band_split(self, spec: torch.Tensor) -> Tuple[List[torch.Tensor], torch.Tensor]:
        subband_spec_ri = []
        subband_power = []
        band_idx = 0
        for width in self.band_width:
            this_spec_ri = spec[:, band_idx : band_idx + width, :, :]
            subband_spec_ri.append(this_spec_ri)
            
            power = (this_spec_ri.pow(2).sum(dim=-1)).sum(dim=1, keepdim=True).add(self.eps).sqrt()
            subband_power.append(power)
            band_idx += width
            
        subband_power = torch.cat(subband_power, 1)
        return subband_spec_ri, subband_power

    def feature_extraction(self, input_spec: torch.Tensor) -> torch.Tensor:
        subband_spec_ri, subband_power = self.spec_band_split(input_spec)
        features = []
        for i in range(self.nband):
            power_for_norm = subband_power[:, i:i+1, :].unsqueeze(1)
            norm_spec_ri = subband_spec_ri[i] / (power_for_norm.transpose(2,3) + self.eps)
            B, F_band, T, _ = norm_spec_ri.shape
            norm_spec_flat = norm_spec_ri.permute(0, 3, 1, 2).reshape(B, F_band*2, T)

            log_power_feature = torch.log(power_for_norm.squeeze(1) + self.eps)
            feature_input = torch.cat([norm_spec_flat, log_power_feature], dim=1)
            
            features.append(self.feature_extractor_layers[i](feature_input))
            
        return torch.stack(features, 1)

    def forward(self, input_spec: torch.Tensor) -> torch.Tensor:
        B, F, T, _ = input_spec.shape

        features = self.feature_extraction(input_spec)

        residual_features = features 
        processed = features
        for layer in self.net:
            processed = layer(processed)
        processed = processed + residual_features

        est_spec_bands = []
        for i in range(self.nband):
            band_output = self.output_layers[i](processed[:, i])
            bw = self.band_width[i]
            est_spec_band = band_output.view(B, bw, 2, T).permute(0, 1, 3, 2)
            est_spec_bands.append(est_spec_band)
        est_spec_full = torch.cat(est_spec_bands, dim=1)

        return est_spec_full