|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import torch |
| 5 | +from sae_lens import SAE |
| 6 | +from transformer_lens import HookedTransformer |
| 7 | + |
| 8 | +from sae_bench.evals.autointerp.eval_config import AutoInterpEvalConfig |
| 9 | +from sae_bench.evals.autointerp.main import run_eval_single_sae |
| 10 | + |
| 11 | + |
| 12 | +def test_run_eval_single_sae_saves_tokens_to_artifacts_folder( |
| 13 | + gpt2_l4_sae: SAE, |
| 14 | + gpt2_model: HookedTransformer, |
| 15 | + gpt2_l4_sae_sparsity: torch.Tensor, |
| 16 | + tmp_path: Path, |
| 17 | +): |
| 18 | + artifacts_folder = tmp_path / "artifacts" |
| 19 | + |
| 20 | + config = AutoInterpEvalConfig( |
| 21 | + model_name="gpt2", |
| 22 | + dataset_name="roneneldan/TinyStories", |
| 23 | + n_latents=2, |
| 24 | + total_tokens=255, |
| 25 | + llm_context_size=128, |
| 26 | + ) |
| 27 | + with patch("sae_bench.evals.autointerp.main.AutoInterp.run", return_value={}): |
| 28 | + run_eval_single_sae( |
| 29 | + config=config, |
| 30 | + sae=gpt2_l4_sae, |
| 31 | + model=gpt2_model, |
| 32 | + device="cpu", |
| 33 | + artifacts_folder=str(artifacts_folder), |
| 34 | + api_key="fake_api_key", |
| 35 | + sae_sparsity=gpt2_l4_sae_sparsity, |
| 36 | + ) |
| 37 | + |
| 38 | + assert (artifacts_folder / "gpt2_255_tokens_128_ctx.pt").exists() |
| 39 | + tokenized_dataset = torch.load(artifacts_folder / "gpt2_255_tokens_128_ctx.pt") |
| 40 | + assert tokenized_dataset.shape == (2, 128) |
| 41 | + |
| 42 | + |
| 43 | +def test_run_eval_single_sae_saves_handles_slash_in_model_name( |
| 44 | + gpt2_l4_sae: SAE, |
| 45 | + gpt2_model: HookedTransformer, |
| 46 | + gpt2_l4_sae_sparsity: torch.Tensor, |
| 47 | + tmp_path: Path, |
| 48 | +): |
| 49 | + artifacts_folder = tmp_path / "artifacts" |
| 50 | + |
| 51 | + config = AutoInterpEvalConfig( |
| 52 | + model_name="openai/gpt2", |
| 53 | + dataset_name="roneneldan/TinyStories", |
| 54 | + n_latents=2, |
| 55 | + total_tokens=255, |
| 56 | + llm_context_size=128, |
| 57 | + ) |
| 58 | + with patch("sae_bench.evals.autointerp.main.AutoInterp.run", return_value={}): |
| 59 | + run_eval_single_sae( |
| 60 | + config=config, |
| 61 | + sae=gpt2_l4_sae, |
| 62 | + model=gpt2_model, |
| 63 | + device="cpu", |
| 64 | + artifacts_folder=str(artifacts_folder), |
| 65 | + api_key="fake_api_key", |
| 66 | + sae_sparsity=gpt2_l4_sae_sparsity, |
| 67 | + ) |
| 68 | + |
| 69 | + assert (artifacts_folder / "openai_gpt2_255_tokens_128_ctx.pt").exists() |
0 commit comments