Files
OBLITERATUS/obliteratus/bayesian_execution.py

478 lines
20 KiB
Python

"""Exact trial plans and reversible execution for Bayesian optimization."""
from __future__ import annotations
from dataclasses import dataclass
import hashlib
import json
from typing import Any, Mapping, Protocol, runtime_checkable
import torch
from obliteratus.analysis.numerical_contracts import (
clone_tensor_to_cpu, copy_tensor_bounded_, validated_weight_update,
)
def tensor_sha256(value: torch.Tensor) -> str:
"""Hash one tensor's identity and logical values without changing storage."""
tensor = value.detach().contiguous() if value.device.type == "cpu" else clone_tensor_to_cpu(value)
digest = hashlib.sha256()
digest.update(str(tensor.dtype).encode())
digest.update(json.dumps(list(tensor.shape), separators=(",", ":")).encode())
digest.update(memoryview(tensor.view(torch.uint8).numpy()))
return digest.hexdigest()
@dataclass(frozen=True)
class BayesianTrialPlan:
"""One fully resolved edit, shared by scoring and final replay."""
parameters: Mapping[str, float]
attention_regularization: Mapping[int, float]
mlp_regularization: Mapping[int, float]
directions: Mapping[int, torch.Tensor]
policy: Mapping[str, Any]
def evidence(self) -> dict[str, Any]:
return {
"schema": 1,
"parameters": {key: float(value) for key, value in sorted(self.parameters.items())},
"attention_regularization": {
str(key): float(value)
for key, value in sorted(self.attention_regularization.items())
},
"mlp_regularization": {
str(key): float(value)
for key, value in sorted(self.mlp_regularization.items())
},
"direction_sha256": {
str(key): tensor_sha256(value)
for key, value in sorted(self.directions.items())
},
"policy": dict(self.policy),
}
@property
def sha256(self) -> str:
payload = json.dumps(self.evidence(), sort_keys=True, separators=(",", ":"))
return hashlib.sha256(payload.encode()).hexdigest()
def legacy_regularization(self) -> dict[int, float]:
"""Compatibility projection of the exact component plan."""
return {
index: (
self.attention_regularization[index] + self.mlp_regularization[index]
) / 2.0
for index in self.attention_regularization
}
@dataclass(frozen=True)
class BayesianApplyResult:
"""Observable mutation coverage for one application."""
applied_count: int
target_scope: tuple[str, ...]
@runtime_checkable
class BayesianExecution(Protocol):
"""Architecture-owned reversible application of a trial plan."""
snapshot_host_bytes: int
snapshot_disk_bytes: int
def identity(self) -> Mapping[str, Any]: ...
def capture(self) -> None: ...
def restore(self) -> None: ...
def apply(self, plan: BayesianTrialPlan) -> BayesianApplyResult: ...
def release(self) -> None: ...
class GenericBayesianExecution:
"""Reversible execution for resident generic residual writers and LM head."""
def __init__(self, pipeline, layer_modules, arch, folded_scales):
from obliteratus.abliterate import (
_ATTN_IN_NAMES,
_ATTN_OUT_NAMES,
_FFN_IN_NAMES,
_FFN_OUT_NAMES,
attention_projection_names,
)
from obliteratus.strategies.utils import get_attention_module, get_ffn_module
self.pipeline = pipeline
self.layer_modules = layer_modules
self.arch = arch
self.folded_scales = folded_scales
self._attn_names = _ATTN_OUT_NAMES
self._attn_input_names = _ATTN_IN_NAMES
self._ffn_names = _FFN_OUT_NAMES
self._ffn_input_names = _FFN_IN_NAMES
self._attention_projection_names = attention_projection_names
self._get_attention_module = get_attention_module
self._get_ffn_module = get_ffn_module
self.projection_target = getattr(pipeline, "projection_target", "output")
self.project_biases = bool(getattr(pipeline, "project_biases", False))
self.norm_preserve = bool(getattr(pipeline, "norm_preserve", False))
unsupported = [
label
for attribute, label in (
("project_embeddings", "embedding projection"),
("use_lora_ablation", "LoRA ablation"),
("use_sae_features", "SAE projection"),
("attention_head_surgery", "attention-head surgery"),
("safety_neuron_masking", "safety-neuron masking"),
("per_expert_directions", "per-expert directions"),
("expert_transplant", "expert transplant"),
("activation_steering", "activation steering"),
("invert_refusal", "refusal inversion"),
)
if bool(getattr(pipeline, attribute, False))
]
if getattr(pipeline, "_qwen35_projection_manifest", None):
unsupported.append("Qwen3.5 manifest targets")
if unsupported:
raise ValueError(
"generic Bayesian execution cannot score the configured extra mutations: "
+ ", ".join(unsupported)
+ "; inject an execution adapter that owns their complete scope"
)
if self.projection_target not in {"all", "output"}:
raise ValueError(
"generic Bayesian execution supports only complete 'all' or 'output' plans"
)
self._selected: list[tuple[str, torch.Tensor]] = []
self._snapshots: list[tuple[str, torch.Tensor, torch.Tensor]] = []
self._released = False
self._lm_head_name: str | None = None
selected: list[tuple[str, torch.Tensor]] = []
seen: set[int] = set()
for index in pipeline._strong_layers:
layer = layer_modules[index]
for label, module in (
("attention", get_attention_module(layer, arch)),
("ffn", get_ffn_module(layer, arch)),
):
for name, parameter in module.named_parameters(recurse=True):
if id(parameter) in seen:
continue
if parameter.device.type == "meta":
raise ValueError(
"generic Bayesian execution does not support offloaded parameters; "
"inject an architecture execution adapter"
)
if not parameter.is_floating_point() or hasattr(parameter, "quant_state"):
raise ValueError(
f"generic Bayesian execution cannot snapshot {label}.{name} "
f"with storage {type(parameter).__name__}/{parameter.dtype}"
)
selected.append((f"layers.{index}.{label}.{name}", parameter))
seen.add(id(parameter))
model = pipeline.handle.model
for head_name in ("lm_head", "embed_out", "output"):
head = getattr(model, head_name, None)
parameter = getattr(head, "weight", None)
if parameter is None:
continue
if parameter.device.type == "meta":
raise ValueError(
"generic Bayesian execution does not support an offloaded LM head; "
"inject an architecture execution adapter"
)
if not parameter.is_floating_point() or hasattr(parameter, "quant_state"):
raise ValueError(
"generic Bayesian execution cannot snapshot the LM head with storage "
f"{type(parameter).__name__}/{parameter.dtype}"
)
self._lm_head_name = head_name
if id(parameter) not in seen:
selected.append((f"global.{head_name}.weight", parameter))
seen.add(id(parameter))
break
if not selected:
raise ValueError("Bayesian execution found no reversible mutation targets")
self._selected = selected
self.snapshot_host_bytes = sum(
parameter.numel() * parameter.element_size() for _, parameter in selected
)
self.snapshot_disk_bytes = 0
self._scope_sha256 = None
def capture(self) -> None:
if self._released:
raise RuntimeError("Bayesian execution snapshots have been released")
if self._snapshots:
raise RuntimeError("Bayesian execution was captured more than once")
self._snapshots = [
(name, parameter, clone_tensor_to_cpu(parameter))
for name, parameter in self._selected
]
self._scope_sha256 = self._snapshot_sha256()
def _snapshot_sha256(self) -> str:
digest = hashlib.sha256()
for name, _parameter, snapshot in self._snapshots:
digest.update(name.encode())
digest.update(tensor_sha256(snapshot).encode())
return digest.hexdigest()
def _live_sha256(self) -> str:
digest = hashlib.sha256()
for name, parameter, _snapshot in self._snapshots:
digest.update(name.encode())
digest.update(tensor_sha256(parameter).encode())
return digest.hexdigest()
def identity(self) -> Mapping[str, Any]:
return {
"kind": "generic-resident-v1",
"snapshot_host_bytes": self.snapshot_host_bytes,
"snapshot_disk_bytes": self.snapshot_disk_bytes,
"mutation_scope_sha256": self._scope_sha256,
"targets": [name for name, _parameter in self._selected],
"projection_target": self.projection_target,
"project_biases": self.project_biases,
"norm_preserve": self.norm_preserve,
"lm_head_name": self._lm_head_name,
"lm_head_regularization": 0.0,
}
def restore(self) -> None:
if self._released:
raise RuntimeError("Bayesian execution snapshots have been released")
try:
for name, parameter, snapshot in self._snapshots:
if parameter.is_meta or snapshot.is_meta:
raise RuntimeError(f"Bayesian snapshot {name!r} is not materialized")
if parameter.shape != snapshot.shape or parameter.dtype != snapshot.dtype:
raise RuntimeError(f"Bayesian snapshot contract changed for {name!r}")
updates = [
(
parameter,
validated_weight_update(
snapshot, parameter, context=f"Bayesian rollback {name}"
),
)
for name, parameter, snapshot in self._snapshots
]
with torch.no_grad():
for parameter, value in updates:
copy_tensor_bounded_(parameter, value)
if self._live_sha256() != self._scope_sha256:
raise RuntimeError("Bayesian rollback did not restore the captured state")
except BaseException as error:
model = getattr(getattr(self.pipeline, "handle", None), "model", None)
if model is not None:
model._obliteratus_surgery_invalid = True
raise RuntimeError(
"Bayesian rollback failed; discard and reload the model"
) from error
def apply(self, plan: BayesianTrialPlan) -> BayesianApplyResult:
if self._released:
raise RuntimeError("Bayesian execution snapshots have been released")
expected_policy = {
"projection_target": self.projection_target,
"project_biases": self.project_biases,
"norm_preserve": self.norm_preserve,
"projection_row_fraction": float(
getattr(self.pipeline, "projection_row_fraction", 1.0)
),
"refinement_passes": int(getattr(self.pipeline, "refinement_passes", 1)),
"true_iterative_refinement": bool(
getattr(self.pipeline, "true_iterative_refinement", False)
),
"executor": dict(self.identity()),
}
if dict(plan.policy) != expected_policy:
raise RuntimeError("Bayesian plan policy differs from the execution contract")
applied = 0
touched: set[str] = set()
for index in self.pipeline._strong_layers:
if index not in plan.directions:
raise ValueError(f"Bayesian plan has no direction for layer {index}")
layer = self.layer_modules[index]
direction = plan.directions[index]
device = next(layer.parameters()).device
d_col = direction.to(device=device)
d_col = d_col.unsqueeze(-1) if d_col.ndim == 1 else d_col
scales = self.folded_scales.get(index, {})
attention = self._get_attention_module(layer, self.arch)
config = getattr(self.pipeline.handle, "config", None)
text_config = getattr(config, "text_config", None)
shared_kv = getattr(
text_config or config,
"num_kv_shared_layers",
getattr(config, "num_kv_shared_layers", 0),
) or 0
attn_names = self._attention_projection_names(
projection_target=self.projection_target,
layer_index=index,
num_layers=len(self.layer_modules),
num_kv_shared_layers=shared_kv,
output_names=self._attn_names,
input_names=self._attn_input_names,
)
attn_count = self.pipeline._project_out_advanced(
attention,
d_col,
attn_names,
norm_preserve=self.norm_preserve,
regularization=plan.attention_regularization[index],
**(
{
"projection_row_fraction": self.pipeline.projection_row_fraction,
"post_norm_scales": {
name: scales["attention"] for name in self._attn_names
},
}
if scales
else {}
),
)
if self.project_biases:
attn_count += self.pipeline._project_bias(
attention,
d_col,
attn_names,
post_norm_scales={
name: scales.get("attention") for name in self._attn_names
},
offload_roots=(layer,),
)
ffn = self._get_ffn_module(layer, self.arch)
mlp_count = self.pipeline._project_out_advanced(
ffn,
d_col,
self._ffn_names,
norm_preserve=self.norm_preserve,
regularization=plan.mlp_regularization[index],
**(
{
"projection_row_fraction": self.pipeline.projection_row_fraction,
"post_norm_scales": {
name: scales["ffn"] for name in self._ffn_names
},
}
if scales
else {}
),
)
if mlp_count == 0:
mlp_count = self.pipeline._project_moe_experts(
ffn,
d_col,
norm_preserve=self.norm_preserve,
regularization=plan.mlp_regularization[index],
project_biases=self.project_biases,
projection_row_fraction=getattr(
self.pipeline, "projection_row_fraction", 1.0
),
)
elif self.projection_target != "output":
mlp_count += self.pipeline._project_out_advanced(
ffn,
d_col,
self._ffn_input_names,
norm_preserve=self.norm_preserve,
regularization=plan.mlp_regularization[index],
projection_row_fraction=getattr(
self.pipeline, "projection_row_fraction", 1.0
),
)
if self.project_biases:
mlp_count += self.pipeline._project_bias(
ffn,
d_col,
self._ffn_names + self._ffn_input_names,
post_norm_scales={
name: scales.get("ffn") for name in self._ffn_names
},
offload_roots=(layer,),
)
elif self.project_biases:
mlp_count += self.pipeline._project_bias(
ffn,
d_col,
self._ffn_names,
post_norm_scales={
name: scales.get("ffn") for name in self._ffn_names
},
offload_roots=(layer,),
)
if attn_count <= 0 or mlp_count <= 0:
raise ValueError(
f"Bayesian plan did not mutate both components at layer {index}: "
f"attention={attn_count}, mlp={mlp_count}"
)
applied += attn_count + mlp_count
if self._lm_head_name is not None:
last_strong = max(self.pipeline._strong_layers)
direction = plan.directions[last_strong]
model = self.pipeline.handle.model
head = getattr(model, self._lm_head_name)
d_col = direction.to(device=head.weight.device)
d_col = d_col.unsqueeze(-1) if d_col.ndim == 1 else d_col
applied += self.pipeline._project_out_advanced(
model,
d_col,
[self._lm_head_name],
norm_preserve=self.norm_preserve,
regularization=0.0,
projection_row_fraction=float(
getattr(self.pipeline, "projection_row_fraction", 1.0)
),
)
for name, parameter, snapshot in self._snapshots:
value = parameter.detach() if parameter.device.type == "cpu" else clone_tensor_to_cpu(parameter)
if not torch.equal(value, snapshot):
touched.add(name)
del value
if not touched:
raise RuntimeError("Bayesian plan reported projections but changed no captured target")
return BayesianApplyResult(applied, tuple(sorted(touched)))
def release(self) -> None:
self._snapshots.clear()
self._selected.clear()
self._released = True
def create_execution(pipeline, layer_modules, arch, folded_scales) -> BayesianExecution:
"""Create and validate the injected or resident generic execution backend."""
factory = getattr(pipeline, "_bayesian_executor_factory", None)
execution = (
factory(pipeline)
if callable(factory)
else GenericBayesianExecution(pipeline, layer_modules, arch, folded_scales)
)
missing = [
name
for name in ("identity", "capture", "restore", "apply", "release")
if not callable(getattr(execution, name, None))
]
if missing:
raise TypeError(
f"Bayesian execution adapter has an invalid contract: missing {', '.join(missing)}"
)
for name in ("snapshot_host_bytes", "snapshot_disk_bytes"):
value = getattr(execution, name, None)
if type(value) is not int or value < 0:
raise TypeError(f"Bayesian execution {name} must be a nonnegative integer")
identity = execution.identity()
if not isinstance(identity, Mapping):
raise TypeError("Bayesian execution identity must be a mapping")
json.dumps(identity, sort_keys=True, allow_nan=False)
return execution