SASA
aisteer360.algorithms.output_control.sasa
args
control
SASA
Bases: OutputControl
Implementation of SASA (Self-disciplined autoregressive sampling) from Ko et al., 2024.
SASA works in two phases:
-
Subspace learning: From a labelled toxic / non-toxic corpus, it fits a linear classifier in the model’s own sentence-embedding space; the weight vector defines a toxicity subspace.
-
Controlled decoding: At every decoding step the candidate-token logits are shifted by beta * margin, where margin is the classifier distance of the updated context from the toxic side of the subspace. Sampling from the soft-max of these adjusted logits (optionally with nucleus sampling) nudges generation away from toxic regions while staying close to the original distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beta
|
float
|
Scaling coefficient for value redistribution. Defaults to 0.0. |
required |
wv_path
|
str
|
Path to a saved steering-vector tensor. Defaults to None. |
required |
gen_wv_data_path
|
str
|
Path to the value dataset, e.g. sentences with labeled toxicity. Defaults to "Jigsaw_data/". |
required |
gen_wv_length
|
int
|
The maximum number of samples used for preparing SASA steering if wv_path does not exist. Defaults to -1 (use all). |
required |
gen_wv_batch_size
|
int
|
The batch size used for preparing SASA steering if wv_path does not exist. Defaults to 4. |
required |
Reference:
- "Large Language Models can Become Strong Self-Detoxifiers" Ching-Yun Ko, Pin-Yu Chen, Payel Das, Youssef Mroueh, Soham Dan, Georgios Kollias, Subhajit Chaudhury, Tejaswini Pedapati, Luca Daniel https://arxiv.org/abs/2410.03818
Source code in aisteer360/algorithms/output_control/sasa/control.py
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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
base_generate = None
class-attribute
instance-attribute
beta
instance-attribute
enabled = True
class-attribute
instance-attribute
model = None
class-attribute
instance-attribute
tokenizer = None
class-attribute
instance-attribute
wv
instance-attribute
generate(input_ids, attention_mask, runtime_kwargs, model, **gen_kwargs)
Execute SASA-guided generation with margin-based logit adjustment.
Performs controlled generation by computing the distance from toxic subspace at each decoding step and adjusting token logits based on this margin. Returns text steered away from toxic regions while maintaining coherence.
At each decoding step:
- Generate embeddings for all valid candidate tokens
- Compute margin (distance from toxic subspace) for each candidate
- Adjust logits by beta * softmax(margins)
- Sample from adjusted distribution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Tensor
|
Input token IDs of shape [batch_size, seq_len]. |
required |
attention_mask
|
Tensor
|
Attention mask matching input_ids shape. |
required |
runtime_kwargs
|
dict | None
|
Runtime parameters (unused). |
required |
model
|
PreTrainedModel
|
The language model used for generation. Must match the model provided during steer(). |
required |
**gen_kwargs
|
Generation parameters passed to model internals:
|
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Generated token IDs including the input prompt. |
Note:
- Computes full forward passes for all valid candidate tokens at each step
- Uses custom KV cache manipulation for efficient candidate evaluation
- Margins computed relative to learned toxic/non-toxic boundary
- SASA is memory intensive; scales with vocabulary size at each generation step
Source code in aisteer360/algorithms/output_control/sasa/control.py
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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
repeat_kv_cache(cache, repeats)
staticmethod
Repeat KV cache entries for parallel candidate evaluation.
Duplicates cache entries to enable efficient parallel processing of multiple candidate tokens without recomputing shared context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache
|
KV cache in various formats (DynamicCache, tuple, or custom). |
required | |
repeats
|
int
|
Number of times to repeat each cache entry. |
required |
Returns:
Type | Description |
---|---|
Repeated cache in same format as input. |
Raises:
Type | Description |
---|---|
TypeError
|
If cache type is not supported. |
Source code in aisteer360/algorithms/output_control/sasa/control.py
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 |
|
select_kv_cache(cache, select_idx)
staticmethod
Select specific entries from KV cache based on indices.
Extracts cache entries corresponding to selected beam paths, used after evaluating multiple candidates to continue with the chosen token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache
|
KV cache in various formats. |
required | |
select_idx
|
Tensor
|
1D tensor of indices to select. |
required |
Returns:
Type | Description |
---|---|
Selected cache entries in same format as input. |
Raises:
Type | Description |
---|---|
ValueError
|
If select_idx is not 1D. |
TypeError
|
If cache type is not supported. |
Source code in aisteer360/algorithms/output_control/sasa/control.py
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 |
|
steer(model, tokenizer=None, **__)
Initialize SASA by loading or generating the toxicity steering vector.
Sets up the linear classifier in the model's embedding space that defines the toxicity subspace. Either loads a pre-computed steering vector or generates one from labeled data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
PreTrainedModel
|
The base language model to be steered. |
required |
tokenizer
|
PreTrainedTokenizer | None
|
Tokenizer for the base model. If None, attempts to retrieve from model attributes. |
None
|
**__
|
Additional arguments (unused). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PreTrainedModel |
PreTrainedModel
|
The input model (unchanged). |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If gen_wv_data_path doesn't contain required Jigsaw dataset |
Note:
- If wv_path is provided, loads pre-computed steering vector
- Otherwise generates steering vector from Jigsaw toxicity dataset
- Steering vector generation uses closed-form Bayes optimal classifier
- Saves generated steering vector to 'steer_wv.pt' for future use
Source code in aisteer360/algorithms/output_control/sasa/control.py
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 |
|