Files
OBLITERATUS/tests/test_phase_kl_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

43 lines
2.1 KiB
Python

"""KL capture retains the actual logits precision; later KL arithmetic uses FP32."""
from unittest.mock import Mock
import pytest
import torch
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.mark.parametrize("dtype,itemsize", [(torch.bfloat16, 2), (torch.float32, 4)])
def test_kl_capture_uses_real_logits_dtype_and_shared_prompt_selection(
monkeypatch, dtype, itemsize,
):
config = LlamaConfig(vocab_size=1024, hidden_size=16, intermediate_size=32,
num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=1)
model = LlamaForCausalLM(config).to(dtype=dtype).eval()
pipeline = AbliterationPipeline("local", method="basic", harmful_prompts=["16"] * 16,
harmless_prompts=["16"] * 16)
pipeline.handle = ModelHandle(model, LengthTokenizer(), config, "local", "causal_lm")
monkeypatch.setattr(pipeline, "_maybe_apply_chat_template", lambda prompts, **_: prompts)
monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None)
selection = Mock(wraps=pipeline._kl_capture_prompts)
monkeypatch.setattr(pipeline, "_kl_capture_prompts", selection)
work, host = pipeline._phase_forward_workloads("excise", 1, 256, diagnostic="kl_capture")
assert host == (16 * 16 + 2 * 16) * 1024 * itemsize
assert all(w.logits_dtype_bytes == itemsize for w in work)
selection.assert_called()
selection.reset_mock()
# Exercise capture's selector independently of the separate admission check.
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", lambda *a, **kw: None)
pipeline._capture_baseline_kl_logits()
selection.assert_called_once()
assert {t.dtype for t in pipeline._baseline_token_logits} == {dtype}
retained = sum(t.numel() * t.element_size() for t in pipeline._baseline_token_logits)
first = pipeline._baseline_first_token_logits
assert host == retained + 2 * first.numel() * first.element_size()
if dtype == torch.bfloat16:
assert host == 589_824