Clamping

class Clamping(module: Module, attr: str, min: int | float | None = None, max: int | float | None = None, *, train_update: bool = True, eval_update: bool = True, as_prehook: bool = False, prepend: bool = False, always_call: bool = False)[source]

Bases: StateHook

Clamps attribute of registered module on call.

Parameters:
  • module (nn.Module) – module to which the hook should be registered.

  • attr (str) – fully-qualified string name of attribute to clamp.

  • min (int | float | None, optional) – inclusive lower-bound of the clamped range. Defaults to None.

  • max (int | float | None, optional) – inclusive upper-bound of the clamped range. Defaults to None.

  • train_update (bool, optional) – if normalization should be performed when hooked module is in train mode. Defaults to True.

  • eval_update (bool, optional) – if normalization should be performed when hooked module is in eval mode. Defaults to True.

  • as_prehook (bool, optional) – if normalization should occur before forward() is. Defaults to False.

  • prepend (bool, optional) – if normalization should occur before other hooks previously registered to the hooked module. Defaults to False.

  • always_call (bool, optional) – if the hook should be run even if an exception occurs, only applies when as_prehook is False. Defaults to False.

hook(module: Module) None[source]

Function to be called on the registered module’s call.

Parameters:

module (nn.Module) – registered module.