mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
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.
269 lines
13 KiB
Python
269 lines
13 KiB
Python
"""Scoped, weight-preserving interventions at dense gated MLP channels."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import weakref
|
|
|
|
import torch
|
|
from torch import nn
|
|
|
|
from obliteratus.strategies.utils import get_layer_modules
|
|
from .spec import NeuronRecipe, _index
|
|
|
|
|
|
SUPPORTED_ARCHITECTURES = frozenset({"llama", "qwen2", "qwen3", "qwen3_5", "qwen3_5_text"})
|
|
_OWNERS = weakref.WeakKeyDictionary()
|
|
_OWNERS_LOCK = threading.RLock()
|
|
|
|
|
|
def _sites(handle):
|
|
if handle.architecture not in SUPPORTED_ARCHITECTURES:
|
|
raise ValueError("Neuron Lab supports dense Llama/Qwen gated MLPs only")
|
|
model = handle.model
|
|
if model.training:
|
|
raise ValueError("Neuron interventions require model.eval()")
|
|
if (getattr(model, "is_quantized", False) or getattr(model, "quantization_method", None)
|
|
or getattr(model, "is_loaded_in_4bit", False)
|
|
or getattr(model, "is_loaded_in_8bit", False)):
|
|
raise ValueError("Quantized neuron adapters have not been qualified")
|
|
device_map = getattr(model, "hf_device_map", None) or {}
|
|
if any(str(value) in {"disk", "meta"} for value in device_map.values()):
|
|
raise ValueError("Disk/meta offloading is unsupported by the neuron adapter")
|
|
devices = {p.device for p in model.parameters()}
|
|
if len(devices) != 1 or any(device.type not in {"cpu", "cuda", "mps"} for device in devices):
|
|
raise ValueError("Neuron Lab requires resident weights on one CPU, CUDA or MPS device")
|
|
if any(getattr(module, "_hf_hook", None) is not None for module in model.modules()):
|
|
raise ValueError("Offload/dispatch hooks are unsupported by the neuron adapter")
|
|
result = []
|
|
for index, layer in enumerate(get_layer_modules(handle)):
|
|
mlp = getattr(layer, "mlp", None)
|
|
if mlp is None or hasattr(mlp, "experts"):
|
|
raise ValueError(f"Layer {index} is not a supported dense gated MLP")
|
|
gate, up, down = (getattr(mlp, name, None)
|
|
for name in ("gate_proj", "up_proj", "down_proj"))
|
|
if any(type(module) is not nn.Linear for module in (gate, up, down)):
|
|
raise ValueError(f"Layer {index} requires native Linear gate/up/down projections")
|
|
if (gate.in_features != up.in_features or gate.out_features != up.out_features
|
|
or down.in_features != up.out_features or down.out_features != up.in_features):
|
|
raise ValueError(f"Layer {index} has inconsistent gated MLP dimensions")
|
|
if any(not p.is_floating_point() for module in (gate, up, down)
|
|
for p in module.parameters()):
|
|
raise ValueError("Neuron adapter requires floating-point projections")
|
|
result.append((layer, down))
|
|
if not result:
|
|
raise ValueError("No supported neuron sites found")
|
|
return result
|
|
|
|
|
|
def validate_neuron_model(handle):
|
|
_sites(handle)
|
|
|
|
|
|
def list_neuron_layers(handle):
|
|
return [{"layer": index, "neurons": down.in_features, "module": "mlp.down_proj.input"}
|
|
for index, (_, down) in enumerate(_sites(handle))]
|
|
|
|
|
|
class NeuronSession:
|
|
"""Own hooks for one model until exit, including exceptional exit.
|
|
|
|
Supports uncached, complete-prefix forwards. Prompt lengths count valid
|
|
tokens, so padding cannot shift prompt/generated boundaries. The generated
|
|
scope changes already-generated positions: the first answer token, predicted
|
|
by the prompt, is unaffected. Thread-sharing a hooked model is rejected.
|
|
"""
|
|
|
|
def __init__(self, handle, recipe, *, prompt_lengths=None):
|
|
if not isinstance(recipe, NeuronRecipe):
|
|
raise ValueError("recipe must be a NeuronRecipe")
|
|
if recipe.model_id != handle.model_name:
|
|
raise ValueError("Neuron recipe model_id does not match the loaded model")
|
|
if (recipe.model_revision is not None
|
|
and recipe.model_revision != handle.source_model_revision):
|
|
raise ValueError("Neuron recipe model_revision does not match the loaded model")
|
|
self.handle, self.recipe = handle, recipe
|
|
self.sites = _sites(handle)
|
|
for edit in recipe.edits:
|
|
if edit.layer >= len(self.sites) or edit.neuron >= self.sites[edit.layer][1].in_features:
|
|
raise ValueError(f"Neuron index out of range: layer {edit.layer}, neuron {edit.neuron}")
|
|
if prompt_lengths is not None:
|
|
values = [prompt_lengths] if type(prompt_lengths) is int else prompt_lengths
|
|
if not isinstance(values, (tuple, list)) or not values:
|
|
raise ValueError("prompt_lengths must be a non-negative integer or list")
|
|
for value in values:
|
|
_index(value, "prompt length")
|
|
self.prompt_lengths = tuple(values)
|
|
self.broadcast_prompt_length = type(prompt_lengths) is int
|
|
else:
|
|
self.prompt_lengths = None
|
|
self.broadcast_prompt_length = False
|
|
if recipe.scope != "all_tokens" and self.prompt_lengths is None:
|
|
raise ValueError("Prompt/generated scope requires prompt_lengths")
|
|
self._hooks = []
|
|
self._mask = None
|
|
self._entered = False
|
|
self._thread = None
|
|
|
|
def __enter__(self):
|
|
if self._entered:
|
|
raise RuntimeError("NeuronSession is single-use; create a new session")
|
|
with _OWNERS_LOCK:
|
|
if self.handle.model in _OWNERS:
|
|
raise RuntimeError("Another neuron session already owns this model")
|
|
_OWNERS[self.handle.model] = self
|
|
self._entered = True
|
|
self._thread = threading.get_ident()
|
|
try:
|
|
self._hooks.append(self.handle.model.register_forward_pre_hook(
|
|
self._prepare_mask, with_kwargs=True))
|
|
self._hooks.append(self.handle.model.register_forward_hook(
|
|
self._finish_forward, always_call=True))
|
|
for index, (_, down) in enumerate(self.sites):
|
|
edits = tuple(edit for edit in self.recipe.edits
|
|
if edit.enabled and edit.layer == index)
|
|
if edits:
|
|
self._hooks.append(down.register_forward_pre_hook(
|
|
self._make_hook(edits), with_kwargs=True))
|
|
except BaseException:
|
|
self.close()
|
|
raise
|
|
return self
|
|
|
|
def _prepare_mask(self, module, args, kwargs):
|
|
if threading.get_ident() != self._thread:
|
|
raise RuntimeError("A hooked neuron model cannot be shared across threads")
|
|
if self._mask is not None:
|
|
raise RuntimeError("Nested model forwards are unsupported by NeuronSession")
|
|
if len(args) > 1:
|
|
raise ValueError("Pass attention, position and cache arguments by keyword")
|
|
cache_mode = kwargs.get("use_cache")
|
|
if (kwargs.get("past_key_values") is not None
|
|
or (cache_mode is not None and cache_mode is not False)):
|
|
raise ValueError("NeuronSession requires uncached full-prefix forwards (use_cache=False)")
|
|
ids = kwargs.get("input_ids", args[0] if args else None)
|
|
embeds = kwargs.get("inputs_embeds")
|
|
inputs = ids if ids is not None else embeds
|
|
if not isinstance(inputs, torch.Tensor) or inputs.ndim not in (2, 3):
|
|
raise ValueError("NeuronSession needs batched input_ids or inputs_embeds")
|
|
batch, tokens = inputs.shape[:2]
|
|
if batch < 1 or tokens < 1:
|
|
raise ValueError("NeuronSession needs nonempty batches and sequences")
|
|
attention = kwargs.get("attention_mask")
|
|
if attention is None:
|
|
valid = torch.ones((batch, tokens), dtype=torch.bool, device=inputs.device)
|
|
else:
|
|
if (not isinstance(attention, torch.Tensor) or attention.shape != (batch, tokens)
|
|
or not torch.all((attention == 0) | (attention == 1))):
|
|
raise ValueError("attention_mask must be a binary batch-by-token mask")
|
|
valid = attention.to(device=inputs.device, dtype=torch.bool)
|
|
if self.recipe.scope != "all_tokens":
|
|
lengths = self.prompt_lengths
|
|
if self.broadcast_prompt_length:
|
|
lengths = lengths * batch
|
|
if len(lengths) != batch:
|
|
raise ValueError("One prompt length is required per batch row")
|
|
lengths = torch.tensor(lengths, device=valid.device).unsqueeze(-1)
|
|
if torch.any(lengths.squeeze(-1) > valid.sum(-1)):
|
|
raise ValueError("Prompt length exceeds the valid full prefix")
|
|
prompt = valid.cumsum(-1) <= lengths
|
|
valid = valid & (prompt if self.recipe.scope == "prompt_tokens" else ~prompt)
|
|
self._mask = valid
|
|
return args, {**kwargs, "use_cache": False}
|
|
|
|
def _finish_forward(self, module, args, output):
|
|
# A rejected call on another thread must not clear the owner's mask.
|
|
if threading.get_ident() == self._thread:
|
|
self._mask = None
|
|
|
|
def _make_hook(self, edits):
|
|
def hook(module, args, kwargs):
|
|
original = args[0] if args else kwargs.get("input")
|
|
if (not isinstance(original, torch.Tensor) or original.ndim != 3
|
|
or self._mask is None or original.shape[:2] != self._mask.shape):
|
|
raise ValueError("Neuron hook requires matching batched token activations")
|
|
mask = self._mask.to(original.device)
|
|
if not mask.any():
|
|
return None
|
|
changed = original.clone()
|
|
for edit in edits:
|
|
channel = changed[..., edit.neuron]
|
|
if edit.operation == "add":
|
|
value = channel + edit.value
|
|
elif edit.operation == "scale":
|
|
value = channel * edit.value
|
|
elif edit.operation == "flip":
|
|
value = -channel
|
|
elif edit.operation == "zero":
|
|
value = torch.zeros_like(channel)
|
|
elif edit.operation == "set":
|
|
value = torch.full_like(channel, edit.value)
|
|
else:
|
|
value = torch.clamp(channel, min=edit.lower, max=edit.upper)
|
|
if not torch.isfinite(value[mask]).all():
|
|
raise ValueError("Neuron intervention produced non-finite activations")
|
|
changed[..., edit.neuron] = torch.where(mask, value, channel)
|
|
if args:
|
|
return (changed, *args[1:]), kwargs
|
|
return args, {**kwargs, "input": changed}
|
|
return hook
|
|
|
|
def close(self):
|
|
for hook in reversed(self._hooks):
|
|
hook.remove()
|
|
self._hooks.clear()
|
|
self._mask = None
|
|
with _OWNERS_LOCK:
|
|
if _OWNERS.get(self.handle.model) is self:
|
|
del _OWNERS[self.handle.model]
|
|
|
|
def __exit__(self, *exc):
|
|
self.close()
|
|
|
|
|
|
def inspect_neuron(handle, layer, neuron, inputs):
|
|
"""Capture one unmodified channel and layer-output norms on bounded inputs.
|
|
|
|
Returns CPU tensors including padded positions plus an explicit validity mask.
|
|
These are descriptive measurements of the supplied text, not a corpus-wide
|
|
calibration or a behavioral explanation.
|
|
"""
|
|
_index(layer, "layer")
|
|
_index(neuron, "neuron")
|
|
recipe = NeuronRecipe(handle.model_name, (), handle.source_model_revision)
|
|
control = NeuronSession(handle, recipe)
|
|
if layer >= len(control.sites) or neuron >= control.sites[layer][1].in_features:
|
|
raise ValueError("Neuron index out of range")
|
|
if not isinstance(inputs, dict):
|
|
raise ValueError("inputs must be a model input mapping")
|
|
block, down = control.sites[layer]
|
|
observed = {}
|
|
|
|
def capture(module, args, kwargs):
|
|
hidden = args[0] if args else kwargs.get("input")
|
|
if hidden.ndim != 3 or control._mask is None:
|
|
raise ValueError("Inspection requires batched token activations")
|
|
if "activations" in observed:
|
|
raise ValueError("Repeated MLP invocation is unsupported by this adapter")
|
|
observed["activations"] = hidden[..., neuron].detach().float().cpu().clone()
|
|
observed["valid_mask"] = control._mask.detach().cpu().clone()
|
|
|
|
def residual(module, args, output):
|
|
hidden = output[0] if isinstance(output, tuple) else output
|
|
observed["residual_norms"] = hidden.detach().float().norm(dim=-1).cpu()
|
|
|
|
handles = []
|
|
with control:
|
|
try:
|
|
handles.append(down.register_forward_pre_hook(capture, with_kwargs=True))
|
|
handles.append(block.register_forward_hook(residual))
|
|
with torch.inference_mode():
|
|
handle.model(**{**inputs, "use_cache": False})
|
|
finally:
|
|
for hook in reversed(handles):
|
|
hook.remove()
|
|
if set(observed) != {"activations", "valid_mask", "residual_norms"}:
|
|
raise ValueError("The selected neuron site was not executed")
|
|
observed["down_column_norm"] = float(down.weight[:, neuron].detach().float().norm().cpu())
|
|
return observed
|