Files
OBLITERATUS/tests/test_neuron_interventions.py
Pliny the Younger 3873d0b2ba feat: add reversible Neuron Lab to Concept Lab
Add scoped native MLP channel controls, strict versioned recipes, and isolated Neuron Lab sessions with inspection, ordered editing, comparison, cancellation, and immutable exports. Cover arithmetic, lifecycle, real offline model forwards and Gradio callbacks; document the CPU reference boundary.
2026-10-27 16:11:54 -04:00

775 lines
31 KiB
Python

"""Offline numerical and lifecycle contracts for reversible neuron interventions."""
from copy import deepcopy
from concurrent.futures import ThreadPoolExecutor
from dataclasses import FrozenInstanceError, replace
import json
from types import SimpleNamespace
import pytest
import torch
from torch import nn
from torch.nn import functional as F
from obliteratus.interventions import (
NeuronEdit,
NeuronRecipe,
NeuronSession,
inspect_neuron,
list_neuron_layers,
validate_neuron_model,
)
from obliteratus.models.loader import ModelHandle
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
MODEL_ID = "offline/neuron-fixture"
INPUTS = torch.tensor([
[[-2.0, -1.0, 0.5, 1.0], [0.5, 1.0, 2.0, -1.0], [2.0, 3.0, -1.0, 0.0]],
[[1.0, 0.0, -1.0, 2.0], [-1.0, 2.0, 1.0, 0.5], [0.0, -2.0, 3.0, 1.0]],
])
class _GatedMLP(nn.Module):
def __init__(self):
super().__init__()
self.gate_proj = nn.Linear(4, 4, bias=False)
self.up_proj = nn.Linear(4, 4, bias=False)
self.down_proj = nn.Linear(4, 4, bias=False)
self.act_fn = F.silu
with torch.no_grad():
self.gate_proj.weight.copy_(torch.eye(4))
self.up_proj.weight.copy_(torch.eye(4).roll(1, 0))
self.down_proj.weight.copy_(torch.tensor([
[1.0, 0.2, -0.5, 0.1], [0.0, 2.0, 0.3, -0.2],
[0.2, 0.0, 1.0, 0.5], [0.1, -0.5, 0.0, 1.0],
]))
def forward(self, hidden):
return self.down_proj(self.act_fn(self.gate_proj(hidden)) * self.up_proj(hidden))
class _Layer(nn.Module):
def __init__(self, tuple_output=False):
super().__init__()
self.mlp = _GatedMLP()
self.tuple_output = tuple_output
def forward(self, hidden):
result = hidden + self.mlp(hidden)
return (result, None) if self.tuple_output else result
class _Model(nn.Module):
def __init__(self, architecture="llama", nested=False, tuple_output=False):
super().__init__()
text_config = SimpleNamespace(
model_type="qwen3_5_text" if nested else architecture,
num_hidden_layers=2, num_attention_heads=1,
hidden_size=4, intermediate_size=4,
)
self.config = (SimpleNamespace(model_type=architecture, text_config=text_config)
if nested else text_config)
self.model = nn.Module()
text = self.model
if nested:
self.model.language_model = nn.Module()
text = self.model.language_model
text.layers = nn.ModuleList([_Layer(tuple_output), _Layer(tuple_output)])
self.fail_forward = False
self.eval()
@property
def layers(self):
return getattr(self.model, "language_model", self.model).layers
def forward(self, inputs_embeds, attention_mask=None, past_key_values=None,
use_cache=False, **kwargs):
hidden = inputs_embeds
for layer in self.layers:
hidden = layer(hidden)
if isinstance(hidden, tuple):
hidden = hidden[0]
if self.fail_forward:
raise RuntimeError("deliberate forward failure")
return SimpleNamespace(logits=hidden)
def _handle(model=None):
model = _Model() if model is None else model
return ModelHandle(
model=model, tokenizer=None, config=model.config,
model_name=MODEL_ID, task="causal_lm", source_model_revision="fixture-revision",
)
@pytest.fixture
def handle():
return _handle()
def _recipe(*edits, **kwargs):
return NeuronRecipe(model_id=MODEL_ID, edits=edits, **kwargs)
def _forward(handle, values=INPUTS, **kwargs):
with torch.inference_mode():
return handle.model(inputs_embeds=values, use_cache=False, **kwargs).logits
def _capture_down_inputs(handle, values=INPUTS, layer=0, **kwargs):
seen = []
hook = handle.model.layers[layer].mlp.down_proj.register_forward_pre_hook(
lambda _module, args: seen.append(args[0].detach().clone()),
)
try:
result = _forward(handle, values, **kwargs)
finally:
hook.remove()
assert len(seen) == 1
return seen[0], result
def _assert_no_hooks(model):
for module in model.modules():
assert not module._forward_pre_hooks
assert not module._forward_hooks
def _weights(model):
return {name: value.detach().clone() for name, value in model.state_dict().items()}
@pytest.mark.parametrize("edit,manual", [
(NeuronEdit(0, 1, "add", value=2.5), lambda x: x + 2.5),
(NeuronEdit(0, 1, "scale", value=-2.0), lambda x: x * -2),
(NeuronEdit(0, 1, "flip"), lambda x: -x),
(NeuronEdit(0, 1, "zero"), lambda x: torch.zeros_like(x)),
(NeuronEdit(0, 1, "set", value=-0.75), lambda x: torch.full_like(x, -0.75)),
(NeuronEdit(0, 1, "clamp", lower=-0.2, upper=0.4),
lambda x: torch.clamp(x, -0.2, 0.4)),
(NeuronEdit(0, 1, "clamp", lower=0.2), lambda x: torch.clamp_min(x, 0.2)),
(NeuronEdit(0, 1, "clamp", upper=0.4), lambda x: torch.clamp_max(x, 0.4)),
])
def test_edit_matches_manual_gated_mlp_oracle_and_preserves_weights(handle, edit, manual):
# The oracle computes the actual post-gate product, not gate_proj's raw output.
before, baseline = _capture_down_inputs(handle)
expected = F.silu(INPUTS) * INPUTS.roll(1, -1)
torch.testing.assert_close(before, expected, rtol=0, atol=0)
expected[..., 1] = manual(expected[..., 1])
original = _weights(handle.model)
with NeuronSession(handle, _recipe(edit)):
actual, changed = _capture_down_inputs(handle)
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
assert torch.equal(actual[..., [0, 2, 3]], before[..., [0, 2, 3]])
assert not torch.equal(changed, baseline)
assert torch.equal(_forward(handle), baseline)
assert all(torch.equal(value, original[name]) for name, value in handle.model.state_dict().items())
_assert_no_hooks(handle.model)
def test_ordered_composition_and_disabled_edits(handle):
before, _ = _capture_down_inputs(handle)
recipe = _recipe(
NeuronEdit(0, 1, "add", value=3), NeuronEdit(0, 1, "scale", value=2),
NeuronEdit(0, 1, "zero", enabled=False), NeuronEdit(0, 2, "set", value=7),
)
with NeuronSession(handle, recipe):
actual, _ = _capture_down_inputs(handle)
expected = before.clone()
expected[..., 1] = (before[..., 1] + 3) * 2
expected[..., 2] = 7
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
@pytest.mark.parametrize("edit", [
NeuronEdit(0, 1, "add", value=0), NeuronEdit(0, 1, "scale", value=1),
NeuronEdit(0, 1, "zero", enabled=False),
])
def test_zero_dose_is_bit_exact(handle, edit):
baseline = _forward(handle)
with NeuronSession(handle, _recipe(edit)):
assert torch.equal(_forward(handle), baseline)
def test_intervention_does_not_mutate_incoming_activation_storage(handle):
originals = []
observer = handle.model.layers[0].mlp.down_proj.register_forward_pre_hook(
lambda _module, args: originals.append((args[0], args[0].detach().clone())),
)
try:
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "set", value=30))):
_forward(handle)
finally:
observer.remove()
assert len(originals) == 1
assert torch.equal(*originals[0])
@pytest.mark.parametrize("scope,expected_selected", [
("all_tokens", [[False, True, True], [True, True, False]]),
("prompt_tokens", [[False, True, False], [True, True, False]]),
("generated_tokens", [[False, False, True], [False, False, False]]),
])
def test_mixed_prompt_lengths_and_both_padding_sides(handle, scope, expected_selected):
mask = torch.tensor([[0, 1, 1], [1, 1, 0]])
before, _ = _capture_down_inputs(handle, attention_mask=mask)
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "add", value=8), scope=scope),
prompt_lengths=[1, 2]):
actual, _ = _capture_down_inputs(handle, attention_mask=mask)
expected = before.clone()
expected[..., 1] += torch.tensor(expected_selected) * 8
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
def test_generated_scope_leaves_first_answer_prediction_untouched(handle):
recipe = _recipe(NeuronEdit(0, 1, "add", value=8), scope="generated_tokens")
prompt = INPUTS[:, :2]
baseline_prompt = _forward(handle, prompt)
before, _ = _capture_down_inputs(handle)
with NeuronSession(handle, recipe, prompt_lengths=2):
# The last prompt position predicts the first answer token and stays baseline.
assert torch.equal(_forward(handle, prompt), baseline_prompt)
actual, _ = _capture_down_inputs(handle)
assert torch.equal(actual[:, :2], before[:, :2])
assert torch.equal(actual[:, 2, 1], before[:, 2, 1] + 8)
# A second complete-prefix forward must not advance an implicit token counter.
repeated, _ = _capture_down_inputs(handle)
assert torch.equal(repeated, actual)
@pytest.mark.parametrize("mask", [
torch.zeros(2, 3, dtype=torch.long),
torch.tensor([[True, False, True], [False, True, False]]),
])
def test_attention_mask_excludes_padding_without_contiguity_assumption(handle, mask):
before, _ = _capture_down_inputs(handle)
with NeuronSession(handle, _recipe(NeuronEdit(0, 0, "set", value=12))):
actual, _ = _capture_down_inputs(handle, attention_mask=mask)
expected = before.clone()
expected[..., 0] = torch.where(mask.bool(), 12, before[..., 0])
assert torch.equal(actual, expected)
def test_recipe_serialization_roundtrip_is_replayable(handle, tmp_path):
recipe = _recipe(
NeuronEdit(0, 1, "add", value=2.25), NeuronEdit(1, 2, "clamp", upper=1),
NeuronEdit(0, 3, "flip", enabled=False), model_revision="fixture-revision",
scope="prompt_tokens",
)
path = tmp_path / "controls.json"
recipe.save(path)
copies = [NeuronRecipe.from_dict(recipe.to_dict()),
NeuronRecipe.from_json(recipe.to_json()), NeuronRecipe.load(path)]
assert all(copy == recipe for copy in copies)
with NeuronSession(handle, recipe, prompt_lengths=2):
expected = _forward(handle)
for copy in copies:
with NeuronSession(handle, copy, prompt_lengths=2):
assert torch.equal(_forward(handle), expected)
def test_edit_and_recipe_are_frozen():
edit = NeuronEdit(0, 1, "flip")
with pytest.raises(FrozenInstanceError):
edit.neuron = 2
recipe = _recipe(edit)
with pytest.raises(FrozenInstanceError):
recipe.scope = "generated_tokens"
encoded = edit.to_dict()
encoded["neuron"] = 3
assert edit.neuron == 1
@pytest.mark.parametrize("kwargs", [
{"layer": -1}, {"layer": True}, {"layer": 0.0}, {"layer": "0"},
{"neuron": -1}, {"neuron": False}, {"neuron": 1.0},
{"operation": "patch"}, {"operation": None}, {"enabled": 1},
{"value": True}, {"value": float("nan")}, {"value": float("inf")},
{"value": -float("inf")}, {"value": "2"}, {"value": None},
{"lower": 0}, {"upper": 1},
])
def test_invalid_edit_fields_fail_before_any_model_work(kwargs):
fields = {"layer": 0, "neuron": 1, "operation": "add", "value": 2}
fields.update(kwargs)
with pytest.raises((ValueError, TypeError)):
NeuronEdit(**fields)
@pytest.mark.parametrize("kwargs", [
{"operation": "flip", "value": 0}, {"operation": "zero", "upper": 1},
{"operation": "clamp"}, {"operation": "clamp", "lower": 2, "upper": 1},
{"operation": "clamp", "lower": float("nan")},
{"operation": "clamp", "upper": float("inf")},
{"operation": "clamp", "lower": False},
{"operation": "clamp", "lower": 0, "value": 1},
])
def test_operation_parameters_are_unambiguous(kwargs):
with pytest.raises((ValueError, TypeError)):
NeuronEdit(layer=0, neuron=0, **kwargs)
@pytest.mark.parametrize("mutate", [
lambda data: data.update(unknown="ignored?"),
lambda data: data.update(scope="final_token"),
lambda data: data.update(model_id=""),
lambda data: data.update(model_id=True),
lambda data: data.update(model_revision=7),
lambda data: data.update(schema_version=True),
lambda data: data.update(schema_version=2),
lambda data: data.pop("schema_version"),
lambda data: data.update(edits={}),
lambda data: data.update(edits=[None]),
lambda data: data["edits"][0].update(unknown=1),
lambda data: data["edits"][0].update(layer=True),
lambda data: data["edits"][0].update(value=float("nan")),
lambda data: data["edits"][0].pop("operation"),
])
def test_import_rejects_malformed_recipe_fields(mutate):
data = _recipe(NeuronEdit(0, 1, "add", value=1)).to_dict()
mutate(data)
with pytest.raises((ValueError, TypeError)):
NeuronRecipe.from_dict(data)
@pytest.mark.parametrize("payload", [
"{", "[]", "null", '"recipe"',
'{"model_id":"a","model_id":"b","edits":[]}',
])
def test_json_parser_rejects_invalid_root_and_duplicate_keys(payload):
with pytest.raises((ValueError, TypeError)):
NeuronRecipe.from_json(payload)
def test_json_parser_rejects_nonfinite_literals_and_nested_duplicate_keys():
data = _recipe(NeuronEdit(0, 1, "add", value=1)).to_dict()
payload = json.dumps(data)
for invalid in ["NaN", "Infinity", "-Infinity"]:
bad = payload.replace('"value": 1.0', f'"value": {invalid}')
assert bad != payload
with pytest.raises(ValueError):
NeuronRecipe.from_json(bad)
duplicate = payload.replace('"neuron": 1', '"neuron": 1, "neuron": 2')
with pytest.raises(ValueError):
NeuronRecipe.from_json(duplicate)
def test_recipe_import_and_export_are_bounded(tmp_path):
edit = NeuronEdit(0, 1, "flip")
assert len(_recipe(*([edit] * 64)).edits) == 64
with pytest.raises(ValueError):
_recipe(*([edit] * 65))
data = _recipe(edit).to_dict()
data["edits"] *= 65
with pytest.raises(ValueError):
NeuronRecipe.from_dict(data)
oversized = " " * (64 * 1024) + _recipe(edit).to_json()
path = tmp_path / "oversized.json"
path.write_text(oversized)
with pytest.raises(ValueError):
NeuronRecipe.from_json(oversized)
with pytest.raises(ValueError):
NeuronRecipe.load(path)
def test_recipe_load_rejects_invalid_utf8(tmp_path):
path = tmp_path / "invalid.json"
path.write_bytes(b"\xff\xfe")
with pytest.raises(ValueError, match="UTF-8"):
NeuronRecipe.load(path)
def test_recipe_save_does_not_replace_an_existing_artifact(tmp_path):
path = tmp_path / "controls.json"
first = _recipe(NeuronEdit(0, 1, "flip"))
first.save(path)
with pytest.raises(FileExistsError):
_recipe(NeuronEdit(1, 2, "zero")).save(path)
assert NeuronRecipe.load(path) == first
@pytest.mark.parametrize("edit", [NeuronEdit(2, 0, "flip"), NeuronEdit(0, 4, "flip")])
def test_out_of_range_target_fails_before_hook_install(handle, edit):
with pytest.raises(ValueError):
with NeuronSession(handle, _recipe(edit)):
pytest.fail("invalid target entered the intervention context")
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("changes", [
{"model_id": "offline/other"}, {"model_revision": "other-revision"},
])
def test_model_identity_must_match_recipe(handle, changes):
recipe = replace(_recipe(NeuronEdit(0, 0, "flip")), **changes)
with pytest.raises(ValueError):
with NeuronSession(handle, recipe):
pytest.fail("mismatched identity was accepted")
_assert_no_hooks(handle.model)
def test_only_one_active_session_per_model_and_close_is_idempotent(handle):
recipe = _recipe(NeuronEdit(0, 1, "add", value=3))
baseline = _forward(handle)
first = NeuronSession(handle, recipe)
with first:
changed = _forward(handle)
with pytest.raises((ValueError, RuntimeError)):
with NeuronSession(_handle(handle.model), recipe):
pytest.fail("same underlying model acquired two active sessions")
assert torch.equal(_forward(handle), changed)
first.close()
first.close()
assert torch.equal(_forward(handle), baseline)
with NeuronSession(handle, recipe):
assert torch.equal(_forward(handle), changed)
_assert_no_hooks(handle.model)
def test_closed_session_is_single_use(handle):
session = NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip")))
with session:
_forward(handle)
with pytest.raises(RuntimeError, match="single-use"):
with session:
pytest.fail("closed session was reused")
_assert_no_hooks(handle.model)
def test_foreign_thread_rejected_without_corrupting_owner_session(handle):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "add", value=3))):
expected = _forward(handle)
with ThreadPoolExecutor(max_workers=1) as pool:
task = pool.submit(_forward, handle)
with pytest.raises(RuntimeError, match="thread"):
task.result(timeout=10)
assert torch.equal(_forward(handle), expected)
_assert_no_hooks(handle.model)
def test_forward_failure_can_be_caught_and_retried_inside_session(handle):
baseline = _forward(handle)
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "add", value=3))):
expected = _forward(handle)
handle.model.fail_forward = True
with pytest.raises(RuntimeError, match="deliberate"):
_forward(handle)
handle.model.fail_forward = False
assert torch.equal(_forward(handle), expected)
assert torch.equal(_forward(handle), baseline)
_assert_no_hooks(handle.model)
def test_cleanup_on_exception_restores_preexisting_hooks(handle):
observed = []
existing = handle.model.layers[0].mlp.down_proj.register_forward_hook(
lambda _module, _args, _output: observed.append(True),
)
try:
with pytest.raises(RuntimeError, match="deliberate"):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
handle.model.fail_forward = True
_forward(handle)
handle.model.fail_forward = False
assert observed == [True]
assert list(handle.model.layers[0].mlp.down_proj._forward_hooks) == [existing.id]
_forward(handle)
assert observed == [True, True]
finally:
existing.remove()
_assert_no_hooks(handle.model)
def test_partial_install_failure_removes_hooks_and_releases_model(handle, monkeypatch):
recipe = _recipe(NeuronEdit(0, 1, "flip"), NeuronEdit(1, 1, "zero"))
later = handle.model.layers[1].mlp.down_proj
original_register = later.register_forward_pre_hook
def fail(*_args, **_kwargs):
raise RuntimeError("deliberate installation failure")
monkeypatch.setattr(later, "register_forward_pre_hook", fail)
with pytest.raises(RuntimeError, match="installation failure"):
with NeuronSession(handle, recipe):
pytest.fail("partial installation should fail")
_assert_no_hooks(handle.model)
monkeypatch.setattr(later, "register_forward_pre_hook", original_register)
with NeuronSession(handle, recipe):
_forward(handle)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("past", [(), (torch.ones(1),)])
def test_cached_forwards_are_rejected_and_context_cleans_up(handle, past):
with pytest.raises((ValueError, RuntimeError), match="cache|past_key_values"):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
_forward(handle, past_key_values=past)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("lengths", [None, True, -1, 1.5, [1], [1, False], [1, -1]])
def test_invalid_prompt_lengths_fail_closed(handle, lengths):
with pytest.raises((ValueError, TypeError)):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"), scope="prompt_tokens"),
prompt_lengths=lengths):
_forward(handle)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("mask", [
torch.ones(2, 2), torch.ones(2, 1, 3), torch.tensor([[1., float("nan"), 1.], [1, 1, 1]]),
torch.tensor([[1, 2, 1], [1, 1, 1]]),
])
def test_invalid_attention_masks_fail_closed(handle, mask):
with pytest.raises((ValueError, TypeError)):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
_forward(handle, attention_mask=mask)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("values", [
None, [[1, 2, 3]], torch.ones(3), torch.ones(2, 3, 4, 1),
torch.ones(0, 3, 4), torch.ones(2, 0, 4),
])
def test_invalid_model_inputs_fail_before_edits(handle, values):
with pytest.raises((ValueError, TypeError)):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
_forward(handle, values)
_assert_no_hooks(handle.model)
def test_prompt_length_cannot_exceed_valid_prefix(handle):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"), scope="prompt_tokens"),
prompt_lengths=2):
with pytest.raises(ValueError, match="length|prefix"):
_forward(handle, attention_mask=torch.tensor([[0, 0, 1], [1, 1, 1]]))
# Root validation failure must not leave a stale per-forward mask.
_forward(handle)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("cache_mode", [True, 0, 1, "false"])
def test_explicit_cache_request_rejected(handle, cache_mode):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
with pytest.raises(ValueError, match="cache"):
handle.model(inputs_embeds=INPUTS, use_cache=cache_mode)
_forward(handle)
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("extra_args", [
(torch.tensor([[0, 1, 1], [1, 1, 0]]),),
(None, (torch.ones(1),)),
(None, None, True),
])
def test_positional_mask_and_cache_arguments_fail_closed(handle, extra_args):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
expected = _forward(handle)
with pytest.raises(ValueError, match="keyword|positional"):
handle.model(INPUTS, *extra_args)
assert torch.equal(_forward(handle), expected)
_assert_no_hooks(handle.model)
def test_single_positional_input_with_keyword_mask_remains_supported(handle):
mask = torch.tensor([[0, 1, 1], [1, 1, 0]])
with torch.inference_mode(), NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
expected = handle.model(inputs_embeds=INPUTS, attention_mask=mask, use_cache=False).logits
actual = handle.model(INPUTS, attention_mask=mask, use_cache=False).logits
assert torch.equal(actual, expected)
_assert_no_hooks(handle.model)
def test_omitted_or_none_cache_setting_is_forced_off_without_changing_model_default():
class CacheByDefaultModel(_Model):
def __init__(self):
super().__init__()
self.cache_modes = []
self.config.use_cache = True
def forward(self, inputs_embeds, attention_mask=None, past_key_values=None,
use_cache=True, **kwargs):
self.cache_modes.append(use_cache)
return super().forward(inputs_embeds, attention_mask, past_key_values,
use_cache=use_cache, **kwargs)
model = CacheByDefaultModel()
handle = _handle(model)
with torch.inference_mode():
model(inputs_embeds=INPUTS)
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
model(inputs_embeds=INPUTS)
model(inputs_embeds=INPUTS, use_cache=None)
model(inputs_embeds=INPUTS, use_cache=False)
model(inputs_embeds=INPUTS)
assert model.cache_modes == [True, False, False, False, True]
assert model.config.use_cache is True
_assert_no_hooks(model)
def test_absent_device_map_value_is_supported(handle):
handle.model.hf_device_map = None
validate_neuron_model(handle)
baseline = _forward(handle)
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
assert not torch.equal(_forward(handle), baseline)
assert torch.equal(_forward(handle), baseline)
_assert_no_hooks(handle.model)
def test_overflow_is_reported_without_weight_or_activation_mutation(handle):
original = _weights(handle.model)
baseline = _forward(handle)
with pytest.raises((ValueError, RuntimeError), match="finite|overflow"):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "scale", value=1e300))):
_forward(handle)
assert torch.equal(_forward(handle), baseline)
assert all(torch.equal(value, original[name]) for name, value in handle.model.state_dict().items())
_assert_no_hooks(handle.model)
@pytest.mark.parametrize("architecture", ["llama", "qwen2", "qwen3", "qwen3_5_text"])
def test_dense_supported_architectures_list_real_neurons(architecture):
handle = _handle(_Model(architecture))
validate_neuron_model(handle)
rows = list_neuron_layers(handle)
assert len(rows) == 2
assert [row["layer"] for row in rows] == [0, 1]
assert [row["neurons"] for row in rows] == [4, 4]
assert all("mlp.down_proj" in row["module"] for row in rows)
def test_nested_qwen35_text_path_and_tuple_layer_output():
handle = _handle(_Model("qwen3_5", nested=True, tuple_output=True))
rows = list_neuron_layers(handle)
assert len(rows) == 2 and rows[0]["neurons"] == 4
assert "mlp.down_proj" in rows[0]["module"]
baseline = _forward(handle)
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "zero"))):
assert not torch.equal(_forward(handle), baseline)
assert torch.equal(_forward(handle), baseline)
class _WrappedLinear(nn.Linear):
pass
@pytest.mark.parametrize("failure", [
"training", "unsupported", "moe", "quantized", "wrapped", "meta", "offload",
"dispatch_hook", "missing_mlp", "moe_layer", "dimensions", "integer_weights",
])
def test_unsupported_models_fail_before_install(handle, failure):
if failure == "training":
handle.model.train()
elif failure == "unsupported":
handle.config.model_type = "gpt2"
handle.architecture = "gpt2"
elif failure == "moe":
handle.config.model_type = "qwen3_moe"
handle.architecture = "qwen3_moe"
elif failure == "quantized":
handle.model.is_loaded_in_4bit = True
elif failure == "wrapped":
handle.model.layers[0].mlp.down_proj = _WrappedLinear(4, 4, bias=False)
elif failure == "meta":
handle.model.layers[0].mlp.down_proj.to("meta")
elif failure == "offload":
handle.model.hf_device_map = {"model.layers.0": "cpu", "model.layers.1": "disk"}
elif failure == "dispatch_hook":
handle.model.layers[0]._hf_hook = object()
elif failure == "missing_mlp":
del handle.model.layers[0].mlp
elif failure == "moe_layer":
handle.model.layers[0].mlp.experts = nn.ModuleList()
elif failure == "dimensions":
handle.model.layers[0].mlp.up_proj = nn.Linear(4, 5, bias=False)
elif failure == "integer_weights":
down = handle.model.layers[0].mlp.down_proj
down.weight = nn.Parameter(down.weight.to(torch.int64), requires_grad=False)
with pytest.raises((ValueError, TypeError, RuntimeError)):
with NeuronSession(handle, _recipe(NeuronEdit(0, 1, "flip"))):
pytest.fail("unsupported model accepted")
_assert_no_hooks(handle.model)
def test_inspection_reports_post_gate_activation_and_layer_residual_norm(handle):
mask = torch.tensor([[0, 1, 1], [1, 1, 0]])
weights = _weights(handle.model)
baseline = _forward(handle)
result = inspect_neuron(handle, 0, 1, {
"inputs_embeds": INPUTS, "attention_mask": mask, "use_cache": False,
})
activation = (F.silu(INPUTS) * INPUTS.roll(1, -1))[..., 1]
column = handle.model.layers[0].mlp.down_proj.weight[:, 1].detach()
gated = F.silu(INPUTS) * INPUTS.roll(1, -1)
residual = INPUTS + F.linear(gated, handle.model.layers[0].mlp.down_proj.weight)
expected_norm = torch.linalg.vector_norm(residual, dim=-1)
assert result["activations"].device.type == "cpu"
assert result["residual_norms"].device.type == "cpu"
assert result["valid_mask"].device.type == "cpu"
assert not result["activations"].requires_grad
torch.testing.assert_close(result["activations"], activation, rtol=0, atol=0)
torch.testing.assert_close(result["residual_norms"], expected_norm)
assert torch.equal(result["valid_mask"], mask.bool())
assert result["down_column_norm"] == pytest.approx(column.norm().item())
assert torch.equal(_forward(handle), baseline)
assert all(torch.equal(value, weights[name]) for name, value in handle.model.state_dict().items())
_assert_no_hooks(handle.model)
def test_inspection_removes_hooks_if_forward_raises(handle):
handle.model.fail_forward = True
with pytest.raises(RuntimeError, match="deliberate"):
inspect_neuron(handle, 0, 1, {"inputs_embeds": INPUTS, "use_cache": False})
_assert_no_hooks(handle.model)
handle.model.fail_forward = False
assert inspect_neuron(handle, 0, 1, {"inputs_embeds": INPUTS})["activations"].shape == (2, 3)
@pytest.mark.parametrize("layer,neuron", [(-1, 0), (True, 0), (2, 0), (0, 4), (0, False)])
def test_inspection_rejects_invalid_coordinates(handle, layer, neuron):
with pytest.raises(ValueError):
inspect_neuron(handle, layer, neuron, {"inputs_embeds": INPUTS})
_assert_no_hooks(handle.model)
def test_inspection_requires_a_mapping(handle):
with pytest.raises(ValueError, match="mapping"):
inspect_neuron(handle, 0, 0, INPUTS)
_assert_no_hooks(handle.model)
def test_inspection_supports_tuple_layer_output():
handle = _handle(_Model(tuple_output=True))
result = inspect_neuron(handle, 0, 1, {"inputs_embeds": INPUTS})
assert result["activations"].shape == result["residual_norms"].shape == (2, 3)
assert result["valid_mask"].all()
_assert_no_hooks(handle.model)
@pytest.mark.integration
def test_random_init_llama_changes_logits_only_inside_context():
from transformers import LlamaConfig, LlamaForCausalLM
torch.manual_seed(19)
model = LlamaForCausalLM(LlamaConfig(
vocab_size=32, hidden_size=16, intermediate_size=24, num_hidden_layers=2,
num_attention_heads=2, num_key_value_heads=2, max_position_embeddings=32,
attention_dropout=0, use_cache=False,
)).eval()
handle = _handle(model)
inputs = {"input_ids": torch.tensor([[1, 2, 3, 4], [0, 0, 5, 6]]),
"attention_mask": torch.tensor([[1, 1, 1, 1], [0, 0, 1, 1]]),
"use_cache": False}
original = deepcopy(model.state_dict())
with torch.inference_mode():
baseline = model(**inputs).logits
for _ in range(2):
with NeuronSession(handle, _recipe(NeuronEdit(0, 3, "add", value=5))):
changed = model(**inputs).logits
assert not torch.equal(changed, baseline)
assert torch.isfinite(changed).all()
assert torch.equal(model(**inputs).logits, baseline)
assert all(torch.equal(value, original[name]) for name, value in model.state_dict().items())
_assert_no_hooks(model)