Files
OBLITERATUS/tests/test_refusal_increase_gate.py
2026-11-24 18:06:20 -05:00

391 lines
17 KiB
Python

"""CPU contracts for the paired stock and edited refusal quality gate."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline, PipelineQualityRejectedError
from obliteratus.refusal_verification import _paired_wilson_interval
class StubTokenizer:
pad_token_id = 0
eos_token_id = 0
padding_side = "right"
def __init__(self, prompts):
self.prompt_ids = {f"chat: {prompt}": index for index, prompt in enumerate(prompts, 1)}
def apply_chat_template(self, messages, **_kwargs):
return f"chat: {messages[0]['content']}"
def __call__(self, prompts, **_kwargs):
if isinstance(prompts, str):
prompts = [prompts]
ids = torch.tensor([[0, self.prompt_ids.get(prompt, 0)] for prompt in prompts])
return {"input_ids": ids, "attention_mask": torch.ones_like(ids)}
def decode(self, tokens, **_kwargs):
return {
1: "Paris is the capital of France.",
2: "I cannot assist with that request.",
3: "",
}[tokens[0].item()]
@pytest.fixture
def make_pipeline(monkeypatch):
def make(*, prompts_per_split=10, **options):
harmful = [f"evaluation H{i}" for i in range(prompts_per_split)]
harmless = [f"evaluation B{i}" for i in range(prompts_per_split)]
pipeline = AbliterationPipeline(
"test-model",
harmful_prompts=["extraction H"],
harmless_prompts=["extraction B"],
evaluation_harmful_prompts=harmful,
evaluation_harmless_prompts=harmless,
verify_sample_size=1,
refusal_max_tokens=7,
use_chat_template=False,
**options,
)
tokenizer = StubTokenizer(harmful + harmless)
state = SimpleNamespace(responses={}, calls=[], oom_on=None)
def generate(input_ids, attention_mask, **settings):
prompt_ids = tuple(input_ids[:, -1].tolist())
if any(prompt_ids):
state.calls.append((prompt_ids, tokenizer.padding_side, settings))
if state.oom_on is not None and any(
state.oom_on(prompt_id) for prompt_id in prompt_ids
):
raise torch.OutOfMemoryError("test out of memory")
responses = torch.tensor([[state.responses.get(prompt_id, 1)] for prompt_id in prompt_ids])
return SimpleNamespace(sequences=torch.cat((input_ids, responses), dim=1))
model = Mock(return_value=SimpleNamespace(loss=torch.tensor(0.0)))
model.generate.side_effect = generate
model.generation_config = SimpleNamespace(num_return_sequences=1)
pipeline.handle = SimpleNamespace(model=model, tokenizer=tokenizer, architecture="gpt2")
monkeypatch.setattr(pipeline, "_get_model_device", lambda _model: torch.device("cpu"))
monkeypatch.setattr(pipeline, "_admit_generation_diagnostic", Mock(return_value=True))
monkeypatch.setattr(pipeline, "_verification_prompts", lambda: (["The capital of France is"], []))
return pipeline, state
return make
def test_default_wilson_allows_one_new_refusal_in_sixteen(make_pipeline, monkeypatch):
pipeline, state = make_pipeline(prompts_per_split=16)
logs = []
monkeypatch.setattr(pipeline, "log", logs.append)
pipeline._capture_stock_baseline()
state.responses[17] = 2
pipeline._verify()
comparison = pipeline._quality_metrics["refusal_comparison"]
assert comparison["mode"] == "wilson"
assert comparison["confidence"] == 0.95
harmless = comparison["harmless"]
assert harmless["increase"] == pytest.approx(1 / 16)
assert harmless["bounds"][0] < 0.05 < harmless["bounds"][1]
assert any("1 of 16 harmless prompts became refusals" in log for log in logs)
def test_default_wilson_rejects_significant_increase_with_counts(make_pipeline):
pipeline, state = make_pipeline(prompts_per_split=16)
pipeline._capture_stock_baseline()
state.responses = dict.fromkeys(range(17, 29), 2)
events = []
pipeline._on_stage = events.append
with pytest.raises(PipelineQualityRejectedError, match="lower bound.*5.0%") as failure:
pipeline._verify()
assert failure.value.metric == "harmless_refusal_increase"
assert "12 of 16 harmless prompts became refusals" in str(failure.value)
comparison = events[-1].details["refusal_comparison"]
assert comparison == pipeline._quality_metrics["refusal_comparison"]
harmless = comparison["harmless"]
assert harmless["increase"] == pytest.approx(0.75)
assert 0.05 < harmless["bounds"][0] < 0.75 < harmless["bounds"][1]
assert (harmless["stock_refused"], harmless["edited_refused"], harmless["paired_count"]) == (0, 12, 16)
@pytest.mark.parametrize("confidence,rejected", [(0.95, False), (0.80, True)])
def test_wilson_confidence_controls_the_gate(make_pipeline, confidence, rejected):
pipeline, state = make_pipeline(prompts_per_split=16, refusal_check_confidence=confidence)
pipeline._capture_stock_baseline()
state.responses = dict.fromkeys(range(17, 21), 2)
if rejected:
with pytest.raises(PipelineQualityRejectedError, match="lower bound"):
pipeline._verify()
else:
pipeline._verify()
def test_wilson_uses_only_complete_pairs_and_accounts_for_both_flip_directions(make_pipeline):
pipeline, state = make_pipeline(prompts_per_split=16, max_refusal_increase=None)
state.responses = {17: 3, 18: 2, 19: 2}
pipeline._capture_stock_baseline()
state.responses = {17: 2, 18: 3, 20: 2, 21: 2}
pipeline._verify()
harmless = pipeline._quality_metrics["refusal_comparison"]["harmless"]
assert harmless["paired_count"] == 14
assert harmless["new_refusals"] == 2
assert harmless["resolved_refusals"] == 1
assert harmless["increase"] == pytest.approx(1 / 14)
assert pipeline._quality_metrics["harmless_refusal_increase"] == pytest.approx(1 / 14)
def test_wilson_bounds_depend_on_pairing_with_identical_marginals(make_pipeline):
pipeline, state = make_pipeline(prompts_per_split=16, max_refusal_increase=None)
state.responses = dict.fromkeys(range(17, 21), 2)
pipeline._capture_stock_baseline()
state.responses = dict.fromkeys(range(17, 25), 2)
pipeline._verify()
overlapping = pipeline._quality_metrics["refusal_comparison"]["harmless"]
state.responses = dict.fromkeys(range(21, 29), 2)
pipeline._verify()
disjoint = pipeline._quality_metrics["refusal_comparison"]["harmless"]
assert overlapping["increase"] == disjoint["increase"] == 0.25
assert disjoint["bounds"][0] < overlapping["bounds"][0]
assert overlapping["bounds"][1] < disjoint["bounds"][1]
# Newcombe RG (1998), "Improved confidence intervals for the difference between
# binomial proportions based on paired data", Statist. Med. 17:2635-2650, Table III.
# Eight expected intervals are transcribed from the paper as printed; the ninth
# (negative-numerator-uncorrected) uses the recalculated lower limit, see its comment.
# Cells (e, f, g, h) are both positive, first only, second only, neither; the
# difference is first minus second, so edited is the first classification here.
# Method 10 continuity-corrects phi's numerator to max(eh - fg - n/2, 0) when
# eh > fg, as also done by ratesci::pairbinci (MOVER_newc) and StatsDirect.
@pytest.mark.parametrize(
("cells", "published"),
[
pytest.param((36, 12, 2, 0), (0.0569, 0.3404), id="eh-zero"),
pytest.param((20, 12, 2, 16), (0.0562, 0.3292), id="corrected-phi-positive"),
pytest.param((18, 12, 2, 18), (0.0562, 0.3290), id="corrected-phi-even-split"),
pytest.param((35, 14, 0, 1), (0.1461, 0.4175), id="one-discordant-direction"),
pytest.param((1, 98, 0, 1), (0.9171, 0.9916), id="correction-floors-at-zero"),
pytest.param((53, 0, 0, 1), (-0.0729, 0.0729), id="no-discordant-pairs"),
pytest.param((27, 0, 0, 27), (-0.0351, 0.0351), id="avoids-zero-width-interval"),
pytest.param((0, 30, 0, 0), (0.8395, 1.0), id="zero-marginals"),
# eh < fg: method 10 leaves phi unchanged, so it equals method 8 (0.8737, 0.9850).
# The paper's method 10 row prints 0.8736 for the lower limit (0.87367 exactly).
pytest.param((1, 97, 1, 1), (0.8737, 0.9850), id="negative-numerator-uncorrected"),
],
)
def test_paired_interval_reproduces_newcombe_method_10(cells, published):
assert _paired_wilson_interval(*cells, z=1.959964) == pytest.approx(published, abs=5e-5)
# Pinned paired Newcombe (1998, method 10) intervals for 16 complete harmless pairs
# at 95% confidence, recomputed from the 2x2 cell counts with a standalone
# implementation of the published formula (#624). The discordant case was
# [-0.209823, 0.325734] without the continuity correction (as first pinned by
# #594/#619); the method-10 value supersedes it.
# Harmless prompt ids are 17-32; response 2 is a refusal.
@pytest.mark.parametrize(
("stock_refused", "edited_refused", "limit", "cells", "bounds", "rejected"),
[
pytest.param((), (17,), 0.05, (0, 0, 1), (-0.137810, 0.283287), False,
id="one-new-refusal"),
pytest.param((), range(17, 21), 0.05, (0, 0, 4), (0.006195, 0.494983), False,
id="four-new-refusals-within-limit"),
pytest.param((), range(17, 21), 0.0, (0, 0, 4), (0.006195, 0.494983), True,
id="four-new-refusals-above-limit"),
pytest.param((), (17,), 0.0, (0, 0, 1), (-0.137810, 0.283287), False,
id="one-new-refusal-zero-limit"),
pytest.param((17, 18, 19), (17, 20, 21, 22), 0.05, (1, 2, 3), (-0.221765, 0.335995),
False, id="discordant-both-directions"),
pytest.param(range(17, 21), range(17, 25), None, (4, 0, 4), (0.001520, 0.455247),
False, id="positively-correlated"),
],
)
def test_wilson_paired_interval_matches_pinned_values(
make_pipeline, stock_refused, edited_refused, limit, cells, bounds, rejected,
):
pipeline, state = make_pipeline(prompts_per_split=16, max_refusal_increase=limit)
state.responses = dict.fromkeys(stock_refused, 2)
pipeline._capture_stock_baseline()
state.responses = dict.fromkeys(edited_refused, 2)
if rejected:
with pytest.raises(PipelineQualityRejectedError, match="lower bound") as failure:
pipeline._verify()
assert failure.value.metric == "harmless_refusal_increase"
else:
pipeline._verify()
harmless = pipeline._quality_metrics["refusal_comparison"]["harmless"]
both, resolved, new = cells
assert harmless["paired_count"] == 16
assert (harmless["new_refusals"], harmless["resolved_refusals"]) == (new, resolved)
assert harmless["stock_refused"] == both + resolved
assert harmless["edited_refused"] == both + new
assert harmless["increase"] == pytest.approx((new - resolved) / 16)
assert harmless["bounds"] == pytest.approx(bounds, abs=5e-7)
def test_wilson_without_complete_pairs_has_no_decision(make_pipeline):
pipeline, state = make_pipeline(prompts_per_split=16)
state.responses = dict.fromkeys(range(17, 25), 3)
pipeline._capture_stock_baseline()
state.responses = {**dict.fromkeys(range(17, 25), 2), **dict.fromkeys(range(25, 33), 3)}
pipeline._verify()
metrics = pipeline._quality_metrics
assert metrics["baseline_harmless_refusal_rate"] == 0
assert metrics["harmless_refusal_rate"] == 1
assert "harmless_refusal_increase" not in metrics
assert metrics["refusal_comparison"]["harmless"]["paired_count"] == 0
assert metrics["refusal_comparison"]["harmless"]["bounds"] is None
@pytest.mark.parametrize("invalid", [False, True])
def test_point_mode_rejects_one_in_sixteen_with_legacy_denominators(make_pipeline, invalid):
pipeline, state = make_pipeline(prompts_per_split=16, refusal_check_mode="point")
if invalid:
state.responses[17] = 3
pipeline._capture_stock_baseline()
state.responses = {17: 2, **({18: 3} if invalid else {})}
valid_count = 15 if invalid else 16
with pytest.raises(PipelineQualityRejectedError, match=f"1 of {valid_count} harmless prompts"):
pipeline._verify()
assert pipeline._quality_metrics["harmless_refusal_increase"] == pytest.approx(1 / valid_count)
def test_bounded_stock_sample_is_reused_with_verify_generation_settings(make_pipeline, monkeypatch):
pipeline, state = make_pipeline(refusal_baseline_size=5)
state.responses = {1: 2, 11: 3}
pipeline._capture_stock_baseline()
stock_calls = state.calls.copy()
assert sum(len(ids) for ids, _, _ in stock_calls) == 5
assert {ids[0] for ids, _, _ in stock_calls} == {1, 11}
assert all(side == "left" and settings == {"max_new_tokens": 7, "do_sample": False}
for _, side, settings in stock_calls)
assert pipeline._quality_metrics["baseline_refusal_rate"] == 0.5
assert pipeline._quality_metrics["baseline_harmless_refusal_rate"] == 0.0
# Mutable evaluation inputs must not change the sample chosen before surgery.
pipeline.evaluation_harmful_prompts.reverse()
pipeline.evaluation_harmless_prompts.reverse()
logs = []
monkeypatch.setattr(pipeline, "log", logs.append)
state.calls.clear()
pipeline._verify()
assert all(call in state.calls for call in stock_calls)
assert pipeline._quality_metrics["refusal_increase"] == 0.0
assert pipeline._quality_metrics["harmless_refusal_increase"] == 0.0
assert any("50.0% -> 50.0%" in log and "0.0% -> 0.0%" in log for log in logs)
assert pipeline.handle.tokenizer.padding_side == "right"
@pytest.mark.parametrize("split", ["harmful", "harmless"])
def test_verify_rejects_refusal_increase_for_either_split(make_pipeline, split):
pipeline, state = make_pipeline(refusal_check_mode="point")
pipeline._capture_stock_baseline()
state.responses[1 if split == "harmful" else 11] = 2
events = []
pipeline._on_stage = events.append
with pytest.raises(PipelineQualityRejectedError, match="stock.*edited") as failure:
pipeline._verify()
metric = "refusal_increase" if split == "harmful" else "harmless_refusal_increase"
assert failure.value.stage == "verify"
assert failure.value.metric == metric
assert "harmful" in str(failure.value) and "harmless" in str(failure.value)
assert pipeline._quality_metrics[metric] == pytest.approx(0.1)
assert events[-1].status == "error"
assert events[-1].details["metric"] == metric
@pytest.mark.parametrize("threshold", [0.1, 0.2, None])
def test_verify_allows_increase_within_tolerance_or_disabled_gate(make_pipeline, threshold):
pipeline, state = make_pipeline(refusal_check_mode="point", max_refusal_increase=threshold)
state.responses = {11: 2, 12: 2, 13: 2}
pipeline._capture_stock_baseline()
state.responses[14] = 2
pipeline._verify()
assert pipeline._quality_metrics["baseline_harmless_refusal_rate"] == 0.3
assert pipeline._quality_metrics["harmless_refusal_rate"] == 0.4
assert pipeline._quality_metrics["harmless_refusal_increase"] == pytest.approx(0.1)
@pytest.mark.parametrize("reason", ["disabled", "admission", "oom"])
def test_unavailable_stock_baseline_records_why_and_cannot_fail_verify(make_pipeline, reason):
pipeline, state = make_pipeline(**({"refusal_baseline_size": 0} if reason == "disabled" else {}))
if reason == "admission":
pipeline._admit_generation_diagnostic.side_effect = (
lambda stage, diagnostic: (stage, diagnostic) != ("baseline", "refusal_comparison")
)
elif reason == "oom":
state.oom_on = lambda prompt_id: prompt_id > 10
pipeline._capture_stock_baseline()
assert pipeline._quality_metrics["baseline_refusal_skip_reason"] == reason
assert pipeline._quality_metrics.get("baseline_refusal_rate") is None
assert pipeline._quality_metrics.get("baseline_harmless_refusal_rate") is None
if reason != "oom":
assert state.calls == []
state.oom_on = None
state.responses = {1: 2, 11: 2}
pipeline._verify()
assert pipeline._quality_metrics["refusal_rate"] == 1.0
assert pipeline._quality_metrics.get("refusal_increase") is None
assert pipeline._quality_metrics.get("harmless_refusal_increase") is None
assert pipeline.handle.tokenizer.padding_side == "right"
def test_incomplete_edited_measurement_cannot_gate_or_reuse_old_result(make_pipeline):
pipeline, state = make_pipeline()
pipeline._capture_stock_baseline()
pipeline._verify()
assert pipeline._quality_metrics["harmless_refusal_increase"] == 0.0
state.oom_on = lambda prompt_id: prompt_id > 10
state.responses[1] = 2
pipeline._verify()
assert pipeline._quality_metrics["refusal_comparison_skip_reason"] == "oom"
assert "refusal_comparison" not in pipeline._quality_metrics
assert pipeline._quality_metrics.get("harmless_refusal_increase") is None
assert pipeline._quality_metrics.get("refusal_increase") is None
@pytest.mark.parametrize("keyword,value", [
("refusal_baseline_size", -1),
("refusal_baseline_size", True),
("refusal_baseline_size", 1.5),
("max_refusal_increase", -0.01),
("max_refusal_increase", 1.01),
("max_refusal_increase", float("nan")),
("max_refusal_increase", True),
("refusal_check_mode", "unknown"),
("refusal_check_mode", None),
("refusal_check_confidence", False),
("refusal_check_confidence", 0),
("refusal_check_confidence", 1),
("refusal_check_confidence", float("nan")),
])
def test_refusal_gate_settings_require_valid_ranges(keyword, value):
with pytest.raises(ValueError, match=keyword):
AbliterationPipeline("test-model", **{keyword: value})