Files
OBLITERATUS/tests/test_activation_cache_policy.py
Joseph Magly f75168f0b0 test(device): stub MPS and CUDA cache calls in the per-batch release test
The per-batch release test made device.is_mps() return true and then ran
the real free_gpu_memory(), which reached torch.mps.empty_cache() and
torch.mps.synchronize(). On an MPS-capable host this unmarked CPU test could
touch real accelerator state. Stub both MPS calls (and the CUDA cache calls
as a guard) and assert their call counts, so the release path is still
exercised without hardware.
2026-11-27 12:49:10 -05:00

166 lines
8.2 KiB
Python

"""Offline cache/hook contracts; no A100 backend or performance qualification."""
from copy import deepcopy
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models.loader import ModelHandle
@pytest.fixture
def tiny_pipeline():
from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from tokenizers.pre_tokenizers import Whitespace
from transformers import GPT2Config, GPT2LMHeadModel, PreTrainedTokenizerFast
backend = Tokenizer(WordLevel({'[PAD]': 0, '[UNK]': 1, 'one': 2, 'two': 3, 'three': 4}, unk_token='[UNK]'))
backend.pre_tokenizer = Whitespace()
tokenizer = PreTrainedTokenizerFast(tokenizer_object=backend, unk_token='[UNK]', pad_token='[PAD]')
def create(attention):
config = GPT2Config(vocab_size=5, n_positions=16, n_embd=8, n_layer=2, n_head=2,
attn_implementation=attention, use_cache=True,
bos_token_id=None, eos_token_id=None, pad_token_id=0)
with torch.random.fork_rng():
torch.manual_seed(17)
model = GPT2LMHeadModel(config).eval()
pipeline = AbliterationPipeline(model_name='cache-contract', max_seq_length=8)
pipeline.handle = ModelHandle(model=model, tokenizer=tokenizer, config=config,
model_name='cache-contract', task='causal_lm')
pipeline._on_log = lambda message: None
return pipeline
previous_threads = torch.get_num_threads()
torch.set_num_threads(1)
try:
yield create
finally:
torch.set_num_threads(previous_threads)
@pytest.mark.parametrize('attention', ['eager', 'sdpa'])
@pytest.mark.parametrize('multi_position', [False, True])
def test_cache_override_preserves_masked_hooks_and_generation_defaults(tiny_pipeline, attention, multi_position):
pipeline = tiny_pipeline(attention)
pipeline.cot_aware = multi_position
model, tokenizer = pipeline.handle.model, pipeline.handle.tokenizer
assert model.config._attn_implementation == attention
original_config = deepcopy(model.config.to_dict())
original_generation = deepcopy(model.generation_config.to_dict())
inputs = tokenizer('one two', return_tensors='pt')
with torch.no_grad():
before = model.generate(**inputs, max_new_tokens=2, do_sample=False)
observations = []
def observe(module, args, kwargs, output):
observations.append((kwargs.get('use_cache', 'inherited'), output.past_key_values is not None,
output.logits.detach().clone(), kwargs['attention_mask'].clone()))
hook = model.register_forward_hook(observe, with_kwargs=True)
try:
start = len(observations)
enabled = pipeline._collect_activations(model.transformer.h,
['one two three one two three', 'one two'], 'cached', use_cache=True)
after_enabled = len(observations)
disabled = pipeline._collect_activations(model.transformer.h,
['one two three one two three', 'one two'], 'uncached', use_cache=False)
after_disabled = len(observations)
inherited = pipeline._collect_activations(model.transformer.h,
['one two three one two three', 'one two'], 'inherited')
finally:
hook.remove()
# Upstream batches by available memory, so each collection may run one or
# more forwards; the three policies must see identical batching.
by_policy = [
observations[start:after_enabled],
observations[after_enabled:after_disabled],
observations[after_disabled:],
]
assert len(by_policy[0]) == len(by_policy[1]) == len(by_policy[2]) >= 1
for policy, cache, _, _ in by_policy[0]:
assert (policy, cache) == (True, True)
for policy, cache, _, _ in by_policy[1]:
assert (policy, cache) == (False, False)
for policy, cache, _, _ in by_policy[2]:
assert policy == 'inherited' and cache
for with_cache, without_cache, default in zip(*by_policy):
assert torch.equal(with_cache[3], without_cache[3])
assert torch.equal(with_cache[3], default[3])
for _, _, logits, _ in (with_cache, without_cache, default):
assert torch.isfinite(logits).all()
torch.testing.assert_close(logits, with_cache[2], atol=1e-6, rtol=1e-6)
for layer in enabled:
assert len(enabled[layer]) == 2
for cached, uncached, default in zip(enabled[layer], disabled[layer], inherited[layer]):
assert torch.isfinite(cached).all()
torch.testing.assert_close(cached, uncached, atol=1e-6, rtol=1e-6)
torch.testing.assert_close(cached, default, atol=1e-6, rtol=1e-6)
assert tokenizer.padding_side == 'right'
assert model.config.to_dict() == original_config
assert model.generation_config.to_dict() == original_generation
assert all(not layer._forward_hooks for layer in model.transformer.h)
with torch.no_grad():
after = model.generate(**inputs, max_new_tokens=2, do_sample=False)
assert torch.equal(before, after)
def test_invalid_override_rejected_before_hooks_or_forward(tiny_pipeline):
pipeline = tiny_pipeline('eager')
layers = pipeline.handle.model.transformer.h
for value in (0, 1, 'false'):
with pytest.raises(ValueError, match='cache override'):
pipeline._collect_activations(layers, ['one two'], 'invalid', use_cache=value)
assert all(not layer._forward_hooks for layer in layers)
def test_unsupported_override_is_not_silently_retried_and_cleans_hooks(tiny_pipeline, monkeypatch):
pipeline = tiny_pipeline('eager')
model = pipeline.handle.model
calls = []
def unsupported(**kwargs):
calls.append(kwargs)
raise TypeError('backend rejects cache override')
monkeypatch.setattr(model, 'forward', unsupported)
with pytest.raises(TypeError, match='backend rejects'):
pipeline._collect_activations(model.transformer.h, ['one two', 'one'], 'unsupported', use_cache=False)
assert len(calls) == 1 and calls[0]['use_cache'] is False
assert pipeline.handle.tokenizer.padding_side == 'right'
assert all(not layer._forward_hooks for layer in model.transformer.h)
@pytest.mark.parametrize(('accelerator', 'expected_releases'), [(False, 0), (True, 2)])
def test_per_batch_release_skips_gc_on_cpu_and_keeps_accelerator_release(
tiny_pipeline, monkeypatch, accelerator, expected_releases,
):
# #684: on CPU-only hosts the activation loop must not call gc.collect()
# every few batches (its cost scales with the live heap); with an
# accelerator it still releases memory every 4 batches and after the last.
import gc
from unittest.mock import Mock
from obliteratus import device
pipeline = tiny_pipeline('eager')
releases = []
pipeline._free_gpu_memory = lambda: releases.append(True) or device.free_gpu_memory()
pipeline._resolved_activation_collection_limits = lambda _layers: (1, 8, None)
monkeypatch.setattr(device, 'is_cuda', lambda: False)
monkeypatch.setattr(device, 'is_mps', lambda: accelerator)
# Stub every accelerator call free_gpu_memory() can reach so this CPU test
# never touches real MPS or CUDA hardware, even on an MPS-capable host.
mps_empty, mps_sync = Mock(), Mock()
monkeypatch.setattr(device.torch.mps, 'empty_cache', mps_empty, raising=False)
monkeypatch.setattr(device.torch.mps, 'synchronize', mps_sync, raising=False)
cuda_calls = {name: Mock() for name in ('empty_cache', 'synchronize', 'reset_peak_memory_stats')}
for name, stub in cuda_calls.items():
monkeypatch.setattr(device.torch.cuda, name, stub)
collect_calls = []
monkeypatch.setattr(gc, 'collect', lambda *args, **kwargs: collect_calls.append(args) or 0)
prompts = ['one two', 'two three', 'three one', 'one', 'two', 'three', 'one three', 'two one']
rows = pipeline._collect_activations(pipeline.handle.model.transformer.h, prompts, 'release')
assert len(releases) == expected_releases
assert len(collect_calls) == expected_releases
assert mps_empty.call_count == expected_releases
assert mps_sync.call_count == expected_releases
assert all(stub.call_count == 0 for stub in cuda_calls.values())
assert all(len(rows[layer]) == len(prompts) for layer in rows)