Files
OBLITERATUS/tests/test_concept_baseline.py
Joseph Magly bb0f69a70b fix(concepts,docs): verdict gate for baselines and setup fixes from the first local run
Fixes found while running the initial core run on a local RTX 4090.

- `concepts baseline --require-verdict {pass,warn}` exits 3 with the
  reasons after saving the report when the validation verdict is worse
  than required. Refusal validation stays in report mode, so without the
  option the command exits 0 even on a failing verdict; default
  behaviour is unchanged.
- Runbook: the L1 dry run no longer sets a hard stop (dry runs charge
  production stage allowances, so a 2-hour cap refused stages); the
  campaign needs its own CUDA environment from
  scripts/qwen38_campaign_setup.sh (qwen-hybrid kernels and PEFT);
  incomplete cached snapshots are passed as local snapshot directories.
- Layer placement: check strong_layers, pin a window with --layers when
  the default selection lands on the final layers (#533), and compare
  settings on the tune split rather than the final test set.
- verify-edit vLLM launch disables the FlashInfer sampler, whose startup
  JIT build fails when the system nvcc or host compiler doesn't match
  vLLM's CUDA build.
- Campaign setup docs: torch backend and nvcc must share a CUDA major
  version, and CUDA 12.4 needs a host compiler older than GCC 14 (use
  CC/CXX/CUDAHOSTCXX=gcc-13/g++-13 with --torch-backend cu126).
2026-11-18 19:51:10 -05:00

207 lines
9.3 KiB
Python

"""Tiny offline baseline extraction without intervention or model publishing."""
import json
import pytest
import torch
import yaml
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.cli import main
from obliteratus.concepts.baseline import run_baseline
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.pack_io import digest
from obliteratus.concepts.registry import ConceptRegistry
from obliteratus.concepts.validation import PackError
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
pytestmark = [
pytest.mark.cpu,
pytest.mark.integration,
pytest.mark.usefixtures("tiny_model_runtime"),
]
@pytest.fixture(autouse=True)
def single_thread():
previous = torch.get_num_threads()
torch.set_num_threads(1)
yield
torch.set_num_threads(previous)
@pytest.fixture
def synthetic_pack(tmp_path):
source = tmp_path / "pack"
source.mkdir()
repository = tmp_path / "synthetic-repository"
repository.mkdir()
rows = [{"category": category, "prompt": f"{word} {item}" + suffix}
for category, word in (("A", "harmful"), ("B", "harmless"))
for item, suffix in [(item, " answer" if index % 2 else "")
for index, item in enumerate(
["hello", "world", "test", "request"] * 2,
)]]
data = json.dumps({"datasets": {"primary": {"sentences": rows}}}).encode()
payload = repository / "data.json"
payload.write_bytes(data)
recipe = ConceptRegistry().pack("pain").manifest["recipe"]
recipe["readout"]["suffix"] = " answer"
manifest = {
"schema_version": "1.1", "id": "synthetic", "tier": "user", "version": "0.1.0",
"title": "Offline baseline fixture", "intended_use": "Synthetic offline test",
"recipe": recipe, "sets": {}, "licence": [], "provenance": {"source_commit": "a" * 40},
}
for logical, category in (("positive.jsonl", "A"), ("controls/neutral.jsonl", "B")):
entry = {"remote": {"repo": "https://github.com/example/fixture", "commit": "a" * 40,
"path": "data.json", "selector": {
"format": "json", "select": "datasets.primary.sentences",
"filter": {"category": [category]}, "text_field": "prompt",
}}, "sha256": digest(data), "n": 8}
if category == "A":
manifest["sets"]["positive"] = entry
else:
manifest["sets"]["controls"] = [{**entry, "name": "neutral"}]
manifest["licence"].append({"path": logical, "spdx": "CC0-1.0", "source": "synthetic",
"attribution": "OBLITERATUS test authors"})
(source / "manifest.yaml").write_text(yaml.safe_dump(manifest))
return source, payload
@pytest.mark.parametrize("required", [None, "warn", "pass"])
def test_baseline_explicit_fetch_report_and_directions_without_weight_edits(
tmp_path, monkeypatch, synthetic_pack, capsys, required,
):
source, payload = synthetic_pack
model = build_tiny_offline_model(tmp_path / "model")
default_cache = tmp_path / "default-cache"
cache = tmp_path / "selected-cache"
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(default_cache))
calls, batches, states = [], [], []
original_summon = AbliterationPipeline._summon
original_probe = AbliterationPipeline._probe
def fetch(pack, **kwargs):
assert not calls
assert kwargs["cache_dir"] == str(cache)
calls.append("explicit fetch")
ConceptPack.fetch(pack, fetcher=lambda url, limit: payload.read_bytes(), **kwargs)
def summon(pipeline):
original_summon(pipeline)
assert pipeline.handle.snapshot_policy["status"] == "disabled_with_reason"
assert pipeline.handle.snapshot_policy["restore_available"] is False
assert pipeline.use_chat_template is False
pipeline.handle.tokenizer.chat_template = "UNEXPECTED CHAT TEMPLATE"
states.append({name: tensor.clone()
for name, tensor in pipeline.handle.model.state_dict().items()})
def probe(pipeline):
tokenizer_class = type(pipeline.handle.tokenizer)
original = tokenizer_class.__call__
def record(tokenizer, prompts, *args, **kwargs):
batches.extend(prompts)
return original(tokenizer, prompts, *args, **kwargs)
with monkeypatch.context() as context:
context.setattr(tokenizer_class, "__call__", record)
original_probe(pipeline)
unchanged = all(torch.equal(states[0][name], tensor)
for name, tensor in pipeline.handle.model.state_dict().items())
assert unchanged
def forbidden(*args, **kwargs):
pytest.fail("Baseline attempted an intervention or checkpoint publication")
pack = ConceptPack.load(source)
monkeypatch.setattr("obliteratus.concepts.baseline._resolve", lambda name: pack)
monkeypatch.setattr(pack, "fetch", lambda **kwargs: fetch(pack, **kwargs))
monkeypatch.setattr(AbliterationPipeline, "_summon", summon)
monkeypatch.setattr(AbliterationPipeline, "_probe", probe)
monkeypatch.setattr(AbliterationPipeline, "_excise", forbidden)
monkeypatch.setattr(AbliterationPipeline, "_rebirth", forbidden)
output = tmp_path / "baseline"
arguments = ["concepts", "baseline", "user/synthetic", str(model), "--output-dir", str(output),
"--cache-dir", str(cache)]
if required is None:
main(arguments)
else:
with pytest.raises(SystemExit) as error:
main([*arguments, "--require-verdict", required])
assert error.value.code == 3
assert list((cache / "files").iterdir())
assert not default_cache.exists()
captured = capsys.readouterr()
if required is None:
assert "report" in captured.out
else:
assert "below required" in captured.err
assert str(output / "validation.json") in captured.err
assert calls == ["explicit fetch"]
assert len(batches) == 16
assert all(prompt.endswith(" answer") and not prompt.endswith(" answer answer")
for prompt in batches)
assert set(path.name for path in output.iterdir()) == {
"validation.json", "direction.json", "direction.safetensors",
}
report = json.loads((output / "validation.json").read_text())
assert report["mode"] == "report"
assert report["verdict"] == "fail" # The underpowered fixture still writes its report.
direction = ConceptDirection.load(output / "direction.safetensors")
assert direction.concept_id == "user/synthetic"
assert direction.seed == 42
assert direction.pack_hash == pack.sha256
assert direction.validation == report
assert direction.recipe.to_dict() == {"collateral_margin": 0.03, **pack.manifest["recipe"]}
assert all(set(row["readout_aucs"]) == {"final_token", "mean_pool"}
for row in report["metrics"].values())
def test_baseline_hash_failure_precedes_model_loading(tmp_path, monkeypatch, synthetic_pack):
source, payload = synthetic_pack
def forbidden(*args, **kwargs):
pytest.fail("Invalid data reached model loading")
monkeypatch.setattr(AbliterationPipeline, "_summon", forbidden)
with pytest.raises(PackError, match="SHA-256 mismatch"):
run_baseline(source, "unused", tmp_path / "out", cache_dir=tmp_path / "cache",
fetcher=lambda url, limit: payload.read_bytes() + b" ")
assert not (tmp_path / "out").exists()
def test_baseline_legacy_refusal_direction_uses_portable_artifacts(
tmp_path, monkeypatch, synthetic_pack,
):
from obliteratus.concepts.registry import refusal_target
source, payload = synthetic_pack
pack = ConceptPack.load(source)
target = refusal_target(["harmful hello", "harmful world"] * 4,
["harmless hello", "harmless world"] * 4)
monkeypatch.setattr("obliteratus.concepts.baseline._resolve", lambda name: pack)
monkeypatch.setattr(pack, "to_target", lambda **kwargs: target)
model = build_tiny_offline_model(tmp_path / "model")
output = run_baseline("refusal", model, tmp_path / "out", cache_dir=tmp_path / "cache",
fetcher=lambda url, limit: payload.read_bytes(), max_seq_length=8)
direction = ConceptDirection.load(output / "direction.safetensors")
assert direction.concept_id == "refusal"
assert direction.recipe.method == "diff_means"
assert direction.validation["mode"] == "report"
assert direction.extraction_layers == direction.intervention_layers == [0]
def test_baseline_extraction_override_persists(tmp_path, synthetic_pack):
source, payload = synthetic_pack
model = build_tiny_offline_model(tmp_path / "model")
output = run_baseline(source, model, tmp_path / "out", cache_dir=tmp_path / "cache",
fetcher=lambda url, limit: payload.read_bytes(),
extraction_layer=0, max_seq_length=8)
direction = ConceptDirection.load(output / "direction.safetensors")
assert direction.extraction_layers == [0]
assert direction.extraction_layer_source == "pinned"
assert direction.cv_scores == {}
assert direction.recipe.extraction_layer[str(model)] == 0
assert direction.validation["mode"] == "report"