Files
OBLITERATUS/obliteratus/analysis/bayesian_kernel_projection.py
Joseph MaglyandClaude Opus 5.5 6aaeb813d7 fix(analysis): exact Gaussian W2 and honest labels for transfer and projection search
Finish the measurement-validity audit for the two remaining geometry
modules.

wasserstein_transfer:
- The Gaussian W2 paired sorted eigenvalues and underestimated the
  distance for non-commuting covariances (0 for a 45-degree rotation of
  diag(100, 1)). Compute the exact closed form with a stable PSD square
  root, from each side's own mean and covariance.
- Mismatched widths gave a truncated value and failures gave 0; both are
  now None with a reason. Failed map inversion no longer falls back to
  the raw cross-covariance. Fidelity is exact Pearson correlation.
- The "transport map" is a ridge least-squares map, not a Gaussian Monge
  map. Viability, fidelity and recommendations are labelled heuristics
  and are None unless their inputs exist; refusal-removal estimates that
  measured nothing are unsupported and always None.

bayesian_kernel_projection:
- Relabel as seeded random search with local perturbation over a
  partial linear projection (no surrogate or kernel). The Pareto front
  could keep a dominated trial on reduction ties; fixed. With no signal
  or no input, best results are None with a reason instead of score 0.

Both modules now carry measurement evidence and catalog rows checked by
the registry test, and the app overview no longer claims a Monge map or
TPE optimization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 12:22:50 -05:00

462 lines
20 KiB
Python

