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>
107 lines
4.9 KiB
Python
107 lines
4.9 KiB
Python
"""MoE observation: sparse coverage reads as untested, route/load changes, shared modules."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import moe_observation as mo
|
|
from obliteratus.arch_adapters import CapabilityRefused, bind
|
|
from tests.fixtures.tiny_arch_models import (
|
|
sparse_coverage_moe, tiny_llama, tiny_mixtral, token_batches,
|
|
)
|
|
|
|
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
|
|
|
|
|
|
def test_sparse_expert_is_insufficient_coverage_never_no_effect():
|
|
bound = bind(sparse_coverage_moe())
|
|
report = mo.observe(bound, token_batches())
|
|
layer0 = report["coverage"]["layers"]["0"]
|
|
assert layer0["tokens"] == 72 and layer0["rows"] == 12
|
|
experts = layer0["experts"]
|
|
assert experts["3"]["routed_tokens"] == 1 and experts["3"]["routed_rows"] == 1
|
|
assert experts["2"]["routed_tokens"] == 0
|
|
for expert in ("2", "3"):
|
|
assert experts[expert]["status"] == "insufficient_coverage"
|
|
assert experts[expert]["tested"] is False
|
|
assert "not evidence of no effect" in experts[expert]["note"]
|
|
assert experts["0"]["status"] == experts["1"]["status"] == "covered"
|
|
assert sum(e["routed_tokens"] for e in experts.values()) == 72 # top-1
|
|
json.dumps(report, allow_nan=False)
|
|
|
|
|
|
def test_route_and_load_change_under_a_reversible_intervention():
|
|
model = sparse_coverage_moe()
|
|
bound = bind(model)
|
|
direction = model.model.layers[1].mlp.gate.weight[0].detach().clone()
|
|
report = mo.observe_intervention(bound, token_batches(), direction, 0)
|
|
assert report["restoration"]["passed"] and report["upstream_unchanged"]["passed"]
|
|
assert report["downstream_layers"] == [1]
|
|
change = report["route_changes"]["1"]
|
|
assert change["changed_choice_fraction"] > 0 and change["expert_choice_kl"] > 0
|
|
assert change["pristine_expert_load"] != change["edited_expert_load"]
|
|
deltas = change["expert_load_deltas"]
|
|
assert any(d["load_delta"] != 0 for d in deltas)
|
|
assert all(d["load_delta_interval"][0] <= d["load_delta"] <= d["load_delta_interval"][1]
|
|
for d in deltas)
|
|
assert report["route_changes"]["0"]["changed_choice_fraction"] == 0
|
|
effects = report["expert_effects"]
|
|
for expert in ("2", "3"): # layer 0: untested, effect fields are None, never 0
|
|
item = effects["0"][expert]
|
|
assert item["status"] == "insufficient_coverage"
|
|
assert item["retention"] is None and item["probability_shift"] is None
|
|
tested = [e for e in effects["1"].values() if e["tested"]]
|
|
assert tested and all(0 <= e["retention"] <= 1 and e["retention_interval"] for e in tested)
|
|
assert effects["0"]["0"]["retention"] == 1.0 # upstream of the hook
|
|
shared = report["shared_modules"]["before"]["1"]
|
|
assert shared["present"] and shared["decomposition_max_abs_error"] < 1e-5
|
|
assert 0 < shared["shared_norm_fraction_mean"] < 1
|
|
json.dumps(report, allow_nan=False)
|
|
|
|
|
|
def test_expert_intervals_scale_with_coverage():
|
|
wide, narrow = mo.wilson(4, 5), mo.wilson(400, 500)
|
|
assert wide[1] - wide[0] > narrow[1] - narrow[0]
|
|
assert mo.wilson(0, 0) is None
|
|
torch.manual_seed(0)
|
|
values = torch.randn(400)
|
|
_, small = mo.mean_interval(values[:10])
|
|
_, large = mo.mean_interval(values)
|
|
assert small[1] - small[0] > large[1] - large[0]
|
|
assert mo.mean_interval(values[:1]) == (None, None)
|
|
|
|
|
|
def test_floor_controls_status_and_is_validated():
|
|
bound = bind(sparse_coverage_moe())
|
|
records = mo.record_routing(bound, token_batches())
|
|
loose = mo.expert_coverage(records, {"min_tokens": 1, "min_rows": 1})
|
|
assert loose["layers"]["0"]["experts"]["3"]["status"] == "covered"
|
|
assert loose["layers"]["0"]["experts"]["2"]["status"] == "insufficient_coverage"
|
|
with pytest.raises(ValueError, match="positive integers"):
|
|
mo.expert_coverage(records, {"min_tokens": 0})
|
|
with pytest.raises(ValueError, match="positive integers"):
|
|
mo.expert_coverage(records, {"tokens": 3})
|
|
shifted = {k: {**v, "rows": v["rows"] + 1} for k, v in records.items()}
|
|
with pytest.raises(ValueError, match="token-aligned"):
|
|
mo.expert_effects(records, shifted)
|
|
|
|
|
|
def test_padding_is_excluded_and_no_shared_module_is_reported_as_absent():
|
|
bound = bind(tiny_mixtral())
|
|
ids = torch.tensor([[2, 3, 4, 5], [6, 7, 0, 0]])
|
|
mask = torch.tensor([[1, 1, 1, 1], [1, 1, 0, 0]])
|
|
report = mo.observe(bound, [{"input_ids": ids, "attention_mask": mask}])
|
|
assert report["coverage"]["layers"]["0"]["tokens"] == 6
|
|
shared = report["shared_modules"]["0"]
|
|
assert shared["present"] is False and "shared_output_norm_mean" not in shared
|
|
assert shared["decomposition_max_abs_error"] < 1e-5
|
|
|
|
|
|
def test_refusals():
|
|
with pytest.raises(CapabilityRefused, match="no router|router"):
|
|
mo.observe(bind(tiny_llama()), token_batches())
|
|
bound = bind(sparse_coverage_moe())
|
|
with pytest.raises(ValueError, match="finite nonzero vector of width 16"):
|
|
mo.observe_intervention(bound, token_batches(), torch.zeros(16), 0)
|