mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Whole-parameter and snapshot finite checks on VERIFY paths now walk the same bounded C-order chunks as tensor_digest, staying within the reserved 8 MiB working term. VERIFY's combined admission also counts EXCISE's CPU projection storage when tensors live on the host. Standalone EXCISE planning is unchanged. Digest regressions now cover rank-3/4 permuted views, stepped slices and non-contiguous leading singleton and zero dimensions, checked against the previous whole-tensor algorithm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
535 lines
18 KiB
Python
535 lines
18 KiB
Python
"""Pure deterministic PROBE/DISTILL contracts for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
import obliteratus.distributed.probe as probe_module
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError,
|
|
RuntimeContractError,
|
|
canonical_record,
|
|
contract_digest,
|
|
)
|
|
from obliteratus.distributed.probe_contracts import (
|
|
DirectionMethod,
|
|
LayerProbeArtifact,
|
|
ProbeDistillConfig,
|
|
ProbeDistillSummary,
|
|
ProbeSample,
|
|
canonicalize_direction,
|
|
compute_layer_artifacts,
|
|
prepare_probe_corpus,
|
|
tensor_digest,
|
|
)
|
|
|
|
|
|
DIGEST = "a" * 64
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [
|
|
torch.float32, torch.float16, torch.bfloat16, torch.float64,
|
|
torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8, torch.bool,
|
|
torch.complex64, torch.complex128,
|
|
])
|
|
@pytest.mark.parametrize("shape", [
|
|
"scalar", "empty", "empty_matrix", "vector", "transpose", "strided",
|
|
"rank3_permuted", "rank4_permuted", "mixed_steps", "leading_singleton",
|
|
"leading_zero",
|
|
])
|
|
def test_streamed_tensor_digest_matches_canonical_bytes(monkeypatch, dtype, shape):
|
|
import obliteratus.distributed.probe_contracts as contracts
|
|
from obliteratus.distributed.numerical import _tensor_digest
|
|
|
|
# Small chunks exercise multiple boundaries and a partial final chunk.
|
|
monkeypatch.setattr(contracts, "TENSOR_DIGEST_CHUNK_BYTES", 128)
|
|
values = torch.arange(7 * 71).to(dtype).reshape(7, 71)
|
|
higher = torch.arange(2 * 3 * 7 * 71).to(dtype).reshape(2, 3, 7, 71)
|
|
tensor = {
|
|
"scalar": values[0, 1], "empty": values.reshape(-1)[:0],
|
|
"empty_matrix": values[:0, :3], "vector": values.reshape(-1),
|
|
"transpose": values.t(), "strided": values[:, ::2],
|
|
"rank3_permuted": higher[0].permute(2, 0, 1),
|
|
"rank4_permuted": higher.permute(2, 0, 3, 1),
|
|
"mixed_steps": higher.permute(2, 0, 3, 1)[::2, :, 1::3, ::2],
|
|
"leading_singleton": values.t().unsqueeze(0),
|
|
"leading_zero": higher.permute(2, 0, 3, 1)[:0],
|
|
}[shape]
|
|
value = tensor.detach().cpu().contiguous()
|
|
previous = hashlib.sha256()
|
|
previous.update(str(value.dtype).encode("ascii"))
|
|
previous.update(repr(tuple(value.shape)).encode("ascii"))
|
|
# Flattening a scalar permits the byte view (the old scalar view raised).
|
|
previous.update(value.reshape(-1).view(torch.uint8).numpy().tobytes())
|
|
assert tensor_digest(tensor) == _tensor_digest(tensor) == previous.hexdigest()
|
|
|
|
|
|
@pytest.mark.parametrize("transpose", [False, True])
|
|
def test_tensor_digest_intermediate_copies_are_chunk_bounded(monkeypatch, transpose):
|
|
from torch.utils._python_dispatch import TorchDispatchMode
|
|
import obliteratus.distributed.probe_contracts as contracts
|
|
|
|
limit = contracts.TENSOR_DIGEST_CHUNK_BYTES
|
|
value = torch.arange(3 * (limit // 2) + 17, dtype=torch.float32).to(torch.bfloat16)
|
|
if transpose:
|
|
value = value[:-(value.numel() % 7)].reshape(7, -1).t()
|
|
source_pointer = value.untyped_storage().data_ptr()
|
|
original_to = torch.Tensor.to
|
|
copy_sizes = []
|
|
|
|
def bounded_to(chunk, *args, **kwargs):
|
|
size = chunk.numel() * chunk.element_size()
|
|
assert size <= limit
|
|
result = original_to(chunk, *args, **kwargs)
|
|
assert result.device.type == "cpu" and result.is_contiguous()
|
|
assert result.untyped_storage().nbytes() <= limit
|
|
copy_sizes.append(size)
|
|
return result
|
|
|
|
class Allocations(TorchDispatchMode):
|
|
def __torch_dispatch__(self, func, types, args=(), kwargs=None):
|
|
result = func(*args, **(kwargs or {}))
|
|
values = result if isinstance(result, (tuple, list)) else (result,)
|
|
for tensor in values:
|
|
if isinstance(tensor, torch.Tensor):
|
|
storage = tensor.untyped_storage()
|
|
if storage.data_ptr() != source_pointer:
|
|
assert storage.nbytes() <= limit
|
|
return result
|
|
|
|
monkeypatch.setattr(torch.Tensor, "to", bounded_to)
|
|
with Allocations():
|
|
tensor_digest(value)
|
|
assert len(copy_sizes) > 1
|
|
assert sum(copy_sizes) == value.numel() * value.element_size()
|
|
|
|
|
|
class _Tokenizer:
|
|
vocab_size = 32
|
|
pad_token_id = 0
|
|
bos_token_id = 1
|
|
eos_token_id = 2
|
|
unk_token_id = 3
|
|
padding_side = "right"
|
|
truncation_side = "right"
|
|
chat_template: str | None = None
|
|
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple[str, bool]] = []
|
|
|
|
def encode(self, text: str, *, add_special_tokens: bool) -> list[int]:
|
|
self.calls.append((text, add_special_tokens))
|
|
values = [4 + (ord(value) % 20) for value in text]
|
|
return ([self.bos_token_id] if add_special_tokens else []) + values
|
|
|
|
|
|
class _ChatTokenizer(_Tokenizer):
|
|
chat_template = "{{ messages[0]['content'] }}"
|
|
|
|
def apply_chat_template(
|
|
self,
|
|
messages: list[dict[str, str]],
|
|
*,
|
|
tokenize: bool,
|
|
add_generation_prompt: bool,
|
|
) -> list[int]:
|
|
assert tokenize is True
|
|
self.calls.append((messages[0]["content"], add_generation_prompt))
|
|
return [1, 9, 2] if add_generation_prompt else [1, 8, 2]
|
|
|
|
|
|
def _samples(prefix: str, count: int = 2) -> tuple[ProbeSample, ...]:
|
|
return tuple(
|
|
ProbeSample(f"{prefix}-{index}", f"{prefix}{chr(97 + index)}") for index in range(count)
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
[
|
|
("schema_version", 2),
|
|
("direction_method", "svd"),
|
|
("n_directions", 0),
|
|
("selected_layer_count", 0),
|
|
("max_samples_per_class", 0),
|
|
("max_tokens", 0),
|
|
("batch_size", 0),
|
|
("max_activation_bytes", 0),
|
|
("activation_atol", float("nan")),
|
|
("activation_rtol", 0.2),
|
|
("stage_timeout_seconds", 0),
|
|
("chat_template_mode", "auto"),
|
|
("add_generation_prompt", 1),
|
|
],
|
|
)
|
|
def test_probe_config_rejects_open_or_unbounded_values(field, value):
|
|
with pytest.raises(ContractError):
|
|
replace(ProbeDistillConfig(), **{field: value})
|
|
|
|
|
|
def test_difference_of_means_profile_rejects_multiple_directions():
|
|
with pytest.raises(ContractError, match="exactly one"):
|
|
ProbeDistillConfig(n_directions=2)
|
|
|
|
|
|
def test_probe_config_digest_is_stable_and_binds_float_values():
|
|
first = ProbeDistillConfig()
|
|
assert first.digest == ProbeDistillConfig().digest
|
|
assert first.digest != replace(first, activation_atol=2e-5).digest
|
|
|
|
|
|
def test_rank_zero_tokenization_preserves_order_and_erases_raw_text():
|
|
tokenizer = _Tokenizer()
|
|
harmful = _samples("harm")
|
|
harmless = _samples("safe")
|
|
config = ProbeDistillConfig(max_tokens=32)
|
|
corpus = prepare_probe_corpus(
|
|
tokenizer,
|
|
harmful,
|
|
harmless,
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
|
|
assert tokenizer.calls == [
|
|
("harma", True),
|
|
("harmb", True),
|
|
("safea", True),
|
|
("safeb", True),
|
|
]
|
|
assert corpus.sample_ids == ("harm-0", "harm-1", "safe-0", "safe-1")
|
|
assert corpus.harmful_count == corpus.harmless_count == 2
|
|
plan = canonical_record(corpus.plan_record())
|
|
assert b"harma" not in plan
|
|
assert b"safea" not in plan
|
|
assert b"input_ids" not in plan
|
|
assert b"attention_mask" not in plan
|
|
assert corpus == prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
harmful,
|
|
harmless,
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
|
|
|
|
def test_chat_template_mode_is_explicit_and_bound_to_tokenization():
|
|
config = ProbeDistillConfig(
|
|
chat_template_mode="required",
|
|
add_generation_prompt=True,
|
|
max_tokens=8,
|
|
)
|
|
tokenizer = _ChatTokenizer()
|
|
corpus = prepare_probe_corpus(
|
|
tokenizer,
|
|
_samples("h", 1),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
assert corpus.input_ids == ((1, 9, 2), (1, 9, 2))
|
|
assert tokenizer.calls == [("ha", True), ("sa", True)]
|
|
with pytest.raises(ContractError, match="requires a chat template"):
|
|
prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
_samples("h", 1),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
|
|
|
|
def test_probe_corpus_rejects_unpaired_duplicate_oversized_and_out_of_vocab_inputs():
|
|
tokenizer = _Tokenizer()
|
|
with pytest.raises(ContractError, match="equal"):
|
|
prepare_probe_corpus(
|
|
tokenizer,
|
|
_samples("h", 2),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=ProbeDistillConfig(),
|
|
)
|
|
with pytest.raises(ContractError, match="globally unique"):
|
|
prepare_probe_corpus(
|
|
tokenizer,
|
|
(ProbeSample("same", "a"),),
|
|
(ProbeSample("same", "b"),),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=ProbeDistillConfig(),
|
|
)
|
|
with pytest.raises(ContractError, match="max_tokens"):
|
|
prepare_probe_corpus(
|
|
tokenizer,
|
|
_samples("harm", 1),
|
|
_samples("safe", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=ProbeDistillConfig(max_tokens=2),
|
|
)
|
|
tokenizer.vocab_size = 5
|
|
with pytest.raises(ContractError, match="outside the vocabulary"):
|
|
prepare_probe_corpus(
|
|
tokenizer,
|
|
_samples("h", 1),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=ProbeDistillConfig(max_tokens=8),
|
|
)
|
|
|
|
|
|
def test_direction_canonicalization_is_normalized_and_sign_stable():
|
|
first = canonicalize_direction(torch.tensor([-1.0, 3.0, -2.0]))
|
|
second = canonicalize_direction(torch.tensor([1.0, -3.0, 2.0]))
|
|
assert first.dtype is torch.float32
|
|
assert torch.equal(first, second)
|
|
assert first.norm() == pytest.approx(1.0)
|
|
assert first[1] > 0
|
|
for value in (torch.zeros(3), torch.tensor([1.0, float("nan")])):
|
|
with pytest.raises(ContractError):
|
|
canonicalize_direction(value)
|
|
|
|
|
|
def _activation_matrices() -> dict[str, torch.Tensor]:
|
|
return {
|
|
"model.layers.0": torch.tensor(
|
|
[[3.0, 0.0, 1.0], [2.0, 1.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]],
|
|
dtype=torch.float32,
|
|
),
|
|
"model.layers.1": torch.tensor(
|
|
[[0.0, 3.0, 0.0], [0.0, 2.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
|
|
dtype=torch.float32,
|
|
),
|
|
}
|
|
|
|
|
|
def _activation_digests(values: dict[str, torch.Tensor]) -> dict[str, str]:
|
|
return {name: tensor_digest(value) for name, value in values.items()}
|
|
|
|
|
|
def test_coordinator_difference_of_means_and_layer_selection_are_exact():
|
|
values = _activation_matrices()
|
|
artifacts, selected = compute_layer_artifacts(
|
|
values,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
config=ProbeDistillConfig(selected_layer_count=1),
|
|
activation_digests=_activation_digests(values),
|
|
)
|
|
assert tuple(item.site for item in artifacts) == ("model.layers.0", "model.layers.1")
|
|
expected = canonicalize_direction(torch.tensor([2.5, 0.0, 0.0]))
|
|
assert torch.equal(artifacts[0].directions[0], expected)
|
|
assert selected == ("model.layers.0",)
|
|
assert artifacts[0].harmful_mean.tolist() == [2.5, 0.5, 0.5]
|
|
assert artifacts[0].harmless_mean.tolist() == [0.0, 0.5, 0.5]
|
|
|
|
|
|
def test_coordinator_svd_is_sign_canonical_deterministic_and_rank_bounded():
|
|
values = _activation_matrices()
|
|
config = ProbeDistillConfig(
|
|
direction_method=DirectionMethod.SVD,
|
|
n_directions=2,
|
|
selected_layer_count=2,
|
|
)
|
|
first, selected = compute_layer_artifacts(
|
|
values,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
config=config,
|
|
activation_digests=_activation_digests(values),
|
|
)
|
|
second, _ = compute_layer_artifacts(
|
|
values,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
config=config,
|
|
activation_digests=_activation_digests(values),
|
|
)
|
|
assert selected == tuple(item.site for item in sorted(first, key=lambda item: -item.score))
|
|
for left, right in zip(first, second, strict=True):
|
|
assert torch.equal(left.directions, right.directions)
|
|
assert torch.allclose(left.directions @ left.directions.T, torch.eye(2), atol=1e-5)
|
|
for row in left.directions:
|
|
pivot = int(row.abs().argmax())
|
|
assert row[pivot] >= 0
|
|
|
|
|
|
@pytest.mark.parametrize("fault", ["nonfinite", "partial", "bad_digest", "too_many_directions"])
|
|
def test_direction_computation_rejects_incomplete_or_unsafe_inputs(fault):
|
|
values = _activation_matrices()
|
|
digests = _activation_digests(values)
|
|
config = ProbeDistillConfig()
|
|
if fault == "nonfinite":
|
|
values["model.layers.0"][0, 0] = float("nan")
|
|
elif fault == "partial":
|
|
values["unknown"] = values.pop("model.layers.0")
|
|
digests["unknown"] = digests.pop("model.layers.0")
|
|
elif fault == "bad_digest":
|
|
digests.pop("model.layers.0")
|
|
else:
|
|
config = ProbeDistillConfig(
|
|
direction_method=DirectionMethod.SVD,
|
|
n_directions=3,
|
|
)
|
|
with pytest.raises(ContractError):
|
|
compute_layer_artifacts(
|
|
values,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
config=config,
|
|
activation_digests=digests,
|
|
)
|
|
|
|
|
|
def test_summary_contains_only_redaction_safe_metadata_and_commits_every_layer():
|
|
values = _activation_matrices()
|
|
artifacts, selected = compute_layer_artifacts(
|
|
values,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
config=ProbeDistillConfig(),
|
|
activation_digests=_activation_digests(values),
|
|
)
|
|
summary = ProbeDistillSummary(
|
|
schema_version=1,
|
|
run_id="1" * 32,
|
|
loaded_identity_digest=DIGEST,
|
|
config_digest="b" * 64,
|
|
corpus_digest="c" * 64,
|
|
sample_ids_digest="d" * 64,
|
|
tokenizer_contract_digest="e" * 64,
|
|
tokenization_digest="f" * 64,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
input_width=8,
|
|
activation_atol_hex=float(1e-5).hex(),
|
|
activation_rtol_hex=float(1e-4).hex(),
|
|
selected_sites=selected,
|
|
layers=tuple(item.summary_record() for item in artifacts),
|
|
)
|
|
assert len(summary.digest) == 64
|
|
payload = canonical_record(summary)
|
|
for forbidden in (b"input_ids", b"attention_mask", b"harma", b"safea"):
|
|
assert forbidden not in payload
|
|
assert b"harmful_mean_digest" in payload
|
|
assert summary.digest != replace(summary, selected_sites=("model.layers.1",)).digest
|
|
|
|
|
|
def test_layer_artifact_rejects_nonfinite_unnormalized_or_unknown_outputs():
|
|
base = LayerProbeArtifact(
|
|
site="model.layers.0",
|
|
layer_index=0,
|
|
activation_digest=DIGEST,
|
|
max_abs_error=0.0,
|
|
max_rel_error=0.0,
|
|
score=1.0,
|
|
harmful_mean=torch.ones(3),
|
|
harmless_mean=torch.zeros(3),
|
|
directions=torch.tensor([[1.0, 0.0, 0.0]]),
|
|
)
|
|
assert base.summary_record().direction_digest == tensor_digest(base.directions)
|
|
with pytest.raises(ContractError):
|
|
replace(base, site="model.layers.partial")
|
|
with pytest.raises(ContractError):
|
|
replace(base, directions=torch.tensor([[2.0, 0.0, 0.0]]))
|
|
with pytest.raises(ContractError):
|
|
replace(base, score=float("inf"))
|
|
|
|
|
|
def test_corpus_identity_binds_source_tokenizer_and_exact_sample_content():
|
|
config = ProbeDistillConfig(max_tokens=16)
|
|
first = prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
_samples("h", 1),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
changed_source = prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
_samples("h", 1),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest="b" * 64,
|
|
config=config,
|
|
)
|
|
changed_text = prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
(ProbeSample("h-0", "different"),),
|
|
_samples("s", 1),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config,
|
|
)
|
|
assert first.tokenizer_contract_digest != changed_source.tokenizer_contract_digest
|
|
assert first.corpus_digest != changed_text.corpus_digest
|
|
assert first.tokenization_digest != changed_text.tokenization_digest
|
|
assert first.sample_ids_digest == changed_text.sample_ids_digest
|
|
assert first.corpus_digest == contract_digest(
|
|
{
|
|
"labels": ("harmful", "harmless"),
|
|
"sample_ids": first.sample_ids,
|
|
"text_digests": first.text_digests,
|
|
}
|
|
)
|
|
|
|
|
|
def test_live_runtime_requires_the_exact_session_capability():
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe_module.runtime_from_live_session(SimpleNamespace())
|
|
assert failure.value.code == "LMS_PROBE_PROFILE_MISMATCH"
|
|
|
|
|
|
def test_exact_tokenizer_is_rank_zero_only(monkeypatch):
|
|
monkeypatch.setattr(
|
|
probe_module,
|
|
"runtime_from_live_session",
|
|
lambda _session: SimpleNamespace(rank=1),
|
|
)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe_module.load_exact_probe_tokenizer(SimpleNamespace())
|
|
assert failure.value.code == "LMS_PROBE_PROFILE_MISMATCH"
|
|
|
|
|
|
def test_exact_tokenizer_load_is_local_pinned_fast_and_revalidated(monkeypatch, tmp_path):
|
|
import transformers
|
|
|
|
observed: dict[str, object] = {}
|
|
tokenizer_type = type(
|
|
"TokenizersBackend",
|
|
(),
|
|
{"__module__": "transformers.tokenization_utils_tokenizers", "is_fast": True},
|
|
)
|
|
tokenizer = tokenizer_type()
|
|
|
|
def load(_cls, _source: str, **kwargs):
|
|
observed.update(kwargs)
|
|
return tokenizer
|
|
|
|
monkeypatch.setattr(
|
|
probe_module,
|
|
"runtime_from_live_session",
|
|
lambda _session: SimpleNamespace(rank=0),
|
|
)
|
|
monkeypatch.setattr(transformers.PreTrainedTokenizerFast, "from_pretrained", classmethod(load))
|
|
monkeypatch.setattr(
|
|
probe_module,
|
|
"revalidate_source",
|
|
lambda config, identity: observed.update(config=config, identity=identity),
|
|
)
|
|
identity = SimpleNamespace(tokenizer_digest=DIGEST)
|
|
config = SimpleNamespace(
|
|
source_path=tmp_path,
|
|
require_model=lambda: SimpleNamespace(artifact_revision="b" * 64),
|
|
)
|
|
session = SimpleNamespace(config=config, preflight=SimpleNamespace(source_identity=identity))
|
|
|
|
assert probe_module.load_exact_probe_tokenizer(session) is tokenizer
|
|
assert observed["local_files_only"] is True
|
|
assert observed["trust_remote_code"] is False
|
|
assert observed["revision"] == "b" * 64
|
|
assert observed["token"] is None
|
|
assert observed["config"] is config
|
|
assert observed["identity"] is identity
|