from typing import TYPE_CHECKING, Any, Dict, List
from hyper_parallel.trainer.state import TrainerState
if TYPE_CHECKING:
from hyper_parallel.trainer.base import BaseTrainer
class Callback:
def __init__(self, trainer: "BaseTrainer") -> None:
"""Bind the callback to its owning trainer."""
self.trainer = trainer
self.mesh = trainer.mesh
def on_step_begin(self, state: TrainerState, micro_batches: List[Dict[str, Any]] = None, **kwargs: Any) -> None:
"""Hook invoked at the start of each training step."""
def on_step_end(
self, state: TrainerState, loss: float, loss_dict: Dict[str, float], grad_norm: float, **kwargs: Any
) -> None:
"""Hook invoked at the end of each training step."""
def on_micro_step_begin(self, state: TrainerState, micro_batch: Dict[str, Any], **kwargs: Any) -> None:
"""Hook invoked at the start of each gradient-accumulation micro step."""
def on_micro_step_end(self, state: TrainerState, **kwargs: Any) -> None:
"""Hook invoked at the end of each gradient-accumulation micro step."""
def on_epoch_begin(self, state: TrainerState, **kwargs: Any) -> None:
"""Hook invoked at the start of each epoch."""
def on_epoch_end(self, state: TrainerState, **kwargs: Any) -> None:
"""Hook invoked at the end of each epoch."""
def on_train_begin(self, state: TrainerState, **kwargs: Any) -> None:
"""Hook invoked at the start of training."""
def on_train_end(self, state: TrainerState, **kwargs: Any) -> None:
"""Hook invoked at the end of training."""