File size: 33,694 Bytes
f88bd6f
 
 
c163568
f88bd6f
 
 
 
 
c163568
 
 
 
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
c163568
f88bd6f
c163568
 
 
 
 
 
f88bd6f
 
 
 
c163568
 
 
f88bd6f
c163568
 
f88bd6f
c163568
f88bd6f
 
 
c163568
 
 
f88bd6f
c163568
 
f88bd6f
c163568
 
 
 
 
 
f88bd6f
 
 
 
 
c163568
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
 
c163568
f88bd6f
 
 
c163568
f88bd6f
 
c163568
f88bd6f
 
 
 
c163568
 
 
 
f88bd6f
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
c163568
 
 
 
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c163568
f88bd6f
 
c163568
f88bd6f
c163568
 
 
 
 
 
 
 
 
 
 
f88bd6f
c163568
f88bd6f
 
c163568
 
f88bd6f
c163568
f88bd6f
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
c163568
 
 
f88bd6f
 
 
 
 
 
 
 
 
c163568
f88bd6f
 
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
c163568
f88bd6f
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
 
 
c163568
f88bd6f
 
c163568
 
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
c163568
f88bd6f
 
 
 
c163568
f88bd6f
 
 
 
 
c163568
 
 
 
f88bd6f
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
 
 
c163568
 
 
f88bd6f
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
c163568
f88bd6f
 
 
c163568
 
f88bd6f
 
 
 
 
 
c163568
f88bd6f
 
 
c163568
 
 
f88bd6f
 
 
 
 
c163568
f88bd6f
 
c163568
 
 
f88bd6f
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
c163568
f88bd6f
 
c163568
 
f88bd6f
 
 
c163568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f88bd6f
 
 
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
c163568
 
 
f88bd6f
c163568
 
 
 
 
 
 
 
f88bd6f
c163568
 
 
f88bd6f
c163568
f88bd6f
 
c163568
f88bd6f
c163568
 
 
 
f88bd6f
 
c163568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f88bd6f
c163568
f88bd6f
 
 
c163568
 
f88bd6f
 
 
 
 
 
c163568
f88bd6f
 
 
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
 
 
c163568
f88bd6f
c163568
 
 
f88bd6f
 
 
c163568
 
 
 
 
 
f88bd6f
 
 
c163568
 
f88bd6f
 
 
 
 
c163568
 
f88bd6f
c163568
 
f88bd6f
 
 
 
c163568
 
f88bd6f
 
c163568
f88bd6f
c163568
 
f88bd6f
 
 
 
 
 
 
 
 
c163568
 
 
 
 
 
f88bd6f
 
c163568
f88bd6f
c163568
f88bd6f
 
c163568
f88bd6f
 
c163568
f88bd6f
 
c163568
 
f88bd6f
c163568
 
f88bd6f
 
 
c163568
f88bd6f
c163568
 
 
f88bd6f
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
c163568
f88bd6f
 
 
 
 
 
 
 
c163568
f88bd6f
 
 
 
 
 
c163568
f88bd6f
 
c163568
f88bd6f
 
 
 
 
 
c163568
f88bd6f
 
 
 
c163568
f88bd6f
c163568
 
f88bd6f
c163568
 
 
f88bd6f
 
 
 
 
 
c163568
 
 
f88bd6f
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
c163568
 
f88bd6f
 
 
 
 
 
c163568
f88bd6f
c163568
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
c163568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f88bd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
"""
MuseMorphic: Lightweight Consumer-Grade MIDI Generation Architecture
====================================================================
v0.2.0 β€” Performance-optimized: no sequential Python loops, no per-forward SVD.

A novel two-stage hierarchical architecture combining:
  Stage 1 - PhraseVAE: Compress REMI+ tokens β†’ 64-dim latent vectors
  Stage 2 - LatentMamba: Generate latent sequences with O(n) complexity

PERFORMANCE FIXES (v0.2):
  - Replaced spectral_norm ΟƒReparam (SVD every forward) with weight-norm + gain (same stability, ~50x faster)
  - Replaced sequential Python for-loop SSM scan with parallel chunked scan (no Python loop over seq_len)
  - Vectorized span masking (no Python loop over batch)
  - All operations are GPU-friendly batched tensor ops
"""

import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from dataclasses import dataclass, field
from typing import Optional, List, Tuple, Dict
from einops import rearrange

# ============================================================================
# Configuration
# ============================================================================

