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 | |
specs
Specification utilities for steering controls.
Provides:
ControlSpec: a description of a steering control plus a hyperparameter search space.
Space = Mapping[str, Sequence[Any]] | Sequence[Mapping[str, Any]] | Callable[[dict], Iterable[Mapping[str, Any]]]
module-attribute
ControlSpec
dataclass
Specification for a parameterized steering control.
A ControlSpec describes a control class plus a search space over its constructor arguments. It is used by a
benchmark object to instantiate control instances for different hyperparameter settings.
Attributes:
| Name | Type | Description |
|---|---|---|
control_cls |
Type
|
The steering control class to instantiate. |
params |
Mapping[str, Any]
|
Fixed constructor arguments for the control. |
vars |
Space | None
|
Optional search space over additional constructor arguments. May be:
|
name |
str | None
|
Optional short name for this spec; defaults to |
search_strategy |
Literal['grid', 'random']
|
Strategy for traversing |
num_samples |
int | None
|
Number of points to sample when |
seed |
int | None
|
Optional random seed used when |
Source code in aisteer360/algorithms/core/specs.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 | |
control_cls
instance-attribute
name = None
class-attribute
instance-attribute
num_samples = None
class-attribute
instance-attribute
params = field(default_factory=dict)
class-attribute
instance-attribute
search_strategy = 'grid'
class-attribute
instance-attribute
seed = None
class-attribute
instance-attribute
vars = None
class-attribute
instance-attribute
iter_points(context)
Iterate over local search points for this spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict
|
Context dictionary; passed through to functional |
required |
Yields:
| Type | Description |
|---|---|
Iterable[dict[str, Any]]
|
Parameter dictionaries (possibly empty) that will be merged into |
Iterable[dict[str, Any]]
|
control instance. |
Source code in aisteer360/algorithms/core/specs.py
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 | |
resolve_params(chosen, context)
Compute the full kwargs for this control at a given search point.
Source code in aisteer360/algorithms/core/specs.py
127 128 129 130 131 132 133 134 135 136 137 138 139 | |
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 → input → state → 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
tokenizerattribute will have it auto-injected if not already set
Source code in aisteer360/algorithms/core/steering_pipeline.py
19 20 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
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
supports_batching
property
Return True if all enabled controls in this pipeline are batch-safe.
tokenizer = field(init=False, default=None)
class-attribute
instance-attribute
tokenizer_name_or_path = None
class-attribute
instance-attribute
compute_logprobs(input_ids, attention_mask=None, ref_output_ids=None, runtime_kwargs=None, **forward_kwargs)
Compute per-token log-probabilities of ref_output_ids with structural, input, and state steering controls applied. Note that output controls are not applied since they concern scoring, not generation.
The strategy below uses teacher forcing, computes log P(ref_t | steered_input, ref_1, ..., ref_{t-1}) for each token in the reference sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
list[int] | LongTensor
|
Input token IDs as list or tensor [seq_len] or [batch, seq_len] |
required |
attention_mask
|
Tensor | None
|
Optional attention mask matching input_ids shape |
None
|
ref_output_ids
|
list[int] | LongTensor
|
Reference tokens to score [ref_len] or [batch, ref_len] |
None
|
runtime_kwargs
|
dict | None
|
Per-call parameters for controls (e.g., {"substrings": [...]}) |
None
|
**forward_kwargs
|
Any
|
Additional arguments passed to model forward pass |
{}
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Log probabilities of shape [batch, ref_len] for decoder-only models, or [batch, ref_len - 1] for encoder-decoder models (excludes first decoder token) |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If steer() has not been called |
ValueError
|
If ref_output_ids is None |
Source code in aisteer360/algorithms/core/steering_pipeline.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
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 -> input -> state -> 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
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 | |
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
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
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 | |