Core
aisteer360.algorithms.core
Core functionality for steering pipelines, steering utilities, and argument parsing.
base_args
Base argument validation for steering method configuration.
T = TypeVar('T', bound='BaseArgs')
module-attribute
BaseArgs
dataclass
Base class for all method's args classes.
Source code in aisteer360/algorithms/core/base_args.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
validate(data=None, **kwargs)
classmethod
Create and validate an Args instance from dict, kwargs, or existing instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any | None
|
Existing instance, dict of args, or None |
None
|
**kwargs
|
Additional args (override values in data if both provided) |
{}
|
Returns:
Type | Description |
---|---|
T
|
Validated instance of the Args class |
Source code in aisteer360/algorithms/core/base_args.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
steering_pipeline
Core steering pipeline for composing and applying multiple LLM control methods.
SteeringPipeline
dataclass
Main steering pipeline for applying various control methods to Hugging Face causal language models.
Enables application of structural, state, input, and output controls in a coordinated manner. Controls are applied in a fixed bottom-up order during steering, then used together during generation.
Workflow:
- Instantiate with a base model checkpoint and/or control objects
- Call
steer()
once to apply all controls in order (structural → state → input → output) - Use
generate()
orgenerate_text()
for inference with steering applied
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name_or_path
|
str or Path
|
HuggingFace model hub name or local directory.
Required when |
None
|
controls
|
Sequence[StructuralControl | StateControl | InputControl | OutputControl]
|
Controls for the steering pipeline, max one control per category. Omitted categories fall back to no-op controls (see control base classes). |
()
|
tokenizer_name_or_path
|
str
|
Tokenizer location. Defaults to |
None
|
device_map
|
str or dict[str, int]
|
Device map (passed to
|
'auto'
|
device
|
(device, str)
|
Device (passed to model's |
None
|
hf_model_kwargs
|
dict
|
Extra keyword arguments passed to
|
dict()
|
lazy_init
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If |
ValueError
|
If multiple controls provided for same category or required arguments missing |
Note:
- Maximum one control per category; omitted categories use no-op defaults
- Controls with a
tokenizer
attribute will have it auto-injected if not already set
Source code in aisteer360/algorithms/core/steering_pipeline.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 143 144 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
controls = ()
class-attribute
instance-attribute
device = None
class-attribute
instance-attribute
device_map = 'auto'
class-attribute
instance-attribute
hf_model_kwargs = field(default_factory=dict)
class-attribute
instance-attribute
input_control = field(init=False)
class-attribute
instance-attribute
lazy_init = False
class-attribute
instance-attribute
model = field(init=False, default=None)
class-attribute
instance-attribute
model_name_or_path = None
class-attribute
instance-attribute
output_control = field(init=False)
class-attribute
instance-attribute
state_control = field(init=False)
class-attribute
instance-attribute
structural_control = field(init=False)
class-attribute
instance-attribute
tokenizer = field(init=False, default=None)
class-attribute
instance-attribute
tokenizer_name_or_path = None
class-attribute
instance-attribute
generate(input_ids, attention_mask=None, runtime_kwargs=None, **gen_kwargs)
Generate text with all steering controls applied.
Applies controls in sequence during generation:
- Input control adapts the prompt
- State control registers hooks for state control (e.g., activation steering)
- Output control handles the actual generation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
list[int] | LongTensor
|
Token IDs as list or tensor (shape: [seq_len] or [batch, seq_len]) |
required |
attention_mask
|
Tensor | None
|
Optional attention mask matching input_ids shape |
None
|
runtime_kwargs
|
dict | None
|
Per-generation parameters for controls (e.g., {"substrings": [...]}) |
None
|
**gen_kwargs
|
Generation parameters passed to |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
Generated token IDs (shape: [batch, generated_len]) |
Raises:
Type | Description |
---|---|
RuntimeError
|
If steer() has not yet been called |
Source code in aisteer360/algorithms/core/steering_pipeline.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|
generate_text(*args, **kwargs)
Generate text and decode to string(s).
Convenience wrapper that calls generate() and decodes the output tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Arguments passed to generate() |
()
|
|
**kwargs
|
Keyword arguments passed to generate() |
{}
|
Returns:
Type | Description |
---|---|
str | list[str]
|
Decoded text string (single prompt) or list of strings (batch) |
Source code in aisteer360/algorithms/core/steering_pipeline.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
steer(**steer_kwargs)
Apply all steering controls to the model in place.
Executes each control's steer() method in a fixed bottom-up order: structural -> state -> input -> output. This ensures that higher-level controls always see the final configured model from lower levels.
If any control's steer() method returns a PreTrainedModel instance, it replaces the current model for subsequent controls.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**steer_kwargs
|
Keyword arguments passed to all control steer() methods |
{}
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If called more than once or no model available after steering |
Source code in aisteer360/algorithms/core/steering_pipeline.py
135 136 137 138 139 140 141 142 143 144 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 178 179 180 181 182 183 184 185 |
|
steering_utils
Helper functions for steering.
ensure_pad_token(tokenizer)
Set pad token to eos token if not already defined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer
|
PreTrainedTokenizerBase
|
HuggingFace tokenizer instance |
required |
Returns:
Type | Description |
---|---|
PreTrainedTokenizerBase
|
The same tokenizer with pad_token configured |
Source code in aisteer360/algorithms/core/steering_utils.py
72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
merge_controls(supplied)
Sort supplied controls by category and ensure at most one per category.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
supplied
|
Iterable[StructuralControl | StateControl | InputControl | OutputControl]
|
List of control instances to organize |
required |
Returns:
Type | Description |
---|---|
dict[str, object]
|
Dict mapping field names to control instances (with default no-ops for unspecified categories) |
Raises:
Type | Description |
---|---|
ValueError
|
If multiple controls of the same category are supplied |
TypeError
|
If an unrecognized control type is supplied |
Source code in aisteer360/algorithms/core/steering_utils.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|