mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Add an optional BOS policy to pack entry points (schema 1.2), warn or fail on EOS-as-BOS and policy mismatches, and persist resolved special tokens through extraction, run and export provenance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
252 lines
12 KiB
Python
252 lines
12 KiB
Python
"""Entry-point special-token policy and provenance, using offline tokenizers."""
|
|
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
from jsonschema import Draft202012Validator, ValidationError
|
|
import pytest
|
|
import torch
|
|
from tokenizers import Tokenizer
|
|
from tokenizers.models import WordLevel
|
|
from tokenizers.pre_tokenizers import WhitespaceSplit
|
|
from tokenizers.processors import TemplateProcessing
|
|
from transformers import PreTrainedTokenizerFast
|
|
import yaml
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.concepts import ConceptDirection, ConceptTarget, EntryPoint, ExtractionRecipe
|
|
from obliteratus.concepts.entry_points import render_entry_point, special_token_observations
|
|
from obliteratus.concepts.export import export_run, validate_provenance
|
|
from obliteratus.concepts.gate import GateFailed
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.concepts.runs import load_run, run_record, write_json
|
|
from obliteratus.concepts.validation import PackError, SCHEMA_PATH, validate_manifest
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
FIXTURE = Path(__file__).parent / "fixtures/concept_packs/valid_minimal"
|
|
|
|
|
|
def tokenizer_with_bos(bos="<bos>"):
|
|
vocab = {"<pad>": 0, "<bos>": 1, "<eos>": 2, "<unk>": 3, "hello": 4, "world": 5}
|
|
backend = Tokenizer(WordLevel(vocab, unk_token="<unk>"))
|
|
backend.pre_tokenizer = WhitespaceSplit()
|
|
if bos is not None:
|
|
backend.post_processor = TemplateProcessing(
|
|
single=f"{bos} $A <eos>", special_tokens=list({bos: vocab[bos], "<eos>": 2}.items()),
|
|
)
|
|
return PreTrainedTokenizerFast(tokenizer_object=backend, bos_token=bos,
|
|
eos_token="<eos>", pad_token="<pad>", unk_token="<unk>")
|
|
|
|
|
|
def pipeline_for_tokens(tokenizer, *, mode="report", override=None):
|
|
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
|
|
pipeline.handle = SimpleNamespace(tokenizer=tokenizer)
|
|
pipeline._gate_mode = mode
|
|
pipeline._override_gate = override
|
|
pipeline.messages = []
|
|
pipeline.log = pipeline.messages.append
|
|
return pipeline
|
|
|
|
|
|
def observe(pipeline, entry, texts=("hello", "hello world"), *, max_length=32, chat=False):
|
|
prompts = [render_entry_point(entry, {"text": text}, pipeline.handle.tokenizer,
|
|
use_chat_template=chat) for text in texts]
|
|
inputs = pipeline._tokenize_activation_batch(prompts, max_length, special_tokens=True)
|
|
pipeline._record_entry_point_tokens(entry, prompts, inputs, "harmful", 0)
|
|
return pipeline._entry_point_token_observations[-1], inputs
|
|
|
|
|
|
@pytest.mark.parametrize("bos,policy,issues,first", [
|
|
("<bos>", "tokenizer_default", {}, 1),
|
|
("<bos>", "require", {}, 1),
|
|
("<bos>", "none", {"bos_forbidden": 2}, 1),
|
|
(None, "tokenizer_default", {}, 4),
|
|
(None, "none", {}, 4),
|
|
(None, "require", {"bos_required": 2}, 4),
|
|
("<eos>", "tokenizer_default", {"eos_as_bos": 2}, 2),
|
|
("<eos>", "require", {"eos_as_bos": 2}, 2),
|
|
])
|
|
def test_tokenizer_policies_check_unpadded_inputs(bos, policy, issues, first):
|
|
pipeline = pipeline_for_tokens(tokenizer_with_bos(bos))
|
|
entry = EntryPoint(special_token_policy={"bos": policy})
|
|
observed, inputs = observe(pipeline, entry)
|
|
assert inputs["attention_mask"][0, 0] == 0 # EOS/pad cannot become the first real token.
|
|
assert observed["issue_counts"] == issues
|
|
assert observed["example"]["first_token_ids"][0] == first
|
|
assert observed["bos_token_id"] == pipeline.handle.tokenizer.bos_token_id
|
|
assert observed["eos_token_id"] == 2
|
|
assert observed["example"]["add_special_tokens"] is True
|
|
assert observed["example"]["bos_added_by_tokenizer"] == (None if bos is None else True)
|
|
assert observed["example"]["eos_added_by_tokenizer"] is (bos is not None)
|
|
assert bool(pipeline.messages) == bool(issues)
|
|
|
|
|
|
@pytest.mark.parametrize("mode,override,raises", [
|
|
("enforce", None, True), ("report", None, False), ("off", None, False),
|
|
("enforce", "replicate EOS-as-BOS experiment", False),
|
|
])
|
|
def test_enforcement_preserves_operator_override(mode, override, raises):
|
|
pipeline = pipeline_for_tokens(tokenizer_with_bos("<eos>"), mode=mode, override=override)
|
|
if raises:
|
|
with pytest.raises(GateFailed, match="eos_as_bos.*--override-gate"):
|
|
observe(pipeline, EntryPoint())
|
|
else:
|
|
observe(pipeline, EntryPoint())
|
|
observed = pipeline._entry_point_token_observations[0]
|
|
assert observed["issue_counts"] == {"eos_as_bos": 2}
|
|
assert observed["gate_mode"] == mode and observed["override_gate"] == override
|
|
assert "WARNING" in pipeline.messages[0]
|
|
|
|
|
|
def test_chat_template_attribution_and_no_duplicate_special_tokens():
|
|
tokenizer = tokenizer_with_bos()
|
|
tokenizer.chat_template = "{{ bos_token }} {{ messages[0]['content'] }} {{ eos_token }}"
|
|
pipeline = pipeline_for_tokens(tokenizer)
|
|
observed, _ = observe(pipeline, EntryPoint(), chat=True)
|
|
example = observed["example"]
|
|
assert example["first_token_ids"] == [1, 4, 2]
|
|
assert example["add_special_tokens"] is False
|
|
assert example["chat_template_applied"] is True
|
|
assert example["chat_template_inserted"] == {"bos": True, "eos": True}
|
|
assert example["bos_added_by_tokenizer"] is False
|
|
assert example["eos_added_by_tokenizer"] is False
|
|
assert example["bos_in_rendered_text"] is True
|
|
assert example["eos_in_rendered_text"] is True
|
|
tokenizer.chat_template = "{{ eos_token }} {{ messages[0]['content'] }}"
|
|
observed, _ = observe(pipeline, EntryPoint(), chat=True)
|
|
assert observed["issue_counts"] == {"eos_as_bos": 2}
|
|
assert observed["example"]["chat_template_inserted"] == {"bos": False, "eos": True}
|
|
|
|
|
|
def test_literal_special_token_attribution_is_unknown_and_later_rows_are_checked():
|
|
tokenizer = tokenizer_with_bos(None)
|
|
tokenizer.chat_template = "{{ messages[0]['content'] }}"
|
|
pipeline = pipeline_for_tokens(tokenizer)
|
|
observed, _ = observe(pipeline, EntryPoint(), ("hello", "<eos> world"), chat=True)
|
|
assert observed["issue_counts"] == {"eos_as_bos": 1}
|
|
assert observed["example"]["first_token_ids"] == [4]
|
|
assert observed["first_violation"]["row"] == 1
|
|
assert observed["first_violation"]["chat_template_inserted"]["eos"] is None
|
|
|
|
|
|
def test_chat_fallback_and_truncation_are_observed():
|
|
tokenizer = tokenizer_with_bos(None)
|
|
tokenizer.truncation_side = "left"
|
|
pipeline = pipeline_for_tokens(tokenizer)
|
|
observed, _ = observe(pipeline, EntryPoint(messages=[]), ("hello <eos> world",), max_length=2)
|
|
assert observed["example"]["first_token_ids"] == [2, 5]
|
|
assert observed["example"]["plain_text_fallback"] is True
|
|
assert observed["example"]["chat_template_applied"] is False
|
|
assert observed["example"]["chat_template_inserted"] == {"bos": False, "eos": False}
|
|
assert observed["issue_counts"] == {"eos_as_bos": 1}
|
|
|
|
|
|
def test_missing_masks_do_not_invent_token_attribution():
|
|
tokenizer = tokenizer_with_bos()
|
|
prompt = render_entry_point(EntryPoint(), {"text": "hello"}, tokenizer, use_chat_template=False)
|
|
observed = special_token_observations(EntryPoint(), tokenizer, [prompt],
|
|
{"input_ids": torch.tensor([[1, 4]])})
|
|
assert observed["example"]["first_token_ids"] == [1, 4]
|
|
assert observed["example"]["bos_added_by_tokenizer"] is None
|
|
empty = special_token_observations(EntryPoint(), tokenizer, [prompt], {
|
|
"input_ids": torch.tensor([[0]]), "attention_mask": torch.tensor([[0]]),
|
|
})
|
|
assert empty["issue_counts"] == {"empty_input": 1}
|
|
|
|
|
|
@pytest.mark.parametrize("policy", ["tokenizer_default", "none", "require"])
|
|
def test_pack_policy_schema_and_serialization(policy):
|
|
manifest = yaml.safe_load((FIXTURE / "manifest.yaml").read_text())
|
|
manifest.update(schema_version="1.2")
|
|
manifest["recipe"]["entry_point"] = {"special_token_policy": {"bos": policy}}
|
|
validate_manifest(manifest)
|
|
schema = Draft202012Validator(json.loads(SCHEMA_PATH.read_text()))
|
|
schema.validate(manifest)
|
|
recipe = ExtractionRecipe(**manifest["recipe"])
|
|
assert recipe.to_dict()["entry_point"]["special_token_policy"] == {"bos": policy}
|
|
for version in ("1.0", "1.1", "1.01"):
|
|
manifest["schema_version"] = version
|
|
with pytest.raises(PackError, match="schema_version"):
|
|
validate_manifest(manifest)
|
|
with pytest.raises(ValidationError):
|
|
schema.validate(manifest)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [{}, {"bos": "always"}, {"bos": False}, {"eos": "none"},
|
|
{"bos": "none", "eos": "none"}, [], "none"])
|
|
def test_invalid_policy_is_rejected_in_schema_and_api(value):
|
|
with pytest.raises(ValueError, match="special_token_policy"):
|
|
EntryPoint(special_token_policy=value)
|
|
manifest = yaml.safe_load((FIXTURE / "manifest.yaml").read_text())
|
|
manifest.update(schema_version="1.2")
|
|
manifest["recipe"]["entry_point"] = {"special_token_policy": value}
|
|
with pytest.raises(PackError):
|
|
validate_manifest(manifest)
|
|
with pytest.raises(ValidationError):
|
|
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest)
|
|
|
|
|
|
def test_old_recipe_serialization_and_policy_identity():
|
|
legacy_entry = {"messages": None, "prefill": "", "readout": "final_token",
|
|
"template": "{text}", "group_by": None, "spans": {}}
|
|
recipe = ExtractionRecipe(entry_point=legacy_entry)
|
|
assert recipe.to_dict()["entry_point"] == legacy_entry
|
|
target = ConceptTarget("user/test", ["hello"], {"control": ["world"]}, recipe=recipe)
|
|
changed = replace(target, recipe=replace(recipe, entry_point=EntryPoint(
|
|
special_token_policy={"bos": "tokenizer_default"},
|
|
)))
|
|
assert changed.activation_identity() != target.activation_identity()
|
|
|
|
|
|
def test_cli_help_explains_token_policy_and_override(capsys):
|
|
from obliteratus.cli import main
|
|
|
|
with pytest.raises(SystemExit) as error:
|
|
main(["abliterate", "--help"])
|
|
assert error.value.code == 0
|
|
help_text = " ".join(capsys.readouterr().out.split())
|
|
assert "Entry-point BOS/EOS policy violations fail under enforce" in help_text
|
|
assert "report/off warn" in help_text
|
|
assert "Recorded reason to proceed" in help_text
|
|
|
|
|
|
def test_policy_and_resolved_tokens_round_trip_pack_direction_run_export(tmp_path):
|
|
original = ConceptPack.load(FIXTURE)
|
|
manifest = deepcopy(original.manifest)
|
|
manifest["schema_version"] = "1.2"
|
|
manifest["recipe"]["entry_point"] = {"special_token_policy": {"bos": "require"}}
|
|
files = {**original.files, "manifest.yaml": yaml.safe_dump(manifest).encode()}
|
|
pack = ConceptPack(files)
|
|
pack.write(tmp_path / "policy.cvpack")
|
|
pack = ConceptPack.load(tmp_path / "policy.cvpack")
|
|
target = pack.to_target()
|
|
pipeline = pipeline_for_tokens(tokenizer_with_bos())
|
|
observed, _ = observe(pipeline, target.entry_point)
|
|
direction = ConceptDirection(
|
|
{0: torch.tensor([1., 0.])}, recipe=target.recipe,
|
|
concept_id=target.id, concept_version=target.version, pack_hash=pack.sha256,
|
|
activation_identity={"special_token_observations": [observed]},
|
|
)
|
|
source = tmp_path / "run"
|
|
source.mkdir()
|
|
direction.save(source / "direction.safetensors")
|
|
loaded = ConceptDirection.load(source / "direction.safetensors")
|
|
assert loaded.recipe == target.recipe
|
|
assert loaded.activation_identity == direction.activation_identity
|
|
record = run_record(loaded, pack=pack, mode="none", parameters={}, tool_commit="a" * 40)
|
|
write_json(source / "concept_run.json", record)
|
|
assert load_run(source) == record
|
|
exported = export_run(source, tmp_path / "export", kind="direction")
|
|
validate_provenance(exported)
|
|
assert exported["run"]["recipe"]["entry_point"]["special_token_policy"] == {"bos": "require"}
|
|
assert exported["run"]["activation_identity"] == direction.activation_identity
|
|
exported_direction = ConceptDirection.load(tmp_path / "export/direction.safetensors")
|
|
assert exported_direction.activation_identity == direction.activation_identity
|
|
assert torch.equal(exported_direction.directions[0], direction.directions[0])
|
|
exported["run"]["activation_identity"]["special_token_observations"][0]["bos_token_id"] = 99
|
|
with pytest.raises(ValueError, match="digest mismatch"):
|
|
validate_provenance(exported)
|