mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Tourney execution tests now patch the low-disk pre-flight to ample space, and a dedicated test covers the low-disk warning path. Add a CLI-level v1 tied-head migration/apply regression, pinned paired Newcombe interval values (including discordant pairs), and real UTF-8 bytes in the data-files copy test. No production code changes. Fixes #594 Fixes #615
380 lines
16 KiB
Python
380 lines
16 KiB
Python
"""Offline CLI contracts for applying model-bound recipes."""
|
|
|
|
from functools import partial
|
|
from hashlib import sha256
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import abliterate, cli, model_profile
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
MODEL = "org/Model"
|
|
REVISION = "a" * 40
|
|
MARGINS = {"spec_path": "margins/example.json", "spec_sha256": "b" * 64}
|
|
|
|
|
|
def recipe(name="individual", *, match=None, status="accepted", **overrides):
|
|
return {
|
|
"schema": "obliteratus.model-recipe/v1",
|
|
"name": name,
|
|
"version": "1.2.3",
|
|
"scope": "individual" if match is None else "family",
|
|
"identity": (
|
|
{"model_id": MODEL, "revision": REVISION} if match is None else {"match": match}
|
|
),
|
|
"edit": {"method": "basic", "overrides": overrides},
|
|
"margins": MARGINS if status == "accepted" else None,
|
|
"evidence": [],
|
|
"status": status,
|
|
"accepted_by": None,
|
|
"accepted_at": None,
|
|
"notes": "",
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def snapshot(tmp_path):
|
|
path = tmp_path / "hub" / "models--org--Model" / "snapshots" / REVISION
|
|
path.mkdir(parents=True)
|
|
(path / "config.json").write_text(json.dumps({"architectures": ["LlamaForCausalLM"]}))
|
|
return path
|
|
|
|
|
|
@pytest.fixture
|
|
def pipeline(monkeypatch):
|
|
run = Mock(return_value=Path("test-output"))
|
|
monkeypatch.setattr("obliteratus.recipes.application.run_recipe_pipeline", run)
|
|
return run
|
|
|
|
|
|
def pipeline_options(pipeline):
|
|
from obliteratus.recipes.techniques_projection import projection_options
|
|
|
|
step, = pipeline.call_args.args[0]["pipeline"]["steps"]
|
|
params = step["params"]
|
|
return {"method": params["backend"]["method"], **projection_options(params)}
|
|
|
|
|
|
@pytest.fixture
|
|
def profile(monkeypatch):
|
|
config_profile = model_profile.ModelProfile(
|
|
model="local-path", source="test", model_type="llama", num_layers=8,
|
|
hidden_size=512, intermediate_size=1024, vocab_size=32,
|
|
total_params=1_000_000, total_params_b=0.001, active_params_b=0.001,
|
|
)
|
|
stub = Mock(return_value=config_profile)
|
|
monkeypatch.setattr(model_profile, "profile_model", stub)
|
|
return stub
|
|
|
|
|
|
@pytest.fixture
|
|
def write_recipe(tmp_path, monkeypatch):
|
|
from obliteratus.recipes import application
|
|
|
|
directory = tmp_path / "recipes"
|
|
directory.mkdir()
|
|
monkeypatch.setattr(application, "prepare_recipe", partial(
|
|
application.prepare_recipe, directory=directory,
|
|
))
|
|
|
|
def write(record):
|
|
path = directory / ("source-" + record["name"] + ".json")
|
|
path.write_text(json.dumps(record, indent=2) + "\n", encoding="utf-8")
|
|
return path
|
|
|
|
return write
|
|
|
|
|
|
def invoke(model, *options, name="auto"):
|
|
cli.main(["obliterate", str(model), "--recipe", name, *options])
|
|
|
|
|
|
def test_unknown_local_identity_fails_before_profile_or_weights(tmp_path, pipeline, profile, capsys):
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(tmp_path)
|
|
assert exc.value.code == 2
|
|
assert "Cannot establish model identity" in capsys.readouterr().out
|
|
profile.assert_not_called()
|
|
pipeline.assert_not_called()
|
|
|
|
|
|
def test_snapshot_and_explicit_identity(snapshot, pipeline, profile, write_recipe):
|
|
write_recipe(recipe())
|
|
invoke(snapshot)
|
|
assert pipeline.call_args.kwargs["source"] == str(snapshot)
|
|
assert pipeline.call_args.kwargs["metadata"]["model_id"] == MODEL
|
|
assert pipeline.call_args.kwargs["metadata"]["revision"] == REVISION
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "exact"
|
|
|
|
other_revision = "c" * 40
|
|
alternate = recipe("alternate")
|
|
alternate["identity"] = {"model_id": "other/model", "revision": other_revision}
|
|
write_recipe(alternate)
|
|
invoke(snapshot, "--recipe-model-id", "other/model", "--recipe-revision", other_revision)
|
|
assert pipeline.call_args.kwargs["metadata"]["name"] == "alternate"
|
|
|
|
|
|
def test_hub_profile_and_weights_use_same_pinned_snapshot(
|
|
snapshot, pipeline, profile, write_recipe, monkeypatch, capsys,
|
|
):
|
|
config = Mock(return_value=str(snapshot / "config.json"))
|
|
weights = Mock(return_value=str(snapshot))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", config)
|
|
monkeypatch.setattr("huggingface_hub.snapshot_download", weights)
|
|
write_recipe(recipe())
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(MODEL, "--recipe-revision", "wrong", name="individual")
|
|
assert exc.value.code == 2
|
|
assert "--allow-recipe-transfer" in capsys.readouterr().out
|
|
config.assert_not_called()
|
|
weights.assert_not_called()
|
|
pipeline.assert_not_called()
|
|
|
|
invoke(MODEL, "--recipe-revision", REVISION, name="individual")
|
|
config.assert_called_once_with(repo_id=MODEL, filename="config.json", revision=REVISION)
|
|
profile.assert_called_once_with(str(snapshot), dtype="float16", trust_remote_code=False)
|
|
weights.assert_called_once_with(repo_id=MODEL, revision=REVISION)
|
|
assert pipeline.call_args.kwargs["source"] == str(snapshot)
|
|
|
|
config.reset_mock()
|
|
weights.reset_mock()
|
|
write_recipe(recipe("family", match={"model_type": "llama"}))
|
|
invoke(MODEL, "--recipe-revision", "stable")
|
|
config.assert_called_once_with(repo_id=MODEL, filename="config.json", revision="stable")
|
|
weights.assert_called_once_with(repo_id=MODEL, revision=REVISION)
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "family-application"
|
|
|
|
|
|
def test_custom_local_path_uses_explicit_identity(tmp_path, pipeline, profile, write_recipe):
|
|
write_recipe(recipe())
|
|
invoke(tmp_path, "--recipe-model-id", MODEL, "--recipe-revision", REVISION)
|
|
assert pipeline.call_args.kwargs["source"] == str(tmp_path)
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "exact"
|
|
|
|
|
|
@pytest.mark.parametrize("scope", ["individual", "family"])
|
|
def test_unbound_named_recipe_requires_transfer(
|
|
scope, snapshot, pipeline, profile, write_recipe, capsys,
|
|
):
|
|
record = recipe("unbound")
|
|
if scope == "family":
|
|
record = recipe("unbound", match={"model_type": "qwen2"})
|
|
else:
|
|
record["identity"]["revision"] = "d" * 40
|
|
write_recipe(record)
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(snapshot, name="unbound")
|
|
assert exc.value.code == 2
|
|
assert "--allow-recipe-transfer" in capsys.readouterr().out
|
|
pipeline.assert_not_called()
|
|
|
|
invoke(snapshot, "--allow-recipe-transfer", name="unbound")
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "transfer"
|
|
|
|
|
|
def test_candidate_opt_in_and_accepted_precedence(snapshot, pipeline, profile, write_recipe, capsys):
|
|
write_recipe(recipe("candidate", status="candidate"))
|
|
for name in ("auto", "candidate"):
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(snapshot, name=name)
|
|
assert exc.value.code == 2
|
|
assert "recipe" in capsys.readouterr().out
|
|
pipeline.assert_not_called()
|
|
invoke(snapshot, "--allow-candidate-recipe", name=name)
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "candidate"
|
|
pipeline.reset_mock()
|
|
|
|
write_recipe(recipe("family", match={"model_type": "llama"}))
|
|
invoke(snapshot, "--allow-candidate-recipe")
|
|
assert pipeline.call_args.kwargs["metadata"]["name"] == "family"
|
|
|
|
|
|
def test_auto_surfaces_family_ambiguity(snapshot, pipeline, profile, write_recipe, capsys):
|
|
for name in ("family-b", "family-a"):
|
|
write_recipe(recipe(name, match={"model_type": "llama"}))
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(snapshot)
|
|
assert exc.value.code == 2
|
|
assert "ambiguous family recipes" in capsys.readouterr().out
|
|
pipeline.assert_not_called()
|
|
|
|
|
|
def test_exact_family_kwargs_and_inherited_metadata(snapshot, pipeline, profile, write_recipe):
|
|
family = recipe(
|
|
"family",
|
|
match={"model_type": "llama", "architectures": ["LlamaForCausalLM"],
|
|
"model_id_patterns": ["org/*"]},
|
|
layers={"start": 0.25, "end": 0.75}, extraction_layer=0.5,
|
|
use_chat_template=True, fold_post_norms=False, project_router=False,
|
|
norm_preserve=False, regularization=0.25, shield_residualize=False,
|
|
)
|
|
write_recipe(family)
|
|
|
|
invoke(snapshot, "--n-directions", "2")
|
|
assert pipeline.call_args.kwargs["metadata"]["application"] == "family-application"
|
|
expected = {
|
|
**family["edit"]["overrides"], "extraction_layer": {"fraction": 0.5},
|
|
"method": "basic", "n_directions": 2,
|
|
}
|
|
assert {key: pipeline_options(pipeline)[key] for key in expected} == expected
|
|
assert pipeline_options(pipeline)["refinement_passes"] == 1
|
|
|
|
child = recipe(regularization=0.0)
|
|
child["inherits"] = "family"
|
|
del child["edit"]["method"]
|
|
del child["margins"]
|
|
path = write_recipe(child)
|
|
invoke(snapshot)
|
|
meta = pipeline.call_args.kwargs["metadata"]
|
|
assert meta == {
|
|
"name": "individual", "version": "1.2.3", "scope": "individual", "status": "accepted",
|
|
"digest": "sha256:" + sha256(path.read_bytes()).hexdigest(),
|
|
"model_id": MODEL, "revision": REVISION,
|
|
"chain": ["family", "individual"],
|
|
"layers": {"start": 0.25, "end": 0.75}, "extraction_layer": {"fraction": 0.5},
|
|
"application": "exact", "cli_overrides": {},
|
|
}
|
|
assert pipeline_options(pipeline)["regularization"] == 0.0
|
|
|
|
|
|
def test_explicit_cli_collisions_and_negative_flags(
|
|
snapshot, pipeline, profile, write_recipe, capsys,
|
|
):
|
|
write_recipe(recipe(
|
|
layers=[2, 3], extraction_layer=2, use_chat_template=True, fold_post_norms=True,
|
|
project_router=False, regularization=0.5,
|
|
))
|
|
options = (
|
|
"--method=advanced", "--layers", "0,1", "--extraction-layer", "0",
|
|
"--no-chat-template", "--no-fold-post-norms", "--project-router", "--regularization", "0",
|
|
)
|
|
with pytest.raises(SystemExit) as exc:
|
|
invoke(snapshot, *options, name="individual")
|
|
assert exc.value.code == 2
|
|
assert "--allow-recipe-override" in capsys.readouterr().out
|
|
pipeline.assert_not_called()
|
|
|
|
invoke(snapshot, *options, "--allow-recipe-override", name="individual")
|
|
expected = {
|
|
"method": "advanced", "layers": [0, 1], "extraction_layer": 0,
|
|
"use_chat_template": False, "fold_post_norms": False, "project_router": True,
|
|
"regularization": 0.0,
|
|
}
|
|
assert {key: pipeline_options(pipeline)[key] for key in expected} == expected
|
|
assert pipeline.call_args.kwargs["metadata"]["cli_overrides"] == expected
|
|
assert pipeline.call_args.kwargs["metadata"]["layers"] == [2, 3]
|
|
|
|
|
|
def test_recipe_dispatch_preserves_bound_metadata(snapshot, pipeline, profile, write_recipe):
|
|
path = write_recipe(recipe(extraction_layer=0))
|
|
invoke(snapshot)
|
|
metadata = pipeline.call_args.kwargs["metadata"]
|
|
assert metadata["name"] == "individual"
|
|
assert metadata["application"] == "exact"
|
|
assert metadata["extraction_layer"] == 0
|
|
assert metadata["digest"] == "sha256:" + sha256(path.read_bytes()).hexdigest()
|
|
assert "recipe" not in abliterate.AbliterationPipeline("offline")._build_metadata()
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
def test_remote_rejects_recipe_before_connection(command, monkeypatch, capsys):
|
|
connect = Mock()
|
|
monkeypatch.setattr(cli, "_make_remote_runner", connect)
|
|
with pytest.raises(SystemExit) as exc:
|
|
cli.main([command, MODEL, "--recipe", "auto", "--remote", "test-host"])
|
|
assert exc.value.code == 2
|
|
assert "--recipe currently requires a local" in capsys.readouterr().out
|
|
connect.assert_not_called()
|
|
|
|
|
|
@pytest.mark.usefixtures("tiny_model_runtime")
|
|
def test_obliterate_v2_pipeline_writes_resolution(tmp_path, write_recipe, monkeypatch):
|
|
from obliteratus.recipes.resolution_records import read_checkpoint_resolution
|
|
from obliteratus.verify_edit.heldout import QWEN38_SPLIT_SHA256
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
|
|
stock = build_tiny_offline_model(tmp_path / "stock")
|
|
output = tmp_path / "edited"
|
|
record = {
|
|
"schema": "obliteratus.model-recipe/v2", "name": "tiny-v2", "version": "1.0.0",
|
|
"engine": {"min_version": "0.1.4", "normalizer": "recipe-normalizer/1"},
|
|
"target": {"scope": "individual", "identity": {"model_id": MODEL, "revision": REVISION}},
|
|
"status": "accepted",
|
|
"inputs": {"train": {"kind": "protocol_split", "dataset": "qwen38-v1", "role": "train",
|
|
"split": "train", "sha256": QWEN38_SPLIT_SHA256}},
|
|
"pipeline": {"steps": [{"id": "tiny-projection", "technique": "abliteration.projection",
|
|
"technique_version": "1", "inputs": ["train"],
|
|
"placement": {"layers": [0]}, "params": {
|
|
"preset": "basic", "lm_head": "skip",
|
|
"overrides": {"small_model_safeguards": False},
|
|
}}]},
|
|
}
|
|
write_recipe(record)
|
|
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", Mock())
|
|
invoke(stock, "--recipe-model-id", MODEL, "--recipe-revision", REVISION, "--device", "cpu",
|
|
"--dtype", "float32", "--output-dir", str(output), "--record-reference")
|
|
resolution = read_checkpoint_resolution(output)
|
|
assert resolution["target"]["model_id"] == MODEL
|
|
assert resolution["steps"][0]["id"] == "tiny-projection"
|
|
assert resolution["steps"][0]["payload"]["resolved_layers"] == [0]
|
|
assert resolution["input_sha256"] == {"train": QWEN38_SPLIT_SHA256}
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_bytes())
|
|
assert metadata["recipe"]["name"] == "tiny-v2"
|
|
assert metadata["recipe"]["application"] == "exact"
|
|
assert metadata["recipe"]["digest"] == "sha256:" + resolution["recipe_sha256"]
|
|
from obliteratus.verify_checkpoint import REFERENCE_FILENAME
|
|
assert (output / REFERENCE_FILENAME).is_file()
|
|
|
|
from obliteratus.recipes import RecipeError
|
|
from obliteratus.verify_edit.random_edit import load_reference_edit
|
|
|
|
with pytest.raises(RecipeError, match="random control cannot match a skipped projection head"):
|
|
load_reference_edit(output)
|
|
|
|
|
|
@pytest.mark.usefixtures("tiny_model_runtime")
|
|
def test_obliterate_v1_recipe_keeps_tied_head_edit(tmp_path, write_recipe, monkeypatch, capsys):
|
|
"""A v1 recipe applied through the CLI still edits a tied head, as v1 did (#588, #594)."""
|
|
from safetensors.torch import load_file
|
|
|
|
from obliteratus.recipes import RecipeRegistry
|
|
from obliteratus.recipes.resolution_records import read_checkpoint_resolution
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
|
|
stock = build_tiny_offline_model(tmp_path / "stock")
|
|
stock_weights = load_file(stock / "model.safetensors")
|
|
assert "lm_head.weight" not in stock_weights # GPT-2 ties the head to the input embedding
|
|
output = tmp_path / "edited"
|
|
source = write_recipe(recipe("tiny-v1", layers=[0], small_model_safeguards=False))
|
|
assert json.loads(source.read_bytes())["schema"] == "obliteratus.model-recipe/v1"
|
|
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", Mock())
|
|
|
|
invoke(stock, "--recipe-model-id", MODEL, "--recipe-revision", REVISION, "--device", "cpu",
|
|
"--dtype", "float32", "--output-dir", str(output), name="tiny-v1")
|
|
|
|
assert "migrating to v2 in memory" in capsys.readouterr().err
|
|
migrated = RecipeRegistry(source.parent).load("tiny-v1")
|
|
assert migrated["schema"] == "obliteratus.model-recipe/v2"
|
|
assert migrated["notes"].endswith("Migration preserves v1 tied-head behaviour.")
|
|
step, = migrated["pipeline"]["steps"]
|
|
assert step["params"]["allow_tied_head_edit"] is True
|
|
assert "lm_head" not in step["params"]
|
|
|
|
payload = read_checkpoint_resolution(output)["steps"][0]["payload"]
|
|
assert payload["tied_head"] == {"tied_embeddings": True, "decision": "allowed"}
|
|
assert payload["resolved_writers"]["lm_head"] == ["lm_head"]
|
|
assert payload["effective_params"]["lm_head"] == "project"
|
|
assert payload["effective_params"]["allow_tied_head_edit"] is True
|
|
# Embedding projection is off, so a changed shared matrix is the head edit itself.
|
|
assert payload["effective_params"]["embeddings"]["project"] is False
|
|
edited_weights = load_file(output / "model.safetensors")
|
|
name = "transformer.wte.weight"
|
|
assert not torch.equal(edited_weights[name], stock_weights[name])
|