Files
OBLITERATUS/tests/test_smaller_change.py
Joseph Magly 51fa9e2213 ci(policy): rebaseline release duration budgets, speed up slow tests, enforce per-test budgets on PRs
Rebaselines the release suite and marker duration budgets to the slowest
measured hosted release time + 25% (run 45678651612; suite 3910 s on py3.11
and 5340 s on py3.12; cpu 2010, integration 700, unmarked 2460) and owns every
test whose time + 25% exceeds 15 s under #679 with a 90-day review (follow-up
#684). Speeds up test_smaller_change rank1 (geometry environments built lazily)
and the recipe-search fake edit step (a light helper module). The PR core gate
now enforces per-test budgets on each shard's JUnit with a new
--testcase-budgets-only mode; suite and marker totals stay release-only.

Refs #679
2026-11-25 15:33:27 -05:00

311 lines
16 KiB
Python

"""Smaller-change study (O08): outcome tests on planted fixture models.
Each fixture is the persistent-candidate model of ``tests/test_persistent_candidates.py``
(layer 0 a plain identity linear map; the head answers "yes" when channel 2 > 2)
with number-token embeddings chosen to plant a geometry, and an ablation of
``channel:layer.0:2`` made ``persistent-pilot eligible`` under the test-only fixture
allowance. Development rows come from a third pack revision; verification and
qualification rows from a fourth, whose neutral controls carry their correct answer
"no" so collateral damage shows as a regression. Neither overlaps discovery or any registered
confirmation set.
* ``rank1``: all variance and all class contrast on channel 2 (a rank-1 change suffices).
* ``tail``: four high-variance nuisance channels; the signal is the low-variance channel 2.
* ``mixed``: the head also reads channel 0; a declared change mixing channels 0 and 2
removes the target behaviour but flips control rows (a collateral failure).
"""
from __future__ import annotations
import pytest
import torch
from obliteratus.analysis.target_dossier import build_dossier, write_dossier
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import smaller_change as sc
from obliteratus.experiments import smaller_change_ledger as ledger
from obliteratus.experiments.candidate_edits import CandidateRefused
from obliteratus.experiments.smaller_change_spec import StudyRefused, load_config
from tests.test_backend_qualification import BackendIO
from tests.test_confirmation_registry import _partition
from tests.test_controlled_interventions import AnswerTokenizer, _run
from tests.test_labeled_activations import WIDTH, build_pack
from tests.test_persistent_candidates import (
ABLATE, SumAnswerModel, ablate, make_eligible,
)
from tests.test_target_dossier import HYPOTHESIS, LEXICAL, REPORT
pytestmark = pytest.mark.cpu
GEOMETRIES = {
"rank1": {"noise": {c: 0.05 for c in range(WIDTH) if c != 2}},
"tail": {"noise": {0: 15.0, 1: 15.0, 3: 15.0, 4: 15.0}},
"mixed": {"noise": {c: 0.05 for c in range(WIDTH) if c not in (0, 2)},
"words": {"alpha": {0: -4.0}, "beta": {0: 6.0}}, "reads_zero": 1.0},
}
class GeometryModel(SumAnswerModel):
def __init__(self, noise, words=None, reads_zero=0.0):
super().__init__()
vocab = AnswerTokenizer().vocab
generator = torch.Generator().manual_seed(11)
with torch.no_grad():
for n in range(400):
row = torch.zeros(WIDTH)
for channel, scale in noise.items():
row[channel] = float(torch.randn((), generator=generator)) * scale
self.embed.weight[vocab[str(n)]] = row
for word, values in (words or {}).items():
for channel, value in values.items():
self.embed.weight[vocab[word], channel] = value
self.head.weight[vocab["yes"], 0] = reads_zero
def _shift(offset):
def mutate(layout):
_partition(layout)
for path, rows in layout.items():
if not path.startswith("battery/"):
for row in rows:
word, _, number = row["text"].rpartition(" ")
row["text"] = f"{word} {(int(number) + offset) % 400}"
if path == "controls/neutral.jsonl":
row["answer"] = "no" # the correct answer, so damage is a regression
return mutate
def _dataset(root, name, offset):
pack = build_pack(root / name, _shift(offset))
return {"pack": str(root / name), "revision": pack.dataset().revision, "role": "select"}
def build_env(root, geometry, replicate=True):
from tests.test_persistent_candidates import FixtureIO
pack = build_pack(root / "pack", _partition)
pack.location = root / "pack"
model = GeometryModel(**GEOMETRIES[geometry])
FixtureIO().save(model, None, root / "reference")
env = {"pack": pack, "model": model, "source": root / "reference",
"verify": _dataset(root, "fresh", 80), "development": _dataset(root, "dev", 160)}
run = _run(root, pack, "base", model, "local/answers")
write_dossier(build_dossier("abl", run, hypothesis={**HYPOTHESIS, **ABLATE},
discovery=REPORT, estimates=["channel:layer.0:2"],
confound_resolutions=[LEXICAL]), run)
env["run"], env["registration"] = run, make_eligible(env, run, ablate, replicate=replicate)
return env
class _ModuleEnvs(dict):
"""Build each geometry once per module, on first use by a test that needs it."""
def __init__(self, tmp_path_factory):
super().__init__()
self._tmp_path_factory = tmp_path_factory
def __missing__(self, name):
if name not in GEOMETRIES:
raise KeyError(name)
env = self[name] = build_env(self._tmp_path_factory.mktemp(name), name)
return env
@pytest.fixture(scope="module")
def envs(tmp_path_factory):
# Same per-geometry environments and module sharing as an eager dict, but the
# first test no longer pays for geometries only later tests use.
return _ModuleEnvs(tmp_path_factory)
def config(env, **changes):
found = {"schema_version": "1", "kind": "smaller_change_study", "dataset": env["verify"],
"development": env["development"], "deployment_tokens": 1_000_000}
found.update(changes)
return found
def study(env, **changes):
return sc.run_smaller_change(env["run"], env["registration"], env["source"],
config(env, **changes), model_io=BackendIO(),
pack_resolver=ConceptPack.load, allow_fixture_results=True)
def variant(report, name, seed=0):
return next(v for v in report["variants"] if v["variant"] == name and v["seed"] == seed)
def failed(record):
return set(record["requirements"]["failed"])
def test_rank1_change_passes_with_smaller_artifact_and_states_unchanged_inference(envs):
report = study(envs["rank1"])
assert report["scope"]["scope"] == "software-contract"
assert report["registration"]["maturity"] == "persistent-pilot eligible"
for seed in (0, 1, 2):
broad, geo = variant(report, "broad_baseline", seed), variant(report, "geometry", seed)
assert broad["rank"] == 4 and geo["rank"] == 1
assert broad["requirements"]["passed"] and geo["requirements"]["passed"]
assert [q["status"] for q in geo["requirements"]["qualification"]] == ["qualified"]
assert geo["resources"]["stored_elements"] < broad["resources"]["stored_elements"]
assert geo["resources"]["artifact_bytes"] < broad["resources"]["artifact_bytes"]
assert "stored_elements" in geo["comparison"]["claimed"]
per = variant(report, "geometry")["resources"]["inference"]["per_representation"]
assert per["weight_edit"]["extra_flops_per_token"] == 0
assert per["weight_edit"]["statement"] == ledger.PERMANENT
assert "inference FLOPs are unchanged" in ledger.PERMANENT
# Ordinary variance-only rank selection finds the same rank-1 change.
assert report["verdict"] == "improvement"
assert {"geometry", "variance_only"} <= set(report["improving_variants"])
assert report["attribution"]["to"] is None
assert "variance_only" in report["attribution"]["detail"]
stored = sc.json.loads((sc.Path(report["location"]) / sc.REPORT).read_text())
assert sc.report_digest(stored) == stored["report_sha256"] == report["report_sha256"]
def test_permanent_edit_saves_no_inference_flops_and_no_storage(envs):
report = study(envs["rank1"], seeds=[0], min_seeds_for_recommendation=1,
representation="weight_edit", arms={"rank_sweep": None})
broad, geo = variant(report, "broad_baseline"), variant(report, "geometry")
assert geo["edit"]["representation"] == broad["edit"]["representation"] == "weight_edit"
assert geo["requirements"]["passed"], failed(geo)
assert geo["edit"]["changed_parameters"] == ["layers.0.weight"]
assert geo["resources"]["inference"]["extra_flops_per_token"] == 0
assert geo["resources"]["inference"]["statement"] == ledger.PERMANENT
assert geo["resources"]["stored_elements"] == broad["resources"]["stored_elements"]
assert "inference_flops_per_token" not in geo["comparison"]["improved"]
assert report["verdict"] == "no improvement"
def test_low_variance_signal_defeats_variance_only_selection(envs):
report = study(envs["tail"], arms={"rank_sweep": None})
for seed in (0, 1, 2):
geo, var = variant(report, "geometry", seed), variant(report, "variance_only", seed)
estimation = geo["estimation"]
assert estimation["variance_only_rank"] == 4 and estimation["selected_rank"] == 5
assert estimation["signal_loss_fraction"][3] > 0.02
assert var["rank"] == 4 and "intended_effect" in failed(var)
assert var["requirements"]["verification"]["comparison"]["effect"] == 0
assert geo["requirements"]["passed"], failed(geo)
assert var["comparison"]["claimed"] == []
# The signal-aware rank is larger than the rank-4 baseline: nothing to claim.
assert report["verdict"] == "no improvement"
def test_savings_not_claimed_when_estimation_and_search_costs_exceed_them(envs):
report = study(envs["rank1"], deployment_tokens=0)
for seed in (0, 1, 2):
for name in ("geometry", "variance_only", "rank_sweep"):
record = variant(report, name, seed)
comparison = record["comparison"]
assert record["requirements"]["passed"]
assert "stored_elements" in comparison["improved"]
assert not comparison["costs_covered"] and comparison["claimed"] == []
assert "costs exceed the saving" in comparison["reason"]
sweep = variant(report, "rank_sweep")
assert sweep["search"]["tokens"] > 0 and sweep["search"]["tried"][0]["passed"]
assert report["verdict"] == "no improvement" and report["attribution"] is None
def test_smaller_change_failing_collateral_is_rejected(envs):
declared = [{"name": "mixed", "rows": [[-1.0, 0, 1.0] + [0.0] * (WIDTH - 3)]}]
report = study(envs["mixed"], arms={"declared": declared, "rank_sweep": None})
for seed in (0, 1, 2):
change, broad = variant(report, "declared:mixed", seed), variant(
report, "broad_baseline", seed)
assert broad["requirements"]["passed"], failed(broad)
assert change["rank"] == 1
assert change["resources"]["stored_elements"] < broad["resources"]["stored_elements"]
checks = {c["check"]: c["passed"]
for c in change["requirements"]["verification"]["checks"]}
assert checks["intended_effect"] and not checks["collateral_endpoints"]
assert change["comparison"]["claimed"] == []
assert "collateral_endpoints" in change["comparison"]["reason"]
assert "declared:mixed" not in report["improving_variants"]
def test_immature_target_is_refused_before_loading(tmp_path):
env = build_env(tmp_path, "rank1", replicate=False)
io = BackendIO()
with pytest.raises(CandidateRefused, match="persistent-pilot eligible"):
sc.run_smaller_change(env["run"], env["registration"], env["source"], config(env),
model_io=io, pack_resolver=ConceptPack.load,
allow_fixture_results=True)
assert io.loads == [] and not (env["run"] / sc.STUDY_DIR).exists()
def test_config_and_dataset_refusals(envs):
env = envs["rank1"]
base = config(env)
for bad, match in (({**base, "seeds": []}, "seeds"),
({**base, "extra": 1}, "Unknown"),
({**base, "kind": "x"}, "kind"),
({**base, "development_fraction": 0}, "development_fraction"),
({**base, "qualification": {"backends": [
{"device": "cpu", "dtype": "bfloat16", "cache": "cached"}]}},
"CPU float32"),
({**base, "arms": {"rank_sweep": {"ranks": [0]}}}, "rank_sweep"),
({**base, "arms": {"geometry": {"token_scopes": ["x"]}}}, "token_scopes"),
({**base, "arms": {"declared": [{"name": "a", "rows": [[1.0]]},
{"name": "a", "rows": [[1.0]]}]}},
"twice"),
({**base, "baseline": {"rank": 0}}, "baseline.rank"),
({**base, "representation": "x"}, "representation")):
with pytest.raises(StudyRefused, match=match):
load_config(bad)
with pytest.raises(StudyRefused, match="overlap"):
study(env, development=env["verify"])
with pytest.raises(StudyRefused, match="width"):
study(env, arms={"declared": [{"name": "short", "rows": [[1.0, 0.0]]}]})
def test_last_token_scope_uses_the_adapter_and_cli_runs(envs, tmp_path, capsys, monkeypatch):
import argparse
import json
from obliteratus.experiments import cli
env = envs["rank1"]
report = study(env, seeds=[0], min_seeds_for_recommendation=1,
arms={"geometry": {"token_scopes": ["all_tokens", "last_token"]},
"rank_sweep": None, "variance_only": False})
last = variant(report, "geometry:last_token")
assert last["edit"]["representation"] == "activation_adapter"
assert last["selection"]["options"]["weight_edit"]["feasible"] is False
assert last["requirements"]["passed"], failed(last)
parser = argparse.ArgumentParser()
cli.add_parser(parser.add_subparsers(dest="command"))
path = tmp_path / "config.json"
path.write_text(json.dumps(config(env, seeds=[0], min_seeds_for_recommendation=1)))
args = parser.parse_args(["experiments", "smaller-change", env["registration"], "--run",
str(env["run"]), "--source", str(env["source"]), "--config",
str(path), "--allow-fixture-results", "--no-timings"])
from obliteratus.experiments import smaller_change_cli
original = sc.run_smaller_change
def fixture_run(*a, **k):
return original(*a, **{**k, "model_io": BackendIO(), "pack_resolver": ConceptPack.load})
monkeypatch.setattr(sc, "run_smaller_change", fixture_run)
cli.run(args)
printed = json.loads(capsys.readouterr().out)
assert printed["verdict"] == "improvement" and "geometry" in printed["arms"]
assert smaller_change_cli.__all__
with pytest.raises(SystemExit) as error:
cli.run(parser.parse_args(["experiments", "smaller-change", env["registration"],
"--run", str(env["run"]), "--source", str(env["source"]),
"--config", str(tmp_path / "missing.json")]))
assert error.value.code == 4
def test_lora_change_with_an_unsupported_backend_fails_its_requirements(envs):
backends = [{"device": "cpu", "dtype": "float32", "cache": "cached"},
{"device": "cpu", "dtype": "float32", "cache": "cached",
"quantization": "bnb-int8"}]
report = study(envs["rank1"], seeds=[0], min_seeds_for_recommendation=1,
representation="lora", qualification={"backends": backends},
arms={"rank_sweep": None, "variance_only": False})
geo = variant(report, "geometry")
assert geo["edit"]["representation"] == "lora" and geo["edit"]["build_flops"] > 0
assert geo["resources"]["inference"]["extra_flops_per_token"] == 2 * (WIDTH + WIDTH)
statuses = [q["status"] for q in geo["requirements"]["qualification"]]
assert statuses == ["qualified", "unsupported"]
assert not geo["requirements"]["passed"] and geo["comparison"]["claimed"] == []
assert report["verdict"] == "no improvement"