"""Search projection settings using retained activation samples.
Scores measure class separation and reconstruction on projected activations.
They do not measure model refusals, token KL or benchmark retention. Direction
counts are bounded by supplied basis rows; a single vector supports one direction.
Despite the historical names, the search is not Bayesian optimization (no
surrogate model or acquisition function: seeded random sampling, then
perturbation of a top-quartile trial) and the edit is a partial linear
projection, not a kernel method. "Best" means lowest sampled score. Results
carry ``measurement_evidence``; see docs/geometry-measurement-validity.md.
"""
from __future__ import annotations
import logging
import math
import random
from dataclasses import dataclass, field
import torch
from obliteratus.analysis.measurement_labels import attach_evidence
logger = logging.getLogger(__name__)
@dataclass
class ProjectionConfig:
"""A single trial configuration for kernel projection."""
layer_range: tuple[int, int] # (start, end) inclusive
per_layer_weights: dict[int, float] # projection weight per layer [0, 1]
n_directions: int # SVD directions to use
regularization: float # L2 regularization strength
norm_preserve: bool # whether to preserve norms
@dataclass
class TrialResult:
"""Result of evaluating a single projection configuration."""
config: ProjectionConfig
refusal_reduction: float | None # fraction of class-mean separation removed (proxy)
harmless_distortion: float # relative squared change on harmless rows (lower=better)
combined_score: float | None # weighted objective value
trial_idx: int
measurement_evidence: dict = field(default_factory=dict)
@dataclass
class BayesianOptimizationResult:
"""Result of the projection search (historical name; not Bayesian)."""
best_config: ProjectionConfig | None # lowest sampled score; None when unavailable
best_score: float | None
best_refusal_reduction: float | None
best_harmless_distortion: float | None
n_trials: int
all_trials: list[TrialResult]
# Analysis
pareto_configs: list[TrialResult] # non-dominated sampled trials
layer_importance: dict[int, float] # heuristic top-trial selection frequency
measurement_evidence: dict = field(default_factory=dict)
_SIGNAL_RTOL = 1e-6
_REDUCTION = ("Fraction of the summed per-layer norm of the class-mean difference on the full supplied "
"basis removed by the edit, clamped to [0, 1]; an activation proxy, not measured refusal.")
_DISTORTION = "Sum over edited layers of mean ||x' - x||^2 / mean ||x||^2 on harmless rows."
_SCORE = "refusal_weight * (1 - refusal_reduction) + distortion_weight * harmless_distortion; lower is better."
_TRIAL_CATALOG = {
"config": ("Sampled ProjectionConfig evaluated in this trial.", "configuration"),
"refusal_reduction": (_REDUCTION, "fraction"),
"harmless_distortion": (_DISTORTION, "relative squared error summed over layers"),
"combined_score": (_SCORE, "dimensionless"),
"trial_idx": ("Zero-based evaluation order.", "index"),
}
_RESULT_CATALOG = {
"best_config": ("Configuration of the sampled trial with the lowest combined_score; not a Bayesian "
"or global optimum.", "configuration"),
"best_score": ("combined_score of best_config. " + _SCORE, "dimensionless"),
"best_refusal_reduction": ("refusal_reduction of best_config. " + _REDUCTION, "fraction"),
"best_harmless_distortion": ("harmless_distortion of best_config. " + _DISTORTION,
"relative squared error summed over layers"),
"n_trials": ("Number of evaluated trials.", "count"),
"all_trials": ("Every evaluated TrialResult, in order, with its own evidence.", "list"),
"pareto_configs": ("Sampled trials not dominated in (higher refusal_reduction, lower "
"harmless_distortion), by decreasing refusal_reduction.", "list"),
"layer_importance": ("Heuristic: fraction of the lowest-score max(10, n // 10) trials (all when fewer) "
"with weight > 0.3 on the layer; a search frequency, not causal importance.",
"fraction per layer"),
}
_ASSUMPTIONS = ("Finite retained activation rows per layer; supplied basis rows orthonormalized by QR in "
"supplied order; harmful and harmless rows are not paired.")
_SCOPE = "Activation-space proxy only; no model refusal, token KL or benchmark measurement."
_NO_LAYERS = "No layer has harmful activations, harmless activations and a basis."
_NO_SIGNAL = ("The class-mean difference has no measurable component on the supplied basis (at most "
"1e-6 of the mean norms), so refusal_reduction is undefined and no configuration is selected.")
class BayesianKernelProjection:
"""Seeded search over projection settings (historical name; not Bayesian).
Random exploration is followed by perturbing a randomly chosen top-quartile
trial. The objective is an activation proxy; see the module docstring.
"""
def __init__(
self,
n_trials: int = 100,
refusal_weight: float = 0.6,
distortion_weight: float = 0.4,
seed: int = 42,
):
"""
Args:
n_trials: Number of optimization trials.
refusal_weight: Weight for refusal reduction in the objective (w_1).
distortion_weight: Weight for distortion penalty (w_2).
seed: Random seed for reproducibility.
"""
self.n_trials = n_trials
self.refusal_weight = refusal_weight
self.distortion_weight = distortion_weight
self.seed = seed
if type(n_trials) is not int or n_trials < 1:
raise ValueError("n_trials must be a positive integer")
if any(not math.isfinite(w) or w < 0 for w in (refusal_weight, distortion_weight)):
raise ValueError("objective weights must be finite and nonnegative")
if refusal_weight + distortion_weight <= 0:
raise ValueError("at least one objective weight must be positive")
self._rng = random.Random(seed)
def optimize(
self,
harmful_acts: dict[int, list[torch.Tensor]],
harmless_acts: dict[int, list[torch.Tensor]],
refusal_directions: dict[int, torch.Tensor],
max_directions: int = 8,
) -> BayesianOptimizationResult:
"""Search projection configurations and return the lowest sampled score.
Args:
harmful_acts: {layer_idx: [activations]} from harmful prompts.
harmless_acts: {layer_idx: [activations]} from harmless prompts.
refusal_directions: Per-layer vector [H] or ordered basis rows [K,H].
max_directions: Maximum supplied basis rows to consider.
Returns:
BayesianOptimizationResult; ``best_*`` are None when no layer is
usable or the basis carries no class-mean separation.
"""
if type(max_directions) is not int or max_directions < 1:
raise ValueError("max_directions must be a positive integer")
self._rng.seed(self.seed)
layers = sorted(set(harmful_acts.keys()) & set(harmless_acts.keys()) & set(refusal_directions.keys()))
n_layers = len(layers)
if n_layers == 0:
return self._unavailable(_NO_LAYERS)
# Pre-compute per-layer statistics for fast trial evaluation
layer_stats = self._precompute_stats(harmful_acts, harmless_acts, refusal_directions, layers)
if not _has_signal(layer_stats, layers):
return self._unavailable(_NO_SIGNAL)
max_directions = min(max_directions, *(len(st["basis"]) for st in layer_stats.values()))
# Phase 1: Random exploration (first 30% of trials)
n_explore = min(self.n_trials, max(int(self.n_trials * 0.3), 10))
trials = []
for i in range(n_explore):
config = self._random_config(layers, max_directions)
result = self._evaluate_trial(config, layer_stats, layers, i)
trials.append(result)
# Phase 2: TPE-inspired exploitation (remaining trials)
for i in range(n_explore, self.n_trials):
config = self._tpe_sample(trials, layers, max_directions)
result = self._evaluate_trial(config, layer_stats, layers, i)
trials.append(result)
# Find best
best = min(trials, key=lambda t: t.combined_score)
# Pareto front
pareto = self._pareto_front(trials)
# Layer importance: how often each layer appears in top-10 configs
top_10 = sorted(trials, key=lambda t: t.combined_score)[:max(10, len(trials) // 10)]
layer_importance = {}
for ly in layers:
count = sum(
1 for t in top_10
if t.config.per_layer_weights.get(ly, 0) > 0.3
)
layer_importance[ly] = count / len(top_10)
return attach_evidence(BayesianOptimizationResult(
best_config=best.config,
best_score=best.combined_score,
best_refusal_reduction=best.refusal_reduction,
best_harmless_distortion=best.harmless_distortion,
n_trials=len(trials),
all_trials=trials,
pareto_configs=pareto,
layer_importance=layer_importance,
), _RESULT_CATALOG, _ASSUMPTIONS, _SCOPE)
@staticmethod
def _unavailable(reason: str) -> BayesianOptimizationResult:
"""No trial is run and no configuration is recommended."""
result = BayesianOptimizationResult(None, None, None, None, 0, [], [], {})
return attach_evidence(result, _RESULT_CATALOG, _ASSUMPTIONS, _SCOPE,
reasons=dict.fromkeys(_RESULT_CATALOG, reason))
def _precompute_stats(
self,
harmful_acts: dict[int, list[torch.Tensor]],
harmless_acts: dict[int, list[torch.Tensor]],
refusal_directions: dict[int, torch.Tensor],
layers: list[int],
) -> dict:
"""Pre-compute per-layer statistics for fast trial evaluation."""
stats = {}
for ly in layers:
H = torch.stack([a.reshape(-1) for a in harmful_acts[ly]]).detach().float()
B = torch.stack([a.reshape(-1) for a in harmless_acts[ly]]).detach().float()
rows = refusal_directions[ly].detach().float()
if rows.ndim == 1:
rows = rows.unsqueeze(0)
if (rows.ndim != 2 or rows.shape[1] != H.shape[1]
or B.shape[1] != H.shape[1] or not 1 <= rows.shape[0] <= rows.shape[1]):
raise ValueError(f"layer {ly} has incompatible activation/basis shapes")
rows = rows.to(H.device)
B = B.to(H.device)
if any(not torch.isfinite(value).all() for value in (H, B, rows)):
raise ValueError(f"layer {ly} contains non-finite evidence")
q, r = torch.linalg.qr(rows.T, mode="reduced")
if (r.diag().abs() <= 1e-7).any():
raise ValueError(f"layer {ly} has dependent or empty basis directions")
basis = q.T
stats[ly] = {
"harmful": H, "harmless": B, "basis": basis,
"refusal_signal": ((H.mean(0) - B.mean(0)) @ basis.T).norm().item(),
"scale": max(H.mean(0).norm().item(), B.mean(0).norm().item()),
}
return stats
def _evaluate_trial(
self,
config: ProjectionConfig,
layer_stats: dict,
layers: list[int],
trial_idx: int,
) -> TrialResult:
"""Measure activation separation and reconstruction after the requested edit."""
if type(config.n_directions) is not int or config.n_directions < 1:
raise ValueError("n_directions must be a positive integer")
if (not math.isfinite(config.regularization)
or not 0 <= config.regularization <= 1):
raise ValueError("regularization must be between zero and one")
total_available = sum(layer_stats[ly]["refusal_signal"] for ly in layers)
total_removed = total_distortion = 0.0
start, end = config.layer_range
for ly in layers:
st = layer_stats[ly]
if config.n_directions > len(st["basis"]):
raise ValueError(f"layer {ly} has fewer supplied directions than requested")
if not start <= ly <= end:
continue
weight = config.per_layer_weights.get(ly, 0.0)
if not math.isfinite(weight) or not 0 <= weight <= 1:
raise ValueError("projection weights must be between zero and one")
basis = st["basis"][:config.n_directions]
strength = weight * (1.0 - config.regularization)
def projected(value):
result = value - strength * ((value @ basis.T) @ basis)
if config.norm_preserve:
ratio = value.norm(dim=1, keepdim=True) / result.norm(
dim=1, keepdim=True,
).clamp_min(1e-10)
result = result * ratio.clamp(max=1.1)
return result
harmful = projected(st["harmful"])
harmless = projected(st["harmless"])
remaining = ((harmful.mean(0) - harmless.mean(0)) @ st["basis"].T).norm().item()
total_removed += st["refusal_signal"] - remaining
total_distortion += ((harmless - st["harmless"]).square().sum(1).mean()
/ st["harmless"].square().sum(1).mean().clamp_min(1e-10)).item()
reduction = score = None
if _has_signal(layer_stats, layers):
reduction = min(1.0, max(0.0, total_removed / total_available))
score = self.refusal_weight * (1.0 - reduction) + self.distortion_weight * total_distortion
return attach_evidence(TrialResult(config, reduction, total_distortion, score, trial_idx),
_TRIAL_CATALOG, _ASSUMPTIONS, _SCOPE,
reasons=dict.fromkeys(("refusal_reduction", "combined_score"), _NO_SIGNAL))
def _random_config(
self, layers: list[int], max_directions: int,
) -> ProjectionConfig:
"""Generate a random projection configuration."""
n_layers = len(layers)
# Random layer range
start_idx = self._rng.randint(0, n_layers - 1)
end_idx = self._rng.randint(start_idx, n_layers - 1)
start = layers[start_idx]
end = layers[end_idx]
# Random per-layer weights
weights = {}
for ly in layers:
if start <= ly <= end:
weights[ly] = self._rng.uniform(0.0, 1.0)
else:
weights[ly] = 0.0
n_dirs = self._rng.randint(1, max_directions)
reg = self._rng.uniform(0.0, 0.5)
norm_preserve = self._rng.choice([True, False])
return ProjectionConfig(
layer_range=(start, end),
per_layer_weights=weights,
n_directions=n_dirs,
regularization=reg,
norm_preserve=norm_preserve,
)
def _tpe_sample(
self,
trials: list[TrialResult],
layers: list[int],
max_directions: int,
) -> ProjectionConfig:
"""TPE-inspired sampling: bias towards configurations similar to good trials."""
n_layers = len(layers)
# Split trials into good (bottom 25%) and bad (top 75%)
sorted_trials = sorted(trials, key=lambda t: t.combined_score)
n_good = max(1, len(sorted_trials) // 4)
good_trials = sorted_trials[:n_good]
# Sample layer range from good trials (with some noise)
ref = self._rng.choice(good_trials).config
try:
ref_start_idx = layers.index(ref.layer_range[0])
except ValueError:
ref_start_idx = 0
try:
ref_end_idx = layers.index(ref.layer_range[1])
except ValueError:
ref_end_idx = n_layers - 1
start_idx = max(0, min(n_layers - 1, ref_start_idx + self._rng.randint(-1, 1)))
end_idx = max(0, min(n_layers - 1, ref_end_idx + self._rng.randint(-1, 1)))
if start_idx > end_idx:
start_idx, end_idx = end_idx, start_idx
start = layers[start_idx]
end = layers[end_idx]
# Sample per-layer weights from good trial weights + noise
weights = {}
for ly in layers:
if start <= ly <= end:
base = ref.per_layer_weights.get(ly, 0.5)
w = max(0.0, min(1.0, base + self._rng.gauss(0, 0.15)))
weights[ly] = w
else:
weights[ly] = 0.0
n_dirs = max(1, min(max_directions, ref.n_directions + self._rng.randint(-1, 1)))
reg = max(0.0, min(0.5, ref.regularization + self._rng.gauss(0, 0.05)))
norm_preserve = ref.norm_preserve if self._rng.random() > 0.2 else (not ref.norm_preserve)
return ProjectionConfig(
layer_range=(start, end),
per_layer_weights=weights,
n_directions=n_dirs,
regularization=reg,
norm_preserve=norm_preserve,
)
def _pareto_front(self, trials: list[TrialResult]) -> list[TrialResult]:
"""Extract non-dominated sampled trials (refusal reduction vs distortion).
Ties in reduction sort by distortion, so a tied trial with larger
distortion (dominated) is never kept.
"""
pareto = []
sorted_by_refusal = sorted(trials, key=lambda t: (-t.refusal_reduction, t.harmless_distortion))
best_distortion = float('inf')
for t in sorted_by_refusal:
if t.harmless_distortion < best_distortion:
pareto.append(t)
best_distortion = t.harmless_distortion
return pareto
@staticmethod
def format_report(result: BayesianOptimizationResult) -> str:
"""Format projection search results."""
lines = []
lines.append("BayesianKernelProjection search (seeded sampling; not Bayesian optimization)")
lines.append("=" * 76)
lines.append("Activation proxies only: no model refusal, KL or benchmark was measured.")
lines.append("")
lines.append(f"Trials run: {result.n_trials}")
bc = result.best_config
if bc is None or result.best_score is None:
evidence = result.measurement_evidence.get("best_config")
lines.append("Best configuration: unavailable"
+ (f" ({evidence.unavailable_reason})" if evidence else ""))
return "\n".join(lines)
lines.append(f"Best score: {result.best_score:.6f}")
lines.append(f"Best refusal reduction (proxy): {result.best_refusal_reduction:.1%}")
lines.append(f"Best harmless distortion: {result.best_harmless_distortion:.6f}")
lines.append("")
lines.append("Best configuration:")
lines.append(f" Layer range: {bc.layer_range[0]} - {bc.layer_range[1]}")
lines.append(f" Directions: {bc.n_directions}")
lines.append(f" Regularization: {bc.regularization:.4f}")
lines.append(f" Norm preserve: {bc.norm_preserve}")
lines.append(" Per-layer weights:")
for ly in sorted(bc.per_layer_weights.keys()):
w = bc.per_layer_weights[ly]
if w > 0.01:
lines.append(f" Layer {ly:3d}: {w:.3f}")
lines.append("")
lines.append(f"Pareto-optimal sampled configs: {len(result.pareto_configs)}")
if result.pareto_configs:
lines.append(" Refusal ↑ Distortion ↓")
for p in result.pareto_configs[:5]:
lines.append(
f" {p.refusal_reduction:6.1%} {p.harmless_distortion:.6f}"
)
lines.append("")
if result.layer_importance:
lines.append("Layer importance (heuristic: fraction of top configs using each layer):")
for ly in sorted(result.layer_importance.keys()):
imp = result.layer_importance[ly]
bar = "#" * int(imp * 20)
lines.append(f" Layer {ly:3d}: {imp:.2f} {bar}")
return "\n".join(lines)
def _has_signal(layer_stats: dict, layers: list[int]) -> bool:
"""Whether the basis carries a class-mean separation above roundoff."""
signal = sum(layer_stats[ly]["refusal_signal"] for ly in layers)
return signal > _SIGNAL_RTOL * sum(layer_stats[ly]["scale"] for ly in layers)