Files
OBLITERATUS/tests/test_bayesian_source_domain.py
T
LLMsherpa acfd373f01 fix: correct Bayesian search inputs and RDO preservation
Search float direction indices across all extracted source layers and include that domain in persisted study identity. Apply informed attention scaling to the warm-start maximum. Penalize the squared harmless activation component removed by the optimized RDO direction so preservation has a gradient. Add behavioral regressions.
2026-10-27 21:40:39 -04:00

99 lines
3.8 KiB
Python

"""Source-layer search and analysis-derived warm-start regressions."""
from types import SimpleNamespace
import optuna
import pytest
import torch
from obliteratus import bayesian_optimizer as bo
from obliteratus.informed_pipeline import InformedAbliterationPipeline
from tests.test_bayesian_optimizer_contracts import _Layer, _optimization_pipeline
def _capture_studies(monkeypatch):
studies = []
create_study = optuna.create_study
def capture(**kwargs):
study = create_study(**kwargs)
studies.append(study)
return study
monkeypatch.setattr(optuna, "create_study", capture)
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_args, **_kwargs: 0.25)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_args, **_kwargs: 0.1)
return studies
@pytest.mark.parametrize("stride", [1, 2])
@pytest.mark.parametrize("direction_index", [38.5, 39.0])
def test_search_reaches_all_source_directions_with_fewer_target_layers(
monkeypatch, stride, direction_index,
):
studies = _capture_studies(monkeypatch)
source_layers = list(range(0, 40 * stride, stride))
pipeline = _optimization_pipeline([_Layer() for _ in range(source_layers[-1] + 1)])
pipeline.refusal_directions = {
layer: torch.tensor([float(index + 1), 1.0])
for index, layer in enumerate(source_layers)
}
pipeline._strong_layers = source_layers[:29]
pipeline._informed_warm_start = {"dir_idx": direction_index}
try:
bo.run_bayesian_optimization(pipeline, n_trials=1, n_kl_prompts=1)
trial = studies[0].trials[0]
assert trial.distributions["dir_idx"].high == 39.0
assert trial.params["dir_idx"] == direction_index
assert studies[0].user_attrs["study_evidence"]["direction_source_layers"] == source_layers
expected = torch.tensor([direction_index + 1.0, 1.0])
expected /= expected.norm()
plan = pipeline._bayesian_plan
assert set(plan.directions) == set(pipeline._strong_layers)
for actual in plan.directions.values():
torch.testing.assert_close(actual, expected)
assert pipeline.projections
for projection in pipeline.projections:
torch.testing.assert_close(projection["direction"].reshape(-1), expected)
finally:
bo._reset_bayesian_state(pipeline)
@pytest.mark.parametrize("entanglement,attention_scale,mlp_scale", [
(0.2, 0.8, 0.6),
(0.8, 0.7, 0.4),
])
def test_analysis_attention_scale_reaches_the_enqueued_trial(
monkeypatch, entanglement, attention_scale, mlp_scale,
):
studies = _capture_studies(monkeypatch)
pipeline = _optimization_pipeline([_Layer(), _Layer()])
pipeline._insights = SimpleNamespace(
detected_alignment_method="cai",
cluster_representative_layers=[1],
direction_clusters=[[0, 1]],
direction_persistence=0.25,
entanglement_score=entanglement,
)
pipeline._informed_bayesian_trials = 1
pipeline._informed_kl_budget = 0.5
pipeline._harmful_means = {0: torch.ones(2), 1: torch.ones(2)}
pipeline._harmless_means = {0: torch.zeros(2), 1: torch.zeros(2)}
InformedAbliterationPipeline._configure_bayesian_warm_start(pipeline)
assert pipeline._informed_warm_start["attn_scale"] == attention_scale
try:
bo.run_bayesian_optimization(pipeline, n_trials=1, n_kl_prompts=1)
trial = studies[0].trials[0]
assert trial.params["attn_max_weight"] == pytest.approx(0.9 * attention_scale)
assert trial.params["mlp_max_weight"] == pytest.approx(0.9 * mlp_scale)
peak = bo._parametric_layer_weight(
1, 2, 0.9 * attention_scale, 0.9, 0.05, 0.6,
)
assert pipeline._bayesian_plan.attention_regularization[1] == pytest.approx(1 - peak)
finally:
bo._reset_bayesian_state(pipeline)