State control
aisteer360.algorithms.state_control.base
State control base classes.
This module provides the abstract base class for methods that register hooks into the model (e.g., to modify intermediate representations during inference); does not change model weights.
Two base classes are provided:
StateControl
: Base class for all state control methods.NoStateControl
: Identity (null) control; used when no state control is defined in steering pipeline.
State controls implement steering through runtime intervention in the model's forward pass, modifying internal states (activations, attention patterns) to produce generations following y ~ p_θᵃ(x), where "p_θᵃ" is the model with state controls.
Examples of state controls:
- Activation steering (e.g., adding direction vectors)
- Attention head manipulation and pruning
- Layer-wise activation editing
- Dynamic routing between components
- Representation engineering techniques
The base class provides automatic hook management through context managers (ensures cleanup and avoids memory leaks).
See Also:
aisteer360.algorithms.state_control
: Implementations of state control methodsaisteer360.core.steering_pipeline
: Integration with steering pipeline
BackwardHook = Callable[[nn.Module, tuple, tuple], tuple]
module-attribute
ForwardHook = Callable[[nn.Module, tuple, torch.Tensor], torch.Tensor]
module-attribute
HookSpec = dict[str, str | PreHook | ForwardHook | BackwardHook]
module-attribute
PreHook = Callable[[nn.Module, tuple], tuple | torch.Tensor]
module-attribute
NoStateControl
Bases: StateControl
Identity state control.
Used as the default when no state control is needed. Returns empty hook dictionaries and skips registration.
Source code in aisteer360/algorithms/state_control/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
enabled = False
class-attribute
instance-attribute
hooks = {'pre': [], 'forward': [], 'backward': []}
instance-attribute
registered = []
instance-attribute
get_hooks(*_, **__)
Return empty hooks.
Source code in aisteer360/algorithms/state_control/base.py
152 153 154 |
|
register_hooks(*_)
Null registration operation.
Source code in aisteer360/algorithms/state_control/base.py
163 164 165 |
|
remove_hooks(*_)
Null removal operation.
Source code in aisteer360/algorithms/state_control/base.py
167 168 169 |
|
reset()
Null reset operation.
Source code in aisteer360/algorithms/state_control/base.py
175 176 177 |
|
set_hooks(hooks)
Null set operation.
Source code in aisteer360/algorithms/state_control/base.py
171 172 173 |
|
steer(model, tokenizer=None, **kwargs)
Null steering operation.
Source code in aisteer360/algorithms/state_control/base.py
156 157 158 159 160 161 |
|
StateControl
Bases: ABC
Abstract base class for state control steering methods.
Modifies internal model states during forward passes via hooks.
Methods:
Name | Description |
---|---|
get_hooks |
Create hook specs (required) |
steer |
One-time preparation (optional) |
reset |
Reset logic (optional) |
register_hooks |
Attach hooks to model (provided) |
remove_hooks |
Remove all registered hooks (provided) |
Source code in aisteer360/algorithms/state_control/base.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
enabled = True
class-attribute
instance-attribute
hooks = {'pre': [], 'forward': [], 'backward': []}
instance-attribute
registered = []
instance-attribute
get_hooks(input_ids, runtime_kwargs, **kwargs)
abstractmethod
Create hook specifications for the current generation.
Source code in aisteer360/algorithms/state_control/base.py
79 80 81 82 83 84 85 86 87 |
|
register_hooks(model)
Attach hooks to model.
Source code in aisteer360/algorithms/state_control/base.py
96 97 98 99 100 101 102 103 104 105 106 107 |
|
remove_hooks()
Remove all registered hooks from the model.
Source code in aisteer360/algorithms/state_control/base.py
109 110 111 112 113 |
|
reset()
Optional reset call for state control
Source code in aisteer360/algorithms/state_control/base.py
140 141 142 |
|
set_hooks(hooks)
Update the hook specifications to be registered.
Source code in aisteer360/algorithms/state_control/base.py
115 116 117 |
|
steer(model, tokenizer=None, **kwargs)
Optional steering/preparation.
Source code in aisteer360/algorithms/state_control/base.py
89 90 91 92 93 94 |
|