Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully handle slashes in model filename for autointerp #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sae_bench/evals/autointerp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def str_bool(b: bool) -> str:
return "Y" if b else ""


def escape_slash(s: str) -> str:
return s.replace("/", "_")


class Example:
"""
Data for a single example sequence.
Expand Down Expand Up @@ -523,7 +527,7 @@ def run_eval_single_sae(

os.makedirs(artifacts_folder, exist_ok=True)

tokens_filename = f"{config.model_name}_{config.total_tokens}_tokens_{config.llm_context_size}_ctx.pt"
tokens_filename = f"{escape_slash(config.model_name)}_{config.total_tokens}_tokens_{config.llm_context_size}_ctx.pt"
tokens_path = os.path.join(artifacts_folder, tokens_filename)

if os.path.exists(tokens_path):
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import torch
from sae_lens import SAE
from transformer_lens import HookedTransformer
from transformers import GPT2LMHeadModel
Expand All @@ -21,6 +22,15 @@ def gpt2_l4_sae() -> SAE:
)[0]


@pytest.fixture
def gpt2_l4_sae_sparsity() -> torch.Tensor:
sparsity = SAE.from_pretrained(
"gpt2-small-res-jb", "blocks.4.hook_resid_pre", device="cpu"
)[2]
assert sparsity is not None
return sparsity


@pytest.fixture
def gpt2_l5_sae() -> SAE:
return SAE.from_pretrained(
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/evals/autointerp/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from pathlib import Path
from unittest.mock import patch

import torch
from sae_lens import SAE
from transformer_lens import HookedTransformer

from sae_bench.evals.autointerp.eval_config import AutoInterpEvalConfig
from sae_bench.evals.autointerp.main import run_eval_single_sae


def test_run_eval_single_sae_saves_tokens_to_artifacts_folder(
gpt2_l4_sae: SAE,
gpt2_model: HookedTransformer,
gpt2_l4_sae_sparsity: torch.Tensor,
tmp_path: Path,
):
artifacts_folder = tmp_path / "artifacts"

config = AutoInterpEvalConfig(
model_name="gpt2",
dataset_name="roneneldan/TinyStories",
n_latents=2,
total_tokens=255,
llm_context_size=128,
)
with patch("sae_bench.evals.autointerp.main.AutoInterp.run", return_value={}):
run_eval_single_sae(
config=config,
sae=gpt2_l4_sae,
model=gpt2_model,
device="cpu",
artifacts_folder=str(artifacts_folder),
api_key="fake_api_key",
sae_sparsity=gpt2_l4_sae_sparsity,
)

assert (artifacts_folder / "gpt2_255_tokens_128_ctx.pt").exists()
tokenized_dataset = torch.load(artifacts_folder / "gpt2_255_tokens_128_ctx.pt")
assert tokenized_dataset.shape == (2, 128)


def test_run_eval_single_sae_saves_handles_slash_in_model_name(
gpt2_l4_sae: SAE,
gpt2_model: HookedTransformer,
gpt2_l4_sae_sparsity: torch.Tensor,
tmp_path: Path,
):
artifacts_folder = tmp_path / "artifacts"

config = AutoInterpEvalConfig(
model_name="openai/gpt2",
dataset_name="roneneldan/TinyStories",
n_latents=2,
total_tokens=255,
llm_context_size=128,
)
with patch("sae_bench.evals.autointerp.main.AutoInterp.run", return_value={}):
run_eval_single_sae(
config=config,
sae=gpt2_l4_sae,
model=gpt2_model,
device="cpu",
artifacts_folder=str(artifacts_folder),
api_key="fake_api_key",
sae_sparsity=gpt2_l4_sae_sparsity,
)

assert (artifacts_folder / "openai_gpt2_255_tokens_128_ctx.pt").exists()
Loading