PASTA
aisteer360.algorithms.state_control.pasta
args
control
PASTA
Bases: StateControl
Implementation of PASTA (Post-hoc Attention STeering Approach) from Zhang et al., 2023.
PASTA performs controlled text generation by dynamically modifying attention patterns during inference to amplify or suppress the influence of specific text spans. This allows for fine-grained steering of model behavior without requiring model retraining or parameter updates.
The algorithm works by:
-
Substring Identification: Locate target substrings within the input prompt using tokenizer offset mapping to determine precise token ranges.
-
Attention Modification: Inject scaling factors into the attention mask of specified layers and heads to increase or decrease attention weights for the identified token ranges.
-
Dynamic Steering: Apply different scaling strategies (include, exclude, or generation-focused) to control how the model attends to relevant spans during text generation.
This approach enables real-time control over model focus and can be used for tasks like concept amplification, bias mitigation, or content filtering without architectural changes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
float
|
Scaling factor for attention modification. Positive values increase attention, negative values decrease attention. Defaults to 1.0. |
required |
head_config
|
dict | list
|
Configuration specifying which layers/heads to modify. If dict, maps layer indices to lists of head indices. If list, applies to all heads in specified layers. |
required |
scale_position
|
str
|
Strategy for applying attention scaling. Options:
Defaults to "include". |
required |
Reference: - "PASTA: Tell Your Model Where to Attend: Post-hoc Attention Steering for LLMs" Qingru Zhang, Chandan Singh, Liyuan Liu, Xiaodong Liu, Bin Yu, Jianfeng Gao, Tuo Zhao https://arxiv.org/abs/2311.02262
Source code in aisteer360/algorithms/state_control/pasta/control.py
13 14 15 16 17 18 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 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
device = None
class-attribute
instance-attribute
enabled = True
class-attribute
instance-attribute
hooks = {'pre': [], 'forward': [], 'backward': []}
instance-attribute
model = None
class-attribute
instance-attribute
registered = []
instance-attribute
tokenizer = None
class-attribute
instance-attribute
get_hooks(input_ids, runtime_kwargs, **__)
Create attention modification hooks for specified substrings.
Identifies token ranges corresponding to target substrings and prepares hooks that will modify attention weights during the forward pass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Tensor
|
Input token IDs of shape [batch_size, seq_len]. |
required |
runtime_kwargs
|
dict | None
|
Must contain "substrings" key with target text spans:
|
required |
**__
|
Additional arguments (unused). |
{}
|
Returns:
Type | Description |
---|---|
dict[str, list]
|
dict[str, list]: Hook specifications with "pre", "forward", "backward" keys. Only "pre" hooks are populated for attention modification. |
Raises:
Type | Description |
---|---|
ValueError
|
If "substrings" not in runtime_kwargs or batch size mismatch. |
Source code in aisteer360/algorithms/state_control/pasta/control.py
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 |
|
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, **__)
Initialize PASTA by configuring attention head mappings and model references.
Sets up the layer and head configurations that will be modified during generation. Validates head configurations against model architecture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
PreTrainedModel
|
The base language model to be steered. |
required |
tokenizer
|
PreTrainedTokenizer | None
|
Tokenizer for substring identification. If None, attempts to retrieve from model attributes. |
None
|
**__
|
Additional arguments (unused). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PreTrainedModel |
PreTrainedModel
|
The input model (unchanged). |
Source code in aisteer360/algorithms/state_control/pasta/control.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|