torch.nn.ForwardPreHook
export type ForwardPreHook = (module: Module, input: Tensor[]) => Tensor[] | void;Hook called before a module's forward pass.
ForwardPreHook is executed before the module's forward() method runs. It receives the module and its input tensors, and can optionally modify the input before forward execution.
Signature:
(module: Module, input: Tensor[]) => Tensor[] | voidParameters:
module: The module on which forward is being calledinput: Array of input tensors that will be passed to forward()
Return Value:
- Return modified input tensors to change what forward receives
- Return void/undefined to use original inputs unchanged
- Returning a modified tensor array changes what forward processes
Use Cases:
- Log or inspect inputs before processing
- Modify inputs (e.g., normalize, augment)
- Implement input validation or constraints
- Profile or debug forward pass
- Conditionally skip forward execution
Examples
const hook: ForwardPreHook = (module, input) => {
console.log(`${module.constructor.name} receiving input shape:`, input[0].shape);
// Don't modify, just observe
return;
};
module.register_forward_pre_hook(hook);