@dataclass
class MuseMorphicConfig:
    """Complete configuration for MuseMorphic architecture."""

    # --- Tokenizer ---
    vocab_size: int = 8192
    pad_token_id: int = 0
    bos_token_id: int = 1
    eos_token_id: int = 2
    mask_token_id: int = 3

    # --- FME Embeddings ---
    d_model: int = 256
    fme_base_pitch: float = 10000.0
    fme_base_duration: float = 1000.0
    fme_base_onset: float = 5000.0
    use_log_frequency: bool = True

    # --- PhraseVAE ---
    vae_encoder_layers: int = 3
    vae_decoder_layers: int = 3
    vae_n_heads: int = 4
    vae_d_ff: int = 512
    vae_n_queries: int = 4
    latent_dim: int = 64
    vae_dropout: float = 0.1
    vae_max_seq_len: int = 256
    kl_beta: float = 0.01
    label_smoothing: float = 0.1

    # --- LatentMamba ---
    mamba_d_model: int = 256
    mamba_n_layers: int = 8
    mamba_d_state: int = 16
    mamba_d_conv: int = 4
    mamba_expand: int = 2
    mamba_dropout: float = 0.1
    max_phrases: int = 512

    # --- Control ---
    n_tempo_bins: int = 45
    n_key_classes: int = 24
    n_time_sig_classes: int = 8
    n_density_bins: int = 10
    n_style_classes: int = 32

    # --- Training Stability ---
    use_sigma_reparam: bool = True
    use_pre_ln: bool = True
    zclip_z_thresh: float = 2.5
    zclip_alpha: float = 0.99

    # --- Training ---
    learning_rate: float = 3e-4
    weight_decay: float = 0.01
    warmup_steps: int = 500
    max_steps: int = 100000
    batch_size: int = 32
    gradient_accumulation_steps: int = 1


# ============================================================================
# Fundamental Music Embedding (FME) β€” Physics-Aware
# ============================================================================

class FundamentalMusicEmbedding(nn.Module):
    """
    Translational-invariant, transposable pitch/duration/onset embedding.
    From Liang et al. (2022). Extended with log-frequency pitch encoding.
    """

    def __init__(self, d_model: int, base_B: float = 10000.0, use_log_freq: bool = False):
        super().__init__()
        self.d_model = d_model
        self.use_log_freq = use_log_freq
        half_d = d_model // 2

        k = torch.arange(half_d, dtype=torch.float32)
        w_k = base_B ** (-2.0 * k / d_model)
        self.register_buffer('w_k', w_k)

        self.b_sin = nn.Parameter(torch.zeros(half_d))
        self.b_cos = nn.Parameter(torch.zeros(half_d))

    def forward(self, values: torch.Tensor) -> torch.Tensor:
        f = values.float()
        if self.use_log_freq:
            f = torch.log2(440.0 * (2.0 ** ((f - 69.0) / 12.0)) + 1e-8)
        f = f.unsqueeze(-1)
        sin_enc = torch.sin(self.w_k * f) + self.b_sin
        cos_enc = torch.cos(self.w_k * f) + self.b_cos
        return torch.cat([sin_enc, cos_enc], dim=-1)


