ModelHook#
- class ModelHook[source]#
Bases:
HookBaseBridge that forwards lifecycle calls to a model implementing
HookBase.Lets a model participate in the training loop as if it were a hook. In
before_trainit resolves the underlying model (unwrapping the DDP.modulein distributed runs); if that model is itself aHookBaseit is bound as a weak proxy, otherwise a no-opHookBasestand-in is used. Every subsequent lifecycle call (before_epoch,before_step,after_step,after_epoch,after_train) is forwarded to the model. Registered asModelHook.Note
modify_configis intentionally not forwarded — by the time hooks run, the model is already built from the config. The bound model’strainerattribute is set to the same trainer so it can reach shared training state.Example
Add to
cfg.hooks; at each lifecycle point the trainer forwards the call to the model so the model’s own hook methods run inside the loop:hooks = [dict(type="ModelHook")] # → in before_train binds the (DDP-unwrapped) model as a HookBase and # forwards before_epoch/before_step/after_step/after_epoch/after_train # to model.<same method> every step (modify_config is NOT forwarded)