Files
OBLITERATUS/tests/test_reproducibility_pipeline.py
Joseph MaglyandClaude Opus 5.5 7fa2ae87e8 Forward fold_post_norms to remote runs and record it in golden metadata
Remote obliterate commands accept the folding opt-out, folded repair
candidates are validated after storage-dtype conversion, the refusal golden
fixtures record the new provenance key (numeric values unchanged), and the
risk map registers the shared post-norm module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 23:22:29 -04:00

250 lines
9.6 KiB
Python

"""Seed propagation, run ordering, and offline reproducibility contracts."""
from __future__ import annotations
import json
import random
import shlex
from unittest.mock import MagicMock
import numpy as np
import pytest
import torch
from obliteratus import cli
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.informed_pipeline import InformedAbliterationPipeline
from obliteratus.run_archive import RunArchive
from obliteratus.remote import RemoteConfig, RemoteRunner
from obliteratus.sweep import SweepConfig, run_sweep
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
pytestmark = pytest.mark.cpu
def _stub_base_stages(monkeypatch):
# Seed-order unit tests stub loading entirely, so no model metadata exists.
monkeypatch.setattr(
AbliterationPipeline, "_admit_pipeline_phase", lambda self, stage, **kwargs: None,
)
for name in (
"_summon",
"_capture_stock_baseline",
"_validate_architecture_surgery_support",
"_free_gpu_memory",
"_distill",
"_capture_baseline_kl_logits",
"_excise",
"_verify",
):
monkeypatch.setattr(AbliterationPipeline, name, lambda _self: None)
monkeypatch.setattr(AbliterationPipeline, "_rebirth", lambda self: self.output_dir)
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("seed", [None, 0, 42])
def test_cli_seed_is_set_before_loading_and_probing(monkeypatch, tmp_path, command, seed):
_stub_base_stages(monkeypatch)
set_seed = MagicMock()
monkeypatch.setattr("obliteratus.reproducibility.set_seed", set_seed)
stages = []
def check_seed(self):
assert self.seed == seed
if seed is None:
set_seed.assert_not_called()
else:
set_seed.assert_called_once_with(seed)
stages.append(self._active_stage)
monkeypatch.setattr(AbliterationPipeline, "_summon", check_seed)
monkeypatch.setattr(AbliterationPipeline, "_probe", check_seed)
monkeypatch.setattr("rich.live.Live", MagicMock())
monkeypatch.setattr(cli, "console", MagicMock())
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", lambda _pipe: None)
arguments = [command, "test-model", "--method", "basic", "--output-dir", str(tmp_path)]
if seed is not None:
arguments.extend(["--seed", str(seed)])
cli.main(arguments)
assert stages == ["summon", "probe"]
if seed is None:
set_seed.assert_not_called()
else:
set_seed.assert_called_once_with(seed)
@pytest.mark.parametrize("seed", [None, 0, 42])
def test_informed_seed_is_forwarded_and_set_on_each_run(monkeypatch, tmp_path, seed):
_stub_base_stages(monkeypatch)
for name in ("_analyze", "_distill_informed", "_excise_informed", "_verify_and_compensate"):
monkeypatch.setattr(InformedAbliterationPipeline, name, lambda _self: None)
monkeypatch.setattr(
InformedAbliterationPipeline, "_rebirth_informed", lambda self: self.output_dir,
)
set_seed = MagicMock()
monkeypatch.setattr("obliteratus.reproducibility.set_seed", set_seed)
stages = []
def check_seed(self):
assert self.seed == seed
assert set_seed.call_count == (0 if seed is None else len(stages) // 2 + 1)
stages.append(self._active_stage)
monkeypatch.setattr(InformedAbliterationPipeline, "_summon", check_seed)
monkeypatch.setattr(InformedAbliterationPipeline, "_probe", check_seed)
pipeline = InformedAbliterationPipeline("test-model", output_dir=str(tmp_path), seed=seed)
set_seed.assert_not_called()
for _ in range(2):
output, _report = pipeline.run_informed()
assert output == tmp_path
assert stages == ["summon", "probe", "summon", "probe"]
expected_calls = [] if seed is None else [(seed,)] * 2
assert [call.args for call in set_seed.call_args_list] == expected_calls
def test_sweep_passes_each_seed_to_the_real_pipeline(monkeypatch, tmp_path):
_stub_base_stages(monkeypatch)
set_seed = MagicMock()
monkeypatch.setattr("obliteratus.reproducibility.set_seed", set_seed)
seeds = []
def probe(self):
set_seed.assert_called_with(self.seed)
seeds.append(self.seed)
monkeypatch.setattr(AbliterationPipeline, "_probe", probe)
results = run_sweep(SweepConfig(
model_name="test-model",
sweep_params={},
fixed_params={"method": "basic"},
output_dir=str(tmp_path),
seed=0,
n_seeds=2,
))
assert seeds == [0, 1]
assert [result.seed for result in results] == seeds
assert all(result.error is None for result in results)
assert [call.args for call in set_seed.call_args_list] == [(0,), (1,)]
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("seed", [None, 0, 42])
@pytest.mark.parametrize("fold_post_norms", [False, True])
def test_remote_seed_is_forwarded_to_cli_command(monkeypatch, command, seed, fold_post_norms):
runner = MagicMock()
runner.run_obliterate.return_value = "output"
monkeypatch.setattr(cli, "_make_remote_runner", lambda _args: runner)
monkeypatch.setattr(cli, "console", MagicMock())
arguments = [command, "test-model", "--remote", "test-host"]
if seed is not None:
arguments.extend(["--seed", str(seed)])
if not fold_post_norms:
arguments.append("--no-fold-post-norms")
cli.main(arguments)
kwargs = runner.run_obliterate.call_args.kwargs
assert kwargs["fold_post_norms"] is fold_post_norms
if seed is None:
assert "seed" not in kwargs
else:
assert kwargs["seed"] == seed
remote = RemoteRunner(RemoteConfig(host="test-host"))
tokens = shlex.split(remote.build_obliterate_command(**{
key: value for key, value in kwargs.items() if key != "local_output_dir"
}))
assert ("--no-fold-post-norms" in tokens) is (not fold_post_norms)
if seed is None:
assert "--seed" not in tokens
else:
assert tokens[tokens.index("--seed") + 1] == str(seed)
@pytest.mark.parametrize("seed", [None, 0, 42])
def test_seed_metadata_preserves_unseeded_format(tmp_path, seed):
pipeline = AbliterationPipeline("test-model", method="basic", seed=seed)
metadata = pipeline._build_metadata()
informed = InformedAbliterationPipeline("test-model", output_dir=str(tmp_path), seed=seed)
informed.handle = MagicMock()
informed._rebirth_informed()
informed_metadata = json.loads((tmp_path / "abliteration_metadata.json").read_text())
for saved_metadata in (metadata, informed_metadata):
if seed is None:
assert "seed" not in saved_metadata
else:
assert saved_metadata["seed"] == seed
@pytest.mark.parametrize("seed_args", [[], ["--seed", "0"], ["--seed", "42"], ["--seed=42"]])
def test_run_archive_exposes_cli_seed(tmp_path, seed_args):
archive = RunArchive(tmp_path)
run_id = archive.begin(["test-model", *seed_args])
expected_seed = None if not seed_args else seed_args[-1].split("=")[-1]
assert archive.status(run_id)["seed"] == expected_seed
@pytest.mark.usefixtures("tiny_model_runtime")
@pytest.mark.integration
def test_seeded_offline_runs_save_seed_and_identical_directions(tmp_path, monkeypatch):
python_state = random.getstate()
numpy_state = np.random.get_state()
deterministic = torch.are_deterministic_algorithms_enabled()
warn_only = torch.is_deterministic_algorithms_warn_only_enabled()
cudnn_deterministic = torch.backends.cudnn.deterministic
cudnn_benchmark = torch.backends.cudnn.benchmark
threads = torch.get_num_threads()
monkeypatch.setenv("CUBLAS_WORKSPACE_CONFIG", ":4096:8")
try:
torch.set_num_threads(1)
with torch.random.fork_rng(devices=[]):
source = build_tiny_offline_model(tmp_path / "source")
archive = RunArchive(tmp_path / "archive")
run_id = archive.begin([str(source), "--seed", "42"])
directions = []
for index in range(2):
torch.manual_seed(100 + index)
output = tmp_path / f"output-{index}"
pipeline = AbliterationPipeline(
model_name=str(source),
output_dir=str(output),
device="cpu",
dtype="float32",
method="basic",
seed=42,
n_directions=1,
max_seq_length=8,
verify_sample_size=1,
refusal_max_tokens=1,
max_perplexity_increase=1000.0,
max_degenerate_fraction=1.0,
harmful_prompts=["harmful request"],
harmless_prompts=["harmless request"],
)
assert pipeline.run() == output
directions.append({
key: value.clone() for key, value in pipeline.refusal_directions.items()
})
metadata = json.loads((output / "abliteration_metadata.json").read_text())
assert metadata["seed"] == int(archive.status(run_id)["seed"]) == 42
assert directions[0]
assert directions[0].keys() == directions[1].keys()
assert all(
torch.equal(value, directions[1][key]) for key, value in directions[0].items()
)
finally:
random.setstate(python_state)
np.random.set_state(numpy_state)
torch.use_deterministic_algorithms(deterministic, warn_only=warn_only)
torch.backends.cudnn.deterministic = cudnn_deterministic
torch.backends.cudnn.benchmark = cudnn_benchmark
torch.set_num_threads(threads)