Spaces:
Sleeping
Sleeping
File size: 4,883 Bytes
9c7e38e | 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 | """
Transformer model for multi-instrument grid-based music generation.
"""
from typing import Dict
import torch
import torch.nn as nn
import torch.nn.functional as F
import config
class PositionalEncoding(nn.Module):
"""
Standard sinusoidal positional encoding for Transformer inputs.
Expects input of shape [batch, seq_len, d_model].
"""
def __init__(self, d_model: int, max_len: int = 2048):
super().__init__()
self.d_model = d_model
pe = torch.zeros(max_len, d_model) # [max_len, d_model]
position = torch.arange(0, max_len, dtype=torch.float32).unsqueeze(1)
div_term = torch.exp(
torch.arange(0, d_model, 2, dtype=torch.float32)
* (-torch.log(torch.tensor(10000.0)) / d_model)
)
pe[:, 0::2] = torch.sin(position * div_term) # even indices
pe[:, 1::2] = torch.cos(position * div_term) # odd indices
pe = pe.unsqueeze(0) # [1, max_len, d_model]
self.register_buffer("pe", pe)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Add positional encoding to input.
Args:
x: [batch, seq_len, d_model]
Returns:
[batch, seq_len, d_model]
"""
seq_len = x.size(1)
# pe[:, :seq_len] has shape [1, seq_len, d_model]
return x + self.pe[:, :seq_len]
class MusicTransformer(nn.Module):
"""
Transformer encoder with per-instrument output heads.
Input:
x: [batch, seq_len, INPUT_FEATURES] (multi-hot grid)
Output:
dict of logits:
{
"strings": [batch, seq_len, 12],
"woodwinds":[batch, seq_len, 12],
"brass": [batch, seq_len, 12],
"perc": [batch, seq_len, 3],
}
"""
def __init__(
self,
input_dim: int = config.INPUT_FEATURES,
d_model: int = config.D_MODEL,
n_layers: int = config.N_LAYERS,
n_heads: int = config.N_HEADS,
ffn_dim: int = config.FFN_DIM,
dropout: float = config.DROPOUT,
):
super().__init__()
self.input_dim = input_dim
self.d_model = d_model
# Project 39-dim multi-hot input to d_model
self.input_proj = nn.Linear(input_dim, d_model)
# Positional encoding
self.pos_encoding = PositionalEncoding(d_model=d_model)
# Transformer encoder
encoder_layer = nn.TransformerEncoderLayer(
d_model=d_model,
nhead=n_heads,
dim_feedforward=ffn_dim,
dropout=dropout,
batch_first=True, # [batch, seq, d_model]
)
self.transformer_encoder = nn.TransformerEncoder(
encoder_layer,
num_layers=n_layers,
)
# Per-instrument heads
self.strings_head = nn.Linear(d_model, config.PITCHES_MELODIC) # 12
self.woodwinds_head = nn.Linear(d_model, config.PITCHES_MELODIC) # 12
self.brass_head = nn.Linear(d_model, config.PITCHES_MELODIC) # 12
self.perc_head = nn.Linear(d_model, config.PITCHES_PERC) # 3
def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
"""
Forward pass.
Args:
x: Tensor of shape [batch, seq_len, INPUT_FEATURES]
Returns:
Dict of logits, each [batch, seq_len, pitches]:
{
"strings": logits_S,
"woodwinds": logits_W,
"brass": logits_B,
"perc": logits_P,
}
"""
# Project input features to d_model
h = self.input_proj(x) # [batch, seq_len, d_model]
# Add positional encoding
h = self.pos_encoding(h) # [batch, seq_len, d_model]
# Transformer encoder
# (no src_key_padding_mask here; you can add later if needed)
h = self.transformer_encoder(h) # [batch, seq_len, d_model]
# Per-instrument logits
logits_strings = self.strings_head(h) # [batch, seq_len, 12]
logits_woodwinds = self.woodwinds_head(h)
logits_brass = self.brass_head(h)
logits_perc = self.perc_head(h) # [batch, seq_len, 3]
return {
"strings": logits_strings,
"woodwinds": logits_woodwinds,
"brass": logits_brass,
"perc": logits_perc,
}
def build_model(cfg=config) -> MusicTransformer:
"""
Convenience function to build a MusicTransformer from config values.
Returns:
MusicTransformer instance with parameters from config.py.
"""
model = MusicTransformer(
input_dim=cfg.INPUT_FEATURES,
d_model=cfg.D_MODEL,
n_layers=cfg.N_LAYERS,
n_heads=cfg.N_HEADS,
ffn_dim=cfg.FFN_DIM,
dropout=cfg.DROPOUT,
)
return model |