Files
OBLITERATUS/tests/test_phase_inputs_admission.py
Joseph MaglyandClaude Opus 5.5 1569e8115a fix(gpu): keep optional diagnostics and cached CUDA memory out of rejections
Admit OOM-tolerant generation diagnostics and KL capture separately from
mandatory work; when their admission fails, the existing diagnostic-skip
path runs instead of failing the phase. Mandatory demand is unchanged and
still rejects in default and strict mode.

Release unused allocator cache before observing CUDA memory at every
boundary, as the main pipeline already did, so the informed and benchmark
transitions cannot reject work the cache would serve. Resolve an explicit
unindexed "cuda" device map to the current CUDA device in admit_model_load.

Shrink the warning-scope tests by stubbing model work they do not test.

Refs #245

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 16:04:12 -04:00

75 lines
3.6 KiB
Python

"""Admission and execution consume the same phase selection contracts."""
from unittest.mock import Mock
import pytest
from transformers import LlamaConfig, LlamaForCausalLM
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models.loader import ModelHandle
from tests.test_phase_collections_admission import LengthTokenizer
@pytest.fixture
def pipeline(tmp_path, monkeypatch):
config = LlamaConfig(vocab_size=32, hidden_size=16, intermediate_size=32,
num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=1)
model = LlamaForCausalLM(config).eval()
result = AbliterationPipeline("local", method="basic", output_dir=tmp_path / "export",
harmful_prompts=["8"] * 16, harmless_prompts=["8"] * 16)
result.handle = ModelHandle(model, LengthTokenizer(), config, "local", "causal_lm")
monkeypatch.setattr(result, "_format_generation_prompt", lambda prompt: prompt)
monkeypatch.setattr(result, "_maybe_apply_chat_template", lambda prompts, **_: prompts)
monkeypatch.setattr(result, "_free_gpu_memory", lambda: None)
return result
@pytest.mark.parametrize("phase,selector,work,selected", [
("baseline", "_stock_coherence_prompts", "_stock_coherence", ["17"]),
("verify", "_verification_prompts", "_verify", (["17"], [])),
])
def test_forward_phase_uses_execution_prompt_selector(
pipeline, monkeypatch, phase, selector, work, selected,
):
selection = Mock(return_value=selected)
monkeypatch.setattr(pipeline, selector, selection)
workloads, _ = pipeline._phase_forward_workloads(phase, 1, 256, diagnostic="coherence")
assert any(w.tokens == 17 and w.logits_copies == 3 for w in workloads)
selection.assert_called_once()
# Enter the real phase work and stop exactly at its shared selection boundary.
selection.reset_mock()
selection.side_effect = RuntimeError("reached shared selector")
with pytest.raises(RuntimeError, match="reached shared selector"):
getattr(pipeline, work)()
selection.assert_called_once()
@pytest.mark.parametrize("retain", [False, True])
def test_generation_hidden_retention_uses_generation_config(pipeline, monkeypatch, retain):
gen = pipeline.handle.model.generation_config
gen.output_hidden_states = True
gen.return_dict_in_generate = retain
monkeypatch.setattr(pipeline, "_stock_coherence_prompts", lambda: ["17"])
workloads, _ = pipeline._phase_forward_workloads("baseline", 1, 256, diagnostic="coherence")
generation = [w for w in workloads if w.output_hidden_states is True]
assert len(generation) == 2
assert generation[0].retained_hidden_tokens == 0
assert generation[1].retained_hidden_tokens == (17 + 64 - 2 if retain else 0)
def test_rebirth_admission_and_execution_use_same_native_planner(pipeline, monkeypatch):
from obliteratus.models import checkpoint_export
from obliteratus.phase_admission import model_phase_demand
planner = Mock(wraps=checkpoint_export.plan_native_export)
monkeypatch.setattr(checkpoint_export, "plan_native_export", planner)
model_phase_demand(pipeline.handle, "rebirth", max_shard_size=1024)
planner.assert_called_once_with(pipeline.handle.model, max_shard_size=1024)
planner.reset_mock()
planner.side_effect = RuntimeError("reached shared planner")
monkeypatch.setattr(pipeline, "_build_metadata", lambda: {})
with pytest.raises(RuntimeError, match="reached shared planner"):
pipeline._rebirth(max_shard_size=1024)
planner.assert_called_once_with(pipeline.handle.model, max_shard_size=1024)
assert not pipeline.output_dir.exists()