Downloads · 30 days
0
asdf98/MuseMorphic
MuseMorphic is a machine learning model from asdf98. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Downloads · 30 days
0
Access
Public
Updated Apr 27, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.py95 KB · 76%
From the Hugging Face model README
Current MIDI generation models suffer from fundamental limitations:
| Problem | Cause | Examples |
|---|---|---|
| Quadratic memory scaling | Full self-attention O(L²) | Music Transformer, MusicGPT |
| Loss of coherence over long sequences | Attention dilution, no structural memory | All AR transformer models |
| Uncontrollable generation | No explicit control interface | Most open-source models |
| Consumer-unfriendly | >8GB VRAM, slow inference | MuseNet, MusicGen |
| Training instability | Post-LN, FP16 overflow, no gradient control | Common in research code |
| No music theory awareness | Absolute pitch encoding, no harmonic inductive bias | Most models |
MuseMorphic is a two-stage hierarchical architecture combining:
| Component | Parameters | VRAM (BF16 training) | VRAM (Inference) |
|---|---|---|---|
| PhraseVAE Encoder | ~8M | ~400MB | ~200MB |
| PhraseVAE Decoder | ~10M | ~500MB | ~300MB |
| LatentMamba | ~12M | ~600MB | ~300MB |
| Embeddings + Heads | ~3M | ~150MB | ~100MB |
| Total | ~33M | ~1.7GB | ~0.9GB |
✅ Trains on free Colab T4 (16GB) with large batches ✅ Inference under 2GB VRAM easily
Following MIDI-RWKV (2025), we use REMI+ encoding with BPE compression:
Raw MIDI → REMI+ tokens → BPE (vocab=8192) → Integer sequence
REMI+ vocabulary structure:
[BAR] — Bar boundary markers[POS_0..POS_15] — 16th-note grid positions within bar[PITCH_0..PITCH_127] — MIDI pitch values[VEL_1..VEL_32] — Velocity bins (32 levels)[DUR_1..DUR_16] — Duration bins (16th note to whole note)[TEMPO_30..TEMPO_210] — Tempo bins (4 BPM resolution)[TIMESIG_*] — Time signature tokens[TRACK_START], [TRACK_END] — Track delimiters[PROGRAM_0..PROGRAM_127] — GM instrument programsFrom Liang et al. (2022), we use physics-aware embeddings that respect musical intervals:
$$\text{FME}(f) = \bigoplus_{k=0}^{d/2-1} \left[\sin(w_k f) + b_{\sin,k},\ \cos(w_k f) + b_{\cos,k}\right]$$
where $w_k = B^{-2k/d}$, $B$ is the base (different for pitch/duration/onset).
Key mathematical properties:
We extend FME by encoding pitch as log-frequency instead of MIDI integer: $$f_{hz} = 440 \cdot 2^{(p - 69)/12}$$
This makes the embedding space respect the physical harmonic series.
Compresses one bar of one track (a "phrase") from REMI+ tokens into a 64-dim latent vector.
Encoder (3-layer Pre-LN Transformer with σReparam): $$h = \text{TransformerEncoder}(\text{FME}(x) + \text{PosEnc}(x))$$
Multi-Query Bottleneck (from PhraseVAE, 2024): $$q_1..q_m = \text{LearnedQueries}(m=4)$$ $$z_{queries} = \text{MultiHeadCrossAttention}(Q=q, K=h, V=h)$$ $$z_{flat} = \text{Flatten}(z_{queries})$$ $$\mu, \log\sigma^2 = \text{Linear}(z_{flat})$$ $$z = \mu + \sigma \cdot \epsilon, \quad \epsilon \sim \mathcal{N}(0, I)$$
Decoder (3-layer Pre-LN Transformer, autoregressive): $$p(x|z) = \prod_t p(x_t | x_{<t}, z)$$
Training Loss: $$\mathcal{L}{VAE} = \mathcal{L}{recon} + \beta \cdot D_{KL}(q(z|x) | p(z))$$
where $\beta = 0.01$ (following PhraseVAE's finding that low β prevents KL domination).
Three-stage training (curriculum):
Generates sequences of phrase latent vectors using Selective State Space Model.
Selective SSM (Mamba) Core:
Given input $x \in \mathbb{R}^{B \times L \times D}$:
$$B(x) = \text{Linear}_N(x), \quad C(x) = \text{Linear}_N(x)$$ $$\Delta(x) = \text{softplus}(\text{Linear}_1(x) + \text{Parameter})$$
Discretization (Zero-Order Hold): $$\bar{A} = \exp(\Delta \cdot A), \quad \bar{B} = (\Delta \cdot A)^{-1}(\bar{A} - I) \cdot \Delta \cdot B(x)$$
Recurrence (parallel scan during training): $$h_t = \bar{A} h_{t-1} + \bar{B} x_t$$ $$y_t = C(x_t) h_t$$
Complexity:
Architecture:
Input: [control_embed, z_phrase_1, z_phrase_2, ..., z_phrase_T]
↓
Linear projection (64 → d_model=256)
↓
MambaBlock × 8 (d_model=256, d_state=16, d_conv=4, expand=2)
↓
Linear projection (256 → 64)
↓
Output: predicted z_phrase_{2..T+1}
Each MambaBlock:
x → Pre-LN → [Linear(expand*D), SiLU, Conv1d] → SSM → × gate → Linear(D) → + residual
Control tokens are projected to d_model and prepended to the latent phrase sequence:
Controls: {tempo_class, key_signature, time_signature, density_level, style_tag}
↓
Control Embeddings: Embed(control_i) for each control
↓
Prepend: [ctrl_1, ctrl_2, ..., ctrl_K, z_1, z_2, ...]
During training, controls are extracted from the actual music. During inference, user specifies controls.
Mamba operates in recurrent mode during inference:
Applied to ALL linear layers: $$\hat{W} = \frac{\gamma}{\sigma(W)} W$$
where σ(W) is the spectral norm (largest singular value), γ is learnable scalar.
Prevents attention entropy collapse — the #1 cause of training instability in music transformers.
μ_t = α · μ_{t-1} + (1 - α) · ||g_t||
σ_t = √(α · σ²_{t-1} + (1-α) · (||g_t|| - μ_t)²)
threshold_t = μ_t + z_thresh · σ_t # z_thresh = 2.5
Clips only genuine gradient spikes, not normal gradients.
All transformer/SSM blocks use Pre-LN:
x → LayerNorm → Sublayer → + residual
Eliminates need for learning rate warmup. Bounded gradient norms analytically.
Same exponent range as FP32 (8-bit). No loss scaling needed. No overflow/underflow.
$$\mathcal{L} = (1-\epsilon) \cdot \text{CE}(p, y) + \epsilon \cdot H(p, u)$$
with ε=0.1. Prevents overconfident pitch predictions that cause mode collapse.
| Feature | Music Transformer | MIDI-RWKV | MusicMamba | MuseMorphic |
|---|---|---|---|---|
| Complexity | O(L²) | O(L) | O(L²) + O(L) | O(L) everywhere |
| VRAM Training | >8GB | ~4GB | ~6GB | ~1.7GB |
| VRAM Inference | >4GB | ~1GB | ~2GB | ~0.9GB |
| Controllable | ✗ | ✓ (attribute) | ✗ | ✓ (multi-attribute) |
| Infinite Gen | ✗ (KV cache grows) | ✓ (recurrent) | ✗ | ✓ (recurrent) |
| Music Theory Aware | Relative attention | REMI+ | Mode-aware | FME + REMI+ + harmonic |
| Training Stable | ✗ (needs tuning) | ✓ | ✓ | ✓ (by design) |
| Model Size | 50-100M+ | 20-50M | ~30M | ~33M |
| Hierarchical | ✗ | ✗ | ✗ | ✓ (phrase latent) |
Apache 2.0