Files
OBLITERATUS/tests/test_strategies.py
Joseph Magly 806dc3bb60 fix(ablation): transact legacy edits against offloaded weights
(cherry picked from commit 8b73d1c3168619e88ef2a671097843377b30fee2)
2026-10-05 17:27:18 -04:00

392 lines
15 KiB
Python

"""Tests for ablation strategies using a small GPT-2 model."""
from __future__ import annotations
from types import SimpleNamespace
import pytest
import torch
import torch.nn as nn
from obliteratus.strategies.base import AblationSpec
from obliteratus.strategies.registry import STRATEGY_REGISTRY, get_strategy
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
def _make_dummy_handle():
"""Create a minimal ModelHandle with a tiny GPT-2 for testing (no network)."""
from unittest.mock import MagicMock
from transformers import GPT2Config, GPT2LMHeadModel
from obliteratus.models.loader import ModelHandle
config = GPT2Config(
vocab_size=1000,
n_positions=128,
n_embd=64,
n_layer=2,
n_head=2,
n_inner=256,
)
model = GPT2LMHeadModel(config)
model.eval()
# Strategy tests don't tokenize — use a simple mock
tokenizer = MagicMock()
tokenizer.pad_token = "<pad>"
tokenizer.eos_token = "<eos>"
handle = ModelHandle(
model=model,
tokenizer=tokenizer,
config=config,
model_name="gpt2-test",
task="causal_lm",
)
handle.snapshot()
return handle
@pytest.fixture
def handle():
return _make_dummy_handle()
# ---------------------------------------------------------------------------
# Architecture navigation
# ---------------------------------------------------------------------------
class _Qwen35MoeLayer(nn.Module):
def __init__(self, *, linear_attention: bool = False):
super().__init__()
if linear_attention:
self.linear_attn = nn.Module()
else:
self.self_attn = nn.Module()
self.mlp = nn.Module()
class _Qwen35MoeModel(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Module()
self.model.layers = nn.ModuleList(
[_Qwen35MoeLayer(), _Qwen35MoeLayer(linear_attention=True)]
)
def test_qwen35_moe_resolves_layer_attention_and_ffn_paths():
from obliteratus.models.loader import ModelHandle
from obliteratus.strategies.utils import (
get_attention_module,
get_ffn_module,
get_layer_modules,
)
model = _Qwen35MoeModel()
handle = ModelHandle(
model=model,
tokenizer=SimpleNamespace(pad_token="<pad>", eos_token="<eos>"),
config=SimpleNamespace(
model_type="qwen3_5_moe",
num_hidden_layers=2,
num_attention_heads=4,
hidden_size=8,
intermediate_size=32,
),
model_name="Qwen/Qwen3.5-35B-A3B",
task="causal_lm",
)
layers = get_layer_modules(handle)
assert layers is model.model.layers
assert get_attention_module(layers[0], handle.architecture) is layers[0].self_attn
assert get_attention_module(layers[1], handle.architecture) is layers[1].linear_attn
assert get_ffn_module(layers[0], handle.architecture) is layers[0].mlp
assert get_ffn_module(layers[1], handle.architecture) is layers[1].mlp
# ---------------------------------------------------------------------------
# Registry tests
# ---------------------------------------------------------------------------
class TestRegistry:
def test_all_strategies_registered(self):
expected = {"layer_removal", "head_pruning", "ffn_ablation", "embedding_ablation"}
assert expected.issubset(set(STRATEGY_REGISTRY.keys()))
def test_get_strategy_returns_instance(self):
strat = get_strategy("layer_removal")
assert strat.name == "layer_removal"
def test_get_unknown_strategy_raises(self):
with pytest.raises(KeyError, match="Unknown strategy"):
get_strategy("nonexistent_strategy")
# ---------------------------------------------------------------------------
# Layer removal
# ---------------------------------------------------------------------------
class TestLayerRemoval:
def test_enumerate(self, handle):
strat = get_strategy("layer_removal")
specs = strat.enumerate(handle)
assert len(specs) == handle.num_layers
assert all(s.strategy_name == "layer_removal" for s in specs)
def test_apply_zeros_layer(self, handle):
strat = get_strategy("layer_removal")
specs = strat.enumerate(handle)
strat.apply(handle, specs[0])
from obliteratus.strategies.utils import get_layer_modules
layer = get_layer_modules(handle)[0]
for param in layer.parameters():
assert torch.all(param == 0), "Layer params should be zeroed after ablation"
def test_restore_after_ablation(self, handle):
strat = get_strategy("layer_removal")
specs = strat.enumerate(handle)
from obliteratus.strategies.utils import get_layer_modules
original_weight = get_layer_modules(handle)[0].attn.c_attn.weight.clone()
strat.apply(handle, specs[0])
handle.restore()
restored_weight = get_layer_modules(handle)[0].attn.c_attn.weight
assert torch.allclose(original_weight, restored_weight)
# ---------------------------------------------------------------------------
# Head pruning
# ---------------------------------------------------------------------------
class TestHeadPruning:
def test_enumerate(self, handle):
strat = get_strategy("head_pruning")
specs = strat.enumerate(handle)
assert len(specs) == handle.num_layers * handle.num_heads
def test_apply_zeros_head(self, handle):
strat = get_strategy("head_pruning")
spec = AblationSpec(
strategy_name="head_pruning",
component="layer_0_head_0",
description="test",
metadata={"layer_idx": 0, "head_idx": 0},
)
strat.apply(handle, spec)
from obliteratus.strategies.utils import get_layer_modules, get_attention_module
attn = get_attention_module(get_layer_modules(handle)[0], handle.architecture)
head_dim = handle.hidden_size // handle.num_heads
# GPT-2 uses c_attn (Conv1D), check output projection c_proj
if hasattr(attn, "c_proj"):
# Conv1D stores weight transposed
assert torch.all(attn.c_proj.weight[0:head_dim, :] == 0)
# ---------------------------------------------------------------------------
# FFN ablation
# ---------------------------------------------------------------------------
class TestFFNAblation:
def test_enumerate(self, handle):
strat = get_strategy("ffn_ablation")
specs = strat.enumerate(handle)
assert len(specs) == handle.num_layers
def test_apply_zeros_ffn(self, handle):
strat = get_strategy("ffn_ablation")
specs = strat.enumerate(handle)
strat.apply(handle, specs[0])
from obliteratus.strategies.utils import get_layer_modules, get_ffn_module
ffn = get_ffn_module(get_layer_modules(handle)[0], handle.architecture)
for param in ffn.parameters():
assert torch.all(param == 0)
# ---------------------------------------------------------------------------
# Embedding ablation
# ---------------------------------------------------------------------------
class TestEmbeddingAblation:
def test_enumerate(self, handle):
strat = get_strategy("embedding_ablation")
specs = strat.enumerate(handle)
assert len(specs) > 0
def test_apply_zeros_dims(self, handle):
strat = get_strategy("embedding_ablation")
spec = AblationSpec(
strategy_name="embedding_ablation",
component="embed_dims_0_4",
description="test",
metadata={"dim_start": 0, "dim_end": 4},
)
strat.apply(handle, spec)
from obliteratus.strategies.utils import get_embedding_module
emb = get_embedding_module(handle)
assert torch.all(emb.weight[:, 0:4] == 0)
@pytest.mark.parametrize("strategy", ["ffn_ablation", "layer_removal", "head_pruning", "embedding_ablation"])
@pytest.mark.parametrize("backing", ["cpu", "disk"])
def test_legacy_ablation_updates_offloaded_forward(handle, tmp_path, strategy, backing):
"""Real hooks must match an independently edited resident model."""
import copy
from accelerate import cpu_offload, disk_offload
from accelerate.utils.modeling import get_state_dict_offloaded_model
reference = copy.copy(handle)
reference.model = copy.deepcopy(handle.model)
operation = get_strategy(strategy)
spec = operation.enumerate(reference)[0]
operation.apply(reference, spec)
tokens = torch.tensor([[1, 2, 3]])
with torch.no_grad():
expected = reference.model(tokens).logits
if backing == "cpu":
cpu_offload(handle.model, execution_device=torch.device("cpu"))
else:
disk_offload(handle.model, str(tmp_path / "backing"), execution_device=torch.device("cpu"))
operation.apply(handle, spec)
with torch.no_grad():
actual = handle.model(tokens).logits
torch.testing.assert_close(actual, expected)
state = get_state_dict_offloaded_model(handle.model)
for name, value in reference.model.state_dict().items():
torch.testing.assert_close(state[name], value, msg=name)
def test_late_unsupported_target_prevents_all_ffn_mutation(handle):
from obliteratus.models.offload_surgery import UnsupportedOffloadedQuantizationError
from obliteratus.strategies.utils import get_layer_modules
ffn = get_layer_modules(handle)[0].mlp
original = ffn.c_fc.weight.clone()
ffn.c_proj.weight.quant_state = object()
with pytest.raises(UnsupportedOffloadedQuantizationError):
operation = get_strategy("ffn_ablation")
operation.apply(handle, operation.enumerate(handle)[0])
torch.testing.assert_close(ffn.c_fc.weight, original)
def test_late_commit_failure_restores_earlier_ffn_writes(handle, monkeypatch):
from obliteratus.models.offload_surgery import LogicalParameterTransaction
from obliteratus.strategies.utils import get_layer_modules
ffn = get_layer_modules(handle)[0].mlp
original = {name: value.clone() for name, value in ffn.state_dict().items()}
commit = LogicalParameterTransaction.commit
calls = 0
def fail_late(self, updated):
nonlocal calls
calls += 1
if calls == 3:
raise RuntimeError("injected late write failure")
return commit(self, updated)
monkeypatch.setattr(LogicalParameterTransaction, "commit", fail_late)
operation = get_strategy("ffn_ablation")
with pytest.raises(RuntimeError, match="injected late write"):
operation.apply(handle, operation.enumerate(handle)[0])
for name, value in ffn.state_dict().items():
torch.testing.assert_close(value, original[name])
def test_shared_logical_parameter_is_committed_once(monkeypatch):
from obliteratus.models.offload_surgery import LogicalParameterTransaction, zero_logical_parameters
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
root = nn.Module()
root.a = nn.Linear(4, 4, bias=False)
root.b = nn.Linear(4, 4, bias=False)
root.b.weight = root.a.weight
hook = AlignDevicesHook(execution_device="cpu", offload=True, place_submodules=True)
add_hook_to_module(root, hook)
calls = []
commit = LogicalParameterTransaction.commit
def count(self, value):
calls.append(self)
return commit(self, value)
monkeypatch.setattr(LogicalParameterTransaction, "commit", count)
zero_logical_parameters(root, [(root.a, "weight", (slice(None), slice(0, 1))),
(root.b, "weight", (slice(None), slice(1, 2)))])
assert len(calls) == 1
assert hook.weights_map["a.weight"].data_ptr() == root.b.weight.data_ptr()
assert torch.count_nonzero(hook.weights_map["a.weight"][:, :2]) == 0
def test_failed_rollback_invalidates_model_and_rejects_reuse(handle, monkeypatch):
from obliteratus.models.offload_surgery import LogicalParameterTransaction, OffloadRollbackError
commit = LogicalParameterTransaction.commit
calls = 0
def fail_commit(self, updated):
nonlocal calls
calls += 1
if calls == 2:
raise RuntimeError("late write")
commit(self, updated)
def fail_rollback(self):
raise OSError("rollback unavailable")
monkeypatch.setattr(LogicalParameterTransaction, "commit", fail_commit)
monkeypatch.setattr(LogicalParameterTransaction, "rollback", fail_rollback)
operation = get_strategy("ffn_ablation")
spec = operation.enumerate(handle)[0]
with pytest.raises(OffloadRollbackError, match="discard and reload"):
operation.apply(handle, spec)
assert handle.model._obliteratus_surgery_invalid
with pytest.raises(OffloadRollbackError, match="reload before surgery"):
operation.apply(handle, spec)
def test_grouped_query_head_pruning_uses_shared_kv_head():
from obliteratus.strategies.head_pruning import HeadPruningStrategy
root = nn.Module()
root.model = nn.Module()
layer = nn.Module()
root.model.layers = nn.ModuleList([layer])
layer.self_attn = nn.Module()
attn = layer.self_attn
for name, width in (("q_proj", 8), ("k_proj", 4), ("v_proj", 4), ("o_proj", 8)):
setattr(attn, name, nn.Linear(8, width))
handle = SimpleNamespace(model=root, architecture="llama", hidden_size=8, num_heads=4)
original_k = attn.k_proj.weight.clone()
original_o_bias = attn.o_proj.bias.clone()
operation = HeadPruningStrategy()
operation.apply(handle, AblationSpec("head_pruning", "head3", "test", {"layer_idx": 0, "head_idx": 3}))
assert torch.count_nonzero(attn.q_proj.weight[6:8]) == 0
assert torch.count_nonzero(attn.k_proj.weight[2:4]) == 0
assert torch.count_nonzero(attn.k_proj.bias[2:4]) == 0
torch.testing.assert_close(attn.k_proj.weight[:2], original_k[:2])
assert torch.count_nonzero(attn.o_proj.weight[:, 6:8]) == 0
torch.testing.assert_close(attn.o_proj.bias, original_o_bias)
def test_stale_disk_tie_metadata_rejects_before_mutation(handle, tmp_path):
from accelerate import disk_offload
from obliteratus.models.offload_surgery import resolve_logical_parameter, UnsupportedOffloadLayoutError
handle.model.lm_head.weight = nn.Parameter(handle.model.lm_head.weight.detach().clone() + 1)
disk_offload(handle.model, str(tmp_path / "backing"), execution_device=torch.device("cpu"))
embed = handle.model.transformer.wte
original = resolve_logical_parameter(embed, search_roots=(handle.model,)).tensor.clone()
operation = get_strategy("embedding_ablation")
with pytest.raises(UnsupportedOffloadLayoutError, match="tied weights disagree"):
operation.apply(handle, operation.enumerate(handle)[0])
torch.testing.assert_close(resolve_logical_parameter(embed, search_roots=(handle.model,)).tensor, original)