Files
OBLITERATUS/tests/test_concept_moe.py
Joseph Magly e795df746b Merge remote-tracking branch 'upstream/main' into feat/331-pinned-extraction-layer
# Conflicts:
#	README.md
#	docs/CONCEPT_PACKS.md
#	obliteratus/cli.py
#	obliteratus/concepts/cli.py
#	obliteratus/concepts/direction.py
#	obliteratus/concepts/geometry.py
#	obliteratus/concepts/intervention.py
#	obliteratus/concepts/target.py
#	tests/fixtures/refusal_golden.md
2026-10-17 19:08:09 -04:00

538 lines
25 KiB
Python

"""CPU-only routing-conditioned concept extraction and MoE gate contracts."""
import json
from dataclasses import replace
import pytest
import torch
from torch import nn
from transformers import (
OlmoeConfig,
OlmoeForCausalLM,
Qwen2MoeConfig,
Qwen2MoeForCausalLM,
Qwen3MoeConfig,
Qwen3MoeForCausalLM,
)
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.concepts import ConceptDirection, ConceptTarget, EntryPoint, ExtractionRecipe
from obliteratus.concepts.evaluation import EvaluationBattery
from obliteratus.concepts.gate import ActivationCache, DEFAULT_THRESHOLDS, validate_direction
from obliteratus.concepts.moe import (
extract_expert_directions,
routing_drift,
routing_probabilities,
routing_readout,
validate_experts,
validate_moe_model,
)
from obliteratus.models.loader import ModelHandle
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
def tiny_moe(family):
config_cls, model_cls = {
"olmoe": (OlmoeConfig, OlmoeForCausalLM),
"qwen2_moe": (Qwen2MoeConfig, Qwen2MoeForCausalLM),
"qwen3_moe": (Qwen3MoeConfig, Qwen3MoeForCausalLM),
}[family]
options = dict(
vocab_size=11, hidden_size=16, intermediate_size=32,
num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2,
num_experts=4, num_experts_per_tok=2, max_position_embeddings=128,
pad_token_id=0, bos_token_id=1, eos_token_id=1,
)
if family != "olmoe":
options.update(moe_intermediate_size=16)
if family == "qwen2_moe":
options.update(shared_expert_intermediate_size=16)
with torch.random.fork_rng():
torch.manual_seed(310)
model = model_cls(config_cls(**options)).eval()
return model
@pytest.mark.parametrize("family", ["olmoe", "qwen2_moe", "qwen3_moe"])
def test_supported_random_init_architectures_capture_real_router_outputs(family):
model = tiny_moe(family)
blocks = validate_moe_model(model, model.model.layers)
assert set(blocks) == {0, 1}
captured = {}
hooks = [router.register_forward_hook(
lambda module, inputs, output, index=index: captured.__setitem__(index, output),
) for index, (_, router, _, _) in blocks.items()]
mask = torch.tensor([[0, 1, 1], [1, 1, 1]])
try:
with torch.no_grad():
outputs = model(input_ids=torch.tensor([[0, 3, 5], [4, 5, 6]]), attention_mask=mask)
finally:
for hook in hooks:
hook.remove()
assert torch.isfinite(outputs.logits).all()
for index, (_, _, count, top_k) in blocks.items():
probabilities, choices = routing_probabilities(captured[index], mask, count, top_k)
assert probabilities.shape == choices.shape == (2, 3, 4)
torch.testing.assert_close(probabilities.sum(-1), torch.ones(2, 3))
torch.testing.assert_close(choices.sum(-1), torch.full((2, 3), 2.))
@pytest.mark.parametrize("failure", ["family", "router", "experts", "top_k", "missing"])
def test_unsupported_layouts_fail_before_any_edit(failure):
model = tiny_moe("olmoe")
if failure == "family":
model.config.model_type = "mixtral"
elif failure == "router":
model.model.layers[0].mlp.gate = nn.Linear(16, 3, bias=False)
elif failure == "experts":
model.model.layers[0].mlp.experts = nn.ModuleList([nn.Linear(16, 16)])
elif failure == "top_k":
model.config.num_experts_per_tok = 5
else:
for layer in model.model.layers:
layer.mlp = nn.ModuleDict({"down_proj": nn.Linear(32, 16)})
with pytest.raises(ValueError, match="Unsupported|no supported"):
validate_moe_model(model, model.model.layers)
def test_module_list_layout_and_dense_layers_are_supported():
model = tiny_moe("olmoe")
model.model.layers[0].mlp.experts = nn.ModuleList([
nn.ModuleDict({"down_proj": nn.Linear(32, 16, bias=False)}) for _ in range(4)
])
model.model.layers[1].mlp = nn.ModuleDict({"down_proj": nn.Linear(32, 16)})
assert set(validate_moe_model(model, model.model.layers)) == {0}
@pytest.mark.parametrize("tuple_output", [False, True])
@pytest.mark.parametrize("position,expected", [
("final_token", [[1., 0., 0.], [0., 0., 1.]]),
("mean_pool", [[.5, .5, 0.], [0., .5, .5]]),
("span:answer", [[0., 1., 0.], [0., 0., 1.]]),
])
def test_routing_readouts_use_real_tokens_and_configured_span(tuple_output, position, expected):
mask = torch.tensor([[0, 1, 1], [1, 1, 0]])
indices = torch.tensor([[[2], [1], [0]], [[1], [2], [0]]])
logits = torch.zeros(2, 3, 3).scatter_(-1, indices, 10.)
output = (logits.flatten(0, 1), torch.ones(6, 1), indices.flatten(0, 1)) if tuple_output else logits
span = torch.tensor([[0, 1, 0], [0, 1, 0]])
actual = routing_readout(output, mask, 3, 1, position, span_mask=span)
torch.testing.assert_close(actual, torch.tensor(expected))
def test_tuple_router_choices_are_authoritative_and_logits_are_not_pooled():
logits = torch.tensor([[9., 0., 0.], [0., 1., 0.]])
selected = torch.tensor([[2], [2]])
probabilities, choices = routing_probabilities(
(logits, torch.ones(2, 1), selected), torch.ones(1, 2), 3, 1,
)
torch.testing.assert_close(probabilities[0], logits.softmax(-1))
torch.testing.assert_close(choices[0], torch.tensor([[0., 0., 1.], [0., 0., 1.]]))
@pytest.mark.parametrize("output", [
torch.zeros(2, 2), torch.zeros(3, 3), torch.full((2, 3), float("nan")),
(torch.zeros(2, 3),),
(torch.zeros(2, 3), torch.ones(2, 1), torch.tensor([[3], [0]])),
(torch.zeros(2, 3), torch.ones(2, 1), torch.zeros(2, 1)),
])
def test_malformed_router_output_rejected(output):
with pytest.raises(ValueError, match="MoE"):
routing_probabilities(output, torch.ones(1, 2), 3, 1)
def planted_experts(*, narrow=False, indistinguishable=False):
count = 40
positive = torch.zeros(count, 4)
negative = torch.zeros(count, 4)
positive[:20, 0], negative[:20, 0] = 2., -2.
positive[20:, 1], negative[20:, 1] = 2., -2.
loads = torch.zeros(count, 3)
loads[:20, 0], loads[20:, 1] = 1., 1.
if narrow:
loads[:, 0], loads[:, 1] = 1., 0.
positive[:, 0], negative[:, 0] = 2., -2.
controls = {"neutral": {0: negative}, "neighbour": {0: negative.clone()}}
if indistinguishable:
controls["neighbour"][0] = positive.clone()
routing = {name: {0: loads.clone()} for name in ("positive", "control:neutral", "control:neighbour")}
recipe = ExtractionRecipe(layer_select={"strategy": "preset", "folds": 2})
experts = extract_expert_directions({0: positive}, controls, routing, recipe)
direction = ConceptDirection(
{0: torch.tensor([1., 1., 0., 0.]) / 2**.5}, expert_directions=experts,
recipe=recipe, seed=310, extraction_layers=[0], intervention_layers=[0],
)
cache = ActivationCache({0: positive}, controls, routing=routing)
return direction, cache
def test_extraction_conditions_on_fired_experts_and_skips_unobserved():
direction, cache = planted_experts()
assert set(direction.expert_directions[0]) == {0, 1}
torch.testing.assert_close(direction.expert_directions[0][0], torch.tensor([1., 0., 0., 0.]))
torch.testing.assert_close(direction.expert_directions[0][1], torch.tensor([0., 1., 0., 0.]))
cache.routing["control:neighbour"][0][:, 1] = 0
experts = extract_expert_directions(cache.positive, cache.controls, cache.routing, direction.recipe)
assert set(experts[0]) == {0}
@pytest.mark.parametrize("narrow", [False, True])
def test_expert_gate_accepts_covered_narrow_and_broad_concepts(narrow):
direction, cache = planted_experts(narrow=narrow)
metrics, reasons = validate_experts(direction, cache, DEFAULT_THRESHOLDS)
assert reasons == []
assert metrics["0"]["expert_direction_coverage"] == 1.
assert metrics["0"]["effective_positive_experts"] == pytest.approx(1. if narrow else 2.)
assert metrics["0"]["experts"]["0"]["specificity"] == {"neutral": 1., "neighbour": 1.}
assert metrics["0"]["experts"]["2"]["shared_fallback"]
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float64])
@pytest.mark.parametrize("mixed_controls", [False, True])
@pytest.mark.parametrize("indistinguishable", [False, True])
def test_expert_gate_normalizes_cached_activation_dtypes(dtype, mixed_controls, indistinguishable):
direction, cache = planted_experts(indistinguishable=indistinguishable)
expected = validate_direction(direction, cache, mode="report", random_directions=8)
cache.positive = {layer: values.to(dtype) for layer, values in cache.positive.items()}
cache.controls = {
name: {layer: values.to(torch.float32 if mixed_controls and name == "neutral" else dtype)
for layer, values in group.items()}
for name, group in cache.controls.items()
}
original = {name: {layer: values.clone() for layer, values in group.items()}
for name, group in {"positive": cache.positive, **cache.controls}.items()}
actual = validate_direction(direction, cache, mode="report", random_directions=8)
assert actual["verdict"] == expected["verdict"]
assert actual["moe"] == expected["moe"]
assert actual["reasons"] == expected["reasons"]
metrics, reasons = validate_experts(direction, cache, DEFAULT_THRESHOLDS)
assert metrics == expected["moe"]
assert bool(reasons) == indistinguishable
for name, group in {"positive": cache.positive, **cache.controls}.items():
for layer, values in group.items():
torch.testing.assert_close(values, original[name][layer])
def test_expert_gate_reports_missing_coverage_and_failed_named_control():
direction, cache = planted_experts(indistinguishable=True)
del direction.expert_directions[0][1]
metrics, reasons = validate_experts(direction, cache, DEFAULT_THRESHOLDS)
assert metrics["0"]["expert_direction_coverage"] == .5
assert metrics["0"]["experts"]["0"]["specificity"]["neighbour"] == .5
assert {reason["check"] for reason in reasons} == {"expert_validation", "expert_coverage"}
assert any("Expert 0" in reason["message"] for reason in reasons)
def test_expert_gate_rejects_supplied_vector_with_no_concept_signal():
direction, cache = planted_experts()
direction.expert_directions[0][0] = torch.tensor([0., 0., 1., 0.])
_, reasons = validate_experts(direction, cache, DEFAULT_THRESHOLDS)
assert any(reason["check"] == "expert_validation" and "Expert 0" in reason["message"]
for reason in reasons)
def test_expert_gate_grouped_folds_and_unselected_layers():
direction, cache = planted_experts()
cache.sample_groups = {name: [f"pair-{i // 2}" for i in range(40)] for name in cache.routing}
metrics, reasons = validate_experts(direction, cache, DEFAULT_THRESHOLDS)
assert not reasons
assert metrics["0"]["experts"]["0"]["heldout_auc"] == 1.
direction.expert_directions = {}
direction.intervention_layers = [1]
assert validate_experts(direction, cache, DEFAULT_THRESHOLDS)[1] == []
@pytest.mark.parametrize("failure", ["control", "layers", "rows", "nonfinite", "range"])
def test_expert_gate_rejects_misaligned_routing(failure):
direction, cache = planted_experts()
if failure == "control":
del cache.routing["control:neutral"]
elif failure == "layers":
cache.routing["control:neutral"] = {}
elif failure == "rows":
cache.routing["control:neutral"][0] = torch.ones(2, 3)
else:
cache.routing["control:neutral"][0][0, 0] = float("nan") if failure == "nonfinite" else 2.
with pytest.raises(ValueError, match="routing"):
validate_experts(direction, cache, DEFAULT_THRESHOLDS)
def test_routing_drift_uses_token_kl_and_preserves_expert_loads():
before = {0: {"probabilities": torch.tensor([[.8, .2], [.2, .8]]), "choices": torch.eye(2)}}
identity = routing_drift(before, before)["0"]
assert identity["expert_choice_kl"] == identity["changed_choice_fraction"] == 0.
assert identity["pristine_expert_load"] == [.5, .5]
after = {0: {"probabilities": before[0]["probabilities"].flip(0), "choices": torch.eye(2).flip(0)}}
changed = routing_drift(before, after)["0"]
assert changed["tokens"] == 2
assert changed["expert_choice_kl"] == pytest.approx(.6 * torch.log(torch.tensor(4.)).item())
assert changed["changed_choice_fraction"] == 1.
assert changed["edited_expert_load"] == [.5, .5]
def test_routing_drift_rejects_missing_or_misaligned_reference():
before = {0: {"probabilities": torch.tensor([[.8, .2]]), "choices": torch.tensor([[1., 0.]])}}
with pytest.raises(ValueError, match="layers"):
routing_drift(before, {})
with pytest.raises(ValueError, match="misaligned"):
routing_drift(before, {0: {"probabilities": torch.ones(2, 2), "choices": torch.ones(2, 2)}})
def test_expert_direction_artifact_roundtrip(tmp_path):
direction, _ = planted_experts()
direction.routing = {
"readout": "routing-conditioned residual", "project_router": False,
"layers": {"0": {"experts": 3, "top_k": 1}},
"expert_load": {"positive": {"0": [.5, .5, 0.]}},
}
path = tmp_path / "experts.safetensors"
direction.save(path)
restored = ConceptDirection.load(path)
assert restored.routing == direction.routing
assert set(restored.expert_directions[0]) == {0, 1}
for expert in direction.expert_directions[0]:
torch.testing.assert_close(restored.expert_directions[0][expert], direction.expert_directions[0][expert])
assert json.loads(path.with_suffix(".json").read_text())["concept_id"] == direction.concept_id
def test_expert_artifact_preserves_shared_affine_geometry_and_hashes(tmp_path):
direction, _ = planted_experts()
direction.subspaces = {0: torch.eye(4)[:2]}
direction.reference_means = {0: torch.arange(4, dtype=torch.float64)}
direction.reference_provenance = {"source": "shared controls", "count": 40}
direction.projector_left = {0: torch.eye(4)[:, :2]}
direction.projector_right = {0: torch.eye(4)[:, :2]}
direction.routing = {"layers": {"0": {"experts": 3, "top_k": 1}}}
path = tmp_path / "combined.safetensors"
direction.save(path)
restored = ConceptDirection.load(path)
for name in direction._tensor_fields():
for layer, tensor in getattr(direction, name).items():
assert torch.equal(getattr(restored, name)[layer], tensor)
for expert, tensor in direction.expert_directions[0].items():
assert torch.equal(restored.expert_directions[0][expert], tensor)
assert restored.routing == direction.routing
assert restored.reference_provenance == direction.reference_provenance
assert restored.reference_mean_hashes() == direction.reference_mean_hashes()
metadata = json.loads(path.with_suffix(".json").read_text())
metadata["reference_mean_sha256"]["0"] = "wrong"
metadata.pop("metadata_sha256")
metadata["metadata_sha256"] = direction._metadata_hash(metadata)
path.with_suffix(".json").write_text(json.dumps(metadata))
with pytest.raises(ValueError, match="reference mean hash mismatch"):
ConceptDirection.load(path)
@pytest.mark.parametrize("scale", ["raw", "vector"])
def test_pinned_expert_artifact_preserves_recipe_and_legacy_defaults(tmp_path, scale):
direction, _ = planted_experts()
direction.model_id = "fixture/moe"
direction.extraction_layer_source = "pinned"
direction.recipe = replace(
direction.recipe, extraction_layer={direction.model_id: 0}, steering_scale=scale,
layer_select={"strategy": "cv_auc", "folds": 2, "group_by": "item"},
)
direction.routing = {"layers": {"0": {"experts": 3, "top_k": 1}}}
path = tmp_path / "pinned-experts.safetensors"
direction.save(path)
restored = ConceptDirection.load(path)
assert restored.recipe == direction.recipe
assert restored.recipe.group_by == "item"
assert restored.extraction_layer_source == "pinned"
assert restored.extraction_layers == [0]
assert restored.routing == direction.routing
for expert, vector in direction.expert_directions[0].items():
assert torch.equal(restored.expert_directions[0][expert], vector)
sidecar = path.with_suffix(".json")
metadata = json.loads(sidecar.read_text())
metadata["extraction_layer_source"] = "selected"
sidecar.write_text(json.dumps(metadata))
with pytest.raises(ValueError, match="metadata hash mismatch"):
ConceptDirection.load(path)
del metadata["extraction_layer_source"]
metadata["recipe"].pop("extraction_layer")
metadata.pop("metadata_sha256")
metadata["metadata_sha256"] = direction._metadata_hash(metadata)
sidecar.write_text(json.dumps(metadata))
legacy = ConceptDirection.load(path)
assert legacy.extraction_layer_source == "selected"
assert legacy.recipe.extraction_layer is None
assert legacy.recipe.steering_scale == scale
assert legacy.routing == direction.routing
@pytest.mark.parametrize("experts", [
{0: {-1: torch.ones(4)}}, {0: {0: torch.ones(3)}},
{0: {0: torch.full((4,), float("nan"))}}, {2: {0: torch.ones(4)}},
])
def test_invalid_expert_direction_artifacts_rejected(experts):
with pytest.raises(ValueError):
ConceptDirection({0: torch.ones(4)}, expert_directions=experts)
@pytest.mark.parametrize("family,entry_point", [
("olmoe", False), ("qwen2_moe", False), ("qwen3_moe", False), ("olmoe", True),
])
def test_pipeline_preserves_named_control_routing_through_extraction(family, entry_point, tmp_path, monkeypatch):
model = tiny_moe(family)
tokenizer = _fixture_tokenizer()
tokenizer.model_input_names = ["input_ids", "attention_mask"]
target = ConceptTarget(
id="user/moe", positive=["harmful hello", "harmful world", "harmful request", "harmful test"],
controls={"neutral": ["harmless hello", "harmless world", "harmless request"],
"other": ["safe hello", "safe world"]},
recipe=(ExtractionRecipe(entry_point=EntryPoint(readout="mean_pool", template="{text} answer"))
if entry_point else ExtractionRecipe(readout={"position": "mean_pool", "suffix": " answer"})),
)
pipeline = AbliterationPipeline(
"offline", concept=target, method="basic", device="cpu", dtype="float32",
output_dir=str(tmp_path), max_seq_length=16, gate_mode="report", seed=310,
)
pipeline.handle = ModelHandle(model, tokenizer, model.config, "offline", "causal_lm")
monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None)
monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.)
pipeline._validate_concept_architecture()
assert pipeline.per_expert_directions
pipeline._probe_impl()
pipeline._distill_concept()
direction = pipeline.concept_direction
assert direction.sample_counts == {"positive": 4, "control:neutral": 3, "control:other": 2}
assert direction.routing["layers"] == {
"0": {"experts": 4, "top_k": 2}, "1": {"experts": 4, "top_k": 2},
}
assert direction.routing["project_router"] is False
cache = pipeline._concept_gate_inputs().materialize()
cache.routing = pipeline._concept_routing_cache()
assert set(cache.routing) == {"positive", "control:neutral", "control:other"}
for name, size in direction.sample_counts.items():
for layer in (0, 1):
assert cache.routing[name][layer].shape == (size, 4)
torch.testing.assert_close(cache.routing[name][layer].sum(-1), torch.full((size,), 2.))
torch.testing.assert_close(
cache.routing[name][layer].mean(0),
torch.tensor(direction.routing["expert_load"][name][str(layer)]),
)
for layer, experts in direction.expert_directions.items():
for expert, vector in experts.items():
assert vector.shape == (16,) and torch.isfinite(vector).all()
assert cache.routing["positive"][layer][:, expert].sum() > 0
path = tmp_path / "direction.safetensors"
direction.save(path)
assert ConceptDirection.load(path).routing == direction.routing
if entry_point:
observations = direction.activation_identity["special_token_observations"]
assert {row["label"] for row in observations} == {"harmful", "harmless"}
assert sum(row["prompt_count"] for row in observations) == 9
metadata = json.loads(json.dumps(pipeline._build_metadata()))["concept"]
assert metadata["activation_identity"] == direction.activation_identity
assert metadata["routing"] == direction.routing
assert ConceptDirection.load(path).activation_identity == direction.activation_identity
pipeline._validate_concept_gate_inner()
assert pipeline.validation_report["verdict"] == "fail"
assert set(pipeline.validation_report["moe"]) == {"0", "1"}
assert json.loads((tmp_path / "validation.json").read_text())["moe"]
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("family", ["olmoe", "qwen2_moe", "qwen3_moe"])
def test_model_evaluator_captures_token_aligned_pristine_routing(family):
from obliteratus.concepts.evaluation_model import ModelEvaluator
model = tiny_moe(family)
tokenizer = _fixture_tokenizer()
tokenizer.model_input_names = ["input_ids", "attention_mask"]
evaluator = ModelEvaluator(model, tokenizer, ConceptDirection({0: torch.eye(16)[0]}))
evaluator.prompts = ["harmful hello", "harmless hello world"]
before = evaluator.measure()
assert set(before["routing"]) == {"0", "1"}
assert before["routing"]["0"]["probabilities"].shape == (5, 4)
identity = routing_drift(before["routing"], evaluator.measure()["routing"])
assert all(row["expert_choice_kl"] == 0 for row in identity.values())
with torch.no_grad():
model.model.layers[0].mlp.gate.weight[0].add_(torch.arange(16) / 4)
changed = routing_drift(before["routing"], evaluator.measure()["routing"])
assert changed["0"]["expert_choice_kl"] > 0
assert all(not module._forward_hooks for module in model.modules())
def test_evaluation_battery_publishes_routing_drift(tmp_path):
pristine = {0: {"probabilities": torch.tensor([[.8, .2]]), "choices": torch.tensor([[1., 0.]])}}
edited = {0: {"probabilities": torch.tensor([[.2, .8]]), "choices": torch.tensor([[0., 1.]])}}
measurements = iter([
{"logits": torch.zeros(1, 1, 3), "routing": pristine},
{"logits": torch.zeros(1, 1, 3), "routing": edited},
])
battery = EvaluationBattery(
lambda prompts, decoding: ["I cannot help with that request."] * len(prompts),
measure=lambda: next(measurements), refusal_size=2,
).capture()
report = battery.finish(tmp_path)
assert report.collateral["routing_drift"]["0"]["expert_choice_kl"] > 0
assert report.collateral["routing_drift"]["0"]["changed_choice_fraction"] == 1.
saved = json.loads((tmp_path / "evaluation.json").read_text())
assert saved["collateral"]["routing_drift"] == report.collateral["routing_drift"]
def test_pipeline_operator_can_select_shared_only_expert_edits():
model = tiny_moe("olmoe")
pipeline = AbliterationPipeline(
"offline", concept=ConceptTarget(id="user/demo", positive=["a"], controls={"neutral": ["b"]}),
method="basic", per_expert_directions=False,
)
pipeline.handle = ModelHandle(model, _fixture_tokenizer(), model.config, "offline", "causal_lm")
pipeline._validate_concept_architecture()
assert pipeline._concept_moe_layouts
assert pipeline.per_expert_directions is False
def test_expert_gate_requires_every_declared_sparse_layer():
direction, cache = planted_experts()
direction.routing = {"layers": {"0": {"experts": 3}, "1": {"experts": 3}}}
with pytest.raises(ValueError, match="missing declared"):
validate_experts(direction, cache, DEFAULT_THRESHOLDS)
direction.routing = {"layers": {"0": {"experts": 4}}}
with pytest.raises(ValueError, match="expert count"):
validate_experts(direction, cache, DEFAULT_THRESHOLDS)
def test_moe_metadata_tampering_rejected(tmp_path):
direction, _ = planted_experts()
direction.routing = {"layers": {"0": {"experts": 3, "top_k": 1}}}
path = tmp_path / "direction.safetensors"
direction.save(path)
metadata = json.loads(path.with_suffix(".json").read_text())
metadata["routing"]["layers"]["0"]["experts"] = 4
path.with_suffix(".json").write_text(json.dumps(metadata))
with pytest.raises(ValueError, match="metadata hash"):
ConceptDirection.load(path)
@pytest.mark.parametrize("family", ["olmoe", "qwen2_moe", "qwen3_moe"])
def test_supported_moe_config_passes_preload_concept_check(family):
from types import SimpleNamespace
pipeline = AbliterationPipeline(
"offline", concept=ConceptTarget(id="user/demo", positive=["a"], controls={"neutral": ["b"]}),
method="basic",
)
pipeline._validate_concept_architecture(SimpleNamespace(
model_type=family, num_experts=4, num_experts_per_tok=2, hidden_size=16,
))
assert pipeline.handle is None
@pytest.mark.parametrize("flag", [False, True])
def test_cli_forwards_optional_router_edit(monkeypatch, flag):
from obliteratus import cli
calls = []
monkeypatch.setattr(cli, "_cmd_abliterate", calls.append)
cli.main(["obliterate", "offline", "--concept", "user/demo"] + (["--project-router"] if flag else []))
assert len(calls) == 1
assert calls[0].project_router is flag