Skip to content

Commit

Permalink
feat: Change eval batch size (#128)
Browse files Browse the repository at this point in the history
* Surface # of eval batches and # of eval sequences

* fix formatting

* fix print statement accidentally left in
  • Loading branch information
tomMcGrath authored May 8, 2024
1 parent ad9418c commit 758a50b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions sae_lens/training/activations_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ def dataloader(self) -> Iterator[Any]:
self._dataloader = self.get_data_loader()
return self._dataloader

def get_batch_tokens(self):
def get_batch_tokens(self, batch_size: int | None = None):
"""
Streams a batch of tokens from a dataset.
"""

batch_size = self.store_batch_size
if not batch_size:
batch_size = self.store_batch_size
context_size = self.context_size
device = self.device

Expand Down
4 changes: 4 additions & 0 deletions sae_lens/training/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class LanguageModelSAERunnerConfig:

dead_feature_threshold: float = 1e-8

# Evals
n_eval_batches: int = 10
n_eval_seqs: int | None = None # useful if evals cause OOM

# WANDB
log_to_wandb: bool = True
log_activations_store_to_wandb: bool = False
Expand Down
10 changes: 7 additions & 3 deletions sae_lens/training/evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def run_evals(
model: HookedRootModule,
n_training_steps: int,
suffix: str = "",
n_eval_batches: int = 10,
n_eval_seqs: int | None = None,
) -> Mapping[str, Any]:
hook_point = sparse_autoencoder.cfg.hook_point
hook_point_layer = sparse_autoencoder.hook_point_layer
Expand All @@ -25,14 +27,15 @@ def run_evals(
layer=hook_point_layer
)
### Evals
eval_tokens = activation_store.get_batch_tokens()
eval_tokens = activation_store.get_batch_tokens(n_eval_seqs)

# Get Reconstruction Score
losses_df = recons_loss_batched(
sparse_autoencoder,
model,
activation_store,
n_batches=10,
n_batches=n_eval_batches,
n_eval_seqs=n_eval_seqs,
)

recons_score = losses_df["score"].mean()
Expand Down Expand Up @@ -100,10 +103,11 @@ def recons_loss_batched(
model: HookedRootModule,
activation_store: ActivationsStore,
n_batches: int = 100,
n_eval_seqs: int | None = None,
):
losses = []
for _ in range(n_batches):
batch_tokens = activation_store.get_batch_tokens()
batch_tokens = activation_store.get_batch_tokens(n_eval_seqs)
score, loss, recons_loss, zero_abl_loss = get_recons_loss(
sparse_autoencoder, model, batch_tokens
)
Expand Down
2 changes: 2 additions & 0 deletions sae_lens/training/lm_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def language_model_sae_runner(cfg: LanguageModelSAERunnerConfig):
wandb_log_frequency=cfg.wandb_log_frequency,
eval_every_n_wandb_logs=cfg.eval_every_n_wandb_logs,
autocast=cfg.autocast,
n_eval_batches=cfg.n_eval_batches,
n_eval_seqs=cfg.n_eval_seqs,
).sae_group

if cfg.log_to_wandb:
Expand Down
8 changes: 8 additions & 0 deletions sae_lens/training/train_sae_on_language_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def train_sae_on_language_model(
wandb_log_frequency: int = 50,
eval_every_n_wandb_logs: int = 100,
autocast: bool = False,
n_eval_batches: int = 10,
n_eval_seqs: int | None = None,
) -> SparseAutoencoderDictionary:
"""
@deprecated Use `train_sae_group_on_language_model` instead. This method is kept for backward compatibility.
Expand All @@ -203,6 +205,8 @@ def train_sae_on_language_model(
wandb_log_frequency=wandb_log_frequency,
eval_every_n_wandb_logs=eval_every_n_wandb_logs,
autocast=autocast,
n_eval_batches=n_eval_batches,
n_eval_seqs=n_eval_seqs,
).sae_group


Expand All @@ -223,6 +227,8 @@ def train_sae_group_on_language_model(
wandb_log_frequency: int = 50,
eval_every_n_wandb_logs: int = 100,
autocast: bool = False,
n_eval_batches: int = 10,
n_eval_seqs: int | None = None,
) -> TrainSAEGroupOutput:
total_training_tokens = get_total_training_tokens(sae_group=sae_group)
_update_sae_lens_training_version(sae_group)
Expand Down Expand Up @@ -325,6 +331,8 @@ def interrupt_callback(sig_num: Any, stack_frame: Any):
model,
training_run_state.n_training_steps,
suffix=wandb_suffix,
n_eval_batches=n_eval_batches,
n_eval_seqs=n_eval_seqs,
)
sparse_autoencoder.train()

Expand Down

0 comments on commit 758a50b

Please sign in to comment.