"""Activation proxy budgets and requested projection behavior.""" import random import pytest import torch from obliteratus.analysis.bayesian_kernel_projection import BayesianKernelProjection, ProjectionConfig def evidence(): harmful = {0: [torch.tensor([3., 2., 1.]), torch.tensor([3., 2., 2.])]} harmless = {0: [torch.tensor([1., 1., 2.]), torch.tensor([1., 1., 3.])]} return harmful, harmless, {0: torch.eye(3)[:2]} @pytest.mark.parametrize("count", [1, 2, 9, 10, 13]) def test_requested_trial_budget_is_exact(count): result = BayesianKernelProjection(n_trials=count).optimize(*evidence()) assert len(result.all_trials) == result.n_trials == count assert all(1 <= trial.config.n_directions <= 2 for trial in result.all_trials) @pytest.mark.parametrize("count", [0, -1, True, 1.5]) def test_invalid_trial_budget_fails_before_sampling(count): with pytest.raises(ValueError, match="positive integer"): BayesianKernelProjection(n_trials=count) def test_projection_controls_change_measured_activation_objective(): optimizer = BayesianKernelProjection(n_trials=2) stats = optimizer._precompute_stats(*evidence(), [0]) one = optimizer._evaluate_trial(ProjectionConfig((0, 0), {0: .5}, 1, .2, False), stats, [0], 0) two = optimizer._evaluate_trial(ProjectionConfig((0, 0), {0: .5}, 2, .2, False), stats, [0], 1) norm = optimizer._evaluate_trial(ProjectionConfig((0, 0), {0: .5}, 2, .2, True), stats, [0], 2) # Independent diagonal projection: strength=.5*(1-.2)=.4 on x and y. before = torch.stack(evidence()[1][0]) after = before * torch.tensor([.6, .6, 1.]) distortion = ((after - before).square().sum(1).mean() / before.square().sum(1).mean()).item() assert two.harmless_distortion == pytest.approx(distortion) assert two.refusal_reduction == pytest.approx(.4) assert one.refusal_reduction != pytest.approx(two.refusal_reduction) assert norm.harmless_distortion != pytest.approx(two.harmless_distortion) identity = optimizer._evaluate_trial(ProjectionConfig((0, 0), {0: .5}, 2, 1., False), stats, [0], 3) assert identity.refusal_reduction == identity.harmless_distortion == 0 def test_optimizer_does_not_change_global_rng_or_input_evidence(): data = evidence() saved = [[v.clone() for rows in group.values() for v in rows] for group in data[:2]] py_state, torch_state = random.getstate(), torch.random.get_rng_state().clone() optimizer = BayesianKernelProjection(n_trials=12) first = optimizer.optimize(*data) second = optimizer.optimize(*data) assert first == second assert random.getstate() == py_state assert torch.equal(torch.random.get_rng_state(), torch_state) for group, original in zip(data[:2], saved, strict=True): for current, before in zip([v for rows in group.values() for v in rows], original, strict=True): assert torch.equal(current, before) def test_single_vector_never_searches_unsupplied_directions(): harmful, harmless, _ = evidence() result = BayesianKernelProjection(n_trials=20).optimize(harmful, harmless, {0: torch.tensor([1., 0., 0.])}) assert all(trial.config.n_directions == 1 for trial in result.all_trials) @pytest.mark.parametrize("bad", [torch.zeros(2, 3), torch.full((1, 3), float("nan")), torch.eye(4)]) def test_invalid_basis_is_not_scored(bad): harmful, harmless, _ = evidence() with pytest.raises(ValueError): BayesianKernelProjection(n_trials=2).optimize(harmful, harmless, {0: bad})