ModelHook#

class ModelHook[source]#

Bases: HookBase

Bridge 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_train it resolves the underlying model (unwrapping the DDP .module in distributed runs); if that model is itself a HookBase it is bound as a weak proxy, otherwise a no-op HookBase stand-in is used. Every subsequent lifecycle call (before_epoch, before_step, after_step, after_epoch, after_train) is forwarded to the model. Registered as ModelHook.

Note

modify_config is intentionally not forwarded — by the time hooks run, the model is already built from the config. The bound model’s trainer attribute 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)
after_epoch()[source]#
after_step()[source]#
after_train()[source]#
before_epoch()[source]#
before_step()[source]#
before_train()[source]#