"""Temporal traces: phases, cache accounting, alignment, labelled modes and cleanup.""" from __future__ import annotations import copy import pytest import torch from torch import nn from obliteratus.analysis.activation_observer import decoder_output_readout from obliteratus.analysis.temporal_trace import ( CachedExecutionUnsupported, _cache_length, trace_free_running, trace_teacher_forced, ) from obliteratus.analysis.trace_compare import ( TraceMismatch, compare_traces, qualify_cached_execution, ) pytestmark = pytest.mark.cpu SEQUENCE = [3, 4, 5, 6, 7, 8, 9] PROMPT = 3 def tiny_gpt2(seed=0): from transformers import GPT2Config, GPT2LMHeadModel torch.manual_seed(seed) config = GPT2Config(n_layer=2, n_embd=16, n_head=2, vocab_size=40, n_positions=32, bos_token_id=1, eos_token_id=2) return GPT2LMHeadModel(config).eval() def sites(model): return {f"layer.{i}": block for i, block in enumerate(model.transformer.h)} def no_hooks(model): return all(not m._forward_hooks and not m._forward_pre_hooks for m in model.modules()) @pytest.fixture def model(): return tiny_gpt2() def test_cached_and_uncached_matched_prefix_traces_agree(model): report = qualify_cached_execution(model, sites(model), SEQUENCE, PROMPT, atol=1e-5) assert report["passed"] and report["each_position_observed_once"] assert report["backend"] == "transformers-cpu-past_key_values" comparison = report["comparison"] assert comparison["mode"] == "matched-prefix" and comparison["aligned_positions"] == 7 assert comparison["max_abs_diff"] <= 1e-5 assert report["decode_calls"] == 4 # Decode calls read 3, 4, 5 and 6 cached tokens; the prefill reads none. assert report["cache_history_read"] == 3 + 4 + 5 + 6 def test_cached_history_is_never_recorded_as_new_tokens(model): cached = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, cache="cached") assert [c.phase for c in cached.calls] == ["prefill"] + ["decode"] * 4 assert [(c.start, c.end, c.cache_history) for c in cached.calls] == [ (0, 3, 0), (3, 4, 3), (4, 5, 4), (5, 6, 5), (6, 7, 6)] assert [c.decode_step for c in cached.calls] == [None, 0, 1, 2, 3] assert cached.call_index.tolist() == [0, 0, 0, 1, 2, 3, 4] assert all(v.shape == (7, 16) for v in cached.values.values()) assert cached.call_mask(0).sum() == 3 and cached.call_mask(4).tolist()[-1] uncached = trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 3, cache="uncached") assert [c.recomputed_history for c in uncached.calls] == [0, 3, 4, 5] assert all(c.cache_history == 0 for c in uncached.calls) assert uncached.call_index.tolist() == [0, 0, 0, 1, 2, 3] assert all(v.shape[0] == uncached.length == 6 for v in uncached.values.values()) class DropsCache(nn.Module): """Accepts cache arguments but recomputes nothing from them and returns no cache.""" def __init__(self, inner, return_cache): super().__init__() self.inner, self.return_cache = inner, return_cache def forward(self, input_ids, past_key_values=None, use_cache=None): output = self.inner(input_ids=input_ids, use_cache=self.return_cache) if not self.return_cache: output.past_key_values = None return output class NoCacheApi(nn.Module): def __init__(self, inner): super().__init__() self.inner = inner def forward(self, input_ids, use_cache=None): return self.inner(input_ids=input_ids, use_cache=False) @pytest.mark.parametrize("return_cache", [False, True]) def test_unsupported_cached_execution_refuses_instead_of_falling_back(model, return_cache): wrapper = DropsCache(model, return_cache).eval() with pytest.raises(CachedExecutionUnsupported, match="cache"): trace_teacher_forced(wrapper, sites(model), SEQUENCE, PROMPT, cache="cached") assert no_hooks(model) with pytest.raises(CachedExecutionUnsupported, match="cached tracing is unavailable"): qualify_cached_execution(NoCacheApi(model).eval(), sites(model), SEQUENCE, PROMPT) # The uncached condition is still available and labelled as such. assert trace_teacher_forced(NoCacheApi(model).eval(), sites(model), SEQUENCE, PROMPT, cache="uncached").cache == "uncached" def test_cache_length_accepts_legacy_tuples_and_refuses_unknown_caches(): legacy = ((torch.zeros(1, 2, 5, 4), torch.zeros(1, 2, 5, 4)),) assert _cache_length(legacy) == 5 with pytest.raises(CachedExecutionUnsupported, match="not qualified"): _cache_length(object()) def test_matched_prefix_positions_align_across_conditions(model): edited = copy.deepcopy(model) with torch.no_grad(): edited.transformer.h[1].mlp.c_proj.weight.mul_(1.5) base = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, condition="base") other = trace_teacher_forced(edited, sites(edited), SEQUENCE, PROMPT, cache="cached", condition="edited") report = compare_traces(base, other) assert report["mode"] == "matched-prefix" and report["aligned_positions"] == 7 assert len(report["max_abs_diff_by_position"]) == 7 assert report["max_abs_diff_by_layer"]["layer.0"] < 1e-5 < report["max_abs_diff_by_layer"]["layer.1"] assert not report["within_tolerance"] assert (report["left"]["condition"], report["right"]["condition"]) == ("base", "edited") shifted = trace_teacher_forced(model, sites(model), SEQUENCE[1:] + [10], PROMPT) with pytest.raises(TraceMismatch, match="identical token sequences"): compare_traces(base, shifted) with pytest.raises(TraceMismatch, match="prompt boundary"): compare_traces(base, trace_teacher_forced(model, sites(model), SEQUENCE, 4)) with pytest.raises(TraceMismatch, match="different layers"): compare_traces(base, trace_teacher_forced(model, {"layer.0": model.transformer.h[0]}, SEQUENCE, PROMPT)) with pytest.raises(ValueError, match="Tolerance"): compare_traces(base, base, atol=-1) def test_teacher_forced_and_free_running_are_distinct_labelled_modes(model): forced = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT) free = trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 4) assert (forced.mode, free.mode) == ("teacher-forced", "free-running") with pytest.raises(TraceMismatch, match="separate modes"): compare_traces(forced, free) other = tiny_gpt2(seed=7) divergent = trace_free_running(other, sites(other), SEQUENCE[:PROMPT], 4) report = compare_traces(free, divergent) assert report["mode"] == "free-running" and report["matched_prefix"] is False assert report["diverged_at"] == report["shared_prefix_tokens"] >= PROMPT assert len(report["max_abs_diff_by_position"]) == report["shared_prefix_tokens"] same = compare_traces(free, trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 4, cache="uncached")) assert same["tokens_identical"] and same["diverged_at"] is None shorter = trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 2) assert compare_traces(free, shorter)["diverged_at"] == shorter.length def test_outputs_are_identical_with_and_without_observation(model): prompt = torch.tensor([SEQUENCE[:PROMPT]]) reference = model.generate(prompt, max_new_tokens=5, do_sample=False, pad_token_id=0) with torch.no_grad(): plain = model(torch.tensor([SEQUENCE]), use_cache=False).logits[0] for cache in ("cached", "uncached"): free = trace_free_running(model, sites(model), prompt, 5, cache=cache) assert free.token_ids.tolist() == reference[0].tolist() forced = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, cache=cache, keep_logits=True) assert torch.allclose(forced.logits, plain, atol=1e-5, rtol=0) assert no_hooks(model) with torch.no_grad(): assert torch.equal(model(torch.tensor([SEQUENCE]), use_cache=False).logits[0], plain) uncached = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, keep_logits=True) assert torch.equal(uncached.logits, plain) def test_free_running_stops_after_eos_and_observes_it(model): first = int(trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 1).token_ids[-1]) trace = trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 6, eos_token_ids=[first]) assert trace.token_ids.tolist() == SEQUENCE[:PROMPT] + [first] assert trace.call_index.tolist() == [0, 0, 0, 1] assert trace.segment_mask("continuation").tolist() == [False] * 3 + [True] assert trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 0).length == PROMPT @pytest.mark.parametrize("error", [RuntimeError, KeyboardInterrupt]) def test_hooks_are_removed_on_cancel_and_exception(model, error): calls = [] def cancel(): calls.append(1) if len(calls) == 3: raise error("stop") with pytest.raises(error): trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, cache="cached", check_cancelled=cancel) assert no_hooks(model) def broken(_layer, _output): raise RuntimeError("readout failed") with pytest.raises(RuntimeError, match="readout failed"): trace_free_running(model, sites(model), SEQUENCE[:PROMPT], 2, readout=broken, readout_name="broken") assert no_hooks(model) def test_thinking_label_is_stored_as_metadata_only(model): plain = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, cache="cached") labelled = trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, cache="cached", labels={"thinking": [(3, 6)]}) assert labelled.labels == {"thinking": { "spans": [[3, 6]], "source": "caller", "verified": False, "semantics": "caller-provided metadata; not detected or verified"}} assert labelled.calls == plain.calls and torch.equal(labelled.call_index, plain.call_index) assert all(torch.equal(labelled.values[k], plain.values[k]) for k in plain.values) assert {c.phase for c in labelled.calls} == {"prefill", "decode"} assert labelled.label_mask("thinking").tolist() == [False] * 3 + [True] * 3 + [False] for bad in ({"thinking": [(5, 5)]}, {"thinking": [(0, 99)]}, {"": [(0, 1)]}, {"thinking": [(True, 2)]}): with pytest.raises(ValueError, match="label|Label"): trace_teacher_forced(model, sites(model), SEQUENCE, PROMPT, labels=bad) def test_invalid_inputs_are_refused_before_tracing(model): layers = sites(model) with pytest.raises(ValueError, match="one unpadded sequence"): trace_teacher_forced(model, layers, [[1, 2], [3, 4]], 1) with pytest.raises(ValueError, match="prompt_tokens"): trace_teacher_forced(model, layers, SEQUENCE, 0) with pytest.raises(ValueError, match="Cache condition"): trace_teacher_forced(model, layers, SEQUENCE, PROMPT, cache="paged") with pytest.raises(ValueError, match="condition"): trace_teacher_forced(model, layers, SEQUENCE, PROMPT, condition=" ") with pytest.raises(ValueError, match="max_new_tokens"): trace_free_running(model, layers, SEQUENCE[:PROMPT], -1) with pytest.raises(ValueError, match="distinct named layer"): trace_teacher_forced(model, {"a": layers["layer.0"], "b": layers["layer.0"]}, SEQUENCE, PROMPT) with pytest.raises(ValueError, match="Not every traced layer"): trace_teacher_forced(model, {"extra": nn.Linear(2, 2)}, SEQUENCE, PROMPT) model.train() with pytest.raises(ValueError, match="eval mode"): trace_teacher_forced(model, layers, SEQUENCE, PROMPT) model.eval() model.config.is_encoder_decoder = True with pytest.raises(CachedExecutionUnsupported, match="decoder-only"): trace_teacher_forced(model, layers, SEQUENCE, PROMPT) model.config.is_encoder_decoder = False assert no_hooks(model) def test_readout_and_comparison_contracts_are_enforced(model): layers = sites(model) def flat(layer, output): return decoder_output_readout(layer, output)[0] def extra_token(layer, output): hidden = decoder_output_readout(layer, output) return torch.cat([hidden, hidden[:, -1:]], dim=1) def nonfinite(layer, output): return decoder_output_readout(layer, output) * float("inf") for readout, error, match in ((flat, ValueError, r"\[1, tokens, hidden\]"), (extra_token, CachedExecutionUnsupported, "recorded as new"), (nonfinite, ValueError, "nonfinite")): with pytest.raises(error, match=match): trace_teacher_forced(model, layers, SEQUENCE, PROMPT, cache="cached", readout=readout, readout_name="custom") assert no_hooks(model) base = trace_teacher_forced(model, layers, SEQUENCE, PROMPT) with pytest.raises(ValueError, match="Segment"): base.segment_mask("thinking") def half(layer, output): return decoder_output_readout(layer, output)[..., :8] with pytest.raises(TraceMismatch, match="different readouts"): compare_traces(base, trace_teacher_forced(model, layers, SEQUENCE, PROMPT, readout=half, readout_name="custom")) narrow = trace_teacher_forced(model, layers, SEQUENCE, PROMPT, readout=half, readout_name="decoder_output") with pytest.raises(TraceMismatch, match="hidden sizes differ"): compare_traces(base, narrow)