PatchedModel
PatchedModel(model: 'nn.Module', concept_interventions: 'Mapping[str, float | Callable[[torch.Tensor], torch.Tensor]]') -> 'None'Wrapper around a PyTorch model that enforces causal concept interventions at runtime.
Usage
from vraven.decompiler import PatchedModel
value = PatchedModel(...)
Methods
add_module(self, name: str, module: Optional[ForwardRef('Module')]) -> None
Add a child module to the current module.
apply(self, fn: collections.abc.Callable[['Module'], None]) -> Self
Apply ``fn`` recursively to every submodule (as returned by ``.children()``) as well as self.
bfloat16(self) -> Self
Casts all floating point parameters and buffers to ``bfloat16`` datatype.
buffers(self, recurse: bool = True) -> collections.abc.Iterator[torch.Tensor]
Return an iterator over module buffers.
children(self) -> collections.abc.Iterator['Module']
Return an iterator over immediate children modules.
compile(self, *args, **kwargs) -> None
Compile this Module's forward using :func:`torch.compile`.
cpu(self) -> Self
Move all model parameters and buffers to the CPU.
cuda(self, device: int | torch.device | None = None) -> Self
Move all model parameters and buffers to the GPU.
double(self) -> Self
Casts all floating point parameters and buffers to ``double`` datatype.
eval(self) -> Self
Set the module in evaluation mode.
extra_repr(self) -> str
Return the extra representation of the module.
float(self) -> Self
Casts all floating point parameters and buffers to ``float`` datatype.
forward(self, *args: 'Any', **kwargs: 'Any') -> 'Any'
Define the computation performed at every call.
get_buffer(self, target: str) -> torch.Tensor
Return the buffer given by ``target`` if it exists, otherwise throw an error.
get_extra_state(self) -> Any
Return any extra state to include in the module's state_dict.
get_parameter(self, target: str) -> torch.nn.parameter.Parameter
Return the parameter given by ``target`` if it exists, otherwise throw an error.
get_submodule(self, target: str) -> 'Module'
Return the submodule given by ``target`` if it exists, otherwise throw an error.
half(self) -> Self
Casts all floating point parameters and buffers to ``half`` datatype.
ipu(self, device: int | torch.device | None = None) -> Self
Move all model parameters and buffers to the IPU.
load_state_dict(self, state_dict: collections.abc.Mapping[str, typing.Any], strict: bool = True, assign: bool = False)
Copy parameters and buffers from :attr:`state_dict` into this module and its descendants.
modules(self, remove_duplicate: bool = True) -> collections.abc.Iterator['Module']
Return an iterator over all modules in the network.
mtia(self, device: int | torch.device | None = None) -> Self
Move all model parameters and buffers to the MTIA.
named_buffers(self, prefix: str = '', recurse: bool = True, remove_duplicate: bool = True) -> collections.abc.Iterator[tuple[str, torch.Tensor]]
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children(self) -> collections.abc.Iterator[tuple[str, 'Module']]
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules(self, memo: set['Module'] | None = None, prefix: str = '', remove_duplicate: bool = True)
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters(self, prefix: str = '', recurse: bool = True, remove_duplicate: bool = True) -> collections.abc.Iterator[tuple[str, torch.nn.parameter.Parameter]]
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters(self, recurse: bool = True) -> collections.abc.Iterator[torch.nn.parameter.Parameter]
Return an iterator over module parameters.
register_backward_hook(self, hook: collections.abc.Callable[['Module', tuple[torch.Tensor, ...] | torch.Tensor, tuple[torch.Tensor, ...] | torch.Tensor], tuple[torch.Tensor, ...] | torch.Tensor | None]) -> torch.utils.hooks.RemovableHandle
Register a backward hook on the module.
register_buffer(self, name: str, tensor: torch.Tensor | None, persistent: bool = True) -> None
Add a buffer to the module.
register_forward_hook(self, hook: collections.abc.Callable[[~T, tuple[typing.Any, ...], typing.Any], typing.Any | None] | collections.abc.Callable[[~T, tuple[typing.Any, ...], dict[str, typing.Any], typing.Any], typing.Any | None], *, prepend: bool = False, with_kwargs: bool = False, always_call: bool = False) -> torch.utils.hooks.RemovableHandle
Register a forward hook on the module.
register_forward_pre_hook(self, hook: collections.abc.Callable[[~T, tuple[typing.Any, ...]], typing.Any | None] | collections.abc.Callable[[~T, tuple[typing.Any, ...], dict[str, typing.Any]], tuple[typing.Any, dict[str, typing.Any]] | None], *, prepend: bool = False, with_kwargs: bool = False) -> torch.utils.hooks.RemovableHandle
Register a forward pre-hook on the module.
register_full_backward_hook(self, hook: collections.abc.Callable[['Module', tuple[torch.Tensor, ...] | torch.Tensor, tuple[torch.Tensor, ...] | torch.Tensor], tuple[torch.Tensor, ...] | torch.Tensor | None], prepend: bool = False) -> torch.utils.hooks.RemovableHandle
Register a backward hook on the module.
register_full_backward_pre_hook(self, hook: collections.abc.Callable[['Module', tuple[torch.Tensor, ...] | torch.Tensor], tuple[torch.Tensor, ...] | torch.Tensor | None], prepend: bool = False) -> torch.utils.hooks.RemovableHandle
Register a backward pre-hook on the module.
register_load_state_dict_post_hook(self, hook)
Register a post-hook to be run after module's :meth:`~nn.Module.load_state_dict` is called.
register_load_state_dict_pre_hook(self, hook)
Register a pre-hook to be run before module's :meth:`~nn.Module.load_state_dict` is called.
register_module(self, name: str, module: Optional[ForwardRef('Module')]) -> None
Alias for :func:`add_module`.
register_parameter(self, name: str, param: torch.nn.parameter.Parameter | None) -> None
Add a parameter to the module.
register_state_dict_post_hook(self, hook)
Register a post-hook for the :meth:`~torch.nn.Module.state_dict` method.
register_state_dict_pre_hook(self, hook)
Register a pre-hook for the :meth:`~torch.nn.Module.state_dict` method.
remove_hooks(self) -> 'None'
Remove all active intervention hooks.
requires_grad_(self, requires_grad: bool = True) -> Self
Change if autograd should record operations on parameters in this module.
set_extra_state(self, state: Any) -> None
Set extra state contained in the loaded `state_dict`.
set_submodule(self, target: str, module: 'Module', strict: bool = False) -> None
Set the submodule given by ``target`` if it exists, otherwise throw an error.
share_memory(self) -> Self
See :meth:`torch.Tensor.share_memory_`.
state_dict(self, *args, destination=None, prefix='', keep_vars=False)
Return a dictionary containing references to the whole state of the module.
to(self, *args, **kwargs)
Move and/or cast the parameters and buffers.
to_empty(self, *, device: str | torch.device | int | None, recurse: bool = True) -> Self
Move the parameters and buffers to the specified device without copying storage.
train(self, mode: bool = True) -> Self
Set the module in training mode.
type(self, dst_type: torch.dtype | str) -> Self
Casts all parameters and buffers to :attr:`dst_type`.
xpu(self, device: int | torch.device | None = None) -> Self
Move all model parameters and buffers to the XPU.
zero_grad(self, set_to_none: bool = True) -> None
Reset gradients of all model parameters.