Files
OBLITERATUS/tests/test_perplexity_weighting.py
Joseph Magly a38b556862 fix(verify): make the quick refusal check confidence-aware
The pre-export refusal comparison judged harmless-refusal increases on
16 prompts against a 5% limit, so a single prompt changing (6.25%)
rejected edits that the 200-prompt verify-edit gates would judge. It
now rejects only when the paired Newcombe interval's lower bound
exceeds the limit (95% confidence by default), reports counts, rates
and bounds, and keeps the old point-estimate rule available as
refusal_check_mode: point. Both settings are v2 projection parameters,
normalized explicitly and recorded in resolution payloads and
checkpoint metadata. Also fixes the perplexity-weighting test, which
built a pipeline without the refusal sample attribute introduced in
46d6dc6 and failed in the full suite.

Fixes #587
2026-11-22 11:51:14 -05:00

129 lines
4.4 KiB
Python

"""Causal perplexity aggregation uses only scored next-token labels."""
from types import SimpleNamespace
import math
import pytest
import torch
import obliteratus.abliterate as abliterate
from obliteratus.abliterate import AbliterationPipeline
pytestmark = pytest.mark.cpu
class _Tokenizer:
def __init__(self, encoded):
self.encoded = encoded
def __call__(self, text, **_kwargs):
input_ids, attention_mask = self.encoded[text]
return {
"input_ids": torch.tensor([input_ids]),
"attention_mask": torch.tensor([attention_mask]),
}
class _LossModel:
def __init__(self, losses):
self.losses = iter(losses)
self.labels = []
def __call__(self, **kwargs):
self.labels.append(kwargs["labels"].detach().cpu())
return SimpleNamespace(loss=torch.tensor(next(self.losses)))
def _pipeline(encoded, losses):
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
model = _LossModel(losses)
pipeline.handle = SimpleNamespace(model=model, tokenizer=_Tokenizer(encoded))
pipeline.max_seq_length = 32
pipeline._get_model_device = lambda _model: torch.device("cpu")
pipeline.log = lambda _message: None
return pipeline, model
def test_reference_perplexity_weights_only_shifted_unmasked_labels(monkeypatch):
pipeline, model = _pipeline(
{
"short": ([1, 2, 0, 0], [1, 1, 0, 0]),
"long": ([3, 4, 5, 0], [1, 1, 1, 0]),
},
[1.0, 3.0],
)
monkeypatch.setattr(abliterate, "_REFERENCE_TEXTS", ("short", "long"))
perplexity = pipeline._reference_perplexity()
assert perplexity == pytest.approx(math.exp((1.0 * 1 + 3.0 * 2) / 3))
assert all(torch.equal(actual, expected) for actual, expected in zip(
model.labels,
[torch.tensor([[1, 2, -100, -100]]), torch.tensor([[3, 4, 5, -100]]),],
strict=True,
))
def test_reference_perplexity_skips_sequences_without_a_scored_next_token(monkeypatch):
pipeline, model = _pipeline(
{"empty": ([1], [1]), "scored": ([2, 3], [1, 1])},
[2.0],
)
monkeypatch.setattr(abliterate, "_REFERENCE_TEXTS", ("empty", "scored"))
assert pipeline._reference_perplexity() == pytest.approx(math.exp(2.0))
assert len(model.labels) == 1
assert torch.equal(model.labels[0], torch.tensor([[2, 3]]))
def test_verify_perplexity_uses_the_same_shifted_label_weighting(monkeypatch):
pipeline, model = _pipeline(
{"short": ([1, 2, 0, 0], [1, 1, 0, 0]),
"long": ([3, 4, 5, 0], [1, 1, 1, 0])},
[1.0, 3.0],
)
monkeypatch.setattr(abliterate, "_REFERENCE_TEXTS", ("short", "long"))
pipeline._quality_metrics = {}
pipeline.kl_budget = 0.5
pipeline.use_kl_optimization = False
pipeline._stock_baseline = {}
pipeline._emit = lambda *_args, **_kwargs: None
pipeline._free_gpu_memory = lambda: None
pipeline._enforce_perplexity_guardrail = lambda _perplexity: None
pipeline._verification_prompts = lambda: ([], [])
pipeline._admit_generation_diagnostic = lambda *_args: False
pipeline._baseline_token_logits = []
pipeline._kl_eval_prompts = []
pipeline._clear_kl_baseline = lambda: None
pipeline._enforce_final_kl_budget = lambda: None
pipeline._measure_spectral_diagnostic = lambda: None
pipeline._refusal_baseline_prompts = None # as set by __init__ before any refusal sample exists
pipeline._verify()
assert pipeline._quality_metrics["perplexity"] == pytest.approx(math.exp((1.0 + 6.0) / 3))
assert pipeline._quality_metrics["nonfinite_reference_loss_count"] == 0
assert all(torch.equal(actual, expected) for actual, expected in zip(
model.labels,
[torch.tensor([[1, 2, -100, -100]]), torch.tensor([[3, 4, 5, -100]]),],
strict=True,
))
def test_verify_rejects_reference_texts_without_scored_labels(monkeypatch):
pipeline, model = _pipeline({"empty": ([1], [1])}, [])
monkeypatch.setattr(abliterate, "_REFERENCE_TEXTS", ("empty",))
pipeline._quality_metrics = {}
pipeline.kl_budget = 0.5
pipeline.use_kl_optimization = False
pipeline._emit = lambda *_args, **_kwargs: None
pipeline._free_gpu_memory = lambda: None
pipeline._fail_validation = lambda _metric, _value, message: (_ for _ in ()).throw(
ValueError(message)
)
with pytest.raises(ValueError, match="no scored next-token labels"):
pipeline._verify()
assert model.labels == []