File size: 10,707 Bytes
a3b520a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Training script for the latent diffusion model."""

import copy
import csv
import sys
from pathlib import Path

import torch
from torch.utils.data import DataLoader, Dataset, random_split
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

sys.path.insert(0, str(Path(__file__).parent.parent))

from models.autoencoder import KickVAE
from models.diffusion import LatentUNet, NoiseScheduler
from models.text_encoder import KeywordEncoder, build_vocab
from training.config import DiffusionConfig


# ---------------------------------------------------------------------------
# Dataset
# ---------------------------------------------------------------------------

class LatentDataset(Dataset):
    """Dataset of pre-encoded VAE latents with keyword token IDs."""

    def __init__(
        self,
        latents_dir: Path,
        metadata_csv: Path,
        vocab: list[str],
    ) -> None:
        self.latent_files = sorted(
            f for f in latents_dir.glob("*.pt") if not f.name.startswith("._")
        )
        if not self.latent_files:
            raise FileNotFoundError(f"No .pt files in {latents_dir}")

        # Build keyword lookup: filename_stem -> list of keyword strings
        self.kw_to_idx = {kw: i for i, kw in enumerate(vocab)}
        self.keywords: dict[str, list[int]] = {}
        with open(metadata_csv) as f:
            reader = csv.DictReader(f)
            for row in reader:
                stem = Path(row["filename"]).stem
                ids = []
                for kw in row["keywords"].split(","):
                    kw = kw.strip().lower()
                    if kw in self.kw_to_idx:
                        ids.append(self.kw_to_idx[kw])
                self.keywords[stem] = ids

    def __len__(self) -> int:
        return len(self.latent_files)

    def __getitem__(self, idx: int) -> tuple[torch.Tensor, list[int]]:
        path = self.latent_files[idx]
        latent = torch.load(path, weights_only=False)
        # latent filename matches mel filename stem
        stem = path.stem
        token_ids = self.keywords.get(stem, [])
        return latent, token_ids


def collate_fn(
    batch: list[tuple[torch.Tensor, list[int]]],
) -> tuple[torch.Tensor, list[list[int]]]:
    """Custom collate to handle variable-length keyword lists."""
    latents = torch.stack([b[0] for b in batch])
    token_ids = [b[1] for b in batch]
    return latents, token_ids


# ---------------------------------------------------------------------------
# Pre-encode latents
# ---------------------------------------------------------------------------

def pre_encode_latents(cfg: DiffusionConfig) -> None:
    """Encode all mel spectrograms to latents using frozen VAE."""
    cfg.latents_dir.mkdir(parents=True, exist_ok=True)

    # Check if already done
    existing = list(cfg.latents_dir.glob("*.pt"))
    if len(existing) > 100:
        print(f"Latents dir already has {len(existing)} files, skipping encoding.")
        return

    device = torch.device(
        "cuda" if torch.cuda.is_available()
        else "mps" if torch.backends.mps.is_available()
        else "cpu"
    )

    # Load VAE
    checkpoint = torch.load(cfg.vae_checkpoint, weights_only=False)
    vae = KickVAE(latent_dim=cfg.latent_dim).to(device)
    vae.load_state_dict(checkpoint["model_state_dict"])
    vae.eval()

    mel_files = sorted(
        f for f in cfg.data_dir.glob("*.pt") if not f.name.startswith("._")
    )
    print(f"Encoding {len(mel_files)} mel spectrograms to latents...")

    with torch.no_grad():
        for f in tqdm(mel_files):
            out_path = cfg.latents_dir / f.name
            if out_path.exists():
                continue
            mel = torch.load(f, weights_only=False).unsqueeze(0).to(device)
            latent = vae.encode(mel).squeeze(0).cpu()
            torch.save(latent, out_path)

    print("Latent encoding complete.")


# ---------------------------------------------------------------------------
# EMA
# ---------------------------------------------------------------------------

class EMA:
    """Exponential moving average of model parameters."""

    def __init__(self, model: torch.nn.Module, decay: float = 0.9999) -> None:
        self.decay = decay
        self.shadow = copy.deepcopy(model)
        self.shadow.eval()
        for p in self.shadow.parameters():
            p.requires_grad_(False)

    @torch.no_grad()
    def update(self, model: torch.nn.Module) -> None:
        for s, p in zip(self.shadow.parameters(), model.parameters()):
            s.data.mul_(self.decay).add_(p.data, alpha=1 - self.decay)


# ---------------------------------------------------------------------------
# Training
# ---------------------------------------------------------------------------

def train(cfg: DiffusionConfig | None = None) -> None:
    if cfg is None:
        cfg = DiffusionConfig()

    # Pre-encode latents
    pre_encode_latents(cfg)

    device = torch.device(
        "cuda" if torch.cuda.is_available()
        else "mps" if torch.backends.mps.is_available()
        else "cpu"
    )
    print(f"Using device: {device}")

    # Build vocab and dataset
    vocab = build_vocab(cfg.metadata_csv)
    print(f"Vocabulary size: {len(vocab)}")

    dataset = LatentDataset(cfg.latents_dir, cfg.metadata_csv, vocab)
    val_size = int(len(dataset) * cfg.val_split)
    train_size = len(dataset) - val_size
    train_set, val_set = random_split(
        dataset, [train_size, val_size],
        generator=torch.Generator().manual_seed(42),
    )

    train_loader = DataLoader(
        train_set,
        batch_size=cfg.batch_size,
        shuffle=True,
        num_workers=cfg.num_workers,
        pin_memory=True,
        collate_fn=collate_fn,
    )
    val_loader = DataLoader(
        val_set,
        batch_size=cfg.batch_size,
        shuffle=False,
        num_workers=cfg.num_workers,
        pin_memory=True,
        collate_fn=collate_fn,
    )
    print(f"Train: {train_size}, Val: {val_size}")

    # Model
    model = LatentUNet(
        latent_dim=cfg.latent_dim,
        base_channels=cfg.base_channels,
        cond_dim=cfg.cond_dim,
    ).to(device)
    text_enc = KeywordEncoder(
        vocab_size=len(vocab),
        embed_dim=cfg.text_embed_dim,
        cond_dim=cfg.cond_dim,
    ).to(device)

    scheduler = NoiseScheduler(cfg.timesteps, cfg.beta_start, cfg.beta_end).to(device)
    ema = EMA(model, cfg.ema_decay)

    optimizer = torch.optim.AdamW(
        list(model.parameters()) + list(text_enc.parameters()),
        lr=cfg.learning_rate,
    )
    scaler = torch.amp.GradScaler(enabled=cfg.use_amp and device.type == "cuda")

    # Logging
    cfg.log_dir.mkdir(parents=True, exist_ok=True)
    cfg.checkpoint_dir.mkdir(parents=True, exist_ok=True)
    writer = SummaryWriter(cfg.log_dir)

    # Training loop (iteration-based)
    global_step = 0
    model.train()
    text_enc.train()

    print(f"Training for {cfg.iterations} iterations...")

    while global_step < cfg.iterations:
        for latents, token_ids in train_loader:
            if global_step >= cfg.iterations:
                break

            latents = latents.to(device)
            batch_size = latents.shape[0]

            # Classifier-free guidance dropout: replace keywords with empty list
            dropped_ids = []
            for ids in token_ids:
                if torch.rand(1).item() < cfg.cfg_dropout:
                    dropped_ids.append([])
                else:
                    dropped_ids.append(ids)

            # Sample timesteps and noise
            t = torch.randint(0, cfg.timesteps, (batch_size,), device=device)
            noise = torch.randn_like(latents)
            noisy = scheduler.add_noise(latents, noise, t)

            with torch.amp.autocast(
                device_type=device.type,
                enabled=cfg.use_amp and device.type == "cuda",
            ):
                cond = text_enc(dropped_ids, device)
                pred_noise = model(noisy, t, cond)
                loss = torch.nn.functional.mse_loss(pred_noise, noise)
                loss = loss / cfg.gradient_accumulation

            scaler.scale(loss).backward()

            if (global_step + 1) % cfg.gradient_accumulation == 0:
                scaler.step(optimizer)
                scaler.update()
                optimizer.zero_grad()
                ema.update(model)

            # Logging
            if global_step % 50 == 0:
                writer.add_scalar(
                    "train/loss", loss.item() * cfg.gradient_accumulation, global_step
                )

            if global_step % 500 == 0:
                print(
                    f"Step {global_step}/{cfg.iterations} "
                    f"loss={loss.item() * cfg.gradient_accumulation:.6f}"
                )

            # Validation
            if global_step % 1000 == 0 and global_step > 0:
                model.eval()
                text_enc.eval()
                val_loss_sum = 0.0
                val_count = 0
                with torch.no_grad():
                    for vl, vt in val_loader:
                        vl = vl.to(device)
                        vt_step = torch.randint(
                            0, cfg.timesteps, (vl.shape[0],), device=device
                        )
                        vn = torch.randn_like(vl)
                        vnoisy = scheduler.add_noise(vl, vn, vt_step)
                        vcond = text_enc(vt, device)
                        vpred = model(vnoisy, vt_step, vcond)
                        val_loss_sum += torch.nn.functional.mse_loss(vpred, vn).item()
                        val_count += 1
                avg_val = val_loss_sum / max(val_count, 1)
                writer.add_scalar("val/loss", avg_val, global_step)
                print(f"  val_loss={avg_val:.6f}")
                model.train()
                text_enc.train()

            # Checkpoint
            if (global_step + 1) % cfg.checkpoint_every == 0:
                path = cfg.checkpoint_dir / f"diffusion_step_{global_step+1}.pt"
                torch.save({
                    "step": global_step + 1,
                    "model_state_dict": model.state_dict(),
                    "ema_state_dict": ema.shadow.state_dict(),
                    "text_enc_state_dict": text_enc.state_dict(),
                    "optimizer_state_dict": optimizer.state_dict(),
                    "vocab": vocab,
                    "config": cfg,
                }, path)
                print(f"Saved checkpoint: {path}")

            global_step += 1

    writer.close()
    print("Diffusion training complete.")


if __name__ == "__main__":
    train()