Files
OBLITERATUS/tests/fixtures/tiny_arch_models.py
Joseph MaglyandClaude Opus 5.5 abae042774 feat(analysis): architecture adapters, MoE coverage and transfer dossiers
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>
2026-11-16 13:31:08 -05:00

83 lines
2.9 KiB
Python

"""Tiny random-init dense and sparse MoE decoders built from configs (no downloads)."""
import torch
from transformers import (
GPT2Config,
GPT2LMHeadModel,
LlamaConfig,
LlamaForCausalLM,
MixtralConfig,
MixtralForCausalLM,
Qwen2MoeConfig,
Qwen2MoeForCausalLM,
)
COMMON = dict(vocab_size=11, pad_token_id=0, bos_token_id=1, eos_token_id=1)
def _build(model_cls, config, seed):
with torch.random.fork_rng():
torch.manual_seed(seed)
return model_cls(config).eval()
def tiny_gpt2(layers=2, seed=11):
return _build(GPT2LMHeadModel, GPT2Config(n_embd=16, n_layer=layers, n_head=2, n_inner=32,
n_positions=64, **COMMON), seed)
def tiny_llama(layers=2, seed=12):
return _build(LlamaForCausalLM, LlamaConfig(
hidden_size=16, intermediate_size=32, num_hidden_layers=layers, num_attention_heads=2,
num_key_value_heads=2, max_position_embeddings=64, **COMMON), seed)
def tiny_mixtral(seed=13):
return _build(MixtralForCausalLM, MixtralConfig(
hidden_size=16, intermediate_size=32, num_hidden_layers=2, num_attention_heads=2,
num_key_value_heads=2, num_local_experts=4, num_experts_per_tok=2,
max_position_embeddings=64, **COMMON), seed)
def tiny_qwen2_moe(top_k=2, mlp_only_layers=(), seed=14):
return _build(Qwen2MoeForCausalLM, Qwen2MoeConfig(
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=top_k, moe_intermediate_size=16,
shared_expert_intermediate_size=16, mlp_only_layers=list(mlp_only_layers),
max_position_embeddings=64, **COMMON), seed)
RARE_TOKEN = 10
@torch.no_grad()
def sparse_coverage_moe():
"""Top-1 Qwen2-MoE whose layer-0 router never picks expert 2 and picks expert 3 only for
``RARE_TOKEN``: layer-0 attention output is zeroed so the router reads the normed
embedding; ordinary tokens carry a shared component that experts 0/1 prefer."""
model = tiny_qwen2_moe(top_k=1)
layer = model.model.layers[0]
layer.self_attn.o_proj.weight.zero_()
generator = torch.Generator().manual_seed(1)
embedding = model.model.embed_tokens.weight
embedding.zero_()
embedding[:, 2:] = torch.randn(11, 14, generator=generator)
embedding[:, 1] = 3.0
embedding[RARE_TOKEN] = 0.0
embedding[RARE_TOKEN, 0] = 5.0
router = layer.mlp.gate.weight
router.zero_()
router[0:2, 2:] = 0.5 * torch.randn(2, 14, generator=generator)
router[0:2, 1] = 2.0
router[3, 0] = 10.0
return model
def token_batches(rows=12, length=6, seed=2, rare_rows=((3, 2),)):
generator = torch.Generator().manual_seed(seed)
ids = torch.randint(2, RARE_TOKEN, (rows, length), generator=generator)
for row, position in rare_rows:
ids[row, position] = RARE_TOKEN
half = rows // 2
return [{"input_ids": ids[:half]}, {"input_ids": ids[half:]}]