Files
OBLITERATUS/tests/test_distributed_verification_gloo.py
Joseph MaglyandClaude Opus 5.5 cdde845a99 Restore original collective limits; size reapers for unchecked intervals
Review r2: "stage budgets + one stalled collective" only holds where a
cooperative deadline is checked. PROBE's DISTILL tail, surgery's
post-EXCISE restoration and VERIFY's rollback run unchecked.

Restore the pre-PR process-group timeouts (VERIFY 4 s; PROBE, surgery and
model 5 s). The observed flake was the 5 s EXCISE stage budget, and the
original collective limits held under the same load. Each reaper is now
checked stage budgets + (collectives outside stage clocks + each unchecked
interval + each stage's longest overshoot interval) x collective timeout +
one stalled collective + exit. Counts are named constants with production
source pointers: model 45 s, PROBE 520 s, surgery 695 s, VERIFY 914 s,
VERIFY rank-exit setup 638 s. The short oracles are unchanged from r2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:21:19 -04:00

1097 lines
44 KiB
Python

"""Two-rank fail-closed VERIFY, rollback, and transition-fault oracle."""
from __future__ import annotations
import hashlib
import json
import multiprocessing as mp
import os
import time
from dataclasses import replace
from datetime import timedelta
from pathlib import Path
from queue import Empty
from types import SimpleNamespace
import pytest
import torch
import torch.distributed as dist
import torch.nn as nn
import obliteratus.distributed.verification as verification_module
from obliteratus.distributed.contracts import RuntimeContractError, canonical_record
from obliteratus.distributed.model_manifest import (
ManifestPlacement,
ParameterManifest,
ParameterManifestEntry,
ParameterShard,
RuntimeStorageKind,
SurgeryRole,
)
from obliteratus.distributed.probe_contracts import (
LayerProbeArtifact,
ProbeDistillResult,
ProbeDistillSummary,
tensor_digest,
)
from obliteratus.distributed.surgery import (
ExciseTransactionState,
SurgeryRuntime,
excise_host_memory_bytes,
run_distributed_excise,
)
from obliteratus.distributed.surgery_contracts import ExciseConfig
from obliteratus.distributed.verification import (
prepare_distributed_verification_baseline,
run_distributed_verify,
)
from obliteratus.distributed.verification_contracts import (
RankVerificationEvidence,
VerificationConfig,
VerificationKind,
VerificationSample,
prepare_verification_corpus,
)
pytestmark = pytest.mark.gloo
WORLD_SIZE = 2
SITE = "model.layers.0"
NAME = "model.layers.0.replicated_proj.weight"
UNEDITED_NAME = "unedited"
# Budget policy: keep the original short collective failure detectors; the
# observed load flake was the EXCISE stage budget, so stage budgets get headroom.
# Reaper = checked stage budgets + (unchecked-interval collectives + longest
# collective interval between cooperative checks per stage) * group timeout
# + one stalled collective + exit margin. Collectives can pace just below their
# timeout: this is a hang-guard envelope, not an expected duration. Explicit
# timeout/rank-loss/hang oracles keep their own short limits.
# Startup is inside the ordinary verification reaper. Rank loss has a separate
# short window starting at verify_before (after setup) and an in-rank latency bound.
_EXCISE_STAGE_TIMEOUT_SECONDS = 60.0
_VERIFY_STAGE_TIMEOUT_SECONDS = 60.0
_VERIFY_TIMEOUT_SECONDS = 2.0
_COLLECTIVE_TIMEOUT_SECONDS = 4
_RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS = 4
# Failing transition vote + rollback-entry vote, plus one short interval of slack.
_RANK_EXIT_FAILURE_TIMEOUT_SECONDS = 3 * _RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS
_RANK_EXIT_WORKER_TIMEOUT_SECONDS = 18.0
_COLD_START_TIMEOUT_SECONDS = 60.0
_WORKER_EXIT_TIMEOUT_SECONDS = 30.0
# Baseline and VERIFY each spend a VERIFY-stage budget; EXCISE spends its own.
_STAGE_BUDGET_SECONDS = 2 * _VERIFY_STAGE_TIMEOUT_SECONDS + _EXCISE_STAGE_TIMEOUT_SECONDS
# verification.py:run_live_distributed_verified_excise / run_distributed_verify.
_OUTSIDE_STAGE_COLLECTIVES = 5 # live wrapper 3, combined memory 1, VERIFY entry 1
# verification.py:prepare_distributed_verification_baseline — up to 3 parameters.
_BASELINE_OVERSHOOT_COLLECTIVES = 3 + 20 + (4 + 3 * 3) + 2 # 38
# surgery.py:run_distributed_excise / _project_detached — one replicated direction.
_EXCISE_OVERSHOOT_COLLECTIVES = 29 + 3 + 1 + 2 + 11 # 46
# verification.py:run_distributed_verify / _evaluate — final batch to next check.
_VERIFY_OVERSHOOT_COLLECTIVES = 2 + 15 + 2 + 19 # 38; includes internal data wait
# verification.py:run_distributed_verify / surgery.py:_restore_transaction.
_ROLLBACK_COLLECTIVES = 1 + 17 + (4 + 3 * 3) + 2 # 33, including whole-model proof
# 60 cold start + 180 stages + (5 outside + 38/46/38 overshoot + 33 unchecked
# rollback + 1 stalled) * 4 + 30 exit = 914.
_WORKER_TIMEOUT_SECONDS = (
_COLD_START_TIMEOUT_SECONDS
+ _STAGE_BUDGET_SECONDS
+ (
_OUTSIDE_STAGE_COLLECTIVES
+ _BASELINE_OVERSHOOT_COLLECTIVES
+ _EXCISE_OVERSHOOT_COLLECTIVES
+ _VERIFY_OVERSHOOT_COLLECTIVES
+ _ROLLBACK_COLLECTIVES
+ 1
) * _COLLECTIVE_TIMEOUT_SECONDS
+ _WORKER_EXIT_TIMEOUT_SECONDS
)
# Before the rank-loss readiness signal, allow ordinary setup (cold start and the
# baseline/EXCISE/VERIFY-prefix budgets at the short group limit). None of it
# enters the measured failure latency; restoration is after the peer-exit signal.
# verification.py:run_distributed_verify — committed-entry vote before the clock.
_RANK_EXIT_SETUP_OUTSIDE_STAGE_COLLECTIVES = 1
# verification.py:run_distributed_verify — stage entry through first check.
_RANK_EXIT_SETUP_PREFIX_COLLECTIVES = 4 + 2 # 6, before verify_before signals
# 60 + 180 + (1 outside + 38 baseline + 46 EXCISE + 6 VERIFY prefix + 1 stalled)
# * 4 + 30 = 638 setup; the separate 18-second failure reaper stays unchanged.
_RANK_EXIT_SETUP_TIMEOUT_SECONDS = (
_COLD_START_TIMEOUT_SECONDS
+ _STAGE_BUDGET_SECONDS
+ (
_RANK_EXIT_SETUP_OUTSIDE_STAGE_COLLECTIVES
+ _BASELINE_OVERSHOOT_COLLECTIVES
+ _EXCISE_OVERSHOOT_COLLECTIVES
+ _RANK_EXIT_SETUP_PREFIX_COLLECTIVES
+ 1
) * _RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS
+ _WORKER_EXIT_TIMEOUT_SECONDS
)
class _Tokenizer:
vocab_size = 4
pad_token_id = 0
bos_token_id = 1
eos_token_id = 2
unk_token_id = 3
padding_side = "right"
truncation_side = "right"
chat_template = None
def encode(self, text: str, *, add_special_tokens: bool) -> list[int]:
rows = {
"harm sample": [1, 2],
"coherence sample": [2, 3, 1],
"capability sample": [3, 1],
}
return rows[text]
class _PaddedTokenizer(_Tokenizer):
vocab_size = 9
class _TinyCausalModel(nn.Module):
def __init__(self) -> None:
super().__init__()
self.model = nn.Module()
self.model.layers = nn.ModuleList([nn.Module()])
self.model.layers[0].replicated_proj = nn.Linear(4, 4, bias=False, dtype=torch.float64)
self.unedited = nn.Parameter(torch.ones(4, 4, dtype=torch.float64))
with torch.no_grad():
self.model.layers[0].replicated_proj.weight.copy_(
torch.tensor(
(
(1.0, 0.5, -0.5, 0.25),
(-0.5, 1.5, 0.25, -0.75),
(0.75, -0.25, 1.25, 0.5),
(0.5, 0.75, -1.0, 1.0),
),
dtype=torch.float64,
)
)
self.nonfinite_output = False
self.output_float32 = False
def forward(self, *, input_ids, attention_mask, use_cache):
del attention_mask, use_cache
hidden = torch.nn.functional.one_hot(input_ids, num_classes=4).to(torch.float64)
logits = self.model.layers[0].replicated_proj(hidden)
if self.nonfinite_output:
logits = logits.clone()
logits[0, 0, 0] = float("nan")
if self.output_float32:
logits = logits.float()
return SimpleNamespace(logits=logits)
class _PaddedCausalModel(_TinyCausalModel):
def __init__(self, *, sharded_head: bool = False) -> None:
super().__init__()
self.config = SimpleNamespace(vocab_size=32)
self.output_vocab_size = 32
self.sync_config_to_output = False
self.padded_refusal = False
if sharded_head:
self.lm_head = nn.Linear(4, 16, bias=False, dtype=torch.float64)
with torch.no_grad():
self.lm_head.weight.zero_()
# The manifest, rather than stale config or the local weight,
# supplies the padded global extent for this head.
self.config.vocab_size = 9
def forward(self, *, input_ids, attention_mask, use_cache):
output = super().forward(
input_ids=input_ids, attention_mask=attention_mask, use_cache=use_cache,
)
output.logits = torch.nn.functional.pad(output.logits, (0, self.output_vocab_size - 4))
if self.sync_config_to_output:
self.config.vocab_size = self.output_vocab_size
if self.padded_refusal:
output.logits[..., 3] = 5.0
output.logits[..., 31] = 6.0
return output
class _CollectiveCausalModel(_TinyCausalModel):
def __init__(self, data_group) -> None:
super().__init__()
self.data_group = data_group
self.fail_before_collective = False
def forward(self, *, input_ids, attention_mask, use_cache):
if self.fail_before_collective:
raise RuntimeError("private failure before internal TP collective")
value = torch.ones(1)
dist.all_reduce(value, group=self.data_group)
return super().forward(
input_ids=input_ids, attention_mask=attention_mask, use_cache=use_cache,
)
def _manifest(model: nn.Module, rank: int) -> ParameterManifest:
parameter = dict(model.named_parameters())[NAME]
digest = tensor_digest(parameter)
manifest = ParameterManifest(
1,
"tiny-llama-tp2",
rank,
2,
(
ParameterManifestEntry(
name=NAME,
global_shape=(4, 4),
local_shape=(4, 4),
dtype="float64",
placement=ManifestPlacement.REPLICATE,
shard_dim=None,
shard_start=0,
shard_end=0,
owner_ranks=(0, 1),
tie_id=None,
local_value_digest=digest,
replicated_value_digest=digest,
hook_site=SITE,
surgery_role=SurgeryRole.ATTENTION_INPUT,
runtime_storage_kind=RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
rank_shards=(
ParameterShard(0, 0, 0, (4, 4)),
ParameterShard(1, 0, 0, (4, 4)),
),
),
),
(SITE,),
)
immutable_digest = tensor_digest(dict(model.named_parameters())[UNEDITED_NAME])
immutable = replace(
manifest.entries[0], name=UNEDITED_NAME, hook_site=None,
surgery_role=SurgeryRole.IMMUTABLE, local_value_digest=immutable_digest,
replicated_value_digest=immutable_digest,
)
entries = (*manifest.entries, immutable)
if hasattr(model, "lm_head"):
head_digest = tensor_digest(model.lm_head.weight)
head = replace(
immutable, name="lm_head.weight", global_shape=(32, 4), local_shape=(16, 4),
placement=ManifestPlacement.SHARD, shard_dim=0,
shard_start=rank * 16, shard_end=(rank + 1) * 16,
local_value_digest=head_digest, replicated_value_digest=None,
rank_shards=(
ParameterShard(0, 0, 16, (16, 4)),
ParameterShard(1, 16, 32, (16, 4)),
),
)
entries = (*entries, head)
return replace(manifest, entries=entries)
def _distilled(run_id: str) -> ProbeDistillResult:
direction = torch.tensor(((0.0, 1.0, 0.0, 0.0),), dtype=torch.float32)
artifact = LayerProbeArtifact(
site=SITE,
layer_index=0,
activation_digest="a" * 64,
max_abs_error=0.0,
max_rel_error=0.0,
score=1.0,
harmful_mean=torch.ones(4, dtype=torch.float32),
harmless_mean=torch.zeros(4, dtype=torch.float32),
directions=direction,
)
summary = ProbeDistillSummary(
schema_version=1,
run_id=run_id,
loaded_identity_digest="b" * 64,
config_digest="c" * 64,
corpus_digest="d" * 64,
sample_ids_digest="e" * 64,
tokenizer_contract_digest="f" * 64,
tokenization_digest="1" * 64,
harmful_count=1,
harmless_count=1,
input_width=2,
activation_atol_hex=(0.0).hex(),
activation_rtol_hex=(0.0).hex(),
selected_sites=(SITE,),
layers=(artifact.summary_record(),),
)
return ProbeDistillResult(summary, (artifact,), (), ())
def _excise_config() -> ExciseConfig:
return ExciseConfig(
roles=(SurgeryRole.ATTENTION_INPUT,),
max_journal_bytes=1024 * 1024,
max_update_bytes=1024 * 1024,
safety_margin_bytes=0,
stage_timeout_seconds=_EXCISE_STAGE_TIMEOUT_SECONDS,
)
def _verify_config(mode: str) -> VerificationConfig:
config = VerificationConfig(
max_samples=3,
max_tokens=4,
batch_size=2,
max_baseline_bytes=1024 * 1024,
safety_margin_bytes=0,
rank_logit_atol=0.0,
rank_logit_rtol=0.0,
max_refusal_rate=1.0,
min_coherence_retention=0.0,
max_forward_kl=100.0,
min_capability_retention=0.0,
stage_timeout_seconds=(
_VERIFY_TIMEOUT_SECONDS if mode == "timeout" else _VERIFY_STAGE_TIMEOUT_SECONDS
),
add_special_tokens=False,
)
if mode == "quality_failure":
return replace(config, max_forward_kl=0.0)
if mode == "padded_refusal":
return replace(config, max_refusal_rate=0.0)
if mode == "config_drift" and os.environ.get("RANK") == "1":
return replace(config, max_forward_kl=99.0)
return config
def _samples() -> tuple[VerificationSample, ...]:
return (
VerificationSample("harm-0", VerificationKind.HARMFUL, "harm sample"),
VerificationSample("coh-0", VerificationKind.COHERENCE, "coherence sample"),
VerificationSample(
"cap-0", VerificationKind.CAPABILITY, "capability sample", target_token_id=2
),
)
def _model_digest(model: nn.Module) -> str:
digest = hashlib.sha256()
for name, parameter in model.named_parameters():
digest.update(name.encode())
digest.update(bytes.fromhex(tensor_digest(parameter)))
return digest.hexdigest()
def _fault(mode: str, rank: int):
point = mode.removeprefix("fault_")
def inject(observed: str, sequence: int) -> None:
del sequence
selected_rank = 0 if point == "decision_before" else 1
if mode.startswith("fault_") and rank == selected_rank and observed == point:
raise RuntimeError("private injected failure must never escape")
if mode == "rank_exit" and rank == 1 and observed == "verify_before":
os._exit(19)
return inject
# Keep the control group alive while the data group times out, so the ranks
# can vote FORWARD_FAILED and prove rollback through control collectives.
_INTERNAL_CONTROL_TIMEOUT_SECONDS = 6
_INTERNAL_DATA_TIMEOUT_SECONDS = 2
def _set_head_entry(runtime: SurgeryRuntime, head: ParameterManifestEntry) -> None:
object.__setattr__(runtime.manifest, "entries", tuple(
head if entry.name == "lm_head.weight" else entry for entry in runtime.manifest.entries
))
def _configure_vocab_fault(runtime: SurgeryRuntime, mode: str) -> None:
model, rank = runtime.model, runtime.rank
if mode.startswith("sharded_"):
model.output_vocab_size = 16 # Identical local-width logits on both ranks.
elif mode.startswith("rank_vocab_"):
model.output_vocab_size = 32 if rank == 0 else 16
# Each rank passes its local metadata check during baseline. The
# shape-consensus guard must reject before any logit data collective.
model.sync_config_to_output = True
if mode.startswith(("sharded_fallback_", "sharded_extent_")):
model.config.vocab_size = 16
head = next(entry for entry in _manifest(model, rank).entries if entry.name == "lm_head.weight")
head = replace(head, global_shape=None if mode.startswith("sharded_fallback_") else (16, 4))
if mode.endswith("_baseline"):
_set_head_entry(runtime, head)
else:
forward = model.forward
def corrupt_head_layout(**kwargs):
# Lose/change layout metadata during the post-edit forward,
# after the pristine baseline has passed revalidation.
_set_head_entry(runtime, head)
return forward(**kwargs)
model.forward = corrupt_head_layout
def _worker(
rank: int, rendezvous: str, root: str, mode: str, queue: mp.Queue, rank_exit_ready,
) -> None:
model = None
excise_result = None
journal_root = None
before = None
verify_started = None
rank_exit_started = None
input_allocation_attempted = False
baseline_vocab = None
baseline_output_width = None
baseline_valid = False
observed_refusal_rate = None
try:
os.environ["RANK"] = str(rank)
dist.init_process_group(
"gloo",
init_method=f"file://{rendezvous}",
rank=rank,
world_size=2,
timeout=timedelta(
seconds=(
_INTERNAL_CONTROL_TIMEOUT_SECONDS
if mode == "internal_forward_failure"
else _RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS
if mode == "rank_exit"
else _COLLECTIVE_TIMEOUT_SECONDS
)
),
)
data_group = dist.group.WORLD
if mode == "internal_forward_failure":
data_group = dist.new_group(
backend="gloo", timeout=timedelta(seconds=_INTERNAL_DATA_TIMEOUT_SECONDS)
)
model = _CollectiveCausalModel(data_group).eval()
elif mode in {"padded_vocab", "padded_refusal"} or mode.startswith(
("sharded_", "rank_vocab_")
):
model = _PaddedCausalModel(sharded_head=mode.startswith("sharded_")).eval()
else:
model = _TinyCausalModel().eval()
tokenizer = _PaddedTokenizer() if isinstance(model, _PaddedCausalModel) else _Tokenizer()
run_id = hashlib.sha256(mode.encode()).hexdigest()[:32]
distilled = _distilled(run_id)
runtime = SurgeryRuntime(
run_id=run_id,
distilled_digest=distilled.digest,
rank=rank,
world_size=2,
model=model,
manifest=_manifest(model, rank),
control_group=dist.group.WORLD,
data_group=data_group,
tensor_device=torch.device("cpu"),
available_host_bytes=1 if mode == "resource_denied" and rank == 1 else 1 << 30,
available_staging_bytes=1 << 30,
available_device_bytes=1 << 30,
journal_parent=Path(root),
)
if mode in {"sharded_fallback_verify", "sharded_extent_verify"}:
# Start with a valid replicated 16-row head, so a 16-wide baseline
# does not mask the post-forward bug via the saved-width guard.
model.output_vocab_size = model.config.vocab_size = 16
head = next(entry for entry in runtime.manifest.entries if entry.name == "lm_head.weight")
_set_head_entry(runtime, replace(
head, global_shape=(16, 4), placement=ManifestPlacement.REPLICATE,
shard_dim=None, shard_start=0, shard_end=0,
replicated_value_digest=head.local_value_digest,
rank_shards=(ParameterShard(0, 0, 0, (16, 4)), ParameterShard(1, 0, 0, (16, 4))),
))
if mode.endswith("_baseline"):
_configure_vocab_fault(runtime, mode)
config = _verify_config(mode)
before = _model_digest(model)
if mode == "live_combined_denied":
# Each standalone stage fits this budget; overlapping storage does
# not. Only rank one rejects, so rank zero must receive its vote.
plan = SimpleNamespace(
sample_count=3, input_width=3, vocab_size=4,
sequence_position_count=4, refusal_token_count=1,
)
snapshot, verify_peak, _ = verification_module._verification_memory_bytes(plan, config)
excise_peak = excise_host_memory_bytes(runtime, distilled, _excise_config())
budget = verify_peak + 1
assert max(verify_peak, excise_peak) <= budget < snapshot + excise_peak + verify_peak
if rank == 1:
runtime = replace(runtime, available_host_bytes=budget)
def unexpected_excise(*_args, **_kwargs):
raise AssertionError("combined denial must precede EXCISE")
verification_module.run_distributed_excise = unexpected_excise
if mode in {"live_success", "live_bad_role", "live_bad_corpus", "live_combined_denied"}:
session = SimpleNamespace(
control_group=runtime.control_group,
preflight=SimpleNamespace(
source_identity=SimpleNamespace(tokenizer_digest="2" * 64)
)
)
verification_module.runtime_from_live_session = lambda *_args: runtime
result = verification_module.run_live_distributed_verified_excise(
session,
distilled,
_excise_config(),
config,
samples=_samples() if rank == 0 or mode == "live_bad_role" else None,
refusal_token_ids=(3,) if rank == 0 or mode == "live_bad_role" else None,
tokenizer=(
object()
if rank == 0 and mode == "live_bad_corpus"
else _Tokenizer()
if rank == 0 or mode == "live_bad_role"
else None
),
)
excise_result = result.excise_result
journal_root = excise_result.transaction.journal.root
queue.put(
(
rank,
"ok",
{
"digest": result.digest,
"run_id": run_id,
"state": excise_result.transaction.state.value,
"journal_removed": not journal_root.exists(),
"changed": _model_digest(model) != before,
"messages": tuple(item.stage.value for item in result.verified_messages),
"rank_evidence": tuple(item.rank for item in result.rank_evidence),
"repr": repr(result),
},
)
)
return
corpus = (
prepare_verification_corpus(
tokenizer,
_samples(),
refusal_token_ids=(3,),
source_tokenizer_digest="2" * 64,
config=config,
)
if (rank == 0 and mode != "missing_corpus")
or (rank == 1 and mode == "noncoordinator_corpus")
else None
)
if mode == "resource_denied":
def forbidden_input(*_args, **_kwargs):
nonlocal input_allocation_attempted
input_allocation_attempted = True
raise AssertionError("input allocation preceded admission")
verification_module._broadcast_tensor = forbidden_input
baseline = prepare_distributed_verification_baseline(
runtime,
config,
expected_excise_config_digest=(
"z" * 64 if mode == "invalid_excise_digest" else _excise_config().digest
),
coordinator_corpus=corpus,
fault_injector=_fault(mode, rank),
)
baseline_vocab = baseline.summary.vocab_size
baseline_output_width = baseline.snapshot.next_log_probs.shape[1]
baseline_valid = verification_module._baseline_capability_valid(baseline, runtime, config)
if mode == "stale_baseline":
baseline = replace(
baseline,
summary=replace(baseline.summary, run_id="f" * 32),
)
excise_result = run_distributed_excise(runtime, distilled, _excise_config())
journal_root = excise_result.transaction.journal.root
if mode == "baseline_payload_tamper" and rank == 1:
baseline.snapshot.next_log_probs[0, 0] += 1.0
if mode == "baseline_input_tamper" and rank == 1:
baseline.inputs.input_ids[0, 0] = (int(baseline.inputs.input_ids[0, 0].item()) + 1) % 4
if mode == "transaction_proof_tamper" and rank == 1:
summaries = excise_result.transaction.operation_summaries
excise_result.transaction.operation_summaries = (
replace(summaries[0], logical_post_digest="f" * 64),
*summaries[1:],
)
if mode == "nonfinite":
model.nonfinite_output = True
if mode == "logit_contract_divergence" and rank == 1:
model.output_float32 = True
if mode.endswith("_verify"):
_configure_vocab_fault(runtime, mode)
if mode == "padded_refusal":
model.padded_refusal = True
original_metrics = verification_module._metrics
def capture_metrics(*args, **kwargs):
nonlocal observed_refusal_rate
metrics = original_metrics(*args, **kwargs)
observed_refusal_rate = float.fromhex(metrics.refusal_rate_hex)
return metrics
verification_module._metrics = capture_metrics
if mode == "state_divergence" and rank == 1:
with torch.no_grad():
dict(model.named_parameters())[NAME][0, 0] += 1.0
if mode == "unedited_divergence" and rank == 1:
with torch.no_grad():
dict(model.named_parameters())[UNEDITED_NAME][0, 0] += 1.0
if mode == "internal_forward_failure" and rank == 1:
model.fail_before_collective = True
if mode.startswith("evidence_"):
original_gather = verification_module.gloo_all_gather_records
def altered_gather(record, *, group=None, capacity=64 * 1024):
payloads = original_gather(record, group=group, capacity=capacity)
if not isinstance(record, RankVerificationEvidence):
return payloads
if mode == "evidence_missing":
return payloads[:1]
if mode == "evidence_duplicate":
return (payloads[0], payloads[0])
decoded = json.loads(payloads[1].decode("ascii"))
if mode == "evidence_stale":
decoded["sequence"] = 1
elif mode == "evidence_divergent":
decoded["post_logit_digest"] = "f" * 64
return (payloads[0], canonical_record(decoded))
verification_module.gloo_all_gather_records = altered_gather
if mode == "state_evidence_missing":
original_gather = verification_module.gloo_all_gather_records
def altered_state_gather(record, *, group=None, capacity=64 * 1024):
payloads = original_gather(record, group=group, capacity=capacity)
if isinstance(record, dict) and set(record) == {
"rank",
"sequence",
"local_digest",
}:
return payloads[:1]
return payloads
verification_module.gloo_all_gather_records = altered_state_gather
timeout_once = [False]
def cancellation_requested() -> bool:
if mode == "cancelled" and rank == 1:
return True
if mode == "timeout" and not timeout_once[0]:
timeout_once[0] = True
time.sleep(2.05)
if mode == "signal" and rank == 1:
raise KeyboardInterrupt
return False
inject_verify_fault = _fault(mode, rank)
def record_verify_fault(point: str, sequence: int) -> None:
nonlocal rank_exit_started
if mode == "rank_exit" and point == "verify_before":
# This hook immediately precedes the failing transition vote;
# baseline, EXCISE and VERIFY preparation are outside the bound.
rank_exit_started = time.monotonic()
rank_exit_ready.set()
inject_verify_fault(point, sequence)
verify_started = time.monotonic()
result = run_distributed_verify(
excise_result,
baseline,
config,
cancellation_requested=cancellation_requested,
fault_injector=record_verify_fault,
)
queue.put(
(
rank,
"ok",
{
"digest": result.digest,
"run_id": run_id,
"state": excise_result.transaction.state.value,
"journal_removed": not journal_root.exists(),
"changed": _model_digest(model) != before,
"messages": tuple(item.stage.value for item in result.verified_messages),
"rank_evidence": tuple(item.rank for item in result.rank_evidence),
"baseline_vocab": baseline_vocab,
"baseline_output_width": baseline_output_width,
"baseline_valid": baseline_valid,
"repr": repr(result),
},
)
)
except BaseException as error:
queue.put(
(
rank,
"error",
{
"code": getattr(error, "code", type(error).__name__),
"input_allocation_attempted": input_allocation_attempted,
"message": str(error),
"refusal_rate": observed_refusal_rate,
"state": (
excise_result.transaction.state.value
if excise_result is not None
else "pristine"
),
"restored": model is not None
and before is not None
and _model_digest(model) == before,
"journal_removed": journal_root is None or not journal_root.exists(),
# Failure latency of the VERIFY stage itself, excluding
# interpreter spawn, imports and rendezvous.
"verify_seconds": (
None if verify_started is None else time.monotonic() - verify_started
),
"rank_exit_seconds": (
None if rank_exit_started is None else time.monotonic() - rank_exit_started
),
},
)
)
finally:
if dist.is_initialized():
try:
dist.destroy_process_group()
except BaseException:
pass
def _run(tmp_path: Path, mode: str, *, timeout: float = _WORKER_TIMEOUT_SECONDS):
context = mp.get_context("spawn")
queue = context.Queue()
rendezvous = tmp_path / f"{mode}.rdzv"
journal = tmp_path / "journal"
journal.mkdir(mode=0o700)
rank_exit_ready = [
context.Event() if mode == "rank_exit" else None for _ in range(WORLD_SIZE)
]
processes = [
context.Process(
target=_worker,
args=(rank, str(rendezvous), str(journal), mode, queue, rank_exit_ready[rank]),
)
for rank in range(2)
]
for process in processes:
process.start()
if mode == "rank_exit":
setup_deadline = time.monotonic() + _RANK_EXIT_SETUP_TIMEOUT_SECONDS
for event in rank_exit_ready:
if not event.wait(max(0.0, setup_deadline - time.monotonic())):
for process in processes:
if process.is_alive():
process.kill()
process.join(3)
pytest.fail("rank_exit workers did not reach the peer-exit boundary")
deadline = time.monotonic() + timeout
for process in processes:
process.join(max(0.0, deadline - time.monotonic()))
alive = [process for process in processes if process.is_alive()]
for process in alive:
process.kill()
process.join(3)
results = []
while True:
try:
results.append(queue.get_nowait())
except Empty:
break
queue.close()
queue.join_thread()
return processes, sorted(results)
def test_combined_host_budget_denial_precedes_mutation_on_both_ranks(tmp_path):
processes, results = _run(tmp_path, "live_combined_denied")
assert not any(process.is_alive() for process in processes)
assert [item[:2] for item in results] == [(0, "error"), (1, "error")]
assert all(
item[2]["code"] == "LMS_EXCISE_RESOURCE_DENIED"
and item[2]["state"] == "pristine"
and item[2]["restored"]
and item[2]["journal_removed"]
for item in results
)
assert not list((tmp_path / "journal").iterdir())
def test_two_rank_verify_accepts_only_after_complete_evidence_and_cleanup(tmp_path):
processes, results = _run(tmp_path, "success")
assert not any(process.is_alive() for process in processes)
assert [item[:2] for item in results] == [(0, "ok"), (1, "ok")]
payloads = [item[2] for item in results]
assert len({item["digest"] for item in payloads}) == 1
assert all(
item["state"] == ExciseTransactionState.VERIFIED.value
and item["journal_removed"]
and item["changed"]
and item["messages"] == ("verified", "verified")
and item["rank_evidence"] == (0, 1)
for item in payloads
)
assert all(
forbidden not in item["repr"].lower()
for item in payloads
for forbidden in ("harm sample", "token_ids", "logits", str(tmp_path).lower())
)
@pytest.mark.parametrize("mode", ["padded_vocab", "sharded_vocab_valid"])
def test_two_rank_padded_vocab_revalidates_and_reaches_verified(tmp_path, mode):
processes, results = _run(tmp_path, mode)
assert all(process.exitcode == 0 for process in processes)
assert [item[:2] for item in results] == [(0, "ok"), (1, "ok")]
assert len({item[2]["digest"] for item in results}) == 1
assert all(
item[2]["baseline_vocab"] == 9
and item[2]["baseline_output_width"] == 32
and item[2]["baseline_valid"]
and item[2]["state"] == ExciseTransactionState.VERIFIED.value
and item[2]["changed"]
and item[2]["journal_removed"]
for item in results
)
assert not list((tmp_path / "journal").iterdir())
@pytest.mark.parametrize("phase", ["baseline", "verify"])
@pytest.mark.parametrize("fault", [
"sharded_vocab", "rank_vocab", "sharded_fallback", "sharded_extent",
])
def test_two_rank_vocab_contract_failure_agrees_and_restores(tmp_path, phase, fault):
# _run imposes a stage-derived wall deadline and kills any hung worker. Both
# workers must exit normally and report the exact same closed failure.
processes, results = _run(tmp_path, f"{fault}_{phase}")
assert all(process.exitcode == 0 for process in processes)
assert [item[:2] for item in results] == [(0, "error"), (1, "error")]
assert [item[2]["code"] for item in results] == ["LMS_VERIFY_FORWARD_CONTRACT"] * 2
expected_state = "pristine" if phase == "baseline" else ExciseTransactionState.ROLLED_BACK.value
assert all(
item[2]["state"] == expected_state
and item[2]["restored"]
and item[2]["journal_removed"]
for item in results
)
assert not list((tmp_path / "journal").iterdir())
def test_two_rank_padded_argmax_detects_refusal_and_rolls_back(tmp_path):
processes, results = _run(tmp_path, "padded_refusal")
assert all(process.exitcode == 0 for process in processes)
assert [item[:2] for item in results] == [(0, "error"), (1, "error")]
assert [item[2]["code"] for item in results] == ["LMS_VERIFY_QUALITY_FAILED"] * 2
assert all(
item[2]["refusal_rate"] == 1.0
and item[2]["state"] == ExciseTransactionState.ROLLED_BACK.value
and item[2]["restored"]
and item[2]["journal_removed"]
for item in results
)
assert not list((tmp_path / "journal").iterdir())
def test_public_live_workflow_owns_rank_zero_inputs_and_reaches_verified(tmp_path):
processes, results = _run(tmp_path, "live_success")
assert not any(process.is_alive() for process in processes)
assert [item[:2] for item in results] == [(0, "ok"), (1, "ok")]
assert len({item[2]["digest"] for item in results}) == 1
assert all(
item[2]["state"] == ExciseTransactionState.VERIFIED.value
and item[2]["journal_removed"]
and item[2]["changed"]
for item in results
)
def test_retry_uses_a_new_process_group_run_identity_and_empty_journal_root(tmp_path):
first_root = tmp_path / "first"
retry_root = tmp_path / "retry"
first_root.mkdir()
retry_root.mkdir()
first_processes, first_results = _run(first_root, "success")
retry_processes, retry_results = _run(retry_root, "success_retry")
assert not any(process.is_alive() for process in (*first_processes, *retry_processes))
assert all(item[1] == "ok" for item in (*first_results, *retry_results))
first_run_ids = {item[2]["run_id"] for item in first_results}
retry_run_ids = {item[2]["run_id"] for item in retry_results}
assert len(first_run_ids) == len(retry_run_ids) == 1
assert first_run_ids.isdisjoint(retry_run_ids)
assert not any((first_root / "journal").iterdir())
assert not any((retry_root / "journal").iterdir())
def test_capability_probability_floor_preserves_an_unchanged_tiny_probability():
floor = 1e-3
unchanged = verification_module._bounded_probability_retention(
torch.tensor(1e-8, dtype=torch.float64),
torch.tensor(1e-8, dtype=torch.float64),
floor,
)
reduced = verification_module._bounded_probability_retention(
torch.tensor(0.1, dtype=torch.float64),
torch.tensor(0.2, dtype=torch.float64),
floor,
)
assert unchanged.item() == 1.0
assert reduced.item() == 0.5
@pytest.mark.parametrize("code", sorted(verification_module.VERIFY_ERROR_CODES))
def test_error_translation_preserves_closed_errors_and_normalizes_unknown_failures(code):
closed = RuntimeContractError(code, "safe")
assert verification_module._translate_error(closed) is closed
assert verification_module._translate_error(TimeoutError()).code == "LMS_VERIFY_TIMEOUT"
assert verification_module._translate_error(ValueError("private")).code == "LMS_VERIFY_FAILED"
def test_baseline_rejects_an_invalid_runtime_before_any_collective():
with pytest.raises(RuntimeContractError, match="verification runtime") as caught:
prepare_distributed_verification_baseline(
object(),
VerificationConfig(),
expected_excise_config_digest="a" * 64,
coordinator_corpus=None,
)
assert caught.value.code == "LMS_VERIFY_CONFIG_INVALID"
@pytest.mark.parametrize(
("mode", "code"),
(
("quality_failure", "LMS_VERIFY_QUALITY_FAILED"),
("cancelled", "LMS_VERIFY_CANCELLED"),
("signal", "LMS_VERIFY_CANCELLED"),
("timeout", "LMS_VERIFY_TIMEOUT"),
("nonfinite", "LMS_VERIFY_NONFINITE"),
("logit_contract_divergence", "LMS_VERIFY_FORWARD_CONTRACT"),
("state_divergence", "LMS_VERIFY_STATE_DIVERGED"),
("fault_verify_before", "LMS_VERIFY_FAILED"),
("fault_verify_after", "LMS_VERIFY_FAILED"),
("fault_evidence_before", "LMS_VERIFY_FAILED"),
("fault_evidence_after", "LMS_VERIFY_FAILED"),
("fault_decision_before", "LMS_VERIFY_FAILED"),
("fault_decision_after", "LMS_VERIFY_FAILED"),
("stale_baseline", "LMS_VERIFY_BASELINE_INVALID"),
("baseline_payload_tamper", "LMS_VERIFY_BASELINE_INVALID"),
("baseline_input_tamper", "LMS_VERIFY_BASELINE_INVALID"),
("transaction_proof_tamper", "LMS_VERIFY_STATE_DIVERGED"),
("evidence_missing", "LMS_VERIFY_METRICS_DIVERGED"),
("evidence_duplicate", "LMS_VERIFY_METRICS_DIVERGED"),
("evidence_stale", "LMS_VERIFY_METRICS_DIVERGED"),
("evidence_divergent", "LMS_VERIFY_METRICS_DIVERGED"),
),
)
def test_live_verification_failures_restore_pristine_state(tmp_path, mode, code):
processes, results = _run(tmp_path, mode)
assert not any(process.is_alive() for process in processes)
assert len(results) == 2
assert all(item[1] == "error" for item in results)
assert {item[2]["code"] for item in results} == {code}
assert all(
item[2]["state"] == ExciseTransactionState.ROLLED_BACK.value
and item[2]["restored"]
and item[2]["journal_removed"]
and "private injected" not in item[2]["message"]
for item in results
)
@pytest.mark.parametrize(
"mode",
(
"fault_cleanup_before",
"fault_cleanup_after",
"fault_lifecycle_before",
"fault_lifecycle_after",
),
)
def test_post_acceptance_cleanup_or_lifecycle_uncertainty_never_returns_a_result(tmp_path, mode):
processes, results = _run(tmp_path, mode)
assert not any(process.is_alive() for process in processes)
assert len(results) == 2
assert all(item[1] == "error" for item in results)
assert all(item[2]["state"] == ExciseTransactionState.QUARANTINED.value for item in results)
assert {item[2]["code"] for item in results} == {"LMS_VERIFY_STATE_UNCERTAIN"}
@pytest.mark.parametrize(
"mode",
(
"fault_baseline_before",
"fault_baseline_after",
"config_drift",
"missing_corpus",
"noncoordinator_corpus",
"resource_denied",
"invalid_excise_digest",
"live_bad_role",
"live_bad_corpus",
),
)
def test_baseline_failure_never_mutates_or_creates_a_journal(tmp_path, mode):
processes, results = _run(tmp_path, mode)
assert not any(process.is_alive() for process in processes)
assert len(results) == 2
assert all(item[1] == "error" and item[2]["state"] == "pristine" for item in results)
assert all(item[2]["restored"] and item[2]["journal_removed"] for item in results)
def test_rank_exit_cannot_return_verified_state(tmp_path):
processes, results = _run(tmp_path, "rank_exit", timeout=_RANK_EXIT_WORKER_TIMEOUT_SECONDS)
assert not any(process.is_alive() for process in processes)
# A reaper-killed survivor must fail, even if it managed to enqueue an error.
assert processes[0].exitcode == 0
assert processes[1].exitcode == 19
assert [item[:2] for item in results] == [(0, "error")]
survivor = results[0][2]
assert 0.0 <= survivor["rank_exit_seconds"] < _RANK_EXIT_FAILURE_TIMEOUT_SECONDS
assert survivor["code"] == "LMS_VERIFY_STATE_UNCERTAIN"
@pytest.mark.parametrize("mode", ["unedited_divergence", "state_evidence_missing"])
def test_whole_model_rollback_proof_failure_keeps_journals_and_quarantines(tmp_path, mode):
processes, results = _run(tmp_path, mode)
assert not any(process.is_alive() for process in processes)
assert [item[:2] for item in results] == [(0, "error"), (1, "error")]
assert all(
item[2]["code"] == "LMS_VERIFY_STATE_UNCERTAIN"
and item[2]["state"] == ExciseTransactionState.QUARANTINED.value
and not item[2]["journal_removed"]
for item in results
)
if mode == "unedited_divergence":
assert not results[1][2]["restored"]
def test_failure_before_internal_tp_collective_fails_closed_within_timeout(tmp_path):
# Bound the VERIFY failure path itself, measured inside each rank. Wall
# time around _run also counts spawn, torch import and rendezvous, which on
# a contended hosted runner alone approached the old 12 s budget (#305).
# The outer timeout only reaps a genuinely hung worker group.
processes, results = _run(tmp_path, "internal_forward_failure")
assert all(process.exitcode == 0 for process in processes)
assert [item[:2] for item in results] == [(0, "error"), (1, "error")]
bound = _INTERNAL_DATA_TIMEOUT_SECONDS + _INTERNAL_CONTROL_TIMEOUT_SECONDS
assert all(0.0 <= item[2]["verify_seconds"] < bound for item in results)
assert [item[2]["code"] for item in results] == ["LMS_VERIFY_FORWARD_FAILED"] * 2
assert all(
item[2]["state"] == ExciseTransactionState.ROLLED_BACK.value
and item[2]["restored"]
and item[2]["journal_removed"]
for item in results
)
assert not list((tmp_path / "journal").iterdir())
def test_one_rank_baseline_memory_denial_precedes_all_input_allocation(tmp_path):
processes, results = _run(tmp_path, "resource_denied")
assert not any(process.is_alive() for process in processes)
assert len(results) == 2
assert all(item[1] == "error" for item in results)
assert {item[2]["code"] for item in results} == {"LMS_VERIFY_CONFIG_INVALID"}
assert all(not item[2]["input_allocation_attempted"] for item in results)
assert all(item[2]["restored"] and item[2]["journal_removed"] for item in results)