mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
1121 lines
41 KiB
Python
1121 lines
41 KiB
Python
"""Single-process PROBE/DISTILL refusal paths behind fake rank collectives (#304).
|
|
|
|
The two-rank Gloo oracle in ``test_distributed_probe_gloo`` proves the real
|
|
collective behaviour. These tests replace the collectives with a deterministic
|
|
"mirror" peer so every validation and refusal branch of the stage adapter and
|
|
its pure contracts runs in-process on CPU, without spawning workers.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
import torch.distributed as dist
|
|
import torch.nn as nn
|
|
|
|
import obliteratus.distributed.probe as probe
|
|
import obliteratus.distributed.probe_contracts as contracts
|
|
from obliteratus.distributed.consensus import decode_frame, encode_frame
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError,
|
|
RuntimeContractError,
|
|
RuntimeStage,
|
|
StageMessage,
|
|
canonical_record,
|
|
contract_digest,
|
|
)
|
|
from obliteratus.distributed.probe import ProbeRuntime
|
|
from obliteratus.distributed.probe_contracts import (
|
|
CoordinatorProbeCorpus,
|
|
DirectionMethod,
|
|
LayerProbeArtifact,
|
|
LayerProbeSummary,
|
|
ProbeDistillConfig,
|
|
ProbeDistillSummary,
|
|
ProbeSample,
|
|
compute_layer_artifacts,
|
|
layer_summary_set_digest,
|
|
prepare_probe_corpus,
|
|
tensor_digest,
|
|
)
|
|
from tests.test_distributed_probe_gloo import _TinyLlamaShapedModel, _Tokenizer
|
|
from tests.test_distributed_probe_topology import _session
|
|
|
|
|
|
RUN_ID = "a" * 32
|
|
LOADED = "b" * 64
|
|
DIGEST = "c" * 64
|
|
SITES = ("model.layers.0", "model.layers.1")
|
|
|
|
|
|
def _config(**overrides) -> ProbeDistillConfig:
|
|
values = dict(
|
|
selected_layer_count=2,
|
|
max_samples_per_class=4,
|
|
max_tokens=32,
|
|
batch_size=2,
|
|
max_activation_bytes=1024 * 1024,
|
|
stage_timeout_seconds=30,
|
|
)
|
|
values.update(overrides)
|
|
return ProbeDistillConfig(**values)
|
|
|
|
|
|
def _samples(prefix: str) -> tuple[ProbeSample, ...]:
|
|
return (
|
|
ProbeSample(f"{prefix}-0", f"{prefix}alpha"),
|
|
ProbeSample(f"{prefix}-1", f"{prefix}beta"),
|
|
)
|
|
|
|
|
|
def _corpus(config: ProbeDistillConfig | None = None) -> CoordinatorProbeCorpus:
|
|
return prepare_probe_corpus(
|
|
_Tokenizer(),
|
|
_samples("harm"),
|
|
_samples("safe"),
|
|
source_tokenizer_digest=DIGEST,
|
|
config=config or _config(),
|
|
)
|
|
|
|
|
|
def _runtime(rank: int = 0, **overrides) -> ProbeRuntime:
|
|
values = dict(
|
|
run_id=RUN_ID,
|
|
loaded_identity_digest=LOADED,
|
|
rank=rank,
|
|
world_size=2,
|
|
model=_TinyLlamaShapedModel(rank=0, mode="success").eval(),
|
|
manifest=SimpleNamespace(activation_sites=SITES, digest=DIGEST),
|
|
control_group="control",
|
|
data_group="data",
|
|
tensor_device=torch.device("cpu"),
|
|
hidden_size=4,
|
|
available_host_bytes=1 << 30,
|
|
)
|
|
values.update(overrides)
|
|
return ProbeRuntime(**values)
|
|
|
|
|
|
class _MirrorDist:
|
|
"""A fake two-rank world whose peer is an exact mirror of this rank.
|
|
|
|
Rank zero records every broadcast payload; a rank-one instance replays a
|
|
recorded stream, so the worker path receives the coordinator's real bytes.
|
|
"""
|
|
|
|
ReduceOp = dist.ReduceOp
|
|
|
|
def __init__(self, rank: int, *, replay=None, data_backend: str = "gloo") -> None:
|
|
self.rank = rank
|
|
self.replay = [item.clone() for item in replay] if replay is not None else None
|
|
self.recorded: list[torch.Tensor] = []
|
|
self.data_backend = data_backend
|
|
self.initialized = True
|
|
self.broadcast_calls = 0
|
|
self.fail_broadcast_at: int | None = None
|
|
self.fail_all_reduce = False
|
|
self.fail_all_gather = False
|
|
self.peer_sequence_offset = 0
|
|
|
|
def is_available(self) -> bool:
|
|
return True
|
|
|
|
def is_initialized(self) -> bool:
|
|
return self.initialized
|
|
|
|
def get_backend(self, group) -> str:
|
|
return "gloo" if group == "control" else self.data_backend
|
|
|
|
def get_rank(self, _group) -> int:
|
|
return self.rank
|
|
|
|
def get_world_size(self, _group) -> int:
|
|
return 2
|
|
|
|
def get_process_group_ranks(self, _group) -> list[int]:
|
|
return [0, 1]
|
|
|
|
def broadcast(self, tensor, *, src, group) -> None:
|
|
assert src == 0
|
|
index = self.broadcast_calls
|
|
self.broadcast_calls += 1
|
|
if self.fail_broadcast_at == index:
|
|
raise RuntimeError("injected broadcast failure")
|
|
if self.replay is None:
|
|
self.recorded.append(tensor.detach().clone())
|
|
else:
|
|
tensor.copy_(self.replay.pop(0))
|
|
|
|
def all_reduce(self, _tensor, *, op, group) -> None:
|
|
if self.fail_all_reduce:
|
|
raise RuntimeError("injected all_reduce failure")
|
|
|
|
def all_gather(self, gathered, local, *, group) -> None:
|
|
if self.fail_all_gather:
|
|
raise RuntimeError("injected all_gather failure")
|
|
for index, item in enumerate(gathered):
|
|
item.copy_(local)
|
|
if index != self.rank:
|
|
item[2] += self.peer_sequence_offset
|
|
|
|
|
|
class _Consensus:
|
|
def __init__(self) -> None:
|
|
self.digest_calls = 0
|
|
self.fail_digest_at: int | None = None
|
|
self.always_accept = False
|
|
self.votes: list[tuple[int, bool]] = []
|
|
|
|
def vote(self, sequence, accepted, *, group):
|
|
self.votes.append((sequence, accepted))
|
|
return True if self.always_accept else accepted
|
|
|
|
def digest(self, value, *, group):
|
|
index = self.digest_calls
|
|
self.digest_calls += 1
|
|
if self.fail_digest_at == index:
|
|
raise ContractError("injected peer digest disagreement")
|
|
return value
|
|
|
|
@staticmethod
|
|
def gather(local, *, group):
|
|
return tuple(replace(local, rank=rank).to_bytes() for rank in (0, 1))
|
|
|
|
|
|
@pytest.fixture
|
|
def world(monkeypatch):
|
|
def install(rank: int = 0, **kwargs):
|
|
fake = _MirrorDist(rank, **kwargs)
|
|
consensus = _Consensus()
|
|
monkeypatch.setattr(probe, "dist", fake)
|
|
monkeypatch.setattr(probe, "unanimous_vote", consensus.vote)
|
|
monkeypatch.setattr(probe, "require_consensus_digest", consensus.digest)
|
|
monkeypatch.setattr(probe, "gloo_all_gather_records", consensus.gather)
|
|
return fake, consensus
|
|
|
|
return install
|
|
|
|
|
|
def _record_coordinator(world, config=None):
|
|
config = config or _config()
|
|
fake, _consensus = world(0)
|
|
result = probe.run_distributed_probe_distill(
|
|
_runtime(0), config, coordinator_corpus=_corpus(config)
|
|
)
|
|
return result, fake.recorded
|
|
|
|
|
|
def _frame_index(frames, key: str) -> int:
|
|
for index, frame in enumerate(frames):
|
|
if frame.dtype is torch.uint8 and frame.ndim == 1:
|
|
try:
|
|
value = json.loads(decode_frame(frame))
|
|
except (ContractError, ValueError):
|
|
continue
|
|
if isinstance(value, dict) and key in value:
|
|
return index
|
|
raise AssertionError(f"no broadcast frame carries {key}")
|
|
|
|
|
|
def _frame(value: dict) -> torch.Tensor:
|
|
return encode_frame(canonical_record(value))
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Full adapter through the mirror world.
|
|
|
|
|
|
def test_mirror_world_coordinator_and_worker_commit_the_same_distilled_summary(world):
|
|
coordinator, frames = _record_coordinator(world)
|
|
|
|
_fake, consensus = world(1, replay=frames)
|
|
worker = probe.run_distributed_probe_distill(
|
|
_runtime(1), _config(), coordinator_corpus=None
|
|
)
|
|
|
|
assert worker.summary.digest == coordinator.summary.digest
|
|
assert worker.summary.selected_sites == coordinator.summary.selected_sites
|
|
assert {item.site for item in worker.layers} == set(SITES)
|
|
for left, right in zip(worker.layers, coordinator.layers):
|
|
assert torch.equal(left.directions, right.directions)
|
|
assert [item.stage for item in worker.distilled_messages] == [RuntimeStage.DISTILLED] * 2
|
|
assert all(accepted for _sequence, accepted in consensus.votes)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mutate", "sequence"),
|
|
[
|
|
(lambda header: {**header, "extra": 1}, 6_300_113),
|
|
(lambda header: {**header, "selected_sites": "model.layers.0"}, 6_300_113),
|
|
(lambda header: {**header, "selected_sites": ["model.layers.9"]}, 6_300_113),
|
|
(lambda header: {**header, "selected_sites": header["selected_sites"][:1]}, 6_300_113),
|
|
(lambda header: {**header, "layer_count": 3}, 6_300_113),
|
|
(lambda header: {**header, "layer_summary_set_digest": "d" * 64}, 6_300_126),
|
|
],
|
|
)
|
|
def test_worker_refuses_a_tampered_distill_decision_header(world, mutate, sequence):
|
|
_result, frames = _record_coordinator(world)
|
|
index = _frame_index(frames, "layer_summary_set_digest")
|
|
frames[index] = _frame(mutate(json.loads(decode_frame(frames[index]))))
|
|
|
|
_fake, consensus = world(1, replay=frames)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(_runtime(1), _config(), coordinator_corpus=None)
|
|
|
|
assert failure.value.code in {"LMS_DISTILL_INVALID", "LMS_DISTILL_DIVERGED"}
|
|
assert (sequence, False) in consensus.votes
|
|
|
|
|
|
def test_worker_refuses_a_layer_summary_with_changed_fields_or_order(world):
|
|
_result, frames = _record_coordinator(world)
|
|
first = _frame_index(frames, "harmful_mean_digest")
|
|
summary = json.loads(decode_frame(frames[first]))
|
|
|
|
missing = list(frames)
|
|
missing[first] = _frame({k: v for k, v in summary.items() if k != "score_hex"})
|
|
_fake, consensus = world(1, replay=missing)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(_runtime(1), _config(), coordinator_corpus=None)
|
|
assert failure.value.code == "LMS_DISTILL_INVALID"
|
|
assert (6_300_120, False) in consensus.votes
|
|
|
|
reordered = list(frames)
|
|
reordered[first] = _frame({**summary, "site": "model.layers.1", "layer_index": 1})
|
|
_fake, consensus = world(1, replay=reordered)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(_runtime(1), _config(), coordinator_corpus=None)
|
|
assert failure.value.code == "LMS_DISTILL_INVALID"
|
|
assert (6_300_120, False) in consensus.votes
|
|
|
|
|
|
def test_training_mode_model_is_refused_before_any_activation_is_used(world):
|
|
_fake, consensus = world(0)
|
|
runtime = _runtime(0)
|
|
runtime.model.train()
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(runtime, _config(), coordinator_corpus=_corpus())
|
|
|
|
assert failure.value.code == "LMS_PROBE_HOOK_UNSUPPORTED"
|
|
assert (6_300_115, False) in consensus.votes
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("fail_digest_at", "code"),
|
|
[
|
|
(0, "LMS_PROBE_PROFILE_MISMATCH"), # config digest consensus
|
|
(4, "LMS_PROBE_ACTIVATION_DIVERGED"), # first activation stream digest
|
|
(6, "LMS_DISTILL_DIVERGED"), # final summary digest
|
|
],
|
|
)
|
|
def test_peer_digest_disagreement_maps_to_the_stage_refusal(world, fail_digest_at, code):
|
|
_fake, consensus = world(0)
|
|
consensus.fail_digest_at = fail_digest_at
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(_runtime(0), _config(), coordinator_corpus=_corpus())
|
|
|
|
assert failure.value.code == code
|
|
|
|
|
|
def test_activation_reference_broadcast_failure_is_a_collective_refusal(world):
|
|
fake, _consensus = world(0)
|
|
# Broadcast order: plan record, input ids, attention mask, then references.
|
|
fake.fail_broadcast_at = 3
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(_runtime(0), _config(), coordinator_corpus=_corpus())
|
|
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
assert "activation reference" in str(failure.value)
|
|
|
|
|
|
def test_runner_rejects_a_runtime_that_is_not_the_probe_capability(world):
|
|
world(0)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_distributed_probe_distill(object(), _config(), coordinator_corpus=None)
|
|
assert failure.value.code == "LMS_PROBE_PROFILE_MISMATCH"
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# run_live_probe_distill rank-owned input checks.
|
|
|
|
|
|
@pytest.mark.parametrize("bad_input", ["missing", "tokenizer_failure"])
|
|
def test_live_rank_zero_without_a_valid_corpus_never_reaches_probe(
|
|
monkeypatch, world, bad_input
|
|
):
|
|
_fake, consensus = world(0)
|
|
# Model a lying peer vote: the defensive rank-zero guard must still refuse.
|
|
consensus.always_accept = True
|
|
monkeypatch.setattr(probe, "runtime_from_live_session", lambda _session: _runtime(0))
|
|
monkeypatch.setattr(
|
|
probe,
|
|
"run_distributed_probe_distill",
|
|
lambda *args, **kwargs: pytest.fail("PROBE must not start without a corpus"),
|
|
)
|
|
session = SimpleNamespace(
|
|
control_group="control",
|
|
preflight=SimpleNamespace(source_identity=SimpleNamespace(tokenizer_digest=DIGEST))
|
|
)
|
|
kwargs = {}
|
|
if bad_input == "tokenizer_failure":
|
|
kwargs = dict(
|
|
harmful=_samples("harm"),
|
|
harmless=_samples("safe"),
|
|
tokenizer=SimpleNamespace(vocab_size=0),
|
|
)
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.run_live_probe_distill(session, _config(), **kwargs)
|
|
|
|
assert failure.value.code == "LMS_PROBE_INPUT_INVALID"
|
|
assert consensus.votes == [(6_300_200, True), (60_900, False)]
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Runtime capability and session narrowing.
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"overrides",
|
|
[
|
|
{"run_id": "A" * 32},
|
|
{"loaded_identity_digest": "short"},
|
|
{"rank": 2},
|
|
{"world_size": 3},
|
|
{"tensor_device": torch.device("meta")},
|
|
{"tensor_device": "cpu"},
|
|
{"hidden_size": True},
|
|
{"hidden_size": 0},
|
|
{"available_host_bytes": 0},
|
|
],
|
|
)
|
|
def test_probe_runtime_rejects_out_of_profile_capabilities(overrides):
|
|
with pytest.raises(ContractError):
|
|
_runtime(**{"rank": 0, **overrides})
|
|
|
|
|
|
def test_incomplete_live_session_is_a_profile_mismatch():
|
|
session = replace(_session(0), handle=SimpleNamespace(model=None))
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.runtime_from_live_session(session)
|
|
assert failure.value.code == "LMS_PROBE_PROFILE_MISMATCH"
|
|
assert "incomplete" in str(failure.value)
|
|
|
|
|
|
def test_probe_peak_budget_includes_live_tensor_overlaps_and_svd():
|
|
runtime = _runtime()
|
|
config = _config()
|
|
batches = [[torch.ones(2, 4), torch.ones(2, 4)] for _ in SITES]
|
|
matrices = [torch.cat(items) for items in batches]
|
|
artifacts = [torch.ones(4) for _ in range(len(SITES) * 3)]
|
|
received = [value.clone() for value in artifacts]
|
|
captures = [torch.ones(2, 4) for _ in SITES]
|
|
token_sources = [torch.ones(2, 10, dtype=torch.int64) for _ in range(2)]
|
|
token_broadcasts = [value.clone() for value in token_sources]
|
|
numeric_scratch = [value.double() for value in matrices]
|
|
live = (
|
|
[value for items in batches for value in items] + matrices
|
|
+ artifacts + received + captures + token_sources + token_broadcasts + numeric_scratch
|
|
)
|
|
observed_payload = sum(value.numel() * value.element_size() for value in live)
|
|
allowance = probe._probe_peak_host_bytes(
|
|
runtime, config, sample_count=4, site_count=2, input_width=10,
|
|
)
|
|
assert allowance > observed_payload
|
|
logical_payload = sum(value.numel() * value.element_size() for value in matrices + artifacts)
|
|
assert logical_payload < observed_payload
|
|
assert probe._probe_peak_host_bytes(
|
|
runtime, replace(config, batch_size=4), sample_count=4, site_count=2, input_width=20,
|
|
) > allowance
|
|
svd = replace(config, direction_method=DirectionMethod.SVD)
|
|
outputs = torch.linalg.svd(numeric_scratch[0][:2], full_matrices=False)
|
|
svd_payload = sum(value.numel() * value.element_size() for value in outputs)
|
|
svd_allowance = probe._probe_peak_host_bytes(
|
|
runtime, svd, sample_count=4, site_count=2, input_width=10,
|
|
)
|
|
assert svd_allowance > allowance
|
|
assert svd_allowance > observed_payload + svd_payload
|
|
|
|
|
|
def test_probe_admission_refreshes_ram_after_a_previous_success(world, monkeypatch):
|
|
_fake, consensus = world(0)
|
|
observations = iter(((1 << 30, 1 << 30), (1 << 30, 0)))
|
|
monkeypatch.setattr(probe, "_host_memory", lambda: next(observations))
|
|
kwargs = dict(sample_count=4, site_count=2, input_width=10)
|
|
probe._resource_admission(_runtime(), _config(), **kwargs)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._resource_admission(_runtime(), _config(), **kwargs)
|
|
assert failure.value.code == "LMS_PROBE_RESOURCE_DENIED"
|
|
assert consensus.votes[-2:] == [(61_001, True), (61_001, False)]
|
|
|
|
|
|
@pytest.mark.parametrize("observation", [(100, 101), (100, -1), (0, 0), (100, True)])
|
|
def test_probe_refuses_invalid_fresh_ram_observations(world, monkeypatch, observation):
|
|
world(0)
|
|
monkeypatch.setattr(probe, "_host_memory", lambda: observation)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._resource_admission(_runtime(), _config(), sample_count=4, site_count=2)
|
|
assert failure.value.code == "LMS_PROBE_RESOURCE_DENIED"
|
|
|
|
|
|
def test_probe_refuses_unavailable_fresh_ram_observation(world, monkeypatch):
|
|
world(0)
|
|
|
|
def unavailable():
|
|
raise OSError("RAM inventory unavailable")
|
|
|
|
monkeypatch.setattr(probe, "_host_memory", unavailable)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._resource_admission(_runtime(), _config(), sample_count=4, site_count=2)
|
|
assert failure.value.code == "LMS_PROBE_RESOURCE_DENIED"
|
|
|
|
|
|
def test_probe_peak_bound_rejects_a_previously_admissible_logical_payload(world, monkeypatch):
|
|
world(0)
|
|
monkeypatch.setattr(probe, "_host_memory", lambda: (1 << 30, 1 << 30))
|
|
config = _config(max_activation_bytes=1024)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._resource_admission(_runtime(), config, sample_count=4, site_count=2)
|
|
assert failure.value.code == "LMS_PROBE_RESOURCE_DENIED"
|
|
|
|
|
|
def _tokenizer_session(tmp_path):
|
|
return SimpleNamespace(
|
|
config=SimpleNamespace(
|
|
source_path=tmp_path,
|
|
require_model=lambda: SimpleNamespace(artifact_revision="b" * 64),
|
|
),
|
|
preflight=SimpleNamespace(source_identity=SimpleNamespace(tokenizer_digest=DIGEST)),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("fault", ["load_error", "source_changed", "slow_tokenizer"])
|
|
def test_exact_tokenizer_refuses_load_errors_source_drift_and_foreign_classes(
|
|
monkeypatch, tmp_path, fault
|
|
):
|
|
import transformers
|
|
|
|
exact = type(
|
|
"TokenizersBackend",
|
|
(),
|
|
{"__module__": "transformers.tokenization_utils_tokenizers", "is_fast": True},
|
|
)
|
|
slow = type("PreTrainedTokenizer", (), {"__module__": "transformers.other", "is_fast": False})
|
|
|
|
def load(_cls, _source, **_kwargs):
|
|
if fault == "load_error":
|
|
raise OSError("missing tokenizer.json")
|
|
return slow() if fault == "slow_tokenizer" else exact()
|
|
|
|
def revalidate(_config, _identity):
|
|
if fault == "source_changed":
|
|
raise RuntimeContractError("LMS_SOURCE_CHANGED", "source changed")
|
|
|
|
monkeypatch.setattr(probe, "runtime_from_live_session", lambda _s: SimpleNamespace(rank=0))
|
|
monkeypatch.setattr(transformers.PreTrainedTokenizerFast, "from_pretrained", classmethod(load))
|
|
monkeypatch.setattr(probe, "revalidate_source", revalidate)
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe.load_exact_probe_tokenizer(_tokenizer_session(tmp_path))
|
|
|
|
assert failure.value.code == {
|
|
"load_error": "LMS_PROBE_INPUT_INVALID",
|
|
"source_changed": "LMS_SOURCE_CHANGED",
|
|
"slow_tokenizer": "LMS_PROBE_PROFILE_MISMATCH",
|
|
}[fault]
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Collective helpers.
|
|
|
|
|
|
def test_group_identity_refuses_uninitialized_groups_and_backend_device_mismatch(world):
|
|
fake, _consensus = world(0)
|
|
fake.initialized = False
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._group_identity(_runtime(0))
|
|
assert "process-group identity" in str(failure.value)
|
|
|
|
fake.initialized = True
|
|
fake.data_backend = "nccl"
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._group_identity(_runtime(0))
|
|
assert failure.value.code == "LMS_PROBE_PROFILE_MISMATCH"
|
|
assert "data group disagree" in str(failure.value)
|
|
|
|
fake.data_backend = "gloo"
|
|
assert probe._group_identity(_runtime(0)) is None
|
|
|
|
|
|
def test_broadcast_record_enforces_roles_and_canonical_unique_json(world, monkeypatch):
|
|
world(0)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._broadcast_record(_runtime(0), None)
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
|
|
world(1, replay=[_frame({"a": 1})])
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._broadcast_record(_runtime(1), {"a": 1})
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
|
|
for payload in (b'{"a":1,"a":2}', b'{"b":1,"a":2}', b"[1]"):
|
|
world(1, replay=[encode_frame(payload)])
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._broadcast_record(_runtime(1), None)
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
|
|
world(1, replay=[_frame({"a": 1, "b": [2]})])
|
|
assert probe._broadcast_record(_runtime(1), None) == {"a": 1, "b": [2]}
|
|
|
|
def timed_out(*_args, **_kwargs):
|
|
raise RuntimeContractError("LMS_PROBE_TIMEOUT", "deadline")
|
|
|
|
fake, _consensus = world(0)
|
|
monkeypatch.setattr(fake, "broadcast", timed_out)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._broadcast_record(_runtime(0), {"a": 1})
|
|
assert failure.value.code == "LMS_PROBE_TIMEOUT"
|
|
|
|
|
|
def test_collective_vote_transport_failure_is_a_collective_refusal(monkeypatch):
|
|
def broken(*_args, **_kwargs):
|
|
raise OSError("socket closed")
|
|
|
|
monkeypatch.setattr(probe, "unanimous_vote", broken)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._collective_accept(_runtime(0), 1, True, code="LMS_PROBE_FAILED", message="x")
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
|
|
|
|
def test_broadcast_tensor_enforces_shapes_and_roles(world):
|
|
fake, _consensus = world(0)
|
|
with pytest.raises(RuntimeContractError):
|
|
probe._broadcast_tensor(_runtime(0), torch.zeros(3), shape=(4,), dtype=torch.float32)
|
|
with pytest.raises(RuntimeContractError):
|
|
probe._broadcast_tensor(_runtime(0), None, shape=(4,), dtype=torch.float32)
|
|
sent = probe._broadcast_tensor(
|
|
_runtime(0), torch.arange(4), shape=(4,), dtype=torch.float32
|
|
)
|
|
assert sent.dtype is torch.float32 and fake.recorded[-1].tolist() == [0.0, 1.0, 2.0, 3.0]
|
|
|
|
world(1, replay=[torch.ones(4)])
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._broadcast_tensor(_runtime(1), torch.ones(4), shape=(4,), dtype=torch.float32)
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
world(1, replay=[torch.ones(4)])
|
|
received = probe._broadcast_tensor(_runtime(1), None, shape=(4,), dtype=torch.float32)
|
|
assert received.tolist() == [1.0] * 4
|
|
|
|
|
|
def test_max_error_reports_errors_and_refuses_failed_aggregation(world):
|
|
fake, _consensus = world(0)
|
|
local = torch.tensor([1.0, 2.0])
|
|
reference = torch.tensor([1.0, 1.0])
|
|
assert probe._max_error(local, reference, _runtime(0)) == (1.0, 1.0)
|
|
fake.fail_all_reduce = True
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._max_error(local, reference, _runtime(0))
|
|
assert failure.value.code == "LMS_PROBE_COLLECTIVE_FAILED"
|
|
|
|
|
|
def test_emit_stage_refuses_invalid_transitions_missing_ranks_and_disagreement(
|
|
world, monkeypatch
|
|
):
|
|
world(0)
|
|
runtime = _runtime(0)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._emit_stage(
|
|
runtime,
|
|
current=RuntimeStage.LOADED,
|
|
requested=RuntimeStage.DISTILLED,
|
|
sequence=3,
|
|
identity_digest=DIGEST,
|
|
)
|
|
assert "lifecycle consensus" in str(failure.value)
|
|
|
|
monkeypatch.setattr(probe, "gloo_all_gather_records", lambda local, *, group: (local.to_bytes(),))
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._emit_stage(
|
|
runtime,
|
|
current=RuntimeStage.LOADED,
|
|
requested=RuntimeStage.PROBED,
|
|
sequence=3,
|
|
identity_digest=DIGEST,
|
|
)
|
|
assert "omitted a fixed rank" in str(failure.value)
|
|
|
|
def disagreeing(local: StageMessage, *, group):
|
|
return (local.to_bytes(), replace(local, rank=1, sequence=99).to_bytes())
|
|
|
|
monkeypatch.setattr(probe, "gloo_all_gather_records", disagreeing)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._emit_stage(
|
|
runtime,
|
|
current=RuntimeStage.LOADED,
|
|
requested=RuntimeStage.PROBED,
|
|
sequence=3,
|
|
identity_digest=DIGEST,
|
|
)
|
|
assert "records disagree" in str(failure.value)
|
|
|
|
|
|
def test_check_continue_maps_cancellation_transport_and_sequence_faults(world):
|
|
fake, _consensus = world(0)
|
|
runtime = _runtime(0)
|
|
config = _config()
|
|
now = probe.time.monotonic()
|
|
|
|
def broken_callback() -> bool:
|
|
raise RuntimeError("operator callback failed")
|
|
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._check_continue(
|
|
runtime, config, started=now, sequence=7, cancellation_requested=broken_callback
|
|
)
|
|
assert failure.value.code == "LMS_PROBE_CANCELLED"
|
|
|
|
fake.peer_sequence_offset = 1
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._check_continue(
|
|
runtime, config, started=now, sequence=7, cancellation_requested=lambda: False
|
|
)
|
|
assert "sequence disagrees" in str(failure.value)
|
|
|
|
fake.peer_sequence_offset = 0
|
|
fake.fail_all_gather = True
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._check_continue(
|
|
runtime, config, started=now, sequence=7, cancellation_requested=lambda: False
|
|
)
|
|
assert "cancellation consensus failed" in str(failure.value)
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Plan, hook sites, hooks, and error translation.
|
|
|
|
|
|
def _plan_record(config: ProbeDistillConfig) -> dict[str, object]:
|
|
return {
|
|
**_corpus(config).plan_record(),
|
|
"config_digest": config.digest,
|
|
"loaded_identity_digest": LOADED,
|
|
}
|
|
|
|
|
|
def test_validate_plan_accepts_the_coordinator_plan_and_rejects_open_records():
|
|
config = _config()
|
|
record = _plan_record(config)
|
|
plan = probe._validate_plan(dict(record), _runtime(0), config)
|
|
assert (plan.harmful_count, plan.input_width) == (2, record["input_width"])
|
|
|
|
faults = [
|
|
{**record, "extra": 1},
|
|
{**record, "corpus_digest": "Z" * 64},
|
|
{**record, "harmful_count": True},
|
|
{**record, "harmful_count": 3},
|
|
{**record, "loaded_identity_digest": "e" * 64},
|
|
{**record, "pad_token_id": record["vocab_size"]},
|
|
]
|
|
for fault in faults:
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._validate_plan(fault, _runtime(0), config)
|
|
assert failure.value.code == "LMS_PROBE_INPUT_INVALID"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"manifest",
|
|
[
|
|
None,
|
|
SimpleNamespace(activation_sites=("model.layers.1",), digest=DIGEST),
|
|
SimpleNamespace(activation_sites=(), digest=DIGEST),
|
|
],
|
|
)
|
|
def test_hook_sites_require_a_complete_contiguous_manifest(world, manifest):
|
|
world(0)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
probe._hook_sites(_runtime(0, manifest=manifest))
|
|
assert failure.value.code == "LMS_PROBE_HOOK_UNSUPPORTED"
|
|
|
|
|
|
class _Layer(nn.Module):
|
|
def __init__(self, width: int = 4) -> None:
|
|
super().__init__()
|
|
self.width = width
|
|
|
|
def forward(self, hidden: torch.Tensor) -> torch.Tensor:
|
|
return hidden[..., : self.width]
|
|
|
|
|
|
def test_hooks_refuse_missing_modules_repeat_firing_and_changed_layout():
|
|
runtime = _runtime(0, model=nn.Module())
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
with probe._install_hooks(runtime, ("model.layers.0",), [None], {}):
|
|
pass
|
|
assert "unavailable" in str(failure.value)
|
|
|
|
holder = nn.Module()
|
|
holder.model = nn.Module()
|
|
holder.model.layers = nn.ModuleList([_Layer(4), _Layer(3)])
|
|
runtime = _runtime(0, model=holder)
|
|
mask = torch.tensor([[1, 1, 0]])
|
|
captured: dict[str, torch.Tensor] = {}
|
|
hidden = torch.arange(12, dtype=torch.float32).reshape(1, 3, 4)
|
|
with probe._install_hooks(runtime, ("model.layers.0", "model.layers.1"), [mask], captured):
|
|
holder.model.layers[0](hidden)
|
|
# The last unpadded position (index 1) is captured as float32.
|
|
assert captured["model.layers.0"].tolist() == [[4.0, 5.0, 6.0, 7.0]]
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
holder.model.layers[0](hidden)
|
|
assert "more than once" in str(failure.value)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
holder.model.layers[1](hidden)
|
|
assert "changed layout" in str(failure.value)
|
|
# Hooks are removed on exit: no further captures occur.
|
|
captured.clear()
|
|
holder.model.layers[0](hidden)
|
|
assert captured == {}
|
|
|
|
|
|
def test_hook_removal_failures_are_contained():
|
|
removed = []
|
|
|
|
def remove() -> None:
|
|
removed.append(True)
|
|
raise RuntimeError("already removed")
|
|
|
|
module = SimpleNamespace(register_forward_hook=lambda _fn: SimpleNamespace(remove=remove))
|
|
runtime = _runtime(0, model=SimpleNamespace(get_submodule=lambda _site: module))
|
|
with probe._install_hooks(runtime, SITES, [None], {}):
|
|
pass
|
|
assert removed == [True, True]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("error", "code"),
|
|
[
|
|
(RuntimeContractError("LMS_PROBE_CANCELLED", "x"), "LMS_PROBE_CANCELLED"),
|
|
(RuntimeContractError("LMS_SOURCE_CHANGED", "x"), "LMS_DISTILL_INVALID"),
|
|
(TimeoutError(), "LMS_PROBE_TIMEOUT"),
|
|
(ContractError("x"), "LMS_DISTILL_INVALID"),
|
|
(KeyError("x"), "LMS_PROBE_FAILED"),
|
|
],
|
|
)
|
|
def test_probe_errors_are_translated_to_the_closed_code_set(error, code):
|
|
translated = probe._translate_probe_error(error)
|
|
assert translated.code == code
|
|
assert translated.code in probe.PROBE_ERROR_CODES
|
|
if code == "LMS_PROBE_CANCELLED":
|
|
assert translated is error
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Pure contracts (probe_contracts.py).
|
|
|
|
|
|
def test_tensor_digest_chunks_cover_small_noncontiguous_tensors_and_reject_sparse():
|
|
value = torch.arange(6, dtype=torch.float32).reshape(2, 3).t()
|
|
chunks = list(contracts._tensor_digest_chunks(value, 1024))
|
|
assert len(chunks) == 1 and chunks[0] is value
|
|
assert contracts._tensor_all_finite(value)
|
|
assert not contracts._tensor_all_finite(torch.tensor([1.0, float("inf")]))
|
|
with pytest.raises(ContractError):
|
|
tensor_digest(torch.eye(2).to_sparse())
|
|
with pytest.raises(ContractError):
|
|
tensor_digest([1.0])
|
|
|
|
|
|
def test_tensor_digest_refuses_unrepresentable_tensors():
|
|
with pytest.raises(ContractError) as failure:
|
|
tensor_digest(torch.zeros(2, device="meta"))
|
|
assert "canonically" in str(failure.value)
|
|
|
|
|
|
def test_bounded_finite_rejects_non_numbers():
|
|
with pytest.raises(ContractError):
|
|
contracts._bounded_finite("1", "x", minimum=0.0, maximum=1.0)
|
|
assert contracts._bounded_finite(1, "x", minimum=0.0, maximum=1.0) == 1.0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("sample_id", "text"),
|
|
[("bad id", "x"), ("ok", ""), ("ok", 3), ("ok", "\ud800"), ("ok", "x" * (64 * 1024 + 1))],
|
|
)
|
|
def test_probe_sample_rejects_invalid_ids_and_text(sample_id, text):
|
|
with pytest.raises(ContractError):
|
|
ProbeSample(sample_id, text)
|
|
|
|
|
|
def test_generation_prompt_requires_chat_template_mode():
|
|
with pytest.raises(ContractError):
|
|
ProbeDistillConfig(add_generation_prompt=True)
|
|
assert ProbeDistillConfig(chat_template_mode="required", add_generation_prompt=True)
|
|
|
|
|
|
def test_coordinator_corpus_rejects_every_inconsistent_field():
|
|
corpus = _corpus()
|
|
width = corpus.input_width
|
|
pad = corpus.pad_token_id
|
|
first_ids = corpus.input_ids[0]
|
|
faults = [
|
|
{"schema_version": 2},
|
|
{"harmful_count": 3},
|
|
{"sample_ids": corpus.sample_ids[:-1]},
|
|
{"sample_ids": (corpus.sample_ids[0],) * 4},
|
|
{"input_ids": (first_ids + (pad,),) + corpus.input_ids[1:]},
|
|
{
|
|
"input_ids": tuple(row + (pad,) * (9000 - width) for row in corpus.input_ids),
|
|
"attention_mask": tuple(
|
|
row + (0,) * (9000 - width) for row in corpus.attention_mask
|
|
),
|
|
},
|
|
{"attention_mask": (corpus.attention_mask[0][:-1],) + corpus.attention_mask[1:]},
|
|
{"input_ids": ((corpus.vocab_size,) + first_ids[1:],) + corpus.input_ids[1:]},
|
|
{"attention_mask": ((0,) * width,) + corpus.attention_mask[1:]},
|
|
{"attention_mask": ((1, 0) + (1,) * (width - 2),) + corpus.attention_mask[1:]},
|
|
{"sample_ids_digest": DIGEST},
|
|
{"corpus_digest": DIGEST},
|
|
{"tokenization_digest": DIGEST},
|
|
]
|
|
for fault in faults:
|
|
with pytest.raises(ContractError):
|
|
replace(corpus, **fault)
|
|
|
|
|
|
def test_coordinator_corpus_requires_right_padding_with_pad_token():
|
|
corpus = _corpus()
|
|
# Find a padded row and replace its padding with a non-pad token.
|
|
index = next(i for i, mask in enumerate(corpus.attention_mask) if 0 in mask)
|
|
row = list(corpus.input_ids[index])
|
|
row[-1] = corpus.pad_token_id + 5
|
|
ids = list(corpus.input_ids)
|
|
ids[index] = tuple(row)
|
|
with pytest.raises(ContractError) as failure:
|
|
replace(corpus, input_ids=tuple(ids))
|
|
assert "right padding" in str(failure.value)
|
|
|
|
|
|
def _artifact(**overrides) -> LayerProbeArtifact:
|
|
values = dict(
|
|
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]]),
|
|
)
|
|
values.update(overrides)
|
|
return LayerProbeArtifact(**values)
|
|
|
|
|
|
def test_layer_artifact_rejects_index_and_tensor_contract_violations():
|
|
assert _artifact().summary_record().hidden_size == 3
|
|
for fault in (
|
|
{"layer_index": 1},
|
|
{"harmful_mean": torch.ones(3, dtype=torch.float64)},
|
|
{"directions": torch.ones(1, 4)},
|
|
):
|
|
with pytest.raises(ContractError):
|
|
_artifact(**fault)
|
|
|
|
|
|
def test_layer_summary_rejects_non_canonical_hex_values():
|
|
summary = _artifact().summary_record()
|
|
for fault in (
|
|
{"site": "model.layers.1"},
|
|
{"score_hex": 1.0},
|
|
{"score_hex": "not-hex"},
|
|
{"score_hex": "-0x1.0000000000000p+0"},
|
|
{"score_hex": "0x1p+0"},
|
|
):
|
|
with pytest.raises(ContractError):
|
|
replace(summary, **fault)
|
|
|
|
|
|
def _summary(**overrides) -> ProbeDistillSummary:
|
|
layer = _artifact().summary_record()
|
|
values = dict(
|
|
schema_version=1,
|
|
run_id=RUN_ID,
|
|
loaded_identity_digest=DIGEST,
|
|
config_digest=DIGEST,
|
|
corpus_digest=DIGEST,
|
|
sample_ids_digest=DIGEST,
|
|
tokenizer_contract_digest=DIGEST,
|
|
tokenization_digest=DIGEST,
|
|
harmful_count=2,
|
|
harmless_count=2,
|
|
input_width=4,
|
|
activation_atol_hex=float(1e-5).hex(),
|
|
activation_rtol_hex=float(1e-4).hex(),
|
|
selected_sites=("model.layers.0",),
|
|
layers=(layer,),
|
|
)
|
|
values.update(overrides)
|
|
return ProbeDistillSummary(**values)
|
|
|
|
|
|
def test_probe_summary_rejects_every_inconsistent_field():
|
|
summary = _summary()
|
|
assert summary.digest == contract_digest(
|
|
{
|
|
"schema_version": 1,
|
|
"run_id": RUN_ID,
|
|
"loaded_identity_digest": DIGEST,
|
|
"config_digest": DIGEST,
|
|
"corpus_digest": DIGEST,
|
|
"sample_ids_digest": DIGEST,
|
|
"tokenizer_contract_digest": DIGEST,
|
|
"tokenization_digest": DIGEST,
|
|
"harmful_count": 2,
|
|
"harmless_count": 2,
|
|
"input_width": 4,
|
|
"activation_atol_hex": float(1e-5).hex(),
|
|
"activation_rtol_hex": float(1e-4).hex(),
|
|
"selected_sites": ("model.layers.0",),
|
|
"layer_count": 1,
|
|
"layer_summary_set_digest": layer_summary_set_digest(summary.layers),
|
|
}
|
|
)
|
|
layer = summary.layers[0]
|
|
for fault in (
|
|
{"schema_version": 2},
|
|
{"run_id": "x"},
|
|
{"harmful_count": 1},
|
|
{"activation_atol_hex": 1e-5},
|
|
{"activation_atol_hex": "not-a-hex-float"},
|
|
{"activation_atol_hex": float(0.5).hex()},
|
|
{"layers": ()},
|
|
{"layers": (layer, layer)},
|
|
{"selected_sites": ("model.layers.3",)},
|
|
):
|
|
with pytest.raises(ContractError):
|
|
_summary(**fault)
|
|
with pytest.raises(ContractError):
|
|
layer_summary_set_digest(())
|
|
|
|
|
|
def test_tokenizer_identity_and_encoding_refuse_unsafe_tokenizers():
|
|
config = _config()
|
|
chat = _config(chat_template_mode="required")
|
|
|
|
class BadTemplate(_Tokenizer):
|
|
chat_template = b"bytes"
|
|
|
|
with pytest.raises(ContractError):
|
|
contracts._tokenizer_identity(BadTemplate(), source_tokenizer_digest=DIGEST, config=config)
|
|
|
|
class Template(_Tokenizer):
|
|
chat_template = "{{ x }}"
|
|
|
|
with pytest.raises(ContractError) as failure:
|
|
contracts._encode_sample(Template(), ProbeSample("s", "x"), chat)
|
|
assert "chat-template API" in str(failure.value)
|
|
|
|
class Raises(_Tokenizer):
|
|
def encode(self, text, *, add_special_tokens):
|
|
raise ValueError("tokenizer exploded")
|
|
|
|
with pytest.raises(ContractError) as failure:
|
|
contracts._encode_sample(Raises(), ProbeSample("s", "x"), config)
|
|
assert "tokenization failed" in str(failure.value)
|
|
|
|
def tokenizer_returning(value):
|
|
return SimpleNamespace(encode=lambda text, *, add_special_tokens: value)
|
|
|
|
assert contracts._encode_sample(
|
|
tokenizer_returning(torch.tensor([1, 2])), ProbeSample("s", "x"), config
|
|
) == (1, 2)
|
|
for bad in ([], "12", [1, True], [1, 2.0]):
|
|
with pytest.raises(ContractError):
|
|
contracts._encode_sample(tokenizer_returning(bad), ProbeSample("s", "x"), config)
|
|
with pytest.raises(ContractError):
|
|
contracts._encode_sample(SimpleNamespace(), ProbeSample("s", "x"), config)
|
|
|
|
|
|
def test_prepare_corpus_rejects_misuse_and_oversized_or_out_of_vocab_input(monkeypatch):
|
|
config = _config()
|
|
tokenizer = _Tokenizer()
|
|
harm, safe = _samples("harm"), _samples("safe")
|
|
kwargs = dict(source_tokenizer_digest=DIGEST, config=config)
|
|
with pytest.raises(ContractError):
|
|
prepare_probe_corpus(tokenizer, harm, safe, source_tokenizer_digest=DIGEST, config=None)
|
|
with pytest.raises(ContractError):
|
|
prepare_probe_corpus(tokenizer, "harm", safe, **kwargs)
|
|
with pytest.raises(ContractError):
|
|
prepare_probe_corpus(tokenizer, (harm[0], "x"), safe, **kwargs)
|
|
monkeypatch.setattr(contracts, "MAX_PROBE_CORPUS_BYTES", 8)
|
|
with pytest.raises(ContractError) as failure:
|
|
prepare_probe_corpus(tokenizer, harm, safe, **kwargs)
|
|
assert "total byte bound" in str(failure.value)
|
|
|
|
|
|
def test_direction_canonicalization_requires_one_dimension():
|
|
with pytest.raises(ContractError):
|
|
contracts.canonicalize_direction(torch.ones(2, 2))
|
|
|
|
|
|
def _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]]
|
|
),
|
|
}
|
|
|
|
|
|
def test_layer_artifacts_refuse_bad_hook_sets_errors_and_svd_failures(monkeypatch):
|
|
values = _matrices()
|
|
digests = {name: tensor_digest(value) for name, value in values.items()}
|
|
config = ProbeDistillConfig()
|
|
kwargs = dict(harmful_count=2, harmless_count=2, activation_digests=digests)
|
|
with pytest.raises(ContractError) as failure:
|
|
compute_layer_artifacts(
|
|
values, harmful_count=1, harmless_count=3, config=config, activation_digests=digests
|
|
)
|
|
assert "paired class counts" in str(failure.value)
|
|
with pytest.raises(ContractError):
|
|
compute_layer_artifacts({}, config=config, **kwargs)
|
|
with pytest.raises(ContractError):
|
|
compute_layer_artifacts({"layers.0": values["model.layers.0"]}, config=config, **kwargs)
|
|
with pytest.raises(ContractError) as failure:
|
|
compute_layer_artifacts(
|
|
values, config=config, activation_errors={"model.layers.0": [0.0, 0.0]}, **kwargs
|
|
)
|
|
assert "error evidence" in str(failure.value)
|
|
with pytest.raises(ContractError) as failure:
|
|
compute_layer_artifacts(values, config=_config(selected_layer_count=2), **kwargs)
|
|
assert "selected_layer_count" in str(failure.value)
|
|
|
|
svd = ProbeDistillConfig(direction_method=DirectionMethod.SVD, n_directions=1)
|
|
|
|
def broken_svd(*_args, **_kwargs):
|
|
raise RuntimeError("svd did not converge")
|
|
|
|
monkeypatch.setattr(torch.linalg, "svd", broken_svd)
|
|
with pytest.raises(ContractError) as failure:
|
|
compute_layer_artifacts(values, config=svd, **kwargs)
|
|
assert "SVD failed" in str(failure.value)
|
|
|
|
def nonfinite_svd(matrix, *, full_matrices):
|
|
nan = torch.full((matrix.shape[1],), float("nan"), dtype=matrix.dtype)
|
|
return None, nan, nan.unsqueeze(0)
|
|
|
|
monkeypatch.setattr(torch.linalg, "svd", nonfinite_svd)
|
|
with pytest.raises(ContractError) as failure:
|
|
compute_layer_artifacts(values, config=svd, **kwargs)
|
|
assert "non-finite" in str(failure.value)
|
|
|
|
|
|
def test_layer_summary_type_is_required_in_summaries():
|
|
with pytest.raises(ContractError):
|
|
_summary(layers=(SimpleNamespace(site="model.layers.0"),))
|
|
assert isinstance(_artifact().summary_record(), LayerProbeSummary)
|