Spaces:
Running on Zero
Running on Zero
File size: 6,765 Bytes
454ecdd | 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 | """Training callbacks for MANIFOLD."""
from __future__ import annotations
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Optional, Dict, Any, List, TYPE_CHECKING
import json
import time
if TYPE_CHECKING:
from manifold.training.trainer import MANIFOLDTrainer
class Callback(ABC):
"""Base class for training callbacks."""
def on_train_start(self, trainer: "MANIFOLDTrainer") -> None:
"""Called at the start of training."""
pass
def on_train_end(self, trainer: "MANIFOLDTrainer") -> None:
"""Called at the end of training."""
pass
def on_epoch_start(self, trainer: "MANIFOLDTrainer", epoch: int) -> None:
"""Called at the start of each epoch."""
pass
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
"""Called at the end of each epoch."""
pass
def on_batch_end(self, trainer: "MANIFOLDTrainer", batch_info: Dict[str, Any]) -> None:
"""Called at the end of each batch."""
pass
class CheckpointCallback(Callback):
"""Save model checkpoints during training."""
def __init__(
self,
save_dir: str | Path,
save_every_n_epochs: int = 5,
save_best: bool = True,
monitor: str = "val_loss",
mode: str = "min",
):
self.save_dir = Path(save_dir)
self.save_dir.mkdir(parents=True, exist_ok=True)
self.save_every_n_epochs = save_every_n_epochs
self.save_best = save_best
self.monitor = monitor
self.mode = mode
self.best_value = float("inf") if mode == "min" else float("-inf")
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
epoch = epoch_info["epoch"]
# Save periodic checkpoint
if (epoch + 1) % self.save_every_n_epochs == 0:
path = self.save_dir / f"checkpoint_epoch_{epoch+1}.pt"
trainer.save_checkpoint(path)
# Save best checkpoint
if self.save_best:
current = epoch_info.get("val", {}).get("loss", float("inf"))
is_best = (self.mode == "min" and current < self.best_value) or \
(self.mode == "max" and current > self.best_value)
if is_best:
self.best_value = current
path = self.save_dir / "best_model.pt"
trainer.save_checkpoint(path)
class EarlyStoppingCallback(Callback):
"""Stop training when metric stops improving."""
def __init__(
self,
monitor: str = "val_loss",
patience: int = 10,
min_delta: float = 0.0,
mode: str = "min",
):
self.monitor = monitor
self.patience = patience
self.min_delta = min_delta
self.mode = mode
self.best_value = float("inf") if mode == "min" else float("-inf")
self.counter = 0
self.should_stop = False
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
current = epoch_info.get("val", {}).get("loss", float("inf"))
if self.mode == "min":
improved = current < self.best_value - self.min_delta
else:
improved = current > self.best_value + self.min_delta
if improved:
self.best_value = current
self.counter = 0
else:
self.counter += 1
if self.counter >= self.patience:
self.should_stop = True
print(f"Early stopping triggered after {self.counter} epochs without improvement")
class WandBCallback(Callback):
"""Log metrics to Weights & Biases."""
def __init__(
self,
project: str = "manifold",
name: Optional[str] = None,
config: Optional[Dict[str, Any]] = None,
):
self.project = project
self.name = name
self.config = config
self._wandb = None
self._run = None
def on_train_start(self, trainer: "MANIFOLDTrainer") -> None:
try:
import wandb
self._wandb = wandb
self._run = wandb.init(
project=self.project,
name=self.name,
config=self.config or {},
)
except ImportError:
print("wandb not installed, skipping WandB logging")
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
if self._wandb is None:
return
metrics = {
"epoch": epoch_info["epoch"],
"lr": epoch_info.get("lr", 0),
"stage": epoch_info.get("stage", {}).get("stage_name", ""),
}
for prefix in ["train", "val"]:
for k, v in epoch_info.get(prefix, {}).items():
metrics[f"{prefix}/{k}"] = v
self._wandb.log(metrics)
def on_train_end(self, trainer: "MANIFOLDTrainer") -> None:
if self._run:
self._run.finish()
class ProgressCallback(Callback):
"""Print training progress."""
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
epoch = epoch_info["epoch"]
stage = epoch_info.get("stage", {}).get("stage_name", "")
train_loss = epoch_info.get("train", {}).get("loss", 0)
val_loss = epoch_info.get("val", {}).get("loss", 0)
val_acc = epoch_info.get("val", {}).get("accuracy", 0)
lr = epoch_info.get("lr", 0)
print(f"Epoch {epoch+1} | {stage} | "
f"Train Loss: {train_loss:.4f} | "
f"Val Loss: {val_loss:.4f} | "
f"Val Acc: {val_acc:.4f} | "
f"LR: {lr:.2e}")
class CallbackManager:
"""Orchestrate multiple callbacks."""
def __init__(self, callbacks: Optional[List[Callback]] = None):
self.callbacks = callbacks or []
def add(self, callback: Callback) -> None:
self.callbacks.append(callback)
def on_train_start(self, trainer: "MANIFOLDTrainer") -> None:
for cb in self.callbacks:
cb.on_train_start(trainer)
def on_train_end(self, trainer: "MANIFOLDTrainer") -> None:
for cb in self.callbacks:
cb.on_train_end(trainer)
def on_epoch_start(self, trainer: "MANIFOLDTrainer", epoch: int) -> None:
for cb in self.callbacks:
cb.on_epoch_start(trainer, epoch)
def on_epoch_end(self, trainer: "MANIFOLDTrainer", epoch_info: Dict[str, Any]) -> None:
for cb in self.callbacks:
cb.on_epoch_end(trainer, epoch_info)
def on_batch_end(self, trainer: "MANIFOLDTrainer", batch_info: Dict[str, Any]) -> None:
for cb in self.callbacks:
cb.on_batch_end(trainer, batch_info)
|