mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
On Python 3.13+, an sqlite3 connection that is garbage-collected without being closed raises ResourceWarning, which the project's filterwarnings=error turns into test failures. pyproject declares requires-python >=3.11 with no upper bound, so these versions must work. - The durable study's RDBStorage is now owned by an ExitStack in both public entry points and closed (remove_session, then engine.dispose) on success, failure and cancellation. - _atomic_sqlite_backup closes both handles with contextlib.closing; sqlite3's own context manager only ends the transaction. - Two tests that reopened or connected to study databases now close their handles. - New regression test tracks every sqlite3 handle opened during a real durable study (success, failure, KeyboardInterrupt) and the backup, asserting all are closed independent of the 3.13+ warning. Storage URLs, file names, snapshots and resume behaviour are unchanged; as with any edit to this module, the study identity (which hashes the module source) changes, so pre-existing durable studies start fresh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1506 lines
59 KiB
Python
1506 lines
59 KiB
Python
"""Bayesian optimization for abliteration hyperparameters.
|
|
|
|
Implements Optuna TPE-based multi-objective optimization that searches for
|
|
optimal ablation parameters co-minimizing refusal rate and KL divergence.
|
|
|
|
Inspired by Heretic (p-e-w, 2025) which pioneered Bayesian optimization
|
|
for abliteration. OBLITERATUS pushes this further by:
|
|
|
|
1. **Parametric layer kernel**: Instead of per-layer independent parameters,
|
|
uses a bell-shaped curve described by 4 global params (Heretic-style).
|
|
This reduces the search space from O(n_layers) to O(1) while capturing
|
|
the spatial structure of refusal across layers.
|
|
2. **Float direction interpolation**: Direction index is continuous — non-
|
|
integer values interpolate between adjacent SVD directions, unlocking
|
|
a smooth direction space beyond the discrete top-k.
|
|
3. **Component-specific weights**: Separate scaling for attention vs MLP
|
|
projections (Heretic showed MLP interventions are more damaging).
|
|
4. **Per-expert granularity**: For MoE models, optimizes per-expert scaling.
|
|
5. **CoT-aware objectives**: Adds chain-of-thought coherence as a third
|
|
optimization objective for CoT models.
|
|
6. **Warm-start from analysis**: Uses OBLITERATUS's analysis-based heuristics
|
|
as initial trial suggestions instead of random initialization.
|
|
|
|
References:
|
|
- Heretic (p-e-w, 2025): Bayesian optimization for LLM abliteration
|
|
- Akiba et al. (2019): Optuna: A Next-generation Hyperparameter
|
|
Optimization Framework
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import ExitStack, closing
|
|
from dataclasses import dataclass
|
|
import hashlib
|
|
from importlib.metadata import PackageNotFoundError, version
|
|
import inspect
|
|
import json
|
|
import logging
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import tempfile
|
|
from typing import TYPE_CHECKING
|
|
|
|
import torch
|
|
import torch.nn.functional as F
|
|
|
|
from obliteratus.abliterate import PipelineQualityRejectedError
|
|
from obliteratus.execution_budget import SearchBudgetExhausted, admit_optional_work
|
|
from obliteratus.bayesian_execution import (
|
|
BayesianApplyResult,
|
|
BayesianTrialPlan,
|
|
create_execution,
|
|
tensor_sha256,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_STUDY_SCHEMA = 2
|
|
_SAMPLER_SEED = 42
|
|
|
|
|
|
class NoFeasibleTrialError(PipelineQualityRejectedError):
|
|
"""Every completed trial exceeded the configured KL budget."""
|
|
|
|
def __init__(self, kl_budget, study_identity, trial_values):
|
|
super().__init__(
|
|
"Bayesian optimization produced no completed trial within the "
|
|
f"configured KL budget {kl_budget:.6g}",
|
|
stage="excise", metric="kl_divergence",
|
|
)
|
|
self.kl_budget = kl_budget
|
|
self.study_identity = study_identity
|
|
self.trial_values = trial_values
|
|
|
|
|
|
def _dependency_versions() -> dict[str, str]:
|
|
result = {}
|
|
for package in ("optuna", "torch", "transformers"):
|
|
try:
|
|
result[package] = version(package)
|
|
except PackageNotFoundError:
|
|
result[package] = "unavailable"
|
|
return result
|
|
|
|
|
|
def _discard_bayesian_execution(pipeline: AbliterationPipeline, execution) -> None:
|
|
"""Restore and release a captured execution after an unsuccessful run."""
|
|
try:
|
|
if getattr(pipeline, "_bayesian_execution_captured", False):
|
|
execution.restore()
|
|
finally:
|
|
try:
|
|
execution.release()
|
|
finally:
|
|
pipeline._bayesian_execution = None
|
|
pipeline._bayesian_execution_captured = False
|
|
|
|
|
|
# Kernel search space, declared once. `objective` suggests from these bounds and
|
|
# the warm-start trial is clamped to them; keeping two copies is what let the
|
|
# warm start drift outside the space it was seeding (see `_clamp_to_space`).
|
|
KERNEL_SPACE: dict[str, tuple[float, float]] = {
|
|
"attn_max_weight": (0.5, 1.0),
|
|
"attn_peak_position": (0.1, 0.9),
|
|
"attn_min_weight": (0.0, 0.3),
|
|
"attn_spread": (0.1, 0.6),
|
|
"mlp_max_weight": (0.3, 1.0),
|
|
"mlp_peak_position": (0.1, 0.9),
|
|
"mlp_min_weight": (0.0, 0.3),
|
|
"mlp_spread": (0.1, 0.6),
|
|
}
|
|
|
|
|
|
def _reset_bayesian_state(pipeline: AbliterationPipeline) -> None:
|
|
"""Clear status and component overrides from an earlier attempt."""
|
|
execution = getattr(pipeline, "_bayesian_execution", None)
|
|
if execution is not None:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
pipeline._bayesian_skipped = None
|
|
pipeline._bayesian_attn_scale = None
|
|
pipeline._bayesian_mlp_scale = None
|
|
pipeline._bayesian_refusal_measurement = None
|
|
pipeline._bayesian_execution = None
|
|
pipeline._bayesian_execution_captured = False
|
|
pipeline._bayesian_plan = None
|
|
pipeline._bayesian_selected_plan_sha256 = None
|
|
pipeline._bayesian_scored_scope = None
|
|
pipeline._bayesian_search = None
|
|
|
|
|
|
def _skip(pipeline: AbliterationPipeline, reason: str, message: str) -> None:
|
|
"""Record and surface a no-op so it cannot be mistaken for a completed run.
|
|
|
|
Reported three ways because each reaches a different audience: the stdlib
|
|
logger for anyone capturing logs, the pipeline log for the console panel the
|
|
operator is watching, and an attribute the caller persists into checkpoint
|
|
metadata so the provenance of an already-written model still shows it.
|
|
"""
|
|
logger.warning(message)
|
|
log = getattr(pipeline, "log", None)
|
|
if callable(log):
|
|
log(f"WARNING: {message}")
|
|
try:
|
|
pipeline._bayesian_skipped = reason
|
|
except AttributeError: # pragma: no cover - pipeline stubs may use __slots__
|
|
pass
|
|
|
|
|
|
def _clamp_to_space(params: dict[str, float]) -> dict[str, float]:
|
|
"""Clamp warm-start values into the declared search space.
|
|
|
|
Optuna warns about an out-of-range enqueued value but still evaluates it.
|
|
Clamping keeps the informed starting point within the same bounds as later
|
|
sampled trials. This is reachable in normal use: `attn_peak_position` is
|
|
seeded from
|
|
`peak_layer / (n_layers - 1)`, which for an early peak layer falls below the
|
|
0.1 floor — a 36-layer model peaking at layer 2 seeds 0.057.
|
|
"""
|
|
out = dict(params)
|
|
for name, (lo, hi) in KERNEL_SPACE.items():
|
|
if name in out:
|
|
out[name] = min(max(float(out[name]), lo), hi)
|
|
return out
|
|
|
|
|
|
def _measure_refusal_rate(
|
|
pipeline: AbliterationPipeline,
|
|
n_prompts: int = 10,
|
|
max_new_tokens: int = 128,
|
|
) -> float:
|
|
"""Measure refusal plus invalid-output rate on a subset of harmful prompts.
|
|
|
|
Applies the model's chat template (when available) so the model is in
|
|
chat mode — matching how users actually interact with it. Uses token-ID
|
|
slicing for robust completion extraction regardless of special tokens.
|
|
"""
|
|
from obliteratus.evaluation.advanced_metrics import measure_refusals
|
|
|
|
model = pipeline.handle.model
|
|
tokenizer = pipeline.handle.tokenizer
|
|
device = pipeline._get_model_device(model)
|
|
|
|
test_prompts = pipeline.harmful_prompts[:n_prompts]
|
|
|
|
# Always apply chat template for refusal measurement — refusal circuitry
|
|
# is activated by chat formatting, so raw prompts give false 0% rates.
|
|
saved_flag = pipeline.use_chat_template
|
|
try:
|
|
pipeline.use_chat_template = True
|
|
formatted_prompts = pipeline._maybe_apply_chat_template(test_prompts)
|
|
finally:
|
|
pipeline.use_chat_template = saved_flag
|
|
responses = []
|
|
|
|
for prompt in formatted_prompts:
|
|
try:
|
|
inputs = tokenizer(
|
|
prompt, return_tensors="pt", truncation=True, max_length=256,
|
|
)
|
|
input_len = inputs["input_ids"].shape[1]
|
|
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
with torch.no_grad():
|
|
output = model.generate(
|
|
**inputs, max_new_tokens=max_new_tokens, do_sample=False,
|
|
)
|
|
# Token-ID slicing: robust regardless of special tokens
|
|
completion_ids = output[0][input_len:]
|
|
completion = tokenizer.decode(
|
|
completion_ids, skip_special_tokens=True,
|
|
).strip()
|
|
responses.append(completion)
|
|
del inputs, output
|
|
except torch.OutOfMemoryError:
|
|
raise
|
|
except Exception:
|
|
responses.append("") # Generation failures are invalid output.
|
|
|
|
pipeline._free_gpu_memory()
|
|
measurement = measure_refusals(responses, mode="combined")
|
|
pipeline._bayesian_refusal_measurement = measurement
|
|
return (
|
|
(measurement.refused + measurement.invalid) / measurement.total
|
|
if measurement.total else 1.0
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class _KLEvidence:
|
|
prompts: tuple[str, ...]
|
|
reference_logits: tuple[torch.Tensor, ...]
|
|
metric: str
|
|
|
|
def identity(self) -> dict[str, object]:
|
|
return {
|
|
"metric": self.metric,
|
|
"prompts_sha256": hashlib.sha256(
|
|
json.dumps(self.prompts, ensure_ascii=False, separators=(",", ":")).encode()
|
|
).hexdigest(),
|
|
"reference_sha256": [tensor_sha256(value) for value in self.reference_logits],
|
|
}
|
|
|
|
|
|
def _measure_kl_divergence(
|
|
pipeline: AbliterationPipeline,
|
|
reference_logits: list[torch.Tensor] | tuple[torch.Tensor, ...],
|
|
prompts: list[str] | tuple[str, ...],
|
|
) -> float:
|
|
"""Measure prompt-bound forward KL and fail closed on missing evidence."""
|
|
if not prompts or len(reference_logits) != len(prompts):
|
|
raise RuntimeError("Bayesian KL evidence does not match its prompt identities")
|
|
model = pipeline.handle.model
|
|
tokenizer = pipeline.handle.tokenizer
|
|
device = pipeline._get_model_device(model)
|
|
|
|
total_kl = 0.0
|
|
total_tokens = 0
|
|
|
|
try:
|
|
for prompt, reference in zip(prompts, reference_logits, strict=True):
|
|
inputs = tokenizer(
|
|
prompt,
|
|
return_tensors="pt",
|
|
truncation=True,
|
|
max_length=getattr(pipeline, "max_seq_length", None) or 64,
|
|
)
|
|
inputs = {key: value.to(device) for key, value in inputs.items()}
|
|
with torch.no_grad():
|
|
current = model(**inputs).logits[0].detach().cpu().float()
|
|
pristine = reference.detach().cpu().float()
|
|
if pristine.ndim == 1:
|
|
current = current[-1]
|
|
if current.shape != pristine.shape:
|
|
raise RuntimeError(
|
|
"Bayesian KL logits changed shape: "
|
|
f"{tuple(current.shape)} != {tuple(pristine.shape)}"
|
|
)
|
|
if not torch.isfinite(current).all() or not torch.isfinite(pristine).all():
|
|
raise RuntimeError("Bayesian KL logits contain non-finite values")
|
|
log_p = F.log_softmax(pristine, dim=-1)
|
|
log_q = F.log_softmax(current, dim=-1)
|
|
per_token = F.kl_div(log_q, log_p, log_target=True, reduction="none")
|
|
per_token = per_token.sum(dim=-1).clamp(min=0.0)
|
|
total_kl += per_token.sum().item()
|
|
total_tokens += per_token.numel()
|
|
del inputs, current, pristine, log_p, log_q, per_token
|
|
finally:
|
|
pipeline._free_gpu_memory()
|
|
|
|
if total_tokens <= 0:
|
|
raise RuntimeError("Bayesian KL measurement produced no token evidence")
|
|
measured = total_kl / total_tokens
|
|
if not torch.isfinite(torch.tensor(measured)):
|
|
raise RuntimeError("Bayesian KL measurement is non-finite")
|
|
return measured
|
|
|
|
|
|
def _capture_kl_evidence(
|
|
pipeline: AbliterationPipeline,
|
|
n_prompts: int,
|
|
) -> tuple[_KLEvidence, object | None]:
|
|
"""Bind pristine logits to exact prompt identities before any trial write."""
|
|
native_prompts = tuple(getattr(pipeline, "_kl_eval_prompts", ()) or ())
|
|
native_logits = tuple(getattr(pipeline, "_baseline_token_logits", ()) or ())
|
|
native_measure = getattr(pipeline, "_measure_sequence_token_kl", None)
|
|
if native_prompts or native_logits:
|
|
if (
|
|
not native_prompts
|
|
or len(native_prompts) != len(native_logits)
|
|
or not callable(native_measure)
|
|
):
|
|
raise RuntimeError("native Bayesian KL baseline is incomplete")
|
|
evidence = _KLEvidence(
|
|
native_prompts,
|
|
tuple(value.detach().cpu() for value in native_logits),
|
|
"native_sequence_token_forward_kl_nats",
|
|
)
|
|
return evidence, native_measure
|
|
|
|
raw_prompts = [
|
|
"The weather today is",
|
|
"In mathematics, the concept of",
|
|
"The history of ancient Rome",
|
|
"How to cook a simple pasta",
|
|
"The process of photosynthesis involves",
|
|
][:n_prompts]
|
|
if not raw_prompts:
|
|
raise RuntimeError("Bayesian KL requires at least one prompt")
|
|
formatter = getattr(pipeline, "_maybe_apply_chat_template", None)
|
|
prompts = tuple(formatter(raw_prompts) if callable(formatter) else raw_prompts)
|
|
model = pipeline.handle.model
|
|
tokenizer = pipeline.handle.tokenizer
|
|
device = pipeline._get_model_device(model)
|
|
references: list[torch.Tensor] = []
|
|
try:
|
|
for prompt in prompts:
|
|
inputs = tokenizer(
|
|
prompt,
|
|
return_tensors="pt",
|
|
truncation=True,
|
|
max_length=getattr(pipeline, "max_seq_length", None) or 64,
|
|
)
|
|
inputs = {key: value.to(device) for key, value in inputs.items()}
|
|
with torch.no_grad():
|
|
logits = model(**inputs).logits[0].detach().cpu()
|
|
if not logits.numel() or not torch.isfinite(logits.float()).all():
|
|
raise RuntimeError("Bayesian pristine KL logits are empty or non-finite")
|
|
references.append(logits)
|
|
finally:
|
|
pipeline._free_gpu_memory()
|
|
if len(references) != len(prompts):
|
|
raise RuntimeError("Bayesian KL capture lost prompt identity")
|
|
return _KLEvidence(prompts, tuple(references), "sequence_token_forward_kl_nats"), None
|
|
|
|
|
|
def _parametric_layer_weight(
|
|
layer_idx: int,
|
|
n_layers: int,
|
|
max_weight: float,
|
|
peak_position: float,
|
|
min_weight: float,
|
|
spread: float,
|
|
) -> float:
|
|
"""Compute ablation weight for a layer using a piecewise-linear tent kernel.
|
|
|
|
Faithful reproduction of Heretic's parametric kernel (p-e-w/heretic):
|
|
- max_weight: peak ablation strength at peak_position
|
|
- peak_position: normalized position of peak (0..1)
|
|
- min_weight: weight at the edges of the tent
|
|
- spread: normalized distance from peak to tent edge (min_weight_distance)
|
|
|
|
Layers beyond ``spread`` from the peak get weight 0 (skipped entirely).
|
|
Within the tent, weight drops linearly from max_weight to min_weight.
|
|
This matches Heretic's actual formula::
|
|
|
|
distance = abs(layer_index - max_weight_position)
|
|
if distance > min_weight_distance: skip
|
|
weight = max_weight + (distance / min_weight_distance) * (min_weight - max_weight)
|
|
"""
|
|
if n_layers <= 1:
|
|
return max_weight
|
|
|
|
normalized_pos = layer_idx / (n_layers - 1)
|
|
dist = abs(normalized_pos - peak_position)
|
|
min_weight_distance = max(spread, 0.01)
|
|
|
|
# Hard cutoff: layers outside the tent get 0 weight (Heretic skips them)
|
|
if dist > min_weight_distance:
|
|
return 0.0
|
|
|
|
# Linear interpolation: max_weight at peak → min_weight at edges
|
|
return max_weight + (dist / min_weight_distance) * (min_weight - max_weight)
|
|
|
|
|
|
def _direction_source_layers(pipeline: AbliterationPipeline) -> list[int]:
|
|
"""Return the ordered source domain, independent of edited target layers."""
|
|
return sorted(pipeline.refusal_directions)
|
|
|
|
|
|
def _interpolate_direction(
|
|
pipeline: AbliterationPipeline,
|
|
layer_idx: int,
|
|
float_dir_idx: float,
|
|
) -> torch.Tensor:
|
|
"""Get an interpolated refusal direction from a float-valued layer index.
|
|
|
|
Faithful reproduction of Heretic's direction interpolation: the index
|
|
selects which *layer's* diff-of-means direction to use, with float
|
|
values interpolating between adjacent layers' directions. This is
|
|
fundamentally different from interpolating between SVD components
|
|
within a single layer — it searches across the layer axis.
|
|
|
|
From Heretic source (model.py)::
|
|
|
|
weight, index = math.modf(direction_index + 1)
|
|
refusal_direction = F.normalize(
|
|
refusal_directions[int(index)].lerp(
|
|
refusal_directions[int(index) + 1], weight), p=2, dim=0)
|
|
|
|
Args:
|
|
pipeline: Pipeline with extracted refusal directions per layer.
|
|
layer_idx: The layer being projected (used as fallback).
|
|
float_dir_idx: Continuous direction index — selects which layer's
|
|
direction to use (e.g., 5.3 interpolates 70% layer-5 + 30% layer-6).
|
|
|
|
Returns:
|
|
Normalized direction tensor.
|
|
"""
|
|
# Build sorted list of layer indices that have refusal directions
|
|
sorted_layers = _direction_source_layers(pipeline)
|
|
if not sorted_layers:
|
|
return pipeline.refusal_directions.get(layer_idx, torch.zeros(1))
|
|
|
|
n_layers_with_dirs = len(sorted_layers)
|
|
|
|
# Heretic uses direction_index + 1 offset; we map float_dir_idx into
|
|
# the sorted layer list, clamped to valid range.
|
|
float_dir_idx = max(0.0, min(float_dir_idx, n_layers_with_dirs - 1))
|
|
|
|
lo = int(float_dir_idx)
|
|
hi = min(lo + 1, n_layers_with_dirs - 1)
|
|
|
|
lo_layer = sorted_layers[lo]
|
|
hi_layer = sorted_layers[hi]
|
|
|
|
d_lo = pipeline.refusal_directions[lo_layer]
|
|
d_hi = pipeline.refusal_directions[hi_layer]
|
|
|
|
if lo == hi:
|
|
d = d_lo
|
|
else:
|
|
# Linear interpolation between adjacent layers' directions
|
|
alpha = float_dir_idx - lo
|
|
d = (1.0 - alpha) * d_lo + alpha * d_hi
|
|
|
|
norm = d.norm()
|
|
if norm > 1e-8:
|
|
d = d / norm
|
|
return d
|
|
|
|
|
|
def _resolve_trial_plan(
|
|
pipeline: AbliterationPipeline,
|
|
params: dict[str, float],
|
|
*,
|
|
n_total_layers: int,
|
|
execution_identity: dict[str, object] | None = None,
|
|
) -> BayesianTrialPlan:
|
|
"""Resolve sampled parameters once so scoring and replay share exact values."""
|
|
attn_regs: dict[int, float] = {}
|
|
mlp_regs: dict[int, float] = {}
|
|
directions: dict[int, torch.Tensor] = {}
|
|
for index in pipeline._strong_layers:
|
|
attn_weight = _parametric_layer_weight(
|
|
index,
|
|
n_total_layers,
|
|
params["attn_max_weight"],
|
|
params["attn_peak_position"],
|
|
params["attn_min_weight"],
|
|
params["attn_spread"],
|
|
)
|
|
mlp_weight = _parametric_layer_weight(
|
|
index,
|
|
n_total_layers,
|
|
params["mlp_max_weight"],
|
|
params["mlp_peak_position"],
|
|
params["mlp_min_weight"],
|
|
params["mlp_spread"],
|
|
)
|
|
attn_regs[index] = 1.0 - attn_weight
|
|
mlp_regs[index] = 1.0 - mlp_weight
|
|
directions[index] = _interpolate_direction(
|
|
pipeline, index, params["dir_idx"]
|
|
).detach().to(device="cpu", copy=True)
|
|
policy = {
|
|
"projection_target": getattr(pipeline, "projection_target", "output"),
|
|
"project_biases": bool(getattr(pipeline, "project_biases", False)),
|
|
"norm_preserve": bool(getattr(pipeline, "norm_preserve", False)),
|
|
"projection_row_fraction": float(
|
|
getattr(pipeline, "projection_row_fraction", 1.0)
|
|
),
|
|
"refinement_passes": int(getattr(pipeline, "refinement_passes", 1)),
|
|
"true_iterative_refinement": bool(
|
|
getattr(pipeline, "true_iterative_refinement", False)
|
|
),
|
|
"executor": _json_identity(execution_identity or {}),
|
|
}
|
|
return BayesianTrialPlan(dict(params), attn_regs, mlp_regs, directions, policy)
|
|
|
|
|
|
def _json_identity(value) -> object:
|
|
"""Convert config/provenance values into a stable JSON identity."""
|
|
if value is None or isinstance(value, (str, int, bool)):
|
|
return value
|
|
if isinstance(value, float):
|
|
if not torch.isfinite(torch.tensor(value)):
|
|
raise ValueError("Bayesian study identity contains a non-finite float")
|
|
return value
|
|
if isinstance(value, dict):
|
|
return {
|
|
str(key): _json_identity(item)
|
|
for key, item in sorted(value.items(), key=lambda pair: str(pair[0]))
|
|
}
|
|
if isinstance(value, (list, tuple)):
|
|
return [_json_identity(item) for item in value]
|
|
return str(value)
|
|
|
|
|
|
def _study_identity(
|
|
pipeline: AbliterationPipeline,
|
|
execution,
|
|
evidence: _KLEvidence,
|
|
*,
|
|
n_refusal_prompts: int,
|
|
) -> tuple[str, dict[str, object]]:
|
|
config = getattr(pipeline.handle, "config", None)
|
|
config_identity = config.to_dict() if callable(getattr(config, "to_dict", None)) else {}
|
|
optimizer_source = inspect.getsource(_run_bayesian_optimization)
|
|
payload = {
|
|
"schema": _STUDY_SCHEMA,
|
|
"model_name": str(getattr(pipeline, "model_name", "")),
|
|
"method": str(getattr(pipeline, "method", "")),
|
|
"seed": int(getattr(pipeline, "seed", None) or 0),
|
|
"dependencies": _dependency_versions(),
|
|
"pipeline_options": _json_identity({
|
|
name: getattr(pipeline, name, None)
|
|
for name in (
|
|
"projection_target",
|
|
"project_biases",
|
|
"norm_preserve",
|
|
"projection_row_fraction",
|
|
"fold_post_norms",
|
|
"use_chat_template",
|
|
"refinement_passes",
|
|
"true_iterative_refinement",
|
|
)
|
|
}),
|
|
"informed_warm_start": _json_identity(
|
|
getattr(pipeline, "_informed_warm_start", None)
|
|
),
|
|
"config": _json_identity(config_identity),
|
|
"strong_layers": [int(index) for index in pipeline._strong_layers],
|
|
"direction_source_layers": [int(index) for index in _direction_source_layers(pipeline)],
|
|
"concept_subspaces": {
|
|
str(index): tensor_sha256(value)
|
|
for index, value in sorted(
|
|
getattr(pipeline, "concept_subspaces", {}).items()
|
|
)
|
|
},
|
|
"directions": {
|
|
str(index): tensor_sha256(value)
|
|
for index, value in sorted(pipeline.refusal_directions.items())
|
|
},
|
|
"harmful_prompts_sha256": hashlib.sha256(
|
|
json.dumps(
|
|
pipeline.harmful_prompts[:n_refusal_prompts],
|
|
ensure_ascii=False,
|
|
separators=(",", ":"),
|
|
).encode()
|
|
).hexdigest(),
|
|
"kl_evidence": evidence.identity(),
|
|
"execution": _json_identity(execution.identity()),
|
|
"optimizer_source_sha256": hashlib.sha256(optimizer_source.encode()).hexdigest(),
|
|
"module_source_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
|
|
"execution_source_sha256": hashlib.sha256(
|
|
Path(inspect.getfile(BayesianTrialPlan)).read_bytes()
|
|
).hexdigest(),
|
|
}
|
|
encoded = json.dumps(payload, sort_keys=True, separators=(",", ":"), allow_nan=False)
|
|
return hashlib.sha256(encoded.encode()).hexdigest(), payload
|
|
|
|
|
|
def _atomic_json(path: Path, payload: dict[str, object]) -> None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
fd, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
|
|
try:
|
|
with os.fdopen(fd, "w", encoding="utf-8") as stream:
|
|
json.dump(payload, stream, sort_keys=True, indent=2, allow_nan=False)
|
|
stream.write("\n")
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
os.replace(temporary, path)
|
|
except BaseException:
|
|
try:
|
|
os.unlink(temporary)
|
|
except FileNotFoundError:
|
|
pass
|
|
raise
|
|
|
|
|
|
def _atomic_sqlite_backup(source: Path, destination: Path) -> None:
|
|
"""Publish a consistent SQLite snapshot without copying a live WAL file."""
|
|
fd, temporary = tempfile.mkstemp(prefix=f".{destination.name}.", dir=destination.parent)
|
|
os.close(fd)
|
|
try:
|
|
# sqlite3's own context manager only ends the transaction; ``closing``
|
|
# releases the handles deterministically (Python 3.13+ warns otherwise).
|
|
with closing(sqlite3.connect(source)) as live, closing(
|
|
sqlite3.connect(temporary)
|
|
) as stable:
|
|
live.backup(stable)
|
|
descriptor = os.open(temporary, os.O_RDONLY)
|
|
try:
|
|
os.fsync(descriptor)
|
|
finally:
|
|
os.close(descriptor)
|
|
os.replace(temporary, destination)
|
|
except BaseException:
|
|
try:
|
|
os.unlink(temporary)
|
|
except FileNotFoundError:
|
|
pass
|
|
raise
|
|
|
|
|
|
def _close_rdb_storage(storage) -> None:
|
|
"""Return the session's connections and close every pooled SQLite handle."""
|
|
try:
|
|
remove_session = getattr(storage, "remove_session", None)
|
|
if callable(remove_session):
|
|
remove_session()
|
|
finally:
|
|
engine = getattr(storage, "engine", None)
|
|
if engine is not None:
|
|
engine.dispose()
|
|
|
|
|
|
def _durable_study_options(
|
|
optuna, pipeline, identity: str, resources: ExitStack
|
|
) -> tuple[dict, Path | None]:
|
|
"""Open the durable study storage; ``resources`` owns closing it."""
|
|
requested_root = getattr(pipeline, "_bayesian_study_path", None)
|
|
output_dir = getattr(pipeline, "output_dir", None)
|
|
storages = getattr(optuna, "storages", None)
|
|
if requested_root is None and output_dir is None:
|
|
return {}, None
|
|
if storages is None or not hasattr(storages, "RDBStorage"):
|
|
return {}, None
|
|
root = (
|
|
Path(requested_root)
|
|
if requested_root is not None
|
|
else Path(output_dir) / ".obliteratus-bayesian"
|
|
)
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
database = root / f"{identity}.sqlite3"
|
|
storage = storages.RDBStorage(url=f"sqlite:///{database}")
|
|
resources.callback(_close_rdb_storage, storage)
|
|
return {"storage": storage, "load_if_exists": True}, root / f"{identity}.json"
|
|
|
|
|
|
def _persist_completed_trial(
|
|
pipeline,
|
|
trial,
|
|
*,
|
|
study_identity: str,
|
|
evidence_path: Path | None,
|
|
) -> None:
|
|
state = getattr(getattr(trial, "state", None), "name", None)
|
|
if state is not None and state != "COMPLETE":
|
|
return
|
|
values = getattr(trial, "values", None)
|
|
attrs = dict(getattr(trial, "user_attrs", {}) or {})
|
|
if values is None or attrs.get("plan_sha256") is None:
|
|
return
|
|
from optuna.distributions import distribution_to_json
|
|
|
|
backup_path = None
|
|
if evidence_path is not None:
|
|
database = evidence_path.with_suffix(".sqlite3")
|
|
if database.exists():
|
|
backup_path = evidence_path.with_name(f"{study_identity}.sqlite3.snapshot")
|
|
_atomic_sqlite_backup(database, backup_path)
|
|
payload = {
|
|
"schema": _STUDY_SCHEMA,
|
|
"study_identity": study_identity,
|
|
"trial_number": int(trial.number),
|
|
"state": "COMPLETE",
|
|
"values": [float(value) for value in values],
|
|
"parameters": {key: float(value) for key, value in sorted(trial.params.items())},
|
|
"distributions": {
|
|
key: distribution_to_json(value)
|
|
for key, value in sorted(trial.distributions.items())
|
|
},
|
|
"user_attrs": attrs,
|
|
"sampler": {"name": "TPESampler", "seed": _SAMPLER_SEED},
|
|
"study_backup": backup_path.name if backup_path is not None else None,
|
|
}
|
|
trial_path = None
|
|
if evidence_path is not None:
|
|
trial_path = evidence_path.with_name(
|
|
f"{study_identity}.trial-{trial.number}.json"
|
|
)
|
|
_atomic_json(trial_path, payload)
|
|
callback = getattr(pipeline, "_bayesian_trial_checkpoint", None)
|
|
if callable(callback):
|
|
callback(payload)
|
|
publish = getattr(pipeline, "_on_bayesian_trial", None)
|
|
if callable(publish):
|
|
if trial_path is None:
|
|
raise RuntimeError("Bayesian trial publication requires durable local evidence")
|
|
publish(trial_path)
|
|
|
|
|
|
def _restore_completed_trials(optuna, study, evidence_path: Path | None, identity: str) -> None:
|
|
"""Reconcile a durable study with identity-bound atomic trial rows."""
|
|
if evidence_path is None:
|
|
return
|
|
from optuna.distributions import distribution_to_json, json_to_distribution
|
|
|
|
receipts = []
|
|
trial_numbers = set()
|
|
for path in evidence_path.parent.glob(f"{identity}.trial-*.json"):
|
|
document = json.loads(path.read_text(encoding="utf-8"))
|
|
trial_number = document.get("trial_number")
|
|
try:
|
|
receipt_number = int(path.name.removeprefix(f"{identity}.trial-").removesuffix(".json"))
|
|
except ValueError:
|
|
receipt_number = None
|
|
if (
|
|
document.get("schema") != _STUDY_SCHEMA
|
|
or document.get("study_identity") != identity
|
|
or document.get("state") != "COMPLETE"
|
|
or type(trial_number) is not int
|
|
or trial_number < 0
|
|
or receipt_number != trial_number
|
|
or trial_number in trial_numbers
|
|
):
|
|
raise RuntimeError(f"invalid Bayesian recovery receipt {path}")
|
|
trial_numbers.add(trial_number)
|
|
receipts.append((trial_number, document))
|
|
for trial_number, document in sorted(receipts, key=lambda item: item[0]):
|
|
existing = list(getattr(study, "trials", []))
|
|
if trial_number < len(existing):
|
|
trial = existing[trial_number]
|
|
matches = (
|
|
getattr(getattr(trial, "state", None), "name", None) == "COMPLETE"
|
|
and {key: float(value) for key, value in trial.params.items()}
|
|
== {key: float(value) for key, value in document["parameters"].items()}
|
|
and [float(value) for value in trial.values]
|
|
== [float(value) for value in document["values"]]
|
|
and {
|
|
key: distribution_to_json(value)
|
|
for key, value in trial.distributions.items()
|
|
} == document["distributions"]
|
|
and dict(trial.user_attrs) == dict(document["user_attrs"])
|
|
)
|
|
if not matches:
|
|
raise RuntimeError(
|
|
f"Bayesian study trial {trial_number} conflicts with its recovery receipt"
|
|
)
|
|
continue
|
|
while len(getattr(study, "trials", [])) < trial_number:
|
|
missing = len(study.trials)
|
|
study.add_trial(optuna.trial.create_trial(
|
|
state=optuna.trial.TrialState.FAIL,
|
|
user_attrs={
|
|
"study_identity": identity,
|
|
"recovery_placeholder_for_missing_trial": missing,
|
|
},
|
|
))
|
|
trial = optuna.trial.create_trial(
|
|
params={key: float(value) for key, value in document["parameters"].items()},
|
|
distributions={
|
|
key: json_to_distribution(value)
|
|
for key, value in document["distributions"].items()
|
|
},
|
|
values=[float(value) for value in document["values"]],
|
|
user_attrs=dict(document["user_attrs"]),
|
|
)
|
|
study.add_trial(trial)
|
|
restored = study.trials[trial_number]
|
|
if restored.number != trial_number:
|
|
raise RuntimeError("Optuna did not preserve recovered Bayesian trial identity")
|
|
|
|
|
|
def bayesian_execution_for(pipeline: AbliterationPipeline):
|
|
"""Return the metadata-only execution backend cached for this model."""
|
|
cached = getattr(pipeline, "_bayesian_execution", None)
|
|
if cached is not None:
|
|
return cached
|
|
from obliteratus.models.post_norms import post_norm_scales
|
|
from obliteratus.strategies.utils import get_layer_modules
|
|
|
|
layers = get_layer_modules(pipeline.handle)
|
|
arch = pipeline.handle.architecture
|
|
model = pipeline.handle.model
|
|
folded = {
|
|
index: post_norm_scales(
|
|
model,
|
|
layers[index],
|
|
direction.numel(),
|
|
architecture=arch,
|
|
)
|
|
for index in pipeline._strong_layers
|
|
if (direction := pipeline.refusal_directions.get(index)) is not None
|
|
} if getattr(pipeline, "fold_post_norms", True) else {}
|
|
execution = create_execution(pipeline, layers, arch, folded)
|
|
pipeline._bayesian_execution = execution
|
|
pipeline._bayesian_execution_captured = False
|
|
return execution
|
|
|
|
|
|
def bayesian_admission_options(
|
|
pipeline: AbliterationPipeline,
|
|
*,
|
|
n_refusal_prompts: int = 8,
|
|
n_kl_prompts: int = 5,
|
|
) -> dict[str, object]:
|
|
"""Describe exact trial snapshot and sequential forward demand."""
|
|
from obliteratus.phase_admission import ForwardWorkload
|
|
|
|
execution = bayesian_execution_for(pipeline)
|
|
model = pipeline.handle.model
|
|
config = getattr(model.config, "text_config", None) or model.config
|
|
generation = getattr(model, "generation_config", None)
|
|
supports_keep = getattr(model, "_supports_logits_to_keep", lambda: False)()
|
|
use_cache = getattr(generation, "use_cache", None)
|
|
if use_cache is None:
|
|
use_cache = getattr(config, "use_cache", True)
|
|
|
|
saved_flag = pipeline.use_chat_template
|
|
try:
|
|
pipeline.use_chat_template = True
|
|
harmful = pipeline._maybe_apply_chat_template(
|
|
pipeline.harmful_prompts[:n_refusal_prompts]
|
|
)
|
|
finally:
|
|
pipeline.use_chat_template = saved_flag
|
|
kl_prompts = list(getattr(pipeline, "_kl_eval_prompts", ()) or ())
|
|
if not kl_prompts:
|
|
raw = [
|
|
"The weather today is",
|
|
"In mathematics, the concept of",
|
|
"The history of ancient Rome",
|
|
"How to cook a simple pasta",
|
|
"The process of photosynthesis involves",
|
|
][:n_kl_prompts]
|
|
formatter = getattr(pipeline, "_maybe_apply_chat_template", None)
|
|
kl_prompts = formatter(raw) if callable(formatter) else raw
|
|
|
|
def lengths(prompts, limit):
|
|
counter = getattr(pipeline, "_admission_token_lengths", None)
|
|
if callable(counter):
|
|
return counter(prompts, limit, add_special_tokens=True)
|
|
tokenizer = pipeline.handle.tokenizer
|
|
return [
|
|
int(tokenizer(prompt, return_tensors="pt", truncation=True,
|
|
max_length=limit)["input_ids"].shape[-1])
|
|
for prompt in prompts
|
|
]
|
|
|
|
workloads = []
|
|
for length in lengths(harmful, 256):
|
|
workloads.append(ForwardWorkload(
|
|
1,
|
|
length,
|
|
cache_tokens=length,
|
|
logits_tokens=1 if supports_keep else length,
|
|
logits_copies=3,
|
|
use_cache=use_cache,
|
|
attention_mask=False,
|
|
))
|
|
final = length + 128
|
|
workloads.append(ForwardWorkload(
|
|
1,
|
|
1 if use_cache else final,
|
|
cache_tokens=final,
|
|
logits_tokens=1 if supports_keep or use_cache else final,
|
|
logits_copies=3,
|
|
use_cache=use_cache,
|
|
attention_mask=False,
|
|
))
|
|
kl_lengths = lengths(kl_prompts, getattr(pipeline, "max_seq_length", None) or 64)
|
|
for length in kl_lengths:
|
|
workloads.append(ForwardWorkload(
|
|
1,
|
|
length,
|
|
logits_copies=1,
|
|
use_cache=use_cache,
|
|
attention_mask=False,
|
|
logits_dtype_bytes=4,
|
|
))
|
|
vocab = int(getattr(config, "vocab_size", 0) or getattr(config, "padded_vocab_size", 0))
|
|
forward_host = 4 * max(kl_lengths, default=0) * vocab
|
|
return {
|
|
"snapshot_host_bytes": execution.snapshot_host_bytes,
|
|
"snapshot_disk_bytes": execution.snapshot_disk_bytes,
|
|
"forward_host_bytes": forward_host,
|
|
"forward_workloads": workloads,
|
|
"executor_identity": execution.identity(),
|
|
}
|
|
|
|
|
|
def replay_bayesian_plan(pipeline: AbliterationPipeline) -> BayesianApplyResult:
|
|
"""Apply the selected scored plan once and release rollback snapshots."""
|
|
plan = getattr(pipeline, "_bayesian_plan", None)
|
|
execution = getattr(pipeline, "_bayesian_execution", None)
|
|
if execution is None:
|
|
raise RuntimeError("no selected Bayesian plan is available for replay")
|
|
selected_sha = getattr(pipeline, "_bayesian_selected_plan_sha256", None)
|
|
try:
|
|
if not isinstance(plan, BayesianTrialPlan):
|
|
raise RuntimeError("no selected Bayesian plan is available for replay")
|
|
if selected_sha is None or plan.sha256 != selected_sha:
|
|
raise RuntimeError("selected Bayesian plan was changed after scoring")
|
|
execution.restore()
|
|
result = execution.apply(plan)
|
|
if plan.sha256 != selected_sha:
|
|
raise RuntimeError("Bayesian execution mutated the selected plan")
|
|
expected = tuple(getattr(pipeline, "_bayesian_scored_scope", ()) or ())
|
|
if result.applied_count <= 0 or not result.target_scope:
|
|
raise RuntimeError("Bayesian final replay changed no target")
|
|
if expected and result.target_scope != expected:
|
|
raise RuntimeError(
|
|
"Bayesian final replay target scope differs from the scored trial"
|
|
)
|
|
pipeline._bayesian_extraction_subspace_sha256 = {
|
|
int(index): tensor_sha256(value)
|
|
for index, value in getattr(pipeline, "concept_subspaces", {}).items()
|
|
}
|
|
if not hasattr(pipeline, "concept_subspaces"):
|
|
pipeline.concept_subspaces = {}
|
|
for index, direction in plan.directions.items():
|
|
current = pipeline.refusal_directions.get(index)
|
|
dtype = current.dtype if isinstance(current, torch.Tensor) else direction.dtype
|
|
selected = direction.detach().to(device="cpu", dtype=dtype, copy=True)
|
|
pipeline.refusal_directions[index] = selected
|
|
pipeline.concept_subspaces[index] = selected.unsqueeze(0)
|
|
pipeline._bayesian_replay = {
|
|
"plan_sha256": plan.sha256,
|
|
"applied_count": result.applied_count,
|
|
"target_scope": list(result.target_scope),
|
|
}
|
|
return result
|
|
except BaseException:
|
|
execution.restore()
|
|
raise
|
|
finally:
|
|
execution.release()
|
|
pipeline._bayesian_execution = None
|
|
pipeline._bayesian_execution_captured = False
|
|
|
|
|
|
def run_bayesian_optimization(
|
|
pipeline: AbliterationPipeline,
|
|
n_trials: int = 50,
|
|
n_refusal_prompts: int = 30,
|
|
n_kl_prompts: int = 5,
|
|
) -> dict[int, float]:
|
|
"""Select a scored plan and return with pristine weights restored.
|
|
|
|
Rollback snapshots remain available for ``replay_bayesian_plan``. An optional
|
|
``pipeline.execution_budget`` can stop search after completed trials.
|
|
"""
|
|
with ExitStack() as resources:
|
|
return _run_bayesian_optimization(
|
|
pipeline, n_trials, n_refusal_prompts, n_kl_prompts,
|
|
restore_before_return=True,
|
|
resources=resources,
|
|
)
|
|
|
|
|
|
def optimize_and_replay_bayesian_plan(
|
|
pipeline: AbliterationPipeline,
|
|
n_trials: int = 50,
|
|
n_refusal_prompts: int = 30,
|
|
n_kl_prompts: int = 5,
|
|
) -> BayesianApplyResult | None:
|
|
"""Select and immediately replay a plan without an intermediate restore.
|
|
|
|
Returns None for an explicitly skipped optimizer. Failed search or replay
|
|
restores pristine weights and releases snapshots before propagating errors.
|
|
"""
|
|
try:
|
|
with ExitStack() as resources:
|
|
regularizations = _run_bayesian_optimization(
|
|
pipeline, n_trials, n_refusal_prompts, n_kl_prompts,
|
|
restore_before_return=False,
|
|
resources=resources,
|
|
)
|
|
return replay_bayesian_plan(pipeline) if regularizations else None
|
|
except BaseException:
|
|
execution = getattr(pipeline, "_bayesian_execution", None)
|
|
if execution is not None:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
|
|
def _run_bayesian_optimization(
|
|
pipeline: AbliterationPipeline,
|
|
n_trials: int = 50,
|
|
n_refusal_prompts: int = 30,
|
|
n_kl_prompts: int = 5,
|
|
*,
|
|
restore_before_return: bool,
|
|
resources: ExitStack,
|
|
) -> dict[int, float]:
|
|
"""Run Bayesian optimization to find optimal ablation parameters.
|
|
|
|
``resources`` owns the durable study storage; callers close it on every
|
|
exit path (success, failure or cancellation).
|
|
|
|
Uses Optuna TPE with a Heretic-style parametric layer kernel to search
|
|
a compact parameter space:
|
|
- 4 kernel params (max_weight, peak_position, min_weight, spread)
|
|
- 1 float direction index (interpolated between SVD directions)
|
|
- 2 component weights (attention vs MLP scaling)
|
|
Total: 7 parameters regardless of model size (vs O(n_layers) before).
|
|
|
|
Also optimizes per-layer independently when the kernel doesn't fit well
|
|
(uses kernel as warm-start for per-layer refinement in phase 2).
|
|
|
|
Args:
|
|
pipeline: Initialized AbliterationPipeline (post-DISTILL stage).
|
|
n_trials: Number of optimization trials.
|
|
n_refusal_prompts: Number of harmful prompts for refusal measurement.
|
|
n_kl_prompts: Number of harmless prompts for KL measurement.
|
|
|
|
Returns:
|
|
Dict mapping layer_idx -> optimal regularization value.
|
|
"""
|
|
budget = getattr(pipeline, "execution_budget", None)
|
|
if budget is not None and n_trials > 0:
|
|
budget.require("bayesian_trial")
|
|
_reset_bayesian_state(pipeline)
|
|
if n_trials <= 0:
|
|
_skip(pipeline, "no-trials", "Bayesian optimization SKIPPED: no trials requested.")
|
|
return {}
|
|
|
|
try:
|
|
import optuna
|
|
from optuna.samplers import TPESampler
|
|
except ImportError:
|
|
# Keep the operator log and saved provenance explicit even when a
|
|
# partial or older installation lacks the optimizer dependency.
|
|
_skip(
|
|
pipeline,
|
|
"optuna-not-installed",
|
|
"Optuna not installed — Bayesian optimization SKIPPED. Layer "
|
|
"weights fall back to method defaults; results are NOT optimized. "
|
|
"Install with: pip install optuna",
|
|
)
|
|
return {}
|
|
|
|
if not pipeline.handle or not pipeline._strong_layers:
|
|
_skip(
|
|
pipeline,
|
|
"no-strong-layers",
|
|
"Bayesian optimization SKIPPED: no model handle or no selected "
|
|
"layers to optimize over.",
|
|
)
|
|
return {}
|
|
if (
|
|
bool(getattr(pipeline, "true_iterative_refinement", False))
|
|
and int(getattr(pipeline, "refinement_passes", 1)) > 1
|
|
):
|
|
raise ValueError(
|
|
"Bayesian execution does not support internal iterative refinement; "
|
|
"use an outer fresh-process strategy"
|
|
)
|
|
|
|
setup_started = None
|
|
if budget is not None and "bayesian_setup" in budget.operation_estimates:
|
|
if not admit_optional_work(pipeline, "bayesian_setup"):
|
|
raise SearchBudgetExhausted("Insufficient time for Bayesian reference and snapshot setup")
|
|
setup_started = budget.clock()
|
|
pipeline.log("Bayesian optimization: collecting reference logits...")
|
|
try:
|
|
kl_evidence, native_kl_measure = _capture_kl_evidence(pipeline, n_kl_prompts)
|
|
except torch.OutOfMemoryError:
|
|
raise
|
|
except Exception as error:
|
|
_skip(
|
|
pipeline,
|
|
"reference-logits-unavailable",
|
|
f"Bayesian optimization FAILED: Failed to collect complete reference logits ({error}).",
|
|
)
|
|
raise RuntimeError(
|
|
"Bayesian optimization requires complete identity-bound KL evidence"
|
|
) from error
|
|
|
|
from obliteratus.strategies.utils import get_layer_modules
|
|
|
|
layer_modules = get_layer_modules(pipeline.handle)
|
|
n_total_layers = len(layer_modules)
|
|
execution = bayesian_execution_for(pipeline)
|
|
try:
|
|
admit = getattr(pipeline, "_admit_bayesian_optimization", None)
|
|
if callable(admit):
|
|
admit(
|
|
host_bytes=execution.snapshot_host_bytes,
|
|
disk_bytes=execution.snapshot_disk_bytes,
|
|
executor_identity=execution.identity(),
|
|
)
|
|
execution.capture()
|
|
pipeline._bayesian_execution_captured = True
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
pipeline._bayesian_execution = execution
|
|
total_saved_mb = execution.snapshot_host_bytes / 1e6
|
|
pipeline.log(f" Captured complete Bayesian rollback scope ({total_saved_mb:.0f} MB on CPU)")
|
|
try:
|
|
study_identity, study_evidence = _study_identity(
|
|
pipeline,
|
|
execution,
|
|
kl_evidence,
|
|
n_refusal_prompts=n_refusal_prompts,
|
|
)
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
# Warm-start values for the parametric kernel.
|
|
# If the informed pipeline provided analysis-derived warm-start params,
|
|
# use those (they're much better than the default heuristic).
|
|
informed_warm = getattr(pipeline, "_informed_warm_start", None)
|
|
if informed_warm:
|
|
warm_peak = informed_warm.get("peak_position", 0.5)
|
|
pipeline.log(f" Using analysis-informed warm-start (peak={warm_peak:.2f})")
|
|
elif pipeline._strong_layers:
|
|
peak_layer = pipeline._strong_layers[0]
|
|
warm_peak = peak_layer / max(n_total_layers - 1, 1)
|
|
else:
|
|
warm_peak = 0.5
|
|
|
|
best_plan: BayesianTrialPlan | None = None
|
|
best_score = float("inf")
|
|
kl_budget = float(getattr(pipeline, "kl_budget", 0.5))
|
|
if not torch.isfinite(torch.tensor(kl_budget)) or kl_budget < 0:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise ValueError("Bayesian KL budget must be finite and nonnegative")
|
|
|
|
# Suppress Optuna's verbose logging
|
|
optuna.logging.set_verbosity(optuna.logging.WARNING)
|
|
|
|
# Direction sources need not belong to the edited target-layer subset.
|
|
n_layers_with_dirs = len(_direction_source_layers(pipeline))
|
|
|
|
trial_started = None
|
|
stopped_for_budget = False
|
|
|
|
# ── Phase 1: Parametric kernel optimization (compact search space) ──
|
|
# Heretic uses SEPARATE kernel parameters for attention and MLP,
|
|
# allowing them to peak at different layers (8 params + 1 dir_idx = 9).
|
|
|
|
def objective(trial: optuna.Trial) -> tuple[float, float]:
|
|
"""Multi-objective: minimize (refusal_rate, kl_divergence)."""
|
|
nonlocal trial_started
|
|
trial_started = budget.clock() if budget is not None else None
|
|
execution.restore()
|
|
|
|
# Attention kernel: 4 params
|
|
attn_max = trial.suggest_float("attn_max_weight", *KERNEL_SPACE["attn_max_weight"])
|
|
attn_peak = trial.suggest_float("attn_peak_position", *KERNEL_SPACE["attn_peak_position"])
|
|
attn_min = trial.suggest_float("attn_min_weight", *KERNEL_SPACE["attn_min_weight"])
|
|
attn_spread = trial.suggest_float("attn_spread", *KERNEL_SPACE["attn_spread"])
|
|
|
|
# MLP kernel: 4 params (separate — can peak at a different layer)
|
|
mlp_max = trial.suggest_float("mlp_max_weight", *KERNEL_SPACE["mlp_max_weight"])
|
|
mlp_peak = trial.suggest_float("mlp_peak_position", *KERNEL_SPACE["mlp_peak_position"])
|
|
mlp_min = trial.suggest_float("mlp_min_weight", *KERNEL_SPACE["mlp_min_weight"])
|
|
mlp_spread = trial.suggest_float("mlp_spread", *KERNEL_SPACE["mlp_spread"])
|
|
|
|
# Float direction index (cross-layer interpolation, Heretic-style)
|
|
dir_idx = trial.suggest_float("dir_idx", 0.0, max(n_layers_with_dirs - 1, 0.0))
|
|
|
|
params = {
|
|
"attn_max_weight": attn_max,
|
|
"attn_peak_position": attn_peak,
|
|
"attn_min_weight": attn_min,
|
|
"attn_spread": attn_spread,
|
|
"mlp_max_weight": mlp_max,
|
|
"mlp_peak_position": mlp_peak,
|
|
"mlp_min_weight": mlp_min,
|
|
"mlp_spread": mlp_spread,
|
|
"dir_idx": dir_idx,
|
|
}
|
|
plan = _resolve_trial_plan(
|
|
pipeline,
|
|
params,
|
|
n_total_layers=n_total_layers,
|
|
execution_identity=dict(execution.identity()),
|
|
)
|
|
resolved_plan_sha256 = plan.sha256
|
|
application = execution.apply(plan)
|
|
if plan.sha256 != resolved_plan_sha256:
|
|
raise RuntimeError("Bayesian execution mutated the resolved trial plan")
|
|
if application.applied_count <= 0 or not application.target_scope:
|
|
raise RuntimeError("Bayesian trial changed no target")
|
|
trial.set_user_attr("study_identity", study_identity)
|
|
trial.set_user_attr("plan_sha256", plan.sha256)
|
|
trial.set_user_attr("plan", plan.evidence())
|
|
trial.set_user_attr("target_scope", list(application.target_scope))
|
|
trial.set_user_attr("applied_count", application.applied_count)
|
|
|
|
# Measure objectives
|
|
pipeline._bayesian_refusal_measurement = None
|
|
refusal = _measure_refusal_rate(pipeline, n_prompts=n_refusal_prompts)
|
|
measurement = pipeline._bayesian_refusal_measurement
|
|
if measurement is not None:
|
|
trial.set_user_attr("exclude_invalid", True)
|
|
trial.set_user_attr("valid_count", measurement.valid)
|
|
trial.set_user_attr("valid_refusal_rate", measurement.rate)
|
|
trial.set_user_attr("invalid_rate", measurement.invalid_rate)
|
|
trial.set_user_attr("invalid_count", measurement.invalid)
|
|
trial.set_user_attr("refusal_count", measurement.refused)
|
|
trial.set_user_attr("total_count", measurement.total)
|
|
pipeline.log(
|
|
f" Refusal measurement: {measurement.refused}/{measurement.valid} valid refused, "
|
|
f"{measurement.invalid} invalid ({measurement.invalid_rate:.0%}); "
|
|
"exclude_invalid=True, objective=(refused + invalid) / total"
|
|
)
|
|
kl = (
|
|
float(native_kl_measure())
|
|
if native_kl_measure is not None
|
|
else _measure_kl_divergence(
|
|
pipeline, kl_evidence.reference_logits, kl_evidence.prompts
|
|
)
|
|
)
|
|
if not torch.isfinite(torch.tensor(kl)) or kl < 0:
|
|
raise RuntimeError("Bayesian KL objective is not finite and nonnegative")
|
|
|
|
nonlocal best_score, best_plan
|
|
combined = refusal + 0.5 * kl
|
|
if kl <= kl_budget and combined < best_score:
|
|
best_score = combined
|
|
best_plan = plan
|
|
|
|
pipeline.log(
|
|
f" Trial {trial.number + 1}/{n_trials}: "
|
|
f"refusal+invalid={refusal:.0%}, KL={kl:.4f} "
|
|
f"(attn_peak={attn_peak:.2f}, mlp_peak={mlp_peak:.2f}, dir={dir_idx:.2f})"
|
|
)
|
|
|
|
return refusal, kl
|
|
|
|
try:
|
|
sampler = TPESampler(seed=_SAMPLER_SEED, n_startup_trials=min(5, n_trials // 3))
|
|
durable_options, evidence_path = _durable_study_options(
|
|
optuna, pipeline, study_identity, resources
|
|
)
|
|
study = optuna.create_study(
|
|
directions=["minimize", "minimize"],
|
|
sampler=sampler,
|
|
study_name=f"obliteratus-bayesian-{study_identity}",
|
|
**durable_options,
|
|
)
|
|
get_attrs = getattr(study, "user_attrs", {})
|
|
previous_identity = (
|
|
get_attrs.get("study_identity") if isinstance(get_attrs, dict) else None
|
|
)
|
|
if previous_identity not in {None, study_identity}:
|
|
raise RuntimeError("durable Bayesian study identity does not match this run")
|
|
set_study_attr = getattr(study, "set_user_attr", None)
|
|
if callable(set_study_attr) and previous_identity is None:
|
|
set_study_attr("study_identity", study_identity)
|
|
set_study_attr("study_evidence", study_evidence)
|
|
_restore_completed_trials(optuna, study, evidence_path, study_identity)
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
# Enqueue warm-start trial with analysis-derived estimates.
|
|
# Translate informed pipeline params to the new per-component format.
|
|
if informed_warm:
|
|
iw = informed_warm
|
|
warm_params = {
|
|
"attn_max_weight": iw.get("max_weight", 0.9) * iw.get("attn_scale", 1.0),
|
|
"attn_peak_position": iw.get("peak_position", warm_peak),
|
|
"attn_min_weight": iw.get("min_weight", 0.05),
|
|
"attn_spread": iw.get("spread", 0.3),
|
|
"mlp_max_weight": iw.get("max_weight", 0.9) * iw.get("mlp_scale", 0.6),
|
|
"mlp_peak_position": iw.get("peak_position", warm_peak),
|
|
"mlp_min_weight": iw.get("min_weight", 0.05),
|
|
"mlp_spread": iw.get("spread", 0.3),
|
|
"dir_idx": iw.get("dir_idx", 0.0),
|
|
}
|
|
else:
|
|
warm_params = {
|
|
"attn_max_weight": 0.9,
|
|
"attn_peak_position": warm_peak,
|
|
"attn_min_weight": 0.05,
|
|
"attn_spread": 0.3,
|
|
"mlp_max_weight": 0.6,
|
|
"mlp_peak_position": warm_peak,
|
|
"mlp_min_weight": 0.05,
|
|
"mlp_spread": 0.3,
|
|
"dir_idx": 0.0,
|
|
}
|
|
try:
|
|
if not getattr(study, "trials", []):
|
|
study.enqueue_trial(_clamp_to_space(warm_params))
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
pipeline.log(f"Bayesian optimization: running {n_trials} trials (parametric kernel)...")
|
|
try:
|
|
existing = [
|
|
trial for trial in getattr(study, "trials", [])
|
|
if (
|
|
getattr(trial, "state", None) is None
|
|
or getattr(trial.state, "name", None) == "COMPLETE"
|
|
)
|
|
]
|
|
if budget is not None:
|
|
if setup_started is not None:
|
|
budget.observe("bayesian_setup", budget.clock() - setup_started)
|
|
for trial in existing:
|
|
duration = getattr(trial, "duration", None)
|
|
if duration is not None:
|
|
budget.observe("bayesian_trial", duration.total_seconds())
|
|
remaining = max(0, n_trials - len(existing))
|
|
if remaining and not admit_optional_work(pipeline, "bayesian_trial"):
|
|
stopped_for_budget = True
|
|
remaining = 0
|
|
if remaining:
|
|
options = {}
|
|
if evidence_path is not None or callable(
|
|
getattr(pipeline, "_bayesian_trial_checkpoint", None)
|
|
):
|
|
options["callbacks"] = [
|
|
lambda _study, trial: _persist_completed_trial(
|
|
pipeline,
|
|
trial,
|
|
study_identity=study_identity,
|
|
evidence_path=evidence_path,
|
|
)
|
|
]
|
|
if budget is not None:
|
|
def after_trial(active_study, trial):
|
|
nonlocal stopped_for_budget
|
|
# Persistence callbacks run first; their cost belongs to the trial.
|
|
if trial_started is not None:
|
|
budget.observe("bayesian_trial", budget.clock() - trial_started)
|
|
completed = sum(
|
|
getattr(getattr(item, "state", None), "name", None) == "COMPLETE"
|
|
for item in active_study.trials
|
|
)
|
|
if completed < n_trials and not admit_optional_work(pipeline, "bayesian_trial"):
|
|
stopped_for_budget = True
|
|
active_study.stop()
|
|
options.setdefault("callbacks", []).append(after_trial)
|
|
study.optimize(
|
|
objective,
|
|
n_trials=remaining,
|
|
show_progress_bar=False,
|
|
**options,
|
|
)
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
try:
|
|
if restore_before_return:
|
|
execution.restore()
|
|
if evidence_path is not None:
|
|
for trial in getattr(study, "trials", []):
|
|
trial_path = evidence_path.with_name(
|
|
f"{study_identity}.trial-{trial.number}.json"
|
|
)
|
|
if not trial_path.exists():
|
|
_persist_completed_trial(
|
|
pipeline,
|
|
trial,
|
|
study_identity=study_identity,
|
|
evidence_path=evidence_path,
|
|
)
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
# Apply best result
|
|
|
|
# Get best trial from Pareto front (prefer low refusal)
|
|
try:
|
|
pareto = [
|
|
trial for trial in study.best_trials
|
|
if trial.values is not None and trial.values[1] <= kl_budget
|
|
]
|
|
if pareto:
|
|
pareto.sort(key=lambda t: (t.values[0], t.values[1]))
|
|
best_trial = pareto[0]
|
|
|
|
p = best_trial.params
|
|
best_plan = _resolve_trial_plan(
|
|
pipeline,
|
|
dict(p),
|
|
n_total_layers=n_total_layers,
|
|
execution_identity=dict(execution.identity()),
|
|
)
|
|
recorded_plan = getattr(best_trial, "user_attrs", {}).get("plan_sha256")
|
|
if recorded_plan is not None and recorded_plan != best_plan.sha256:
|
|
raise RuntimeError("durable Bayesian trial plan identity is inconsistent")
|
|
|
|
pipeline.log(
|
|
f" Best trial: refusal+invalid={best_trial.values[0]:.0%}, "
|
|
f"KL={best_trial.values[1]:.4f}"
|
|
)
|
|
pipeline.log(
|
|
f" Attn kernel: peak={p['attn_peak_position']:.2f}, "
|
|
f"spread={p['attn_spread']:.2f}, max={p['attn_max_weight']:.2f}"
|
|
)
|
|
pipeline.log(
|
|
f" MLP kernel: peak={p['mlp_peak_position']:.2f}, "
|
|
f"spread={p['mlp_spread']:.2f}, max={p['mlp_max_weight']:.2f}"
|
|
)
|
|
pipeline.log(f" dir_idx={p['dir_idx']:.2f}")
|
|
|
|
pipeline._bayesian_scored_scope = tuple(
|
|
getattr(best_trial, "user_attrs", {}).get("target_scope", ())
|
|
)
|
|
elif best_plan is not None:
|
|
pipeline.log(f" Using best combined score: {best_score:.4f}")
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
pipeline._bayesian_search = {
|
|
"requested_trials": n_trials,
|
|
"completed_trials": sum(
|
|
getattr(getattr(trial, "state", None), "name", None) == "COMPLETE"
|
|
for trial in getattr(study, "trials", ())
|
|
),
|
|
"stopped_for_budget": stopped_for_budget,
|
|
}
|
|
pipeline._bayesian_plan = best_plan
|
|
pipeline._bayesian_selected_plan_sha256 = best_plan.sha256 if best_plan else None
|
|
pipeline._bayesian_attn_scale = None
|
|
pipeline._bayesian_mlp_scale = None
|
|
try:
|
|
pipeline._free_gpu_memory()
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
|
|
if best_plan is None:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
if stopped_for_budget:
|
|
pipeline._bayesian_skipped = "search-budget-exhausted"
|
|
raise SearchBudgetExhausted(
|
|
"Search budget exhausted without a completed feasible Bayesian trial"
|
|
)
|
|
pipeline._bayesian_skipped = "no-feasible-trials"
|
|
trials = list(getattr(study, "trials", ()))
|
|
if len(trials) >= n_trials and all(
|
|
getattr(getattr(trial, "state", None), "name", None) == "COMPLETE"
|
|
and trial.values is not None and len(trial.values) == 2
|
|
and all(type(value) in (int, float) and math.isfinite(value) and value >= 0
|
|
for value in trial.values)
|
|
and trial.values[1] > kl_budget
|
|
for trial in trials
|
|
):
|
|
raise NoFeasibleTrialError(
|
|
kl_budget, study_identity,
|
|
{trial.number: tuple(trial.values) for trial in trials},
|
|
)
|
|
raise RuntimeError(
|
|
"Bayesian optimization produced no completed trial within the "
|
|
f"configured KL budget {kl_budget:.6g}"
|
|
)
|
|
|
|
if evidence_path is not None:
|
|
try:
|
|
_atomic_json(
|
|
evidence_path,
|
|
{
|
|
"schema": _STUDY_SCHEMA,
|
|
"study_identity": study_identity,
|
|
"study_evidence": study_evidence,
|
|
"search": pipeline._bayesian_search,
|
|
"selected_plan": best_plan.evidence(),
|
|
"selected_plan_sha256": best_plan.sha256,
|
|
"selected_values": list(best_trial.values) if pareto else None,
|
|
"target_scope": list(pipeline._bayesian_scored_scope or ()),
|
|
},
|
|
)
|
|
except BaseException:
|
|
_discard_bayesian_execution(pipeline, execution)
|
|
raise
|
|
return best_plan.legacy_regularization()
|