mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Resolve against #593 (quick-check settings) and #591 (tied-head migration) by keeping both sides: the pipeline gains analysis_device alongside refusal_check_mode/confidence; migration and the shipped recipes emit allow_tied_head_edit and the analysis_device cpu pin; _structured relaxes both the refusal-check settings and analysis_device for historical resolution records; both sides' tests are kept.
300 lines
12 KiB
Python
300 lines
12 KiB
Python
"""Offline schema, technique registration, normalization and migration contracts."""
|
|
|
|
from copy import deepcopy
|
|
from hashlib import sha256
|
|
from importlib.resources import files
|
|
import json
|
|
|
|
from jsonschema import Draft202012Validator
|
|
import pytest
|
|
|
|
from obliteratus.qualification import canonical_json
|
|
from obliteratus.recipes import RecipeError, RecipeRegistry, load_recipe, validate_recipe
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.fixture
|
|
def recipe():
|
|
return {
|
|
"schema": "obliteratus.model-recipe/v2",
|
|
"name": "example",
|
|
"version": "1.0.0",
|
|
"engine": {"min_version": "0.1.4", "normalizer": "recipe-normalizer/1"},
|
|
"target": {"scope": "individual", "identity": {"model_id": "org/model", "revision": "rev"}},
|
|
"pipeline": {"steps": [
|
|
{"id": "first", "technique": "test.stub", "technique_version": "1", "params": {}},
|
|
]},
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def schema_validator():
|
|
path = files("obliteratus.recipes").joinpath("schema", "model-recipe.v2.schema.json")
|
|
schema = json.loads(path.read_text(encoding="utf-8"))
|
|
Draft202012Validator.check_schema(schema)
|
|
return Draft202012Validator(schema)
|
|
|
|
|
|
def test_load_v2_core_record(tmp_path, recipe):
|
|
path = tmp_path / "v2.json"
|
|
path.write_text(json.dumps(recipe), encoding="utf-8")
|
|
assert load_recipe(path) == recipe
|
|
|
|
|
|
def test_valid_v2_formats(recipe, schema_validator):
|
|
schema_validator.validate(recipe)
|
|
validate_recipe(recipe)
|
|
recipe["target"] = {
|
|
"scope": "family",
|
|
"match": {"model_type": ["llama"], "num_layers": {"min": 8, "max": 24}},
|
|
"requires": {
|
|
"architecture": ["LlamaForCausalLM"], "tie_word_embeddings": False,
|
|
"width": {"min": 32, "max": 128}, "kv_heads": [4, 8],
|
|
},
|
|
}
|
|
recipe["inputs"] = {
|
|
"train": {"kind": "dataset", "id": "sample", "sha256": "opaque", "role": "train"},
|
|
}
|
|
step = recipe["pipeline"]["steps"][0]
|
|
step.update(placement={"custom_rule": ["kept", 2]}, inputs=["train"], outputs=["model"])
|
|
recipe["pipeline"]["steps"].append(dict(step, id="second", inputs=["model"], outputs=[]))
|
|
recipe["evaluation"] = {"profile": "profiles/example.json", "sha256": "a" * 64}
|
|
recipe["evidence"] = [{"kind": "search", "path": "run.json", "sha256": "0" * 64}]
|
|
schema_validator.validate(recipe)
|
|
validate_recipe(recipe)
|
|
|
|
|
|
@pytest.mark.parametrize(("modify", "message"), [
|
|
(lambda r: r.update(extra=True), "unknown field extra"),
|
|
(lambda r: r["target"].update(scope="fleet"), r"target.scope"),
|
|
(lambda r: r["target"].update(match={"model_type": "llama"}), r"target.match"),
|
|
(lambda r: r["target"].update(scope="family"), r"target.*missing match"),
|
|
(lambda r: r["target"].update(requires={"width": {"min": "wide"}}), r"target.requires.width"),
|
|
(lambda r: r.update(inputs={"x": {"kind": "data", "id": "set", "sha256": "x",
|
|
"role": "validation"}}), r"inputs.x.role"),
|
|
(lambda r: r.update(inputs={"x": ["untyped"]}), r"inputs.x"),
|
|
(lambda r: r["pipeline"]["steps"][0].update(placement="middle"), r"steps\[0\].placement"),
|
|
(lambda r: r["pipeline"]["steps"][0].update(outputs={"model": "x"}), r"steps\[0\].outputs"),
|
|
(lambda r: r["pipeline"]["steps"].append(deepcopy(r["pipeline"]["steps"][0])), "duplicate"),
|
|
(lambda r: r.update(canonical_sha256="z" * 64), "canonical_sha256"),
|
|
])
|
|
def test_invalid_v2_schema_parity(recipe, schema_validator, modify, message):
|
|
modify(recipe)
|
|
assert not schema_validator.is_valid(recipe)
|
|
with pytest.raises(RecipeError, match=message):
|
|
validate_recipe(recipe)
|
|
|
|
|
|
def test_distinct_steps_cannot_share_an_id(recipe):
|
|
first, = recipe["pipeline"]["steps"]
|
|
recipe["pipeline"]["steps"].append(dict(first, params={"depth": 3}))
|
|
with pytest.raises(RecipeError, match="duplicate step id 'first'"):
|
|
validate_recipe(recipe)
|
|
|
|
|
|
def test_v2_json_loading_is_strict(tmp_path, recipe):
|
|
path = tmp_path / "v2.json"
|
|
contents = json.dumps(recipe)
|
|
for replacement, message in (
|
|
('{"n": 0, "n": 1}', "duplicate JSON key"),
|
|
('{"n": 1e999}', "non-finite JSON number"),
|
|
):
|
|
path.write_text(contents.replace('"params": {}', '"params": ' + replacement),
|
|
encoding="utf-8")
|
|
with pytest.raises(RecipeError, match=f"v2.json.*{message}"):
|
|
load_recipe(path)
|
|
|
|
|
|
def test_registry_indexes_v2_records(tmp_path, recipe):
|
|
(tmp_path / "recipe.json").write_text(json.dumps(recipe), encoding="utf-8")
|
|
assert RecipeRegistry(tmp_path).load(recipe["name"])["pipeline"] == recipe["pipeline"]
|
|
|
|
|
|
@pytest.fixture
|
|
def techniques():
|
|
from obliteratus.recipes import TechniqueRegistry, TechniqueSpec
|
|
|
|
calls = []
|
|
|
|
def normalize(params, context):
|
|
calls.append(context)
|
|
params.setdefault("depth", 2)
|
|
return params
|
|
|
|
def apply(state, *, params, placement, inputs, constraints):
|
|
pytest.fail("normalization must never apply a technique")
|
|
|
|
spec = TechniqueSpec(
|
|
id="test.stub", version="1",
|
|
parameter_schema={
|
|
"type": "object", "additionalProperties": False,
|
|
"properties": {"depth": {"type": "integer", "minimum": 1}},
|
|
},
|
|
normalize=normalize, apply=apply, resolution_schema={"type": "object"},
|
|
)
|
|
registry = TechniqueRegistry()
|
|
registry.register_technique(spec)
|
|
return registry, spec, calls
|
|
|
|
|
|
def test_technique_registry_is_versioned(techniques):
|
|
from dataclasses import replace
|
|
|
|
registry, spec, _ = techniques
|
|
newer = replace(spec, version="2")
|
|
registry.register_technique(newer)
|
|
assert registry.lookup("test.stub", "1") is spec
|
|
assert registry.lookup("test.stub", "2") is newer
|
|
with pytest.raises(RecipeError, match=r"already registered.*test.stub.*1"):
|
|
registry.register_technique(spec)
|
|
with pytest.raises(RecipeError, match=r"unknown technique.*test.missing.*1"):
|
|
registry.lookup("test.missing", "1")
|
|
with pytest.raises(RecipeError, match=r"unknown technique.*test.stub.*3"):
|
|
registry.lookup("test.stub", "3")
|
|
with pytest.raises(RecipeError, match="unsupported.*patternProperties"):
|
|
registry.register_technique(replace(newer, version="4", parameter_schema={
|
|
"patternProperties": {"^a": {"type": "number"}},
|
|
}))
|
|
|
|
|
|
def test_normalizer_checks_input_and_returned_parameters(recipe, techniques):
|
|
from dataclasses import replace
|
|
|
|
from obliteratus.recipes import normalize_recipe
|
|
|
|
registry, spec, calls = techniques
|
|
step, = recipe["pipeline"]["steps"]
|
|
step["params"] = {"depth": 0}
|
|
with pytest.raises(RecipeError, match=r"steps\[0\].params.depth"):
|
|
normalize_recipe(recipe, registry=registry)
|
|
assert calls == []
|
|
|
|
registry.register_technique(replace(spec, version="2", normalize=lambda p, c: {"depth": False}))
|
|
step.update(technique_version="2", params={})
|
|
with pytest.raises(RecipeError, match=r"steps\[0\].params.depth"):
|
|
normalize_recipe(recipe, registry=registry)
|
|
|
|
|
|
def test_normalizer_rejects_unknown_versions(recipe, techniques):
|
|
from obliteratus.recipes import normalize_recipe
|
|
|
|
registry, _, _ = techniques
|
|
step, = recipe["pipeline"]["steps"]
|
|
step["technique_version"] = "2"
|
|
with pytest.raises(RecipeError, match=r"unknown technique.*test.stub.*2"):
|
|
normalize_recipe(recipe, registry=registry)
|
|
step["technique_version"] = "1"
|
|
recipe["engine"]["normalizer"] = "recipe-normalizer/2"
|
|
with pytest.raises(RecipeError, match=r"engine.normalizer.*recipe-normalizer/2"):
|
|
normalize_recipe(recipe, registry=registry)
|
|
|
|
|
|
def test_normalizer_materializes_defaults_and_hashes_ordered_steps(recipe, techniques):
|
|
from obliteratus.recipes import normalize_recipe
|
|
|
|
registry, _, calls = techniques
|
|
recipe["pipeline"]["steps"].append(
|
|
dict(recipe["pipeline"]["steps"][0], id="second", params={"depth": 4}),
|
|
)
|
|
before = deepcopy(recipe)
|
|
normalized = normalize_recipe(recipe, registry=registry)
|
|
assert recipe == before
|
|
assert [context.step_id for context in calls] == ["first", "second"]
|
|
assert calls[0].target == normalized["target"]
|
|
assert calls[0].inputs == {}
|
|
assert normalized["inputs"] == normalized["target"]["requires"] == {}
|
|
assert normalized["evaluation"] is normalized["accepted_by"] is normalized["accepted_at"] is None
|
|
assert normalized["status"] == "candidate"
|
|
assert normalized["evidence"] == []
|
|
assert normalized["notes"] == ""
|
|
steps = normalized["pipeline"]["steps"]
|
|
assert [step["params"] for step in steps] == [{"depth": 2}, {"depth": 4}]
|
|
for step in steps:
|
|
assert step["placement"] == step["constraints"] == {}
|
|
assert step["inputs"] == step["outputs"] == []
|
|
assert list(normalized) == sorted(normalized)
|
|
assert list(steps[0]) == sorted(steps[0])
|
|
assert normalize_recipe(normalized, registry=registry) == normalized
|
|
|
|
unsigned = dict(normalized)
|
|
digest = unsigned.pop("canonical_sha256")
|
|
assert digest == sha256(canonical_json(unsigned)).hexdigest()
|
|
reordered = {key: recipe[key] for key in reversed(recipe)}
|
|
reordered["target"] = dict(reversed(list(recipe["target"].items())))
|
|
assert normalize_recipe(reordered, registry=registry) == normalized
|
|
recipe["pipeline"]["steps"].reverse()
|
|
assert normalize_recipe(recipe, registry=registry)["canonical_sha256"] != digest
|
|
|
|
|
|
def test_bundled_recipes_use_the_v2_schema(schema_validator):
|
|
from obliteratus.recipes import V1_AUTHORING_ALLOWED
|
|
|
|
assert V1_AUTHORING_ALLOWED is False
|
|
registry = RecipeRegistry()
|
|
for name in registry.list():
|
|
path = registry.source(name)
|
|
record = load_recipe(path)
|
|
assert record == json.loads(path.read_text(encoding="utf-8"))
|
|
schema_validator.validate(record)
|
|
validate_recipe(record)
|
|
step, = record["pipeline"]["steps"]
|
|
assert (step["technique"], step["technique_version"]) == ("abliteration.projection", "1")
|
|
|
|
|
|
@pytest.mark.parametrize("name", [
|
|
"llama3-instruct-family-e03", "llama3.1-8b-e03",
|
|
"qwen2.5-1.5b-mid12-16", "qwen38-27b-e03",
|
|
])
|
|
def test_bundled_v1_derived_recipes_declare_legacy_head_intent(name):
|
|
from obliteratus.recipes import normalize_recipe
|
|
|
|
record = RecipeRegistry().load(name)
|
|
assert "preserves v1 tied-head behaviour" in record["notes"]
|
|
step, = normalize_recipe(record)["pipeline"]["steps"]
|
|
assert step["params"]["allow_tied_head_edit"] is True
|
|
assert step["params"]["lm_head"] == "project"
|
|
|
|
|
|
@pytest.mark.parametrize("target", ["all", "attention", "ffn", "output"])
|
|
def test_v1_layer_projection_targets_keep_head_intent_and_notes(target):
|
|
from obliteratus.recipes import migrate_v1, normalize_recipe
|
|
from tests.test_family_recipes import individual
|
|
|
|
record = individual()
|
|
record["notes"] = "Original note."
|
|
record["edit"]["overrides"] = {"projection_target": target, "project_embeddings": False}
|
|
original = deepcopy(record)
|
|
|
|
migrated = migrate_v1(record)
|
|
assert record == original
|
|
assert migrated["notes"].startswith(record["notes"])
|
|
assert "preserves v1 tied-head behaviour" in migrated["notes"]
|
|
step, = normalize_recipe(migrated)["pipeline"]["steps"]
|
|
assert step["params"]["backend"]["projection_target"] == target
|
|
assert step["params"]["embeddings"]["project"] is False
|
|
assert step["params"]["allow_tied_head_edit"] is True
|
|
assert step["params"]["lm_head"] == "project"
|
|
|
|
|
|
def test_v1_inheritance_keeps_missing_and_null_margins_distinct(schema_validator):
|
|
from obliteratus.recipes import migrate_v1
|
|
|
|
from tests.test_family_recipes import family, individual
|
|
|
|
parent = family()
|
|
child = individual(status="candidate")
|
|
child.update(inherits=parent["name"], edit={"overrides": {"n_directions": 1}})
|
|
del child["margins"]
|
|
migrated = migrate_v1(child)
|
|
schema_validator.validate(migrated)
|
|
assert migrated["inherits"] == migrate_v1(parent)["name"]
|
|
assert "evaluation" not in migrated
|
|
assert migrated["pipeline"]["steps"][0]["params"] == {
|
|
"allow_tied_head_edit": True, "overrides": {"n_directions": 1, "analysis_device": "cpu"},
|
|
}
|
|
child["margins"] = None
|
|
assert migrate_v1(child)["evaluation"] is None
|
|
with pytest.raises(RecipeError, match="v1"):
|
|
migrate_v1(migrated)
|