mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Add F21: architecture adapters that record tensor semantics and staged support levels, MoE observation with explicit coverage, and transfer dossiers compared against within-model baselines. - Adapters record site meaning, block order, norm placement and module roles (attention, MLP, router, routed and shared experts) and check residual semantics on a real forward. Support is staged: observation, then reversible intervention, then persistent editing. Dense decoders support all three; sparse MoE decoders support observation and residual intervention and refuse persistent editing. - MoE observation reports per-expert routed tokens and rows with intervals; experts below the coverage floor are insufficient coverage with null effects, never no effect. Route and load changes, shared expert contributions and routing restoration are measured. - Transfer needs its own alignment-fit and test rows (refused on overlap), the same row budget for a direction fitted directly on the target, and must beat that baseline and random directions with bootstrap intervals. Tested and untested conditions are listed; experts listed as tested must be covered on the test rows; new modalities need their own targets and scoring tasks. - `obliteratus experiments transfer run` and `arch-profile`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
7.1 KiB
Python
146 lines
7.1 KiB
Python
"""Architecture adapters: tensor semantics, ordered capability support and clear refusals."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
from torch import nn
|
|
from transformers import BertConfig
|
|
|
|
from obliteratus.arch_adapters import (
|
|
CAPABILITIES, DENSE, MOE, Adapter, ArchitectureRefused, CapabilityRefused, Support,
|
|
adapter_for, bind, check_residual_semantics, residual_readout,
|
|
)
|
|
from obliteratus.analysis.activation_observer import observe_activations
|
|
from obliteratus.analysis.labeled_subspace import site_layer
|
|
from obliteratus.cli import main
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.strategies.utils import get_attention_module, get_ffn_module, get_layer_modules
|
|
from tests.fixtures.tiny_arch_models import tiny_gpt2, tiny_llama, tiny_mixtral, tiny_qwen2_moe
|
|
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
|
|
|
|
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
|
|
IDS = torch.tensor([[2, 3, 4, 5, 6, 7]])
|
|
|
|
|
|
@pytest.mark.parametrize("build", [tiny_gpt2, tiny_llama])
|
|
def test_dense_adapter_resolves_the_existing_modules_and_readout(build):
|
|
model = build()
|
|
bound = bind(model)
|
|
assert bound.adapter is DENSE
|
|
handle = ModelHandle(model=model, tokenizer=_fixture_tokenizer(), config=model.config,
|
|
model_name="tiny", task="causal_lm")
|
|
existing = get_layer_modules(handle)
|
|
assert bound.layers is existing
|
|
for layer, block in zip(existing, bound.blocks):
|
|
assert block["kind"] == "dense"
|
|
assert block["roles"]["mlp"] is get_ffn_module(layer, model.config.model_type)
|
|
assert block["roles"]["attention"] is get_attention_module(layer, model.config.model_type)
|
|
mask = torch.tensor([[1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1]])
|
|
ids = torch.cat([IDS, IDS])
|
|
with observe_activations({"layer.1": existing[1]}, attention_mask=mask, phase="prompt") as seen:
|
|
model(input_ids=ids, attention_mask=mask)
|
|
ours = residual_readout(bound, [{"input_ids": ids, "attention_mask": mask}], 1)
|
|
assert torch.equal(ours, seen["layer.1"].values.float())
|
|
mean = residual_readout(bound, [{"input_ids": ids, "attention_mask": mask}], 1, "mean")
|
|
assert mean.shape == ours.shape and not torch.equal(mean, ours)
|
|
with pytest.raises(ValueError, match="position"):
|
|
residual_readout(bound, [{"input_ids": ids}], 1, "first")
|
|
site = bound.site(1)
|
|
assert site_layer(site["site"]) == 1 and site["role"] == "residual_stream"
|
|
with pytest.raises(ArchitectureRefused, match="outside"):
|
|
bound.site(5)
|
|
|
|
|
|
@pytest.mark.parametrize("build", [tiny_gpt2, tiny_llama, tiny_mixtral, tiny_qwen2_moe])
|
|
def test_residual_site_meaning_and_norm_placement_hold_on_a_forward_pass(build):
|
|
bound = bind(build())
|
|
check = check_residual_semantics(bound, IDS)
|
|
assert check["passed"], check
|
|
assert "final_norm(layer.1)" in check["max_abs_error"]
|
|
profile = bound.profile(check)
|
|
assert profile["semantics_check"]["passed"]
|
|
assert profile["block"]["order"].index("attention_norm") < profile["block"]["order"].index("attention")
|
|
json.dumps(profile)
|
|
|
|
|
|
def test_moe_profile_records_roles_not_only_shapes():
|
|
bound = bind(tiny_qwen2_moe(mlp_only_layers=[0]))
|
|
profile = bound.profile()
|
|
assert profile["family"] == "sparse_moe_decoder"
|
|
dense, sparse = profile["layers"]
|
|
assert dense["kind"] == "dense" and "router" not in dense["roles"]
|
|
assert sparse["kind"] == "sparse" and sparse["num_experts"] == 4 and sparse["top_k"] == 2
|
|
assert sparse["shared_expert"] is True
|
|
roles = sparse["roles"]
|
|
assert "softmax(logits)" in roles["router"]["meaning"]
|
|
assert "top-k" in roles["routed_experts"]["meaning"]
|
|
assert "every token" in roles["shared_expert"]["meaning"]
|
|
assert "sigmoid" in roles["shared_expert_gate"]["meaning"]
|
|
assert "routed expert mixture" in roles["mlp"]["meaning"]
|
|
assert bound.sparse_layers() == [1]
|
|
with pytest.raises(CapabilityRefused, match="has no router"):
|
|
bound.role(0, "router")
|
|
mixtral = bind(tiny_mixtral()).profile()
|
|
assert all(layer["shared_expert"] is False for layer in mixtral["layers"])
|
|
support = {s["capability"]: s for s in profile["support"]}
|
|
assert support["persistent_editing"]["supported"] is False
|
|
assert profile["highest_supported"] == "reversible_intervention"
|
|
|
|
|
|
def test_unsupported_capabilities_and_scopes_refuse_clearly():
|
|
with pytest.raises(CapabilityRefused, match="persistent_editing.*not implemented"):
|
|
MOE.require("persistent_editing")
|
|
with pytest.raises(CapabilityRefused, match="routed_experts.*residual_stream"):
|
|
MOE.require("reversible_intervention", "routed_experts")
|
|
with pytest.raises(CapabilityRefused, match="unknown capability"):
|
|
DENSE.require("training")
|
|
assert MOE.require("observation", "router").implementation.endswith("moe_observation")
|
|
assert DENSE.require("persistent_editing", "residual_stream").supported
|
|
with pytest.raises(ArchitectureRefused, match="No architecture adapter.*'bert'"):
|
|
adapter_for(BertConfig(vocab_size=11, hidden_size=16, num_hidden_layers=1,
|
|
num_attention_heads=2, intermediate_size=32))
|
|
|
|
|
|
def test_capabilities_extend_in_order():
|
|
def make(*flags):
|
|
return Adapter("x", ("x",), {}, tuple(
|
|
Support(c, f, ("residual_stream",) if f else ()) for c, f in zip(CAPABILITIES, flags)))
|
|
assert make(True, True, False).highest_level() == "reversible_intervention"
|
|
assert make(False, False, False).highest_level() is None
|
|
with pytest.raises(ValueError, match="extend in order"):
|
|
make(True, False, True)
|
|
with pytest.raises(ValueError, match="must declare"):
|
|
Adapter("x", ("x",), {}, (Support("observation", True, ("a",)),))
|
|
|
|
|
|
def test_moe_layout_mismatches_refuse():
|
|
model = tiny_qwen2_moe()
|
|
model.model.layers[1].mlp.gate = nn.Linear(16, 3, bias=False)
|
|
with pytest.raises(ArchitectureRefused, match="router weight"):
|
|
bind(model)
|
|
model = tiny_qwen2_moe()
|
|
del model.model.layers[0].mlp.shared_expert_gate
|
|
with pytest.raises(ArchitectureRefused, match="shared expert without its gate"):
|
|
bind(model)
|
|
model = tiny_llama()
|
|
model.model.norm = None
|
|
with pytest.raises(ArchitectureRefused, match="Final norm"):
|
|
bind(model)
|
|
|
|
|
|
def test_arch_profile_cli(tmp_path, capsys):
|
|
path = tmp_path / "moe"
|
|
tiny_qwen2_moe().save_pretrained(path)
|
|
main(["experiments", "arch-profile", "--model", str(path)])
|
|
profile = json.loads(capsys.readouterr().out)
|
|
assert profile["schema"] == "obliteratus.architecture-profile"
|
|
assert profile["source"]["weights_loaded"] is False and profile["semantics_check"] is None
|
|
main(["experiments", "arch-profile", "--model", str(path), "--verify",
|
|
"--require", "reversible_intervention", "--scope", "residual_stream"])
|
|
assert json.loads(capsys.readouterr().out)["semantics_check"]["passed"] is True
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "arch-profile", "--model", str(path), "--require",
|
|
"persistent_editing"])
|
|
assert refused.value.code == 4 and "not implemented" in capsys.readouterr().err
|