File size: 17,559 Bytes
e7f17a4 d7fa769 e7f17a4 d7fa769 e7f17a4 d7fa769 e7f17a4 d7fa769 e7f17a4 d7fa769 e7f17a4 d7fa769 e7f17a4 | 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | """
Training loop for the Transformer translator.
===============================================
Provides:
β’ ``TranslationDataset`` β a PyTorch Dataset that tokenises and pads
source/target sentence pairs.
β’ ``create_dataloaders`` β builds train / validation DataLoaders with
an 90/10 split.
β’ ``train_one_epoch`` β one full pass over the training set.
β’ ``evaluate_loss`` β average loss on the validation set.
β’ ``train`` β full training driver with logging, LR
scheduling, checkpointing, and early stopping.
Design choices:
β’ Label-smoothed cross-entropy (smoothing = 0.1) for better
generalisation.
β’ AdamW with a linear-warmup + cosine-decay schedule (stable for
small datasets).
β’ Mixed-precision (AMP) with ``torch.amp`` for speed / memory on T4.
β’ Gradient clipping at max_norm = 1.0 to avoid exploding gradients.
"""
from __future__ import annotations
import math
import os
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional, Tuple
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from tokenizers import Tokenizer
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. Translation Dataset
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TranslationDataset(Dataset):
"""
Wraps a HuggingFace dataset of translation pairs into a PyTorch
Dataset that returns padded token-ID tensors.
Each ``__getitem__`` returns::
{
"src": LongTensor[max_len], # source token IDs (padded)
"tgt": LongTensor[max_len], # target input (with [BOS], no final [EOS])
"label": LongTensor[max_len], # target labels (no [BOS], with [EOS])
}
The *tgt* / *label* split implements **teacher forcing**: the decoder
receives ``[BOS] w1 w2 β¦`` and must predict ``w1 w2 β¦ [EOS]``.
"""
def __init__(
self,
hf_dataset,
src_tokenizer: Tokenizer,
tgt_tokenizer: Tokenizer,
src_lang: str = "en",
tgt_lang: str = "ms",
max_len: int = 128,
pad_id: int = 0,
):
self.data = hf_dataset
self.src_tok = src_tokenizer
self.tgt_tok = tgt_tokenizer
self.src_lang = src_lang
self.tgt_lang = tgt_lang
self.max_len = max_len
self.pad_id = pad_id
def __len__(self) -> int:
return len(self.data)
def _pad(self, ids: List[int]) -> List[int]:
"""Truncate to max_len, then right-pad with pad_id."""
ids = ids[: self.max_len]
return ids + [self.pad_id] * (self.max_len - len(ids))
def __getitem__(self, idx: int) -> dict:
pair = self.data[idx]["translation"]
# Encode (includes [BOS] β¦ [EOS] from post-processor)
src_ids = self.src_tok.encode(pair[self.src_lang]).ids
tgt_ids = self.tgt_tok.encode(pair[self.tgt_lang]).ids
# Teacher-forcing split:
# tgt_input = [BOS] w1 w2 β¦ wN (drop last token)
# tgt_label = w1 w2 β¦ wN [EOS] (drop first token)
tgt_input = tgt_ids[:-1]
tgt_label = tgt_ids[1:]
return {
"src": torch.tensor(self._pad(src_ids), dtype=torch.long),
"tgt": torch.tensor(self._pad(tgt_input), dtype=torch.long),
"label": torch.tensor(self._pad(tgt_label), dtype=torch.long),
}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. DataLoader factory
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def create_dataloaders(
hf_dataset,
src_tokenizer: Tokenizer,
tgt_tokenizer: Tokenizer,
src_lang: str = "en",
tgt_lang: str = "ms",
max_len: int = 128,
batch_size: int = 32,
val_ratio: float = 0.1,
pad_id: int = 0,
seed: int = 42,
) -> Tuple[DataLoader, DataLoader, TranslationDataset]:
"""
Build training and validation DataLoaders from a HuggingFace dataset.
Returns
-------
train_loader, val_loader, full_dataset
"""
full_ds = TranslationDataset(
hf_dataset, src_tokenizer, tgt_tokenizer,
src_lang, tgt_lang, max_len, pad_id,
)
val_size = max(1, int(len(full_ds) * val_ratio))
train_size = len(full_ds) - val_size
generator = torch.Generator().manual_seed(seed)
train_ds, val_ds = random_split(full_ds, [train_size, val_size], generator=generator)
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True, drop_last=False)
val_loader = DataLoader(val_ds, batch_size=batch_size, shuffle=False, drop_last=False)
print(f"Train: {train_size} | Val: {val_size} | Batch size: {batch_size}")
return train_loader, val_loader, full_ds
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. Training configuration dataclass
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class TrainConfig:
"""All tuneable knobs in one place."""
epochs: int = 50
batch_size: int = 32
max_len: int = 128
lr: float = 5e-4
warmup_steps: int = 200
label_smoothing: float = 0.1
grad_clip: float = 1.0
use_amp: bool = True
val_ratio: float = 0.1
checkpoint_dir: str = "training/checkpoints"
log_every: int = 10 # print loss every N steps
patience: int = 10 # early-stopping patience (epochs)
seed: int = 42
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. LR scheduler with linear warmup + cosine decay
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _build_scheduler(optimizer, warmup_steps: int, total_steps: int):
"""Linear warmup for `warmup_steps`, then cosine decay to 0."""
def lr_lambda(step):
if step < warmup_steps:
return step / max(1, warmup_steps)
progress = (step - warmup_steps) / max(1, total_steps - warmup_steps)
return 0.5 * (1.0 + math.cos(math.pi * progress))
return torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. Single-epoch training
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def train_one_epoch(
model: nn.Module,
loader: DataLoader,
optimizer: torch.optim.Optimizer,
scheduler,
criterion: nn.Module,
device: torch.device,
scaler: Optional[torch.amp.GradScaler],
grad_clip: float = 1.0,
log_every: int = 10,
epoch: int = 0,
) -> float:
"""Train for one epoch. Returns average loss."""
model.train()
total_loss = 0.0
n_tokens = 0
for step, batch in enumerate(loader):
src = batch["src"].to(device)
tgt = batch["tgt"].to(device)
label = batch["label"].to(device)
optimizer.zero_grad()
amp_enabled = scaler is not None
with torch.amp.autocast("cuda", enabled=amp_enabled):
logits = model(src, tgt) # (B, T, V)
loss = criterion(logits.reshape(-1, logits.size(-1)), label.reshape(-1))
if scaler is not None:
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
nn.utils.clip_grad_norm_(model.parameters(), grad_clip)
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), grad_clip)
optimizer.step()
scheduler.step()
# Accumulate loss (ignore padding contribution)
non_pad = (label != model.pad_idx).sum().item()
total_loss += loss.item() * non_pad
n_tokens += non_pad
if (step + 1) % log_every == 0:
avg = total_loss / max(n_tokens, 1)
lr = scheduler.get_last_lr()[0]
print(f" Epoch {epoch+1} | Step {step+1}/{len(loader)} | Loss {avg:.4f} | LR {lr:.2e}")
return total_loss / max(n_tokens, 1)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 6. Validation loss
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@torch.no_grad()
def evaluate_loss(
model: nn.Module,
loader: DataLoader,
criterion: nn.Module,
device: torch.device,
use_amp: bool = False,
) -> float:
"""Compute average loss over a validation set (with AMP to match training)."""
model.eval()
total_loss = 0.0
n_tokens = 0
n_batches = len(loader)
for step, batch in enumerate(loader):
src = batch["src"].to(device)
tgt = batch["tgt"].to(device)
label = batch["label"].to(device)
with torch.amp.autocast("cuda", enabled=use_amp):
logits = model(src, tgt)
loss = criterion(logits.reshape(-1, logits.size(-1)), label.reshape(-1))
non_pad = (label != model.pad_idx).sum().item()
total_loss += loss.item() * non_pad
n_tokens += non_pad
if (step + 1) % max(1, n_batches // 4) == 0 or (step + 1) == n_batches:
print(f" Val {step+1}/{n_batches}", end="\r")
return total_loss / max(n_tokens, 1)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 7. Full training driver
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def train(
model: nn.Module,
train_loader: DataLoader,
val_loader: DataLoader,
cfg: TrainConfig,
device: torch.device,
trial=None,
resume_from: Optional[str] = None,
epoch_callback=None,
) -> dict:
"""
Full training loop with logging, checkpointing, and early stopping.
Parameters
----------
trial : optuna.trial.Trial, optional
If provided, reports val_loss after each epoch for ASHA pruning.
resume_from : str, optional
Path to a ``resume_state.pt`` file. If provided, training resumes
from the saved epoch with the exact optimizer / scheduler / scaler
state, history, and early-stopping counters.
epoch_callback : callable, optional
Called after every epoch as ``epoch_callback(epoch, history)``.
Useful for live plotting in notebooks.
Returns
-------
history : dict
``{"train_loss": [...], "val_loss": [...], "lr": [...]}``
"""
# --- Loss function (label-smoothed CE, ignoring PAD) ---------------
criterion = nn.CrossEntropyLoss(
ignore_index=model.pad_idx,
label_smoothing=cfg.label_smoothing,
)
# --- Optimiser ------------------------------------------------------
optimizer = torch.optim.AdamW(model.parameters(), lr=cfg.lr, betas=(0.9, 0.98), eps=1e-9)
# --- LR schedule ---------------------------------------------------
total_steps = cfg.epochs * len(train_loader)
scheduler = _build_scheduler(optimizer, cfg.warmup_steps, total_steps)
# --- AMP scaler ----------------------------------------------------
scaler = torch.amp.GradScaler("cuda") if (cfg.use_amp and device.type == "cuda") else None
# --- Checkpoint dir ------------------------------------------------
ckpt_dir = Path(cfg.checkpoint_dir)
ckpt_dir.mkdir(parents=True, exist_ok=True)
history: dict = {"train_loss": [], "val_loss": [], "lr": []}
best_val = float("inf")
patience_ctr = 0
start_epoch = 0
# --- Resume from checkpoint ----------------------------------------
if resume_from is not None and os.path.exists(resume_from):
print(f"\nπ Resuming from {resume_from}")
ckpt = torch.load(resume_from, map_location=device, weights_only=False)
model.load_state_dict(ckpt["model_state_dict"])
optimizer.load_state_dict(ckpt["optimizer_state_dict"])
scheduler.load_state_dict(ckpt["scheduler_state_dict"])
if scaler is not None and "scaler_state_dict" in ckpt:
scaler.load_state_dict(ckpt["scaler_state_dict"])
start_epoch = ckpt["epoch"] + 1 # resume from *next* epoch
best_val = ckpt["best_val_loss"]
patience_ctr = ckpt["patience_ctr"]
history = ckpt["history"]
print(f" Resumed at epoch {start_epoch+1}/{cfg.epochs} | "
f"best_val={best_val:.4f} | patience={patience_ctr}/{cfg.patience}")
print(f"\n{'='*60}")
print(f"Starting training: {cfg.epochs} epochs (from epoch {start_epoch+1}), lr={cfg.lr}, AMP={cfg.use_amp}")
print(f"{'='*60}\n")
for epoch in range(start_epoch, cfg.epochs):
t0 = time.time()
train_loss = train_one_epoch(
model, train_loader, optimizer, scheduler, criterion,
device, scaler, cfg.grad_clip, cfg.log_every, epoch,
)
use_amp = cfg.use_amp and device.type == "cuda"
val_loss = evaluate_loss(model, val_loader, criterion, device, use_amp=use_amp)
lr = scheduler.get_last_lr()[0]
elapsed = time.time() - t0
history["train_loss"].append(train_loss)
history["val_loss"].append(val_loss)
history["lr"].append(lr)
print(
f"Epoch {epoch+1}/{cfg.epochs} | "
f"Train {train_loss:.4f} | Val {val_loss:.4f} | "
f"LR {lr:.2e} | {elapsed:.1f}s"
)
# --- Optuna ASHA pruning (if trial provided) ------------------
if trial is not None:
import optuna
trial.report(val_loss, epoch)
if trial.should_prune():
print(f"\nβ Optuna pruned this trial at epoch {epoch+1}.")
raise optuna.TrialPruned()
# --- Checkpoint best model ------------------------------------
if val_loss < best_val:
best_val = val_loss
patience_ctr = 0
torch.save(model.state_dict(), ckpt_dir / "best_model.pt")
print(f" β³ New best val loss β checkpoint saved.")
else:
patience_ctr += 1
if patience_ctr >= cfg.patience:
print(f"\nβΉ Early stopping after {cfg.patience} epochs without improvement.")
break
# --- Save resumable state after every epoch --------------------
resume_state = {
"epoch": epoch,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"scheduler_state_dict": scheduler.state_dict(),
"scaler_state_dict": scaler.state_dict() if scaler is not None else None,
"best_val_loss": best_val,
"patience_ctr": patience_ctr,
"history": history,
"cfg_epochs": cfg.epochs,
}
torch.save(resume_state, ckpt_dir / "resume_state.pt")
# --- Epoch callback (e.g. live plotting) ----------------------
if epoch_callback is not None:
epoch_callback(epoch, history)
# Load best checkpoint
model.load_state_dict(torch.load(ckpt_dir / "best_model.pt", map_location=device, weights_only=True))
print(f"\nβ Training complete. Best val loss: {best_val:.4f}")
return history
|