Spaces:
Sleeping
Sleeping
Download model.py from hephaestusonfire/Music_Generator: direct link, hf CLI and curl.
- Browser
- Download file 4.88 kB
-
https://huggingface.co/spaces/hephaestusonfire/Music_Generator/resolve/main/model.py
- Command line
-
hf download hf://spaces/hephaestusonfire/Music_Generator/model.py
-
curl -L -o model.py https://huggingface.co/spaces/hephaestusonfire/Music_Generator/resolve/main/model.py
4.88 kB
| """ | |
| 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 |