Files
OBLITERATUS/obliteratus/distributed/verification.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

1918 lines
79 KiB
Python

"""Experimental TP2 collective baseline and fail-closed post-EXCISE VERIFY (#244)."""
from __future__ import annotations
import hashlib
import json
import math
import re
import time
from collections.abc import Callable, Iterator, Mapping, Sequence
from contextlib import contextmanager
from dataclasses import dataclass, field
from typing import cast
import torch
import torch.distributed as dist
from obliteratus.distributed.consensus import (
decode_frame,
encode_frame,
gloo_all_gather_records,
require_consensus_digest,
unanimous_vote,
)
from obliteratus.distributed.contracts import (
ContractError,
RuntimeContractError,
RuntimeStage,
StageMessage,
Vote,
advance_stage,
canonical_record,
contract_digest,
)
from obliteratus.distributed.model_manifest import (
MAX_MANIFEST_PARAMETERS,
ManifestPlacement,
ParameterManifestEntry,
ParameterShard,
)
from obliteratus.distributed.model_session import LiveModelSession
from obliteratus.distributed.probe import load_exact_probe_tokenizer
from obliteratus.distributed.probe_contracts import (
TENSOR_DIGEST_WORKING_BYTES,
ProbeDistillResult,
_tensor_all_finite,
tensor_digest,
token_rows_digest,
)
from obliteratus.distributed.surgery import (
ExciseResult,
ExciseTransaction,
ExciseTransactionState,
SurgeryRuntime,
excise_host_memory_bytes,
rollback_distributed_excise,
run_distributed_excise,
runtime_from_live_session,
)
from obliteratus.distributed.surgery_contracts import ExciseConfig, excise_digest_set
from obliteratus.distributed.verification_contracts import (
CoordinatorVerificationCorpus,
RankVerificationEvidence,
VerificationBaselineSummary,
VerificationConfig,
VerificationKind,
VerificationMetrics,
VerificationSample,
VerificationSummary,
prepare_verification_corpus,
verification_aggregate_digest,
verification_refusal_token_digest,
verification_target_token_digest,
)
VERIFY_ERROR_CODES = frozenset(
{
"LMS_VERIFY_BASELINE_INVALID",
"LMS_VERIFY_CANCELLED",
"LMS_VERIFY_CLEANUP_FAILED",
"LMS_VERIFY_COLLECTIVE_FAILED",
"LMS_VERIFY_CONFIG_INVALID",
"LMS_VERIFY_FAILED",
"LMS_VERIFY_FORWARD_CONTRACT",
"LMS_VERIFY_FORWARD_FAILED",
"LMS_VERIFY_INPUT_INVALID",
"LMS_VERIFY_METRICS_DIVERGED",
"LMS_VERIFY_NONFINITE",
"LMS_VERIFY_QUALITY_FAILED",
"LMS_VERIFY_STATE_DIVERGED",
"LMS_VERIFY_STATE_UNCERTAIN",
"LMS_VERIFY_TIMEOUT",
}
)
# VERIFY reserves [10_000_000, 17_000_000), disjoint from EXCISE (including
# projection's operation-dependent range). Baseline: 10M; roots: 11M/14M;
# evaluation: 12M/15M (10 slots per batch); VERIFY: 13M; live setup and
# rollback whole-model roots: 16M.
# A successful capability's export recheck uses guards 16_050_000/001 and
# roots 16_050_100 (including terminal vote 16_950_100). This is a subsequent
# attempt after VERIFY succeeds, so it never overlaps a VERIFY rollback.
# Root entries use three slots; terminal work has separate offsets.
# Evaluation memory admission uses a separate +200_000 range so all three
# forward-result votes fit the existing ten slots per batch.
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
# Keep these bounds in sync with _verification_memory_bytes and the reductions.
_METRIC_CHUNK_ROWS = 16
# Three simultaneous float64 KL chunks, plus row reductions/indices and scalars.
_METRIC_BYTES_PER_LOGIT = 32
_METRIC_SCALAR_BYTES = 128
# Output, reference, conversions, error temporaries, and padded log probabilities.
_FORWARD_BYTES_PER_LOGIT = 48
@dataclass(frozen=True)
class _VerificationPlan:
schema_version: int
tokenizer_contract_digest: str
corpus_digest: str
sample_ids_digest: str
target_token_digest: str
refusal_token_digest: str
tokenization_digest: str
sample_count: int
kind_counts: Mapping[str, int]
refusal_token_count: int
vocab_size: int
pad_token_id: int
input_width: int
sequence_position_count: int
expected_excise_config_digest: str
verification_config_digest: str
distilled_digest: str
manifest_digest: str
@dataclass(frozen=True)
class _VerificationInputs:
plan: _VerificationPlan
input_ids: torch.Tensor = field(repr=False)
attention_mask: torch.Tensor = field(repr=False)
kinds: tuple[VerificationKind, ...]
target_token_ids: tuple[int, ...] = field(repr=False)
refusal_token_ids: tuple[int, ...] = field(repr=False)
@dataclass(frozen=True)
class _EvaluationSnapshot:
"""Full model-distribution log probabilities, including padded columns."""
sequence_log_probs: tuple[torch.Tensor, ...] = field(repr=False)
next_log_probs: torch.Tensor = field(repr=False)
logit_digest: str
rank_max_abs_error: float
rank_max_rel_error: float
@property
def nbytes(self) -> int:
return self.next_log_probs.numel() * self.next_log_probs.element_size() + sum(
item.numel() * item.element_size() for item in self.sequence_log_probs
)
@dataclass(frozen=True)
class VerificationBaseline:
"""Private pristine logits and state roots bound to one immutable attempt."""
summary: VerificationBaselineSummary
inputs: _VerificationInputs = field(repr=False)
snapshot: _EvaluationSnapshot = field(repr=False)
logical_roots: Mapping[str, str] = field(repr=False)
@property
def digest(self) -> str:
return self.summary.digest
@dataclass(frozen=True)
class VerificationResult:
"""A non-publishable VERIFIED-stage capability for distributed REBIRTH."""
summary: VerificationSummary
rank_evidence: tuple[RankVerificationEvidence, ...]
verified_messages: tuple[StageMessage, ...]
excise_result: ExciseResult = field(repr=False)
@property
def digest(self) -> str:
return self.summary.digest
def _fault(
fault_injector: Callable[[str, int], None] | None,
point: str,
sequence: int,
) -> None:
if fault_injector is not None:
fault_injector(point, sequence)
def _fault_accept(
runtime: SurgeryRuntime,
fault_injector: Callable[[str, int], None] | None,
*,
point: str,
sequence: int,
vote_sequence: int,
) -> None:
local_ok = True
try:
_fault(fault_injector, point, sequence)
except BaseException:
local_ok = False
_collective_accept(
runtime,
vote_sequence,
local_ok,
code="LMS_VERIFY_FAILED",
message="not every rank completed a VERIFY transition",
)
def _group_identity(runtime: SurgeryRuntime) -> None:
try:
control = str(dist.get_backend(runtime.control_group)).lower()
data = str(dist.get_backend(runtime.data_group)).lower()
identity = (
dist.get_rank(runtime.control_group),
dist.get_world_size(runtime.control_group),
dist.get_rank(runtime.data_group),
dist.get_world_size(runtime.data_group),
)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification process-group identity is unavailable"
) from None
expected_data = "gloo" if runtime.tensor_device.type == "cpu" else "nccl"
if (
control != "gloo"
or data != expected_data
or identity
!= (
runtime.rank,
runtime.world_size,
runtime.rank,
runtime.world_size,
)
):
raise RuntimeContractError(
"LMS_VERIFY_CONFIG_INVALID", "verification process-group identity changed"
)
def _collective_accept(
runtime: SurgeryRuntime,
sequence: int,
accepted: bool,
*,
code: str,
message: str,
) -> None:
try:
agreed = unanimous_vote(sequence, accepted, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification vote failed"
) from None
if not agreed:
raise RuntimeContractError(code, message)
@contextmanager
def _local_step(
runtime: SurgeryRuntime,
sequence: int,
*,
code: str = "LMS_VERIFY_FAILED",
) -> Iterator[None]:
"""Vote after fallible local work, before any following collective.
The body must contain no collectives. Do not put fallible preparation
between this vote and the collective that consumes the prepared value.
"""
ready = True
try:
yield
except BaseException:
ready = False
_collective_accept(
runtime, sequence, ready, code=code,
message="not every rank completed local VERIFY preparation",
)
def _record_consensus(runtime: SurgeryRuntime, record: object, *, code: str) -> str:
# Pass raw records into the symmetric primitive: canonicalization, framing
# and copies run in its poison-protected region. The comparison/hash below
# is deterministic over identical gathered bytes (the process-fault boundary).
# Forward-internal failures between TP data collectives cannot be voted on
# from outside the model. They share the bounded process-group timeout /
# rank-loss uncertainty policy: fail closed, never establish success.
try:
payloads = gloo_all_gather_records(record, group=runtime.control_group)
if any(payload != payloads[0] for payload in payloads):
raise ContractError("rank records disagree")
return hashlib.sha256(payloads[0]).hexdigest()
except BaseException:
raise RuntimeContractError(code, "rank VERIFY records disagree") from None
def _check_continue(
runtime: SurgeryRuntime,
config: VerificationConfig,
*,
started: float,
sequence: int,
cancellation_requested: Callable[[], bool],
) -> None:
try:
cancelled = bool(cancellation_requested())
except BaseException:
cancelled = True
# Callback errors are cancellation; clock errors are deadline failure.
try:
timed_out = time.monotonic() - started > config.stage_timeout_seconds
except BaseException:
timed_out = True
try:
payloads = gloo_all_gather_records(
{"sequence": sequence, "cancelled": cancelled, "timed_out": timed_out},
group=runtime.control_group,
)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification cancellation consensus failed"
) from None
# Continuation decode and validation is local work, so vote before a rank
# can enter either the success path or journal restoration. This offset is
# outside the <=4096-batch evaluation slots and the static VERIFY slots.
with _local_step(runtime, sequence + 100_000, code="LMS_VERIFY_COLLECTIVE_FAILED"):
flags = tuple(json.loads(payload) for payload in payloads)
if len(flags) != runtime.world_size or any(
set(item) != {"sequence", "cancelled", "timed_out"}
or item["sequence"] != sequence
or not isinstance(item["cancelled"], bool)
or not isinstance(item["timed_out"], bool)
for item in flags
):
raise ContractError("verification continuation records disagree")
cancelled = any(item["cancelled"] for item in flags)
timed_out = any(item["timed_out"] for item in flags)
if cancelled:
raise RuntimeContractError("LMS_VERIFY_CANCELLED", "distributed VERIFY was cancelled")
if timed_out:
raise RuntimeContractError("LMS_VERIFY_TIMEOUT", "distributed VERIFY exceeded its bound")
def _broadcast_record(
runtime: SurgeryRuntime,
source: Callable[[], object] | None,
*,
sequence: int,
) -> dict[str, object]:
with _local_step(runtime, sequence, code="LMS_VERIFY_COLLECTIVE_FAILED"):
if runtime.rank == 0:
if source is None:
raise ContractError("coordinator verification record is unavailable")
frame = encode_frame(canonical_record(source()))
else:
if source is not None:
raise ContractError("non-coordinator supplied a verification record")
frame = encode_frame(b"")
try:
dist.broadcast(frame, src=0, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification record broadcast failed"
) from None
with _local_step(runtime, sequence + 1, code="LMS_VERIFY_INPUT_INVALID"):
decoded = json.loads(decode_frame(frame).decode("ascii"))
if not isinstance(decoded, dict):
raise ContractError("verification record is not a bounded object")
return decoded
def _broadcast_tensor(
runtime: SurgeryRuntime,
source: Callable[[], torch.Tensor] | None,
*,
shape: tuple[int, ...],
dtype: torch.dtype,
sequence: int,
) -> torch.Tensor:
with _local_step(runtime, sequence, code="LMS_VERIFY_COLLECTIVE_FAILED"):
if runtime.rank == 0:
tensor = source() if source is not None else None
if not isinstance(tensor, torch.Tensor) or tuple(tensor.shape) != shape:
raise ContractError("coordinator verification tensor shape changed")
value = tensor.detach().to(runtime.tensor_device, dtype=dtype).contiguous()
else:
if source is not None:
raise ContractError("non-coordinator supplied a verification tensor")
value = torch.empty(shape, dtype=dtype, device=runtime.tensor_device)
try:
dist.broadcast(value, src=0, group=runtime.data_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification tensor broadcast failed"
) from None
return value
def _plan_from_record(value: object) -> _VerificationPlan:
fields = set(_VerificationPlan.__dataclass_fields__)
if not isinstance(value, dict) or set(value) != fields:
raise RuntimeContractError("LMS_VERIFY_INPUT_INVALID", "verification plan fields changed")
try:
counts = value["kind_counts"]
if not isinstance(counts, dict) or set(counts) != {kind.value for kind in VerificationKind}:
raise ContractError("verification kind counts changed")
return _VerificationPlan(**value)
except (ContractError, TypeError):
raise RuntimeContractError(
"LMS_VERIFY_INPUT_INVALID", "verification plan is invalid"
) from None
def _verification_memory_bytes(
plan: _VerificationPlan,
config: VerificationConfig,
*,
output_vocab_size: int | None = None,
) -> tuple[int, int, int]:
"""Bound snapshot storage and peak host/device tensor working sets.
Host: inputs + two snapshots + max(forward/logprob, metric) + digest/finite chunks.
The forward bound also covers CPU execution, trimmed-row cloning, snapshot
digest serialization, and the duplicate next-token rows during stacking.
Batch tensors must be released before the next forward or snapshot digest.
"""
count, width = plan.sample_count, plan.input_width
vocab = (
plan.vocab_size if output_vocab_size is None else max(plan.vocab_size, output_vocab_size)
)
snapshot_bytes = (plan.sequence_position_count + count) * vocab * 4
input_bytes = count * width * 16 + (count * 2 + plan.refusal_token_count) * 8
batch_bytes = min(config.batch_size, count) * width * vocab * _FORWARD_BYTES_PER_LOGIT
forward_bytes = max(batch_bytes, count * vocab * 4)
metric_bytes = _METRIC_CHUNK_ROWS * vocab * _METRIC_BYTES_PER_LOGIT + _METRIC_SCALAR_BYTES
host_bytes = (
input_bytes + snapshot_bytes * 2 + max(forward_bytes, metric_bytes)
+ TENSOR_DIGEST_WORKING_BYTES
)
device_bytes = batch_bytes + count * width * 16
return snapshot_bytes, host_bytes, device_bytes
def _model_output_vocab_size(
runtime: SurgeryRuntime,
*,
logits_width: int | None = None,
) -> int | None:
"""Read the live head, never treating a TP-local weight extent as global.
Replicated head weights take precedence over out_features/config, which
can retain an unpadded size. Vocab-sharded heads require gathered logits
matching a manifest global row extent corroborated by the live local weight
and complete even-shard layout. Config alone cannot establish that extent.
Minimal runtimes without any head metadata may use the observed width.
"""
head = getattr(runtime.model, "lm_head", None)
weight = getattr(head, "weight", None)
head_entry = next(
(entry for entry in runtime.manifest.entries if entry.name == "lm_head.weight"), None,
)
sharded = bool(
head_entry is not None and head_entry.placement is ManifestPlacement.SHARD
and head_entry.shard_dim == 0
)
if weight is not None:
if not isinstance(weight, torch.Tensor) or weight.ndim != 2:
raise ContractError("verification output head weight is invalid")
sharded = sharded or any(
placement.is_shard(0) for placement in getattr(weight, "placements", ())
)
if sharded:
global_shape = getattr(head_entry, "global_shape", None)
local_shape = getattr(head_entry, "local_shape", None)
if (
head_entry is None
or head_entry.placement is not ManifestPlacement.SHARD
or type(head_entry.shard_dim) is not int or head_entry.shard_dim != 0
or not isinstance(global_shape, tuple) or len(global_shape) != 2
or not isinstance(local_shape, tuple) or len(local_shape) != 2
or any(type(size) is not int or size <= 0 for size in (*global_shape, *local_shape))
or weight is None or tuple(weight.shape) != local_shape
or type(runtime.world_size) is not int or runtime.world_size < 2
or type(runtime.rank) is not int or not 0 <= runtime.rank < runtime.world_size
):
raise ContractError("verification sharded head has no corroborated global vocabulary")
vocab = global_shape[0]
local_rows = local_shape[0]
if (
local_rows * runtime.world_size != vocab
or local_shape[1] != global_shape[1]
or type(head_entry.shard_start) is not int
or type(head_entry.shard_end) is not int
or head_entry.shard_start != runtime.rank * local_rows
or head_entry.shard_end != (runtime.rank + 1) * local_rows
or head_entry.owner_ranks != tuple(range(runtime.world_size))
or head_entry.rank_shards != tuple(
ParameterShard(rank, rank * local_rows, (rank + 1) * local_rows, local_shape)
for rank in range(runtime.world_size)
)
):
raise ContractError("verification sharded head global and local layouts disagree")
elif weight is not None:
vocab = weight.shape[0]
else:
vocab = getattr(head, "out_features", None)
if vocab is None:
vocab = getattr(getattr(runtime.model, "config", None), "vocab_size", None)
if vocab is None:
vocab = logits_width
if vocab is not None and (
isinstance(vocab, bool) or not isinstance(vocab, int) or not 2 <= vocab <= 2**31 - 1
):
raise ContractError("verification output vocabulary is invalid")
return vocab
def _admit_verification_memory(
runtime: SurgeryRuntime,
plan: _VerificationPlan,
config: VerificationConfig,
*,
output_vocab_size: int | None,
) -> None:
baseline_bytes, host_peak_bytes, peak_device_bytes = _verification_memory_bytes(
plan, config, output_vocab_size=output_vocab_size,
)
if not (
baseline_bytes <= config.max_baseline_bytes
and host_peak_bytes + config.safety_margin_bytes <= runtime.available_host_bytes
and peak_device_bytes + config.safety_margin_bytes <= runtime.available_device_bytes
):
raise ContractError("verification baseline working set was not admitted")
def _prepare_inputs(
runtime: SurgeryRuntime,
config: VerificationConfig,
*,
expected_excise_config_digest: str,
coordinator_corpus: CoordinatorVerificationCorpus | None,
) -> _VerificationInputs:
role_ok = (
isinstance(coordinator_corpus, CoordinatorVerificationCorpus)
if runtime.rank == 0
else coordinator_corpus is None
)
_collective_accept(
runtime,
10_000_000,
role_ok,
code="LMS_VERIFY_INPUT_INVALID",
message="only rank zero may own the verification corpus",
)
def source() -> dict[str, object]:
assert coordinator_corpus is not None
lengths = tuple(sum(row) for row in coordinator_corpus.attention_mask)
return {
**coordinator_corpus.plan_record(),
"sequence_position_count": sum(length - 1 for length in lengths),
"expected_excise_config_digest": expected_excise_config_digest,
"verification_config_digest": config.digest,
"distilled_digest": runtime.distilled_digest,
"manifest_digest": runtime.manifest.digest,
}
record = _broadcast_record(
runtime, source if runtime.rank == 0 else None, sequence=10_000_020,
)
with _local_step(runtime, 10_000_022, code="LMS_VERIFY_INPUT_INVALID"):
plan = _plan_from_record(record)
try:
valid_plan = bool(
plan.schema_version == 1
and plan.expected_excise_config_digest == expected_excise_config_digest
and plan.verification_config_digest == config.digest
and plan.distilled_digest == runtime.distilled_digest
and plan.manifest_digest == runtime.manifest.digest
and all(
_DIGEST_RE.fullmatch(value) is not None
for value in (
plan.tokenizer_contract_digest,
plan.corpus_digest,
plan.sample_ids_digest,
plan.target_token_digest,
plan.refusal_token_digest,
plan.tokenization_digest,
plan.expected_excise_config_digest,
plan.verification_config_digest,
plan.distilled_digest,
plan.manifest_digest,
)
)
and isinstance(plan.sample_count, int)
and not isinstance(plan.sample_count, bool)
and 3 <= plan.sample_count <= config.max_samples
and isinstance(plan.input_width, int)
and not isinstance(plan.input_width, bool)
and 2 <= plan.input_width <= config.max_tokens
and isinstance(plan.sequence_position_count, int)
and not isinstance(plan.sequence_position_count, bool)
and plan.sample_count
<= plan.sequence_position_count
<= plan.sample_count * (plan.input_width - 1)
and all(
isinstance(count, int) and not isinstance(count, bool) and count >= 1
for count in plan.kind_counts.values()
)
and sum(cast(int, count) for count in plan.kind_counts.values()) == plan.sample_count
and isinstance(plan.refusal_token_count, int)
and not isinstance(plan.refusal_token_count, bool)
and isinstance(plan.vocab_size, int)
and not isinstance(plan.vocab_size, bool)
and 2 <= plan.vocab_size <= 2**31 - 1
and 1 <= plan.refusal_token_count <= min(plan.vocab_size, 4_096)
and isinstance(plan.pad_token_id, int)
and not isinstance(plan.pad_token_id, bool)
and 0 <= plan.pad_token_id < plan.vocab_size
)
except BaseException:
valid_plan = False
_collective_accept(
runtime,
10_000_001,
valid_plan,
code="LMS_VERIFY_INPUT_INVALID",
message="verification plan identity or bounds disagree",
)
with _local_step(runtime, 10_000_037, code="LMS_VERIFY_FORWARD_CONTRACT"):
output_vocab_size = _model_output_vocab_size(runtime)
with _local_step(runtime, 10_000_035, code="LMS_VERIFY_CONFIG_INVALID"):
_admit_verification_memory(
runtime, plan, config, output_vocab_size=output_vocab_size,
)
sample_count, width = plan.sample_count, plan.input_width
def tensor_source(name: str) -> Callable[[], torch.Tensor] | None:
def make() -> torch.Tensor:
assert coordinator_corpus is not None
values = getattr(coordinator_corpus, name)
if name == "kinds":
values = tuple(list(VerificationKind).index(kind) for kind in values)
return torch.tensor(values, dtype=torch.int64)
return make if runtime.rank == 0 else None
input_ids = _broadcast_tensor(
runtime, tensor_source("input_ids"), shape=(sample_count, width),
dtype=torch.int64, sequence=10_000_030,
)
attention_mask = _broadcast_tensor(
runtime, tensor_source("attention_mask"), shape=(sample_count, width),
dtype=torch.int64, sequence=10_000_031,
)
kind_values = _broadcast_tensor(
runtime, tensor_source("kinds"), shape=(sample_count,),
dtype=torch.int64, sequence=10_000_032,
)
target_values = _broadcast_tensor(
runtime, tensor_source("target_token_ids"), shape=(sample_count,),
dtype=torch.int64, sequence=10_000_033,
)
refusal_values = _broadcast_tensor(
runtime, tensor_source("refusal_token_ids"), shape=(plan.refusal_token_count,),
dtype=torch.int64, sequence=10_000_034,
)
try:
kinds = tuple(list(VerificationKind)[int(value)] for value in kind_values.cpu())
targets = tuple(int(value) for value in target_values.cpu())
refusal = tuple(int(value) for value in refusal_values.cpu())
rows = tuple(tuple(int(value) for value in row) for row in input_ids.cpu())
masks = tuple(tuple(int(value) for value in row) for row in attention_mask.cpu())
valid = bool(
token_rows_digest(rows, masks) == plan.tokenization_digest
and verification_target_token_digest(targets) == plan.target_token_digest
and verification_refusal_token_digest(refusal) == plan.refusal_token_digest
and set(kinds) == set(VerificationKind)
and {kind.value: kinds.count(kind) for kind in VerificationKind} == plan.kind_counts
and tuple(sorted(set(refusal))) == refusal
and all(0 <= token < plan.vocab_size for row in rows for token in row)
and all(value in {0, 1} for row in masks for value in row)
and all(sum(row) >= 2 for row in masks)
and all(
masks[row][index] >= masks[row][index + 1]
for row in range(sample_count)
for index in range(width - 1)
)
and all(
rows[row][index] == plan.pad_token_id
for row in range(sample_count)
for index in range(width)
if masks[row][index] == 0
)
and all(0 <= token < plan.vocab_size for token in refusal)
and all(
(0 <= target < plan.vocab_size)
if kind is VerificationKind.CAPABILITY
else target == -1
for kind, target in zip(kinds, targets, strict=True)
)
)
except BaseException:
valid = False
kinds, targets, refusal = (), (), ()
_collective_accept(
runtime,
10_000_002,
valid,
code="LMS_VERIFY_INPUT_INVALID",
message="broadcast verification tensors violate their closed contract",
)
with _local_step(runtime, 10_000_036, code="LMS_VERIFY_INPUT_INVALID"):
inputs = _VerificationInputs(
plan=plan,
input_ids=input_ids.cpu(),
attention_mask=attention_mask.cpu(),
kinds=kinds,
target_token_ids=targets,
refusal_token_ids=refusal,
)
return inputs
def _state_roots_digest(roots: Mapping[str, str]) -> str:
# Match the name order bound by the manifest before the parameter loop.
return verification_aggregate_digest(
(
{"name_digest": hashlib.sha256(name.encode("utf-8")).hexdigest(), "root": root}
for name, root in sorted(roots.items())
),
domain="state-roots",
)
def _logical_state_roots(
runtime: SurgeryRuntime,
*,
sequence_base: int,
require_manifest_values: bool = False,
) -> tuple[dict[str, str], str]:
parameters: dict[str, torch.Tensor] = {}
entries: tuple[ParameterManifestEntry, ...] = ()
structural_valid = True
try:
parameters = dict(runtime.model.named_parameters(remove_duplicate=False))
entries = tuple(sorted(runtime.manifest.entries, key=lambda item: item.name))
structural_valid = bool(
1 <= len(entries) <= MAX_MANIFEST_PARAMETERS
and len(entries) == len(parameters)
and set(parameters) == {entry.name for entry in entries}
)
# The bounded logical manifest root binds entry order and placement on
# both ranks before the parameter-dependent collective loop begins.
root_order = {"manifest": runtime.manifest.digest}
except BaseException:
structural_valid = False
_collective_accept(
runtime,
sequence_base - 2,
structural_valid,
code="LMS_VERIFY_STATE_DIVERGED",
message="distributed model structure changed",
)
_record_consensus(runtime, root_order, code="LMS_VERIFY_STATE_DIVERGED")
roots: dict[str, str] = {}
for sequence, entry in enumerate(entries):
local_valid = True
local_digest = ""
try:
parameter = parameters[entry.name]
local_valid = bool(
tuple(parameter.shape) == entry.local_shape
and str(parameter.dtype).removeprefix("torch.") == entry.dtype
and parameter.layout is torch.strided
and parameter.device == runtime.tensor_device
and _tensor_all_finite(parameter)
)
local_digest = tensor_digest(parameter)
if require_manifest_values and local_digest != entry.local_value_digest:
local_valid = False
local_record = {
"rank": runtime.rank, "sequence": sequence_base + sequence * 3 + 1,
"local_digest": local_digest,
}
except BaseException:
local_valid = False
_collective_accept(
runtime,
sequence_base + sequence * 3,
local_valid,
code="LMS_VERIFY_STATE_DIVERGED",
message="distributed model parameter changed or became non-finite",
)
try:
payloads = gloo_all_gather_records(
local_record, group=runtime.control_group,
)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "distributed parameter evidence failed"
) from None
with _local_step(runtime, sequence_base + sequence * 3 + 1, code="LMS_VERIFY_STATE_DIVERGED"):
records = tuple(json.loads(payload.decode("ascii")) for payload in payloads)
evidence_valid = len(records) == runtime.world_size and not any(
not isinstance(item, dict)
or set(item) != {"rank", "sequence", "local_digest"}
or item["rank"] != rank
or item["sequence"] != sequence_base + sequence * 3 + 1
or not isinstance(item["local_digest"], str)
or _DIGEST_RE.fullmatch(item["local_digest"]) is None
for rank, item in enumerate(records)
)
digests = (
tuple(cast(str, item["local_digest"]) for item in records) if evidence_valid else ()
)
if entry.placement is ManifestPlacement.REPLICATE and (
len(digests) != runtime.world_size or len(set(digests)) != 1
):
evidence_valid = False
if not evidence_valid:
raise ContractError("parameter evidence changed")
roots[entry.name] = contract_digest(digests)
with _local_step(runtime, sequence_base + 900_000, code="LMS_VERIFY_STATE_DIVERGED"):
state_digest = _state_roots_digest(roots)
try:
require_consensus_digest(state_digest, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED", "distributed model state roots disagree"
) from None
return roots, state_digest
def _rank_logit_error(
runtime: SurgeryRuntime,
logits: torch.Tensor,
*,
sequence: int,
) -> tuple[float, float]:
with _local_step(runtime, sequence, code="LMS_VERIFY_COLLECTIVE_FAILED"):
reference = logits.detach().clone() if runtime.rank == 0 else torch.empty_like(logits)
try:
dist.broadcast(reference, src=0, group=runtime.data_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification logit broadcast failed"
) from None
with _local_step(runtime, sequence + 1, code="LMS_VERIFY_NONFINITE"):
difference = (logits.float() - reference.float()).abs()
absolute = difference.max().reshape(1).cpu()
relative = (difference / reference.float().abs().clamp_min(1e-12)).max().reshape(1).cpu()
try:
dist.all_reduce(absolute, op=dist.ReduceOp.MAX, group=runtime.control_group)
dist.all_reduce(relative, op=dist.ReduceOp.MAX, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "verification logit comparison failed"
) from None
with _local_step(runtime, sequence + 2, code="LMS_VERIFY_NONFINITE"):
errors = float(absolute.item()), float(relative.item())
if not all(math.isfinite(value) for value in errors):
raise ContractError("logit errors are non-finite")
return errors
def _snapshot_digest(
sequence_log_probs: Sequence[torch.Tensor],
next_log_probs: torch.Tensor,
) -> str:
def records() -> Iterator[object]:
for item in sequence_log_probs:
yield {"sequence": tensor_digest(item)}
yield {"next": tensor_digest(next_log_probs)}
return verification_aggregate_digest(records(), domain="snapshot")
def _evaluate(
runtime: SurgeryRuntime,
inputs: _VerificationInputs,
config: VerificationConfig,
*,
started: float,
phase_sequence: int,
cancellation_requested: Callable[[], bool],
expected_vocab_size: int | None = None,
) -> _EvaluationSnapshot:
sequence_rows: list[torch.Tensor] = []
next_rows: list[torch.Tensor] = []
max_absolute = 0.0
max_relative = 0.0
count = inputs.plan.sample_count
for batch_index, start in enumerate(range(0, count, config.batch_size)):
_check_continue(
runtime,
config,
started=started,
sequence=phase_sequence + batch_index * 10,
cancellation_requested=cancellation_requested,
)
stop = min(count, start + config.batch_size)
forward_ok = True
inference = None
try:
with _local_step(
runtime, phase_sequence + batch_index * 10 + 9, code="LMS_VERIFY_FORWARD_CONTRACT",
):
ids = inputs.input_ids[start:stop].to(runtime.tensor_device)
mask = inputs.attention_mask[start:stop].to(runtime.tensor_device)
if bool(getattr(runtime.model, "training", True)):
raise ContractError("verification model must remain in evaluation mode")
forward = runtime.model
inference = torch.inference_mode()
inference.__enter__()
except BaseException:
# The preparation vote already failed on every rank. Unwind local
# mode setup without replacing that unanimous failure.
if inference is not None:
try:
inference.__exit__(None, None, None)
except BaseException:
pass
raise
try:
try:
output = forward(input_ids=ids, attention_mask=mask, use_cache=False)
finally:
inference.__exit__(None, None, None)
except BaseException:
forward_ok = False
# Every rank votes in this order, even when different ranks fail for
# different reasons. No rank can enter a logit collective prematurely.
_collective_accept(
runtime,
phase_sequence + batch_index * 10 + 1,
forward_ok,
code="LMS_VERIFY_FORWARD_FAILED",
message="verification model forward raised",
)
with _local_step(
runtime, phase_sequence + batch_index * 10 + 2, code="LMS_VERIFY_FORWARD_CONTRACT",
):
logits = getattr(output, "logits", None)
contract_ok = bool(
isinstance(logits, torch.Tensor)
and logits.layout is torch.strided
and logits.ndim == 3
and tuple(logits.shape[:2]) == tuple(ids.shape)
and logits.shape[2] >= inputs.plan.vocab_size
and logits.shape[2] == _model_output_vocab_size(
runtime, logits_width=logits.shape[2],
)
and (expected_vocab_size is None or logits.shape[2] == expected_vocab_size)
and logits.device == runtime.tensor_device
and logits.is_floating_point()
)
if not contract_ok:
raise ContractError("verification logits violate the model output contract")
assert isinstance(logits, torch.Tensor)
expected_vocab_size = logits.shape[2]
# Check the entire padded tensor, including all output vocabulary
# columns. The larger model vocabulary never relaxes finiteness.
finite_ok = bool(torch.isfinite(logits).all())
logit_contract = {
"shape": tuple(logits.shape),
"dtype": str(logits.dtype).removeprefix("torch."),
}
_record_consensus(runtime, logit_contract, code="LMS_VERIFY_FORWARD_CONTRACT")
_collective_accept(
runtime,
phase_sequence + batch_index * 10 + 8,
finite_ok,
code="LMS_VERIFY_NONFINITE",
message="verification logits contain non-finite values",
)
# Metadata-free runtimes learn their output width after forward.
# Re-admit before allocating comparisons, log probabilities or snapshots.
with _local_step(
runtime, phase_sequence + 200_000 + batch_index, code="LMS_VERIFY_CONFIG_INVALID",
):
_admit_verification_memory(
runtime, inputs.plan, config, output_vocab_size=expected_vocab_size,
)
absolute, relative = _rank_logit_error(
runtime, logits, sequence=phase_sequence + batch_index * 10 + 3,
)
with _local_step(runtime, phase_sequence + batch_index * 10 + 6, code="LMS_VERIFY_METRICS_DIVERGED"):
max_absolute = max(max_absolute, absolute)
max_relative = max(max_relative, relative)
if not (absolute <= config.rank_logit_atol or relative <= config.rank_logit_rtol):
raise ContractError("verification logits diverge between ranks")
with _local_step(runtime, phase_sequence + batch_index * 10 + 7, code="LMS_VERIFY_NONFINITE"):
# Preserve the true model distribution, including padded columns.
# Token selection is restricted to tokenizer IDs in _metrics.
log_probs = torch.log_softmax(logits.float(), dim=-1).cpu()
for row_index in range(stop - start):
length = int(mask[row_index].sum().item())
# A contiguous view still retains the padded batch allocation.
# Keep independent trimmed storage matching admitted bytes.
sequence_rows.append(log_probs[row_index, : length - 1].clone())
next_rows.append(log_probs[row_index, length - 1].clone())
# Drop the padded CPU batch and forward output before another batch
# can allocate. Cloning overlap is covered by the transient bound.
del log_probs, logits, output, ids, mask
with _local_step(runtime, phase_sequence + 900_000, code="LMS_VERIFY_METRICS_DIVERGED"):
next_log_probs = torch.stack(next_rows)
next_rows.clear()
digest = _snapshot_digest(sequence_rows, next_log_probs)
snapshot = _EvaluationSnapshot(
sequence_log_probs=tuple(sequence_rows), next_log_probs=next_log_probs,
logit_digest=digest, rank_max_abs_error=max_absolute,
rank_max_rel_error=max_relative,
)
try:
require_consensus_digest(digest, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_METRICS_DIVERGED", "verification logit digests disagree"
) from None
return snapshot
def _baseline_capability_valid(
baseline: VerificationBaseline,
runtime: SurgeryRuntime,
config: VerificationConfig,
) -> bool:
"""Revalidate every mutable private baseline component before VERIFY."""
try:
inputs = baseline.inputs
plan = inputs.plan
summary = baseline.summary
snapshot = baseline.snapshot
output_vocab_size = snapshot.next_log_probs.shape[1]
rows = tuple(tuple(int(value) for value in row) for row in inputs.input_ids)
masks = tuple(tuple(int(value) for value in row) for row in inputs.attention_mask)
lengths = tuple(sum(row) for row in masks)
expected_names = {entry.name for entry in runtime.manifest.entries}
roots = dict(baseline.logical_roots)
tensors = (*snapshot.sequence_log_probs, snapshot.next_log_probs)
return bool(
inputs.input_ids.device.type == "cpu"
and inputs.input_ids.dtype is torch.int64
and tuple(inputs.input_ids.shape) == (plan.sample_count, plan.input_width)
and inputs.attention_mask.device.type == "cpu"
and inputs.attention_mask.dtype is torch.int64
and tuple(inputs.attention_mask.shape) == (plan.sample_count, plan.input_width)
and token_rows_digest(rows, masks) == plan.tokenization_digest
and plan.tokenization_digest == summary.tokenization_digest
and len(inputs.kinds) == plan.sample_count
and set(inputs.kinds) == set(VerificationKind)
and {kind.value: inputs.kinds.count(kind) for kind in VerificationKind}
== plan.kind_counts
and len(inputs.target_token_ids) == plan.sample_count
and verification_target_token_digest(inputs.target_token_ids)
== plan.target_token_digest
and verification_refusal_token_digest(inputs.refusal_token_ids)
== plan.refusal_token_digest
and all(
(0 <= target < plan.vocab_size)
if kind is VerificationKind.CAPABILITY
else target == -1
for kind, target in zip(
inputs.kinds,
inputs.target_token_ids,
strict=True,
)
)
and tuple(sorted(set(inputs.refusal_token_ids))) == inputs.refusal_token_ids
and all(0 <= value < plan.vocab_size for value in inputs.refusal_token_ids)
and len(snapshot.sequence_log_probs) == plan.sample_count
and output_vocab_size >= plan.vocab_size
and output_vocab_size == _model_output_vocab_size(
runtime, logits_width=output_vocab_size,
)
and tuple(snapshot.next_log_probs.shape) == (plan.sample_count, output_vocab_size)
and all(
tuple(value.shape) == (length - 1, output_vocab_size)
for value, length in zip(snapshot.sequence_log_probs, lengths, strict=True)
)
and all(
value.device.type == "cpu"
and value.dtype is torch.float32
and value.layout is torch.strided
and _tensor_all_finite(value)
for value in tensors
)
and snapshot.nbytes == summary.baseline_bytes
and snapshot.nbytes <= config.max_baseline_bytes
and _snapshot_digest(snapshot.sequence_log_probs, snapshot.next_log_probs)
== snapshot.logit_digest
== summary.baseline_logit_digest
and set(roots) == expected_names
and all(_DIGEST_RE.fullmatch(value) is not None for value in roots.values())
and _state_roots_digest(roots) == summary.pristine_state_digest
and summary.sample_count == plan.sample_count
and summary.sequence_position_count == sum(length - 1 for length in lengths)
and summary.vocab_size == plan.vocab_size
)
except BaseException:
return False
def prepare_distributed_verification_baseline(
runtime: SurgeryRuntime,
config: VerificationConfig,
*,
expected_excise_config_digest: str,
coordinator_corpus: CoordinatorVerificationCorpus | None,
cancellation_requested: Callable[[], bool] = lambda: False,
fault_injector: Callable[[str, int], None] | None = None,
) -> VerificationBaseline:
"""Capture a bounded pristine baseline before the first EXCISE mutation."""
try:
if not isinstance(runtime, SurgeryRuntime):
raise RuntimeContractError("LMS_VERIFY_CONFIG_INVALID", "verification runtime is invalid")
with _local_step(runtime, 10_000_012, code="LMS_VERIFY_CONFIG_INVALID"):
started = time.monotonic()
if not isinstance(config, VerificationConfig):
raise ContractError("verification config is invalid")
if not isinstance(expected_excise_config_digest, str) or _DIGEST_RE.fullmatch(expected_excise_config_digest) is None:
raise ContractError("expected EXCISE identity is invalid")
_group_identity(runtime)
identities = {
"config": config.digest, "expected_excise": expected_excise_config_digest,
"distilled": runtime.distilled_digest, "manifest": runtime.manifest.digest,
}
_record_consensus(runtime, identities, code="LMS_VERIFY_CONFIG_INVALID")
_fault_accept(
runtime,
fault_injector,
point="baseline_before",
sequence=-1,
vote_sequence=10_000_010,
)
inputs = _prepare_inputs(
runtime,
config,
expected_excise_config_digest=expected_excise_config_digest,
coordinator_corpus=coordinator_corpus,
)
roots, pristine_state_digest = _logical_state_roots(
runtime,
sequence_base=11_000_100,
require_manifest_values=True,
)
snapshot = _evaluate(
runtime,
inputs,
config,
started=started,
phase_sequence=12_000_000,
cancellation_requested=cancellation_requested,
)
_fault_accept(
runtime,
fault_injector,
point="baseline_after",
sequence=-1,
vote_sequence=10_000_011,
)
with _local_step(runtime, 10_000_013, code="LMS_VERIFY_BASELINE_INVALID"):
if snapshot.nbytes > config.max_baseline_bytes:
raise RuntimeContractError(
"LMS_VERIFY_CONFIG_INVALID", "verification baseline exceeded its admitted size"
)
summary = VerificationBaselineSummary(
schema_version=1,
run_id=runtime.run_id,
distilled_digest=runtime.distilled_digest,
manifest_digest=runtime.manifest.digest,
expected_excise_config_digest=expected_excise_config_digest,
verification_config_digest=config.digest,
corpus_digest=inputs.plan.corpus_digest,
sample_ids_digest=inputs.plan.sample_ids_digest,
tokenizer_contract_digest=inputs.plan.tokenizer_contract_digest,
tokenization_digest=inputs.plan.tokenization_digest,
pristine_state_digest=pristine_state_digest,
baseline_logit_digest=snapshot.logit_digest,
sample_count=inputs.plan.sample_count,
sequence_position_count=inputs.plan.sequence_position_count,
vocab_size=inputs.plan.vocab_size,
baseline_bytes=snapshot.nbytes,
rank_max_abs_error_hex=snapshot.rank_max_abs_error.hex(),
rank_max_rel_error_hex=snapshot.rank_max_rel_error.hex(),
)
baseline = VerificationBaseline(summary, inputs, snapshot, roots)
try:
_record_consensus(runtime, summary, code="LMS_VERIFY_BASELINE_INVALID")
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_BASELINE_INVALID", "rank verification baselines disagree"
) from None
_check_continue(
runtime, config, started=started, sequence=10_000_014,
cancellation_requested=cancellation_requested,
)
return baseline
except BaseException as error:
raise _translate_error(error) from None
def _forward_kl(
pristine: Sequence[torch.Tensor],
edited: Sequence[torch.Tensor],
) -> torch.Tensor:
"""Reduce full-distribution KL in fixed chunks with bounded float64 storage."""
total = torch.zeros((), dtype=torch.float64)
positions = 0
for before, after in zip(pristine, edited, strict=True):
for start in range(0, before.shape[0], _METRIC_CHUNK_ROWS):
stop = min(start + _METRIC_CHUNK_ROWS, before.shape[0])
probabilities = before[start:stop].double()
# At subtraction's peak, only three full float64 chunks are live.
difference = probabilities - after[start:stop].double()
probabilities.exp_().mul_(difference)
total.add_(probabilities.sum(dim=-1).sum())
positions += stop - start
del probabilities, difference
return total.div_(positions).clamp_min_(0.0)
def _metrics(
baseline: VerificationBaseline,
post: _EvaluationSnapshot,
config: VerificationConfig,
) -> VerificationMetrics:
inputs = baseline.inputs
harmful = [index for index, kind in enumerate(inputs.kinds) if kind is VerificationKind.HARMFUL]
coherence = [
index for index, kind in enumerate(inputs.kinds) if kind is VerificationKind.COHERENCE
]
capability = [
index for index, kind in enumerate(inputs.kinds) if kind is VerificationKind.CAPABILITY
]
refusal = set(inputs.refusal_token_ids)
# Padded model-only columns must never win a tokenizer-token decision.
tokenizer_next = post.next_log_probs[:, : inputs.plan.vocab_size]
refusal_rate = sum(
int(int(tokenizer_next[index].argmax().item()) in refusal) for index in harmful
) / len(harmful)
def coherence_nll(snapshot: _EvaluationSnapshot) -> torch.Tensor:
total = torch.zeros((), dtype=torch.float64)
positions = 0
for index in coherence:
length = int(inputs.attention_mask[index].sum().item())
# Inputs were validated against the tokenizer vocabulary. Index
# those IDs in full-width log_softmax, without renormalizing them.
targets = inputs.input_ids[index, 1:length]
for start in range(0, length - 1, _METRIC_CHUNK_ROWS):
stop = min(start + _METRIC_CHUNK_ROWS, length - 1)
rows = torch.arange(start, stop)
values = snapshot.sequence_log_probs[index][rows, targets[start:stop]].double()
total.sub_(values.sum())
positions += stop - start
del rows, values
return total.div_(positions)
baseline_nll = coherence_nll(baseline.snapshot)
post_nll = coherence_nll(post)
coherence_retention = torch.exp(torch.minimum(baseline_nll - post_nll, torch.zeros(())))
forward_kl = _forward_kl(baseline.snapshot.sequence_log_probs, post.sequence_log_probs)
def capability_probability(snapshot: _EvaluationSnapshot) -> torch.Tensor:
total = torch.zeros((), dtype=torch.float64)
for index in capability:
# Preregistered targets are tokenizer-valid; retain their true
# probabilities under the full model distribution.
total.add_(snapshot.next_log_probs[index, inputs.target_token_ids[index]].double().exp())
return total.div_(len(capability))
baseline_capability = capability_probability(baseline.snapshot)
post_capability = capability_probability(post)
capability_retention = _bounded_probability_retention(
post_capability,
baseline_capability,
config.probability_floor,
)
values = (
refusal_rate,
float(coherence_retention.item()),
float(forward_kl.item()),
float(capability_retention.item()),
float(baseline_nll.item()),
float(post_nll.item()),
float(baseline_capability.item()),
float(post_capability.item()),
)
if not all(math.isfinite(value) and value >= 0.0 for value in values):
raise RuntimeContractError(
"LMS_VERIFY_NONFINITE", "verification aggregate contains a non-finite value"
)
return VerificationMetrics(
refusal_rate_hex=float(refusal_rate).hex(),
coherence_retention_hex=float(coherence_retention.item()).hex(),
forward_kl_hex=float(forward_kl.item()).hex(),
capability_retention_hex=float(capability_retention.item()).hex(),
baseline_coherence_nll_hex=float(baseline_nll.item()).hex(),
post_coherence_nll_hex=float(post_nll.item()).hex(),
baseline_capability_probability_hex=float(baseline_capability.item()).hex(),
post_capability_probability_hex=float(post_capability.item()).hex(),
nonfinite_count=0,
)
def _bounded_probability_retention(
post: torch.Tensor,
baseline: torch.Tensor,
floor: float,
) -> torch.Tensor:
return torch.minimum(
post.clamp_min(floor) / baseline.clamp_min(floor),
torch.ones((), dtype=torch.float64),
)
def _quality_accepted(metrics: VerificationMetrics, config: VerificationConfig) -> bool:
return bool(
metrics.nonfinite_count == 0
and float.fromhex(metrics.refusal_rate_hex) <= config.max_refusal_rate
and float.fromhex(metrics.coherence_retention_hex) >= config.min_coherence_retention
and float.fromhex(metrics.forward_kl_hex) <= config.max_forward_kl
and float.fromhex(metrics.capability_retention_hex) >= config.min_capability_retention
)
def _gather_rank_evidence(
runtime: SurgeryRuntime,
local: RankVerificationEvidence,
) -> tuple[RankVerificationEvidence, ...]:
try:
payloads = gloo_all_gather_records(local, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "rank verification evidence gather failed"
) from None
with _local_step(runtime, 13_000_030, code="LMS_VERIFY_METRICS_DIVERGED"):
records = tuple(
RankVerificationEvidence.from_record(json.loads(payload.decode("ascii")))
for payload in payloads
)
if tuple(item.rank for item in records) != tuple(range(runtime.world_size)) or any(
item.sequence != local.sequence
or item.verification_identity_digest != local.verification_identity_digest
or item.pristine_state_digest != local.pristine_state_digest
or item.post_state_digest != local.post_state_digest
or item.tokenization_digest != local.tokenization_digest
or item.post_logit_digest != local.post_logit_digest
or item.metrics != local.metrics
or item.quality_accepted != local.quality_accepted
for item in records
):
raise ContractError("rank evidence is missing, duplicated, stale, or divergent")
return records
def _emit_verified(
runtime: SurgeryRuntime,
*,
identity_digest: str,
fault_injector: Callable[[str, int], None] | None,
) -> tuple[StageMessage, ...]:
with _local_step(runtime, 13_000_903, code="LMS_VERIFY_COLLECTIVE_FAILED"):
_fault(fault_injector, "lifecycle_before", 7)
advance_stage(RuntimeStage.MUTATING, RuntimeStage.VERIFIED)
local = StageMessage(
run_id=runtime.run_id, identity_digest=identity_digest, rank=runtime.rank,
sequence=7, stage=RuntimeStage.VERIFIED, vote=Vote.COMMITTED,
)
try:
payloads = gloo_all_gather_records(local, group=runtime.control_group)
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_COLLECTIVE_FAILED", "VERIFY lifecycle consensus failed"
) from None
with _local_step(runtime, 13_000_902, code="LMS_VERIFY_COLLECTIVE_FAILED"):
messages = tuple(StageMessage.from_bytes(payload) for payload in payloads)
if tuple(item.rank for item in messages) != (0, 1) or any(
item.run_id != runtime.run_id
or item.identity_digest != identity_digest
or item.sequence != 7
or item.stage is not RuntimeStage.VERIFIED
or item.vote is not Vote.COMMITTED
or item.error_code is not None
for item in messages
):
raise ContractError("VERIFY lifecycle records disagree")
_fault(fault_injector, "lifecycle_after", 7)
return messages
def _postcondition_set_digest(summaries: Sequence[object]) -> str:
# Share EXCISE's ordered, operation-bounded postcondition identity.
return excise_digest_set(item.digest for item in summaries)
def _validate_state_transition(
excise_result: ExciseResult,
baseline: VerificationBaseline,
post_roots: Mapping[str, str],
) -> None:
transaction = excise_result.transaction
if len(transaction.operations) != len(transaction.pristine_roots) or len(
transaction.operations
) != len(transaction.operation_summaries):
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED", "EXCISE transaction proof is incomplete"
)
if excise_result.summary.postcondition_set_digest != _postcondition_set_digest(
transaction.operation_summaries
):
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED", "EXCISE postcondition proof changed"
)
mutable_names = set()
for operation, pristine, summary in zip(
transaction.operations,
transaction.pristine_roots,
transaction.operation_summaries,
strict=True,
):
mutable_names.add(operation.logical_name)
if (
baseline.logical_roots.get(operation.logical_name) != pristine
or summary.logical_pristine_digest != pristine
or summary.logical_post_digest != post_roots.get(operation.logical_name)
or summary.logical_post_digest != summary.logical_update_digest
):
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED", "pristine or post-edit model roots disagree"
)
if any(
post_roots.get(name) != root
for name, root in baseline.logical_roots.items()
if name not in mutable_names
):
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED", "EXCISE changed an immutable parameter"
)
def run_distributed_verify(
excise_result: ExciseResult,
baseline: VerificationBaseline,
config: VerificationConfig,
*,
cancellation_requested: Callable[[], bool] = lambda: False,
fault_injector: Callable[[str, int], None] | None = None,
) -> VerificationResult:
"""Verify a committed collective edit, or prove rollback and return no result."""
transaction: ExciseTransaction | None = (
excise_result.transaction if isinstance(excise_result, ExciseResult) else None
)
runtime = transaction.runtime if transaction is not None else None
cleanup_started = False
rollback_allowed = False
try:
if not isinstance(runtime, SurgeryRuntime):
raise RuntimeContractError("LMS_VERIFY_CONFIG_INVALID", "VERIFY runtime is unavailable")
committed = False
try:
committed = transaction is not None and transaction.state is ExciseTransactionState.COMMITTED
except BaseException:
pass
_collective_accept(
runtime, 13_000_019, committed,
code="LMS_VERIFY_STATE_UNCERTAIN",
message="not every rank owns an active committed VERIFY transaction",
)
rollback_allowed = True
with _local_step(runtime, 13_000_020, code="LMS_VERIFY_CONFIG_INVALID"):
started = time.monotonic()
if (
not isinstance(excise_result, ExciseResult)
or not isinstance(baseline, VerificationBaseline)
or not isinstance(config, VerificationConfig)
or transaction is None
or transaction.state is not ExciseTransactionState.COMMITTED
or not isinstance(runtime, SurgeryRuntime)
):
raise RuntimeContractError(
"LMS_VERIFY_CONFIG_INVALID", "VERIFY requires one active committed transaction"
)
_group_identity(runtime)
identity_valid = bool(
baseline.summary.run_id == runtime.run_id == excise_result.summary.run_id
and baseline.summary.distilled_digest == runtime.distilled_digest
and baseline.summary.manifest_digest == runtime.manifest.digest
and baseline.summary.expected_excise_config_digest
== excise_result.summary.config_digest
and baseline.summary.verification_config_digest == config.digest
and baseline.inputs.plan.corpus_digest == baseline.summary.corpus_digest
and baseline.snapshot.logit_digest == baseline.summary.baseline_logit_digest
)
identity_records = {"excise": excise_result.summary, "baseline": baseline.summary, "config": config.digest}
_collective_accept(
runtime,
13_000_000,
identity_valid,
code="LMS_VERIFY_BASELINE_INVALID",
message="verification baseline is stale or belongs to another edit",
)
_collective_accept(
runtime,
13_000_001,
_baseline_capability_valid(baseline, runtime, config),
code="LMS_VERIFY_BASELINE_INVALID",
message="verification baseline contents changed after the pristine pass",
)
_record_consensus(
runtime, identity_records,
code="LMS_VERIFY_CONFIG_INVALID",
)
_check_continue(
runtime,
config,
started=started,
sequence=13_000_002,
cancellation_requested=cancellation_requested,
)
_fault_accept(
runtime,
fault_injector,
point="verify_before",
sequence=-1,
vote_sequence=13_000_010,
)
post_roots, post_state_digest = _logical_state_roots(runtime, sequence_base=14_000_100)
state_transition_valid = True
try:
_validate_state_transition(excise_result, baseline, post_roots)
except BaseException:
state_transition_valid = False
_collective_accept(
runtime,
13_000_003,
state_transition_valid,
code="LMS_VERIFY_STATE_DIVERGED",
message="EXCISE transaction proof disagrees with the live model",
)
post = _evaluate(
runtime,
baseline.inputs,
config,
started=started,
phase_sequence=15_000_000,
cancellation_requested=cancellation_requested,
expected_vocab_size=baseline.snapshot.next_log_probs.shape[1],
)
_fault_accept(
runtime,
fault_injector,
point="verify_after",
sequence=-1,
vote_sequence=13_000_011,
)
with _local_step(runtime, 13_000_021, code="LMS_VERIFY_NONFINITE"):
metrics = _metrics(baseline, post, config)
accepted = _quality_accepted(metrics, config)
verification_identity = contract_digest(
{
"run_id": runtime.run_id,
"excise_digest": excise_result.digest,
"baseline_digest": baseline.digest,
"config_digest": config.digest,
"corpus_digest": baseline.summary.corpus_digest,
"pristine_state_digest": baseline.summary.pristine_state_digest,
"post_state_digest": post_state_digest,
}
)
_fault_accept(
runtime,
fault_injector,
point="evidence_before",
sequence=0,
vote_sequence=13_000_012,
)
with _local_step(runtime, 13_000_022, code="LMS_VERIFY_METRICS_DIVERGED"):
local_evidence = RankVerificationEvidence(
schema_version=1,
rank=runtime.rank,
sequence=0,
verification_identity_digest=verification_identity,
pristine_state_digest=baseline.summary.pristine_state_digest,
post_state_digest=post_state_digest,
tokenization_digest=baseline.summary.tokenization_digest,
post_logit_digest=post.logit_digest,
metrics=metrics,
quality_accepted=accepted,
)
rank_evidence = _gather_rank_evidence(runtime, local_evidence)
_fault_accept(
runtime,
fault_injector,
point="evidence_after",
sequence=0,
vote_sequence=13_000_013,
)
with _local_step(runtime, 13_000_023, code="LMS_VERIFY_METRICS_DIVERGED"):
evidence_set_digest = contract_digest(tuple(item.digest for item in rank_evidence))
_fault_accept(
runtime,
fault_injector,
point="decision_before",
sequence=0,
vote_sequence=13_000_014,
)
def expected_decision() -> dict[str, object]:
return {
"schema_version": 1, "sequence": 0,
"verification_identity_digest": verification_identity,
"rank_evidence_set_digest": evidence_set_digest,
"metrics_digest": metrics.digest,
"action": "accept" if accepted else "abort",
}
decision = _broadcast_record(
runtime, expected_decision if runtime.rank == 0 else None, sequence=13_000_024,
)
with _local_step(runtime, 13_000_026, code="LMS_VERIFY_METRICS_DIVERGED"):
decision_valid = decision == expected_decision()
_collective_accept(
runtime,
13_000_900,
decision_valid,
code="LMS_VERIFY_METRICS_DIVERGED",
message="VERIFY coordinator decision disagrees with rank evidence",
)
_fault_accept(
runtime,
fault_injector,
point="decision_after",
sequence=0,
vote_sequence=13_000_015,
)
if not accepted:
raise RuntimeContractError(
"LMS_VERIFY_QUALITY_FAILED", "collective verification quality gate failed"
)
with _local_step(runtime, 13_000_027, code="LMS_VERIFY_METRICS_DIVERGED"):
summary = VerificationSummary(
schema_version=1,
run_id=runtime.run_id,
excise_digest=excise_result.digest,
baseline_digest=baseline.digest,
config_digest=config.digest,
corpus_digest=baseline.summary.corpus_digest,
pristine_state_digest=baseline.summary.pristine_state_digest,
post_state_digest=post_state_digest,
rank_evidence_set_digest=evidence_set_digest,
metrics=metrics,
quality_accepted=True,
)
summary_digest = summary.digest
try:
_record_consensus(runtime, summary, code="LMS_VERIFY_METRICS_DIVERGED")
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_METRICS_DIVERGED", "rank VERIFY summaries disagree"
) from None
_check_continue(
runtime, config, started=started, sequence=13_000_028,
cancellation_requested=cancellation_requested,
)
cleanup_started = True
local_cleanup = True
try:
_fault(fault_injector, "cleanup_before", -1)
transaction.journal.cleanup()
_fault(fault_injector, "cleanup_after", -1)
except BaseException:
local_cleanup = False
try:
_collective_accept(
runtime,
13_000_901,
local_cleanup,
code="LMS_VERIFY_STATE_UNCERTAIN",
message="verification journal cleanup did not finish on every rank",
)
except BaseException:
transaction.state = ExciseTransactionState.QUARANTINED
raise RuntimeContractError("LMS_VERIFY_STATE_UNCERTAIN", "VERIFY journal cleanup is uncertain") from None
# Cleanup can itself consume the remaining stage bound. Once journals
# have been deleted, interruption means quarantine, never clean rollback.
_check_continue(
runtime, config, started=started, sequence=13_000_029,
cancellation_requested=cancellation_requested,
)
try:
verified_messages = _emit_verified(
runtime,
identity_digest=summary_digest,
fault_injector=fault_injector,
)
except BaseException:
transaction.state = ExciseTransactionState.QUARANTINED
raise RuntimeContractError("LMS_VERIFY_STATE_UNCERTAIN", "VERIFY lifecycle is uncertain") from None
_check_continue(
runtime, config, started=started, sequence=13_000_031,
cancellation_requested=cancellation_requested,
)
with _local_step(runtime, 13_000_904, code="LMS_VERIFY_STATE_UNCERTAIN"):
result = VerificationResult(summary, rank_evidence, verified_messages, excise_result)
transaction.state = ExciseTransactionState.VERIFIED
return result
except BaseException as error:
if transaction is not None and not rollback_allowed:
transaction.state = ExciseTransactionState.QUARANTINED
if (
rollback_allowed
and transaction is not None
and transaction.state is ExciseTransactionState.COMMITTED
and not cleanup_started
):
try:
with _local_step(runtime, 13_000_906, code="LMS_VERIFY_STATE_UNCERTAIN"):
# Release the post-edit snapshot before loading the pristine
# journal. The baseline remains live for restoration proof.
post = None
def prove_restoration() -> None:
roots, state_digest = _logical_state_roots(runtime, sequence_base=16_000_100)
with _local_step(runtime, 13_000_905, code="LMS_VERIFY_STATE_UNCERTAIN"):
if (
roots != baseline.logical_roots
or state_digest != baseline.summary.pristine_state_digest
):
raise ContractError("whole-model restoration is not pristine")
_record_consensus(
runtime, {"pristine_state_digest": state_digest},
code="LMS_VERIFY_STATE_UNCERTAIN",
)
rollback_distributed_excise(excise_result, prove_restoration=prove_restoration)
except BaseException:
transaction.state = ExciseTransactionState.QUARANTINED
raise RuntimeContractError(
"LMS_VERIFY_STATE_UNCERTAIN",
"VERIFY rollback did not reach certain global state",
) from None
if transaction is not None and cleanup_started:
transaction.state = ExciseTransactionState.QUARANTINED
if transaction is not None and transaction.state is ExciseTransactionState.QUARANTINED:
raise RuntimeContractError("LMS_VERIFY_STATE_UNCERTAIN", "VERIFY transaction is quarantined") from None
raise _translate_error(error) from None
def run_live_distributed_verified_excise(
session: LiveModelSession,
distilled: ProbeDistillResult,
excise_config: ExciseConfig,
verification_config: VerificationConfig,
*,
samples: Sequence[VerificationSample] | None = None,
refusal_token_ids: Sequence[int] | None = None,
tokenizer: object | None = None,
cancellation_requested: Callable[[], bool] = lambda: False,
) -> VerificationResult:
"""Run pristine baseline, EXCISE, and VERIFY inside one exact live session."""
# The live-session control group is the trusted entry capability. A missing
# runtime/group is a rank-loss boundary, never permission to return success.
with _local_step(cast(SurgeryRuntime, session), 16_000_002, code="LMS_VERIFY_CONFIG_INVALID"):
runtime = runtime_from_live_session(session, distilled)
excise_config_digest = excise_config.digest
corpus = None
role_ok = bool(
(runtime.rank == 0 and samples is not None and refusal_token_ids is not None)
or (
runtime.rank != 0
and samples is None
and refusal_token_ids is None
and tokenizer is None
)
)
_collective_accept(
runtime,
16_000_000,
role_ok,
code="LMS_VERIFY_INPUT_INVALID",
message="verification inputs must be owned only by rank zero",
)
corpus_ok = True
if runtime.rank == 0:
try:
assert samples is not None and refusal_token_ids is not None
selected_tokenizer = tokenizer or load_exact_probe_tokenizer(session)
corpus = prepare_verification_corpus(
selected_tokenizer,
samples,
refusal_token_ids=refusal_token_ids,
source_tokenizer_digest=session.preflight.source_identity.tokenizer_digest,
config=verification_config,
)
except BaseException:
corpus_ok = False
_collective_accept(
runtime,
16_000_001,
corpus_ok,
code="LMS_VERIFY_INPUT_INVALID",
message="coordinator verification corpus preparation failed",
)
baseline = prepare_distributed_verification_baseline(
runtime,
verification_config,
expected_excise_config_digest=excise_config_digest,
coordinator_corpus=corpus,
cancellation_requested=cancellation_requested,
)
with _local_step(runtime, 16_000_003, code="LMS_EXCISE_RESOURCE_DENIED"):
baseline_bytes, verify_peak, _ = _verification_memory_bytes(
baseline.inputs.plan, verification_config,
output_vocab_size=baseline.snapshot.next_log_probs.shape[1],
)
# Both stages use this runtime's single available-memory observation.
# Reserve overlapping lifetimes before EXCISE can create or commit a journal.
combined_host_bytes = (
baseline_bytes + excise_host_memory_bytes(runtime, distilled, excise_config)
+ verify_peak
+ max(excise_config.safety_margin_bytes, verification_config.safety_margin_bytes)
)
if combined_host_bytes > runtime.available_host_bytes:
raise ContractError("combined EXCISE/VERIFY host working set was not admitted")
excise_result = run_distributed_excise(
runtime,
distilled,
excise_config,
cancellation_requested=cancellation_requested,
)
return run_distributed_verify(
excise_result,
baseline,
verification_config,
cancellation_requested=cancellation_requested,
)
def revalidate_distributed_verified_state(
result: VerificationResult,
) -> tuple[dict[str, str], str]:
"""Recheck a live VERIFIED capability immediately before distributed export."""
# As for VERIFY entry, a missing runtime/control group is the process-fault
# boundary. Once available, every capability check is voted symmetrically.
runtime = result.excise_result.transaction.runtime
try:
with _local_step(runtime, 16_050_000, code="LMS_VERIFY_STATE_DIVERGED"):
if not isinstance(result, VerificationResult):
raise ContractError("VERIFIED capability has the wrong type")
excise_result = result.excise_result
transaction = excise_result.transaction
if (
transaction.state is not ExciseTransactionState.VERIFIED
or transaction.journal.active
or transaction.journal.root.exists()
or result.summary.run_id != runtime.run_id
or result.summary.excise_digest != excise_result.digest
or not result.summary.quality_accepted
or len(transaction.operations) != len(transaction.operation_summaries)
or not transaction.operation_summaries
):
raise ContractError("VERIFIED capability identity changed")
if excise_result.summary.postcondition_set_digest != _postcondition_set_digest(
transaction.operation_summaries
):
raise ContractError("VERIFIED transaction proof changed")
summary = result.summary
_record_consensus(runtime, summary, code="LMS_VERIFY_STATE_DIVERGED")
roots, state_digest = _logical_state_roots(runtime, sequence_base=16_050_100)
with _local_step(runtime, 16_050_001, code="LMS_VERIFY_STATE_DIVERGED"):
if state_digest != summary.post_state_digest:
raise ContractError("VERIFIED model state changed after verification")
return roots, state_digest
except RuntimeContractError:
raise
except BaseException:
raise RuntimeContractError(
"LMS_VERIFY_STATE_DIVERGED",
"VERIFIED model state changed before publication",
) from None
def _translate_error(error: BaseException) -> RuntimeContractError:
if isinstance(error, RuntimeContractError) and error.code in VERIFY_ERROR_CODES:
return error
if isinstance(error, TimeoutError):
return RuntimeContractError("LMS_VERIFY_TIMEOUT", "distributed VERIFY failed")
return RuntimeContractError("LMS_VERIFY_FAILED", "distributed VERIFY failed")
__all__ = [
"VERIFY_ERROR_CODES",
"VerificationBaseline",
"VerificationResult",
"prepare_distributed_verification_baseline",
"revalidate_distributed_verified_state",
"run_distributed_verify",
"run_live_distributed_verified_excise",
]