mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
359 lines
17 KiB
Python
359 lines
17 KiB
Python
"""CV-08 offline fixture lifecycle and UI/CLI publication boundaries."""
|
|
|
|
import ast
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
from safetensors.torch import load_file
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.concepts import (
|
|
ConceptDirection, apply_concept_intervention, export_run, save_concept_run,
|
|
)
|
|
from obliteratus.concepts.evaluation import EvaluationBattery, evaluate_intervention
|
|
from obliteratus.concepts.evaluation_model import ModelEvaluator
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.concepts.runs import PublicationBlocked, load_run, write_json
|
|
from obliteratus.lora_ablation import UnmergedLoraAdapters
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
from tests.test_concept_extraction_pipeline import small_target
|
|
from tests.test_concept_intervention import _tiny_residual_model
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def base(tmp_path_factory):
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
path = build_tiny_offline_model(tmp_path_factory.mktemp("concept-export") / "model")
|
|
model = AutoModelForCausalLM.from_pretrained(path, local_files_only=True).eval()
|
|
tokenizer = AutoTokenizer.from_pretrained(path, local_files_only=True)
|
|
yield model, tokenizer, path
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
@pytest.mark.parametrize("mode,kind", [
|
|
("orthogonalize", "weights"), ("lora", "lora"), ("project", "direction"), ("steer", "report"),
|
|
])
|
|
def test_live_run_persists_exportable_artifacts(base, tmp_path, mode, kind, monkeypatch):
|
|
model, tokenizer = deepcopy(base[:2])
|
|
pack = ConceptPack.load(ROOT / "tests/fixtures/concept_packs/valid_minimal")
|
|
direction = ConceptDirection(
|
|
{0: torch.eye(16)[0]}, concept_id="user/synthetic", concept_version="0.1.0",
|
|
pack_hash=pack.sha256, model_id="fixture/tiny", model_revision="a" * 40, seed=231,
|
|
validation={"concept_id": "user/synthetic", "pack_hash": pack.sha256,
|
|
"verdict": "pass", "metrics": {"0": {"heldout_auc": .9}}},
|
|
)
|
|
evaluator = ModelEvaluator(model, tokenizer, direction)
|
|
battery = EvaluationBattery(
|
|
evaluator.generate, measure=evaluator.measure, skip_collateral=True,
|
|
model_id=direction.model_id, model_revision=direction.model_revision,
|
|
)
|
|
source = tmp_path / "run"
|
|
pristine = {name: value.clone() for name, value in model.state_dict().items()}
|
|
with evaluate_intervention(
|
|
model, direction, mode, battery, output_dir=source, pack=pack,
|
|
) as run:
|
|
assert load_run(source)["intervention"]["mode"] == mode
|
|
if mode == "steer":
|
|
run.alpha = -.5
|
|
if mode == "project":
|
|
run.config.per_layer_alpha[0] = .25
|
|
record = save_concept_run(
|
|
source, direction, run, pack=pack,
|
|
model=model if mode in {"orthogonalize", "lora"} else None,
|
|
tokenizer=tokenizer, tool_commit="b" * 40,
|
|
)
|
|
result = export_run(source, tmp_path / "export", kind=kind)
|
|
assert result["run"] == record
|
|
assert record["pack"]["references"]
|
|
if mode == "steer":
|
|
assert record["intervention"]["parameters"]["alpha"] == -.5
|
|
if kind == "weights":
|
|
restored = AutoModelForCausalLM.from_pretrained(
|
|
tmp_path / "export", local_files_only=True,
|
|
)
|
|
for name, value in model.state_dict().items():
|
|
assert torch.equal(value, restored.state_dict()[name])
|
|
elif kind == "lora":
|
|
assert (tmp_path / "export/adapter_model.safetensors").is_file()
|
|
assert (tmp_path / "export/adapter_config.json").is_file()
|
|
if mode != "orthogonalize":
|
|
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
|
|
if kind == "direction":
|
|
monkeypatch.chdir(tmp_path / "export")
|
|
namespace = {"model": model}
|
|
exec((tmp_path / "export/loader.py").read_text(), namespace)
|
|
loaded = namespace["run"]
|
|
assert loaded.mode == mode
|
|
assert loaded.layers == [0]
|
|
assert loaded.config.per_layer_alpha == {0: .25}
|
|
loaded.remove()
|
|
|
|
|
|
@pytest.mark.parametrize("architecture", ["gpt2", "llama"])
|
|
def test_mixed_subspace_ranks_save_and_export_without_changing_updates(base, tmp_path, architecture):
|
|
config = deepcopy(base[0].config)
|
|
config.n_layer = 2
|
|
model = (AutoModelForCausalLM.from_config(config).eval() if architecture == "gpt2"
|
|
else _tiny_residual_model(architecture))
|
|
tokenizer = base[1]
|
|
clean = deepcopy(model)
|
|
direction = ConceptDirection(
|
|
{0: torch.eye(16)[0], 1: torch.eye(16)[0]},
|
|
subspaces={0: torch.eye(16)[:1], 1: torch.eye(16)[:2]},
|
|
model_id="fixture/tiny",
|
|
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
|
|
)
|
|
source, output = tmp_path / "run", tmp_path / "export"
|
|
tokens = torch.tensor([[3, 5, 6]])
|
|
with apply_concept_intervention(model, direction, "lora", subspace=True) as run:
|
|
original = {name: (b.clone(), a.clone()) for name, (b, a) in run.manager.adapters.items()}
|
|
assert {a.shape[0] for _, a in original.values()} == {1, 2}
|
|
expected = model(tokens).logits.detach()
|
|
save_concept_run(source, direction, run, model=model, tokenizer=tokenizer)
|
|
exported = export_run(source, output, kind="lora")
|
|
geometry = exported["run"]["intervention"]["parameters"]["geometry"]
|
|
assert {layer: g["rank"] for layer, g in geometry.items()} == {"0": 1, "1": 2}
|
|
config = json.loads((output / "adapter_config.json").read_text())
|
|
assert config["r"] == config["lora_alpha"] == 2
|
|
assert config["fan_in_fan_out"] is (architecture == "gpt2")
|
|
state = load_file(output / "adapter_model.safetensors")
|
|
restored = {}
|
|
for name, (b, a) in original.items():
|
|
saved_b = state[f"base_model.model.{name}.lora_B.weight"]
|
|
saved_a = state[f"base_model.model.{name}.lora_A.weight"]
|
|
assert saved_b.shape[1] == saved_a.shape[0] == 2
|
|
torch.testing.assert_close(saved_b @ saved_a, b @ a)
|
|
if a.shape[0] == 1:
|
|
assert not saved_b[:, 1:].count_nonzero()
|
|
assert not saved_a[1:].count_nonzero()
|
|
assert torch.equal(run.manager.adapters[name][0], b)
|
|
assert torch.equal(run.manager.adapters[name][1], a)
|
|
restored[name] = (saved_b, saved_a)
|
|
reloaded = UnmergedLoraAdapters(clean, restored)
|
|
try:
|
|
torch.testing.assert_close(clean(tokens).logits, expected)
|
|
finally:
|
|
reloaded.remove()
|
|
assert torch.equal(model(tokens).logits, expected)
|
|
|
|
|
|
def test_affine_native_bias_checkpoint_reloads_with_standard_hf(base, tmp_path):
|
|
model, tokenizer = deepcopy(base[:2])
|
|
direction = ConceptDirection(
|
|
{0: torch.eye(16)[0]}, subspaces={0: torch.eye(16)[:2]},
|
|
reference_means={0: torch.arange(16, dtype=torch.float32) / 16},
|
|
reference_provenance={"source": "fixture calibration", "seed": 335},
|
|
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
|
|
)
|
|
original_keys = set(model.state_dict())
|
|
run = apply_concept_intervention(
|
|
model, direction, "orthogonalize", subspace=True, reference="mean",
|
|
)
|
|
source, output = tmp_path / "run", tmp_path / "bundle"
|
|
record = save_concept_run(
|
|
source, direction, run, model=model, tokenizer=tokenizer, tool_commit="b" * 40,
|
|
)
|
|
assert record["intervention"]["parameters"]["reference"] == "mean"
|
|
assert record["intervention"]["parameters"]["affine_bias"]
|
|
assert set(model.state_dict()) == original_keys
|
|
exported = export_run(source, output, kind="weights")
|
|
assert exported["run"] == record
|
|
assert "existing residual-writer biases" in (output / "MODEL_CARD.md").read_text()
|
|
restored = AutoModelForCausalLM.from_pretrained(output, local_files_only=True).eval()
|
|
for name, value in model.state_dict().items():
|
|
assert torch.equal(value, restored.state_dict()[name]), name
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
with torch.inference_mode():
|
|
assert torch.equal(model(inputs).logits, restored(inputs).logits)
|
|
|
|
|
|
def test_pipeline_saves_run_and_exports_skipped_validation(base, tmp_path):
|
|
pipeline = AbliterationPipeline(
|
|
str(base[2]), concept=small_target(), seed=231, method="basic", device="cpu",
|
|
dtype="float32", output_dir=str(tmp_path / "run"), max_seq_length=8,
|
|
gate_mode="off", override_gate="Explicit fixture gate override",
|
|
skip_collateral=True, verify_sample_size=1, refusal_max_tokens=8,
|
|
max_perplexity_increase=1000., max_degenerate_fraction=1.,
|
|
)
|
|
pipeline.run()
|
|
record = load_run(tmp_path / "run")
|
|
assert record["validation"]["verdict"] == "skipped"
|
|
assert record["validation"]["override_gate"] == "Explicit fixture gate override"
|
|
assert record["evaluation"]["collateral"]["status"] == "skipped"
|
|
assert record["intervention"]["parameters"]["pipeline"]["regularization"] == 0.
|
|
result = export_run(tmp_path / "run", tmp_path / "export", kind="weights")
|
|
assert result["run"] == record
|
|
assert ConceptDirection.load(tmp_path / "run/direction.safetensors").seed == 231
|
|
with pytest.raises(PublicationBlocked):
|
|
export_run(tmp_path / "run", tmp_path / "card", kind="report", publish_card=True)
|
|
|
|
|
|
def test_research_pipeline_allows_hub_configuration(tmp_path):
|
|
pipeline = AbliterationPipeline(
|
|
"never/load", concept=replace(small_target(), tier="research"),
|
|
output_dir=str(tmp_path / "run"), push_to_hub="fixture/research",
|
|
)
|
|
assert pipeline.push_to_hub == "fixture/research"
|
|
assert pipeline.concept.tier == "research"
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["checkpoint", "session", "concept_run"])
|
|
def test_ui_collateral_guard_precedes_refinement_credentials_and_upload(tmp_path, evidence):
|
|
# Execute the real callback with inert UI defaults; no Gradio server or sockets.
|
|
module = ast.parse((ROOT / "app.py").read_text())
|
|
function = next(node for node in module.body
|
|
if isinstance(node, ast.FunctionDef) and node.name == "push_session_to_hub")
|
|
function.args.defaults = []
|
|
directory = tmp_path / "run"
|
|
directory.mkdir()
|
|
metadata = {"output_dir": str(directory)}
|
|
evaluation = {"collateral": {"status": "FAIL", "refusal": {"status": "PASS"}}}
|
|
concept = {"tier": "research", "evaluation": evaluation}
|
|
if evidence == "checkpoint":
|
|
write_json(directory / "abliteration_metadata.json", {"concept": concept})
|
|
elif evidence == "session":
|
|
metadata["concept"] = concept
|
|
else:
|
|
from obliteratus.concepts.runs import run_record
|
|
direction = ConceptDirection(
|
|
{0: torch.ones(2)}, concept_id="user/research",
|
|
pack_provenance={"id": "user/research", "version": "1.0.0", "sha256": None,
|
|
"tier": "research", "references": []},
|
|
)
|
|
record = run_record(direction, mode="orthogonalize", parameters={})
|
|
record["evaluation"]["collateral"] = evaluation["collateral"]
|
|
write_json(directory / "concept_run.json", record)
|
|
credentials = Mock(side_effect=AssertionError("must not access credentials"))
|
|
namespace = {"Path": Path, "_session_models": {"fixture": metadata},
|
|
"resolve_first": credentials}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
messages = list(namespace["push_session_to_hub"](
|
|
"fixture", "fixture/blocked", "", True, .3, 1, None, None, None, Mock(),
|
|
))
|
|
assert len(messages) == 1
|
|
assert "Publication blocked" in messages[0][0]
|
|
assert "--allow-collateral-fail" in messages[0][0]
|
|
credentials.assert_not_called()
|
|
|
|
|
|
def test_existing_concept_metadata_keys_are_additive(base, tmp_path):
|
|
# A refusal run keeps every pre-existing key, including its existing concept block.
|
|
source = tmp_path / "run"
|
|
source.mkdir()
|
|
model, tokenizer = base[:2]
|
|
model.save_pretrained(source, safe_serialization=True)
|
|
tokenizer.save_pretrained(source)
|
|
from obliteratus.concepts.runs import run_record
|
|
from obliteratus.concepts.pack import builtin_refusal_pack
|
|
pack = builtin_refusal_pack()
|
|
direction = ConceptDirection({0: torch.ones(16)}, pack_hash=pack.sha256)
|
|
record = run_record(direction, pack=pack, mode="orthogonalize", parameters={})
|
|
write_json(source / "concept_run.json", record)
|
|
original = {"source_model": "fixture/base", "concept": {"id": "refusal", "seed": 231},
|
|
"technique": "refusal_direction_ablation"}
|
|
write_json(source / "abliteration_metadata.json", original)
|
|
export_run(source, tmp_path / "export", kind="weights")
|
|
exported = json.loads((tmp_path / "export/abliteration_metadata.json").read_text())
|
|
assert exported["source_model"] == original["source_model"]
|
|
assert exported["technique"] == original["technique"]
|
|
assert {key: exported["concept"][key] for key in original["concept"]} == original["concept"]
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["project", "lora"])
|
|
def test_evaluation_identity_mismatch_does_not_write_partial_output(base, tmp_path, mode):
|
|
model, tokenizer = deepcopy(base[:2])
|
|
pack = ConceptPack.load(ROOT / "tests/fixtures/concept_packs/valid_minimal")
|
|
direction = ConceptDirection(
|
|
{0: torch.eye(16)[0]}, concept_id="refusal", pack_hash=None,
|
|
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
|
|
)
|
|
evaluator = ModelEvaluator(model, tokenizer, direction)
|
|
battery = EvaluationBattery(evaluator.generate, measure=evaluator.measure, skip_collateral=True)
|
|
source = tmp_path / "run"
|
|
original = model(torch.tensor([[3, 5, 6]])).logits.detach()
|
|
with pytest.raises(ValueError, match="direction and pack identity disagree"):
|
|
evaluate_intervention(model, direction, mode, battery, output_dir=source, pack=pack)
|
|
assert not source.exists()
|
|
assert torch.equal(model(torch.tensor([[3, 5, 6]])).logits, original)
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["concept_run.json", "concept_provenance.json"])
|
|
def test_evaluation_auto_save_preserves_existing_run_before_mutation(tmp_path, name):
|
|
record = tmp_path / name
|
|
record.write_text("existing run")
|
|
evaluation = tmp_path / "evaluation.json"
|
|
evaluation.write_text("existing evaluation")
|
|
battery = Mock(side_effect=AssertionError("must not evaluate or mutate"))
|
|
with pytest.raises(ValueError, match="without a run record"):
|
|
evaluate_intervention(None, None, "orthogonalize", battery, output_dir=tmp_path)
|
|
battery.capture.assert_not_called()
|
|
battery.finish.assert_not_called()
|
|
assert record.read_text() == "existing run"
|
|
assert evaluation.read_text() == "existing evaluation"
|
|
|
|
|
|
def test_successful_auto_save_has_matching_run_and_battery_reports(base, tmp_path):
|
|
model, tokenizer = deepcopy(base[:2])
|
|
direction = ConceptDirection(
|
|
{0: torch.eye(16)[0]},
|
|
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
|
|
)
|
|
evaluator = ModelEvaluator(model, tokenizer, direction)
|
|
battery = EvaluationBattery(evaluator.generate, measure=evaluator.measure, skip_collateral=True)
|
|
with evaluate_intervention(model, direction, "project", battery, output_dir=tmp_path):
|
|
assert load_run(tmp_path)["evaluation"] == json.loads(
|
|
(tmp_path / "evaluation.json").read_text()
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("evaluation", [None, "skipped"])
|
|
def test_research_pipeline_local_checkpoint_allows_missing_collateral(tmp_path, evaluation):
|
|
from obliteratus.concepts.evaluation import EvaluationReport
|
|
pipeline = AbliterationPipeline(
|
|
"never/load", concept=replace(small_target(), tier="research"),
|
|
output_dir=str(tmp_path / "run"),
|
|
)
|
|
if evaluation == "skipped":
|
|
pipeline._evaluation_report = EvaluationReport(
|
|
collateral={"status": "skipped", "reason": "fixture skip"},
|
|
)
|
|
pipeline._gather_state_dict = Mock(side_effect=RuntimeError("local save reached"))
|
|
with pytest.raises(RuntimeError, match="local save reached"):
|
|
pipeline._rebirth()
|
|
pipeline._gather_state_dict.assert_called_once()
|
|
assert not (tmp_path / "run").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("status", ["skipped", "FAIL"])
|
|
def test_ui_artifact_local_export_allows_research_collateral(tmp_path, status):
|
|
module = ast.parse((ROOT / "app.py").read_text())
|
|
function = next(node for node in module.body
|
|
if isinstance(node, ast.FunctionDef) and node.name == "export_artifacts")
|
|
write_json(tmp_path / "abliteration_metadata.json", {
|
|
"concept": {"tier": "research", "evaluation": {
|
|
"collateral": {"status": status, "reason": "fixture collateral"},
|
|
}},
|
|
})
|
|
import time
|
|
namespace = {"Path": Path, "time": time,
|
|
"_state": {"status": "ready", "output_dir": str(tmp_path)}}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
artifact, message = namespace["export_artifacts"]()
|
|
assert Path(artifact).is_file()
|
|
assert "Export Complete" in message
|
|
Path(artifact).unlink()
|