File size: 4,168 Bytes
bbc981d
 
 
 
 
 
 
 
 
 
 
 
 
 
30e9297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
tags:
- music
- music-generation
- midi
- pytorch
- onnx
- custom
datasets:
- drengskapur/midi-classical-music
pipeline_tag: text-generation
---

# Music Generation LLM

A LLaMA-style Transformer model for symbolic music generation, trained on MIDI data.

## Architecture

- **Model**: LLaMA-style Transformer with RoPE, GQA, SwiGLU, RMSNorm
- **Tokenizer**: REMI (REvamped MIDI-derived) β€” SOTA for symbolic music
- **Dataset**: `drengskapur/midi-classical-music` β€” 4,796 classical MIDI files (~50MB)
- **Training**: AdamW + Cosine LR warmup + AMP + Gradient Checkpointing

## Key Features

- **Memory Efficient**: Grouped Query Attention, gradient checkpointing, mixed precision
- **OOM Safe**: Conservative batch sizes, AMP, lazy data loading
- **SOTA Techniques**: RoPE, SwiGLU, RMSNorm, KV-cache, top-p/top-k sampling

## Quick Start

```bash
# Install dependencies
pip install -r requirements.txt

# Train + Generate (default)
python3 -m src.s00_main train+generate

# Train only
python3 -m src.s00_main train --epochs 20 --batch-size 4

# Generate from checkpoint
python3 -m src.s00_main generate --temperature 0.85
```

## Project Structure

```
music_gen_llm/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ s00_main.py          # Entry point β€” orchestrates pipeline
β”‚   β”œβ”€β”€ s01_config.py         # All configuration dataclasses
β”‚   β”œβ”€β”€ s02_tokenizer.py      # REMI MIDI tokenizer
β”‚   β”œβ”€β”€ s03_dataset.py        # Data download + tokenization + DataLoader
β”‚   β”œβ”€β”€ s04_model.py          # MusicTransformer (LLaMA-style)
β”‚   β”œβ”€β”€ s05_trainer.py        # Training loop with AMP + checkpointing
β”‚   β”œβ”€β”€ s06_generator.py      # Autoregressive generation with KV-cache
β”‚   └── s07_utils.py          # Logging, memory monitoring, seeding
β”œβ”€β”€ tests/
β”‚   └── test_pipeline.py      # 7 unit tests covering all components
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ download_data.sh      # Dataset setup
β”‚   β”œβ”€β”€ train.sh              # Training launcher
β”‚   └── generate.sh           # Generation launcher
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ README.md             # This file
β”‚   β”œβ”€β”€ HLD.md                # High-Level Design
β”‚   β”œβ”€β”€ LLD.md                # Low-Level Design
β”‚   └── flow_diagram.drawio   # Execution flow diagram
β”œβ”€β”€ data/                     # Downloaded MIDI + tokenized cache
β”œβ”€β”€ checkpoints/              # Saved model weights
β”œβ”€β”€ output/                   # Generated MIDI files
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
└── .gitignore
```

## Execution Flow

```
s00_main.py β†’ s01_config.py β†’ s02_tokenizer.py β†’ s03_dataset.py β†’ s04_model.py β†’ s05_trainer.py β†’ s06_generator.py
     β”‚              β”‚                β”‚                  β”‚                β”‚               β”‚                β”‚
  Entry point   Load configs   Init tokenizer   Download & tokenize  Build model  Train loop     Generate MIDI
```

## Model Specifications

| Parameter     | Value                              |
|---------------|-------------------------------------|
| Dim           | 256                                |
| Layers        | 6                                  |
| Heads         | 8 (Q) / 4 (KV) β€” GQA              |
| Hidden (FFN)  | 448 (SwiGLU)                       |
| Max Seq Len   | 1024                               |
| Vocab Size    | 485 (REMI tokens)                  |
| Parameters    | ~5M                                |
| Precision     | BF16/FP16 (AMP)                    |

## Algorithms Used

1. **Rotary Position Embeddings (RoPE)** β€” Su et al. 2021
2. **Grouped Query Attention (GQA)** β€” Ainslie et al. 2023, from LLaMA-2
3. **SwiGLU Activation** β€” Shazeer 2020, from LLaMA
4. **RMS Layer Normalization** β€” Zhang & Sennrich 2019
5. **REMI Tokenization** β€” Huang & Yang 2020
6. **Cosine Annealing with Warmup** β€” Loshchilov & Hutter 2017
7. **Gradient Checkpointing** β€” Chen et al. 2016
8. **KV-Cache** β€” Standard for efficient autoregressive decoding
9. **Nucleus (Top-p) + Top-k Sampling** β€” Holtzman et al. 2020
10. **Repetition Penalty** β€” Keskar et al. 2019