mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Replace significance-only FAIL with a configurable non-inferiority decision (default 3 points): PASS, FAIL, INCONCLUSIVE, UNDERPOWERED. Plumb the margin through recipes, CLI, reports, cards, metadata and the Q4 export gate, and add 'concepts rescore' for saved batteries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
395 lines
18 KiB
Python
395 lines
18 KiB
Python
"""Default evaluation, cache identity, reporting and local pack hook contracts."""
|
|
|
|
from dataclasses import asdict
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.concepts.evaluation import (
|
|
EvaluationBattery,
|
|
KV_UNTESTED,
|
|
pack_battery,
|
|
)
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.evaluation import advanced_metrics
|
|
from obliteratus.prompts import BUILTIN_HARMFUL
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def measurements(projection=1.0):
|
|
return {
|
|
"projections": {"0": projection},
|
|
"neighbours": {"neutral": {"0": 0.2}},
|
|
"logits": torch.tensor([[[1.0, 2.0]]]),
|
|
}
|
|
|
|
|
|
def generator(prompts, decoding):
|
|
assert decoding["do_sample"] is False and decoding["num_beams"] == 1
|
|
assert decoding["chat_template"] is True and decoding["max_new_tokens"] == 48
|
|
return ["I cannot help with that request."] * len(prompts)
|
|
|
|
|
|
def test_default_on_paired_sample_and_sections(tmp_path):
|
|
generate = Mock(side_effect=generator)
|
|
battery = EvaluationBattery(generate, measure=measurements, seed=230).capture()
|
|
report = battery.finish(tmp_path)
|
|
assert len(battery.prompts) == 320
|
|
assert set(battery.prompts) <= set(BUILTIN_HARMFUL)
|
|
assert generate.call_args_list[0].args[0] == generate.call_args_list[2].args[0]
|
|
assert report.collateral["status"] == "PASS"
|
|
assert report.collateral["refusal"]["n"] == 320
|
|
assert report.collateral["first_token_kl"] == 0
|
|
assert report.collateral["coherence"]["before"]["n"] > 0
|
|
assert report.mechanistic["status"] == "residual"
|
|
assert report.behavioural["status"] == "not tested"
|
|
assert KV_UNTESTED in report.untested_channels
|
|
assert json.loads((tmp_path / "evaluation.json").read_text()) == asdict(report)
|
|
assert battery.prompts == EvaluationBattery(generator, seed=230).prompts
|
|
assert battery.prompts != EvaluationBattery(generator, seed=231).prompts
|
|
|
|
|
|
@pytest.mark.parametrize("size,status,reasons", [
|
|
(10, "UNDERPOWERED", []), (320, "FAIL", ["refusal_drop"]),
|
|
])
|
|
def test_refusal_drop_respects_minimum_sample_size(size, status, reasons):
|
|
battery = EvaluationBattery(generator, measure=measurements, refusal_size=size).capture()
|
|
battery.generate = lambda prompts, decoding: ["Sure, here is the answer."] * len(prompts)
|
|
collateral = battery.finish().collateral
|
|
assert collateral["status"] == status
|
|
assert collateral["fail_reasons"] == reasons
|
|
|
|
|
|
@pytest.mark.parametrize("margin,status", [(0, "FAIL"), (0.03, "INCONCLUSIVE"), (0.1, "PASS")])
|
|
def test_battery_records_margin_flags_and_decision(tmp_path, margin, status):
|
|
battery = EvaluationBattery(generator, measure=measurements, collateral_margin=margin).capture()
|
|
battery.generate = lambda p, d: (["Sure, here is the answer."] * 9 + generator(p[9:], d)
|
|
if p == battery.prompts else generator(p, d))
|
|
report = battery.finish(tmp_path)
|
|
refusal = report.collateral["refusal"]
|
|
assert report.collateral["status"] == refusal["status"] == status
|
|
assert refusal["collateral_margin"] == report.metadata["collateral_margin"] == margin
|
|
assert refusal["decision_rule"] == report.metadata["refusal_decision_rule"]
|
|
assert report.collateral["paired_refusal_flags"] == {
|
|
"pristine": [True] * 320, "intervened": [False] * 9 + [True] * 311,
|
|
"pair_indices": list(range(320)),
|
|
}
|
|
assert json.loads((tmp_path / "evaluation.json").read_text())["collateral"] == report.collateral
|
|
|
|
|
|
def test_margin_does_not_invalidate_pristine_generation_cache(tmp_path):
|
|
options = dict(model_id="offline", model_revision="pinned", cache_dir=tmp_path)
|
|
first = EvaluationBattery(generator, collateral_margin=0.03, **options).capture()
|
|
second = EvaluationBattery(generator, collateral_margin=0.1, **options).capture()
|
|
assert second.cache_hit and first.cache_path == second.cache_path
|
|
|
|
|
|
def test_invalid_outputs_excluded_from_both_sides_of_refusal_pairs():
|
|
refusal = "I cannot help with that request."
|
|
battery = EvaluationBattery(generator, refusal_size=4)
|
|
battery.generate = lambda p, d: ([" illegally" * 48] + [refusal] * 3
|
|
if p == battery.prompts else generator(p, d))
|
|
battery.capture()
|
|
assert battery.before["invalid"] == [True, False, False, False]
|
|
battery.generate = lambda p, d: ([refusal, " I'm sorry" * 24, refusal, refusal]
|
|
if p == battery.prompts else generator(p, d))
|
|
collateral = battery.finish().collateral
|
|
assert collateral["status"] == "UNDERPOWERED"
|
|
assert collateral["fail_reasons"] == []
|
|
assert collateral["refusal"]["n"] == 2
|
|
assert collateral["refusal"]["pristine"]["rate"] == 1
|
|
assert collateral["refusal"]["intervened"]["rate"] == 1
|
|
assert collateral["refusal"]["losses"] == 0
|
|
invalid = collateral["invalid_output"]
|
|
assert invalid["pristine"] == invalid["intervened"] == {"count": 1, "rate": 0.25}
|
|
assert invalid["excluded_pairs"] == 2
|
|
assert invalid["increases"] == invalid["decreases"] == 1
|
|
assert invalid["p_value"] == 0.75
|
|
|
|
|
|
@pytest.mark.parametrize("refusal_size", [10, 320])
|
|
@pytest.mark.parametrize("refusal_drop", [False, True])
|
|
def test_significant_invalid_increase_fails_even_with_missing_channels(refusal_size, refusal_drop):
|
|
battery = EvaluationBattery(generator, refusal_size=refusal_size).capture()
|
|
valid = "Sure, here is the answer." if refusal_drop else "I cannot help with that request."
|
|
battery.generate = lambda p, d: ([" excellence" * 48] * 5 + [valid] * (len(p) - 5)
|
|
if p == battery.prompts else generator(p, d))
|
|
collateral = battery.finish().collateral
|
|
assert collateral["status"] == "FAIL"
|
|
assert collateral["fail_reasons"] == (
|
|
["refusal_drop", "invalid_output_increase"] if refusal_drop and refusal_size == 320
|
|
else ["invalid_output_increase"]
|
|
)
|
|
assert collateral["refusal"]["n"] == refusal_size - 5
|
|
assert collateral["invalid_output"]["pristine"]["count"] == 0
|
|
assert collateral["invalid_output"]["intervened"]["count"] == 5
|
|
assert collateral["invalid_output"]["intervened"]["rate"] == 5 / refusal_size
|
|
assert collateral["invalid_output"]["excluded_pairs"] == 5
|
|
assert collateral["invalid_output"]["p_value"] == 1 / 32
|
|
|
|
|
|
def test_invalid_decrease_does_not_fail():
|
|
battery = EvaluationBattery(lambda p, d: ["dogs " * 20] * len(p), refusal_size=10).capture()
|
|
battery.generate = generator
|
|
collateral = battery.finish().collateral
|
|
assert collateral["status"] == "INVALID"
|
|
assert collateral["fail_reasons"] == []
|
|
assert collateral["invalid_output"]["increases"] == 0
|
|
assert collateral["invalid_output"]["decreases"] == 10
|
|
assert collateral["invalid_output"]["p_value"] == 1
|
|
|
|
|
|
@pytest.mark.parametrize("pristine_invalid", [False, True])
|
|
def test_all_invalid_run_reports_counts_without_refusal_statistics(tmp_path, pristine_invalid):
|
|
invalid_generator = lambda p, d: [" I'm sorry" * 24] * len(p)
|
|
battery = EvaluationBattery(
|
|
invalid_generator if pristine_invalid else generator, refusal_size=10
|
|
).capture()
|
|
battery.generate = invalid_generator
|
|
collateral = battery.finish(tmp_path).collateral
|
|
# Degenerating every valid output is a collateral failure, not a missing measurement.
|
|
assert collateral["status"] == ("INVALID" if pristine_invalid else "FAIL")
|
|
assert collateral["refusal"]["status"] == "INVALID"
|
|
assert collateral["refusal"]["n"] == 0
|
|
assert collateral["refusal"]["pristine"] == {"count": 0, "rate": None}
|
|
assert collateral["refusal"]["intervened"] == {"count": 0, "rate": None}
|
|
assert collateral["invalid_output"]["pristine"]["count"] == (10 if pristine_invalid else 0)
|
|
assert collateral["invalid_output"]["intervened"] == {"count": 10, "rate": 1}
|
|
assert collateral["invalid_output"]["excluded_pairs"] == 10
|
|
assert collateral["fail_reasons"] == ([] if pristine_invalid else ["invalid_output_increase"])
|
|
assert json.loads((tmp_path / "evaluation.json").read_text())["collateral"] == collateral
|
|
|
|
|
|
@pytest.mark.parametrize(("refusal_size", "measure", "status"), [
|
|
(284, measurements, "UNDERPOWERED"),
|
|
(320, measurements, "PASS"),
|
|
(320, None, "INCOMPLETE"),
|
|
])
|
|
def test_filtered_pairs_determine_power_and_completeness(refusal_size, measure, status):
|
|
battery = EvaluationBattery(generator, measure=measure, refusal_size=refusal_size).capture()
|
|
battery.generate = lambda p, d: ["dogs " * 20] + generator(p[1:], d)
|
|
collateral = battery.finish().collateral
|
|
assert collateral["status"] == status
|
|
assert collateral["refusal"]["n"] == refusal_size - 1
|
|
assert collateral["fail_reasons"] == []
|
|
|
|
|
|
def test_skip_recorded_but_other_channels_still_run():
|
|
generate = Mock(side_effect=AssertionError("collateral was skipped"))
|
|
measure = Mock(side_effect=[measurements(1), measurements(0)])
|
|
behaviour = Mock(return_value={"choices": [1, 2]})
|
|
battery = EvaluationBattery(
|
|
generate, measure=measure, skip_collateral=True, behavioural_battery=behaviour
|
|
).capture()
|
|
report = battery.finish()
|
|
assert report.collateral == {"status": "skipped", "reason": "skip_collateral=True"}
|
|
assert report.behavioural["status"] == "null"
|
|
assert report.behavioural["statement"] == (
|
|
"direction removed; no behaviour change detected on battery choices"
|
|
)
|
|
assert behaviour.call_count == 2 and measure.call_count == 2
|
|
generate.assert_not_called()
|
|
assert any("Collateral battery: skipped" in x for x in report.untested_channels)
|
|
|
|
|
|
def test_pack_supplied_battery_invoked():
|
|
pack = ConceptPack.load(Path(__file__).parent / "fixtures/concept_packs/valid_full")
|
|
target = pack.to_target()
|
|
assert target.battery
|
|
generate = Mock(side_effect=generator)
|
|
hook = pack_battery(target.battery)
|
|
battery = EvaluationBattery(generate, skip_collateral=True, behavioural_battery=hook).capture()
|
|
report = battery.finish()
|
|
assert generate.call_count == 2 * len(target.battery)
|
|
assert report.behavioural["status"] == "null"
|
|
assert report.behavioural["battery"] == list(target.battery)
|
|
battery.generate = lambda prompts, decoding: ["Changed"] * len(prompts)
|
|
assert battery.finish().behavioural["status"] == "changed"
|
|
|
|
|
|
def test_missing_channels_never_overclaim_pass():
|
|
report = EvaluationBattery(generator).capture().finish()
|
|
assert report.collateral["status"] == "INCOMPLETE"
|
|
assert report.collateral["refusal"]["status"] == "PASS"
|
|
assert report.mechanistic["status"] == "not tested"
|
|
assert any(x.startswith("First-token KL") for x in report.untested_channels)
|
|
assert any(x.startswith("Neighbour-concept") for x in report.untested_channels)
|
|
|
|
|
|
def test_cache_hit_and_identity_invalidation(tmp_path):
|
|
generate = Mock(side_effect=generator)
|
|
kwargs = dict(model_id="offline/model", model_revision="abc", cache_dir=tmp_path)
|
|
first = EvaluationBattery(generate, **kwargs).capture()
|
|
assert not first.cache_hit
|
|
second = EvaluationBattery(generate, **kwargs).capture()
|
|
assert second.cache_hit
|
|
assert generate.call_count == 3 # refusal once, coherence twice
|
|
assert first.before["refusal"] == second.before["refusal"]
|
|
assert first.before["invalid"] == second.before["invalid"] == [False] * 320
|
|
cached = json.loads(first.cache_path.read_text())
|
|
assert cached == {
|
|
"identity": first.identity, "flags": first.before["refusal"],
|
|
"invalid": first.before["invalid"],
|
|
}
|
|
variants = [
|
|
{"model_id": "other"},
|
|
{"model_revision": "def"},
|
|
{"seed": 9},
|
|
{"decoding_metadata": {"template": "different"}},
|
|
{"refusal_size": 284},
|
|
]
|
|
for override in variants:
|
|
assert not EvaluationBattery(generate, **(kwargs | override)).capture().cache_hit
|
|
other = EvaluationBattery(generate, **(kwargs | {"max_new_tokens": 49}))
|
|
assert other.cache_path != first.cache_path
|
|
unpinned = EvaluationBattery(generate, model_id="unresolved", cache_dir=tmp_path).capture()
|
|
assert unpinned.cache_path is None
|
|
first.cache_path.write_text('{"flags": []}')
|
|
with pytest.raises(ValueError, match="cache"):
|
|
EvaluationBattery(generate, **kwargs).capture()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kwargs",
|
|
[
|
|
{"refusal_size": 0},
|
|
{"refusal_size": len(BUILTIN_HARMFUL) + 1},
|
|
{"refusal_size": True},
|
|
{"max_new_tokens": 0},
|
|
{"seed": None},
|
|
{"seed": 1.5},
|
|
{"seed": True},
|
|
{"seed": "230"},
|
|
{"skip_collateral": True, "skip_reason": " "},
|
|
],
|
|
)
|
|
def test_invalid_configuration(kwargs):
|
|
with pytest.raises(ValueError):
|
|
EvaluationBattery(generator, **kwargs)
|
|
|
|
|
|
def test_lifecycle_and_generation_contract():
|
|
with pytest.raises(ValueError, match="Capture"):
|
|
EvaluationBattery(generator).finish()
|
|
for values in ([], [True] * 320):
|
|
with pytest.raises(ValueError, match="completion string"):
|
|
EvaluationBattery(lambda p, d: values).capture()
|
|
|
|
|
|
def test_absent_chat_template_is_an_untested_channel(tmp_path):
|
|
generate = Mock(side_effect=lambda p, d: ["I cannot help with that request."] * len(p))
|
|
report = (
|
|
EvaluationBattery(
|
|
generate,
|
|
measure=measurements,
|
|
decoding_metadata={"chat_template_available": False},
|
|
)
|
|
.capture()
|
|
.finish(tmp_path)
|
|
)
|
|
assert all(call.args[1]["chat_template"] is False for call in generate.call_args_list)
|
|
assert report.metadata["decoding"]["chat_template"] is False
|
|
saved = json.loads((tmp_path / "evaluation.json").read_text())
|
|
assert saved["metadata"]["decoding"]["chat_template"] is False
|
|
assert report.collateral["status"] == "INCOMPLETE"
|
|
assert any(x.startswith("Chat-template refusal") for x in report.untested_channels)
|
|
|
|
|
|
def test_behavioural_hook_forwards_task_decoding():
|
|
generate = Mock(return_value=["answer"])
|
|
requested = []
|
|
|
|
def behaviour(hook, decoding):
|
|
decoding["max_new_tokens"] = 96
|
|
requested.append(dict(decoding))
|
|
return {"long_answer": hook(["hello"], decoding)}
|
|
|
|
battery = EvaluationBattery(
|
|
generate, skip_collateral=True, behavioural_battery=behaviour
|
|
).capture()
|
|
report = battery.finish()
|
|
assert report.behavioural["status"] == "null"
|
|
assert [call.args[1] for call in generate.call_args_list] == requested
|
|
assert all(d["max_new_tokens"] == 96 for d in requested)
|
|
assert battery.decoding["max_new_tokens"] == 48
|
|
generate.return_value = []
|
|
with pytest.raises(ValueError, match="completion string"):
|
|
battery.finish()
|
|
|
|
|
|
@pytest.mark.parametrize("invalid", [None, {}, [], [0], ["false"]])
|
|
def test_cache_rejects_missing_or_malformed_invalid_flags(tmp_path, invalid):
|
|
kwargs = dict(model_id="offline/model", model_revision="abc", cache_dir=tmp_path, refusal_size=1)
|
|
first = EvaluationBattery(generator, **kwargs).capture()
|
|
cached = json.loads(first.cache_path.read_text())
|
|
if invalid is None:
|
|
del cached["invalid"]
|
|
else:
|
|
cached["invalid"] = invalid
|
|
first.cache_path.write_text(json.dumps(cached))
|
|
with pytest.raises(ValueError, match="Invalid pristine refusal cache"):
|
|
EvaluationBattery(generator, **kwargs).capture()
|
|
|
|
|
|
def test_cache_round_trip_preserves_invalid_refusal_loop(tmp_path):
|
|
generate = Mock(side_effect=lambda p, d: [" I'm sorry" * 24] * len(p))
|
|
kwargs = dict(model_id="offline/model", model_revision="abc", cache_dir=tmp_path, refusal_size=1)
|
|
first = EvaluationBattery(generate, **kwargs).capture()
|
|
second = EvaluationBattery(generate, **kwargs).capture()
|
|
assert second.cache_hit
|
|
assert first.before["invalid"] == second.before["invalid"] == [True]
|
|
assert first.before["refusal"] == second.before["refusal"] == [False]
|
|
assert json.loads(first.cache_path.read_text())["invalid"] == [True]
|
|
assert generate.call_count == 3
|
|
|
|
|
|
def test_detector_version_changes_invalidate_cache(tmp_path, monkeypatch):
|
|
kwargs = dict(model_id="offline/model", model_revision="abc", cache_dir=tmp_path, refusal_size=1)
|
|
first = EvaluationBattery(generator, **kwargs).capture()
|
|
monkeypatch.setattr(
|
|
advanced_metrics, "DEGENERATE_DETECTOR_VERSION", advanced_metrics.DEGENERATE_DETECTOR_VERSION + 1
|
|
)
|
|
second = EvaluationBattery(generator, **kwargs).capture()
|
|
assert second.identity["refusal_scorer"] != first.identity["refusal_scorer"]
|
|
assert second.cache_path != first.cache_path
|
|
assert not second.cache_hit
|
|
|
|
|
|
@pytest.mark.parametrize("patterns", ["REFUSAL_PREFIXES", "REFUSAL_SUBSTRINGS"])
|
|
def test_scorer_pattern_changes_invalidate_cache(tmp_path, monkeypatch, patterns):
|
|
generate = Mock(return_value=["new refusal marker"])
|
|
kwargs = dict(
|
|
model_id="offline/model", model_revision="abc", cache_dir=tmp_path, refusal_size=1
|
|
)
|
|
generate.side_effect = lambda p, d: ["new refusal marker"] * len(p)
|
|
first = EvaluationBattery(generate, **kwargs).capture()
|
|
assert first.before["refusal"] == [False]
|
|
label = first.identity["refusal_scorer"]
|
|
assert label.startswith("combined-") and len(label.removeprefix("combined-")) == 64
|
|
assert label == advanced_metrics._refusal_scorer_label("combined")
|
|
assert label != advanced_metrics._refusal_scorer_label("prefix")
|
|
monkeypatch.setattr(
|
|
advanced_metrics, patterns, [*getattr(advanced_metrics, patterns), "new refusal marker"]
|
|
)
|
|
second = EvaluationBattery(generate, **kwargs).capture()
|
|
assert second.identity["refusal_scorer"] != label
|
|
assert second.cache_path != first.cache_path
|
|
assert not second.cache_hit
|
|
assert second.before["refusal"] == [True]
|
|
assert EvaluationBattery(generate, **kwargs).capture().cache_hit
|
|
|
|
|
|
@pytest.mark.parametrize("value", [None, {}, []])
|
|
def test_empty_behaviour_or_probe_does_not_count_as_tested(value):
|
|
with pytest.raises(ValueError, match="named JSON scores"):
|
|
EvaluationBattery(
|
|
generator, skip_collateral=True, behavioural_battery=lambda g, d: value
|
|
).capture()
|
|
battery = EvaluationBattery(generator, skip_collateral=True, kv_probe=lambda: value).capture()
|
|
with pytest.raises(ValueError, match="JSON measurements"):
|
|
battery.finish()
|