Skip to content
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
1 change: 1 addition & 0 deletions delphi/scorers/classifier/intruder.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def _prepare_and_batch(self, record: LatentRecord) -> list[IntruderSentence]:
n_incorrect=n_incorrect,
threshold=0.3,
highlighted=True,
rng=self.rng,
)

elif self.type == "internal":
Expand Down
39 changes: 38 additions & 1 deletion tests/test_scorers/test_intruder_example_embedding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import random

import torch

from delphi.clients.client import Client, Response
Expand Down Expand Up @@ -37,7 +39,7 @@ def _make_non_activating_example(token: str, distance: float) -> NonActivatingEx


def test_intruder_prepare_and_batch_returns_string_examples(monkeypatch):
def fake_prepare_text(example, n_incorrect, threshold, highlighted):
def fake_prepare_text(example, n_incorrect, threshold, highlighted, rng=None):
return f"fmt-{example.str_tokens[0]}-{n_incorrect}", example.str_tokens

monkeypatch.setattr(
Expand Down Expand Up @@ -90,6 +92,41 @@ def test_intruder_internal_takes_intruder_from_another_quantile():
assert batch.intruder_distance != batch.chosen_quantile


def test_intruder_default_batches_are_reproducible_with_seed():
def make_record() -> LatentRecord:
return LatentRecord(
latent=Latent(module_name="layers.0", latent_index=0),
test=[
ActivatingExample(
tokens=torch.arange(16),
activations=torch.eye(16)[i],
str_tokens=list("abcdefghijklmnop"),
quantile=0,
)
for i in range(3)
],
not_active=[
NonActivatingExample(
tokens=torch.arange(16),
activations=torch.zeros(16),
str_tokens=list("ABCDEFGHIJKLMNOP"),
distance=-1.0,
)
for _ in range(4)
],
)

def build_batches(global_seed: int) -> list[list[str]]:
# other code may use the global random module between runs
random.seed(global_seed)
scorer = IntruderScorer(
DummyClient(), n_examples_shown=4, type="default", seed=42
)
return [batch.examples for batch in scorer._prepare_and_batch(make_record())]

assert build_batches(global_seed=1) == build_batches(global_seed=2)


def test_example_embedding_internal_batch_creation(monkeypatch):
def fake_prepare_text(example, n_incorrect, threshold, highlighted):
return f"fmt-{example.str_tokens[0]}-{n_incorrect}", example.str_tokens
Expand Down
Loading