Files
OBLITERATUS/tests/test_distributed_verification_edges.py
Joseph MaglyandClaude Opus 5.5 5da7ec7bb4 fix(verify): require a corroborated global extent for sharded heads
Review of 5a4835e: with no manifest global shape, a sharded head fell back
to mutable config.vocab_size, so 16-row shards with config vocab 16 passed
on both ranks. A vocab-sharded head now needs the manifest global row
extent corroborated by the live local weight shape and a complete even
shard layout (local rows x world size, per-rank start/end and rank
shards); config alone never establishes it, and anything else fails closed
with LMS_VERIFY_FORWARD_CONTRACT in baseline and post-edit revalidation.
Docs state the exact rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 17:11:28 -04:00

1051 lines
40 KiB
Python

"""Single-process VERIFY contract and collective-guard edges without Gloo (#304).
Collective primitives are replaced by a one-rank in-memory fake, so each test
drives exactly one fallible guard and asserts its stable failure code.
"""
from __future__ import annotations
import json
import time
from dataclasses import replace
from types import SimpleNamespace
import pytest
import torch
import obliteratus.distributed.verification as verify
import obliteratus.distributed.verification_contracts as verification_contracts
from obliteratus.distributed.contracts import (
ContractError,
RuntimeContractError,
RuntimeStage,
StageMessage,
Vote,
canonical_record,
contract_digest,
)
from obliteratus.distributed.model_manifest import ManifestPlacement, ParameterShard
from obliteratus.distributed.probe_contracts import tensor_digest
from obliteratus.distributed.surgery import ExciseTransactionState
from obliteratus.distributed.verification_contracts import (
RankVerificationEvidence,
VerificationBaselineSummary,
VerificationConfig,
VerificationKind,
VerificationSample,
VerificationSummary,
prepare_verification_corpus,
verification_refusal_token_digest,
verification_target_token_digest,
)
from tests.test_distributed_verification_contracts import _corpus, _metrics, _samples, _Tokenizer
pytestmark = pytest.mark.cpu
DIGEST = "a" * 64
RUN_ID = "1" * 32
# --- verification_contracts.py --------------------------------------------------------
def _baseline_summary(**changes) -> VerificationBaselineSummary:
values = {
"schema_version": 1,
"run_id": RUN_ID,
"distilled_digest": DIGEST,
"manifest_digest": DIGEST,
"expected_excise_config_digest": DIGEST,
"verification_config_digest": DIGEST,
"corpus_digest": DIGEST,
"sample_ids_digest": DIGEST,
"tokenizer_contract_digest": DIGEST,
"tokenization_digest": DIGEST,
"pristine_state_digest": DIGEST,
"baseline_logit_digest": DIGEST,
"sample_count": 3,
"sequence_position_count": 3,
"vocab_size": 32,
"baseline_bytes": 768,
"rank_max_abs_error_hex": (0.0).hex(),
"rank_max_rel_error_hex": (0.5).hex(),
}
values.update(changes)
return VerificationBaselineSummary(**values)
def _evidence(**changes) -> RankVerificationEvidence:
values = {
"schema_version": 1,
"rank": 0,
"sequence": 0,
"verification_identity_digest": DIGEST,
"pristine_state_digest": DIGEST,
"post_state_digest": DIGEST,
"tokenization_digest": DIGEST,
"post_logit_digest": DIGEST,
"metrics": _metrics(),
"quality_accepted": True,
}
values.update(changes)
return RankVerificationEvidence(**values)
def _summary(**changes) -> VerificationSummary:
values = {
"schema_version": 1,
"run_id": RUN_ID,
"excise_digest": DIGEST,
"baseline_digest": DIGEST,
"config_digest": DIGEST,
"corpus_digest": DIGEST,
"pristine_state_digest": DIGEST,
"post_state_digest": DIGEST,
"rank_evidence_set_digest": DIGEST,
"metrics": _metrics(),
"quality_accepted": True,
}
values.update(changes)
return VerificationSummary(**values)
@pytest.mark.parametrize(
("changes", "message"),
[
({"schema_version": 2}, "schema_version must be 1"),
({"run_id": "1" * 31}, "128-bit hexadecimal identity"),
({"run_id": "A" * 32}, "128-bit hexadecimal identity"),
({"corpus_digest": "A" * 64}, "lowercase SHA-256 digest"),
({"rank_max_abs_error_hex": 0.0}, "canonical hexadecimal float"),
({"rank_max_abs_error_hex": "not-hex"}, "canonical hexadecimal float"),
({"rank_max_abs_error_hex": "0x1p+0"}, "canonical hexadecimal float"),
({"rank_max_abs_error_hex": "inf"}, "canonical hexadecimal float"),
({"rank_max_rel_error_hex": (-1.0).hex()}, "canonical hexadecimal float"),
({"sequence_position_count": 2}, "sequence_position_count"),
],
)
def test_baseline_summary_is_closed_and_canonical(changes, message):
assert _baseline_summary().digest == _baseline_summary().digest
with pytest.raises(ContractError, match=message):
_baseline_summary(**changes)
@pytest.mark.parametrize(
("changes", "message"),
[
({"schema_version": 2}, "schema_version must be 1"),
({"rank": 2}, "verification rank"),
({"post_logit_digest": "x"}, "post_logit_digest"),
({"metrics": {}}, "metrics are invalid"),
({"quality_accepted": 1}, "decision must be boolean"),
],
)
def test_rank_evidence_rejects_open_fields(changes, message):
with pytest.raises(ContractError, match=message):
_evidence(**changes)
def test_rank_evidence_record_decoder_is_closed():
record = json.loads(canonical_record(_evidence()))
assert RankVerificationEvidence.from_record(record) == _evidence()
with pytest.raises(ContractError, match="evidence fields changed"):
RankVerificationEvidence.from_record([record])
with pytest.raises(ContractError, match="evidence fields changed"):
RankVerificationEvidence.from_record({**record, "extra": 1})
with pytest.raises(ContractError, match="metric fields changed"):
RankVerificationEvidence.from_record({**record, "metrics": {"nonfinite_count": 0}})
with pytest.raises(ContractError, match="metric fields changed"):
RankVerificationEvidence.from_record({**record, "metrics": None})
@pytest.mark.parametrize(
("changes", "message"),
[
({"schema_version": 2}, "schema_version must be 1"),
({"metrics": None}, "metrics are invalid"),
({"run_id": "bad"}, "verification run_id"),
],
)
def test_verification_summary_rejects_open_fields(changes, message):
assert _summary().digest != _summary(post_state_digest="b" * 64).digest
with pytest.raises(ContractError, match=message):
_summary(**changes)
@pytest.mark.parametrize(
("field", "value"),
[
("refusal_rate_hex", (1.5).hex()),
("forward_kl_hex", "nan"),
("nonfinite_count", -1),
],
)
def test_metrics_reject_out_of_range_values(field, value):
with pytest.raises(ContractError):
replace(_metrics(), **{field: value})
def test_config_requires_numeric_thresholds():
with pytest.raises(ContractError, match="rank_logit_atol must be finite"):
VerificationConfig(rank_logit_atol="0")
with pytest.raises(ContractError, match="max_refusal_rate must be finite"):
VerificationConfig(max_refusal_rate=True)
@pytest.mark.parametrize("values", ["1 2", b"\x01", (1, True), (1, 2.0)])
def test_token_sequence_digests_require_integer_sequences(values):
with pytest.raises(ContractError, match="integer sequence"):
verification_target_token_digest(values)
with pytest.raises(ContractError, match="integer sequence"):
verification_refusal_token_digest(values)
def test_token_sequence_digests_are_domain_separated():
assert verification_target_token_digest((7, 8)) != verification_refusal_token_digest((7, 8))
assert verification_target_token_digest([7, 8]) == verification_target_token_digest((7, 8))
def test_corpus_rejects_missing_kind_size_and_tokenization_drift(monkeypatch):
corpus = _corpus()
with pytest.raises(ContractError, match="every preregistered kind"):
replace(
corpus,
kinds=(VerificationKind.HARMFUL, VerificationKind.HARMFUL, VerificationKind.CAPABILITY),
)
with pytest.raises(ContractError, match="tokenization digest disagrees"):
replace(corpus, tokenization_digest="0" * 64)
with pytest.raises(ContractError, match="tokenizer_contract_digest"):
replace(corpus, tokenizer_contract_digest="bad")
monkeypatch.setattr(verification_contracts, "MAX_VERIFICATION_INPUT_BYTES", 1)
with pytest.raises(ContractError, match="token matrices exceed"):
replace(corpus)
def _prepare(tokenizer=None, samples=None, **kwargs):
values = {
"refusal_token_ids": (7, 8),
"source_tokenizer_digest": "b" * 64,
"config": VerificationConfig(max_tokens=8),
}
values.update(kwargs)
return prepare_verification_corpus(
_Tokenizer() if tokenizer is None else tokenizer,
_samples() if samples is None else samples,
**values,
)
class _TemplateTokenizer(_Tokenizer):
chat_template = "{{ messages }}"
def __init__(self, result=None):
self.result = result
self.calls = []
def apply_chat_template(self, messages, *, tokenize, add_generation_prompt):
self.calls.append((messages, tokenize, add_generation_prompt))
if self.result is not None:
return self.result
return [1, 4 + len(messages[0]["content"]) % 20, 2]
def test_required_chat_template_tokenizes_through_the_template():
tokenizer = _TemplateTokenizer()
config = VerificationConfig(
max_tokens=8, chat_template_mode="required", add_generation_prompt=True
)
corpus = _prepare(tokenizer, config=config)
assert corpus.input_width == 3
assert len(tokenizer.calls) == 3
assert all(tokenize and prompt for _messages, tokenize, prompt in tokenizer.calls)
assert corpus.tokenizer_contract_digest != _prepare(config=VerificationConfig(max_tokens=8)).tokenizer_contract_digest
def test_tensor_tokenization_is_accepted_as_integer_rows():
tokenizer = _TemplateTokenizer(result=torch.tensor([1, 5, 2]))
config = VerificationConfig(max_tokens=8, chat_template_mode="required")
assert _prepare(tokenizer, config=config).input_ids[0] == (1, 5, 2)
@pytest.mark.parametrize(
("kwargs", "message"),
[
({"config": None}, "config is required"),
({"source_tokenizer_digest": "bad"}, "source_tokenizer_digest"),
({"samples": "harm,coh,cap"}, "bounded sequence"),
({"samples": b"samples"}, "bounded sequence"),
(
{
"samples": (
VerificationSample("harm-0", VerificationKind.HARMFUL, "a b"),
VerificationSample("harm-1", VerificationKind.HARMFUL, "c d"),
VerificationSample("cap-0", VerificationKind.CAPABILITY, "e f", 9),
)
},
"cover every preregistered kind",
),
(
{
"samples": (
VerificationSample("harm-0", VerificationKind.HARMFUL, "a b"),
VerificationSample("coh-0", VerificationKind.COHERENCE, "c d"),
VerificationSample("cap-0", VerificationKind.CAPABILITY, "e f", 32),
)
},
"outside the tokenizer vocabulary",
),
({"config": VerificationConfig(max_tokens=8, chat_template_mode="required")}, "requires a tokenizer chat template"),
],
)
def test_corpus_preparation_rejects_invalid_requests(kwargs, message):
with pytest.raises(ContractError, match=message):
_prepare(**kwargs)
def test_corpus_preparation_bounds_total_text_bytes(monkeypatch):
monkeypatch.setattr(verification_contracts, "MAX_VERIFICATION_CORPUS_BYTES", 10)
with pytest.raises(ContractError, match="total byte bound"):
_prepare()
class _BrokenIdentity(_Tokenizer):
vocab_size = None
class _BadTemplate(_Tokenizer):
chat_template = 7
class _Encoding(_Tokenizer):
def __init__(self, result):
self.result = result
def encode(self, text, *, add_special_tokens):
return self.result
@pytest.mark.parametrize(
("tokenizer", "message"),
[
(_BrokenIdentity(), "identity is incomplete"),
(_BadTemplate(), "chat template is invalid"),
(_Encoding([1]), "empty, truncated, or oversized"),
(_Encoding(list(range(1, 10))), "empty, truncated, or oversized"),
(_Encoding("12"), "empty, truncated, or oversized"),
(_Encoding([1, 2.0]), "non-integer token"),
(_Encoding([1, True]), "non-integer token"),
(_Encoding([1, 32]), "out-of-vocabulary"),
(_Encoding([1, -1]), "out-of-vocabulary"),
],
)
def test_corpus_preparation_rejects_untrusted_tokenizer_output(tokenizer, message):
with pytest.raises(ContractError, match=message):
_prepare(tokenizer)
# --- verification.py single-rank guards -----------------------------------------------
@pytest.fixture
def solo(monkeypatch):
"""Replace collectives with a one-rank world that records every vote."""
votes: list[tuple[int, bool]] = []
def vote(sequence, accepted, *, group):
votes.append((sequence, bool(accepted)))
return bool(accepted)
monkeypatch.setattr(verify, "unanimous_vote", vote)
monkeypatch.setattr(
verify, "gloo_all_gather_records", lambda record, *, group: [canonical_record(record)]
)
monkeypatch.setattr(verify.dist, "broadcast", lambda tensor, *, src, group: None)
monkeypatch.setattr(verify.dist, "all_reduce", lambda tensor, *, op, group: None)
monkeypatch.setattr(verify, "require_consensus_digest", lambda digest, *, group: None)
return votes
@pytest.fixture
def sharded_solo(solo, monkeypatch):
"""Exercise one local TP2 shard with identical peer continuation records."""
monkeypatch.setattr(
verify, "gloo_all_gather_records", lambda record, *, group: [canonical_record(record)] * 2,
)
return solo
def _runtime(rank=0, world_size=1, **changes):
values = {
"rank": rank,
"world_size": world_size,
"run_id": RUN_ID,
"distilled_digest": DIGEST,
"control_group": "control",
"data_group": "data",
"tensor_device": torch.device("cpu"),
"manifest": SimpleNamespace(digest="b" * 64, entries=()),
"model": None,
"available_host_bytes": 1 << 40,
"available_device_bytes": 1 << 40,
}
values.update(changes)
return SimpleNamespace(**values)
def _raise(*_args, **_kwargs):
raise RuntimeError("private backend detail")
def _code(call) -> str:
with pytest.raises(RuntimeContractError) as raised:
call()
assert "private" not in str(raised.value)
return raised.value.code
def test_group_identity_requires_gloo_control_and_fixed_ranks(solo, monkeypatch):
runtime = _runtime(world_size=2)
monkeypatch.setattr(verify.dist, "get_rank", lambda group: 0)
monkeypatch.setattr(verify.dist, "get_world_size", lambda group: 2)
monkeypatch.setattr(verify.dist, "get_backend", lambda group: "gloo")
verify._group_identity(runtime)
monkeypatch.setattr(verify.dist, "get_backend", lambda group: "nccl")
assert _code(lambda: verify._group_identity(runtime)) == "LMS_VERIFY_CONFIG_INVALID"
monkeypatch.setattr(verify.dist, "get_backend", lambda group: "gloo")
monkeypatch.setattr(verify.dist, "get_world_size", lambda group: 3)
assert _code(lambda: verify._group_identity(runtime)) == "LMS_VERIFY_CONFIG_INVALID"
monkeypatch.setattr(verify.dist, "get_backend", _raise)
assert _code(lambda: verify._group_identity(runtime)) == "LMS_VERIFY_COLLECTIVE_FAILED"
def test_record_broadcast_role_and_shape_guards(solo, monkeypatch):
coordinator, worker = _runtime(rank=0), _runtime(rank=1)
assert verify._broadcast_record(coordinator, lambda: {"value": 1}, sequence=5) == {"value": 1}
assert solo == [(5, True), (6, True)]
solo.clear()
assert _code(lambda: verify._broadcast_record(coordinator, None, sequence=5)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
assert solo == [(5, False)]
assert _code(lambda: verify._broadcast_record(worker, lambda: {}, sequence=5)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
assert _code(lambda: verify._broadcast_record(coordinator, lambda: [1], sequence=5)) == (
"LMS_VERIFY_INPUT_INVALID"
)
monkeypatch.setattr(verify.dist, "broadcast", _raise)
assert _code(lambda: verify._broadcast_record(coordinator, lambda: {}, sequence=5)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
def test_tensor_broadcast_role_and_shape_guards(solo, monkeypatch):
coordinator, worker = _runtime(rank=0), _runtime(rank=1)
value = verify._broadcast_tensor(
coordinator, lambda: torch.ones(2, 3), shape=(2, 3), dtype=torch.int64, sequence=1
)
assert value.dtype is torch.int64 and torch.equal(value, torch.ones(2, 3, dtype=torch.int64))
received = verify._broadcast_tensor(worker, None, shape=(4,), dtype=torch.int64, sequence=1)
assert tuple(received.shape) == (4,) and received.dtype is torch.int64
for runtime, source in (
(coordinator, lambda: torch.ones(3, 2)),
(coordinator, None),
(coordinator, lambda: [[1, 1, 1]] * 2),
(worker, lambda: torch.ones(2, 3)),
):
assert _code(
lambda: verify._broadcast_tensor(
runtime, source, shape=(2, 3), dtype=torch.int64, sequence=1
)
) == "LMS_VERIFY_COLLECTIVE_FAILED"
monkeypatch.setattr(verify.dist, "broadcast", _raise)
assert _code(
lambda: verify._broadcast_tensor(worker, None, shape=(1,), dtype=torch.int64, sequence=1)
) == "LMS_VERIFY_COLLECTIVE_FAILED"
def _plan_record(**changes):
record = {name: DIGEST for name in verify._VerificationPlan.__dataclass_fields__}
record.update(
schema_version=1,
sample_count=3,
kind_counts={"harmful": 1, "coherence": 1, "capability": 1},
refusal_token_count=1,
vocab_size=32,
pad_token_id=0,
input_width=3,
sequence_position_count=3,
)
record.update(changes)
return record
@pytest.mark.parametrize(
"record",
[
[],
{"schema_version": 1},
_plan_record(kind_counts={"harmful": 3}),
_plan_record(kind_counts=[1, 1, 1]),
],
)
def test_plan_record_is_closed(record):
with pytest.raises(RuntimeContractError) as raised:
verify._plan_from_record(record)
assert raised.value.code == "LMS_VERIFY_INPUT_INVALID"
def test_plan_record_round_trips_exact_fields():
plan = verify._plan_from_record(_plan_record())
assert plan.sample_count == 3 and plan.kind_counts["capability"] == 1
def test_malformed_plan_values_are_voted_invalid_not_raised(solo, monkeypatch):
plan = verify._plan_from_record(_plan_record(tokenizer_contract_digest=7))
monkeypatch.setattr(verify, "_broadcast_record", lambda *args, **kwargs: {})
monkeypatch.setattr(verify, "_plan_from_record", lambda _record: plan)
runtime = _runtime(manifest=SimpleNamespace(digest=DIGEST))
config = VerificationConfig()
plan = replace(
plan,
expected_excise_config_digest=DIGEST,
verification_config_digest=config.digest,
)
code = _code(
lambda: verify._prepare_inputs(
runtime, config, expected_excise_config_digest=DIGEST, coordinator_corpus=_corpus()
)
)
assert code == "LMS_VERIFY_INPUT_INVALID"
assert solo[-1] == (10_000_001, False)
def test_continuation_records_must_match_the_requested_sequence(solo, monkeypatch):
runtime = _runtime()
config = VerificationConfig()
verify._check_continue(
runtime, config, started=time.monotonic(), sequence=4, cancellation_requested=lambda: False
)
monkeypatch.setattr(
verify,
"gloo_all_gather_records",
lambda record, *, group: [canonical_record({**record, "sequence": 5})],
)
assert _code(
lambda: verify._check_continue(
runtime, config, started=time.monotonic(), sequence=4,
cancellation_requested=lambda: False,
)
) == "LMS_VERIFY_COLLECTIVE_FAILED"
assert solo[-1] == (100_004, False)
def test_rank_logit_error_guards_each_collective(solo, monkeypatch):
runtime = _runtime()
logits = torch.arange(6, dtype=torch.float32).reshape(1, 2, 3)
assert verify._rank_logit_error(runtime, logits, sequence=10) == (0.0, 0.0)
nonfinite = logits.clone()
nonfinite[0, 0, 0] = float("inf")
assert _code(lambda: verify._rank_logit_error(runtime, nonfinite, sequence=10)) == (
"LMS_VERIFY_NONFINITE"
)
monkeypatch.setattr(verify.dist, "all_reduce", _raise)
assert _code(lambda: verify._rank_logit_error(runtime, logits, sequence=10)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
monkeypatch.setattr(verify.dist, "broadcast", _raise)
assert _code(lambda: verify._rank_logit_error(runtime, logits, sequence=10)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
def _state_runtime(local_value_digest=None):
model = torch.nn.Linear(2, 2, bias=False).eval()
weight = dict(model.named_parameters())["weight"]
entry = SimpleNamespace(
name="weight",
local_shape=(2, 2),
dtype="float32",
placement=ManifestPlacement.REPLICATE,
local_value_digest=local_value_digest or tensor_digest(weight),
)
runtime = _runtime(model=model, manifest=SimpleNamespace(digest="b" * 64, entries=(entry,)))
return runtime, weight
def test_logical_state_roots_bind_each_replicated_parameter(solo):
runtime, weight = _state_runtime()
roots, state = verify._logical_state_roots(
runtime, sequence_base=100, require_manifest_values=True
)
assert roots == {"weight": contract_digest((tensor_digest(weight),))}
assert state == verify._state_roots_digest(roots)
def test_logical_state_roots_refuse_manifest_value_drift(solo):
runtime, _ = _state_runtime(local_value_digest="0" * 64)
assert verify._logical_state_roots(runtime, sequence_base=100)[0]
assert _code(
lambda: verify._logical_state_roots(
runtime, sequence_base=100, require_manifest_values=True
)
) == "LMS_VERIFY_STATE_DIVERGED"
assert solo[-1] == (100, False)
def test_logical_state_roots_map_collective_failures(solo, monkeypatch):
runtime, _ = _state_runtime()
original_gather = verify.gloo_all_gather_records
def fail_parameter_gather(record, *, group):
if "local_digest" in record:
raise RuntimeError("private")
return original_gather(record, group=group)
monkeypatch.setattr(verify, "gloo_all_gather_records", fail_parameter_gather)
assert _code(lambda: verify._logical_state_roots(runtime, sequence_base=100)) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
monkeypatch.setattr(verify, "gloo_all_gather_records", original_gather)
monkeypatch.setattr(verify, "require_consensus_digest", _raise)
assert _code(lambda: verify._logical_state_roots(runtime, sequence_base=100)) == (
"LMS_VERIFY_STATE_DIVERGED"
)
class _LogitModel(torch.nn.Module):
vocab = 5
def forward(self, *, input_ids, attention_mask, use_cache):
assert use_cache is False
batch, width = input_ids.shape
logits = torch.linspace(-1.0, 1.0, batch * width * self.vocab).reshape(
batch, width, self.vocab
)
return SimpleNamespace(logits=logits)
def _inputs(count=3, width=3, vocab=_LogitModel.vocab):
plan = SimpleNamespace(
sample_count=count, vocab_size=vocab, input_width=width,
sequence_position_count=count * (width - 1), refusal_token_count=1,
)
return SimpleNamespace(
plan=plan,
input_ids=torch.ones(count, width, dtype=torch.int64),
attention_mask=torch.ones(count, width, dtype=torch.int64),
)
def _evaluate(runtime, config=None, inputs=None):
return verify._evaluate(
runtime, _inputs() if inputs is None else inputs,
config or VerificationConfig(batch_size=2),
started=time.monotonic(), phase_sequence=12_000_000,
cancellation_requested=lambda: False,
)
def test_evaluate_snapshots_trimmed_log_probabilities(solo):
snapshot = _evaluate(_runtime(model=_LogitModel().eval()))
assert len(snapshot.sequence_log_probs) == 3
assert tuple(snapshot.next_log_probs.shape) == (3, _LogitModel.vocab)
assert all(tuple(item.shape) == (2, _LogitModel.vocab) for item in snapshot.sequence_log_probs)
assert snapshot.logit_digest == verify._snapshot_digest(
snapshot.sequence_log_probs, snapshot.next_log_probs
)
assert snapshot.nbytes == 9 * _LogitModel.vocab * 4
def test_evaluate_refuses_training_mode_model(solo):
assert _code(lambda: _evaluate(_runtime(model=_LogitModel().train()))) == (
"LMS_VERIFY_FORWARD_CONTRACT"
)
assert solo[-1] == (12_000_009, False)
@pytest.mark.parametrize("metadata", ["weight", "out_features", "config", "observed"])
def test_evaluate_accepts_padded_vocab_and_normalizes_all_columns(solo, metadata):
model = _LogitModel().eval()
model.vocab = 32
if metadata == "weight":
# Live replicated weight shape wins over stale tokenizer-sized metadata.
model.lm_head = SimpleNamespace(weight=torch.zeros(32, 4), out_features=9)
model.config = SimpleNamespace(vocab_size=9)
elif metadata == "out_features":
model.lm_head = SimpleNamespace(out_features=32)
elif metadata == "config":
model.config = SimpleNamespace(vocab_size=32)
snapshot = _evaluate(_runtime(model=model), inputs=_inputs(vocab=9))
assert snapshot.next_log_probs.shape == (3, 32)
expected = torch.log_softmax(torch.linspace(-1.0, 1.0, 2 * 3 * 32).reshape(2, 3, 32), dim=-1)
assert torch.equal(snapshot.next_log_probs[:2], expected[:, -1])
assert snapshot.nbytes == 9 * 32 * 4
def _sharded_head_runtime(*, output_vocab=32, config_vocab=16, **entry_changes):
model = _LogitModel().eval()
model.vocab = output_vocab
model.lm_head = SimpleNamespace(weight=torch.zeros(16, 4), out_features=16)
model.config = SimpleNamespace(vocab_size=config_vocab)
fields = dict(
name="lm_head.weight", placement=ManifestPlacement.SHARD, shard_dim=0,
global_shape=(32, 4), local_shape=(16, 4), shard_start=0, shard_end=16,
owner_ranks=(0, 1), rank_shards=(
ParameterShard(0, 0, 16, (16, 4)), ParameterShard(1, 16, 32, (16, 4)),
),
)
fields.update(entry_changes)
entry = SimpleNamespace(**fields)
return _runtime(world_size=2, model=model, manifest=SimpleNamespace(entries=(entry,)))
@pytest.mark.parametrize("tokenizer_vocab", [9, 20])
@pytest.mark.parametrize("config_vocab", [9, 16, 32])
def test_evaluate_requires_corroborated_global_logits_for_a_vocab_sharded_head(
sharded_solo, tokenizer_vocab, config_vocab,
):
# The corroborated manifest wins over unpadded or shard-sized config.
runtime = _sharded_head_runtime(config_vocab=config_vocab)
assert _evaluate(runtime, inputs=_inputs(vocab=tokenizer_vocab)).next_log_probs.shape == (3, 32)
@pytest.mark.parametrize("entry_changes", [
{"global_shape": None},
{"global_shape": (16, 4)}, # Claimed global rows equal one local shard.
{"global_shape": (33, 4)},
{"global_shape": (32, 8)},
{"global_shape": (True, 4)},
{"global_shape": (32,)},
{"local_shape": (8, 4)},
{"shard_start": 1},
{"shard_end": 32},
{"shard_dim": False},
{"owner_ranks": (0,)},
{"rank_shards": (ParameterShard(0, 0, 16, (16, 4)),)},
{"rank_shards": (
ParameterShard(0, 0, 16, (16, 4)), ParameterShard(1, 0, 16, (16, 4)),
)},
])
@pytest.mark.parametrize("output_vocab", [16, 32])
def test_sharded_head_rejects_uncorroborated_global_extent(sharded_solo, entry_changes, output_vocab):
runtime = _sharded_head_runtime(output_vocab=output_vocab, **entry_changes)
assert _code(lambda: _evaluate(runtime, inputs=_inputs(vocab=9))) == (
"LMS_VERIFY_FORWARD_CONTRACT"
)
assert sharded_solo[-1] == (12_000_002, False)
@pytest.mark.parametrize("metadata,output_vocab", [
("manifest", 16), ("config", 16), ("config", 32), ("missing", 16), ("missing", 32),
])
def test_sharded_head_rejects_local_logits_or_missing_global_extent(
sharded_solo, metadata, output_vocab,
):
if metadata == "manifest":
runtime = _sharded_head_runtime(output_vocab=output_vocab)
else:
model = _LogitModel().eval()
model.vocab = output_vocab
weight = torch.zeros(16, 4)
model.lm_head = SimpleNamespace(weight=weight, out_features=16)
# Exercise DTensor placement detection without a manifest head entry.
weight.placements = (SimpleNamespace(is_shard=lambda dim: dim == 0),)
if metadata == "config":
model.config = SimpleNamespace(vocab_size=output_vocab)
runtime = _runtime(world_size=2, model=model)
assert _code(lambda: _evaluate(runtime, inputs=_inputs(vocab=9))) == (
"LMS_VERIFY_FORWARD_CONTRACT"
)
assert sharded_solo[-1] == (12_000_002, False)
@pytest.mark.parametrize("fault", [
"missing", "wrong_type", "sparse", "ndim", "batch", "sequence", "small_vocab",
"head_mismatch", "integer", "device",
])
def test_evaluate_rejects_invalid_forward_contract(solo, fault):
model = _LogitModel().eval()
if fault == "head_mismatch":
model.config = SimpleNamespace(vocab_size=32)
original = model.forward
def forward(**kwargs):
output = original(**kwargs)
logits = output.logits
if fault == "missing":
return SimpleNamespace()
output.logits = {
"wrong_type": None,
"sparse": lambda: logits.to_sparse(),
"ndim": lambda: logits[0],
"batch": lambda: logits[:1],
"sequence": lambda: logits[:, :1],
"small_vocab": lambda: logits[:, :, :4],
"integer": lambda: logits.long(),
"device": lambda: logits.to("meta"),
}.get(fault, lambda: logits)
if callable(output.logits):
output.logits = output.logits()
return output
model.forward = forward
assert _code(lambda: _evaluate(_runtime(model=model))) == "LMS_VERIFY_FORWARD_CONTRACT"
assert solo[-1] == (12_000_002, False)
def test_evaluate_classifies_forward_exception_without_exposing_details(solo):
model = _LogitModel().eval()
model.forward = _raise
assert _code(lambda: _evaluate(_runtime(model=model))) == "LMS_VERIFY_FORWARD_FAILED"
assert solo[-1] == (12_000_001, False)
@pytest.mark.parametrize("position", [0, 2])
def test_evaluate_rejects_nonfinite_even_outside_tokenizer_vocab_or_attention(solo, position):
model = _LogitModel().eval()
model.vocab = 32
original = model.forward
def forward(**kwargs):
output = original(**kwargs)
output.logits[0, position, 31] = float("nan")
return output
model.forward = forward
inputs = _inputs(vocab=9)
inputs.attention_mask[0, 2] = 0
assert _code(lambda: _evaluate(_runtime(model=model), inputs=inputs)) == "LMS_VERIFY_NONFINITE"
assert solo[-1] == (12_000_008, False)
def test_evaluate_readmits_observed_output_width_before_snapshot_allocation(solo):
model = _LogitModel().eval()
model.vocab = 32
config = VerificationConfig(batch_size=2, max_baseline_bytes=9 * 9 * 4)
assert _code(lambda: _evaluate(_runtime(model=model), config, _inputs(vocab=9))) == (
"LMS_VERIFY_CONFIG_INVALID"
)
assert solo[-1] == (12_200_000, False)
def test_evaluate_refuses_rank_divergent_logits(solo, monkeypatch):
monkeypatch.setattr(verify, "_rank_logit_error", lambda *args, **kwargs: (1.0, 1.0))
assert _code(lambda: _evaluate(_runtime(model=_LogitModel().eval()))) == (
"LMS_VERIFY_METRICS_DIVERGED"
)
assert solo[-1] == (12_000_006, False)
def test_evaluate_maps_snapshot_digest_disagreement(solo, monkeypatch):
monkeypatch.setattr(verify, "require_consensus_digest", _raise)
assert _code(lambda: _evaluate(_runtime(model=_LogitModel().eval()))) == (
"LMS_VERIFY_METRICS_DIVERGED"
)
def _metric_baseline(next_log_probs, *, tokenizer_vocab=4):
vocab = next_log_probs.shape[1]
inputs = SimpleNamespace(
plan=SimpleNamespace(vocab_size=tokenizer_vocab),
kinds=(VerificationKind.HARMFUL, VerificationKind.COHERENCE, VerificationKind.CAPABILITY),
refusal_token_ids=(3,),
input_ids=torch.tensor([[0, 1, 2], [0, 1, 2], [0, 1, 2]]),
attention_mask=torch.ones(3, 3, dtype=torch.int64),
target_token_ids=(-1, -1, 1),
)
rows = tuple(torch.log_softmax(torch.zeros(2, vocab), dim=-1) for _ in range(3))
snapshot = SimpleNamespace(sequence_log_probs=rows, next_log_probs=next_log_probs)
return SimpleNamespace(inputs=inputs, snapshot=snapshot), snapshot
def test_metrics_of_unchanged_model_retain_quality():
baseline, snapshot = _metric_baseline(torch.log_softmax(torch.zeros(3, 4), dim=-1))
metrics = verify._metrics(baseline, snapshot, VerificationConfig())
assert float.fromhex(metrics.coherence_retention_hex) == 1.0
assert float.fromhex(metrics.forward_kl_hex) == 0.0
assert float.fromhex(metrics.capability_retention_hex) == 1.0
assert verify._quality_accepted(metrics, VerificationConfig())
def test_padded_argmax_cannot_hide_a_tokenizer_valid_refusal():
logits = torch.zeros(3, 32)
logits[:, 3] = 5.0
logits[:, 31] = 6.0
log_probs = torch.log_softmax(logits, dim=-1)
baseline, snapshot = _metric_baseline(log_probs, tokenizer_vocab=9)
config = replace(VerificationConfig(), max_refusal_rate=0.0)
assert log_probs[0].argmax().item() == 31
metrics = verify._metrics(baseline, snapshot, config)
assert float.fromhex(metrics.refusal_rate_hex) == 1.0
assert not verify._quality_accepted(metrics, config)
# Valid target probabilities and sequence NLL use the full normalization.
assert float.fromhex(metrics.post_capability_probability_hex) == pytest.approx(
log_probs[2, 1].double().exp().item(), rel=1e-13,
)
assert float.fromhex(metrics.post_coherence_nll_hex) == pytest.approx(
torch.log(torch.tensor(32.0, dtype=torch.float64)).item(), rel=1e-6,
)
def test_metrics_refuse_non_finite_aggregates():
baseline, _ = _metric_baseline(torch.log_softmax(torch.zeros(3, 4), dim=-1))
post_next = torch.log_softmax(torch.zeros(3, 4), dim=-1)
post_next[2, 1] = float("nan")
post = SimpleNamespace(
sequence_log_probs=baseline.snapshot.sequence_log_probs, next_log_probs=post_next
)
with pytest.raises(RuntimeContractError) as raised:
verify._metrics(baseline, post, VerificationConfig())
assert raised.value.code == "LMS_VERIFY_NONFINITE"
def test_rank_evidence_and_lifecycle_gathers_map_failures(solo, monkeypatch):
runtime = _runtime()
assert verify._gather_rank_evidence(runtime, _evidence()) == (_evidence(),)
# A one-rank lifecycle gather cannot cover the exact (0, 1) TP2 membership.
assert _code(
lambda: verify._emit_verified(runtime, identity_digest=DIGEST, fault_injector=None)
) == "LMS_VERIFY_COLLECTIVE_FAILED"
assert solo[-1] == (13_000_902, False)
monkeypatch.setattr(verify, "gloo_all_gather_records", _raise)
assert _code(lambda: verify._gather_rank_evidence(runtime, _evidence())) == (
"LMS_VERIFY_COLLECTIVE_FAILED"
)
assert _code(
lambda: verify._emit_verified(runtime, identity_digest=DIGEST, fault_injector=None)
) == "LMS_VERIFY_COLLECTIVE_FAILED"
def test_lifecycle_gather_accepts_exact_two_rank_commit(solo, monkeypatch):
runtime = _runtime(world_size=2)
messages = tuple(
StageMessage(RUN_ID, DIGEST, rank, 7, RuntimeStage.VERIFIED, Vote.COMMITTED)
for rank in (0, 1)
)
monkeypatch.setattr(
verify, "gloo_all_gather_records", lambda record, *, group: [m.to_bytes() for m in messages]
)
assert verify._emit_verified(runtime, identity_digest=DIGEST, fault_injector=None) == messages
def _transition(pristine="p" * 64, post="q" * 64, *, roots=None, pristine_roots=None):
operation = SimpleNamespace(logical_name="weight")
summary = SimpleNamespace(
digest="d" * 64,
logical_pristine_digest=pristine,
logical_post_digest=post,
logical_update_digest=post,
)
transaction = SimpleNamespace(
operations=(operation,),
pristine_roots=(pristine,) if pristine_roots is None else pristine_roots,
operation_summaries=(summary,),
)
excise = SimpleNamespace(
transaction=transaction,
summary=SimpleNamespace(postcondition_set_digest=contract_digest(("d" * 64,))),
)
baseline = SimpleNamespace(logical_roots=roots or {"weight": pristine, "frozen": "f" * 64})
return excise, baseline
def test_state_transition_accepts_exact_edited_and_frozen_roots():
excise, baseline = _transition()
verify._validate_state_transition(excise, baseline, {"weight": "q" * 64, "frozen": "f" * 64})
@pytest.mark.parametrize(
("changes", "post_roots", "message"),
[
({"pristine_roots": ()}, {"weight": "q" * 64}, "proof is incomplete"),
({}, {"weight": "r" * 64, "frozen": "f" * 64}, "post-edit model roots disagree"),
({"roots": {"weight": "x" * 64}}, {"weight": "q" * 64}, "post-edit model roots disagree"),
({}, {"weight": "q" * 64, "frozen": "g" * 64}, "immutable parameter"),
],
)
def test_state_transition_refuses_incomplete_or_divergent_proofs(changes, post_roots, message):
excise, baseline = _transition(**changes)
with pytest.raises(RuntimeContractError, match=message) as raised:
verify._validate_state_transition(excise, baseline, post_roots)
assert raised.value.code == "LMS_VERIFY_STATE_DIVERGED"
def test_verify_without_an_excise_capability_is_config_invalid(solo):
with pytest.raises(RuntimeContractError) as raised:
verify.run_distributed_verify(object(), None, None)
assert raised.value.code == "LMS_VERIFY_CONFIG_INVALID"
assert solo == []
def test_baseline_rejects_invalid_config_before_any_collective(solo, monkeypatch):
class Runtime(SimpleNamespace):
pass
monkeypatch.setattr(verify, "SurgeryRuntime", Runtime)
runtime = Runtime(**vars(_runtime()))
for config, digest in ((None, DIGEST), (VerificationConfig(), "bad")):
solo.clear()
with pytest.raises(RuntimeContractError) as raised:
verify.prepare_distributed_verification_baseline(
runtime, config, expected_excise_config_digest=digest, coordinator_corpus=None
)
assert raised.value.code == "LMS_VERIFY_CONFIG_INVALID"
assert solo == [(10_000_012, False)]
def _verified_result(tmp_path, runtime, *, postcondition):
summaries = (SimpleNamespace(digest="d" * 64),)
transaction = SimpleNamespace(
runtime=runtime,
state=ExciseTransactionState.VERIFIED,
journal=SimpleNamespace(active=False, root=tmp_path / "released-journal"),
operations=(object(),),
operation_summaries=summaries,
)
excise = SimpleNamespace(
digest="e" * 64,
transaction=transaction,
summary=SimpleNamespace(postcondition_set_digest=postcondition),
)
return verify.VerificationResult(
summary=_summary(excise_digest="e" * 64),
rank_evidence=(),
verified_messages=(),
excise_result=excise,
)
def test_revalidation_refuses_wrong_type_and_changed_proof(solo, tmp_path):
runtime = _runtime()
impostor = SimpleNamespace(excise_result=SimpleNamespace(transaction=SimpleNamespace(runtime=runtime)))
assert _code(lambda: verify.revalidate_distributed_verified_state(impostor)) == (
"LMS_VERIFY_STATE_DIVERGED"
)
changed = _verified_result(tmp_path, runtime, postcondition="0" * 64)
assert _code(lambda: verify.revalidate_distributed_verified_state(changed)) == (
"LMS_VERIFY_STATE_DIVERGED"
)
assert solo[-1] == (16_050_000, False)
def test_revalidation_translates_unexpected_failures(solo, tmp_path, monkeypatch):
runtime = _runtime()
result = _verified_result(
tmp_path, runtime, postcondition=contract_digest(("d" * 64,))
)
monkeypatch.setattr(verify, "_record_consensus", lambda *args, **kwargs: DIGEST)
monkeypatch.setattr(verify, "_logical_state_roots", _raise)
assert _code(lambda: verify.revalidate_distributed_verified_state(result)) == (
"LMS_VERIFY_STATE_DIVERGED"
)
monkeypatch.setattr(
verify, "_logical_state_roots", lambda *args, **kwargs: ({}, DIGEST)
)
assert verify.revalidate_distributed_verified_state(result) == ({}, DIGEST)
assert solo[-1] == (16_050_001, True)