Files
OBLITERATUS/tests/test_phase_benchmark_admission.py
Joseph MaglyandClaude Opus 5.5 db39905ffa fix(eval): report invalid completions beside refusal rates and stop rewarding degeneration (#312)
`refusal_rate` and its callers counted empty, reasoning-only and degenerate
completions as non-refusals, so the Bayesian optimizer objective and
tourney and benchmark rankings could reward an edit that destroyed output.

- New `measure_refusals` returns a `RefusalMeasurement` (refused, valid,
  invalid, total, rate over valid completions, invalid rate).
- `refusal_rate`, `refusal_rate_with_ci` and `heretic_eval.arditi_refusal_rate`
  gain `exclude_invalid`. The default keeps the legacy all-response
  denominator for a deprecation window; `refusal_rate_with_ci` adds
  valid, invalid and total counts.
- Verification metadata adds invalid counts, the valid-only rate and a
  refusal objective that counts invalid completions as refusals.
- The Bayesian optimizer, tourney and benchmark rankings use that objective
  and record invalid rates; displayed refusal rates are unchanged.
- docs/refusal-metrics.md documents the denominators and the planned
  default change.

Fixes #312

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 17:16:26 -04:00

222 lines
8.9 KiB
Python

"""Benchmark orchestration admits pipeline and custom evaluation work offline."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus import phase_admission as admission
from scripts import benchmark_sota_comparison as benchmark
@pytest.fixture
def benchmark_run(monkeypatch, tmp_path):
trace = []
pipeline = SimpleNamespace(
_active_stage="summon", _strong_layers=[0], use_chat_template=False,
gpu_memory_utilization=0.9,
)
def forward(**inputs):
trace.append("work:baseline" if pipeline._active_stage == "baseline" else "work:KL")
return SimpleNamespace(logits=torch.zeros(1, 2, 4))
def generate(**inputs):
trace.append("work:refusal")
return torch.tensor([[0, 1, 2]])
tokenizer = Mock(return_value={"input_ids": torch.tensor([[0, 1]])})
tokenizer.decode.return_value = "Here is a response."
tokenizer.pad_token_id = 0
tokenizer.eos_token_id = 1
model = Mock(side_effect=forward, device=torch.device("cpu"))
model.generate.side_effect = generate
pipeline.handle = SimpleNamespace(model=model, tokenizer=tokenizer)
def admit(stage, **options):
assert options == ({"work_scope": "surgery"} if stage == "excise" else {})
pipeline._active_stage = stage
trace.append(f"admit:{stage}")
pipeline._admit_pipeline_phase = admit
for stage in ("summon", "probe", "distill", "excise"):
setattr(pipeline, f"_{stage}", lambda stage=stage: trace.append(f"work:{stage}"))
def capabilities(*args):
trace.append("work:capability")
return SimpleNamespace(run_all=lambda: SimpleNamespace(
knowledge=None, truthfulness=None, math=None,
))
def full_evaluation(**kwargs):
trace.append("work:full")
return SimpleNamespace(harmbench_asr=0.0, jailbreakbench_refusal_rate=0.0)
monkeypatch.setattr(benchmark, "AbliterationPipeline", lambda **kwargs: pipeline)
monkeypatch.setattr(benchmark, "BenchmarkRunner", capabilities)
monkeypatch.setattr(
"obliteratus.evaluation.heretic_eval.run_full_heretic_eval", full_evaluation,
)
monkeypatch.setattr(benchmark.torch.cuda, "is_available", lambda: False)
monkeypatch.setattr(benchmark.gc, "collect", lambda: None)
monkeypatch.setattr(admission, "phase_memory_devices", lambda handle: ("cpu",))
monkeypatch.setattr(admission, "observe_memory", lambda device: admission.MemoryObservation(
device, 1024**3, 1024**3,
))
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
args = SimpleNamespace(
output_dir=str(tmp_path), device="cpu", dtype="float32", quantization=None,
full_eval=True, keep_models=True,
)
def run():
return benchmark.run_single_method(
"local", "basic", ["harmful"], ["harmless"], ["evaluation"], ["KL"], args,
)
return pipeline, trace, run
def test_benchmark_admits_each_workload_in_order(benchmark_run, monkeypatch, caplog):
pipeline, trace, run = benchmark_run
original = benchmark._admit_benchmark_phase
def admit(pipeline, stage, workload):
trace.append(f"admit:{stage}:{workload}")
original(pipeline, stage, workload)
monkeypatch.setattr(benchmark, "_admit_benchmark_phase", admit)
result = run()
assert result.error is None
assert trace == [
"work:summon", "admit:baseline:baseline logits", "work:baseline",
"admit:probe", "work:probe", "admit:distill", "work:distill",
"admit:excise", "work:excise",
"admit:verify:refusal generation", "work:refusal",
"admit:verify:KL divergence", "work:KL",
"admit:verify:capability probes", "work:capability",
"admit:verify:full Heretic evaluation", "work:full",
]
assert pipeline._active_stage == "verify"
assert "Proceeding in default mode" in caplog.text
@pytest.mark.parametrize("completion", [" I'm sorry" * 24, "", "<think>Considering.</think>"])
def test_benchmark_reports_invalid_completions(benchmark_run, completion):
pipeline, _, run = benchmark_run
pipeline.handle.tokenizer.decode.return_value = completion
result = run()
assert result.error is None
assert result.refusal_rate == 0.0
assert result.invalid_count == 1
assert result.invalid_rate == 1.0
def test_benchmark_generation_failure_is_invalid(benchmark_run):
pipeline, _, _ = benchmark_run
pipeline.handle.model.generate.side_effect = RuntimeError("generation failed")
metrics = {}
assert benchmark.measure_refusal_rate(pipeline, ["prompt"], metrics=metrics) == 0.0
assert metrics == {"invalid_count": 1, "invalid_rate": 1.0}
def test_benchmark_ranking_penalizes_invalid_output():
garbage = benchmark.MethodResult("basic", "garbage", invalid_count=10, invalid_rate=1.0)
helpful = benchmark.MethodResult("advanced", "helpful", refusal_rate=0.25)
table = benchmark.format_comparison_table([garbage, helpful])
assert "Best refusal removal: helpful" in table
@pytest.mark.parametrize("stage", ["probe", "distill", "excise"])
def test_benchmark_pipeline_rejection_precedes_work(benchmark_run, stage):
pipeline, trace, run = benchmark_run
original = pipeline._admit_pipeline_phase
def admit(current, **options):
original(current, **options)
if current == stage:
raise admission.PhaseAdmissionError(stage, "cpu", 100, 0)
pipeline._admit_pipeline_phase = admit
result = run()
assert result.error is not None and stage in result.error
assert f"work:{stage}" not in trace
assert pipeline._active_stage == stage
@pytest.mark.parametrize("workload,stage,work", [
("baseline logits", "baseline", "baseline"),
("refusal generation", "verify", "refusal"),
("KL divergence", "verify", "KL"),
("capability probes", "verify", "capability"),
("full Heretic evaluation", "verify", "full"),
])
def test_benchmark_custom_rejection_is_a_method_error_before_work(
benchmark_run, monkeypatch, workload, stage, work,
):
pipeline, trace, run = benchmark_run
original = benchmark._admit_benchmark_phase
def admit(pipeline, current, name):
# Earlier custom stages proceed with default warnings; the selected
# stage exercises real strict admission before its broad error handler.
with monkeypatch.context() as patch:
patch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict" if name == workload else "default")
original(pipeline, current, name)
monkeypatch.setattr(benchmark, "_admit_benchmark_phase", admit)
result = run()
assert result.error is not None and workload in result.error
assert f"work:{work}" not in trace
assert pipeline._active_stage == stage
@pytest.mark.parametrize("helper,stage", [
("collect_baseline_logits", "baseline"),
("measure_refusal_rate", "verify"),
("measure_kl_divergence", "verify"),
])
def test_direct_benchmark_helpers_reject_before_tokenization(
benchmark_run, monkeypatch, helper, stage,
):
pipeline, trace, _ = benchmark_run
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
args = ({0: torch.zeros(4)}, ["prompt"]) if helper == "measure_kl_divergence" else (["prompt"],)
with pytest.raises(admission.PhaseAdmissionError, match=stage):
getattr(benchmark, helper)(pipeline, *args)
pipeline.handle.tokenizer.assert_not_called()
pipeline.handle.model.assert_not_called()
pipeline.handle.model.generate.assert_not_called()
assert pipeline._active_stage == stage
assert trace == []
def test_custom_benchmark_demand_stays_unknown_on_cpu_and_model_devices(
benchmark_run, monkeypatch,
):
pipeline, _, _ = benchmark_run
monkeypatch.setattr(admission, "phase_memory_devices", lambda handle: ("cuda:1", "cuda:2"))
admit = Mock()
monkeypatch.setattr(admission, "admit_phase", admit)
benchmark._admit_benchmark_phase(pipeline, "baseline", "baseline logits")
(stage, demand), options = admit.call_args
assert stage == "baseline"
assert set(demand.devices) == {"cpu", "cuda:1", "cuda:2"}
assert all(component.bytes is None and component.confidence == "estimated"
for component in demand.components)
assert options == {"gpu_memory_utilization": 0.9}
def test_custom_benchmark_off_bypasses_metadata(benchmark_run, monkeypatch, caplog):
pipeline, _, _ = benchmark_run
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
inspect = Mock(side_effect=AssertionError("off must not inspect model metadata"))
monkeypatch.setattr(admission, "phase_memory_devices", inspect)
monkeypatch.setattr(admission, "unknown_phase_demand", inspect)
monkeypatch.setattr(admission, "admit_phase", inspect)
benchmark._admit_benchmark_phase(pipeline, "baseline", "baseline logits")
inspect.assert_not_called()
assert pipeline._active_stage == "baseline"
assert "PHASE MEMORY ADMISSION DISABLED for baseline" in caplog.text