mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Extend the controlled intervention harness beyond single channels. - Fixed directions (rows of the pack's stored basis) and rank-k subspaces load from their direction artifact before the model does. The artifact must match the file, tensor and metadata digests that the dossier's source report recorded; discovery and subspace reports now record that binding, and reports without it are refused. Refitted difference-of-means directions stay refused. - Subspace ablation removes the dose fraction of the projection onto the whole span. Steering a rank > 1 span needs one declared direction inside it (within 1e-4). - Random controls are rank- and norm-matched: a seeded rank-k span orthogonal to the target span, and for a declared steering direction a rank-1 vector orthogonal to the whole span. Mismatched controls are refused before the arms run and at attachment. - Each arm records target kind, rank, digests and orthogonalization. Evaluation rows on the artifact's own fit role and revision are refused. Channel specs and digests are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
3.3 KiB
Python
68 lines
3.3 KiB
Python
"""Temporary operations of a controlled experiment, owned by a context manager.
|
|
|
|
Both operations reuse the shared ``SteeringHookManager``: one forward hook on the
|
|
decoder layer module of the estimate's site, removed when the context exits,
|
|
including on exceptions and cancellation.
|
|
|
|
* ``steer`` adds ``coefficient * unit(v)`` (units ``raw``) or
|
|
``coefficient * ||x|| * unit(v)`` (units ``residual_norm``) to the layer output.
|
|
* ``ablate`` removes ``coefficient * (x . unit(v)) * unit(v)``: the coefficient is
|
|
the fraction of the projection removed (units ``projection_fraction``; 1 removes
|
|
it completely). A rank-k basis (orthonormal rows, ``ablate`` only) removes
|
|
``coefficient`` of the projection onto the whole span: one projection per
|
|
orthonormal row, which equals the projection onto the span.
|
|
|
|
Token scope applies to every forward pass, including each decoding step:
|
|
``all_tokens`` edits every position; ``last_token`` edits the final position of
|
|
each pass (the last prompt token on the first pass, then the newest token).
|
|
"""
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from obliteratus.analysis.steering_vectors import (
|
|
SteeringConfig, SteeringHookManager, SteeringVector,
|
|
)
|
|
from obliteratus.experiments.intervention_checks import vector_sha256
|
|
|
|
POSITIONS = {"all_tokens": "all", "last_token": "last"}
|
|
HOOK = "forward hook on the decoder layer output (residual stream after the layer)"
|
|
ORDERING = ("sole forward hook on the module: installed after the baseline was verified "
|
|
"hook-free, removed before the restoration check; arms run in recorded order")
|
|
SCOPE_NOTE = ("token scope applies to every forward pass including each decoding step; "
|
|
"last_token edits the final position of each pass")
|
|
|
|
|
|
@contextmanager
|
|
def apply_operation(model, layer, vector, coefficient, operation):
|
|
"""Install the operation's hook on ``layer`` for the duration of the context."""
|
|
rows = [vector] if vector.ndim == 1 else list(vector)
|
|
if len(rows) > 1 and operation["kind"] != "ablate":
|
|
raise ValueError("Only ablation acts on a rank-k span; steer one declared direction")
|
|
manager = SteeringHookManager()
|
|
config = SteeringConfig(
|
|
vectors=[SteeringVector(row, None, "dossier estimate", 1.0) for row in rows],
|
|
target_layers=[0],
|
|
alpha=float(coefficient), position=POSITIONS[operation["token_scope"]], normalize=True,
|
|
mode="project" if operation["kind"] == "ablate" else "steer",
|
|
norm_scaled=operation["units"] == "residual_norm")
|
|
manager.install(model, config, layer_modules=[layer])
|
|
try:
|
|
yield manager
|
|
finally:
|
|
manager.remove()
|
|
|
|
|
|
OPERATIONS = {"steer": apply_operation, "ablate": apply_operation}
|
|
|
|
|
|
def hook_record(target, operation, coefficient, vector, direction, details=None):
|
|
rank = 1 if vector.ndim == 1 else int(vector.shape[0])
|
|
return {"rank": rank, "orthogonalization": details,"site": target["geometry"]["site"], "module": target["module"], "hook": HOOK,
|
|
"operation": operation["kind"], "token_scope": operation["token_scope"],
|
|
"position": POSITIONS[operation["token_scope"]], "scope_note": SCOPE_NOTE,
|
|
"units": operation["units"], "coefficient": coefficient, "direction": direction,
|
|
"vector_sha256": vector_sha256(vector), "ordering": ORDERING}
|
|
|
|
|
|
__all__ = ["HOOK", "OPERATIONS", "ORDERING", "POSITIONS", "apply_operation", "hook_record"]
|