"""Entry-point contracts; model tests use only the synthetic offline fixture.""" from copy import deepcopy from dataclasses import replace import hashlib import json from pathlib import Path from types import SimpleNamespace from jsonschema import Draft202012Validator, ValidationError import pytest import torch import yaml from obliteratus.abliterate import AbliterationPipeline, get_layer_modules from obliteratus.concepts import ConceptDirection, ConceptTarget, EntryPoint, ExtractionRecipe from obliteratus.concepts.entry_points import RenderedPrompt, render_entry_point, span_token_mask from obliteratus.concepts.extraction import extract_directions, sample_folds from obliteratus.concepts.gate import ActivationCache, _fold_indices, validate_direction from obliteratus.concepts.pack import ConceptPack, builtin_refusal_pack from obliteratus.concepts.selectors import select_records from obliteratus.concepts.validation import PackError, SCHEMA_PATH, validate_manifest from tests.fixtures.tiny_offline_model import build_tiny_offline_model pytestmark = pytest.mark.cpu FIXTURES = Path(__file__).parent / "fixtures/concept_packs" LEGACY_RECIPE = { "method": "diff_means", "n_directions": 1, "pool_controls": "mean_of_means", "readout": {"position": "final_token", "suffix": ""}, "denoise": None, "layer_select": {"strategy": "preset"}, "intervention_layer": {"rule": "same"}, "use_chat_template": None, } def manifest(): return yaml.safe_load((FIXTURES / "valid_minimal/manifest.yaml").read_text()) @pytest.mark.parametrize("version,entry", [("1.0", None), ("1.1", None), ("1.1", { "messages": [{"role": "system", "content": "Describe {person}"}], "template": "{text} {{concept}}{word}{{/concept}}", "prefill": "I feel:", "readout": "span:concept", "group_by": "group", })]) def test_schema_and_runtime_accept_v1_and_entry_points(version, entry): value = manifest() value["schema_version"] = version if entry is not None: value["recipe"]["entry_point"] = entry validate_manifest(value) Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(value) recipe = ExtractionRecipe(**value["recipe"]) if entry is None: assert recipe.entry_point is None else: assert isinstance(recipe.entry_point, EntryPoint) @pytest.mark.parametrize("recipe,version", [ ({"typo": True}, "1.0"), ({"entry_piont": {}}, "1.1"), ({"entry_point": {"prefil": "answer"}}, "1.1"), ({"entry_point": {"readout": "last_k:0"}}, "1.1"), ({"entry_point": {"readout": "generated:5"}}, "1.1"), ({"entry_point": {"messages": [{"role": "system", "content": "x", "typo": 1}]}}, "1.1"), ({"entry_point": {}}, "1.0"), ({"readout": {"postion": "final_token"}}, "1.0"), ({"layer_select": {"fold": 5}}, "1.1"), ]) def test_schema_and_runtime_reject_unknown_or_invalid_fields(recipe, version): value = manifest() value.update(recipe=recipe, schema_version=version) with pytest.raises(PackError): validate_manifest(value) with pytest.raises(ValidationError): Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(value) def test_unknown_recipe_key_names_error_location(): value = manifest() value["recipe"]["entry_piont"] = {} with pytest.raises(PackError, match=r"manifest.recipe: unknown field entry_piont"): validate_manifest(value) with pytest.raises(ValueError, match="prefil"): ExtractionRecipe(entry_point={"prefil": "answer"}) def test_structured_context_templates_prefill_and_fallback(): entry = EntryPoint( messages=[{"role": "system", "content": "Speak as {person}"}, {"role": "user", "content": "hello"}, {"role": "assistant", "content": "world"}], template="Observe {text}", prefill=" I feel:", ) record = {"text": "safe", "person": "1st"} calls = [] def chat(messages, **kwargs): calls.append((messages, kwargs)) return "|".join(m["role"] + ":" + m["content"] for m in messages) + "|assistant:" tokenizer = SimpleNamespace(chat_template="fixture", apply_chat_template=chat) rendered = render_entry_point(entry, record, tokenizer, use_chat_template=False) assert rendered == "system:Speak as 1st|user:hello|assistant:world|user:Observe safe|assistant: I feel:" assert not rendered.fallback assert calls[0][1] == {"tokenize": False, "add_generation_prompt": True, "enable_thinking": False} fallback = render_entry_point(entry, record, SimpleNamespace(), use_chat_template=False) assert fallback == "Speak as 1st\nhello\nworld\nObserve safe I feel:" assert fallback.fallback plain = render_entry_point(EntryPoint(template="{text} I feel:"), record, SimpleNamespace(), use_chat_template=False) assert plain == "safe I feel:" assert not plain.fallback @pytest.mark.parametrize("template,match", [ ("{missing}", "missing"), ("{text.__class__}", "only"), ("{text[0]}", "only"), ("{text!r}", "only"), ("{{x}}{text}", "Unclosed"), ("{{x}}{{/x}}", "empty"), ("{{x}}{{y}}text{{/y}}{{/x}}", "Nested"), ]) def test_template_errors_are_explicit(template, match): with pytest.raises(ValueError, match=match): render_entry_point(EntryPoint(template=template), {"text": "hello"}, SimpleNamespace(), use_chat_template=False) def test_spans_remove_markers_and_keep_record_braces_literal(): rendered = render_entry_point( EntryPoint(template="{text} {{word}}hello world{{/word}}", readout="span:word"), {"text": "{{record}}"}, SimpleNamespace(), use_chat_template=False, ) assert rendered == "{{record}} hello world" assert rendered[rendered.span[0]:rendered.span[1]] == "hello world" literal = render_entry_point(EntryPoint(spans={"word": "world"}, readout="span:word"), {"text": "hello world"}, SimpleNamespace(), use_chat_template=False) assert literal.span == (6, 11) for text in ("hello", "world world"): with pytest.raises(ValueError, match="exactly once"): render_entry_point(EntryPoint(spans={"word": "world"}, readout="span:word"), {"text": text}, SimpleNamespace(), use_chat_template=False) def test_selector_and_pack_keep_annotations_and_variables(): source = [{"prompt": "hello", "person": "1st", "split": "heldout", "group": "g1", "set": "s1", "word": "world"}] records = select_records(json.dumps(source), { "format": "json", "select": "$", "text_field": "prompt", }, "fixture") assert records == [{**source[0], "id": "$:0", "text": "hello"}] pack = ConceptPack.load(FIXTURES / "valid_minimal") from obliteratus.concepts.pack_io import digest records = [{**records[0], "id": str(i)} for i in range(8)] raw = ("\n".join(json.dumps(record) for record in records) + "\n").encode() value = deepcopy(pack.manifest) value["sets"]["positive"].update(n=8, sha256=digest(raw)) files = {**pack.files, "positive.jsonl": raw, "manifest.yaml": yaml.safe_dump(value).encode()} target = ConceptPack(files).to_target() assert target.records["positive"] == records assert target.positive == ["hello"] * 8 def grouped_rows(): groups = {"positive": ["shared", "a", "a", "b", "b", "c", "d", "d"], "control:n": ["a", "shared", "shared", "b", "c", "c", "d", "d"]} return {name: len(values) for name, values in groups.items()}, groups def test_grouped_folds_never_split_groups_across_sets(): counts, groups = grouped_rows() folds = sample_folds(counts, 3, 42, groups) again = sample_folds(counts, 3, 42, groups) memberships = {} for name, parts in folds.items(): assert sorted(torch.cat(parts).tolist()) == list(range(counts[name])) for fold, rows in enumerate(parts): assert torch.equal(rows, again[name][fold]) assert len(rows) for row in rows.tolist(): group = groups[name][row] assert memberships.setdefault(group, fold) == fold for train, test in _fold_indices(counts, 3, 42, groups): training = {groups[n][i] for n, rows in train.items() for i in rows.tolist()} heldout = {groups[n][i] for n, rows in test.items() for i in rows.tolist()} assert training.isdisjoint(heldout) # Each set has five distinct groups: five folds fit, six cannot. assert all(len(rows) for parts in sample_folds(counts, 5, 42, groups).values() for rows in parts) with pytest.raises(ValueError, match="distinct group"): sample_folds(counts, 6, 42, groups) def test_legacy_folds_keep_exact_rng_order(): counts = {"positive": 8, "control:n": 11} generator = torch.Generator().manual_seed(42) expected = {n: torch.randperm(c, generator=generator).tensor_split(3) for n, c in counts.items()} actual = sample_folds(counts, 3, 42) for name in counts: assert all(torch.equal(a, b) for a, b in zip(actual[name], expected[name])) def test_grouped_folds_ignore_control_declaration_order(): groups = {"positive": list(range(8)), "control:z": [f"z{i}" for i in range(8)], "control:a": [f"a{i}" for i in range(8)]} counts = {name: len(rows) for name, rows in groups.items()} reordered = {name: counts[name] for name in ("positive", "control:a", "control:z")} expected = sample_folds(counts, 3, 42, groups) actual = sample_folds(reordered, 3, 42, {name: groups[name] for name in reordered}) for name in counts: assert all(torch.equal(a, b) for a, b in zip(actual[name], expected[name])) @pytest.mark.parametrize("legacy_readout", [False, True]) def test_extraction_and_heldout_validation_share_grouped_folds(monkeypatch, legacy_readout): groups = {"positive": list(range(8)), "control:z": [f"z{i}" for i in range(8)], "control:a": [f"a{i}" for i in range(8)]} counts = {name: len(rows) for name, rows in groups.items()} generator = torch.Generator().manual_seed(18) positive = {0: torch.randn(counts["positive"], 4, generator=generator)} controls = {name: {0: torch.randn(counts[f"control:{name}"], 4, generator=generator)} for name in ("z", "a")} recipe = ExtractionRecipe(entry_point=EntryPoint(group_by="family"), layer_select={"strategy": "cv_auc", "folds": 3}) if legacy_readout: recipe = replace(recipe, entry_point=None, layer_select={**recipe.layer_select, "group_by": "family"}) observed = [] def capture(counts, folds, seed, sample_groups=None): splits = sample_folds(counts, folds, seed, sample_groups) observed.append((list(counts), deepcopy(sample_groups), splits)) return splits monkeypatch.setattr("obliteratus.concepts.extraction.sample_folds", capture) direction = extract_directions(positive, controls, recipe, seed=42, sample_groups=groups, concept_id="user/grouped") report = validate_direction(direction, ActivationCache(positive, controls, sample_groups=groups), mode="report", random_directions=4) assert report["folds"] == 3 assert len(observed) == 3 # extraction, fixed-vector validation, recipe-refit validation assert observed[0][0] == ["positive", "control:z", "control:a"] assert observed[1][0] == ["positive", "control:a", "control:z"] for _, value, splits in observed: assert value == groups for name in counts: assert all(torch.equal(a, b) for a, b in zip(splits[name], observed[0][2][name])) with pytest.raises(ValueError, match="sample groups"): extract_directions(positive, controls, recipe) with pytest.raises(ValueError, match="sample groups"): validate_direction(direction, ActivationCache(positive, controls), random_directions=4) @pytest.mark.parametrize("entry,positive,control", [ (None, "Observe.", "Observe."), (EntryPoint(template="{word}"), "pain", "calm"), (EntryPoint(template="{{word}}{word}{{/word}}", readout="span:word"), "pain", "calm"), (EntryPoint(messages=[{"role": "system", "content": "{word}"}]), "pain\nObserve.", "calm\nObserve."), (EntryPoint(prefill=" {word}"), "Observe. pain", "Observe. calm"), ]) def test_entry_point_lexical_diagnostics_see_substituted_words(entry, positive, control, monkeypatch): from obliteratus.concepts.gate import _lexical_baseline target = ConceptTarget( "user/lexical", ["Observe."] * 8, {"n": ["Observe."] * 8}, recipe=ExtractionRecipe(entry_point=entry), records={"positive": [{"text": "Observe.", "word": "pain"} for _ in range(8)], "control:n": [{"text": "Observe.", "word": "calm"} for _ in range(8)]}, ) # Construction computes masking tokens before a tokenizer/model is loaded. pipeline = AbliterationPipeline("offline", concept=target, method="basic", device="cpu", gate_mode="report") monkeypatch.setattr(pipeline, "_check_entry_point_cache", lambda: None) chunks = {0: [torch.ones(8, 2)]} pipeline._harmful_acts = chunks pipeline._control_acts = {"n": chunks} cache = pipeline._concept_gate_inputs().materialize() assert cache.texts == ([positive] * 8, {"n": [control] * 8}) assert cache.top_tokens == pipeline._confound_tokens assert {record["token"] for record in cache.top_tokens} == ( {"pain", "calm"} if entry is not None else set() ) sizes = {"positive": 8, "control:n": 8} lexical = _lexical_baseline(cache.texts, sizes, _fold_indices(sizes, 2, 42), "mean_of_means") assert lexical["heldout_auc"] == (1.0 if entry is not None else 0.5) if entry is None: assert cache.texts[0] is pipeline.concept.positive assert cache.texts[1] is pipeline.concept.controls @pytest.mark.parametrize("change", [ {"template": "{text} answer"}, {"prefill": "answer"}, {"readout": "last_k:2"}, {"messages": [{"role": "system", "content": "hello"}]}, {"group_by": "family"}, {"spans": {"word": "hello"}}, {"special_token_policy": {"bos": "none"}}, ]) def test_every_entry_point_field_changes_identity(change): target = ConceptTarget("user/demo", ["hello"], {"n": ["world"]}, recipe=ExtractionRecipe(entry_point=EntryPoint())) changed = replace(target, recipe=replace(target.recipe, entry_point=replace(target.entry_point, **change))) assert changed.activation_identity() != target.activation_identity() annotated = replace(target, records={"positive": [{"text": "hello", "family": "new"}]}) assert annotated.activation_identity() != target.activation_identity() def test_direction_metadata_binds_entry_point_and_provenance(tmp_path): recipe = ExtractionRecipe(entry_point=EntryPoint(prefill="answer")) direction = ConceptDirection({0: torch.tensor([1., 0.])}, recipe=recipe, activation_identity={"plain_text_fallback_counts": {"harmful": 8}}) path = tmp_path / "direction.safetensors" direction.save(path) loaded = ConceptDirection.load(path) assert loaded.recipe == recipe assert loaded.activation_identity == direction.activation_identity metadata = json.loads(path.with_suffix(".json").read_text()) assert metadata["metadata_sha256"] metadata["recipe"]["entry_point"]["prefill"] = "changed" path.with_suffix(".json").write_text(json.dumps(metadata)) with pytest.raises(ValueError, match="metadata hash"): ConceptDirection.load(path) def test_direction_metadata_round_trip_with_integer_layer_keys(tmp_path): direction = ConceptDirection( {2: torch.tensor([1., 0.]), 10: torch.tensor([0., 1.])}, recipe=ExtractionRecipe(entry_point=EntryPoint(prefill="answer")), cv_scores={2: [0.5], 10: [0.7]}, validation={"layers": {2: {"auc": 0.5}, 10: {"auc": 0.7}}}, ) path = tmp_path / "direction.safetensors" direction.save(path) loaded = ConceptDirection.load(path) assert loaded.cv_scores == direction.cv_scores assert loaded.recipe == direction.recipe for layer in direction.directions: assert torch.equal(loaded.directions[layer], direction.directions[layer]) metadata = json.loads(path.with_suffix(".json").read_text()) metadata["cv_scores"]["10"] = [0.8] path.with_suffix(".json").write_text(json.dumps(metadata)) with pytest.raises(ValueError, match="metadata hash"): ConceptDirection.load(path) def test_direction_loads_pre_entry_point_release_format(tmp_path): from safetensors.torch import save_file # Frozen pre-#311 writer format: tensor hash only, no entry-point fields. path = tmp_path / "legacy.safetensors" tensors = {"directions.2": torch.tensor([1., 0.]), "directions.10": torch.tensor([0., 1.])} save_file(tensors, str(path)) metadata = { "schema_version": 1, "recipe": LEGACY_RECIPE, "direction_method_effective": "diff_means", "extraction_layers": [2, 10], "intervention_layers": [2, 10], "seed": 42, "model_id": "offline", "model_revision": None, "dtype": "float32", "pack_hash": None, "concept_id": "user/legacy", "concept_version": "1.0.0", "cv_scores": {2: [0.5], 10: [0.7]}, "sample_counts": {}, "validation": {}, "pack_provenance": {}, "tensor_sha256": hashlib.sha256(path.read_bytes()).hexdigest(), } path.with_suffix(".json").write_text(json.dumps(metadata, indent=2) + "\n") loaded = ConceptDirection.load(path) assert loaded.recipe.to_dict() == {**LEGACY_RECIPE, "collateral_margin": 0.03} assert loaded.cv_scores == {2: [0.5], 10: [0.7]} assert loaded.activation_identity == {} for layer in (2, 10): assert torch.equal(loaded.directions[layer], tensors[f"directions.{layer}"]) @pytest.fixture def identity_pipeline(): # Identity checks need neither a loaded model nor a tokenizer implementation. pipeline = AbliterationPipeline.__new__(AbliterationPipeline) pipeline.concept = ConceptTarget( "user/identity", ["hello"], {"n": ["world"]}, recipe=ExtractionRecipe(entry_point=EntryPoint()), ) pipeline.handle = SimpleNamespace( tokenizer=SimpleNamespace(chat_template={"default": "original"}, truncation_side="right", padding_side="right"), model=SimpleNamespace(config=SimpleNamespace(_commit_hash="revision")), hidden_size=4096, ) pipeline.model_name = "offline" pipeline.dtype = "float32" pipeline.use_chat_template = True pipeline.max_seq_length = None pipeline._entry_point_collection_limits = (1, 64, None) return pipeline @pytest.mark.parametrize("free_gb,expected_length", [(1., 64), (3., 128), (16., 256)]) def test_entry_point_probe_records_and_freezes_effective_limits( identity_pipeline, monkeypatch, free_gb, expected_length, ): pipeline = identity_pipeline available = [free_gb] monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: available[0]) monkeypatch.setattr("obliteratus.abliterate.dev.is_gpu_available", lambda: True) monkeypatch.setattr("obliteratus.abliterate.get_layer_modules", lambda handle: []) pipeline.harmful_prompts = pipeline.concept.positive pipeline.harmless_prompts = pipeline.concept.controls["n"] pipeline.per_expert_directions = False pipeline.use_jailbreak_contrast = False pipeline.shield_concept_count = 0 pipeline._gate_mode = "off" pipeline.log = lambda *args: None pipeline._emit = lambda *args, **kwargs: None pipeline._format_collection_prompts = lambda prompts, label: prompts observed = [] def collect(layers, prompts, label): observed.append(pipeline._resolved_activation_collection_limits(len(layers))[1]) available[0] = 32. # Memory changes must not change this probe's sizing. return {} pipeline._collect_activations = collect pipeline._probe() identity = pipeline._entry_point_activation_identity assert observed == [expected_length, expected_length] assert identity["max_seq_length"] == expected_length assert identity["requested_max_seq_length"] is None assert identity["padding_side"] == ("right" if free_gb == 1. else "left") assert pipeline._admitted_collection_limits is None pipeline._check_entry_point_cache() # Do not re-size against post-probe memory. pipeline.max_seq_length = 96 with pytest.raises(ValueError, match="collect activations again"): pipeline._check_entry_point_cache() pipeline._probe() assert observed[-2:] == [96, 96] assert pipeline._entry_point_activation_identity["max_seq_length"] == 96 assert pipeline._entry_point_activation_identity != identity def test_selector_drops_source_annotations_outside_pack_vocabulary(): source = [{"prompt": "A valid sample.", "split": "test", "person": "2nd", "word": "w"}] records = select_records(json.dumps(source), { "format": "json", "select": "$", "text_field": "prompt", }, "fixture") assert records == [{"id": "$:0", "text": "A valid sample.", "prompt": "A valid sample.", "word": "w"}] def test_entry_point_cache_rejects_changed_special_tokens(identity_pipeline): pipeline = identity_pipeline tokenizer = pipeline.handle.tokenizer tokenizer.special_tokens_map = {"bos_token": ""} pipeline._entry_point_activation_identity = pipeline._entry_point_cache_identity() pipeline._check_entry_point_cache() tokenizer.special_tokens_map = {"bos_token": ""} with pytest.raises(ValueError, match="collect activations again"): pipeline._check_entry_point_cache() @pytest.mark.parametrize("field", ["bos_token_id", "eos_token_id", "add_bos_token", "add_eos_token"]) def test_entry_point_cache_rejects_changed_token_policy(identity_pipeline, field): pipeline = identity_pipeline pipeline._entry_point_activation_identity = pipeline._entry_point_cache_identity() setattr(pipeline.handle.tokenizer, field, 1) with pytest.raises(ValueError, match="collect activations again"): pipeline._check_entry_point_cache() @pytest.mark.parametrize("side", ["truncation_side", "padding_side"]) def test_entry_point_cache_rejects_changed_tokenizer_side(identity_pipeline, side): pipeline = identity_pipeline pipeline._entry_point_activation_identity = pipeline._entry_point_cache_identity() assert pipeline._entry_point_activation_identity[side] == "right" setattr(pipeline.handle.tokenizer, side, "left") with pytest.raises(ValueError, match="collect activations again"): pipeline._check_entry_point_cache() def test_entry_point_cache_snapshots_mutable_chat_template(identity_pipeline): pipeline = identity_pipeline pipeline._entry_point_activation_identity = pipeline._entry_point_cache_identity() pipeline._check_entry_point_cache() pipeline.handle.tokenizer.chat_template["default"] = "changed" assert pipeline._entry_point_activation_identity["chat_template"] == {"default": "original"} with pytest.raises(ValueError, match="collect activations again"): pipeline._check_entry_point_cache() def test_entry_point_cache_requires_collection(identity_pipeline): with pytest.raises(ValueError, match="collect activations again"): identity_pipeline._check_entry_point_cache() @pytest.mark.parametrize("fixture", ["valid_minimal", "valid_full"]) def test_v1_recipe_configuration_bytes_are_golden(fixture, tmp_path): target = ConceptPack.load(FIXTURES / fixture).to_target() expected_recipe = {**LEGACY_RECIPE, "collateral_margin": 0.03} expected = json.dumps(expected_recipe, separators=(",", ":")).encode() assert json.dumps(target.recipe.to_dict(), separators=(",", ":")).encode() == expected assert target.entry_point is None path = tmp_path / "legacy.safetensors" ConceptDirection({0: torch.tensor([1., 0.])}, recipe=target.recipe).save(path) metadata = json.loads(path.with_suffix(".json").read_text()) assert "activation_identity" not in metadata assert "metadata_sha256" not in metadata assert metadata["recipe"] == expected_recipe assert builtin_refusal_pack().sha256 == "2e7be2696e059da6dcaa892053080fd27f20c56af699243a7bc059300c764bdb" @pytest.fixture(scope="module") def offline_model(tmp_path_factory): return build_tiny_offline_model(tmp_path_factory.mktemp("entry-point") / "model") @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime") @pytest.mark.parametrize("readout", ["final_token", "mean_pool", "last_k:2", "span:word", "literal"]) def test_all_readouts_match_direct_hidden_states(offline_model, readout, monkeypatch): literal = readout == "literal" entry = EntryPoint( template="{text} hello world" if literal else "{text} {{word}}hello world{{/word}}", readout="span:word" if literal else readout, spans={"word": "hello world"} if literal else {}, messages=[{"role": "system", "content": "safe"}], prefill=" answer", ) target = ConceptTarget("user/readout", ["harmful", "harmful request test"], {"n": ["harmless", "safe"]}, recipe=ExtractionRecipe(entry_point=entry)) pipeline = AbliterationPipeline(str(offline_model), concept=target, method="basic", device="cpu", dtype="float32", max_seq_length=32) pipeline._summon() monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None) monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.) tokenizer = pipeline.handle.tokenizer tokenizer.chat_template = "{% for m in messages %}{{ m['content'] }} {% endfor %}assistant:" rendered = pipeline._format_collection_prompts(target.positive, "harmful") assert all(p.endswith("assistant: answer") for p in rendered) inputs = pipeline._tokenize_activation_batch(rendered, 32, offsets=True) offsets = inputs.pop("offset_mapping") span = span_token_mask(rendered, offsets, inputs["attention_mask"]) if entry.readout.startswith("span:") else None expected = [] layers = get_layer_modules(pipeline.handle) def capture(module, args, output): hidden = output[0] if isinstance(output, tuple) else output rows = [] for row, mask in enumerate(inputs["attention_mask"].bool()): values = hidden[row][mask] if entry.readout == "final_token": rows.append(values[-1]) elif entry.readout == "last_k:2": rows.append(values[-2:].mean(0)) elif span is not None: rows.append(hidden[row][span[row]].mean(0)) else: rows.append(values.mean(0)) expected.append(torch.stack(rows)) hook = layers[0].register_forward_hook(capture) try: with torch.no_grad(): pipeline.handle.model(**inputs) finally: hook.remove() actual = torch.cat(pipeline._collect_activations(layers, rendered, "harmful")[0]) torch.testing.assert_close(actual, expected[0], rtol=1e-5, atol=1e-6) @pytest.mark.parametrize("offsets", [[[0, 0], [0, 5]], [[6, 11], [0, 0]]]) def test_truncated_spans_fail_explicitly(offsets): with pytest.raises(ValueError, match="truncated"): span_token_mask([RenderedPrompt("hello world", (0, 11))], torch.tensor([offsets]), torch.ones(1, 2)) @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime") @pytest.mark.parametrize("layers", [None, [0]]) def test_pipeline_persists_fallback_and_invalidates_changed_entry_point(offline_model, tmp_path, monkeypatch, layers): target = ConceptTarget("user/fallback", ["harmful hello", "harmful world"], {"n": ["harmless hello", "harmless world"]}, recipe=ExtractionRecipe( entry_point=EntryPoint(messages=[], prefill=" answer"), layer_select={"strategy": "preset", "preset": "all"}, )) pipeline = AbliterationPipeline(str(offline_model), concept=target, method="basic", device="cpu", dtype="float32", output_dir=str(tmp_path), layers=layers) pipeline._summon() monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None) monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.) pipeline._probe() pipeline._distill() if layers is not None: assert pipeline.concept_direction.extraction_layers == layers assert pipeline.concept_direction.intervention_layers == layers assert pipeline.concept_direction.recipe.entry_point == target.entry_point assert pipeline.concept_direction.activation_identity["plain_text_fallback_counts"] == { "harmful": 2, "harmless": 2, } observations = pipeline.concept_direction.activation_identity["special_token_observations"] assert {row["label"] for row in observations} == {"harmful", "harmless"} assert sum(row["prompt_count"] for row in observations) == 4 assert all(row["example"]["first_token_ids"] for row in observations) assert all(row["example"]["plain_text_fallback"] for row in observations) assert pipeline._build_metadata()["concept"]["activation_identity"] == ( pipeline.concept_direction.activation_identity ) path = tmp_path / "direction.safetensors" pipeline.concept_direction.save(path) assert ConceptDirection.load(path).activation_identity == pipeline.concept_direction.activation_identity pipeline.concept = replace(target, recipe=replace(target.recipe, entry_point=EntryPoint(prefill="world"))) with pytest.raises(ValueError, match="collect activations again"): pipeline._distill() @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime") def test_token_policy_enforced_before_forward_and_hooks_cleaned(offline_model, monkeypatch): from obliteratus.concepts.gate import GateFailed target = ConceptTarget("user/eos", ["hello"], {"n": ["world"]}, recipe=ExtractionRecipe(entry_point=EntryPoint(messages=[]))) pipeline = AbliterationPipeline(str(offline_model), concept=target, method="basic", device="cpu", dtype="float32", gate_mode="enforce") pipeline._summon() pipeline.handle.tokenizer.chat_template = "{{ eos_token }} {{ messages[0]['content'] }}" def forbidden_forward(*args, **kwargs): raise AssertionError("Policy must fail before a model forward") monkeypatch.setattr(pipeline.handle.model, "forward", forbidden_forward) layers = get_layer_modules(pipeline.handle) original_hooks = [dict(layer._forward_hooks) for layer in layers] with pytest.raises(GateFailed, match="eos_as_bos"): pipeline._probe() assert [dict(layer._forward_hooks) for layer in layers] == original_hooks assert pipeline._entry_point_activation_identity is None @pytest.mark.integration @pytest.mark.usefixtures("tiny_model_runtime") @pytest.mark.parametrize("fixture", ["valid_minimal", "valid_full", "pain"]) def test_v1_pipeline_directions_equal_frozen_legacy_readout(offline_model, fixture, tmp_path, monkeypatch): """Exercise old formatting/readout/fold algorithms with every shipped recipe.""" if fixture == "pain": value = yaml.safe_load((Path(__file__).parents[1] / "obliteratus/concepts/packs/pain/manifest.yaml").read_text()) recipe = ExtractionRecipe(**value["recipe"]) else: recipe = ConceptPack.load(FIXTURES / fixture).to_target().recipe endings = ["hello", "world", "request", "test", "answer", "safe", "hello world", "test answer"] target = ConceptTarget("user/legacy", ["harmful " + end for end in endings], {"n": ["harmless " + end for end in endings]}, recipe=recipe) def legacy_readout(hidden, attention_mask, position="final_token", **kwargs): mask = attention_mask.to(device=hidden.device, dtype=torch.bool) if position == "mean_pool": return hidden.masked_fill(~mask.unsqueeze(-1), 0).sum(1) / mask.sum(1, keepdim=True) positions = torch.arange(mask.shape[1], device=hidden.device).expand_as(mask) last = positions.masked_fill(~mask, -1).max(1).values return hidden[torch.arange(hidden.shape[0], device=hidden.device), last] def legacy_folds(counts, folds, seed, groups=None): assert groups is None generator = torch.Generator().manual_seed(0 if seed is None else seed) return {n: torch.randperm(c, generator=generator).tensor_split(folds) for n, c in counts.items()} results, configs, sidecars = [], [], [] for reference in (False, True): pipeline = AbliterationPipeline(str(offline_model), concept=target, method="basic", device="cpu", dtype="float32", seed=42, max_seq_length=32) configs.append(json.dumps({ "recipe": pipeline.concept.recipe.to_dict(), "settings": {key: value for key, value in vars(pipeline).items() if not key.startswith("_") and key not in {"model_name", "output_dir"} and (value is None or isinstance(value, (str, int, float, bool)))}, }, sort_keys=True).encode()) pipeline._summon() monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None) monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.) with monkeypatch.context() as patch: if reference: patch.setattr(pipeline, "_format_collection_prompts", lambda prompts, label, **kw: pipeline._maybe_apply_chat_template(prompts)) patch.setattr("obliteratus.abliterate.readout_activations", legacy_readout) patch.setattr("obliteratus.concepts.extraction.sample_folds", legacy_folds) pipeline._probe() pipeline._distill() results.append(pipeline.concept_direction) path = tmp_path / f"{reference}.safetensors" pipeline.concept_direction.save(path) sidecars.append(path.with_suffix(".json").read_bytes()) assert configs[0] == configs[1] assert sidecars[0] == sidecars[1] assert results[0].cv_scores == results[1].cv_scores for name in ("directions", "subspaces"): left, right = getattr(results[0], name), getattr(results[1], name) assert left.keys() == right.keys() assert all(torch.equal(left[layer], right[layer]) for layer in left) def test_entry_point_serialization_and_conflicting_legacy_readout(): value = manifest() value.update(schema_version="1.1", recipe=ExtractionRecipe(entry_point=EntryPoint()).to_dict()) validate_manifest(value) Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(value) value["recipe"]["readout"]["suffix"] = "answer" with pytest.raises(PackError, match="replaces legacy readout"): validate_manifest(value) with pytest.raises(ValidationError): Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(value) def test_group_annotations_reach_gate_materialization(): from obliteratus.concepts.gate import select_gate_cache_inputs target = ConceptTarget( "user/grouped", ["harmful", "harmful world"], {"n": ["harmless", "harmless world"]}, recipe=ExtractionRecipe(entry_point=EntryPoint(group_by="family")), records={"positive": [{"text": "harmful", "family": "a", "split": "train", "person": "1st"}, {"text": "harmful world", "family": "b"}], "control:n": [{"text": "harmless", "family": "a"}, {"text": "harmless world", "family": "b"}]}, ) groups = target.sample_groups() assert groups == {"positive": ["a", "b"], "control:n": ["a", "b"]} chunks = {0: [torch.ones(1, 2), torch.zeros(1, 2)]} inputs = select_gate_cache_inputs( chunks, {"n": chunks}, harmless=chunks, refusal=False, readouts={}, neighbours={}, texts=(target.positive, target.controls), top_tokens=[], sample_groups=groups, ) assert inputs.materialize().sample_groups == groups missing = replace(target, records={}) with pytest.raises(ValueError, match="family"): missing.sample_groups() def test_rendered_span_disambiguates_identical_prior_content(): tokenizer = SimpleNamespace(chat_template="fixture", apply_chat_template=lambda messages, **kw: "|".join(m["content"] for m in messages) + "|assistant") rendered = render_entry_point(EntryPoint( messages=[{"role": "system", "content": "hello"}], template="{{word}}hello{{/word}}", readout="span:word", ), {}, tokenizer, use_chat_template=True) assert rendered == "hello|hello|assistant" assert rendered.span == (6, 11) def test_provenance_hash_and_saved_direction_hash_change_with_entry_point(tmp_path): from obliteratus.concepts.export import _digest from obliteratus.concepts.runs import run_record direction = ConceptDirection({0: torch.tensor([1., 0.])}, concept_id="user/demo", recipe=ExtractionRecipe(entry_point=EntryPoint(prefill="hello")), activation_identity={"plain_text_fallback_counts": {"harmful": 1}}) hashes, records = [], [] for prefill in ("hello", "world"): direction.recipe = replace(direction.recipe, entry_point=EntryPoint(prefill=prefill)) path = tmp_path / f"{prefill}.safetensors" direction.save(path) hashes.append(json.loads(path.with_suffix(".json").read_text())["metadata_sha256"]) records.append(run_record(direction, mode="steer", parameters={}, tool_commit="a" * 40)) assert hashes[0] != hashes[1] assert _digest(records[0]) != _digest(records[1]) assert records[0]["activation_identity"] == direction.activation_identity @pytest.mark.parametrize("position", ["span:word", "last_k:2", "last_k:99"]) @pytest.mark.parametrize("left_padding", [True, False]) def test_span_and_last_k_ignore_padding(position, left_padding): from obliteratus.concepts.extraction import readout_activations real = torch.tensor([[1., 2.], [3., 4.], [5., 6.]]) padding = torch.full((2, 2), 1000.) hidden = torch.cat([padding, real] if left_padding else [real, padding]).unsqueeze(0) mask = torch.tensor([[0, 0, 1, 1, 1] if left_padding else [1, 1, 1, 0, 0]]) span = torch.tensor([[0, 0, 1, 1, 0] if left_padding else [1, 1, 0, 0, 0]]) expected = real[:2].mean(0) if position.startswith("span:") else ( real[-2:].mean(0) if position == "last_k:2" else real.mean(0) ) actual = readout_activations(hidden, mask, position, span_mask=span) assert torch.equal(actual[0], expected) @pytest.mark.parametrize("chat_rendered,expected", [(True, False), (False, None)]) def test_chat_rendered_entry_points_do_not_add_special_tokens_twice(chat_rendered, expected): from types import SimpleNamespace from obliteratus.abliterate import AbliterationPipeline from obliteratus.concepts.entry_points import RenderedPrompt calls = [] def tokenizer(prompts, **kwargs): calls.append(kwargs) return {"input_ids": torch.zeros(len(prompts), 1, dtype=torch.long)} tokenizer.padding_side, tokenizer.pad_token, tokenizer.pad_token_id = "right", "", 0 pipeline = AbliterationPipeline.__new__(AbliterationPipeline) pipeline.handle = SimpleNamespace(tokenizer=tokenizer) prompts = [RenderedPrompt("user: hi\nassistant: I feel", chat_rendered=chat_rendered)] pipeline._tokenize_activation_batch(prompts, 64) assert calls[-1].get("add_special_tokens") is expected pipeline._tokenize_activation_batch(["legacy prompt"], 64) assert "add_special_tokens" not in calls[-1] def test_plain_template_without_chat_is_not_marked_chat_rendered(): from obliteratus.concepts.entry_points import render_entry_point class Tokenizer: chat_template = "{{ messages }}" def apply_chat_template(self, *args, **kwargs): raise AssertionError("chat template must not be used") entry = EntryPoint(template="{text} I feel:") rendered = render_entry_point(entry, {"text": "hello"}, Tokenizer(), use_chat_template=False) assert str(rendered) == "hello I feel:" assert rendered.chat_rendered is False and rendered.fallback is False def test_reprobe_recomputes_entry_point_confound_tokens(): target = ConceptTarget( "user/lexical", ["Observe."] * 8, {"n": ["Observe."] * 8}, recipe=ExtractionRecipe(entry_point=EntryPoint(template="{word}")), records={"positive": [{"text": "Observe.", "word": "pain"} for _ in range(8)], "control:n": [{"text": "Observe.", "word": "calm"} for _ in range(8)]}, ) pipeline = AbliterationPipeline("offline", concept=target, method="basic", device="cpu", gate_mode="report") assert {r["token"] for r in pipeline._confound_tokens} == {"pain", "calm"} for record in pipeline.concept.records["positive"]: record["word"] = "agony" for record in pipeline.concept.records["control:n"]: record["word"] = "serenity" pipeline._refresh_entry_point_confounds() assert {r["token"] for r in pipeline._confound_tokens} == {"agony", "serenity"} @pytest.mark.parametrize("routing", [{}, {"layers": {"0": {"experts": 4, "top_k": 2}}}]) def test_checkpoint_metadata_keeps_activation_identity_with_routing(routing): recipe = ExtractionRecipe(entry_point=EntryPoint(special_token_policy={"bos": "require"})) target = ConceptTarget("user/metadata", ["hello"], {"n": ["world"]}, recipe=recipe) pipeline = AbliterationPipeline("offline", concept=target, method="basic", device="cpu") identity = { "chat_template": "{{ bos_token }} {{ messages[0]['content'] }}", "special_tokens": {"bos_token": ""}, "add_bos_token": True, "max_seq_length": 64, "truncation_side": "right", "plain_text_fallback_counts": {"harmful": 0, "harmless": 0}, "rendered_inputs_sha256": ["a" * 64], "special_token_observations": [{"issue_counts": {}, "bos_token_id": 1}], } pipeline._concept_direction = ConceptDirection( {0: torch.tensor([1., 0.])}, recipe=recipe, concept_id=target.id, activation_identity=identity, routing=routing, ) metadata = pipeline._build_metadata() saved = json.loads(json.dumps(metadata))["concept"] assert saved["activation_identity"] == identity assert saved["recipe"]["entry_point"]["special_token_policy"] == {"bos": "require"} if routing: assert saved["routing"] == routing else: assert "routing" not in saved metadata["concept"]["activation_identity"]["rendered_inputs_sha256"].append("b" * 64) assert pipeline.concept_direction.activation_identity == identity assert identity["rendered_inputs_sha256"] == ["a" * 64]