fn
register_module_full_backward_pre_hook
→RemovableHandleregister_module_full_backward_pre_hook(hook: Callable[..., object])Register a global pre-hook fired before any module's backward pass.
Invoked at the moment Lucid's autograd engine is about to compute gradients for a Module's parameters. The hook receives the module and the gradient(s) of the module's output(s); it may return a modified tuple of grad-outputs to inject custom backward behaviour (e.g. gradient clipping, sign-SGD-style modifications).
Parameters
hookCallableSignature:
hook(module, grad_output) -> None | tuple.
Returning a tuple replaces the gradient flowing into the module
before any registered post-hook runs.Returns
RemovableHandleHandle for later deregistration.
Notes
"Full" backward pre-hooks are paired with full backward hooks
(register_module_full_backward_hook). Both fire only when
autograd actually visits the module's backward node — modules whose
output gradient is never requested (e.g. frozen branches) are skipped.
Examples
>>> from lucid.nn.hooks import register_module_full_backward_pre_hook
>>> def scale_grads(mod, grad_output):
... return tuple(g * 0.5 for g in grad_output)
>>> h = register_module_full_backward_pre_hook(scale_grads)