mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
83 lines
3.5 KiB
Python
83 lines
3.5 KiB
Python
"""AT-CV-01.3: exact refusal compatibility across CPU-runnable presets."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.abliterate import METHODS
|
|
from tests.fixtures.refusal_golden import SKIPPED_METHODS, capture_refusal
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def golden_model(tmp_path_factory):
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
try:
|
|
yield build_tiny_offline_model(tmp_path_factory.mktemp("refusal") / "model")
|
|
finally:
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
@pytest.mark.parametrize("method", METHODS)
|
|
def test_refusal_golden(golden_model, method):
|
|
if method in SKIPPED_METHODS:
|
|
pytest.skip(SKIPPED_METHODS[method])
|
|
baseline = json.loads(
|
|
(Path(__file__).parent / "fixtures/refusal_golden.json").read_text()
|
|
)[method]
|
|
default_config, default_tensors = capture_refusal(golden_model, method)
|
|
explicit_config, explicit_tensors = capture_refusal(golden_model, method, concept="refusal")
|
|
assert default_config == explicit_config == baseline["config"]
|
|
assert json.dumps(default_config, sort_keys=True).encode() == json.dumps(
|
|
baseline["config"], sort_keys=True,
|
|
).encode()
|
|
assert default_tensors.keys() == explicit_tensors.keys() == baseline["tensors"].keys()
|
|
for name, tensor in default_tensors.items():
|
|
# Exact parity between the two entry points is the compatibility contract.
|
|
assert torch.equal(tensor, explicit_tensors[name]), name
|
|
# Independently captured at the base commit (fixtures/refusal_golden.md) on
|
|
# one CPU. Other BLAS backends differ by up to ~2e-4 on these unit vectors,
|
|
# so compare within a cross-platform tolerance, sign-agnostic for
|
|
# eigen/SVD directions.
|
|
expected = torch.tensor(baseline["tensors"][name])
|
|
_assert_direction_rows_close(tensor, expected)
|
|
|
|
|
|
def _assert_direction_rows_close(actual, expected):
|
|
"""SVD basis rows have independent signs, while row order stays pinned."""
|
|
assert actual.shape == expected.shape
|
|
actual_rows = actual.unsqueeze(0) if actual.ndim == 1 else actual
|
|
expected_rows = expected.unsqueeze(0) if expected.ndim == 1 else expected
|
|
for row, reference in zip(actual_rows, expected_rows):
|
|
if not torch.allclose(row, reference, rtol=1e-3, atol=1e-3):
|
|
torch.testing.assert_close(row, -reference, rtol=1e-3, atol=1e-3)
|
|
|
|
|
|
def test_golden_comparison_accepts_independent_signs_but_rejects_wrong_vectors():
|
|
basis = torch.tensor([[1., 0., 0.], [0., 1., 0.]])
|
|
_assert_direction_rows_close(basis * torch.tensor([[-1.], [1.]]), basis)
|
|
with pytest.raises(AssertionError):
|
|
_assert_direction_rows_close(torch.tensor([[1., 0., 0.], [0., 0., 1.]]), basis)
|
|
|
|
|
|
def test_refusal_capture_keeps_single_cpu_grouping_with_available_gpu(golden_model, monkeypatch):
|
|
from obliteratus import device
|
|
|
|
monkeypatch.setattr(device, "get_total_free_gb", lambda: 16.0)
|
|
monkeypatch.setattr(device, "is_gpu_available", lambda: True)
|
|
_, actual = capture_refusal(golden_model, "basic")
|
|
baseline = json.loads(
|
|
(Path(__file__).parent / "fixtures/refusal_golden.json").read_text()
|
|
)["basic"]["tensors"]
|
|
for name, tensor in actual.items():
|
|
torch.testing.assert_close(tensor, torch.tensor(baseline[name]), rtol=1e-3, atol=1e-3)
|