API
ActivationsStore
Class for streaming tokens and generating and storing activations while training SAEs.
Source code in sae_lens/training/activations_store.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
|
get_activations(batch_tokens)
Returns activations of shape (batches, context, num_layers, d_in)
d_in may result from a concatenated head dimension.
Source code in sae_lens/training/activations_store.py
get_batch_tokens(batch_size=None, raise_at_epoch_end=False)
Streams a batch of tokens from a dataset.
If raise_at_epoch_end is true we will reset the dataset at the end of each epoch and raise a StopIteration. Otherwise we will reset silently.
Source code in sae_lens/training/activations_store.py
get_buffer(n_batches_in_buffer, raise_on_epoch_end=False, shuffle=True)
Loads the next n_batches_in_buffer batches of activations into a tensor and returns half of it.
The primary purpose here is maintaining a shuffling buffer.
If raise_on_epoch_end is True, when the dataset it exhausted it will automatically refill the dataset and then raise a StopIteration so that the caller has a chance to react.
Source code in sae_lens/training/activations_store.py
get_data_loader()
Return a torch.utils.dataloader which you can get batches from.
Should automatically refill the buffer when it gets to n % full. (better mixing if you refill and shuffle regularly).
Source code in sae_lens/training/activations_store.py
load_cached_activation_dataset()
Load the cached activation dataset from disk.
- If cached_activations_path is set, returns Huggingface Dataset else None
- Checks that the loaded dataset has current has activations for hooks in config and that shapes match.
Source code in sae_lens/training/activations_store.py
next_batch()
Get the next batch from the current DataLoader. If the DataLoader is exhausted, refill the buffer and create a new DataLoader.
Source code in sae_lens/training/activations_store.py
reset_input_dataset()
shuffle_input_dataset(seed, buffer_size=1)
This applies a shuffle to the huggingface dataset that is the input to the activations store. This also shuffles the shards of the dataset, which is especially useful for evaluating on different sections of very large streaming datasets. Buffer size is only relevant for streaming datasets. The default buffer_size of 1 means that only the shard will be shuffled; larger buffer sizes will additionally shuffle individual elements within the shard.
Source code in sae_lens/training/activations_store.py
CacheActivationsRunner
Source code in sae_lens/cache_activations_runner.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 276 277 278 279 280 281 282 283 284 |
|
__str__()
Print the number of tokens to be cached. Print the number of buffers, and the number of tokens per buffer. Print the disk space required to store the activations.
Source code in sae_lens/cache_activations_runner.py
CacheActivationsRunnerConfig
dataclass
Configuration for caching activations of an LLM.
Source code in sae_lens/config.py
HookedSAETransformer
Bases: HookedTransformer
Source code in sae_lens/analysis/hooked_sae_transformer.py
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 |
|
__init__(*model_args, **model_kwargs)
Model initialization. Just HookedTransformer init, but adds a dictionary to keep track of attached SAEs.
Note that if you want to load the model from pretrained weights, you should use
:meth:from_pretrained
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*model_args |
Any
|
Positional arguments for HookedTransformer initialization |
()
|
**model_kwargs |
Any
|
Keyword arguments for HookedTransformer initialization |
{}
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
add_sae(sae, use_error_term=None)
Attaches an SAE to the model
WARNING: This sae will be permanantly attached until you remove it with reset_saes. This function will also overwrite any existing SAE attached to the same hook point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sae |
SAE
|
SparseAutoencoderBase. The SAE to attach to the model |
required |
use_error_term |
Optional[bool]
|
(Optional[bool]) If provided, will set the use_error_term attribute of the SAE to this value. Determines whether the SAE returns input or reconstruction. Defaults to None. |
None
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
reset_saes(act_names=None, prev_saes=None)
Reset the SAEs attached to the model
If act_names are provided will just reset SAEs attached to those hooks. Otherwise will reset all SAEs attached to the model. Optionally can provide a list of prev_saes to reset to. This is mainly used to restore previously attached SAEs after temporarily running with different SAEs (eg with run_with_saes).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
act_names |
Optional[Union[str, List[str]]
|
The act_names of the SAEs to reset. If None, will reset all SAEs attached to the model. Defaults to None. |
None
|
prev_saes |
Optional[List[Union[HookedSAE, None]]]
|
List of SAEs to replace the current ones with. If None, will just remove the SAEs. Defaults to None. |
None
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
run_with_cache_with_saes(*model_args, saes=[], reset_saes_end=True, use_error_term=None, return_cache_object=True, remove_batch_dim=False, **kwargs)
Wrapper around 'run_with_cache' in HookedTransformer.
Attaches given SAEs before running the model with cache and then removes them. By default, will reset all SAEs to original state after.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*model_args |
Any
|
Positional arguments for the model forward pass |
()
|
saes |
Union[SAE, List[SAE]]
|
(Union[HookedSAE, List[HookedSAE]]) The SAEs to be attached for this forward pass |
[]
|
reset_saes_end |
bool
|
(bool) If True, all SAEs added during this run are removed at the end, and previously attached SAEs are restored to their original state. Default is True. |
True
|
use_error_term |
Optional[bool]
|
(Optional[bool]) If provided, will set the use_error_term attribute of all SAEs attached during this run to this value. Determines whether the SAE returns input or reconstruction. Defaults to None. |
None
|
return_cache_object |
bool
|
(bool) if True, this will return an ActivationCache object, with a bunch of useful HookedTransformer specific methods, otherwise it will return a dictionary of activations as in HookedRootModule. |
True
|
remove_batch_dim |
bool
|
(bool) Whether to remove the batch dimension (only works for batch_size==1). Defaults to False. |
False
|
**kwargs |
Any
|
Keyword arguments for the model forward pass |
{}
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
run_with_hooks_with_saes(*model_args, saes=[], reset_saes_end=True, fwd_hooks=[], bwd_hooks=[], reset_hooks_end=True, clear_contexts=False, **model_kwargs)
Wrapper around 'run_with_hooks' in HookedTransformer.
Attaches the given SAEs to the model before running the model with hooks and then removes them. By default, will reset all SAEs to original state after.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*model_args |
Any
|
Positional arguments for the model forward pass |
()
|
act_names |
(Union[HookedSAE, List[HookedSAE]]) The SAEs to be attached for this forward pass |
required | |
reset_saes_end |
bool
|
(bool) If True, all SAEs added during this run are removed at the end, and previously attached SAEs are restored to their original state. (default: True) |
True
|
fwd_hooks |
List[Tuple[Union[str, Callable], Callable]]
|
(List[Tuple[Union[str, Callable], Callable]]) List of forward hooks to apply |
[]
|
bwd_hooks |
List[Tuple[Union[str, Callable], Callable]]
|
(List[Tuple[Union[str, Callable], Callable]]) List of backward hooks to apply |
[]
|
reset_hooks_end |
bool
|
(bool) Whether to reset the hooks at the end of the forward pass (default: True) |
True
|
clear_contexts |
bool
|
(bool) Whether to clear the contexts at the end of the forward pass (default: False) |
False
|
**model_kwargs |
Any
|
Keyword arguments for the model forward pass |
{}
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
run_with_saes(*model_args, saes=[], reset_saes_end=True, use_error_term=None, **model_kwargs)
Wrapper around HookedTransformer forward pass.
Runs the model with the given SAEs attached for one forward pass, then removes them. By default, will reset all SAEs to original state after.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*model_args |
Any
|
Positional arguments for the model forward pass |
()
|
saes |
Union[SAE, List[SAE]]
|
(Union[HookedSAE, List[HookedSAE]]) The SAEs to be attached for this forward pass |
[]
|
reset_saes_end |
bool
|
If True, all SAEs added during this run are removed at the end, and previously attached SAEs are restored to their original state. Default is True. |
True
|
use_error_term |
Optional[bool]
|
(Optional[bool]) If provided, will set the use_error_term attribute of all SAEs attached during this run to this value. Defaults to None. |
None
|
**model_kwargs |
Any
|
Keyword arguments for the model forward pass |
{}
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
saes(saes=[], reset_saes_end=True, use_error_term=None)
A context manager for adding temporary SAEs to the model. See HookedTransformer.hooks for a similar context manager for hooks. By default will keep track of previously attached SAEs, and restore them when the context manager exits.
Example:
.. code-block:: python
from transformer_lens import HookedSAETransformer, HookedSAE, HookedSAEConfig
model = HookedSAETransformer.from_pretrained('gpt2-small')
sae_cfg = HookedSAEConfig(...)
sae = HookedSAE(sae_cfg)
with model.saes(saes=[sae]):
spliced_logits = model(text)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
saes |
Union[HookedSAE, List[HookedSAE]]
|
SAEs to be attached. |
[]
|
reset_saes_end |
bool
|
If True, removes all SAEs added by this context manager when the context manager exits, returning previously attached SAEs to their original state. |
True
|
use_error_term |
Optional[bool]
|
If provided, will set the use_error_term attribute of all SAEs attached during this run to this value. Defaults to None. |
None
|
Source code in sae_lens/analysis/hooked_sae_transformer.py
LanguageModelSAERunnerConfig
dataclass
Configuration for training a sparse autoencoder on a language model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
architecture |
str
|
The architecture to use, either "standard", "gated", or "jumprelu". |
'standard'
|
model_name |
str
|
The name of the model to use. This should be the name of the model in the Hugging Face model hub. |
'gelu-2l'
|
model_class_name |
str
|
The name of the class of the model to use. This should be either |
'HookedTransformer'
|
hook_name |
str
|
The name of the hook to use. This should be a valid TransformerLens hook. |
'blocks.0.hook_mlp_out'
|
hook_eval |
str
|
NOT CURRENTLY IN USE. The name of the hook to use for evaluation. |
'NOT_IN_USE'
|
hook_layer |
int
|
The index of the layer to hook. Used to stop forward passes early and speed up processing. |
0
|
hook_head_index |
int
|
When the hook if for an activatio with a head index, we can specify a specific head to use here. |
None
|
dataset_path |
str
|
A Hugging Face dataset path. |
''
|
dataset_trust_remote_code |
bool
|
Whether to trust remote code when loading datasets from Huggingface. |
True
|
streaming |
bool
|
Whether to stream the dataset. Streaming large datasets is usually practical. |
True
|
is_dataset_tokenized |
bool
|
NOT IN USE. We used to use this but now automatically detect if the dataset is tokenized. |
True
|
context_size |
int
|
The context size to use when generating activations on which to train the SAE. |
128
|
use_cached_activations |
bool
|
Whether to use cached activations. This is useful when doing sweeps over the same activations. |
False
|
cached_activations_path |
str
|
The path to the cached activations. |
None
|
d_in |
int
|
The input dimension of the SAE. |
512
|
d_sae |
int
|
The output dimension of the SAE. If None, defaults to |
None
|
b_dec_init_method |
str
|
The method to use to initialize the decoder bias. Zeros is likely fine. |
'geometric_median'
|
expansion_factor |
int
|
The expansion factor. Larger is better but more computationally expensive. Default is 4. |
None
|
activation_fn |
str
|
The activation function to use. Relu is standard. |
'relu'
|
normalize_sae_decoder |
bool
|
Whether to normalize the SAE decoder. Unit normed decoder weights used to be preferred. |
True
|
noise_scale |
float
|
Using noise to induce sparsity is supported but not recommended. |
0.0
|
from_pretrained_path |
str
|
The path to a pretrained SAE. We can finetune an existing SAE if needed. |
None
|
apply_b_dec_to_input |
bool
|
Whether to apply the decoder bias to the input. Not currently advised. |
True
|
decoder_orthogonal_init |
bool
|
Whether to use orthogonal initialization for the decoder. Not currently advised. |
False
|
decoder_heuristic_init |
bool
|
Whether to use heuristic initialization for the decoder. See Anthropic April Update. |
False
|
init_encoder_as_decoder_transpose |
bool
|
Whether to initialize the encoder as the transpose of the decoder. See Anthropic April Update. |
False
|
n_batches_in_buffer |
int
|
The number of batches in the buffer. When not using cached activations, a buffer in ram is used. The larger it is, the better shuffled the activations will be. |
20
|
training_tokens |
int
|
The number of training tokens. |
2000000
|
finetuning_tokens |
int
|
The number of finetuning tokens. See here |
0
|
store_batch_size_prompts |
int
|
The batch size for storing activations. This controls how many prompts are in the batch of the language model when generating actiations. |
32
|
train_batch_size_tokens |
int
|
The batch size for training. This controls the batch size of the SAE Training loop. |
4096
|
normalize_activations |
str
|
Activation Normalization Strategy. Either none, expected_average_only_in (estimate the average activation norm and divide activations by it following Antrhopic April update -> this can be folded post training and set to None), or constant_norm_rescale (at runtime set activation norm to sqrt(d_in) and then scale up the SAE output). |
'none'
|
seqpos_slice |
tuple
|
Determines slicing of activations when constructing batches during training. The slice should be (start_pos, end_pos, optional[step_size]), e.g. for Othello we sometimes use (5, -5). Note, step_size > 0. |
(None)
|
device |
str
|
The device to use. Usually cuda. |
'cpu'
|
act_store_device |
str
|
The device to use for the activation store. CPU is advised in order to save vram. |
'with_model'
|
seed |
int
|
The seed to use. |
42
|
dtype |
str
|
The data type to use. |
'float32'
|
prepend_bos |
bool
|
Whether to prepend the beginning of sequence token. You should use whatever the model was trained with. |
True
|
jumprelu_init_threshold |
float
|
The threshold to initialize for training JumpReLU SAEs. |
0.001
|
jumprelu_bandwidth |
float
|
Bandwidth for training JumpReLU SAEs. |
0.001
|
autocast |
bool
|
Whether to use autocast during training. Saves vram. |
False
|
autocast_lm |
bool
|
Whether to use autocast during activation fetching. |
False
|
compile_llm |
bool
|
Whether to compile the LLM. |
False
|
llm_compilation_mode |
str
|
The compilation mode to use for the LLM. |
None
|
compile_sae |
bool
|
Whether to compile the SAE. |
False
|
sae_compilation_mode |
str
|
The compilation mode to use for the SAE. |
None
|
adam_beta1 |
float
|
The beta1 parameter for Adam. |
0
|
adam_beta2 |
float
|
The beta2 parameter for Adam. |
0.999
|
mse_loss_normalization |
str
|
The normalization to use for the MSE loss. |
None
|
l1_coefficient |
float
|
The L1 coefficient. |
0.001
|
lp_norm |
float
|
The Lp norm. |
1
|
scale_sparsity_penalty_by_decoder_norm |
bool
|
Whether to scale the sparsity penalty by the decoder norm. |
False
|
l1_warm_up_steps |
int
|
The number of warm-up steps for the L1 loss. |
0
|
lr |
float
|
The learning rate. |
0.0003
|
lr_scheduler_name |
str
|
The name of the learning rate scheduler to use. |
'constant'
|
lr_warm_up_steps |
int
|
The number of warm-up steps for the learning rate. |
0
|
lr_end |
float
|
The end learning rate if lr_decay_steps is set. Default is lr / 10. |
None
|
lr_decay_steps |
int
|
The number of decay steps for the learning rate. |
0
|
n_restart_cycles |
int
|
The number of restart cycles for the cosine annealing warm restarts scheduler. |
1
|
finetuning_method |
str
|
The method to use for finetuning. |
None
|
use_ghost_grads |
bool
|
Whether to use ghost gradients. |
False
|
feature_sampling_window |
int
|
The feature sampling window. |
2000
|
dead_feature_window |
int
|
The dead feature window. |
1000
|
dead_feature_threshold |
float
|
The dead feature threshold. |
1e-08
|
n_eval_batches |
int
|
The number of evaluation batches. |
10
|
eval_batch_size_prompts |
int
|
The batch size for evaluation. |
None
|
log_to_wandb |
bool
|
Whether to log to Weights & Biases. |
True
|
log_activations_store_to_wandb |
bool
|
NOT CURRENTLY USED. Whether to log the activations store to Weights & Biases. |
False
|
log_optimizer_state_to_wandb |
bool
|
NOT CURRENTLY USED. Whether to log the optimizer state to Weights & Biases. |
False
|
wandb_project |
str
|
The Weights & Biases project to log to. |
'mats_sae_training_language_model'
|
wandb_id |
str
|
The Weights & Biases ID. |
None
|
run_name |
str
|
The name of the run. |
None
|
wandb_entity |
str
|
The Weights & Biases entity. |
None
|
wandb_log_frequency |
int
|
The frequency to log to Weights & Biases. |
10
|
eval_every_n_wandb_logs |
int
|
The frequency to evaluate. |
100
|
resume |
bool
|
Whether to resume training. |
False
|
n_checkpoints |
int
|
The number of checkpoints. |
0
|
checkpoint_path |
str
|
The path to save checkpoints. |
'checkpoints'
|
verbose |
bool
|
Whether to print verbose output. |
True
|
model_kwargs |
dict[str, Any]
|
Additional keyword arguments for the model. |
dict()
|
model_from_pretrained_kwargs |
dict[str, Any]
|
Additional keyword arguments for the model from pretrained. |
None
|
Source code in sae_lens/config.py
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 |
|
PretokenizeRunner
Runner to pretokenize a dataset using a given tokenizer, and optionally upload to Huggingface.
Source code in sae_lens/pretokenize_runner.py
run()
Load the dataset, tokenize it, and save it to disk and/or upload to Huggingface.
Source code in sae_lens/pretokenize_runner.py
SAE
Bases: HookedRootModule
Core Sparse Autoencoder (SAE) class used for inference. For training, see TrainingSAE
.
Source code in sae_lens/sae.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
|
decode(feature_acts)
Decodes SAE feature activation tensor into a reconstructed input activation tensor.
Source code in sae_lens/sae.py
encode_jumprelu(x)
Calculate SAE features from inputs
Source code in sae_lens/sae.py
encode_standard(x)
Calculate SAE features from inputs
Source code in sae_lens/sae.py
from_pretrained(release, sae_id, device='cpu')
classmethod
Load a pretrained SAE from the Hugging Face model hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
release |
str
|
The release name. This will be mapped to a huggingface repo id based on the pretrained_saes.yaml file. |
required |
id |
The id of the SAE to load. This will be mapped to a path in the huggingface repo. |
required | |
device |
str
|
The device to load the SAE on. |
'cpu'
|
return_sparsity_if_present |
If True, will return the log sparsity tensor if it is present in the model directory in the Hugging Face model hub. |
required |
Source code in sae_lens/sae.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
|
SAETrainingRunner
Class to run the training of a Sparse Autoencoder (SAE) on a TransformerLens model.
Source code in sae_lens/sae_training_runner.py
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 |
|
run()
Run the training of the SAE.
Source code in sae_lens/sae_training_runner.py
TrainingSAE
Bases: SAE
A SAE used for training. This class provides a training_forward_pass
method which calculates
losses used for training.
Source code in sae_lens/training/training_sae.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
|
encode_standard(x)
Calcuate SAE features from inputs
initialize_decoder_norm_constant_norm(norm=0.1)
A heuristic proceedure inspired by: https://transformer-circuits.pub/2024/april-update/index.html#training-saes
Source code in sae_lens/training/training_sae.py
initialize_weights_complex()
Source code in sae_lens/training/training_sae.py
remove_gradient_parallel_to_decoder_directions()
Update grads so that they remove the parallel component (d_sae, d_in) shape