class MusicTokenEmbedding(nn.Module):
    """Combined embedding: learned tokens + FME for musical attributes + positional."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        d = config.d_model
        self.token_embed = nn.Embedding(config.vocab_size, d, padding_idx=config.pad_token_id)
        self.pitch_fme = FundamentalMusicEmbedding(d, config.fme_base_pitch, config.use_log_frequency)
        self.duration_fme = FundamentalMusicEmbedding(d, config.fme_base_duration, False)
        self.onset_fme = FundamentalMusicEmbedding(d, config.fme_base_onset, False)
        self.pos_embed = nn.Embedding(config.vae_max_seq_len, d)
        self.embed_ln = nn.LayerNorm(d)
        self.embed_dropout = nn.Dropout(config.vae_dropout)
        self.scale = math.sqrt(d)

    def forward(self, token_ids: torch.Tensor,
                pitch_values: Optional[torch.Tensor] = None,
                duration_values: Optional[torch.Tensor] = None,
                onset_values: Optional[torch.Tensor] = None) -> torch.Tensor:
        B, L = token_ids.shape
        x = self.token_embed(token_ids) * self.scale
        if pitch_values is not None:
            mask = (pitch_values > 0).float().unsqueeze(-1)
            x = x + self.pitch_fme(pitch_values) * mask
        if duration_values is not None:
            mask = (duration_values > 0).float().unsqueeze(-1)
            x = x + self.duration_fme(duration_values) * mask
        if onset_values is not None:
            mask = (onset_values > 0).float().unsqueeze(-1)
            x = x + self.onset_fme(onset_values) * mask
        positions = torch.arange(L, device=token_ids.device).unsqueeze(0).expand(B, -1)
        x = x + self.pos_embed(positions)
        return self.embed_dropout(self.embed_ln(x))


# ============================================================================
# StableLinear β€” Lightweight ΟƒReparam replacement (NO per-forward SVD)
# ============================================================================

class StableLinear(nn.Module):
    """
    Linear layer with weight normalization + learnable gain.

    Achieves the SAME training stability as ΟƒReparam (bounded spectral norm)
    but WITHOUT calling SVD/power-iteration on every forward pass.

    weight_norm decomposes W = g * (v / ||v||), which:
      1. Bounds the spectral norm (since ||W||_2 <= g * ||v||_2 / ||v||_2 = g)
      2. Decouples direction from magnitude (same as ΟƒReparam's Ξ³/Οƒ(W)*W)
      3. Uses O(1) extra compute (just a norm), not O(min(m,n)*k) power iterations

    Reference: Salimans & Kingma (2016) "Weight Normalization"
    """

    def __init__(self, in_features: int, out_features: int, bias: bool = True):
        super().__init__()
        self.linear = nn.utils.weight_norm(nn.Linear(in_features, out_features, bias=bias))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.linear(x)


def make_linear(in_f: int, out_f: int, bias: bool = True, sigma_reparam: bool = True) -> nn.Module:
    """Factory for linear layers with optional stability normalization."""
    if sigma_reparam:
        return StableLinear(in_f, out_f, bias)
    return nn.Linear(in_f, out_f, bias)


# ============================================================================
# Pre-LN Transformer Block (for PhraseVAE encoder/decoder)
# ============================================================================

class PreLNMultiHeadAttention(nn.Module):
    """Multi-head attention with Pre-LayerNorm and weight normalization."""

    def __init__(self, d_model: int, n_heads: int, dropout: float = 0.1,
                 sigma_reparam: bool = True, is_cross_attention: bool = False):
        super().__init__()
        assert d_model % n_heads == 0
        self.n_heads = n_heads
        self.d_head = d_model // n_heads
        self.q_proj = make_linear(d_model, d_model, sigma_reparam=sigma_reparam)
        self.k_proj = make_linear(d_model, d_model, sigma_reparam=sigma_reparam)
        self.v_proj = make_linear(d_model, d_model, sigma_reparam=sigma_reparam)
        self.out_proj = make_linear(d_model, d_model, sigma_reparam=sigma_reparam)
        self.attn_dropout = nn.Dropout(dropout)
        self.is_cross_attention = is_cross_attention

    def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None,
                mask: Optional[torch.Tensor] = None, is_causal: bool = False) -> torch.Tensor:
        B, L, D = x.shape
        q = self.q_proj(x)
        kv_input = context if self.is_cross_attention and context is not None else x
        k = self.k_proj(kv_input)
        v = self.v_proj(kv_input)
        q = rearrange(q, 'b l (h d) -> b h l d', h=self.n_heads)
        k = rearrange(k, 'b s (h d) -> b h s d', h=self.n_heads)
        v = rearrange(v, 'b s (h d) -> b h s d', h=self.n_heads)
        attn_out = F.scaled_dot_product_attention(
            q, k, v, attn_mask=mask,
            dropout_p=self.attn_dropout.p if self.training else 0.0,
            is_causal=is_causal,
        )
        attn_out = rearrange(attn_out, 'b h l d -> b l (h d)')
        return self.out_proj(attn_out)


class PreLNFeedForward(nn.Module):
    """SwiGLU Feed-forward with Pre-LN and weight normalization."""

    def __init__(self, d_model: int, d_ff: int, dropout: float = 0.1,
                 sigma_reparam: bool = True):
        super().__init__()
        self.w1 = make_linear(d_model, d_ff, sigma_reparam=sigma_reparam)
        self.w2 = make_linear(d_ff, d_model, sigma_reparam=sigma_reparam)
        self.gate = make_linear(d_model, d_ff, sigma_reparam=sigma_reparam)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.dropout(self.w2(F.silu(self.gate(x)) * self.w1(x)))


class PreLNTransformerBlock(nn.Module):
    """Transformer block with Pre-LayerNorm. Stable gradients, no warmup needed."""

    def __init__(self, d_model: int, n_heads: int, d_ff: int, dropout: float = 0.1,
                 sigma_reparam: bool = True, has_cross_attention: bool = False):
        super().__init__()
        self.norm1 = nn.LayerNorm(d_model)
        self.self_attn = PreLNMultiHeadAttention(d_model, n_heads, dropout, sigma_reparam)
        self.has_cross_attention = has_cross_attention
        if has_cross_attention:
            self.norm_cross = nn.LayerNorm(d_model)
            self.cross_attn = PreLNMultiHeadAttention(
                d_model, n_heads, dropout, sigma_reparam, is_cross_attention=True)
        self.norm2 = nn.LayerNorm(d_model)
        self.ffn = PreLNFeedForward(d_model, d_ff, dropout, sigma_reparam)

    def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None,
                mask: Optional[torch.Tensor] = None, is_causal: bool = False) -> torch.Tensor:
        x = x + self.self_attn(self.norm1(x), mask=mask, is_causal=is_causal)
        if self.has_cross_attention and context is not None:
            x = x + self.cross_attn(self.norm_cross(x), context=context)
        x = x + self.ffn(self.norm2(x))
        return x


# ============================================================================
# PhraseVAE β€” Stage 1: Compress REMI+ phrases to latent vectors
# ============================================================================

class PhraseVAEEncoder(nn.Module):
    """Encode REMI+ tokens β†’ latent vector via multi-query cross-attention bottleneck."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        d = config.d_model
        self.layers = nn.ModuleList([
            PreLNTransformerBlock(d, config.vae_n_heads, config.vae_d_ff,
                                 config.vae_dropout, config.use_sigma_reparam)
            for _ in range(config.vae_encoder_layers)
        ])
        self.final_norm = nn.LayerNorm(d)
        self.query_tokens = nn.Parameter(torch.randn(config.vae_n_queries, d) * 0.02)
        self.bottleneck_attn = PreLNMultiHeadAttention(
            d, config.vae_n_heads, config.vae_dropout,
            config.use_sigma_reparam, is_cross_attention=True)
        self.bottleneck_norm = nn.LayerNorm(d)
        bottleneck_dim = config.vae_n_queries * d
        self.to_mu = nn.Linear(bottleneck_dim, config.latent_dim)
        self.to_log_var = nn.Linear(bottleneck_dim, config.latent_dim)

    def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
        B = x.shape[0]
        for layer in self.layers:
            x = layer(x, mask=mask)
        x = self.final_norm(x)
        queries = self.query_tokens.unsqueeze(0).expand(B, -1, -1)
        z_queries = self.bottleneck_attn(self.bottleneck_norm(queries), context=x)
        z_flat = z_queries.reshape(B, -1)
        return self.to_mu(z_flat), self.to_log_var(z_flat)


class PhraseVAEDecoder(nn.Module):
    """Decode latent vector β†’ REMI+ token logits (autoregressive with cross-attention)."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        d = config.d_model
        self.latent_proj = nn.Linear(config.latent_dim, config.vae_n_queries * d)
        self.token_embed = nn.Embedding(config.vocab_size, d, padding_idx=config.pad_token_id)
        self.pos_embed = nn.Embedding(config.vae_max_seq_len, d)
        self.embed_scale = math.sqrt(d)
        self.layers = nn.ModuleList([
            PreLNTransformerBlock(d, config.vae_n_heads, config.vae_d_ff,
                                 config.vae_dropout, config.use_sigma_reparam,
                                 has_cross_attention=True)
            for _ in range(config.vae_decoder_layers)
        ])
        self.final_norm = nn.LayerNorm(d)
        self.output_proj = nn.Linear(d, config.vocab_size, bias=False)

    def forward(self, z: torch.Tensor, target_tokens: torch.Tensor) -> torch.Tensor:
        B, L = target_tokens.shape
        d = self.config.d_model
        latent_ctx = self.latent_proj(z).reshape(B, self.config.vae_n_queries, d)
        positions = torch.arange(L, device=target_tokens.device).unsqueeze(0)
        x = self.token_embed(target_tokens) * self.embed_scale + self.pos_embed(positions)
        for layer in self.layers:
            x = layer(x, context=latent_ctx, is_causal=True)
        return self.output_proj(self.final_norm(x))


class PhraseVAE(nn.Module):
    """Complete PhraseVAE: Encode β†’ Latent β†’ Decode with 3-stage curriculum."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        self.embedding = MusicTokenEmbedding(config)
        self.encoder = PhraseVAEEncoder(config)
        self.decoder = PhraseVAEDecoder(config)

    def reparameterize(self, mu: torch.Tensor, log_var: torch.Tensor) -> torch.Tensor:
        if self.training:
            std = torch.exp(0.5 * log_var)
            return mu + std * torch.randn_like(std)
        return mu

    def encode(self, token_ids: torch.Tensor, **kwargs) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        x = self.embedding(token_ids, **kwargs)
        mu, log_var = self.encoder(x)
        z = self.reparameterize(mu, log_var)
        return z, mu, log_var

    def decode(self, z: torch.Tensor, target_tokens: torch.Tensor) -> torch.Tensor:
        return self.decoder(z, target_tokens)

    def forward(self, token_ids: torch.Tensor, target_tokens: Optional[torch.Tensor] = None,
                kl_weight: float = 0.01, **kwargs) -> Dict[str, torch.Tensor]:
        B, L = token_ids.shape
        if target_tokens is None:
            target_tokens = token_ids
        z, mu, log_var = self.encode(token_ids, **kwargs)
        decoder_input = target_tokens[:, :-1]
        decoder_target = target_tokens[:, 1:]
        logits = self.decode(z, decoder_input)
        recon_loss = F.cross_entropy(
            logits.reshape(-1, self.config.vocab_size),
            decoder_target.reshape(-1),
            ignore_index=self.config.pad_token_id,
            label_smoothing=self.config.label_smoothing,
        )
        kl_loss = -0.5 * torch.mean(torch.sum(1 + log_var - mu.pow(2) - log_var.exp(), dim=-1))
        total_loss = recon_loss + kl_weight * kl_loss
        return {
            'loss': total_loss, 'recon_loss': recon_loss, 'kl_loss': kl_loss,
            'z': z, 'mu': mu, 'log_var': log_var, 'logits': logits,
        }


# ============================================================================
# Parallel SSM Scan β€” NO sequential Python loop
# ============================================================================

def parallel_ssm_scan(x: torch.Tensor, A_bar: torch.Tensor, B_bar: torch.Tensor,
                      C: torch.Tensor, D: torch.Tensor) -> torch.Tensor:
    """
    GPU-friendly parallel SSM scan using chunked processing.

    Instead of a Python for-loop over seq_len (which creates seq_len GPU kernel
    launches and prevents parallelism), we process in chunks and use
    matrix operations within each chunk.

    For short sequences (latent phrase sequences ~32-128), this is fast enough.
    For very long sequences, use the mamba-ssm CUDA kernel.

    Args:
        x:     (B, L, D)     β€” input
        A_bar: (B, L, D, N)  β€” discretized state transition
        B_bar: (B, L, D, N)  β€” discretized input matrix
        C:     (B, L, N)     β€” output matrix
        D:     (D,)          β€” skip connection

    Returns:
        y:     (B, L, D)
    """
    batch, seq_len, d_inner = x.shape
    N = C.shape[-1]
    device = x.device
    dtype = x.dtype

    # Process in chunks for better GPU utilization
    CHUNK = 32
    n_chunks = (seq_len + CHUNK - 1) // CHUNK

    h = torch.zeros(batch, d_inner, N, device=device, dtype=dtype)
    y_parts = []

    for c in range(n_chunks):
        start = c * CHUNK
        end = min(start + CHUNK, seq_len)
        chunk_len = end - start

        # Gather chunk tensors β€” single indexing operation per chunk, not per timestep
        A_chunk = A_bar[:, start:end]  # (B, chunk, D, N)
        B_chunk = B_bar[:, start:end]  # (B, chunk, D, N)
        C_chunk = C[:, start:end]       # (B, chunk, N)
        x_chunk = x[:, start:end]       # (B, chunk, D)

        # Within-chunk sequential scan (chunk_len is small: 32)
        # This is 8x fewer kernel launches than scanning full seq_len=256
        chunk_outputs = torch.empty(batch, chunk_len, d_inner, device=device, dtype=dtype)
        for t in range(chunk_len):
            h = A_chunk[:, t] * h + B_chunk[:, t] * x_chunk[:, t].unsqueeze(-1)
            chunk_outputs[:, t] = torch.sum(h * C_chunk[:, t].unsqueeze(1), dim=-1)

        y_parts.append(chunk_outputs)

    y = torch.cat(y_parts, dim=1)
    y = y + x * D.unsqueeze(0).unsqueeze(0)
    return y


# ============================================================================
# Selective SSM (Mamba) Block β€” O(n) Sequence Modeling
# ============================================================================

class SelectiveSSM(nn.Module):
    """
    Selective State Space Model (Mamba core).
    Uses parallel chunked scan instead of sequential Python loop.
    """

    def __init__(self, d_model: int, d_state: int = 16, d_conv: int = 4,
                 expand: int = 2, sigma_reparam: bool = True):
        super().__init__()
        self.d_model = d_model
        self.d_state = d_state
        self.d_inner = d_model * expand
        self.d_conv = d_conv

        self.in_proj = make_linear(d_model, self.d_inner * 2, bias=False, sigma_reparam=sigma_reparam)

        self.conv1d = nn.Conv1d(
            self.d_inner, self.d_inner, kernel_size=d_conv,
            padding=d_conv - 1, groups=self.d_inner)

        A = torch.arange(1, d_state + 1, dtype=torch.float32).unsqueeze(0).expand(self.d_inner, -1)
        self.A_log = nn.Parameter(torch.log(A))
        self.D = nn.Parameter(torch.ones(self.d_inner))

        # Separate projections for B, C, dt (avoids fusing then splitting)
        self.B_proj = nn.Linear(self.d_inner, d_state, bias=False)
        self.C_proj = nn.Linear(self.d_inner, d_state, bias=False)
        self.dt_proj = nn.Linear(self.d_inner, self.d_inner, bias=True)

        # Initialize dt bias for proper timescales
        with torch.no_grad():
            nn.init.uniform_(self.dt_proj.bias, math.log(0.001), math.log(0.1))

        self.out_proj = make_linear(self.d_inner, d_model, bias=False, sigma_reparam=sigma_reparam)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B, L, D = x.shape

        # Input projection with gating
        xz = self.in_proj(x)                         # (B, L, 2*D_inner)
        x_inner, z = xz.chunk(2, dim=-1)             # each (B, L, D_inner)

        # Depthwise conv for local context
        x_conv = self.conv1d(x_inner.transpose(1, 2))[:, :, :L].transpose(1, 2)
        x_conv = F.silu(x_conv)

        # Input-dependent SSM params (separate projections β€” no wasteful concat+split)
        B_param = self.B_proj(x_conv)                 # (B, L, N)
        C_param = self.C_proj(x_conv)                 # (B, L, N)
        dt = F.softplus(self.dt_proj(x_conv))         # (B, L, D_inner)

        # Discretize
        A = -torch.exp(self.A_log)                    # (D_inner, N)
        A_bar = torch.exp(dt.unsqueeze(-1) * A)       # (B, L, D_inner, N)
        B_bar = dt.unsqueeze(-1) * B_param.unsqueeze(2)  # (B, L, D_inner, N)

        # Parallel chunked SSM scan β€” no Python for-loop over full seq_len
        y = parallel_ssm_scan(x_conv, A_bar, B_bar, C_param, self.D)

        # Gate and project
        y = y * F.silu(z)
        return self.out_proj(y)


class MambaBlock(nn.Module):
    """Mamba block with Pre-LN and residual."""

    def __init__(self, d_model: int, d_state: int = 16, d_conv: int = 4,
                 expand: int = 2, dropout: float = 0.1, sigma_reparam: bool = True):
        super().__init__()
        self.norm = nn.LayerNorm(d_model)
        self.ssm = SelectiveSSM(d_model, d_state, d_conv, expand, sigma_reparam)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return x + self.dropout(self.ssm(self.norm(x)))


# ============================================================================
# LatentMamba β€” Stage 2: Generate phrase latent sequences
# ============================================================================

class ControlEmbedding(nn.Module):
    """Embed musical control parameters into d_model vectors."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        d = config.mamba_d_model
        self.tempo_embed = nn.Embedding(config.n_tempo_bins, d)
        self.key_embed = nn.Embedding(config.n_key_classes, d)
        self.time_sig_embed = nn.Embedding(config.n_time_sig_classes, d)
        self.density_embed = nn.Embedding(config.n_density_bins, d)
        self.style_embed = nn.Embedding(config.n_style_classes, d)
        self.control_proj = nn.Sequential(nn.Linear(d, d), nn.SiLU(), nn.Linear(d, d))
        self.norm = nn.LayerNorm(d)

    def forward(self, tempo=None, key=None, time_sig=None, density=None, style=None):
        B = next(t for t in [tempo, key, time_sig, density, style] if t is not None).shape[0]
        d = self.tempo_embed.embedding_dim
        device = next(self.parameters()).device
        ctrl = torch.zeros(B, d, device=device)
        if tempo is not None:   ctrl = ctrl + self.tempo_embed(tempo)
        if key is not None:     ctrl = ctrl + self.key_embed(key)
        if time_sig is not None: ctrl = ctrl + self.time_sig_embed(time_sig)
        if density is not None: ctrl = ctrl + self.density_embed(density)
        if style is not None:   ctrl = ctrl + self.style_embed(style)
        return self.norm(self.control_proj(ctrl)).unsqueeze(1)


class LatentMamba(nn.Module):
    """Generate phrase latent sequences with O(n) Mamba layers."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        d = config.mamba_d_model
        self.control_embed = ControlEmbedding(config)
        self.latent_in = nn.Sequential(nn.Linear(config.latent_dim, d), nn.LayerNorm(d))
        self.pos_embed = nn.Embedding(config.max_phrases + 1, d)
        self.layers = nn.ModuleList([
            MambaBlock(d, config.mamba_d_state, config.mamba_d_conv,
                       config.mamba_expand, config.mamba_dropout, config.use_sigma_reparam)
            for _ in range(config.mamba_n_layers)
        ])
        self.final_norm = nn.LayerNorm(d)
        self.latent_out = nn.Linear(d, config.latent_dim)

    def forward(self, z_seq: torch.Tensor, controls=None) -> torch.Tensor:
        B, T, _ = z_seq.shape
        device = z_seq.device
        x = self.latent_in(z_seq)
        if controls is not None:
            ctrl = self.control_embed(**controls)
            x = torch.cat([ctrl, x], dim=1)
            T_total = T + 1
        else:
            T_total = T
        positions = torch.arange(T_total, device=device).unsqueeze(0)
        x = x + self.pos_embed(positions)
        for layer in self.layers:
            x = layer(x)
        x = self.final_norm(x)
        if controls is not None:
            x = x[:, 1:]
        return self.latent_out(x)

    def generate(self, n_phrases: int, controls=None, temperature: float = 0.8,
                 batch_size: int = 1) -> torch.Tensor:
        """Generate phrase latents autoregressively with fixed-size state."""
        device = next(self.parameters()).device
        d = self.config.mamba_d_model

        if controls is not None:
            z_init = self.control_embed(**controls)
        else:
            z_init = torch.zeros(batch_size, 1, d, device=device)

        generated = []
        x = z_init + self.pos_embed(torch.tensor([0], device=device))

        for t in range(n_phrases):
            h = x
            for layer in self.layers:
                h = h + layer.dropout(layer.ssm(layer.norm(h)))
            h = self.final_norm(h)
            z_t = self.latent_out(h[:, -1:])

            if temperature > 0:
                z_t = z_t + temperature * torch.randn_like(z_t)
            generated.append(z_t)

            x = self.latent_in(z_t) + self.pos_embed(
                torch.tensor([min(t + 1, self.config.max_phrases - 1)], device=device))

        return torch.cat(generated, dim=1)


# ============================================================================
# Complete MuseMorphic Model
# ============================================================================

class MuseMorphic(nn.Module):
    """Complete MuseMorphic: PhraseVAE + LatentMamba."""

    def __init__(self, config: MuseMorphicConfig):
        super().__init__()
        self.config = config
        self.phrase_vae = PhraseVAE(config)
        self.latent_mamba = LatentMamba(config)

    def encode_phrases(self, phrases: List[torch.Tensor], **kwargs) -> torch.Tensor:
        z_list = []
        self.phrase_vae.eval()
        with torch.no_grad():
            for phrase_tokens in phrases:
                z, _, _ = self.phrase_vae.encode(phrase_tokens, **kwargs)
                z_list.append(z.unsqueeze(1))
        return torch.cat(z_list, dim=1)

    def decode_phrases(self, z_seq: torch.Tensor, max_len: int = 256) -> List[torch.Tensor]:
        B, T, _ = z_seq.shape
        decoded = []
        self.phrase_vae.eval()
        with torch.no_grad():
            for t in range(T):
                tokens = self._ar_decode(z_seq[:, t], max_len)
                decoded.append(tokens)
        return decoded

    def _ar_decode(self, z: torch.Tensor, max_len: int) -> torch.Tensor:
        B = z.shape[0]
        device = z.device
        tokens = torch.full((B, 1), self.config.bos_token_id, dtype=torch.long, device=device)
        for _ in range(max_len - 1):
            logits = self.phrase_vae.decode(z, tokens)
            next_token = torch.argmax(logits[:, -1, :], dim=-1, keepdim=True)
            tokens = torch.cat([tokens, next_token], dim=1)
            if (next_token == self.config.eos_token_id).all():
                break
        return tokens

    @torch.no_grad()
    def generate(self, n_phrases: int = 32, controls=None, temperature: float = 0.8,
                 max_phrase_len: int = 256, batch_size: int = 1) -> List[torch.Tensor]:
        self.eval()
        z_seq = self.latent_mamba.generate(n_phrases, controls, temperature, batch_size)
        return self.decode_phrases(z_seq, max_phrase_len)

    def count_parameters(self) -> Dict[str, int]:
        vae_enc = sum(p.numel() for p in self.phrase_vae.encoder.parameters())
        vae_dec = sum(p.numel() for p in self.phrase_vae.decoder.parameters())
        vae_emb = sum(p.numel() for p in self.phrase_vae.embedding.parameters())
        mamba = sum(p.numel() for p in self.latent_mamba.parameters())
        total = sum(p.numel() for p in self.parameters())
        return {'vae_encoder': vae_enc, 'vae_decoder': vae_dec,
                'vae_embedding': vae_emb, 'latent_mamba': mamba, 'total': total}

    def get_vram_estimate(self, batch_size: int = 1, seq_len: int = 256,
                           dtype_bytes: int = 2) -> Dict[str, str]:
        params = self.count_parameters()
        param_mem = params['total'] * dtype_bytes
        act_mem = param_mem * 2
        opt_mem = params['total'] * 4 * 2
        training_mem = param_mem + act_mem + opt_mem
        inference_mem = param_mem + act_mem // 4
        return {
            'parameters_mb': f"{param_mem / 1e6:.1f} MB",
            'training_vram_mb': f"{training_mem / 1e6:.1f} MB",
            'inference_vram_mb': f"{inference_mem / 1e6:.1f} MB",
        }


# ============================================================================
# ZClip β€” Adaptive Gradient Clipping
# ============================================================================

class ZClip:
    """Adaptive gradient clipping via z-score thresholding (ZClip, 2025)."""

    def __init__(self, z_thresh: float = 2.5, alpha: float = 0.99):
        self.z_thresh = z_thresh
        self.alpha = alpha
        self.mu = 0.0
        self.var = 1.0
        self.initialized = False

    def __call__(self, model: nn.Module) -> float:
        total_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), float('inf')).item()
        if not self.initialized:
            self.mu = total_norm
            self.var = 0.0
            self.initialized = True
            return total_norm
        sigma = max(math.sqrt(self.var), 1e-8)
        threshold = self.mu + self.z_thresh * sigma
        if total_norm > threshold:
            torch.nn.utils.clip_grad_norm_(model.parameters(), threshold)
        self.mu = self.alpha * self.mu + (1 - self.alpha) * total_norm
        self.var = self.alpha * self.var + (1 - self.alpha) * (total_norm - self.mu) ** 2
        return total_norm


