Files
OBLITERATUS/obliteratus/analysis/activation_observer.py

150 lines
6.5 KiB
Python

"""Read-only decoder observations with explicit token and phase metadata."""
from __future__ import annotations
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Callable, Iterator, Mapping
import torch
from torch import nn
@dataclass(frozen=True)
class ActivationObservation:
"""Detached CPU values at one valid token per batch row."""
values: torch.Tensor
token_positions: torch.Tensor
phase: str
readout: str
def decoder_output_readout(_layer: nn.Module, output: object) -> torch.Tensor:
"""Read an ordinary decoder output without collapsing extra axes."""
hidden = output[0] if isinstance(output, tuple) and output else output
if not isinstance(hidden, torch.Tensor) or hidden.ndim != 3:
raise ValueError("Decoder readout requires [batch, tokens, hidden] output")
return hidden
def deepseek_v41_post_block_readout(layer: nn.Module, output: object) -> torch.Tensor:
"""Read V4.1's post-block state before the next block's Engram injection."""
if not isinstance(output, tuple) or len(output) != 2:
raise ValueError("V4.1 readout requires (hidden_streams, pre_mix)")
streams, pre = output
if (
not isinstance(streams, torch.Tensor)
or not isinstance(pre, torch.Tensor)
or streams.ndim != 4
or pre.ndim != 3
or streams.shape[:-1] != pre.shape
or streams.device != pre.device
or not streams.is_floating_point()
or not pre.is_floating_point()
):
raise ValueError("V4.1 readout requires matching [B,S,hc,H] and [B,S,hc] tensors")
hc_mult = getattr(getattr(layer, "attn_hc", None), "hc_mult", None)
config = getattr(getattr(layer, "self_attn", None), "config", None)
if streams.shape[2:] != (hc_mult, getattr(config, "hidden_size", None)):
raise ValueError("V4.1 readout disagrees with the layer's stream and hidden dimensions")
collapse = getattr(layer, "hc_collapse", None)
if not callable(collapse):
raise ValueError("V4.1 layer has no native hc_collapse")
return collapse(streams, pre)
@contextmanager
def observe_activations(
layers: Mapping[str, nn.Module],
*,
attention_mask: torch.Tensor,
phase: str,
token_positions: torch.Tensor | None = None,
readout: Callable[[nn.Module, object], torch.Tensor] = decoder_output_readout,
readout_name: str = "decoder_output",
) -> Iterator[dict[str, ActivationObservation]]:
"""Observe each selected layer once during one forward call.
The mask covers the current call's tokens, excluding cached history.
Positions are physical indices in this call; the default is each row's last
valid token. The caller supplies the semantic phase.
Use a new scope for each decode step. Values never replace layer outputs.
Custom readouts must not mutate their inputs.
"""
if not layers or any(not isinstance(layer, nn.Module) for layer in layers.values()):
raise ValueError("Observation requires named layer modules")
if any(not isinstance(name, str) or not name for name in layers):
raise ValueError("Observation layer names must be nonempty strings")
if len({id(layer) for layer in layers.values()}) != len(layers):
raise ValueError("Observation layers must be distinct")
if not isinstance(phase, str) or not phase.strip():
raise ValueError("Observation phase must be explicit")
if not isinstance(readout_name, str) or not readout_name.strip() or not callable(readout):
raise ValueError("Observation requires a named readout")
if readout is not decoder_output_readout and readout_name == "decoder_output":
raise ValueError("Custom readouts require their own identity")
if (
not isinstance(attention_mask, torch.Tensor)
or attention_mask.ndim != 2
or 0 in attention_mask.shape
or not torch.all((attention_mask == 0) | (attention_mask == 1))
or not torch.all(attention_mask.bool().any(dim=1))
):
raise ValueError("Observation mask requires binary rows with at least one valid token")
mask = attention_mask.detach().to(device="cpu", dtype=torch.bool, copy=True)
if token_positions is None:
positions = torch.arange(mask.shape[1]).expand_as(mask).masked_fill(~mask, -1).max(1).values
else:
if (
not isinstance(token_positions, torch.Tensor)
or token_positions.shape != (mask.shape[0],)
or token_positions.dtype not in (torch.int32, torch.int64)
):
raise ValueError("Token positions require one integer index per batch row")
positions = token_positions.detach().to(device="cpu", dtype=torch.long, copy=True)
if (
torch.any(positions < 0)
or torch.any(positions >= mask.shape[1])
or not mask[torch.arange(mask.shape[0]), positions].all()
):
raise ValueError("Token positions must select unmasked tokens in the current call")
observations: dict[str, ActivationObservation] = {}
handles = []
def make_hook(name: str):
def observe(layer, _args, output):
if name in observations:
raise ValueError("Use a new observation scope for each forward call")
with torch.no_grad():
hidden = readout(layer, output)
if (
not isinstance(hidden, torch.Tensor)
or hidden.ndim != 3
or hidden.shape[:2] != mask.shape
or hidden.shape[-1] == 0
or not hidden.is_floating_point()
):
raise ValueError("Readout must match the current call's [batch, tokens] mask")
rows = torch.arange(hidden.shape[0], device=hidden.device)
values = hidden[rows, positions.to(hidden.device)].detach().to("cpu", copy=True)
if not torch.isfinite(values).all():
raise ValueError("Observed activation contains nonfinite values")
observations[name] = ActivationObservation(
values, positions.clone(), phase, readout_name,
)
return observe
try:
for name, layer in layers.items():
handles.append(layer.register_forward_hook(make_hook(name)))
yield observations
if observations.keys() != layers.keys():
raise ValueError("Not all requested layers executed during observation")
except BaseException:
observations.clear()
raise
finally:
for handle in handles:
handle.remove()