Files
OBLITERATUS/obliteratus/interventions/spec.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

175 lines
6.8 KiB
Python

"""Bounded, data-only recipes for reversible native MLP channel interventions."""
from __future__ import annotations
from dataclasses import asdict, dataclass
import json
import math
from pathlib import Path
MAX_RECIPE_BYTES = 65536
MAX_EDITS = 64
OPERATIONS = frozenset({"add", "scale", "flip", "zero", "set", "clamp"})
SCOPES = frozenset({"all_tokens", "prompt_tokens", "generated_tokens"})
def _index(value, name):
if type(value) is not int or value < 0:
raise ValueError(f"{name} must be a non-negative integer")
def _number(value, name):
if type(value) not in (int, float):
raise ValueError(f"{name} must be a finite number")
try:
result = float(value)
except (OverflowError, ValueError) as exc:
raise ValueError(f"{name} must be a finite number") from exc
if not math.isfinite(result):
raise ValueError(f"{name} must be a finite number")
return result
def _text(value, name, limit):
if not isinstance(value, str) or not value.strip() or len(value) > limit:
raise ValueError(f"{name} must be a nonempty string of at most {limit} characters")
@dataclass(frozen=True)
class NeuronEdit:
"""One ordered operation on a post-gate MLP channel, before down projection.
Add, set, and clamp use raw activation units. Scale uses dimensionless gain;
flip and zero are fixed gain presets. No residual-normalized dose is implied.
"""
layer: int
neuron: int
operation: str
value: float | None = None
lower: float | None = None
upper: float | None = None
enabled: bool = True
def __post_init__(self):
_index(self.layer, "layer")
_index(self.neuron, "neuron")
if not isinstance(self.operation, str) or self.operation not in OPERATIONS:
raise ValueError("Unsupported neuron operation")
if type(self.enabled) is not bool:
raise ValueError("enabled must be a boolean")
if self.operation in {"add", "scale", "set"}:
object.__setattr__(self, "value", _number(self.value, "value"))
elif self.value is not None:
raise ValueError(f"{self.operation} does not take value")
if self.operation == "clamp":
if self.lower is None and self.upper is None:
raise ValueError("clamp requires at least one bound")
for name in ("lower", "upper"):
if getattr(self, name) is not None:
object.__setattr__(self, name, _number(getattr(self, name), name))
if self.lower is not None and self.upper is not None and self.lower > self.upper:
raise ValueError("clamp lower must not exceed upper")
elif self.lower is not None or self.upper is not None:
raise ValueError("Only clamp accepts lower/upper bounds")
def to_dict(self):
return asdict(self)
@classmethod
def from_dict(cls, value):
if not isinstance(value, dict) or set(value) - set(cls.__dataclass_fields__):
raise ValueError("Neuron edit must be an object with known fields")
try:
return cls(**value)
except TypeError as exc:
raise ValueError("Neuron edit requires layer, neuron and operation") from exc
def _unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"Duplicate JSON field: {key}")
result[key] = value
return result
def _reject_constant(value):
raise ValueError(f"Non-finite JSON number: {value}")
@dataclass(frozen=True)
class NeuronRecipe:
"""Model-bound recipe. Duplicate targets execute in the recorded order."""
model_id: str
edits: tuple[NeuronEdit, ...]
model_revision: str | None = None
scope: str = "all_tokens"
def __post_init__(self):
_text(self.model_id, "model_id", 1024)
if self.model_revision is not None:
_text(self.model_revision, "model_revision", 256)
if not isinstance(self.scope, str) or self.scope not in SCOPES:
raise ValueError("Unsupported token scope")
if not isinstance(self.edits, (tuple, list)) or len(self.edits) > MAX_EDITS:
raise ValueError(f"edits must contain at most {MAX_EDITS} operations")
if any(not isinstance(edit, NeuronEdit) for edit in self.edits):
raise ValueError("edits must contain NeuronEdit objects")
object.__setattr__(self, "edits", tuple(self.edits))
def to_dict(self):
return {"schema_version": 1, "model_id": self.model_id,
"model_revision": self.model_revision, "scope": self.scope,
"edits": [edit.to_dict() for edit in self.edits]}
def to_json(self):
text = json.dumps(self.to_dict(), indent=2, allow_nan=False) + "\n"
if len(text.encode("utf-8")) > MAX_RECIPE_BYTES:
raise ValueError("Neuron recipe exceeds size limit")
return text
@classmethod
def from_dict(cls, value):
fields = {"schema_version", "model_id", "model_revision", "scope", "edits"}
if not isinstance(value, dict) or set(value) - fields:
raise ValueError("Neuron recipe must be an object with known fields")
if type(value.get("schema_version")) is not int or value["schema_version"] != 1:
raise ValueError("Unsupported neuron recipe schema_version")
edits = value.get("edits")
if not isinstance(edits, list) or len(edits) > MAX_EDITS:
raise ValueError(f"edits must be a list with at most {MAX_EDITS} operations")
return cls(model_id=value.get("model_id"),
model_revision=value.get("model_revision"),
scope=value.get("scope", "all_tokens"),
edits=tuple(NeuronEdit.from_dict(edit) for edit in edits))
@classmethod
def from_json(cls, text):
if not isinstance(text, str) or len(text.encode("utf-8")) > MAX_RECIPE_BYTES:
raise ValueError("Neuron recipe must be UTF-8 JSON of at most 64 KiB")
try:
return cls.from_dict(json.loads(text, object_pairs_hook=_unique_object,
parse_constant=_reject_constant))
except (json.JSONDecodeError, RecursionError) as exc:
raise ValueError("Invalid neuron recipe JSON") from exc
def save(self, path):
"""Write a new recipe; never silently replace an existing artifact."""
with Path(path).open("x", encoding="utf-8") as stream:
stream.write(self.to_json())
@classmethod
def load(cls, path):
with Path(path).open("rb") as stream:
data = stream.read(MAX_RECIPE_BYTES + 1)
if len(data) > MAX_RECIPE_BYTES:
raise ValueError("Neuron recipe exceeds size limit")
try:
return cls.from_json(data.decode("utf-8"))
except UnicodeDecodeError as exc:
raise ValueError("Neuron recipe must be UTF-8") from exc