mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Add single-process unit tests for validation, contract and failure paths in
obliteratus/distributed/{preflight,export,probe,probe_contracts,surgery}.py
using in-process collective fakes, monkeypatching and tmp directories.
Release-lane missing lines drop from 463 to 3 across the five modules.
Refs #304
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
913 lines
36 KiB
Python
913 lines
36 KiB
Python
"""Single-process unit tests for distributed EXPORT validation and failure paths (#304).
|
|
|
|
These tests replace the TP2 collectives with in-process fakes so each refusal,
|
|
cleanup and translation branch is exercised deterministically on CPU. The
|
|
two-rank thread world reuses the ``mocked_export_world`` fixture.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
import torch.nn as nn
|
|
from safetensors.torch import save_file
|
|
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError,
|
|
RuntimeContractError,
|
|
canonical_record,
|
|
)
|
|
from obliteratus.distributed.export_contracts import (
|
|
ReloadValidationEvidence,
|
|
ReloadValidationRequest,
|
|
RankExportEvidence,
|
|
)
|
|
from obliteratus.distributed.model_manifest import ManifestPlacement, ParameterShard
|
|
from obliteratus.distributed.model_session import LiveModelSession
|
|
from obliteratus.distributed.model_topology import TP2DeploymentLayout
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
from tests.test_distributed_export_contracts import mocked_export_world # noqa: F401
|
|
from tests.test_distributed_export_gloo import (
|
|
_direct_runtime,
|
|
_export_config,
|
|
_verified,
|
|
)
|
|
|
|
DIGEST = "e" * 64
|
|
|
|
|
|
@pytest.fixture
|
|
def local_votes(monkeypatch):
|
|
"""Single-rank stand-in: a unanimous vote is this rank's own acceptance."""
|
|
votes: list[tuple[int, bool]] = []
|
|
|
|
def vote(sequence, accepted, *, group):
|
|
del group
|
|
votes.append((sequence, bool(accepted)))
|
|
return bool(accepted)
|
|
|
|
monkeypatch.setattr(export, "unanimous_vote", vote)
|
|
return votes
|
|
|
|
|
|
def _code(captured) -> str:
|
|
return captured.value.code
|
|
|
|
|
|
def _replicated_assignment(runtime):
|
|
entry = next(item for item in runtime.manifest.entries if item.name == "replicated")
|
|
assignments = export._assignments(replace(runtime.manifest, entries=(entry,)))
|
|
return assignments, entry
|
|
|
|
|
|
def _write_readonly(path: Path, payload: bytes) -> Path:
|
|
path.write_bytes(payload)
|
|
path.chmod(0o444)
|
|
return path
|
|
|
|
|
|
# ---------------------------------------------------------------- capabilities
|
|
|
|
|
|
def test_host_availability_probe_degrades_to_zero_when_sysconf_fails(monkeypatch):
|
|
assert export._current_host_available() > 0
|
|
|
|
def broken(name):
|
|
raise ValueError(name)
|
|
|
|
monkeypatch.setattr(export.os, "sysconf", broken)
|
|
assert export._current_host_available() == 0
|
|
|
|
|
|
def _live_session(tmp_path, *, plan_digest="7" * 64):
|
|
runtime = _direct_runtime(tmp_path)
|
|
surgery_runtime, verified = _verified(
|
|
runtime.model, runtime.manifest, 0, tmp_path, runtime.run_id,
|
|
)
|
|
inventory = SimpleNamespace(
|
|
rank=0, free_host_memory_bytes=1 << 20, free_staging_bytes=1 << 21,
|
|
free_device_memory_bytes=1 << 22,
|
|
)
|
|
preflight = SimpleNamespace(
|
|
identity=SimpleNamespace(run_id=runtime.run_id),
|
|
attestations=(inventory,),
|
|
source_identity=runtime.source_identity,
|
|
topology={"kind": "local"},
|
|
)
|
|
config = SimpleNamespace(
|
|
staging_path=runtime.staging_path, source_path=runtime.source_path,
|
|
placement_plan_digest="7" * 64, digest="5" * 64, code_digest="9" * 64,
|
|
storage_digest="a" * 64, commit_sha="b" * 40, max_source_files=32,
|
|
max_source_bytes=1 << 20, max_source_file_bytes=1 << 19, source_timeout_seconds=10,
|
|
)
|
|
handle = SimpleNamespace(
|
|
model=runtime.model, manifest=runtime.manifest,
|
|
source_identity=runtime.source_identity, manifest_digest=runtime.manifest.digest,
|
|
plan_digest=plan_digest, config=SimpleNamespace(config_digest="6" * 64),
|
|
)
|
|
session = object.__new__(LiveModelSession)
|
|
for name, value in {
|
|
"config": config, "launch": SimpleNamespace(local_rank=0), "preflight": preflight,
|
|
"handle": handle, "control_group": object(),
|
|
}.items():
|
|
object.__setattr__(session, name, value)
|
|
return session, verified, surgery_runtime
|
|
|
|
|
|
def test_live_session_is_narrowed_to_the_minimum_attested_capacity(tmp_path, monkeypatch):
|
|
session, verified, _ = _live_session(tmp_path)
|
|
monkeypatch.setattr(export, "classify_tp2_topology", lambda *args: SimpleNamespace(
|
|
layout=TP2DeploymentLayout.LOCAL_TP2))
|
|
monkeypatch.setattr(export, "_current_host_available", lambda: 1 << 30)
|
|
monkeypatch.setattr(export.shutil, "disk_usage", lambda path: SimpleNamespace(free=1 << 10))
|
|
runtime = export.runtime_from_live_session(session, verified, retained_host_bytes=5)
|
|
assert runtime.rank == 0 and runtime.world_size == 2
|
|
assert runtime.model is session.handle.model
|
|
assert runtime.available_host_bytes == 1 << 20
|
|
assert runtime.available_staging_bytes == 1 << 10 # live disk is the tighter bound
|
|
assert runtime.available_device_bytes == 1 << 22
|
|
assert runtime.retained_host_bytes == 5
|
|
assert runtime.deployment_layout is TP2DeploymentLayout.LOCAL_TP2
|
|
|
|
|
|
def test_live_session_identity_disagreement_is_a_closed_config_error(tmp_path, monkeypatch):
|
|
session, verified, _ = _live_session(tmp_path, plan_digest="c" * 64)
|
|
monkeypatch.setattr(export, "classify_tp2_topology", lambda *args: SimpleNamespace(
|
|
layout=TP2DeploymentLayout.LOCAL_TP2))
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export.runtime_from_live_session(session, verified)
|
|
assert _code(captured) == "LMS_EXPORT_CONFIG_INVALID"
|
|
assert "inconsistent" in str(captured.value)
|
|
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export.runtime_from_live_session(object(), verified)
|
|
assert _code(captured) == "LMS_EXPORT_CONFIG_INVALID"
|
|
|
|
|
|
# ----------------------------------------------------------- collective guards
|
|
|
|
|
|
def test_control_group_identity_failures_are_classified(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
|
|
def unavailable(group):
|
|
raise RuntimeError("group destroyed")
|
|
|
|
monkeypatch.setattr(export.dist, "get_backend", unavailable)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._group_identity(runtime)
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
monkeypatch.setattr(export.dist, "get_backend", lambda group: "nccl")
|
|
monkeypatch.setattr(export.dist, "get_rank", lambda group: 0)
|
|
monkeypatch.setattr(export.dist, "get_world_size", lambda group: 2)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._group_identity(runtime)
|
|
assert _code(captured) == "LMS_EXPORT_CONFIG_INVALID"
|
|
|
|
|
|
def test_collective_accept_maps_vote_failure_and_refusal(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
|
|
def broken(*args, **kwargs):
|
|
raise RuntimeError("peer lost")
|
|
|
|
monkeypatch.setattr(export, "unanimous_vote", broken)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._collective_accept(runtime, 1, True, code="LMS_EXPORT_TIMEOUT", message="m")
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
monkeypatch.setattr(export, "unanimous_vote", lambda *args, **kwargs: False)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._collective_accept(runtime, 1, True, code="LMS_EXPORT_TIMEOUT", message="m")
|
|
assert _code(captured) == "LMS_EXPORT_TIMEOUT"
|
|
|
|
|
|
def test_broadcast_record_rejects_failed_or_non_object_payloads(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
monkeypatch.setattr(export, "gloo_all_gather_records", lambda value, *, group: (
|
|
canonical_record({"source": 0}), canonical_record(value)))
|
|
assert export._broadcast_record(runtime, 0, None) == {"source": 0}
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._broadcast_record(runtime, 1, [1, 2])
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
assert "invalid" in str(captured.value)
|
|
|
|
def broken(value, *, group):
|
|
raise RuntimeError("gather failed")
|
|
|
|
monkeypatch.setattr(export, "gloo_all_gather_records", broken)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._broadcast_record(runtime, 0, {})
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
|
|
def test_raising_cancellation_probe_is_treated_as_cancellation(tmp_path, local_votes):
|
|
runtime = _direct_runtime(tmp_path)
|
|
|
|
def probe():
|
|
raise RuntimeError("probe failed")
|
|
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._check_continue(runtime, _export_config(), 0.0, probe, 40)
|
|
assert _code(captured) == "LMS_EXPORT_CANCELLED"
|
|
assert local_votes == [(40, False)]
|
|
|
|
|
|
def test_source_and_storage_revalidation_vote_on_identity(tmp_path, local_votes):
|
|
runtime = _direct_runtime(tmp_path)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._revalidate_storage(runtime)
|
|
assert _code(captured) == "LMS_EXPORT_STORAGE_CHANGED"
|
|
assert local_votes == [(17_000_011, False)]
|
|
|
|
|
|
# ------------------------------------------------------------ metadata copies
|
|
|
|
|
|
def test_metadata_snapshot_rejects_writable_and_concurrently_changed_files(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
writable = tmp_path / "config.json"
|
|
writable.write_text("{}", encoding="ascii")
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._snapshot_metadata_file(writable, "config.json")
|
|
assert _code(captured) == "LMS_EXPORT_SOURCE_CHANGED"
|
|
|
|
(tmp_path / "rt").mkdir()
|
|
runtime = _direct_runtime(tmp_path / "rt")
|
|
(runtime.source_path / "config.json").write_text("{}", encoding="ascii")
|
|
# A propagated boundary error keeps its source-changed classification.
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._metadata_snapshots(runtime, _export_config())
|
|
assert _code(captured) == "LMS_EXPORT_SOURCE_CHANGED"
|
|
|
|
stable = _write_readonly(tmp_path / "tokenizer.json", b"{}")
|
|
original = export._hash_descriptor
|
|
|
|
def touch_while_hashing(descriptor):
|
|
os.utime(stable, ns=(1, 1))
|
|
return original(descriptor)
|
|
|
|
monkeypatch.setattr(export, "_hash_descriptor", touch_while_hashing)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._snapshot_metadata_file(stable, "tokenizer.json")
|
|
assert _code(captured) == "LMS_EXPORT_SOURCE_CHANGED"
|
|
|
|
|
|
def test_copy_snapshot_refuses_identity_or_digest_drift(tmp_path, monkeypatch):
|
|
source = _write_readonly(tmp_path / "tokenizer.json", b'{"vocab": 1}')
|
|
snapshot = export._snapshot_metadata_file(source, "tokenizer.json")
|
|
|
|
staging = tmp_path / "ok"
|
|
export._copy_snapshot(snapshot, staging)
|
|
assert (staging / "tokenizer.json").read_bytes() == b'{"vocab": 1}'
|
|
assert not os.access(staging / "tokenizer.json", os.W_OK) or os.geteuid() == 0
|
|
|
|
cases = {
|
|
"identity": replace(snapshot, identity=(0, 0, 0, 0, 0, 0)),
|
|
"digest": replace(snapshot, sha256=DIGEST),
|
|
"missing": replace(snapshot, source_path=tmp_path / "absent.json"),
|
|
}
|
|
for name, broken in cases.items():
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._copy_snapshot(broken, tmp_path / name)
|
|
assert _code(captured) == "LMS_EXPORT_SOURCE_CHANGED", name
|
|
|
|
calls = [0]
|
|
original = export._file_identity
|
|
|
|
def drift_after_copy(value):
|
|
calls[0] += 1
|
|
return (-1,) * 6 if calls[0] == 2 else original(value)
|
|
|
|
monkeypatch.setattr(export, "_file_identity", drift_after_copy)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._copy_snapshot(snapshot, tmp_path / "late")
|
|
assert _code(captured) == "LMS_EXPORT_SOURCE_CHANGED"
|
|
assert calls[0] == 2
|
|
|
|
|
|
# --------------------------------------------------------- manifest/assignments
|
|
|
|
|
|
def test_logical_bytes_and_assignment_bounds(tmp_path):
|
|
assert export._logical_bytes((3, 4)) == 24
|
|
with pytest.raises(ContractError, match="overflow"):
|
|
export._logical_bytes((2**62, 4))
|
|
with pytest.raises(ContractError, match="empty, duplicated"):
|
|
export._assignments(SimpleNamespace(entries=(), world_size=2))
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
assignments = export._assignments(runtime.manifest)
|
|
assert [item.writer_rank for item in assignments] == [0, 1, 0]
|
|
missing = replace(assignments[0], tensor_name="absent")
|
|
with pytest.raises(ContractError, match="exactly one"):
|
|
export._entry_for(runtime, missing)
|
|
|
|
|
|
def test_tree_walk_and_prior_output_enforce_entry_bounds(tmp_path, monkeypatch):
|
|
tree = tmp_path / "tree"
|
|
tree.mkdir()
|
|
for index in range(5):
|
|
(tree / f"f{index}").write_bytes(b"x")
|
|
monkeypatch.setattr(export, "MAX_EXPORT_FILES", 1)
|
|
with pytest.raises(ContractError, match="entry bound"):
|
|
tuple(export._walk_entries(tree))
|
|
|
|
observed = (tree / "f0").stat()
|
|
monkeypatch.setattr(
|
|
export, "_walk_entries", lambda path: iter([(tree / "f0", observed)] * 5),
|
|
)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._path_bytes(tree, maximum=1 << 20)
|
|
assert _code(captured) == "LMS_EXPORT_RESOURCE_DENIED"
|
|
|
|
|
|
def test_source_parser_sizing_skips_directories_and_other_files(tmp_path):
|
|
runtime = replace(_direct_runtime(tmp_path), max_source_file_bytes=1 << 30)
|
|
(runtime.source_path / "nested").mkdir()
|
|
(runtime.source_path / "README.md").write_bytes(b"x" * 100)
|
|
(runtime.source_path / "nested" / "config.json").write_bytes(b"{}")
|
|
with (runtime.source_path / "model.safetensors").open("wb") as stream:
|
|
stream.write((16).to_bytes(8, "little") + b" " * 16)
|
|
assert export._source_parser_host_bytes(runtime) == 256 * (16 + 2)
|
|
|
|
(runtime.source_path / "short.safetensors").write_bytes(b"abc")
|
|
with pytest.raises(ContractError, match="truncated"):
|
|
export._source_parser_host_bytes(runtime)
|
|
|
|
|
|
def test_source_parser_sizing_detects_replacement_around_header_read(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
weights = runtime.source_path / "model.safetensors"
|
|
weights.write_bytes((4).to_bytes(8, "little") + b" ")
|
|
other = runtime.source_path / "other.bin"
|
|
other.write_bytes(b"different identity")
|
|
original_walk = export._walk_entries
|
|
monkeypatch.setattr(export, "_walk_entries", lambda path: iter(
|
|
[(weights, other.stat())]))
|
|
with pytest.raises(ContractError, match="before header sizing"):
|
|
export._source_parser_host_bytes(runtime)
|
|
|
|
monkeypatch.setattr(export, "_walk_entries", original_walk)
|
|
other.unlink()
|
|
calls = [0]
|
|
original_identity = export._file_identity
|
|
|
|
def drift(value):
|
|
calls[0] += 1
|
|
return (-1,) * 6 if calls[0] == 3 else original_identity(value)
|
|
|
|
monkeypatch.setattr(export, "_file_identity", drift)
|
|
with pytest.raises(ContractError, match="during header sizing"):
|
|
export._source_parser_host_bytes(runtime)
|
|
|
|
|
|
def test_admission_denies_when_staging_capacity_cannot_be_measured(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
config = _export_config()
|
|
votes = []
|
|
monkeypatch.setattr(export, "_collective_accept", lambda runtime, sequence, accepted, **kw:
|
|
votes.append((sequence, accepted)))
|
|
|
|
def unavailable(path):
|
|
raise OSError("statvfs failed")
|
|
|
|
monkeypatch.setattr(export.shutil, "disk_usage", unavailable)
|
|
export._resource_admission(
|
|
runtime, config, export._assignments(runtime.manifest), (),
|
|
runtime.staging_path / config.output_name,
|
|
)
|
|
assert votes == [(17_000_020, True), (17_000_021, False)]
|
|
|
|
|
|
class _Buffered(nn.Module):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.weight = nn.Parameter(torch.ones(2, dtype=torch.bfloat16))
|
|
self.register_buffer("persistent", torch.zeros(1))
|
|
self.register_buffer("transient", torch.zeros(1), persistent=False)
|
|
self.register_buffer("absent", None)
|
|
self.child = nn.Module()
|
|
self.child.register_buffer("nested", torch.zeros(1))
|
|
|
|
|
|
def test_persistent_buffers_are_named_and_refused_by_structure_check(tmp_path, local_votes):
|
|
model = _Buffered()
|
|
assert export._persistent_buffer_names(model) == ("child.nested", "persistent")
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
runtime.model.register_buffer("stale", torch.zeros(1))
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_model_structure(runtime)
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
assert local_votes == [(17_000_030, False)]
|
|
|
|
|
|
def test_shard_layout_validation_rejects_each_malformed_shape(tmp_path):
|
|
runtime = _direct_runtime(tmp_path)
|
|
entries = {item.name: item for item in runtime.manifest.entries}
|
|
sharded = entries["sharded"]
|
|
assert export._validate_shards(sharded, runtime)
|
|
assert export._validate_shards(entries["replicated"], runtime)
|
|
|
|
reordered = SimpleNamespace(
|
|
rank_shards=tuple(reversed(sharded.rank_shards)), placement=sharded.placement,
|
|
shard_dim=0, global_shape=sharded.global_shape,
|
|
)
|
|
assert not export._validate_shards(reordered, runtime)
|
|
no_dim = SimpleNamespace(
|
|
rank_shards=sharded.rank_shards, placement=ManifestPlacement.SHARD,
|
|
shard_dim=None, global_shape=sharded.global_shape,
|
|
)
|
|
assert not export._validate_shards(no_dim, runtime)
|
|
gap = SimpleNamespace(
|
|
rank_shards=(ParameterShard(0, 0, 1, (1, 2)), ParameterShard(1, 2, 4, (2, 2))),
|
|
placement=ManifestPlacement.SHARD, shard_dim=0, global_shape=(4, 2),
|
|
)
|
|
assert not export._validate_shards(gap, runtime)
|
|
|
|
|
|
def test_local_digest_gathering_refuses_a_shard_outside_its_manifest(tmp_path, local_votes):
|
|
runtime = _direct_runtime(tmp_path)
|
|
assignment = next(
|
|
item for item in export._assignments(runtime.manifest) if item.tensor_name == "sharded"
|
|
)
|
|
entry = export._entry_for(runtime, assignment)
|
|
wrong_shape = torch.ones((4, 2), dtype=torch.bfloat16)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._gather_local_digests(runtime, assignment, entry, wrong_shape, DIGEST)
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
assert local_votes == [(18_000_001 + assignment.sequence * 32, False)]
|
|
|
|
|
|
def test_reconstruction_maps_shard_transfer_failure(tmp_path, local_votes, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
assignments = export._assignments(runtime.manifest)
|
|
replicated = next(item for item in assignments if item.tensor_name == "replicated")
|
|
entry = export._entry_for(runtime, replicated)
|
|
complete = export._reconstruct_tensor(runtime, replicated, entry, runtime.model.replicated)
|
|
if replicated.writer_rank == 0:
|
|
assert torch.equal(complete, runtime.model.replicated.detach())
|
|
|
|
sharded = next(item for item in assignments if item.tensor_name == "sharded")
|
|
|
|
def lost(*args, **kwargs):
|
|
raise RuntimeError("peer lost")
|
|
|
|
monkeypatch.setattr(export.dist, "broadcast", lost)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._reconstruct_tensor(
|
|
runtime, sharded, export._entry_for(runtime, sharded), runtime.model.sharded,
|
|
)
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
|
|
# ----------------------------------------------------------------- tensor I/O
|
|
|
|
|
|
def test_tensor_writer_refuses_existing_targets_and_invalid_tensors(tmp_path):
|
|
runtime = _direct_runtime(tmp_path)
|
|
(assignment,), _ = _replicated_assignment(runtime)
|
|
(runtime.staging_path / assignment.file_name).write_bytes(b"prior")
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._write_tensor(runtime.staging_path, assignment, runtime.model.replicated)
|
|
assert _code(captured) == "LMS_EXPORT_WRITE_FAILED"
|
|
|
|
fresh = tmp_path / "fresh"
|
|
fresh.mkdir()
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._write_tensor(fresh, assignment, torch.ones(4))
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
assert not tuple(fresh.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("fault", ["metadata", "digest", "nonregular", "runtime_error"])
|
|
def test_tensor_writer_reload_failures_are_write_failures(tmp_path, monkeypatch, fault):
|
|
runtime = _direct_runtime(tmp_path)
|
|
(assignment,), _ = _replicated_assignment(runtime)
|
|
if fault == "metadata":
|
|
original_save = export.save_file
|
|
monkeypatch.setattr(export, "save_file", lambda tensors, path, metadata: original_save(
|
|
tensors, path, metadata={"format": "np"}))
|
|
elif fault == "digest":
|
|
calls = [0]
|
|
original_digest = export.tensor_digest
|
|
|
|
def digest(value):
|
|
calls[0] += 1
|
|
return DIGEST if calls[0] == 2 else original_digest(value)
|
|
|
|
monkeypatch.setattr(export, "tensor_digest", digest)
|
|
elif fault == "nonregular":
|
|
monkeypatch.setattr(export, "stat", SimpleNamespace(S_ISREG=lambda mode: False))
|
|
else:
|
|
def sync(path):
|
|
raise RuntimeContractError("LMS_EXPORT_TIMEOUT", "fsync deadline")
|
|
|
|
monkeypatch.setattr(export, "_sync_file", sync)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._write_tensor(runtime.staging_path, assignment, runtime.model.replicated)
|
|
expected = "LMS_EXPORT_TIMEOUT" if fault == "runtime_error" else "LMS_EXPORT_WRITE_FAILED"
|
|
assert _code(captured) == expected
|
|
|
|
|
|
def test_tree_inventory_refuses_unsafe_or_unexpected_entries(tmp_path):
|
|
root = tmp_path / "tree"
|
|
(root / "nested").mkdir(parents=True)
|
|
(root / "nested" / "vocab.json").write_bytes(b"{}")
|
|
(root / "config.json").write_bytes(b"{}")
|
|
expected = {"config.json", "nested/vocab.json"}
|
|
assert export._tree_files(root, expected_files=expected) == expected
|
|
assert export._tree_files(root) == expected
|
|
|
|
with pytest.raises(ContractError, match="unexpected file"):
|
|
export._tree_files(root, expected_files={"nested/vocab.json"})
|
|
(root / "empty").mkdir()
|
|
with pytest.raises(ContractError, match="unexpected directory"):
|
|
export._tree_files(root, expected_files=expected)
|
|
(root / "empty").rmdir()
|
|
(root / "link").symlink_to(root / "config.json")
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._tree_files(root, expected_files=expected)
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
|
|
|
|
def _staged_tensor(tmp_path):
|
|
runtime = _direct_runtime(tmp_path)
|
|
assignments, _ = _replicated_assignment(runtime)
|
|
record = export._write_tensor(runtime.staging_path, assignments[0], runtime.model.replicated)
|
|
export._write_index(runtime.staging_path, assignments)
|
|
# The untouched tree validates; each corruption below must break it.
|
|
export._validate_tensor_tree(runtime.staging_path, assignments, (record,), _export_config())
|
|
return runtime, assignments, record
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"corruption",
|
|
["missing_expected", "extra_weight", "missing_weight", "header_dtype",
|
|
"file_metadata", "tensor_digest"],
|
|
)
|
|
def test_tensor_tree_validation_rejects_every_disagreement(tmp_path, monkeypatch, corruption):
|
|
runtime, assignments, record = _staged_tensor(tmp_path)
|
|
staging = runtime.staging_path
|
|
kwargs = {}
|
|
records = (record,)
|
|
if corruption == "missing_expected":
|
|
kwargs["expected_files"] = {
|
|
assignments[0].file_name, "model.safetensors.index.json", "config.json",
|
|
}
|
|
elif corruption == "extra_weight":
|
|
save_file({"x": torch.ones(1)}, str(staging / "stray.safetensors"))
|
|
elif corruption == "missing_weight":
|
|
assignments = (*assignments, replace(
|
|
assignments[0], sequence=1, tensor_name="ghost", file_name="ghost.safetensors",
|
|
))
|
|
elif corruption == "header_dtype":
|
|
original = export.inspect_safetensors_metadata
|
|
|
|
def inspect(path, **kw):
|
|
return tuple(replace(item, dtype="F32") for item in original(path, **kw))
|
|
|
|
monkeypatch.setattr(export, "inspect_safetensors_metadata", inspect)
|
|
elif corruption == "file_metadata":
|
|
original_open = export.safe_open
|
|
|
|
class _Relabelled:
|
|
def __init__(self, *args, **kw):
|
|
self.inner = original_open(*args, **kw)
|
|
|
|
def __enter__(self):
|
|
self.handle = self.inner.__enter__()
|
|
return self
|
|
|
|
def __exit__(self, *exc):
|
|
return self.inner.__exit__(*exc)
|
|
|
|
def keys(self):
|
|
return self.handle.keys()
|
|
|
|
def metadata(self):
|
|
return {"format": "np"}
|
|
|
|
monkeypatch.setattr(export, "safe_open", _Relabelled)
|
|
else:
|
|
records = (replace(record, tensor_sha256=DIGEST),)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_tensor_tree(staging, assignments, records, _export_config(), **kwargs)
|
|
assert _code(captured) == "LMS_EXPORT_HASH_MISMATCH"
|
|
|
|
|
|
# ------------------------------------------------------------ evidence gathers
|
|
|
|
|
|
def test_evidence_exchanges_map_transport_failure(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
|
|
def broken(value, *, group):
|
|
raise RuntimeError("transport lost")
|
|
|
|
monkeypatch.setattr(export, "gloo_all_gather_records", broken)
|
|
request = ReloadValidationRequest(1, 0, 2, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST)
|
|
evidence = ReloadValidationEvidence(
|
|
1, 0, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST, True,
|
|
)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._gather_reload_evidence(runtime, evidence, request)
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
rank_evidence = RankExportEvidence(1, 0, DIGEST, DIGEST, DIGEST, 1, 8, 0, 0, 0, DIGEST)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._gather_rank_evidence(runtime, rank_evidence, ())
|
|
assert _code(captured) == "LMS_EXPORT_COLLECTIVE_FAILED"
|
|
|
|
|
|
def test_peak_rss_probe_degrades_to_zero(monkeypatch):
|
|
assert export._peak_host_rss_bytes() > 0
|
|
|
|
def broken():
|
|
raise OSError("uname unavailable")
|
|
|
|
monkeypatch.setattr(export.os, "uname", broken)
|
|
assert export._peak_host_rss_bytes() == 0
|
|
|
|
|
|
# --------------------------------------------------------- final tree/cleanup
|
|
|
|
|
|
def _final_tree(tmp_path, manifest_value):
|
|
staging = tmp_path / "final"
|
|
staging.mkdir()
|
|
_write_readonly(
|
|
staging / "distributed-export-manifest.json", json.dumps(manifest_value).encode(),
|
|
)
|
|
return staging
|
|
|
|
|
|
def test_final_tree_rejects_incomplete_oversized_or_divergent_manifest(tmp_path):
|
|
expected = {"tensor_records": [], "metadata_records": [],
|
|
"summary": {"hf_index_digest": DIGEST}}
|
|
names = {"distributed-export-manifest.json"}
|
|
staging = _final_tree(tmp_path, expected)
|
|
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_final_tree(staging, names | {"config.json"}, expected, _export_config())
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
|
|
tiny = replace(_export_config(), max_output_bytes=1)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_final_tree(staging, names, expected, tiny)
|
|
assert _code(captured) == "LMS_EXPORT_RESOURCE_DENIED"
|
|
|
|
divergent = {**expected, "summary": {"hf_index_digest": "f" * 64}}
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_final_tree(staging, names, divergent, _export_config())
|
|
assert _code(captured) == "LMS_EXPORT_MANIFEST_INVALID"
|
|
assert "disagrees" in str(captured.value)
|
|
|
|
|
|
def test_staging_invalidation_tolerates_absence_and_rename_failure(tmp_path, monkeypatch):
|
|
export._invalidate_staging(None)
|
|
export._invalidate_staging(tmp_path / "absent")
|
|
|
|
staging = tmp_path / "staging"
|
|
staging.mkdir()
|
|
(staging / "model.safetensors.index.json").write_text("{}")
|
|
(staging / "model-00001-of-00001.safetensors").write_bytes(b"x")
|
|
|
|
def refused(*args):
|
|
raise OSError("read-only filesystem")
|
|
|
|
with monkeypatch.context() as patch:
|
|
patch.setattr(export.os, "replace", refused)
|
|
export._invalidate_staging(staging)
|
|
assert (staging / "model.safetensors.index.json").exists()
|
|
|
|
export._invalidate_staging(staging)
|
|
assert sorted(path.name for path in staging.iterdir()) == [
|
|
".model.safetensors.index.invalid", "model-00001-of-00001.invalid",
|
|
]
|
|
|
|
|
|
def test_error_translation_is_closed():
|
|
assert export._translate_error(ValueError(), promoted=True).code == (
|
|
"LMS_EXPORT_STATE_UNCERTAIN")
|
|
kept = RuntimeContractError("LMS_EXPORT_TIMEOUT", "kept")
|
|
assert export._translate_error(kept, promoted=False) is kept
|
|
assert export._translate_error(
|
|
RuntimeContractError("LMS_VERIFY_STATE_CHANGED", "x"), promoted=False,
|
|
).code == "LMS_EXPORT_STATE_INVALID"
|
|
assert export._translate_error(TimeoutError(), promoted=False).code == "LMS_EXPORT_TIMEOUT"
|
|
assert export._translate_error(KeyError("x"), promoted=False).code == (
|
|
"LMS_EXPORT_WRITE_FAILED")
|
|
|
|
|
|
def test_entry_rejects_open_capabilities_and_retires_verified_state(tmp_path, local_votes):
|
|
runtime = _direct_runtime(tmp_path)
|
|
_, verified = _verified(runtime.model, runtime.manifest, 0, tmp_path, runtime.run_id)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export.run_distributed_export(
|
|
runtime, verified, object(), semantic_reload_validator=lambda *a: None,
|
|
)
|
|
assert _code(captured) == "LMS_EXPORT_CONFIG_INVALID"
|
|
assert verified.excise_result.transaction.state is ExciseTransactionState.QUARANTINED
|
|
assert local_votes == [(17_000_000, False), (23_999_999, True)]
|
|
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export.run_distributed_export(
|
|
runtime, object(), _export_config(), semantic_reload_validator=lambda *a: None,
|
|
)
|
|
assert _code(captured) == "LMS_EXPORT_CONFIG_INVALID"
|
|
|
|
|
|
def test_live_export_refuses_unclassifiable_topology_and_quarantines(tmp_path, local_votes,
|
|
monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
_, verified = _verified(runtime.model, runtime.manifest, 0, tmp_path, runtime.run_id)
|
|
|
|
def unclassifiable(*args):
|
|
raise RuntimeError("launch metadata missing")
|
|
|
|
monkeypatch.setattr(export, "classify_tp2_topology", unclassifiable)
|
|
session = SimpleNamespace(control_group=object(), config=None, launch=None, preflight=None)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export.run_live_distributed_export(
|
|
session, verified, _export_config(), semantic_reload_validator=lambda *a: None,
|
|
)
|
|
assert _code(captured) == "LMS_EXPORT_TOPOLOGY_UNSUPPORTED"
|
|
assert local_votes == [(17_000_071, False)]
|
|
assert verified.excise_result.transaction.state is ExciseTransactionState.QUARANTINED
|
|
|
|
|
|
# ------------------------------------------------- two-rank thread-world paths
|
|
|
|
|
|
def _assert_failed(case, results, code):
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {code}
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_live_export_success_returns_published_result(mocked_export_world): # noqa: F811
|
|
case = mocked_export_world
|
|
results = case.run(mode="live")
|
|
assert all(isinstance(item, export.DistributedExportResult) for item in results), results
|
|
assert results[0].digest == results[1].digest
|
|
assert all(tx.state is ExciseTransactionState.PUBLISHED for tx in case.transactions)
|
|
assert (case.output / "distributed-export-manifest.json").is_file()
|
|
|
|
|
|
class _CheckpointProxy:
|
|
def __init__(self, inner, *, enter_as_text=False, failing_exit=False):
|
|
self.inner = inner
|
|
self.enter_as_text = enter_as_text
|
|
self.failing_exit = failing_exit
|
|
|
|
def __enter__(self):
|
|
path = self.inner.__enter__()
|
|
return str(path) if self.enter_as_text else path
|
|
|
|
def __exit__(self, kind, value, traceback):
|
|
result = self.inner.__exit__(kind, value, traceback)
|
|
if self.failing_exit and kind is not None:
|
|
raise RuntimeError("cleanup could not be confirmed")
|
|
return result
|
|
|
|
|
|
def _proxy_checkpoints(monkeypatch, **options):
|
|
original = export.atomic_checkpoint_directory
|
|
monkeypatch.setattr(export, "atomic_checkpoint_directory", lambda *args, **kwargs:
|
|
_CheckpointProxy(original(*args, **kwargs), **options))
|
|
|
|
|
|
def test_coordinator_staging_must_be_a_path(mocked_export_world, monkeypatch): # noqa: F811
|
|
case = mocked_export_world
|
|
_proxy_checkpoints(monkeypatch, enter_as_text=True)
|
|
results = case.run()
|
|
_assert_failed(case, results, "LMS_EXPORT_WRITE_FAILED")
|
|
assert (17_000_040, False) in case.votes[0]
|
|
assert (17_000_040, True) in case.votes[1]
|
|
assert not tuple(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
def test_unconfirmed_coordinator_cleanup_fails_closed(mocked_export_world, monkeypatch): # noqa: F811
|
|
case = mocked_export_world
|
|
_proxy_checkpoints(monkeypatch, failing_exit=True)
|
|
|
|
def fault(point, sequence):
|
|
if point == "writer_before" and case.context.rank == 0:
|
|
raise RuntimeError("private writer failure")
|
|
|
|
results = case.run(fault=fault)
|
|
_assert_failed(case, results, "LMS_EXPORT_CLEANUP_FAILED")
|
|
assert (23_999_999, False) in case.votes[0]
|
|
|
|
|
|
def test_invalidation_errors_during_cleanup_do_not_mask_the_failure(
|
|
mocked_export_world, monkeypatch, # noqa: F811
|
|
):
|
|
case = mocked_export_world
|
|
|
|
def invalidate(staging):
|
|
raise RuntimeError("invalidate failed")
|
|
|
|
monkeypatch.setattr(export, "_invalidate_staging", invalidate)
|
|
|
|
def fault(point, sequence):
|
|
if point == "writer_before" and case.context.rank == 0:
|
|
raise RuntimeError("private writer failure")
|
|
|
|
results = case.run(fault=fault)
|
|
_assert_failed(case, results, "LMS_EXPORT_WRITE_FAILED")
|
|
assert not tuple(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
def test_lost_cleanup_vote_is_a_cleanup_failure(mocked_export_world, monkeypatch): # noqa: F811
|
|
case = mocked_export_world
|
|
original = export.unanimous_vote
|
|
|
|
def vote(sequence, accepted, *, group):
|
|
if sequence == 23_999_999:
|
|
raise RuntimeError("peer lost during cleanup")
|
|
return original(sequence, accepted, group=group)
|
|
|
|
monkeypatch.setattr(export, "unanimous_vote", vote)
|
|
|
|
def fault(point, sequence):
|
|
if point == "writer_before" and case.context.rank == 0:
|
|
raise RuntimeError("private writer failure")
|
|
|
|
results = case.run(fault=fault)
|
|
_assert_failed(case, results, "LMS_EXPORT_CLEANUP_FAILED")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key,mutate,code",
|
|
[
|
|
("staging_name", lambda record: {**record, "export_id": "2" * 32},
|
|
"LMS_EXPORT_COLLECTIVE_FAILED"),
|
|
("assignment_digest", lambda record: {**record, "sequence": record["sequence"] + 1},
|
|
"LMS_EXPORT_HASH_MISMATCH"),
|
|
("index_digest", lambda record: {"index_digest": "not-a-digest"},
|
|
"LMS_EXPORT_INDEX_INVALID"),
|
|
],
|
|
)
|
|
def test_one_rank_broadcast_disagreement_is_refused(
|
|
mocked_export_world, monkeypatch, key, mutate, code, # noqa: F811
|
|
):
|
|
case = mocked_export_world
|
|
original = export._broadcast_record
|
|
|
|
def broadcast(*args, **kwargs):
|
|
record = original(*args, **kwargs)
|
|
if case.context.rank == 1 and key in record:
|
|
return mutate(record)
|
|
return record
|
|
|
|
monkeypatch.setattr(export, "_broadcast_record", broadcast)
|
|
results = case.run()
|
|
_assert_failed(case, results, code)
|
|
|
|
|
|
def test_pre_promotion_barrier_failure_never_promotes(mocked_export_world, monkeypatch): # noqa: F811
|
|
case = mocked_export_world
|
|
|
|
def barrier(*, group):
|
|
raise RuntimeError("barrier lost")
|
|
|
|
monkeypatch.setattr(export.dist, "barrier", barrier)
|
|
results = case.run()
|
|
_assert_failed(case, results, "LMS_EXPORT_COLLECTIVE_FAILED")
|
|
assert not (case.output / "distributed-export-manifest.json").exists()
|
|
|
|
|
|
def test_failed_promotion_with_failed_invalidation_is_uncertain(
|
|
mocked_export_world, monkeypatch, # noqa: F811
|
|
):
|
|
case = mocked_export_world
|
|
invalidations = []
|
|
|
|
def invalidate(staging):
|
|
invalidations.append(staging)
|
|
raise RuntimeError("invalidate failed")
|
|
|
|
monkeypatch.setattr(export, "_invalidate_staging", invalidate)
|
|
|
|
def fault(point, sequence):
|
|
if point == "promotion_after" and case.context.rank == 0:
|
|
raise RuntimeError("confirmation lost")
|
|
|
|
results = case.run(fault=fault)
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_UNCERTAIN"}
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert invalidations
|