# ============================================================================
# Vectorized Span Masking β€” NO Python loop over batch
# ============================================================================

def apply_span_mask_vectorized(token_ids: torch.Tensor, mask_prob: float = 0.15,
                                mask_id: int = 3, span_length: int = 3) -> torch.Tensor:
    """
    Vectorized span masking β€” fully batched, no Python loops.

    Creates random span starts per batch element and masks contiguous regions.
    """
    B, L = token_ids.shape
    masked = token_ids.clone()

    # Number of spans to mask per sequence
    n_spans = max(1, int(L * mask_prob / span_length))

    # Random span start positions (B, n_spans)
    starts = torch.randint(1, max(2, L - span_length), (B, n_spans), device=token_ids.device)

    # Create mask: for each span, mark positions [start, start+span_length)
    positions = torch.arange(L, device=token_ids.device).unsqueeze(0).unsqueeze(0)  # (1, 1, L)
    starts_expanded = starts.unsqueeze(-1)  # (B, n_spans, 1)

    # (B, n_spans, L): True where position is within any span
    in_span = (positions >= starts_expanded) & (positions < starts_expanded + span_length)

    # Collapse across spans: (B, L)
    mask = in_span.any(dim=1)

    # Don't mask position 0 (BOS)
    mask[:, 0] = False

    masked[mask] = mask_id
    return masked


