mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
235 lines
8.9 KiB
Python
235 lines
8.9 KiB
Python
"""Readout geometry, token selection and observation lifecycle contracts."""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from torch import nn
|
|
|
|
from obliteratus.analysis.activation_observer import (
|
|
decoder_output_readout,
|
|
deepseek_v41_post_block_readout,
|
|
observe_activations,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def test_observer_is_available_through_lazy_public_api():
|
|
from obliteratus.analysis import observe_activations as public_observer
|
|
|
|
assert public_observer is observe_activations
|
|
|
|
|
|
class HyperBlock(nn.Module):
|
|
"""Small fixture with the V4.1 decoder output contract."""
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.attn_hc = SimpleNamespace(hc_mult=4)
|
|
self.self_attn = SimpleNamespace(config=SimpleNamespace(hidden_size=2))
|
|
|
|
@staticmethod
|
|
def hc_collapse(streams, pre):
|
|
return (streams.float() * pre.unsqueeze(-1)).sum(2).to(streams.dtype)
|
|
|
|
def forward(self, streams, pre):
|
|
return streams, pre
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
|
|
def test_hyperconnection_observation_preserves_outputs_and_uses_weighted_readout(dtype):
|
|
layer = HyperBlock()
|
|
streams = torch.tensor([[[[1., 3.], [5., 7.], [9., 11.], [13., 15.]]]], dtype=dtype)
|
|
pre = torch.tensor([[[0.125, 0.125, 0.25, 0.5]]])
|
|
versions = streams._version, pre._version
|
|
with observe_activations(
|
|
{"decoder.0": layer}, attention_mask=torch.ones(1, 1), phase="answer",
|
|
readout=deepseek_v41_post_block_readout, readout_name="deepseek_v41.post_block.v1",
|
|
) as result:
|
|
output = layer(streams, pre)
|
|
assert output[0] is streams and output[1] is pre
|
|
assert (streams._version, pre._version) == versions
|
|
observation = result["decoder.0"]
|
|
torch.testing.assert_close(observation.values, torch.tensor([[9.5, 11.5]], dtype=dtype))
|
|
assert observation.readout == "deepseek_v41.post_block.v1"
|
|
assert observation.phase == "answer"
|
|
assert not layer._forward_hooks
|
|
|
|
|
|
def test_left_right_and_gapped_masks_select_last_valid_tokens():
|
|
layer = nn.Identity()
|
|
hidden = torch.arange(24.).reshape(3, 4, 2).requires_grad_()
|
|
mask = torch.tensor([[0, 0, 1, 1], [1, 1, 0, 0], [1, 0, 1, 0]])
|
|
with observe_activations({"layer": layer}, attention_mask=mask, phase="prompt") as result:
|
|
output = layer(hidden)
|
|
observation = result["layer"]
|
|
assert output is hidden
|
|
assert observation.token_positions.tolist() == [3, 1, 2]
|
|
assert observation.values.tolist() == [[6., 7.], [10., 11.], [20., 21.]]
|
|
assert observation.values.device.type == "cpu"
|
|
assert not observation.values.requires_grad
|
|
output.sum().backward()
|
|
assert torch.equal(hidden.grad, torch.ones_like(hidden))
|
|
with torch.no_grad():
|
|
output.zero_()
|
|
assert observation.values[0].tolist() == [6., 7.]
|
|
|
|
|
|
def test_explicit_positions_select_valid_context_tokens_and_are_copied():
|
|
layer = nn.Identity()
|
|
mask = torch.tensor([[0, 1, 1], [1, 1, 0]])
|
|
positions = torch.tensor([1, 0])
|
|
hidden = torch.arange(12.).reshape(2, 3, 2)
|
|
with observe_activations(
|
|
{"layer": layer}, attention_mask=mask, phase="tool", token_positions=positions,
|
|
) as result:
|
|
mask.zero_()
|
|
positions.fill_(2)
|
|
layer(hidden)
|
|
assert result["layer"].token_positions.tolist() == [1, 0]
|
|
assert result["layer"].values.tolist() == [[2., 3.], [6., 7.]]
|
|
|
|
|
|
def test_current_decode_mask_excludes_cached_history():
|
|
layer = nn.Identity()
|
|
hidden = torch.ones(1, 1, 2)
|
|
with observe_activations({"layer": layer}, attention_mask=torch.ones(1, 1), phase="answer"):
|
|
layer(hidden)
|
|
with pytest.raises(ValueError, match="current call"):
|
|
with observe_activations(
|
|
{"layer": layer}, attention_mask=torch.ones(1, 12), phase="answer",
|
|
) as result:
|
|
layer(hidden)
|
|
assert not result and not layer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("mask", [
|
|
None, torch.ones(3), torch.ones(0, 2), torch.ones(1, 0), torch.zeros(1, 2),
|
|
torch.tensor([[1., float("nan")]]), torch.tensor([[1, 2]]),
|
|
])
|
|
def test_invalid_masks_fail_before_hooks(mask):
|
|
layer = nn.Identity()
|
|
with pytest.raises(ValueError, match="mask"):
|
|
with observe_activations({"layer": layer}, attention_mask=mask, phase="prompt"):
|
|
pytest.fail("invalid mask accepted")
|
|
assert not layer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("positions", [
|
|
[1], torch.tensor([1.]), torch.tensor([[1]]), torch.tensor([-1]),
|
|
torch.tensor([3]), torch.tensor([0]),
|
|
])
|
|
def test_invalid_positions_fail_before_hooks(positions):
|
|
layer = nn.Identity()
|
|
with pytest.raises(ValueError, match="positions"):
|
|
with observe_activations(
|
|
{"layer": layer}, attention_mask=torch.tensor([[0, 1, 1]]),
|
|
phase="prompt", token_positions=positions,
|
|
):
|
|
pytest.fail("invalid position accepted")
|
|
assert not layer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("overrides", [
|
|
{"layers": {}}, {"layers": {"layer": object()}}, {"layers": {"": nn.Identity()}},
|
|
{"phase": " "}, {"readout": None}, {"readout_name": ""},
|
|
{"readout": deepseek_v41_post_block_readout},
|
|
])
|
|
def test_observation_identity_is_required(overrides):
|
|
kwargs = dict(layers={"layer": nn.Identity()}, attention_mask=torch.ones(1, 1), phase="prompt")
|
|
kwargs.update(overrides)
|
|
with pytest.raises(ValueError):
|
|
with observe_activations(**kwargs):
|
|
pytest.fail("invalid contract accepted")
|
|
|
|
|
|
def test_duplicate_modules_and_missing_execution_are_rejected():
|
|
layer = nn.Identity()
|
|
with pytest.raises(ValueError, match="distinct"):
|
|
with observe_activations(
|
|
{"a": layer, "b": layer}, attention_mask=torch.ones(1, 1), phase="prompt",
|
|
):
|
|
pytest.fail("duplicate layer accepted")
|
|
with pytest.raises(ValueError, match="Not all"):
|
|
with observe_activations({"a": layer}, attention_mask=torch.ones(1, 1), phase="prompt"):
|
|
pass
|
|
assert not layer._forward_hooks
|
|
|
|
|
|
def test_repeated_forward_discards_incomplete_observation():
|
|
layer = nn.Identity()
|
|
with pytest.raises(ValueError, match="new observation scope"):
|
|
with observe_activations(
|
|
{"layer": layer}, attention_mask=torch.ones(1, 1), phase="prompt",
|
|
) as result:
|
|
layer(torch.ones(1, 1, 2))
|
|
layer(torch.ones(1, 1, 2))
|
|
assert not result and not layer._forward_hooks
|
|
|
|
|
|
def test_registration_and_body_failures_remove_hooks(monkeypatch):
|
|
first, second = nn.Identity(), nn.Identity()
|
|
|
|
def fail_registration(*args, **kwargs):
|
|
raise RuntimeError("registration failed")
|
|
|
|
monkeypatch.setattr(second, "register_forward_hook", fail_registration)
|
|
with pytest.raises(RuntimeError, match="registration failed"):
|
|
with observe_activations(
|
|
{"first": first, "second": second}, attention_mask=torch.ones(1, 1), phase="prompt",
|
|
):
|
|
pytest.fail("registration failure swallowed")
|
|
assert not first._forward_hooks
|
|
with pytest.raises(RuntimeError, match="forward failed"):
|
|
with observe_activations(
|
|
{"first": first}, attention_mask=torch.ones(1, 1), phase="prompt",
|
|
) as result:
|
|
first(torch.ones(1, 1, 2))
|
|
raise RuntimeError("forward failed")
|
|
assert not result and not first._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("output", [(), None, torch.ones(1, 2), (torch.ones(1, 1, 4, 2),)])
|
|
def test_generic_readout_rejects_unsupported_layouts(output):
|
|
with pytest.raises(ValueError, match="Decoder readout"):
|
|
decoder_output_readout(nn.Identity(), output)
|
|
|
|
|
|
def test_tuple_readout_retains_tensor_identity():
|
|
hidden = torch.ones(1, 1, 2)
|
|
assert decoder_output_readout(nn.Identity(), (hidden, "cache")) is hidden
|
|
|
|
|
|
@pytest.mark.parametrize("hidden", [
|
|
torch.ones(1, 1, 0), torch.ones(1, 1, 2, dtype=torch.long),
|
|
torch.full((1, 1, 2), float("nan")),
|
|
])
|
|
def test_invalid_activation_rejects_without_mutation(hidden):
|
|
layer = nn.Identity()
|
|
version = hidden._version
|
|
with pytest.raises(ValueError):
|
|
with observe_activations({"layer": layer}, attention_mask=torch.ones(1, 1), phase="prompt"):
|
|
layer(hidden)
|
|
assert hidden._version == version
|
|
assert not layer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("output", [
|
|
None, (torch.ones(1, 1, 4, 2),), (None, None),
|
|
(torch.ones(1, 1, 4, 2), torch.ones(1, 1, 3)),
|
|
(torch.ones(1, 1, 4, 2, dtype=torch.long), torch.ones(1, 1, 4)),
|
|
(torch.ones(1, 1, 3, 2), torch.ones(1, 1, 3)),
|
|
(torch.ones(1, 1, 4, 3), torch.ones(1, 1, 4)),
|
|
])
|
|
def test_hyperconnection_readout_requires_matching_contract(output):
|
|
with pytest.raises(ValueError, match="V4.1"):
|
|
deepseek_v41_post_block_readout(HyperBlock(), output)
|
|
|
|
|
|
def test_hyperconnection_readout_requires_native_collapse():
|
|
layer = HyperBlock()
|
|
layer.hc_collapse = None
|
|
with pytest.raises(ValueError, match="native hc_collapse"):
|
|
deepseek_v41_post_block_readout(layer, (torch.ones(1, 1, 4, 2), torch.ones(1, 1, 4)))
|