"""Observed baseline work counters: hand-computed values, explicit unknowns, hook removal.""" import json import pytest import torch from obliteratus.abliterate import AbliterationPipeline from obliteratus.analysis.work_counters import KEYS, NOT_TORCH, UNOBSERVED, WorkCounters from obliteratus.concepts.baseline import run_baseline from tests.test_concept_baseline import synthetic_pack as synthetic_pack pytestmark = pytest.mark.cpu class Tiny(torch.nn.Module): def __init__(self): super().__init__() self.embed = torch.nn.Embedding(8, 2) def get_input_embeddings(self): return self.embed def forward(self, input_ids=None, attention_mask=None, inputs_embeds=None): return self.embed(input_ids) if input_ids is not None else inputs_embeds def generate(self, input_ids=None, attention_mask=None, steps=2): for _ in range(steps): self(input_ids=input_ids[:, -1:]) input_ids = torch.cat([input_ids, input_ids[:, -1:]], dim=1) return input_ids class Owner: def __init__(self, model): self.model = model self._validation_readouts = {} def _collect_activations(self, layers, prompts, label, **kwargs): ids = torch.tensor([[1, 2, 3]]) self.model(input_ids=ids, attention_mask=torch.ones_like(ids)) self._validation_readouts[label] = {"mean_pool": {0: [torch.zeros(1, 4)]}} return {0: [torch.zeros(1, 4, dtype=torch.float64)]} def staged(counters, action): counters.begin() action() return counters.end() def test_collection_generation_and_scope_counts(): model = Tiny() owner = Owner(model) counters = WorkCounters() ids = torch.tensor([[0, 4, 5], [4, 5, 6]]) mask = torch.tensor([[0, 1, 1], [1, 1, 1]]) with counters.observe(model, owner): values, reasons = staged(counters, lambda: ( owner._collect_activations(None, [], "harmful"), owner._collect_activations(None, [], "harmful"))) assert values == {"unique_examples": 1, "token_presentations": 6, "forwards": 2, "generated_tokens": 0, "tensor_bytes": 2 * (32 + 16)} assert set(reasons.values()) == {None} values, reasons = staged(counters, lambda: model.generate(input_ids=ids[1:])) assert values["generated_tokens"] == 2 and values["forwards"] == 2 assert values["unique_examples"] == 1 assert values["token_presentations"] is values["tensor_bytes"] is None assert "decode" in reasons["token_presentations"] assert "outside activation collection" in reasons["tensor_bytes"] values, reasons = staged(counters, lambda: model(input_ids=ids, attention_mask=mask)) assert values["unique_examples"] == 2 assert values["token_presentations"] == 5 totals, total_reasons = counters.totals() assert totals["forwards"] == 5 and totals["generated_tokens"] == 2 assert totals["unique_examples"] == 3 # [1,2,3], [4,5,6] and [4,5] are distinct. assert totals["token_presentations"] is None assert total_reasons["token_presentations"].startswith("an observed stage is unknown") assert model._forward_pre_hooks == {} and model.embed._forward_pre_hooks == {} assert "generate" not in vars(model) and "_collect_activations" not in vars(owner) @pytest.mark.parametrize("call,key,reason", [ (lambda m: m(input_ids=torch.tensor([[1, 2]])), "token_presentations", "attention_mask"), (lambda m: m(inputs_embeds=torch.zeros(1, 2, 2)), "unique_examples", "input_ids"), (lambda m: m.embed(torch.tensor([[1]])), "forwards", "bypassed"), (lambda m: m.generate(input_ids=torch.tensor([[1], [2]])), "generated_tokens", "batched"), ]) def test_unobservable_work_is_null_with_reason(call, key, reason): model, counters = Tiny(), WorkCounters() with counters.observe(model, Owner(model)): values, reasons = staged(counters, lambda: call(model)) assert values[key] is None and reason in reasons[key] def test_unobserved_and_non_torch_models_report_reasons(): counters = WorkCounters() values, reasons = staged(counters, lambda: None) assert set(values.values()) == {None} and set(reasons.values()) == {UNOBSERVED} assert counters.totals() == (dict.fromkeys(KEYS), dict.fromkeys(KEYS, UNOBSERVED)) owner = Owner(object()) with counters.observe(object(), owner): values, reasons = staged(counters, lambda: None) assert set(values.values()) == {None} and set(reasons.values()) == {NOT_TORCH} def test_existing_instance_attributes_restored_after_error(): model, counters = Tiny(), WorkCounters() owner = Owner(model) original = owner._collect_activations owner._collect_activations = original with pytest.raises(RuntimeError, match="injected"): with counters.observe(model, owner): assert owner._collect_activations is not original raise RuntimeError("injected") assert owner._collect_activations is original and not counters.observing assert model._forward_pre_hooks == {} def _run(tmp_path, synthetic_pack, name, **kwargs): from tests.fixtures.tiny_offline_model import build_tiny_offline_model source, payload = synthetic_pack model = tmp_path / "model" if not model.exists(): build_tiny_offline_model(model) return run_baseline(source, model, tmp_path / name, cache_dir=tmp_path / "cache", max_seq_length=8, fetcher=lambda url, limit: payload.read_bytes(), **kwargs) @pytest.fixture def one_thread(): previous = torch.get_num_threads() torch.set_num_threads(1) yield torch.set_num_threads(previous) ARTIFACTS = ("direction.json", "direction.safetensors", "validation.json") @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime", "one_thread") def test_replay_counters_match_hand_values_and_outputs(tmp_path, synthetic_pack): plain = _run(tmp_path, synthetic_pack, "plain") profiled = _run(tmp_path, synthetic_pack, "profiled", profile=True) assert sorted(path.name for path in plain.iterdir()) == sorted(ARTIFACTS) for name in ARTIFACTS: assert (plain / name).read_bytes() == (profiled / name).read_bytes() report = json.loads((profiled / "cost-profile.json").read_text()) # 8 positive + 8 control prompts, each three fixture words after the readout # suffix; four distinct texts per set; one 16-wide layer; float32 rows plus # three auxiliary readouts (final_token, mean_pool, masked) per prompt. expected = {"unique_examples": 2 * 4, "token_presentations": 16 * 3, "forwards": 16, "generated_tokens": 0, "tensor_bytes": (16 + 3 * 16) * 16 * 4} assert report["counters"] == expected assert set(report["counter_reasons"].values()) == {None} assert "top-level model" in report["counter_scope"] stages = {row["name"]: row for row in report["stages"]} assert stages["probe"]["counters"] == expected for name in ("resolve_fetch", "setup", "load"): assert set(stages[name]["counters"].values()) == {None} assert set(stages[name]["counter_reasons"].values()) == {UNOBSERVED} for name in ("distill", "gate", "save", "cleanup"): assert set(stages[name]["counters"].values()) == {0} @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime", "one_thread") def test_replay_hooks_removed_after_injected_failure(tmp_path, synthetic_pack, monkeypatch): seen = {} original = AbliterationPipeline._probe def failing(pipeline): seen.update(model=pipeline.handle.model, pipeline=pipeline) assert pipeline.handle.model._forward_pre_hooks original(pipeline) raise RuntimeError("injected probe failure") monkeypatch.setattr(AbliterationPipeline, "_probe", failing) with pytest.raises(RuntimeError, match="injected"): _run(tmp_path, synthetic_pack, "out", profile=True) model = seen["model"] assert model._forward_pre_hooks == {} assert model.get_input_embeddings()._forward_pre_hooks == {} assert "generate" not in vars(model) assert "_collect_activations" not in vars(seen["pipeline"]) report = json.loads((tmp_path / "out" / "cost-profile.json").read_text()) probe = next(row for row in report["stages"] if row["name"] == "probe") assert probe["status"] == "failure" and probe["counters"]["forwards"] == 16