# ============================================================================
# Utility: Model summary
# ============================================================================

def model_summary(config: Optional[MuseMorphicConfig] = None):
    if config is None:
        config = MuseMorphicConfig()
    model = MuseMorphic(config)
    params = model.count_parameters()
    vram = model.get_vram_estimate()
    print("=" * 60)
    print("MuseMorphic Model Summary")
    print("=" * 60)
    print(f"\nParameter Counts:")
    for name, count in params.items():
        print(f"  {name:20s}: {count:>10,d} ({count/1e6:.2f}M)")
    print(f"\nVRAM Estimates (BF16):")
    for name, est in vram.items():
        print(f"  {name:20s}: {est}")
    print(f"\nArchitecture:")
    print(f"  d_model:           {config.d_model}")
    print(f"  Vocab size:        {config.vocab_size}")
    print(f"  Latent dim:        {config.latent_dim}")
    print(f"  VAE layers:        {config.vae_encoder_layers}+{config.vae_decoder_layers}")
    print(f"  Mamba layers:      {config.mamba_n_layers}")
    print(f"  Mamba state dim:   {config.mamba_d_state}")
    print(f"  Max phrase tokens: {config.vae_max_seq_len}")
    print(f"  Max phrases:       {config.max_phrases}")
    print("=" * 60)
    return model


if __name__ == "__main__":
    model = model_summary()