mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
261 lines
12 KiB
Python
261 lines
12 KiB
Python
"""Refinement re-admits each phase before its work and reports the failed stage."""
|
|
|
|
import inspect
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import phase_admission
|
|
from obliteratus.abliterate import AbliterationPipeline, PipelineFailure
|
|
from tests.test_phase_memory_helpers import torch_thread_count
|
|
from tests.test_phase_optional_admission import make_pipeline
|
|
|
|
|
|
@pytest.mark.parametrize("rejected", ["probe", "distill", "validate", "excise", None])
|
|
def test_refinement_admission_precedes_each_operation(rejected, monkeypatch):
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, gate_mode="report",
|
|
true_iterative_refinement=True, refinement_passes=2)
|
|
trace = []
|
|
|
|
def admit(stage, **kwargs):
|
|
assert pipeline._active_stage == stage
|
|
if stage == "validate":
|
|
assert kwargs["gate_only"] is True
|
|
elif stage == "excise":
|
|
assert kwargs["work_scope"] == "projection"
|
|
trace.append("admit:" + stage)
|
|
if stage == rejected:
|
|
raise phase_admission.PhaseAdmissionError(stage, "cpu", 100, 0)
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase_impl", admit)
|
|
for stage, operation in (("probe", "_probe"), ("distill", "_distill_inner"),
|
|
("validate", "_validate_concept_gate")):
|
|
monkeypatch.setattr(pipeline, operation, lambda s=stage: trace.append("work:" + s))
|
|
# Use the real exception translator so a nested rejection must identify
|
|
# VALIDATE (or the other fresh phase), not the outer EXCISE operation.
|
|
monkeypatch.setattr(pipeline, "_run_pipeline_stages", pipeline._excise)
|
|
events = []
|
|
pipeline._on_stage = events.append
|
|
if rejected is None:
|
|
pipeline._run_pipeline()
|
|
assert pipeline._active_stage == "excise"
|
|
else:
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline._run_pipeline()
|
|
assert caught.value.stage == rejected
|
|
assert caught.value.metric == "memory_admission"
|
|
assert events[-1].stage == rejected and events[-1].status == "error"
|
|
expected = ["admit:probe", "work:probe", "admit:distill", "work:distill",
|
|
"admit:validate", "work:validate", "admit:excise"]
|
|
if rejected:
|
|
expected = expected[:expected.index("admit:" + rejected) + 1]
|
|
assert trace == expected
|
|
|
|
|
|
def test_single_pass_excise_skips_refinement_lifecycle_hashing(monkeypatch):
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, true_iterative_refinement=False, refinement_passes=1)
|
|
monkeypatch.setattr(
|
|
pipeline, "_refinement_candidate_identity",
|
|
lambda _names: pytest.fail("ordinary single-pass excision must not hash lifecycle targets"),
|
|
)
|
|
monkeypatch.setattr(
|
|
pipeline, "_refinement_candidate_parameter_names",
|
|
lambda: pytest.fail("ordinary single-pass excision must not snapshot lifecycle targets"),
|
|
)
|
|
|
|
pipeline._excise()
|
|
|
|
|
|
def test_converged_iterative_pass_reenters_mutation_before_later_global_edits():
|
|
source = inspect.getsource(AbliterationPipeline._excise_inner)
|
|
branch = source[source.index("if converged:"):source.index("self.log(\" Re-probing")]
|
|
|
|
assert source.index('"candidate_completed"') < source.index("if converged:")
|
|
assert branch.index('"mutation_started"') > branch.index("if converged:")
|
|
assert branch.index('"mutation_started"') < branch.rindex("break")
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
def test_refinement_gate_uses_real_validate_plan_before_materialization(mode, monkeypatch):
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, gate_mode="report",
|
|
true_iterative_refinement=True, refinement_passes=2)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
monkeypatch.setattr(phase_admission, "observe_memory", lambda d:
|
|
phase_admission.MemoryObservation(
|
|
d, phase_admission.DEFAULT_RESERVE_BYTES, 1024**3,
|
|
))
|
|
|
|
def probe():
|
|
pipeline._harmful_acts = {0: [torch.ones(8, 64)]}
|
|
pipeline._harmless_acts = {0: [torch.zeros(8, 64)]}
|
|
|
|
monkeypatch.setattr(pipeline, "_probe", probe)
|
|
monkeypatch.setattr(pipeline, "_distill_inner", lambda: None)
|
|
original = pipeline._admit_pipeline_phase_impl
|
|
plans = []
|
|
|
|
def admit(stage, **kwargs):
|
|
if stage == "validate":
|
|
plans.append(pipeline._gate_admission_host_bytes())
|
|
original(stage, **kwargs)
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase_impl", admit)
|
|
materialize = Mock(side_effect=lambda: pipeline._concept_gate_inputs().materialize())
|
|
monkeypatch.setattr(pipeline, "_validate_concept_gate", materialize)
|
|
if mode == "strict":
|
|
with pytest.raises(phase_admission.PhaseAdmissionError) as caught:
|
|
pipeline._excise()
|
|
assert caught.value.phase == "validate"
|
|
assert pipeline._active_stage == "validate"
|
|
materialize.assert_not_called()
|
|
else:
|
|
pipeline._excise()
|
|
materialize.assert_called_once()
|
|
assert pipeline._active_stage == "excise"
|
|
assert len(plans) == 1 and plans[0] > 0
|
|
|
|
|
|
def test_disabled_admission_still_tracks_active_stage(monkeypatch):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1)
|
|
pipeline._admit_pipeline_phase("validate")
|
|
assert pipeline._active_stage == "validate"
|
|
|
|
|
|
@pytest.mark.parametrize("evaluation", [{"skip_collateral": True}, {"repair": True}])
|
|
def test_refinement_gate_proceeds_without_unused_cuda_evaluation(evaluation, monkeypatch):
|
|
from tests.test_phase_confidence import cuda_metadata_handle
|
|
from tests.test_phase_evaluation_admission import RecordingTokenizer
|
|
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=2, gate_mode="report",
|
|
true_iterative_refinement=True, refinement_passes=2,
|
|
**evaluation)
|
|
cpu_handle = pipeline.handle
|
|
cuda_handle = cuda_metadata_handle()
|
|
cuda_handle.tokenizer = RecordingTokenizer()
|
|
pipeline.gpu_memory_utilization = None
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
monkeypatch.setattr(phase_admission, "observe_memory", lambda d:
|
|
phase_admission.MemoryObservation(
|
|
d, phase_admission.DEFAULT_RESERVE_BYTES
|
|
+ (1024**3 if d == "cpu" else 1024**2), 2 * 1024**3,
|
|
))
|
|
|
|
def probe():
|
|
pipeline._harmful_acts = {i: [torch.ones(8, 64)] for i in range(2)}
|
|
pipeline._harmless_acts = {i: [torch.zeros(8, 64)] for i in range(2)}
|
|
|
|
probe()
|
|
pipeline.handle = cuda_handle
|
|
with pytest.raises(phase_admission.PhaseAdmissionError) as caught:
|
|
pipeline._admit_pipeline_phase("validate")
|
|
assert caught.value.device == "cuda:0"
|
|
assert caught.value.required_bytes > 1024**2
|
|
pipeline.handle = cpu_handle
|
|
original = pipeline._admit_pipeline_phase_impl
|
|
checked = []
|
|
|
|
def admit(stage, **options):
|
|
# Gate admission sees the reviewer's 1 MiB CUDA headroom; the
|
|
# actual projection admission and surgery use the tiny CPU model.
|
|
if stage in {"validate", "excise"}:
|
|
pipeline.handle = cuda_handle if stage == "validate" else cpu_handle
|
|
try:
|
|
original(stage, **options)
|
|
checked.append(stage)
|
|
finally:
|
|
pipeline.handle = cpu_handle
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase_impl", admit)
|
|
monkeypatch.setattr(pipeline, "_probe", probe)
|
|
monkeypatch.setattr(pipeline, "_distill_inner", lambda: None)
|
|
gate = Mock(side_effect=lambda: pipeline._concept_gate_inputs().materialize())
|
|
monkeypatch.setattr(pipeline, "_validate_concept_gate", gate)
|
|
unused = Mock(side_effect=AssertionError("refinement must not recapture evaluation"))
|
|
monkeypatch.setattr(pipeline, "_capture_concept_evaluation", unused)
|
|
monkeypatch.setattr(pipeline, "_capture_concept_repair", unused)
|
|
pipeline._excise()
|
|
assert checked == ["validate", "excise"]
|
|
gate.assert_called_once()
|
|
unused.assert_not_called()
|
|
assert pipeline._active_stage == "excise"
|
|
|
|
|
|
@pytest.mark.parametrize("scope", ["surgery", "projection"])
|
|
def test_scoped_excise_excludes_driver_capture_and_evaluation(scope, monkeypatch):
|
|
from tests.test_phase_confidence import cuda_metadata_handle
|
|
from tests.test_phase_evaluation_admission import RecordingTokenizer
|
|
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=2, repair=True)
|
|
pipeline.handle = cuda_metadata_handle()
|
|
pipeline.handle.tokenizer = RecordingTokenizer()
|
|
pipeline.harmless_prompts = ["capture"] * 10
|
|
pipeline.gpu_memory_utilization = None
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
monkeypatch.setattr(phase_admission, "observe_memory", lambda d:
|
|
phase_admission.MemoryObservation(
|
|
d, phase_admission.DEFAULT_RESERVE_BYTES
|
|
+ (1024**3 if d == "cpu" else 2 * 1024**2), 2 * 1024**3,
|
|
))
|
|
with pytest.raises(phase_admission.PhaseAdmissionError):
|
|
pipeline._admit_pipeline_phase("excise")
|
|
pipeline.handle.tokenizer.calls.clear()
|
|
pipeline._admit_pipeline_phase("excise", work_scope=scope)
|
|
assert pipeline.handle.tokenizer.calls == []
|
|
|
|
|
|
def test_standalone_surgery_keeps_actual_kl_scoring_forwards(monkeypatch):
|
|
from tests.test_phase_evaluation_admission import RecordingTokenizer
|
|
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, repair=True)
|
|
pipeline.handle.tokenizer = RecordingTokenizer()
|
|
pipeline.use_kl_optimization = True
|
|
pipeline._baseline_token_logits = [torch.zeros(3, 32)]
|
|
pipeline._kl_eval_prompts = ["existing KL prompt"]
|
|
workloads, host = pipeline._phase_forward_workloads(
|
|
"excise", 1, 256, surgery_only=True,
|
|
)
|
|
assert workloads and host > 0
|
|
assert [prompt for prompt, _ in pipeline.handle.tokenizer.calls] == ["existing KL prompt"]
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
def test_standalone_gate_does_not_admit_unused_forward_or_repair_capture(mode, monkeypatch):
|
|
from tests.test_phase_confidence import cuda_metadata_handle
|
|
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, gate_mode="report", repair=True)
|
|
pipeline.handle = cuda_metadata_handle()
|
|
pipeline.gpu_memory_utilization = None
|
|
pipeline._harmful_acts = {0: [torch.ones(8, 64)]}
|
|
pipeline._harmless_acts = {0: [torch.zeros(8, 64)]}
|
|
required = pipeline._gate_admission_host_bytes()
|
|
assert required > 0
|
|
unused = Mock(side_effect=AssertionError("standalone gate planned unrelated work"))
|
|
for name in ("_phase_forward_workloads", "_repair_capture_host_bytes",
|
|
"_repair_capture_device_bytes"):
|
|
monkeypatch.setattr(pipeline, name, unused)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
monkeypatch.setattr(phase_admission, "observe_memory", lambda d:
|
|
phase_admission.MemoryObservation(
|
|
d, phase_admission.DEFAULT_RESERVE_BYTES
|
|
+ (required if d == "cpu" else 0), 1024**3,
|
|
))
|
|
# The host gate fits exactly. There is no additional GPU headroom for
|
|
# evaluation forwards that these standalone callers never execute.
|
|
pipeline._admit_pipeline_phase("validate", gate_only=True)
|
|
unused.assert_not_called()
|
|
assert pipeline._active_stage == "validate"
|