mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
549 lines
21 KiB
Python
549 lines
21 KiB
Python
"""Verification diagnostics keep planning and collection evidence aligned."""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import abliterate
|
|
from obliteratus.abliterate import AbliterationPipeline, PipelineFailure
|
|
from obliteratus.analysis.spectral_certification import CertificationLevel
|
|
from obliteratus.informed_pipeline import InformedAbliterationPipeline
|
|
from obliteratus.telemetry import build_report
|
|
from tests import test_phase_calibration, test_phase_diagnostic_admission
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
qwen_handle = test_phase_calibration.qwen_handle
|
|
pipeline = test_phase_diagnostic_admission.pipeline
|
|
|
|
|
|
@pytest.mark.parametrize("budget", [True, False, "0.5", [], -0.1, float("nan"), float("inf")])
|
|
def test_kl_budget_rejects_invalid_limits_before_loading(budget):
|
|
with pytest.raises(ValueError, match="kl_budget must be a finite nonnegative number"):
|
|
AbliterationPipeline("offline", method="basic", kl_budget=budget)
|
|
|
|
|
|
@pytest.mark.parametrize("budget", [0, 0.5])
|
|
def test_kl_budget_accepts_finite_nonnegative_limits(budget):
|
|
pipeline = AbliterationPipeline("offline", method="basic", kl_budget=budget)
|
|
assert pipeline.kl_budget == budget
|
|
|
|
|
|
@pytest.mark.parametrize("budget", [float("nan"), float("inf"), -0.1])
|
|
def test_final_kl_gate_revalidates_runtime_budget(pipeline, budget):
|
|
pipeline.use_kl_optimization = True
|
|
pipeline.kl_budget = budget
|
|
pipeline._quality_metrics["kl_divergence"] = 0.1
|
|
|
|
with pytest.raises(PipelineFailure, match="finite nonnegative") as caught:
|
|
pipeline._enforce_final_kl_budget()
|
|
|
|
assert caught.value.metric == "kl_budget"
|
|
|
|
|
|
def test_intermediate_verification_retains_pristine_kl_until_final_gate(pipeline):
|
|
pipeline.use_kl_optimization = True
|
|
pipeline._strong_layers = []
|
|
pipeline.kl_budget = 0.5
|
|
pristine = [torch.zeros(1)]
|
|
pipeline._baseline_token_logits = pristine
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: 0.75
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
|
|
pipeline._verify(final=False)
|
|
|
|
assert pipeline._baseline_token_logits is pristine
|
|
assert pipeline._kl_eval_prompts == ["KL"]
|
|
assert pipeline._quality_metrics["kl_budget_status"] == "exceeded"
|
|
assert pipeline._quality_metrics["kl_budget_enforced"] is False
|
|
with pytest.raises(abliterate.PipelineValidationError, match="after all edits"):
|
|
pipeline._enforce_final_kl_budget()
|
|
|
|
|
|
@pytest.mark.parametrize("fail_second_measurement", [False, True])
|
|
def test_informed_passes_measure_against_the_same_pristine_logits(pipeline, fail_second_measurement):
|
|
pipeline.use_kl_optimization = True
|
|
pipeline._strong_layers = []
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
pipeline.harmless_prompts = ["calibration"] * 10
|
|
state = SimpleNamespace(bias=0.0)
|
|
|
|
def forward(**_kwargs):
|
|
logits = torch.zeros(1, 8, 2)
|
|
logits[..., 0] = state.bias
|
|
return SimpleNamespace(loss=torch.tensor(0.0), logits=logits)
|
|
|
|
pipeline.handle.model.forward.side_effect = forward
|
|
pipeline._capture_baseline_kl_logits()
|
|
pristine = pipeline._baseline_token_logits
|
|
assert len(pristine) == 10
|
|
assert all(torch.count_nonzero(row) == 0 for row in pristine)
|
|
|
|
state.bias = 0.1
|
|
original_measure = pipeline._measure_sequence_token_kl
|
|
measured = []
|
|
|
|
def measure():
|
|
assert pipeline._baseline_token_logits is pristine
|
|
assert all(torch.count_nonzero(row) == 0 for row in pristine)
|
|
if measured and fail_second_measurement:
|
|
raise RuntimeError("injected second KL measurement failure")
|
|
value = original_measure()
|
|
measured.append(value)
|
|
return value
|
|
|
|
pipeline._measure_sequence_token_kl = measure
|
|
pipeline._report = SimpleNamespace()
|
|
pipeline._max_ouroboros_passes = 1
|
|
pipeline._ouroboros_threshold = 0.5
|
|
pipeline._probe = lambda: None
|
|
pipeline._distill_inner = lambda: setattr(pipeline, "_strong_layers", [0])
|
|
pipeline._excise_informed = lambda: setattr(state, "bias", 0.2)
|
|
class SyntheticSnapshot:
|
|
parameter_names = ("synthetic.bias",)
|
|
|
|
def __init__(self):
|
|
self.bias = state.bias
|
|
self.identity = f"synthetic:{self.bias:.6f}"
|
|
self.released = False
|
|
|
|
def restore(self):
|
|
state.bias = self.bias
|
|
|
|
def release(self):
|
|
self.released = True
|
|
|
|
snapshots = []
|
|
|
|
def capture():
|
|
snapshot = SyntheticSnapshot()
|
|
snapshots.append(snapshot)
|
|
return snapshot
|
|
|
|
pipeline._capture_refinement_candidate = capture
|
|
pipeline._refinement_candidate_identity = lambda _names: f"synthetic:{state.bias:.6f}"
|
|
pipeline._prepare_spectral_certification = lambda: (_ for _ in ()).throw(RuntimeError("skip"))
|
|
pipeline._verify_and_compensate_impl = (
|
|
InformedAbliterationPipeline._verify_and_compensate_impl.__get__(pipeline)
|
|
)
|
|
pipeline.handle.tokenizer.decode.side_effect = [
|
|
"Paris is the capital city of France.", "A useful capability response.",
|
|
"I cannot help with that request.",
|
|
"Paris is the capital city of France.", "A useful capability response.",
|
|
"Here is a useful response.",
|
|
]
|
|
events = []
|
|
pipeline._on_stage = events.append
|
|
|
|
if fail_second_measurement:
|
|
with pytest.raises(PipelineFailure, match="KL verification failed closed"):
|
|
InformedAbliterationPipeline._verify_and_compensate(pipeline)
|
|
assert events[-1].status == "error"
|
|
assert state.bias == pytest.approx(0.1)
|
|
else:
|
|
InformedAbliterationPipeline._verify_and_compensate(pipeline)
|
|
assert 0 < measured[0] < measured[1] < pipeline.kl_budget
|
|
assert events[-1].status == "done"
|
|
assert events[-1].details["kl_budget_enforced"] is True
|
|
assert events[-1].details["kl_divergence"] == measured[1]
|
|
assert state.bias == pytest.approx(0.2)
|
|
|
|
assert snapshots and all(snapshot.released for snapshot in snapshots)
|
|
assert pipeline._baseline_first_token_logits is None
|
|
assert pipeline._baseline_token_logits == []
|
|
assert pipeline._kl_eval_prompts == []
|
|
|
|
|
|
def test_repeated_verification_discards_previous_diagnostic_results(pipeline):
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: 0.25
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline._strong_layers = [0]
|
|
certificate = SimpleNamespace(
|
|
level=CertificationLevel.INCONCLUSIVE, diagnostic_reason="insufficient_samples",
|
|
effective_noise_rank=38, n_samples_required=72, n_samples_used=40,
|
|
)
|
|
pipeline._prepare_spectral_certification = lambda: (
|
|
SimpleNamespace(certify=lambda *_args, **_kwargs: certificate),
|
|
[0], {0: [torch.ones(8)]}, {0: [torch.ones(8)]},
|
|
)
|
|
pipeline._verify()
|
|
assert pipeline._quality_metrics["capability_score"] == 1.0
|
|
assert pipeline._quality_metrics["first_token_kl_divergence"] == 0.1
|
|
assert pipeline._quality_metrics["spectral_samples_required"] == 72
|
|
|
|
pipeline._strong_layers = []
|
|
pipeline.handle.model.generate.side_effect = RuntimeError("CUDA out of memory")
|
|
pipeline._verify()
|
|
|
|
metrics = pipeline._quality_metrics
|
|
assert metrics["coherence"] is None
|
|
assert metrics["kl_budget_status"] == "unavailable"
|
|
assert metrics["spectral_certification"] is None
|
|
assert metrics["capability_completed_count"] == 0
|
|
for field in ("capability_score", "capability_results", "first_token_kl_divergence",
|
|
"kl_metric", "spectral_samples_required", "spectral_diagnostic_reason",
|
|
"harmful_degenerate_count", "harmful_degenerate_fraction"):
|
|
assert field not in metrics
|
|
|
|
|
|
def _spectral_pipeline(hidden_size, harmful_count, harmless_count):
|
|
pipeline = AbliterationPipeline(
|
|
"offline",
|
|
method="basic",
|
|
harmful_prompts=["harmful"],
|
|
harmless_prompts=["harmless"],
|
|
evaluation_harmful_prompts=["harmful"] * harmful_count,
|
|
evaluation_harmless_prompts=["harmless"] * harmless_count,
|
|
use_chat_template=False,
|
|
)
|
|
config = SimpleNamespace(hidden_size=hidden_size, vocab_size=32, use_cache=False)
|
|
model = SimpleNamespace(
|
|
config=config,
|
|
generation_config=None,
|
|
_supports_logits_to_keep=lambda: False,
|
|
)
|
|
pipeline.handle = SimpleNamespace(
|
|
model=model,
|
|
config=config,
|
|
hidden_size=hidden_size,
|
|
tokenizer=SimpleNamespace(padding_side="right", pad_token_id=0),
|
|
)
|
|
pipeline._strong_layers = [0]
|
|
pipeline._maybe_apply_chat_template = lambda prompts, **_kwargs: prompts
|
|
pipeline._admission_token_lengths = lambda prompts, *_args, **_kwargs: [8] * len(prompts)
|
|
pipeline._logits_element_size = lambda: 2
|
|
pipeline._tokenize_activation_batch = lambda prompts, _limit: {
|
|
"input_ids": torch.ones(len(prompts), 8, dtype=torch.long),
|
|
"attention_mask": torch.ones(len(prompts), 8, dtype=torch.long),
|
|
}
|
|
return pipeline
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("hidden_size", "harmful_count", "harmless_count", "expected_per_class"),
|
|
[
|
|
(5120, 100, 100, 36),
|
|
(128, 100, 100, 20),
|
|
(5120, 40, 22, 22),
|
|
],
|
|
)
|
|
def test_spectral_planning_and_collection_use_the_same_balanced_sample_count(
|
|
monkeypatch, hidden_size, harmful_count, harmless_count, expected_per_class,
|
|
):
|
|
pipeline = _spectral_pipeline(hidden_size, harmful_count, harmless_count)
|
|
planned_batches = []
|
|
collected = []
|
|
|
|
def admit(stage, *, work_scope=None, **_kwargs):
|
|
assert (stage, work_scope) == ("verify", "spectral")
|
|
pipeline._phase_forward_workloads(
|
|
stage,
|
|
probe_batch=100,
|
|
probe_limit=64,
|
|
activation_batches=planned_batches,
|
|
diagnostic=work_scope,
|
|
)
|
|
|
|
def collect(_layers, prompts, label):
|
|
collected.append((label, len(prompts)))
|
|
return {}
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", admit)
|
|
monkeypatch.setattr(pipeline, "_collect_activations", collect)
|
|
monkeypatch.setattr(abliterate, "get_layer_modules", lambda _handle: [object()])
|
|
|
|
pipeline._prepare_spectral_certification()
|
|
|
|
assert collected == [
|
|
("cert_harmful", expected_per_class),
|
|
("cert_harmless", expected_per_class),
|
|
]
|
|
assert [batch.batch_size for batch in planned_batches] == [
|
|
expected_per_class,
|
|
expected_per_class,
|
|
]
|
|
assert pipeline._spectral_sample_count() == expected_per_class
|
|
|
|
|
|
def test_verify_records_an_advisory_kl_budget_exceedance_without_claiming_enforcement(
|
|
pipeline, monkeypatch,
|
|
):
|
|
"""VERIFY records an over-budget measurement but leaves export policy to callers."""
|
|
pipeline._strong_layers = []
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: 0.75
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline.kl_budget = 0.5
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
events = []
|
|
pipeline._on_stage = lambda event: events.append(event)
|
|
|
|
pipeline._verify()
|
|
|
|
metrics = pipeline._quality_metrics
|
|
assert metrics["kl_divergence"] == 0.75
|
|
assert metrics["kl_budget"] == 0.5
|
|
assert metrics["kl_budget_status"] == "exceeded"
|
|
assert metrics["kl_budget_enforced"] is False
|
|
assert events[-1].status == "done"
|
|
assert "KL budget=exceeded" in events[-1].message
|
|
|
|
|
|
def test_verify_marks_kl_equal_to_its_budget_within_budget(pipeline, monkeypatch):
|
|
pipeline._strong_layers = []
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: 0.5
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline.kl_budget = 0.5
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
|
|
pipeline._verify()
|
|
|
|
assert pipeline._quality_metrics["kl_budget_status"] == "within_budget"
|
|
|
|
|
|
@pytest.mark.parametrize("value", [float("nan"), float("inf"), -0.1])
|
|
def test_verify_marks_nonfinite_or_negative_kl_as_unavailable(pipeline, monkeypatch, value):
|
|
pipeline._strong_layers = []
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: value
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
|
|
pipeline._verify()
|
|
|
|
assert pipeline._quality_metrics["kl_budget_status"] == "unavailable"
|
|
assert pipeline._quality_metrics["kl_budget_enforced"] is False
|
|
|
|
|
|
def test_configured_kl_failure_closes_and_releases_baseline_artifacts(pipeline, monkeypatch):
|
|
pipeline._strong_layers = []
|
|
pipeline._baseline_first_token_logits = torch.zeros(1)
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: float("nan")
|
|
pipeline.use_kl_optimization = True
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
events = []
|
|
pipeline._on_stage = events.append
|
|
|
|
with pytest.raises(PipelineFailure, match="configured KL verification failed closed") as caught:
|
|
pipeline._verify()
|
|
|
|
assert caught.value.stage == "verify"
|
|
assert caught.value.metric == "kl_divergence"
|
|
assert pipeline._baseline_first_token_logits is None
|
|
assert pipeline._baseline_token_logits == []
|
|
assert pipeline._kl_eval_prompts == []
|
|
assert events[-1].status == "error"
|
|
assert events[-1].details["metric"] == "kl_divergence"
|
|
|
|
|
|
@pytest.mark.parametrize("measured, expected", [(0.5, "done"), (0.75, "error")])
|
|
def test_configured_kl_checks_the_final_candidate_after_prior_correction(pipeline, measured, expected):
|
|
pipeline.use_kl_optimization = True
|
|
pipeline.kl_budget = 0.5
|
|
pipeline._quality_metrics["kl_divergence"] = 0.1 # Earlier correction passed.
|
|
pipeline._baseline_first_token_logits = torch.zeros(1)
|
|
pipeline._baseline_token_logits = [torch.zeros(1)]
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._measure_sequence_token_kl = lambda: measured
|
|
pipeline._last_first_token_kl = 0.1
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
events = []
|
|
pipeline._on_stage = events.append
|
|
|
|
if expected == "error":
|
|
with pytest.raises(abliterate.PipelineValidationError, match="after all edits"):
|
|
pipeline._verify()
|
|
else:
|
|
pipeline._verify()
|
|
|
|
assert pipeline._quality_metrics["kl_divergence"] == measured
|
|
assert pipeline._quality_metrics["kl_budget_enforced"] is True
|
|
assert pipeline._quality_metrics["kl_budget_status"] == (
|
|
"exceeded" if expected == "error" else "within_budget"
|
|
)
|
|
assert events[-1].status == expected
|
|
assert pipeline._baseline_first_token_logits is None
|
|
assert pipeline._baseline_token_logits == []
|
|
assert pipeline._kl_eval_prompts == []
|
|
|
|
|
|
def test_configured_kl_requires_a_baseline_for_final_verification(pipeline):
|
|
pipeline.use_kl_optimization = True
|
|
pipeline._baseline_token_logits = []
|
|
pipeline._kl_eval_prompts = ["KL"]
|
|
pipeline._admit_pipeline_phase = lambda *_args, **_kwargs: None
|
|
|
|
with pytest.raises(PipelineFailure, match="requires a pristine baseline"):
|
|
pipeline._verify()
|
|
|
|
assert pipeline._quality_metrics["kl_budget_status"] == "unavailable"
|
|
assert pipeline._quality_metrics["kl_budget_enforced"] is True
|
|
assert pipeline._kl_eval_prompts == []
|
|
|
|
|
|
def test_telemetry_preserves_verification_diagnostic_availability():
|
|
report = build_report(
|
|
architecture="OfflineModel",
|
|
num_layers=1,
|
|
num_heads=1,
|
|
hidden_size=5120,
|
|
total_params=1,
|
|
method="basic",
|
|
quality_metrics={
|
|
"kl_budget": 0.5,
|
|
"kl_budget_status": "exceeded",
|
|
"kl_budget_enforced": False,
|
|
"capability_total_count": 6,
|
|
"capability_completed_count": 5,
|
|
"perplexity_increase": 1.18,
|
|
"spectral_samples_used": 72,
|
|
"spectral_samples_required": 72,
|
|
"spectral_layers_expected": 5,
|
|
"spectral_layers_completed": 4,
|
|
"spectral_diagnostic_scope": "rank_one_class_mean_contrast",
|
|
},
|
|
)
|
|
|
|
assert report["quality_metrics"] == {
|
|
"capability_total_count": 6.0,
|
|
"capability_completed_count": 5.0,
|
|
"kl_budget": 0.5,
|
|
"kl_budget_enforced": False,
|
|
"kl_budget_status": "exceeded",
|
|
"perplexity_increase": 1.18,
|
|
"spectral_diagnostic_scope": "rank_one_class_mean_contrast",
|
|
"spectral_layers_completed": 4.0,
|
|
"spectral_layers_expected": 5.0,
|
|
"spectral_samples_required": 72.0,
|
|
"spectral_samples_used": 72.0,
|
|
}
|
|
assert report["quality_metric_status"] == {
|
|
"capability_total_count": "measured",
|
|
"capability_completed_count": "measured",
|
|
"kl_budget": "measured",
|
|
"kl_budget_enforced": "measured",
|
|
"kl_budget_status": "measured",
|
|
"perplexity_increase": "measured",
|
|
"spectral_diagnostic_scope": "measured",
|
|
"spectral_layers_completed": "measured",
|
|
"spectral_layers_expected": "measured",
|
|
"spectral_samples_required": "measured",
|
|
"spectral_samples_used": "measured",
|
|
}
|
|
|
|
|
|
def test_missing_spectral_layer_cannot_be_reported_green():
|
|
pipeline = _spectral_pipeline(128, 20, 20)
|
|
pipeline._strong_layers = [2, 4]
|
|
pipeline._quality_metrics = {}
|
|
pipeline._free_gpu_memory = lambda: None
|
|
pipeline.log = lambda _message: None
|
|
certificate = SimpleNamespace(
|
|
level=CertificationLevel.GREEN,
|
|
diagnostic_reason=None,
|
|
effective_noise_rank=20,
|
|
n_samples_required=30,
|
|
n_samples_used=40,
|
|
)
|
|
certifier = SimpleNamespace(certify=lambda *_args, **_kwargs: certificate)
|
|
activation = torch.ones(128)
|
|
pipeline._prepare_spectral_certification = lambda: (
|
|
certifier,
|
|
[2, 4],
|
|
{2: [activation]},
|
|
{2: [activation]},
|
|
)
|
|
|
|
pipeline._measure_spectral_diagnostic()
|
|
|
|
assert pipeline._quality_metrics["spectral_certification"] == "INCONCLUSIVE"
|
|
assert pipeline._quality_metrics["spectral_diagnostic_reason"] == "missing_layer_activations"
|
|
assert pipeline._quality_metrics["spectral_layers_expected"] == 2
|
|
assert pipeline._quality_metrics["spectral_layers_completed"] == 1
|
|
|
|
|
|
def test_red_spectral_layer_remains_red_when_another_layer_is_missing():
|
|
pipeline = _spectral_pipeline(128, 20, 20)
|
|
pipeline._strong_layers = [2, 4]
|
|
pipeline._quality_metrics = {}
|
|
pipeline._free_gpu_memory = lambda: None
|
|
pipeline.log = lambda _message: None
|
|
certificate = SimpleNamespace(
|
|
level=CertificationLevel.RED,
|
|
diagnostic_reason=None,
|
|
effective_noise_rank=20,
|
|
n_samples_required=30,
|
|
n_samples_used=40,
|
|
)
|
|
activation = torch.ones(128)
|
|
pipeline._prepare_spectral_certification = lambda: (
|
|
SimpleNamespace(certify=lambda *_args, **_kwargs: certificate),
|
|
[2, 4],
|
|
{2: [activation]},
|
|
{2: [activation]},
|
|
)
|
|
|
|
pipeline._measure_spectral_diagnostic()
|
|
|
|
assert pipeline._quality_metrics["spectral_certification"] == "RED"
|
|
assert pipeline._quality_metrics["spectral_diagnostic_reason"] == "missing_layer_activations"
|
|
|
|
|
|
def test_failed_spectral_collection_is_inconclusive():
|
|
pipeline = _spectral_pipeline(128, 20, 20)
|
|
pipeline._strong_layers = [2]
|
|
pipeline._quality_metrics = {}
|
|
pipeline._free_gpu_memory = lambda: None
|
|
pipeline.log = lambda _message: None
|
|
pipeline._prepare_spectral_certification = lambda: (_ for _ in ()).throw(RuntimeError("collect"))
|
|
|
|
pipeline._measure_spectral_diagnostic()
|
|
|
|
assert pipeline._quality_metrics["spectral_certification"] == "INCONCLUSIVE"
|
|
assert pipeline._quality_metrics["spectral_diagnostic_reason"] == "collection_failed"
|
|
assert pipeline._quality_metrics["spectral_layers_completed"] == 0
|
|
|
|
|
|
def test_failed_spectral_layer_is_inconclusive():
|
|
pipeline = _spectral_pipeline(128, 20, 20)
|
|
pipeline._strong_layers = [2, 4]
|
|
pipeline._quality_metrics = {}
|
|
pipeline._free_gpu_memory = lambda: None
|
|
pipeline.log = lambda _message: None
|
|
certificate = SimpleNamespace(
|
|
level=CertificationLevel.GREEN,
|
|
diagnostic_reason=None,
|
|
effective_noise_rank=20,
|
|
n_samples_required=30,
|
|
n_samples_used=40,
|
|
)
|
|
activation = torch.ones(128)
|
|
|
|
def certify(_harmful, _harmless, *, layer_idx):
|
|
if layer_idx == 4:
|
|
raise RuntimeError("analysis")
|
|
return certificate
|
|
|
|
pipeline._prepare_spectral_certification = lambda: (
|
|
SimpleNamespace(certify=certify),
|
|
[2, 4],
|
|
{2: [activation], 4: [activation]},
|
|
{2: [activation], 4: [activation]},
|
|
)
|
|
|
|
pipeline._measure_spectral_diagnostic()
|
|
|
|
assert pipeline._quality_metrics["spectral_certification"] == "INCONCLUSIVE"
|
|
assert pipeline._quality_metrics["spectral_diagnostic_reason"] == "layer_analysis_failed"
|