Files
OBLITERATUS/tests/test_concept_moe_intervention.py

1005 lines
47 KiB
Python

"""Per-expert concept surgery on random, offline MoE layouts."""
from copy import deepcopy
from dataclasses import replace
import time
import pytest
import torch
from torch import nn
from transformers import (
OlmoeConfig, OlmoeForCausalLM, Qwen2MoeConfig, Qwen2MoeForCausalLM,
Qwen3MoeConfig, Qwen3MoeForCausalLM,
)
from obliteratus.concepts import ConceptDirection, apply_concept_intervention
from obliteratus.models.offload_surgery import LogicalParameterTransaction, resolve_logical_parameter
pytestmark = [pytest.mark.cpu, pytest.mark.integration, pytest.mark.usefixtures("tiny_model_runtime")]
@pytest.fixture(params=["olmoe", "qwen2_moe", "qwen3_moe"])
def model(request):
configs = {
"olmoe": (OlmoeConfig, OlmoeForCausalLM),
"qwen2_moe": (Qwen2MoeConfig, Qwen2MoeForCausalLM),
"qwen3_moe": (Qwen3MoeConfig, Qwen3MoeForCausalLM),
}
config_cls, model_cls = configs[request.param]
config = config_cls(
vocab_size=32, hidden_size=16, intermediate_size=24,
moe_intermediate_size=24, shared_expert_intermediate_size=24,
num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=2,
num_experts=3, num_experts_per_tok=2, max_position_embeddings=32,
pad_token_id=0, eos_token_id=2, bos_token_id=1,
)
config._attn_implementation = "eager"
with torch.random.fork_rng():
torch.manual_seed(310)
return model_cls(config).eval()
@pytest.fixture
def direction():
return ConceptDirection(
directions={0: torch.eye(16)[0]}, expert_directions={0: {0: torch.eye(16)[1]}},
extraction_layers=[0], intervention_layers=[0], concept_id="test-concept",
validation={"concept_id": "test-concept", "pack_hash": None, "verdict": "pass"},
)
def _weights(ffn):
if isinstance(ffn.experts, nn.ModuleList):
return torch.stack([expert.down_proj.weight.detach() for expert in ffn.experts])
return resolve_logical_parameter(ffn.experts, "down_proj").tensor.detach()
def _as_module_list(ffn):
weights = _weights(ffn).clone()
experts = nn.ModuleList()
for weight in weights:
expert = nn.Module()
expert.down_proj = nn.Linear(weight.shape[1], weight.shape[0], bias=False)
expert.down_proj.weight.data.copy_(weight)
experts.append(expert)
ffn.experts = experts
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
@pytest.mark.parametrize("project_router", [False, True])
def test_expert_projection_fallback_and_router(model, direction, dtype, project_router):
model.to(dtype=dtype)
layer = model.model.layers[0]
ffn = layer.mlp
before = _weights(ffn).clone()
router = ffn.gate.weight.detach().clone()
attention = layer.self_attn.o_proj.weight.detach().clone()
run = apply_concept_intervention(
model, direction, "orthogonalize", project_router=project_router,
)
expected = before.clone()
expected[0, 1] = 0
expected[1:, 0] = 0
assert torch.equal(_weights(ffn), expected)
attention[0] = 0
assert torch.equal(layer.self_attn.o_proj.weight, attention)
if project_router:
router[:, 0] = 0
assert torch.equal(ffn.gate.weight, router)
assert run.parameters["project_router"] is project_router
with torch.inference_mode():
assert torch.isfinite(model(torch.tensor([[1, 3, 4]]), use_cache=False).logits).all()
def test_module_list_lora_matches_projection_and_restores(model, direction):
_as_module_list(model.model.layers[0].mlp)
reference = deepcopy(model)
apply_concept_intervention(reference, direction, "orthogonalize", projection_target="ffn")
experts = model.model.layers[0].mlp.experts
expected = reference.model.layers[0].mlp.experts
inputs = torch.arange(48, dtype=torch.float32).reshape(2, 24) / 48
before = [expert.down_proj(inputs).detach().clone() for expert in experts]
weights = _weights(model.model.layers[0].mlp).clone()
with apply_concept_intervention(model, direction, "lora", projection_target="ffn"):
for expert, target in zip(experts, expected):
torch.testing.assert_close(expert.down_proj(inputs), target.down_proj(inputs))
assert torch.equal(_weights(model.model.layers[0].mlp), weights)
for expert, output in zip(experts, before):
assert torch.equal(expert.down_proj(inputs), output)
assert not expert.down_proj._forward_hooks
@pytest.mark.parametrize("mode", ["orthogonalize", "lora", "project", "steer"])
@pytest.mark.parametrize("pinned", [False, True])
def test_per_expert_preservation_rejected_before_mutation(model, direction, mode, pinned):
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
if pinned:
model.model.layers.append(deepcopy(model.model.layers[0]))
model.config.num_hidden_layers = 2
direction.directions[1] = torch.eye(16)[2]
direction.extraction_layers = [1]
direction.extraction_layer_source = "pinned"
direction.recipe = replace(direction.recipe, extraction_layer={"fixture/moe": 1},
intervention_layer={"rule": "explicit", "layers": [0]})
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
protected = [torch.eye(16)[1]]
with pytest.raises(ValueError, match="preserve does not support per-expert target"):
apply_concept_intervention(model, direction, mode, preserve=protected)
with pytest.raises(ValueError, match="preserve does not support per-expert target"):
ProtectedDirectionRepair(model, direction, mode, protected)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
def test_per_expert_partial_rows_rejected_before_mutation(model, direction, mode):
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError, match="Per-expert directions require projection_row_fraction=1.0"):
apply_concept_intervention(model, direction, mode, projection_row_fraction=0.5)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
def test_empty_protection_retains_per_expert_targets(model, direction):
original = _weights(model.model.layers[0].mlp).clone()
run = apply_concept_intervention(model, direction, "orthogonalize", preserve=[])
original[0, 1] = 0
original[1:, 0] = 0
assert torch.equal(_weights(model.model.layers[0].mlp), original)
assert "preserve" not in run.parameters
@pytest.mark.parametrize("fused", [False, True])
def test_shared_target_protection_includes_experts_and_router(model, direction, fused):
model.model.layers[0].mlp = _square_ffn(fused, torch.float32)
direction.expert_directions.clear()
direction.directions[0] = torch.eye(16)[0] + torch.eye(16)[1]
ffn = model.model.layers[0].mlp
original, router = _weights(ffn).clone(), ffn.gate.weight.detach().clone()
shared = ffn.shared_expert.down_proj.weight.detach().clone()
apply_concept_intervention(
model, direction, "orthogonalize", preserve=[torch.eye(16)[0]],
project_router=True, projection_target="ffn",
)
original[:, 1] = 0
router[:, 1] = 0
shared[1] = 0
torch.testing.assert_close(_weights(ffn), original)
torch.testing.assert_close(ffn.gate.weight, router)
torch.testing.assert_close(ffn.shared_expert.down_proj.weight, shared)
def test_protected_repair_restores_router_on_rejected_trial(model, direction):
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
model.model.layers[0].mlp = _square_ffn(False, torch.float32)
direction.expert_directions.clear()
direction.directions[0] = torch.eye(16)[0] + torch.eye(16)[1]
options = {"project_router": True, "projection_target": "ffn"}
backend = ProtectedDirectionRepair(
model, direction, "orthogonalize", [torch.eye(16)[0]], **options,
)
router = model.model.layers[0].mlp.gate.weight.detach().clone()
original = apply_concept_intervention(model, direction, "orthogonalize", **options)
edited = {name: value.detach().clone() for name, value in model.state_dict().items()}
backend.bind(original)
assert backend.available
with next(backend("protected_direction", None)):
router[:, 1] = 0
torch.testing.assert_close(model.model.layers[0].mlp.gate.weight, router)
assert all(torch.equal(value, edited[name]) for name, value in model.state_dict().items())
@pytest.mark.parametrize("project_router", [False, True])
def test_pinned_shared_transfer_keeps_destination_expert_directions(model, direction, project_router):
model.model.layers.append(deepcopy(model.model.layers[0]))
model.config.num_hidden_layers = 2
direction.directions[1] = torch.eye(16)[2]
direction.expert_directions[1] = {0: torch.eye(16)[3]}
direction.intervention_layers = [1]
direction.extraction_layer_source = "pinned"
direction.recipe = replace(
direction.recipe, extraction_layer={"fixture/moe": 0},
intervention_layer={"rule": "explicit", "layers": [1]}, steering_scale="raw",
)
untouched = {name: value.clone() for name, value in model.model.layers[0].state_dict().items()}
layer = model.model.layers[1]
expected = _weights(layer.mlp).clone()
expected[0, 3] = 0
expected[1:, 0] = 0
attention = layer.self_attn.o_proj.weight.detach().clone()
attention[0] = 0
router = layer.mlp.gate.weight.detach().clone()
if project_router:
router[:, 0] = 0
run = apply_concept_intervention(model, direction, "orthogonalize", project_router=project_router)
assert run.layers == [1]
assert torch.equal(_weights(layer.mlp), expected)
assert torch.equal(layer.self_attn.o_proj.weight, attention)
assert torch.equal(layer.mlp.gate.weight, router)
shared = getattr(layer.mlp, "shared_expert", None)
if shared is not None:
assert torch.count_nonzero(shared.down_proj.weight[0]) == 0
for name, value in model.model.layers[0].state_dict().items():
assert torch.equal(value, untouched[name])
def test_tied_expert_writers_keep_expert_target_despite_different_shared_vectors(model, direction):
_as_module_list(model.model.layers[0].mlp)
model.model.layers.append(deepcopy(model.model.layers[0]))
model.config.num_hidden_layers = 2
first, second = (layer.mlp.experts[0].down_proj for layer in model.model.layers)
second.weight = first.weight
direction.directions[1] = torch.eye(16)[2]
direction.expert_directions[1] = {0: torch.eye(16)[1]}
expected = first.weight.detach().clone()
expected[1] = 0
apply_concept_intervention(model, direction, "orthogonalize", layers="all", projection_target="ffn")
assert first.weight is second.weight
assert torch.equal(first.weight, expected)
def test_shared_expert_uses_shared_direction(model, direction):
ffn = model.model.layers[0].mlp
shared = getattr(ffn, "shared_expert", None)
if shared is None:
pytest.skip("This architecture has no shared expert")
original = shared.down_proj.weight.detach().clone()
apply_concept_intervention(model, direction, "orthogonalize", projection_target="ffn")
original[0] = 0
assert torch.equal(shared.down_proj.weight, original)
@pytest.mark.parametrize("chunk_bytes", [16, 16 * 1024**2])
def test_fused_offload_and_chunking_preserve_expert_vectors(model, direction, monkeypatch, chunk_bytes):
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
import obliteratus.concepts.intervention as intervention
ffn = model.model.layers[0].mlp
if isinstance(ffn.experts, nn.ModuleList):
pytest.skip("This transformers version uses individual expert modules")
original = _weights(ffn).clone()
add_hook_to_module(ffn.experts, AlignDevicesHook(execution_device="cpu", offload=True))
monkeypatch.setattr(intervention, "_PROJECTION_CHUNK_BYTES", chunk_bytes)
apply_concept_intervention(model, direction, "orthogonalize", projection_target="ffn")
original[0, 1] = 0
original[1:, 0] = 0
assert torch.equal(_weights(ffn), original)
assert ffn.experts.down_proj.device.type == "meta"
def test_router_failure_rolls_back_all_expert_writers(model, direction, monkeypatch):
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
router = model.model.layers[0].mlp.gate
commit = LogicalParameterTransaction.commit
def fail(transaction, updated):
if transaction.module is router:
raise RuntimeError("router commit failed")
return commit(transaction, updated)
monkeypatch.setattr(LogicalParameterTransaction, "commit", fail)
with pytest.raises(RuntimeError, match="router commit failed"):
apply_concept_intervention(model, direction, "orthogonalize", project_router=True)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
@pytest.mark.parametrize("mode", ["steer", "project"])
def test_residual_hooks_keep_shared_direction_and_remove(model, direction, mode):
weights = {name: value.detach().clone() for name, value in model.state_dict().items()}
with apply_concept_intervention(model, direction, mode) as run:
vector = run.config.per_layer_vectors[0][0]
assert torch.equal(vector.direction, direction.directions[0])
assert all(torch.equal(value, weights[name]) for name, value in model.state_dict().items())
assert not model.model.layers[0]._forward_hooks
@pytest.mark.parametrize("options", [{"subspace": True}, {"reference": "mean"}])
@pytest.mark.parametrize("entry", ["project", "orthogonalize", "lora", "repair", "evaluate"])
def test_per_expert_extended_geometry_rejected_before_mutation(model, direction, options, entry):
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.concepts.repair_model import WeightRepair
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
direction.subspaces = {0: torch.eye(16)[:2]}
direction.reference_means = {0: torch.ones(16)}
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
# Affine LoRA is already rejected independently of expert targeting.
message = "Affine erasure" if entry == "lora" and "reference" in options else "Per-expert directions"
with pytest.raises(ValueError, match=message):
if entry == "repair":
WeightRepair(model, direction, **options)
elif entry == "evaluate":
ModelEvaluator(model, _fixture_tokenizer(), direction, **options)
else:
apply_concept_intervention(model, direction, entry, **options)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("fused", [False, True])
@pytest.mark.parametrize("project_router", [False, True])
def test_shared_rank_k_projects_expert_output_and_router_input_axes(
model, direction, monkeypatch, fused, project_router,
):
model.model.layers[0].mlp = _square_ffn(fused, torch.bfloat16)
ffn = model.model.layers[0].mlp
direction.expert_directions = {0: {}}
direction.subspaces = {0: torch.eye(16)[:2]}
original = _weights(ffn).clone()
router = ffn.gate.weight.detach().clone()
monkeypatch.setattr("obliteratus.concepts.intervention._PROJECTION_CHUNK_BYTES", 16)
run = apply_concept_intervention(
model, direction, "orthogonalize", subspace=True,
projection_target="ffn", project_router=project_router,
)
original[:, :2] = 0
if project_router:
router[:, :2] = 0
assert torch.equal(_weights(ffn), original)
assert torch.equal(ffn.gate.weight, router)
assert torch.equal(ffn.shared_expert.down_proj.weight[:2], torch.zeros(2, 16))
assert run.parameters["geometry"]["0"]["rank"] == 2
def test_affine_router_rejected_before_mutation(model, direction):
direction.expert_directions.clear()
direction.reference_means = {0: torch.ones(16)}
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError, match="project_router does not support affine"):
apply_concept_intervention(
model, direction, "orthogonalize", reference="mean", project_router=True,
)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
def test_shared_rank_k_module_list_lora_matches_permanent_edit(model, direction):
_as_module_list(model.model.layers[0].mlp)
direction.expert_directions.clear()
direction.subspaces = {0: torch.eye(16)[:2]}
reference = deepcopy(model)
apply_concept_intervention(
reference, direction, "orthogonalize", subspace=True, projection_target="ffn",
)
experts = model.model.layers[0].mlp.experts
expected = reference.model.layers[0].mlp.experts
inputs = torch.arange(48, dtype=torch.float32).reshape(2, 24) / 48
before = [expert.down_proj(inputs).detach().clone() for expert in experts]
weights = _weights(model.model.layers[0].mlp).clone()
with apply_concept_intervention(model, direction, "lora", subspace=True, projection_target="ffn"):
for expert, target in zip(experts, expected):
torch.testing.assert_close(expert.down_proj(inputs), target.down_proj(inputs))
assert torch.equal(_weights(model.model.layers[0].mlp), weights)
for expert, output in zip(experts, before):
assert torch.equal(expert.down_proj(inputs), output)
assert not expert.down_proj._forward_hooks
def test_shared_affine_readout_retains_routing_metrics(model, direction):
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.concepts.moe import routing_drift
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
direction.expert_directions.clear()
direction.subspaces = {0: torch.eye(16)[:2]}
direction.reference_means = {0: torch.ones(16)}
tokenizer = _fixture_tokenizer()
tokenizer.model_input_names = ["input_ids", "attention_mask"]
evaluator = ModelEvaluator(model, tokenizer, direction, subspace=True, reference="mean")
evaluator.prompts = ["hello world"]
before = evaluator.measure()
with apply_concept_intervention(model, direction, "project", subspace=True, reference="mean"):
after = evaluator.measure()
assert after["projections"]["0"] == pytest.approx(0, abs=1e-6)
assert after["readout"] == "mean final-token norm of P(x - reference)"
drift = routing_drift(before["routing"], after["routing"])["0"]
assert drift["tokens"] > 0
assert drift["expert_choice_kl"] == 0
assert drift["pristine_expert_load"] == drift["edited_expert_load"]
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("option", ["unknown_expert", "fused_lora", "router_hook", "final_token_lora"])
def test_unsupported_options_fail_before_mutation(model, direction, option):
mode, kwargs = "orthogonalize", {}
if option == "unknown_expert":
direction.expert_directions[0][99] = torch.eye(16)[2]
elif option == "fused_lora":
if isinstance(model.model.layers[0].mlp.experts, nn.ModuleList):
pytest.skip("This transformers version uses individual expert modules")
mode = "lora"
elif option == "router_hook":
mode, kwargs = "steer", {"project_router": True}
else:
mode, kwargs = "lora", {"positions": "final_token"}
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError):
apply_concept_intervention(model, direction, mode, **kwargs)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
def _square_ffn(fused, dtype):
ffn = nn.Module()
ffn.gate = nn.Linear(16, 3, bias=False, dtype=dtype)
weights = (torch.arange(3 * 16 * 16).reshape(3, 16, 16).float() / 512).to(dtype)
if fused:
ffn.experts = nn.Module()
ffn.experts.down_proj = nn.Parameter(weights.clone())
ffn.experts.gate_up_proj = nn.Parameter(torch.ones(3, 32, 16, dtype=dtype))
else:
ffn.experts = nn.ModuleList()
for weight in weights:
expert = nn.Module()
expert.down_proj = nn.Linear(16, 16, bias=False, dtype=dtype)
expert.down_proj.weight.data.copy_(weight)
expert.gate_proj = nn.Linear(16, 16, bias=False, dtype=dtype)
expert.up_proj = nn.Linear(16, 16, bias=False, dtype=dtype)
ffn.experts.append(expert)
ffn.shared_expert = nn.Module()
ffn.shared_expert.down_proj = nn.Linear(16, 16, bias=False, dtype=dtype)
ffn.shared_expert.down_proj.weight.data.fill_(1)
ffn.shared_expert.up_proj = nn.Linear(16, 16, bias=False, dtype=dtype)
return ffn
@pytest.mark.parametrize("fused", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
def test_pipeline_square_experts_use_output_axis_and_shared_higher_direction(fused, dtype):
from obliteratus.abliterate import AbliterationPipeline
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
pipeline._expert_directions = {0: {0: torch.eye(16)[1]}}
ffn = _square_ffn(fused, dtype)
original = _weights(ffn).clone()
unchanged = {name: value.detach().clone() for name, value in ffn.named_parameters()
if "down_proj" not in name}
with torch.no_grad():
pipeline._project_moe_experts_granular(
ffn, torch.eye(16)[0], 0, output_only=True, project_router=False,
)
expected = original.clone()
expected[0, 1] = 0
expected[1:, 0] = 0
assert torch.equal(_weights(ffn), expected)
assert torch.equal(ffn.shared_expert.down_proj.weight[0], torch.zeros(16, dtype=dtype))
assert torch.equal(ffn.shared_expert.down_proj.weight[1:], torch.ones(15, 16, dtype=dtype))
with torch.no_grad():
pipeline._project_moe_experts_granular(
ffn, torch.eye(16)[2], 0, output_only=True, project_router=False,
use_expert_directions=False,
)
expected[:, 2] = 0
assert torch.equal(_weights(ffn), expected)
assert all(torch.equal(value, unchanged[name]) for name, value in ffn.named_parameters()
if name in unchanged)
@pytest.mark.parametrize("fused", [False, True])
def test_pipeline_router_orthogonalization_does_not_reintroduce_direction(fused):
from obliteratus.abliterate import AbliterationPipeline
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
pipeline._expert_directions = {}
ffn = _square_ffn(fused, torch.float32)
direction = torch.ones(16) / 4
with torch.no_grad():
ffn.gate.weight.zero_()
ffn.gate.weight[0, 0] = 100
pristine = ffn.gate.weight.detach().clone()
expected = pristine - (pristine @ direction)[:, None] * direction
with torch.no_grad():
pipeline._project_moe_experts_granular(
ffn, direction, 0, output_only=True, project_router=True,
)
torch.testing.assert_close(ffn.gate.weight, expected)
torch.testing.assert_close(ffn.gate.weight @ direction, torch.zeros(3), atol=1e-5, rtol=0)
# The old router stabilizer clips the outlier and destroys orthogonality.
clipped = expected.clamp(expected.mean() - 3 * expected.std(), expected.mean() + 3 * expected.std())
assert (clipped @ direction).abs().max() > 1
def _pipeline(model, tmp_path, **kwargs):
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.concepts import ConceptTarget, ExtractionRecipe
from obliteratus.models.loader import ModelHandle
n_directions = kwargs.get("n_directions", 1)
pipeline = AbliterationPipeline(
"offline", concept=ConceptTarget(
id="user/demo", positive=["a"], controls={"neutral": ["b"]},
recipe=ExtractionRecipe(
method="svd" if n_directions > 1 else "diff_means", n_directions=n_directions,
),
),
method="basic", output_dir=str(tmp_path), **kwargs,
)
pipeline.handle = ModelHandle(model, None, model.config, "offline", "causal_lm")
pipeline._strong_layers = [0]
pipeline.concept_directions = {0: torch.eye(16)[0]}
pipeline.concept_subspaces = {0: torch.eye(16)[:1]}
pipeline._expert_directions = {0: {0: torch.eye(16)[1]}}
return pipeline
@pytest.mark.parametrize("method", ["basic", "advanced", "inverted", "nuclear"])
@pytest.mark.parametrize("fused", [False, True])
@pytest.mark.parametrize("concept", [None, "refusal"])
@pytest.mark.parametrize("norm_preserve", [False, None])
@pytest.mark.parametrize("per_expert", [False, True])
def test_refusal_multidirection_honors_expert_norm_policy(
tmp_path, method, fused, concept, norm_preserve, per_expert,
):
from obliteratus.abliterate import AbliterationPipeline
pipeline = AbliterationPipeline(
"offline", method=method, concept=concept, output_dir=str(tmp_path),
projection_target="ffn", direction_method="svd", n_directions=3,
per_expert_directions=per_expert, project_biases=True, norm_preserve=norm_preserve,
regularization=0, refinement_passes=1, use_sae_features=False,
attention_head_surgery=False, expert_transplant=False, fold_post_norms=False,
)
layer = nn.Module()
layer.mlp = _square_ffn(fused, torch.float32)
ffn = layer.mlp
with torch.no_grad():
for parameter in ffn.parameters():
parameter.fill_(1)
ffn.gate.weight[:, ::2] = -1
if fused:
ffn.experts.down_proj_bias = nn.Parameter(torch.ones(3, 16))
else:
for expert in ffn.experts:
expert.down_proj.bias = nn.Parameter(torch.ones(16))
reference = deepcopy(ffn)
original_norms = {name: module.weight.float().norm().item()
for name, module in reference.named_modules() if hasattr(module, "weight")}
original_fused_norms = {
name: parameter.float().norm(dim=(1, 2))
for name, parameter in reference.named_parameters()
if parameter.ndim == 3
}
if method == "advanced" and norm_preserve is None:
assert pipeline.norm_preserve is True
router = ffn.gate.weight.detach().clone()
basis = torch.eye(16)[:3]
pipeline._strong_layers = [0]
pipeline.concept_directions = {0: basis[0]}
pipeline.concept_subspaces = {0: basis}
pipeline._expert_directions = {0: {0: (torch.eye(16)[1] + torch.eye(16)[3]) / 2**0.5}}
pipeline._expert_safety_scores = {0: [(0, 3.0), (1, 0.0), (2, -3.0)]}
strength = pipeline.reflection_strength if pipeline.invert_refusal else 1.0
with torch.no_grad():
# Reproduce the pre-concept dispatch with the original shared higher rows.
for index, row in enumerate(basis):
if per_expert and index == 0:
pipeline._project_moe_experts_granular(
reference, row[:, None], 0, regularization=1.0 - strength,
project_biases=True,
)
elif pipeline.invert_refusal:
pipeline._project_moe_experts_inverted(
reference, row[:, None], 0, project_biases=True,
)
else:
pipeline._project_moe_experts(reference, row[:, None], project_biases=True)
if pipeline.norm_preserve:
for name, module in reference.named_modules():
if name in original_norms:
current_norm = module.weight.float().norm().item()
if current_norm > 1e-8:
module.weight.mul_(min(1.1, original_norms[name] / current_norm))
for name, parameter in reference.named_parameters():
if name not in original_fused_norms:
continue
for expert, original_norm in zip(
parameter, original_fused_norms[name], strict=True,
):
current_norm = expert.float().norm().item()
if current_norm > 1e-8:
expert.mul_(min(1.1, original_norm.item() / current_norm))
pipeline._excise_inner([layer], "qwen2_moe", None, 2, time.time(), {})
for name, value in ffn.state_dict().items():
torch.testing.assert_close(value, reference.state_dict()[name])
if per_expert and not pipeline.norm_preserve:
# Higher rows remove capability components, even with reflection enabled.
weights = _weights(ffn)
torch.testing.assert_close(weights[1:, :, 1:3], torch.zeros(2, 16, 2))
torch.testing.assert_close(weights[0, :, 2], torch.full((16,), 1.0 - strength))
torch.testing.assert_close(ffn.gate.weight[:, 1:3],
router[:, 1:3] * (1.0 - min(strength, 1.5)))
if fused and per_expert:
# Legacy fused biases use shared rows, including the primary direction.
expected_bias = torch.ones(3, 16)
expected_bias[:, :3] = 0
torch.testing.assert_close(ffn.experts.down_proj_bias, expected_bias)
@pytest.mark.parametrize("fused", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
@pytest.mark.parametrize("dependent", [False, True])
@pytest.mark.parametrize("norm_preserve", [False, True])
def test_pipeline_multidirection_preserves_expert_removal(
model, tmp_path, fused, dtype, dependent, norm_preserve,
):
ffn = _square_ffn(fused, dtype)
model.model.layers[0].mlp = ffn
identity = torch.eye(16, dtype=dtype)
biases = torch.arange(48, dtype=dtype).reshape(3, 16)
with torch.no_grad():
if fused:
ffn.experts.down_proj.copy_(identity.expand(3, -1, -1))
ffn.experts.down_proj_bias = nn.Parameter(biases.clone())
else:
for index, expert in enumerate(ffn.experts):
expert.down_proj.weight.copy_(identity)
expert.down_proj.bias = nn.Parameter(biases[index].clone())
pipeline = _pipeline(model, tmp_path, projection_target="ffn", regularization=0,
norm_preserve=norm_preserve, n_directions=3, project_biases=True)
pipeline.concept_subspaces = {0: torch.eye(16)[:3]}
vector = torch.eye(16)[1] if dependent else torch.eye(16)[:3].sum(0) / 3**0.5
pipeline._expert_directions = {0: {0: vector}}
pipeline._validate_concept_architecture()
with torch.no_grad():
pipeline._excise_inner(model.model.layers, model.config.model_type,
model.config, 2, time.time(), {})
expected = identity.expand(3, -1, -1).clone()
expected[:, :3] = 0
biases[:, :3] = 0
if dependent:
expected[0, 0, 0] = 1
if norm_preserve:
for expert in expected:
expert.mul_(min(1.1, 4 / expert.float().norm().item()))
actual_biases = (ffn.experts.down_proj_bias if fused else
torch.stack([expert.down_proj.bias for expert in ffn.experts]))
tolerance = 0.02 if dtype == torch.bfloat16 else 1e-5
torch.testing.assert_close(_weights(ffn), expected, atol=tolerance, rtol=0)
torch.testing.assert_close(actual_biases, biases, atol=tolerance, rtol=0)
torch.testing.assert_close(vector @ _weights(ffn)[0].float(), torch.zeros(16),
atol=tolerance, rtol=0)
shared = torch.ones_like(ffn.shared_expert.down_proj.weight)
shared[:3] = 0
if norm_preserve:
shared.mul_(min(1.1, 16 / shared.float().norm().item()))
torch.testing.assert_close(ffn.shared_expert.down_proj.weight, shared)
@pytest.mark.parametrize("name", ["down_proj", "w2", "gate_up_proj"])
@pytest.mark.parametrize("offloaded", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
def test_fused_multidirection_norms_restore_each_expert(name, offloaded, dtype):
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
from obliteratus.abliterate import AbliterationPipeline
layer = nn.Module()
layer.experts = nn.Module()
pristine = torch.eye(16, dtype=dtype).expand(3, -1, -1).clone()
pristine[1, 0, 0] = 0.5
pristine[2] = 0
setattr(layer.experts, name, nn.Parameter(pristine.clone()))
if offloaded:
add_hook_to_module(layer.experts, AlignDevicesHook(execution_device="cpu", offload=True))
identity = getattr(layer.experts, name)
assert AbliterationPipeline._capture_layer_weight_norms(layer) == {}
saved = AbliterationPipeline._capture_layer_weight_norms(layer, include_fused=True)
assert saved[f"experts.{name}"] == pytest.approx([4., 15.25**0.5, 0.])
assert AbliterationPipeline._capture_layer_weight_norms(
layer, parameter_names=frozenset(), include_fused=True,
) == {}
with torch.no_grad():
for direction in torch.eye(16)[:2]:
# Callers pass (hidden, 1) column directions.
AbliterationPipeline._project_fused_3d(
layer.experts, direction.unsqueeze(-1), [name], norm_preserve=False, scale=1.,
offload_roots=(layer,),
)
AbliterationPipeline._restore_layer_weight_norms(layer, saved)
result = resolve_logical_parameter(layer.experts, name).tensor
expected = pristine.clone()
expected[:, :, :2] = 0
expected[0].mul_(4 / 14**0.5)
expected[1].mul_((15.25 / 14)**0.5)
torch.testing.assert_close(result, expected)
torch.testing.assert_close(result.float().norm(dim=(1, 2)), pristine.float().norm(dim=(1, 2)),
atol=0.02 if dtype == torch.bfloat16 else 0.002, rtol=0)
assert getattr(layer.experts, name) is identity
assert identity.device.type == ("meta" if offloaded else "cpu")
def test_fused_norm_restoration_preserves_ties_and_unselected_parameters():
from obliteratus.abliterate import AbliterationPipeline
layer = nn.Module()
layer.experts = nn.Module()
layer.alias = nn.Module()
layer.experts.down_proj = nn.Parameter(torch.ones(2, 16, 16))
layer.alias.down_proj = layer.experts.down_proj
layer.experts.gate_up_proj = nn.Parameter(torch.ones(2, 32, 16))
layer.experts.down_proj_bias = nn.Parameter(torch.ones(2, 16))
saved = AbliterationPipeline._capture_layer_weight_norms(
layer, parameter_names=frozenset({"experts.down_proj", "alias.down_proj"}),
include_fused=True,
)
assert saved == {"experts.down_proj": [16., 16.]}
with torch.no_grad():
layer.experts.down_proj.mul_(0.5)
AbliterationPipeline._restore_layer_weight_norms(layer, saved)
# Apply the 1.1 cap once even when two modules share the same parameter.
torch.testing.assert_close(layer.experts.down_proj, torch.full((2, 16, 16), 0.55))
assert layer.alias.down_proj is layer.experts.down_proj
assert torch.equal(layer.experts.gate_up_proj, torch.ones(2, 32, 16))
assert torch.equal(layer.experts.down_proj_bias, torch.ones(2, 16))
def test_fused_norm_restoration_preflights_before_any_weight_change():
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.analysis.numerical_contracts import NonfiniteWeightError
layer = nn.Module()
layer.dense = nn.Linear(1, 1, bias=False, dtype=torch.float16)
layer.experts = nn.Module()
layer.experts.down_proj = nn.Parameter(torch.tensor([[[1.]], [[65000.]]], dtype=torch.float16))
pristine = {name: value.detach().clone() for name, value in layer.state_dict().items()}
saved = {"dense.weight": 1., "experts.down_proj": [2., 72000.]}
with pytest.raises(NonfiniteWeightError):
AbliterationPipeline._restore_layer_weight_norms(layer, saved)
for name, value in layer.state_dict().items():
assert torch.equal(value, pristine[name])
def test_module_list_repair_keeps_expert_targets(model, direction, monkeypatch):
from obliteratus.concepts import RepairConfig
from obliteratus.concepts.repair_model import WeightRepair
_as_module_list(model.model.layers[0].mlp)
backend = WeightRepair(model, direction)
expert_name = "model.layers.0.mlp.experts.0.down_proj.weight"
fallback_name = "model.layers.0.mlp.experts.1.down_proj.weight"
assert torch.equal(backend.vectors[expert_name], direction.expert_directions[0][0])
assert torch.equal(backend.vectors[fallback_name], direction.directions[0])
assert WeightRepair.capture_host_bytes(model, direction) >= sum(
weight.numel() * weight.element_size() for weight in backend.pristine.values()
)
apply_concept_intervention(model, direction, "orthogonalize")
with torch.no_grad():
for weight in backend.weights.values():
weight.mul_(0.5)
backend.bind()
config = RepairConfig(lora_rank=16)
inputs = torch.eye(24)
writer = model.model.layers[0].mlp.experts[0].down_proj
before = writer(inputs).detach().clone()
with next(backend("lora_repair", config)):
expected = backend.pristine[expert_name].T.clone()
expected[:, 1] = 0
torch.testing.assert_close(writer(inputs), expected, atol=1e-6, rtol=1e-5)
assert torch.equal(writer(inputs), before)
assert not writer._forward_hooks
monkeypatch.setattr(backend, "_refined_vectors", lambda config: {0: torch.eye(16)[2]})
with next(backend("rdo_refinement", config)):
assert torch.count_nonzero(writer.weight[1]) == 0
assert torch.count_nonzero(model.model.layers[0].mlp.experts[1].down_proj.weight[2]) == 0
assert torch.count_nonzero(model.model.layers[0].self_attn.o_proj.weight[2]) == 0
assert torch.equal(writer(inputs), before)
def test_evaluation_includes_expert_target_in_repair_acceptance(model, direction):
from obliteratus.concepts import RepairConfig
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.concepts.repair import rejection_reasons
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
from tests.test_concept_repair import report
tokenizer = _fixture_tokenizer()
tokenizer.model_input_names = ["input_ids", "attention_mask"]
evaluator = ModelEvaluator(model, tokenizer, direction)
evaluator.prompts = ["hello world"]
def restore_expert(module, args, output):
hidden = output[0] if isinstance(output, tuple) else output
hidden = hidden.clone()
hidden[..., 0] = 0
hidden[..., 1] = 3
return (hidden, *output[1:]) if isinstance(output, tuple) else hidden
hook = model.model.layers[0].register_forward_hook(restore_expert)
try:
readings = evaluator.measure()["projections"]
finally:
hook.remove()
assert readings == {"0": 0.0, "0:expert:0": 3.0}
original, candidate = report(), report(1)
original.mechanistic["before"]["0:expert:0"] = 3.0
candidate.mechanistic["after"] = readings
assert "target projection reintroduced or unmeasured" in rejection_reasons(
candidate, original, RepairConfig(),
)
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("entry", ["preload", "admission", "excise"])
def test_pipeline_moe_lora_rejected_before_mutation(model, tmp_path, entry):
pipeline = _pipeline(model, tmp_path, use_lora_ablation=True)
original = {name: value.clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError, match="Pipeline LoRA does not support non-refusal MoE"):
if entry == "preload":
pipeline._validate_concept_architecture(model.config)
elif entry == "admission":
pipeline._excise_parameter_names()
else:
pipeline._excise_inner(model.model.layers, model.config.model_type,
model.config, 2, time.time(), {})
assert all(torch.equal(value, original[name]) for name, value in model.state_dict().items())
@pytest.mark.parametrize("target", ["attention", "ffn", "output", "all"])
@pytest.mark.parametrize("project_router", [False, True])
def test_pipeline_router_is_independent_of_projection_target(model, tmp_path, target, project_router):
pipeline = _pipeline(model, tmp_path, projection_target=target, project_router=project_router,
regularization=0.5)
pipeline._validate_concept_architecture()
ffn = model.model.layers[0].mlp
router = ffn.gate.weight.detach().clone()
experts = _weights(ffn).clone()
names = pipeline._excise_parameter_names(ordinary_only=True)
if project_router:
assert "model.layers.0.mlp.gate.weight" in names
with torch.no_grad():
pipeline._excise_inner(model.model.layers, model.config.model_type,
model.config, 2, time.time(), {})
if project_router:
router[:, 0] *= 0.5
torch.testing.assert_close(ffn.gate.weight, router)
if target != "attention":
experts[0, 1] *= 0.5
experts[1:, 0] *= 0.5
torch.testing.assert_close(_weights(ffn), experts)
@pytest.mark.parametrize("project_router", [False, True])
def test_portable_attention_only_router_edit_preserves_experts(model, direction, project_router):
ffn = model.model.layers[0].mlp
experts = _weights(ffn).clone()
router = ffn.gate.weight.detach().clone()
apply_concept_intervention(model, direction, "orthogonalize",
projection_target="attention", project_router=project_router)
if project_router:
router[:, 0] = 0
assert torch.equal(ffn.gate.weight, router)
assert torch.equal(_weights(ffn), experts)
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
def test_shared_attention_does_not_require_expert_layout(model, direction, mode):
direction.expert_directions.clear()
del model.model.layers[0].mlp.gate
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
writer = model.model.layers[0].self_attn.o_proj
inputs = torch.arange(48, dtype=torch.float32).reshape(1, 3, 16) / 48
expected = writer(inputs).detach().clone()
expected[..., 0] = 0
run = apply_concept_intervention(model, direction, mode, projection_target="attention")
try:
torch.testing.assert_close(writer(inputs), expected, atol=1e-7, rtol=1e-5)
for name, value in model.state_dict().items():
original = pristine[name].clone()
if mode == "orthogonalize" and name == "model.layers.0.self_attn.o_proj.weight":
original[0] = 0
assert torch.equal(value, original)
finally:
if mode == "lora":
run.remove()
assert all(not module._forward_hooks for module in model.modules())
def _mixtral_model(num_layers=1):
from transformers import MixtralConfig, MixtralForCausalLM
config = MixtralConfig(
vocab_size=11, hidden_size=16, intermediate_size=24, num_hidden_layers=num_layers,
num_attention_heads=2, num_key_value_heads=2, num_local_experts=3,
num_experts_per_tok=2, max_position_embeddings=128,
pad_token_id=0, eos_token_id=1, bos_token_id=1,
)
with torch.random.fork_rng():
torch.manual_seed(310)
return MixtralForCausalLM(config).eval()
@pytest.mark.parametrize("num_layers", [1, 2])
@pytest.mark.parametrize("concept_id", ["refusal", "test-concept"])
@pytest.mark.parametrize("mode,positions", [
("orthogonalize", "all_tokens"), ("lora", "all_tokens"), ("lora", "final_token"),
])
def test_mixtral_shared_attention_interventions(num_layers, concept_id, mode, positions):
model = _mixtral_model(num_layers)
direction = ConceptDirection(
{0: torch.eye(16)[0]}, concept_id=concept_id,
validation={"concept_id": concept_id, "pack_hash": None, "verdict": "pass"},
)
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
writers = [layer.self_attn.o_proj for layer in model.model.layers]
inputs = torch.arange(48, dtype=torch.float32).reshape(1, 3, 16) / 48
before = [writer(inputs).detach().clone() for writer in writers]
run = apply_concept_intervention(
model, direction, mode, layers="all", positions=positions, projection_target="attention",
)
expected_names = [f"model.layers.{index}.self_attn.o_proj.weight" for index in range(num_layers)]
try:
assert run.edited_parameters == expected_names
for writer, output in zip(writers, before):
expected = output.clone()
if positions == "final_token":
expected[:, -1, 0] = 0
else:
expected[..., 0] = 0
torch.testing.assert_close(writer(inputs), expected, atol=1e-7, rtol=1e-5)
for name, value in model.state_dict().items():
expected = pristine[name].clone()
if mode == "orthogonalize" and name in expected_names:
expected[0] = 0
assert torch.equal(value, expected)
finally:
if mode == "lora":
run.remove()
if mode == "lora":
for writer, output in zip(writers, before):
assert torch.equal(writer(inputs), output)
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("option", ["expert_directions", "project_router"])
def test_mixtral_expert_options_fail_before_shared_attention_edits(option):
model = _mixtral_model(2)
direction = ConceptDirection(
{0: torch.eye(16)[0]},
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
)
if option == "expert_directions":
direction.expert_directions = {0: {0: torch.eye(16)[1]}}
pristine = {name: value.detach().clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError, match="Unsupported MoE concept layout"):
apply_concept_intervention(
model, direction, "orthogonalize", projection_target="attention",
project_router=option == "project_router",
)
assert all(torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("mode", ["steer", "project"])
def test_mixtral_residual_intervention_and_evaluation(mode, tmp_path):
from obliteratus.concepts.evaluation import EvaluationBattery, evaluate_intervention
from obliteratus.concepts.evaluation_model import ModelEvaluator
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
model = _mixtral_model()
tokenizer = _fixture_tokenizer()
tokenizer.model_input_names = ["input_ids", "attention_mask"]
direction = ConceptDirection(
{0: torch.eye(16)[0]},
validation={"concept_id": "refusal", "pack_hash": None, "verdict": "pass"},
)
original = {name: value.clone() for name, value in model.state_dict().items()}
evaluator = ModelEvaluator(model, tokenizer, direction)
evaluator.prompts = ["hello world"]
assert evaluator.moe_layouts == {}
before = evaluator.measure()
assert "routing" not in before
battery = EvaluationBattery(evaluator.generate, measure=evaluator.measure,
refusal_size=2, max_new_tokens=2)
with evaluate_intervention(model, direction, mode, battery, output_dir=tmp_path) as run:
assert run.manager is not None
assert run.evaluation.mechanistic["before"]
assert "routing_drift" not in run.evaluation.collateral
if mode == "project":
assert evaluator.measure()["projections"]["0"] == 0
assert (tmp_path / "evaluation.json").exists()
assert all(torch.equal(value, original[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())