mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Search float direction indices across all extracted source layers and include that domain in persisted study identity. Apply informed attention scaling to the warm-start maximum. Penalize the squared harmless activation component removed by the optimized RDO direction so preservation has a gradient. Add behavioral regressions.
91 lines
3.7 KiB
Python
91 lines
3.7 KiB
Python
"""RDO preserves harmless activation energy through its optimized direction."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
|
|
def _pipeline(harmful, harmless, *, directions=1, layers=1, refine=True):
|
|
pipeline = AbliterationPipeline(
|
|
model_name="fixture/rdo", method="rdo", layers=[0],
|
|
n_directions=directions, rdo_refinement=refine,
|
|
)
|
|
pipeline._harmful_acts = {i: list(harmful) for i in range(layers)}
|
|
pipeline._harmless_acts = {i: list(harmless) for i in range(layers)}
|
|
pipeline._harmful_means = {
|
|
i: harmful.mean(0, keepdim=True) for i in range(layers)
|
|
}
|
|
pipeline._harmless_means = {
|
|
i: harmless.mean(0, keepdim=True) for i in range(layers)
|
|
}
|
|
return pipeline
|
|
|
|
|
|
@pytest.mark.parametrize("sign", [-1.0, 1.0])
|
|
def test_balanced_harmless_variance_contributes_direction_gradient(monkeypatch, sign):
|
|
harmful = torch.tensor([[1., 0., 0.]]).repeat(4, 1)
|
|
benign_axis = torch.tensor([1., sign, 0.])
|
|
harmless = torch.stack([benign_axis, -benign_axis] * 2)
|
|
pipeline = _pipeline(harmful, harmless)
|
|
gradients = []
|
|
original_step = torch.optim.Adam.step
|
|
|
|
def capture_step(optimizer, *args, **kwargs):
|
|
gradients.append(optimizer.param_groups[0]["params"][0].grad.detach().clone())
|
|
return original_step(optimizer, *args, **kwargs)
|
|
|
|
monkeypatch.setattr("obliteratus.abliterate._RDO_REFINEMENT_STEPS", 1)
|
|
monkeypatch.setattr(torch.optim.Adam, "step", capture_step)
|
|
|
|
pipeline._distill()
|
|
|
|
assert len(gradients) == 1
|
|
gradient = gradients[0]
|
|
assert torch.isfinite(gradient).all()
|
|
# The refusal objective and tether are stationary at the initial direction.
|
|
# Opposite harmless rows cancel in the old mean but still suffer erasure.
|
|
assert gradient[1] * sign > 0.1
|
|
initial = torch.tensor([1., 0., 0.])
|
|
updated = torch.nn.functional.normalize(initial - 0.01 * gradient, dim=0)
|
|
assert (harmless @ updated).square().sum() < (harmless @ initial).square().sum()
|
|
|
|
|
|
def test_rdo_reduces_harmless_erasure_without_changing_class_means(monkeypatch):
|
|
harmful = torch.tensor([[1., 0., 0.]]).repeat(4, 1)
|
|
harmless = torch.tensor([[1., 1., 0.], [-1., -1., 0.]]).repeat(2, 1)
|
|
unprotected = _pipeline(harmful, torch.zeros_like(harmless))
|
|
protected = _pipeline(harmful, harmless)
|
|
monkeypatch.setattr("obliteratus.abliterate._RDO_REFINEMENT_STEPS", 100)
|
|
|
|
unprotected._distill()
|
|
protected._distill()
|
|
|
|
before = harmless @ unprotected.concept_directions[0]
|
|
after = harmless @ protected.concept_directions[0]
|
|
assert after.square().mean() < 0.9 * before.square().mean()
|
|
assert torch.allclose(protected.concept_directions[0].norm(), torch.tensor(1.))
|
|
|
|
|
|
def test_refined_direction_reaches_orthonormal_multidirection_subspace(monkeypatch):
|
|
generator = torch.Generator().manual_seed(2201)
|
|
harmful = torch.randn(12, 7, generator=generator) + torch.tensor(
|
|
[2., 1., 0., 0., 0., 0., 0.],
|
|
)
|
|
harmless = torch.randn(12, 7, generator=generator)
|
|
# Forty cached layers avoid the separate small-model direction-count cap.
|
|
baseline = _pipeline(harmful, harmless, directions=4, layers=40, refine=False)
|
|
refined = _pipeline(harmful, harmless, directions=4, layers=40)
|
|
monkeypatch.setattr("obliteratus.abliterate._RDO_REFINEMENT_STEPS", 20)
|
|
|
|
baseline._distill()
|
|
refined._distill()
|
|
|
|
subspace = refined.concept_subspaces[0]
|
|
assert subspace.shape == (4, 7)
|
|
assert not torch.allclose(refined.concept_directions[0], baseline.concept_directions[0])
|
|
assert torch.equal(subspace[0], refined.concept_directions[0])
|
|
torch.testing.assert_close(subspace @ subspace.T, torch.eye(4), atol=1e-6, rtol=1e-6)
|