Files
OBLITERATUS/tests/test_distributed_export_gloo.py
Joseph Magly e2db84183f fix(persistence,vocab): fd-held staging cleanup, keep cancellations, guard apply_all, silent-owner tie flag
Staging cleanup now removes only the directory held open since creation, so a
directory recreated at the staging path is never deleted. Cancellations after
a no-replace rename stay cancellations, with the outcome as a note; an
interrupted discovery publish names the file as possibly published.
VocabSurgeryStrategy.apply_all enforces the ID-only tokenizer guard itself, a
silent alias owner defers to the top-level tie flag, and
logical_values_identical no longer reshapes non-rank-2 values.

Fixes #649
Fixes #655
2026-11-25 02:38:37 -05:00

997 lines
36 KiB
Python

"""Two-rank bounded reconstruction, reload, failure, and atomicity 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
from safetensors import safe_open
from safetensors.torch import save_file
import obliteratus.persistence_contracts as persistence_module
import obliteratus.distributed.export as export_module
from obliteratus.distributed.contracts import ContractError, contract_digest
from obliteratus.distributed.export import ExportRuntime, run_distributed_export
from obliteratus.distributed.export_contracts import (
ExportConfig,
ReloadValidationEvidence,
ReloadValidationRequest,
)
from obliteratus.distributed.model import model_config_digest
from obliteratus.distributed.model_manifest import (
ManifestPlacement,
ParameterManifest,
ParameterManifestEntry,
ParameterShard,
RuntimeStorageKind,
SurgeryRole,
)
from obliteratus.distributed.preflight import (
SourceIdentity,
inspect_source,
storage_mount_digest,
)
from obliteratus.distributed.probe_contracts import tensor_digest
from obliteratus.distributed.surgery import (
ExciseResult,
ExciseTransaction,
ExciseTransactionState,
SurgeryRuntime,
)
from obliteratus.distributed.surgery_contracts import (
ExciseOperationSummary,
ExciseSummary,
)
from obliteratus.distributed.verification import VerificationResult, _state_roots_digest
from obliteratus.distributed.verification_contracts import (
VerificationMetrics,
VerificationSummary,
)
pytestmark = pytest.mark.gloo
WORLD_SIZE = 2
COLUMN_SHARDED = "column_sharded"
REPLICATED = "replicated"
SHARDED = "sharded"
def _export_config() -> ExportConfig:
return ExportConfig(
export_id="1" * 32,
output_name="canonical-output",
max_output_bytes=32 << 20,
max_staging_bytes=64 << 20,
max_host_bytes_per_rank=1 << 28,
max_device_bytes_per_rank=1 << 28,
semantic_reload_host_bytes_per_rank=64 << 20,
semantic_reload_device_bytes_per_rank=64 << 20,
max_tensor_bytes=1 << 18,
max_metadata_bytes=1 << 16,
max_metadata_files=16,
safety_margin_bytes=0,
stage_timeout_seconds=20,
)
class _TinyPublishedModel(nn.Module):
def __init__(self, rank: int) -> None:
super().__init__()
self.replicated = nn.Parameter(
torch.tensor((1.0, 2.0, 3.0, 4.0), dtype=torch.bfloat16)
)
self.sharded = nn.Parameter(
torch.tensor(
((1.0, 2.0), (3.0, 4.0))
if rank == 0
else ((5.0, 6.0), (7.0, 8.0)),
dtype=torch.bfloat16,
)
)
self.column_sharded = nn.Parameter(
torch.tensor(
((10.0,), (20.0,), (30.0,), (40.0,))
if rank == 0
else ((11.0,), (21.0,), (31.0,), (41.0,)),
dtype=torch.bfloat16,
)
)
def state_dict(self, *args, **kwargs):
del args, kwargs
raise AssertionError("rank-zero complete-model state gathering is prohibited")
def _manifest(model: nn.Module, rank: int) -> ParameterManifest:
parameters = dict(model.named_parameters())
replicated_digest = tensor_digest(parameters[REPLICATED])
return ParameterManifest(
schema_version=1,
adapter_id="tiny-bf16-tp2",
rank=rank,
world_size=WORLD_SIZE,
entries=(
ParameterManifestEntry(
name=COLUMN_SHARDED,
global_shape=(4, 2),
local_shape=(4, 1),
dtype="bfloat16",
placement=ManifestPlacement.SHARD,
shard_dim=1,
shard_start=rank,
shard_end=rank + 1,
owner_ranks=(0, 1),
tie_id=None,
local_value_digest=tensor_digest(parameters[COLUMN_SHARDED]),
replicated_value_digest=None,
hook_site=None,
surgery_role=SurgeryRole.IMMUTABLE,
runtime_storage_kind=RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
rank_shards=(
ParameterShard(0, 0, 1, (4, 1)),
ParameterShard(1, 1, 2, (4, 1)),
),
),
ParameterManifestEntry(
name=REPLICATED,
global_shape=(4,),
local_shape=(4,),
dtype="bfloat16",
placement=ManifestPlacement.REPLICATE,
shard_dim=None,
shard_start=0,
shard_end=0,
owner_ranks=(0, 1),
tie_id=None,
local_value_digest=replicated_digest,
replicated_value_digest=replicated_digest,
hook_site=None,
surgery_role=SurgeryRole.IMMUTABLE,
runtime_storage_kind=RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
rank_shards=(
ParameterShard(0, 0, 0, (4,)),
ParameterShard(1, 0, 0, (4,)),
),
),
ParameterManifestEntry(
name=SHARDED,
global_shape=(4, 2),
local_shape=(2, 2),
dtype="bfloat16",
placement=ManifestPlacement.SHARD,
shard_dim=0,
shard_start=rank * 2,
shard_end=(rank + 1) * 2,
owner_ranks=(0, 1),
tie_id=None,
local_value_digest=tensor_digest(parameters[SHARDED]),
replicated_value_digest=None,
hook_site=None,
surgery_role=SurgeryRole.IMMUTABLE,
runtime_storage_kind=RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
rank_shards=(
ParameterShard(0, 0, 2, (2, 2)),
ParameterShard(1, 2, 4, (2, 2)),
),
),
),
activation_sites=(),
)
def _direct_runtime(tmp_path: Path) -> ExportRuntime:
model = _TinyPublishedModel(0)
source = tmp_path / "source"
source.mkdir()
staging = tmp_path / "staging"
staging.mkdir()
return ExportRuntime(
run_id="1" * 32,
rank=0,
world_size=WORLD_SIZE,
model=model,
manifest=_manifest(model, 0),
control_group=object(),
tensor_device=torch.device("cpu"),
source_path=source,
source_identity=SourceIdentity("2" * 64, "3" * 64, "4" * 64, 1, 1),
staging_path=staging,
runtime_config_digest="5" * 64,
hf_config_digest="6" * 64,
placement_plan_digest="7" * 64,
topology_digest="8" * 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,
available_host_bytes=1 << 30,
available_staging_bytes=1 << 30,
available_device_bytes=1 << 30,
)
def _state_digest() -> str:
replicated = torch.tensor((1.0, 2.0, 3.0, 4.0), dtype=torch.bfloat16)
shard0 = torch.tensor(((1.0, 2.0), (3.0, 4.0)), dtype=torch.bfloat16)
shard1 = torch.tensor(((5.0, 6.0), (7.0, 8.0)), dtype=torch.bfloat16)
column0 = torch.tensor(((10.0,), (20.0,), (30.0,), (40.0,)), dtype=torch.bfloat16)
column1 = torch.tensor(((11.0,), (21.0,), (31.0,), (41.0,)), dtype=torch.bfloat16)
roots = {
COLUMN_SHARDED: contract_digest((tensor_digest(column0), tensor_digest(column1))),
REPLICATED: contract_digest((tensor_digest(replicated), tensor_digest(replicated))),
SHARDED: contract_digest((tensor_digest(shard0), tensor_digest(shard1))),
}
return _state_roots_digest(roots)
def _metrics() -> VerificationMetrics:
return VerificationMetrics(
refusal_rate_hex=(0.0).hex(),
coherence_retention_hex=(1.0).hex(),
forward_kl_hex=(0.0).hex(),
capability_retention_hex=(1.0).hex(),
baseline_coherence_nll_hex=(1.0).hex(),
post_coherence_nll_hex=(1.0).hex(),
baseline_capability_probability_hex=(0.5).hex(),
post_capability_probability_hex=(0.5).hex(),
nonfinite_count=0,
)
def _verified(
model: nn.Module,
manifest: ParameterManifest,
rank: int,
root: Path,
run_id: str,
) -> tuple[SurgeryRuntime, VerificationResult]:
runtime = SurgeryRuntime(
run_id=run_id,
distilled_digest="a" * 64,
rank=rank,
world_size=2,
model=model,
manifest=manifest,
control_group=dist.group.WORLD,
data_group=dist.group.WORLD,
tensor_device=torch.device("cpu"),
available_host_bytes=1 << 30,
available_staging_bytes=1 << 30,
available_device_bytes=1 << 30,
journal_parent=root,
)
operation_summary = ExciseOperationSummary(
schema_version=1,
sequence=0,
operation_digest="b" * 64,
logical_pristine_digest="c" * 64,
logical_update_digest="d" * 64,
logical_post_digest="d" * 64,
coefficient_norm_sq_hex=(0.0).hex(),
)
excise_summary = ExciseSummary(
schema_version=1,
run_id=run_id,
distilled_digest=runtime.distilled_digest,
manifest_digest=manifest.digest,
config_digest="e" * 64,
operation_plan_digest="f" * 64,
journal_set_digest="1" * 64,
postcondition_set_digest=contract_digest((operation_summary.digest,)),
operation_count=1,
local_tensor_bytes=32,
)
transaction = ExciseTransaction(
runtime=runtime,
journal=SimpleNamespace(root=root / "removed-journal", active=False),
operations=(object(),),
parameters=dict(model.named_parameters()),
pristine_roots=("2" * 64,),
operation_summaries=(operation_summary,),
state=ExciseTransactionState.VERIFIED,
)
excise = ExciseResult(excise_summary, (), (), transaction)
state_digest = _state_digest()
summary = VerificationSummary(
schema_version=1,
run_id=run_id,
excise_digest=excise.digest,
baseline_digest="3" * 64,
config_digest="4" * 64,
corpus_digest="5" * 64,
pristine_state_digest="6" * 64,
post_state_digest=state_digest,
rank_evidence_set_digest="7" * 64,
metrics=_metrics(),
quality_accepted=True,
)
return runtime, VerificationResult(summary, (), (), excise)
def _semantic_validator(
staging: Path,
request: ReloadValidationRequest,
) -> ReloadValidationEvidence:
index = json.loads((staging / "model.safetensors.index.json").read_text())
tensors = {}
for name, file_name in index["weight_map"].items():
with safe_open(staging / file_name, framework="pt", device="cpu") as handle:
tensors[name] = handle.get_tensor(name)
config = json.loads((staging / "config.json").read_text())
if model_config_digest(config) != request.config_digest:
raise AssertionError("reloaded config digest changed")
if not (staging / "tokenizer_config.json").is_file():
raise AssertionError("reloaded tokenizer metadata is absent")
logits = (
tensors[SHARDED].float() @ tensors[REPLICATED][:2].float()
+ tensors[COLUMN_SHARDED].float().sum(dim=1)
)
generation = tuple(int(value) for value in torch.argsort(logits, descending=True))
return ReloadValidationEvidence(
schema_version=1,
rank=request.rank,
export_identity_digest=request.export_identity_digest,
key_set_digest=request.key_set_digest,
tensor_set_digest=request.tensor_set_digest,
config_digest=request.config_digest,
tokenizer_digest=request.tokenizer_digest,
logits_digest=tensor_digest(logits),
generation_digest=hashlib.sha256(repr(generation).encode()).hexdigest(),
passed=True,
)
def _worker(
rank: int,
rendezvous: str,
source: str,
staging_root: str,
source_identity: SourceIdentity,
config_payload: dict[str, object],
mode: str,
queue: mp.Queue,
) -> None:
source_config: Path | None = None
original_source_config: str | None = None
try:
dist.init_process_group(
"gloo",
init_method=f"file://{rendezvous}",
rank=rank,
world_size=WORLD_SIZE,
timeout=timedelta(seconds=10),
)
model = _TinyPublishedModel(rank)
manifest = _manifest(model, rank)
if mode == "tied_parameter":
manifest = replace(
manifest,
entries=(
replace(manifest.entries[0], tie_id="unsupported-tie"),
*manifest.entries[1:],
),
)
run_id = hashlib.sha256(mode.encode()).hexdigest()[:32]
surgery_runtime, verified = _verified(
model,
manifest,
rank,
Path(staging_root),
run_id,
)
del surgery_runtime
if mode == "state_tamper" and rank == 1:
with torch.no_grad():
model.sharded[0, 0] += 1
if mode == "persistent_buffer":
model.register_buffer("unexpected", torch.ones(1, dtype=torch.bfloat16))
raw_config = json.loads((Path(source) / "config.json").read_text())
source_config = Path(source) / "config.json"
original_source_config = source_config.read_text(encoding="utf-8")
runtime = ExportRuntime(
run_id=run_id,
rank=rank,
world_size=WORLD_SIZE,
model=model,
manifest=manifest,
control_group=dist.group.WORLD,
tensor_device=torch.device("cpu"),
source_path=Path(source),
source_identity=source_identity,
staging_path=Path(staging_root),
runtime_config_digest="8" * 64,
hf_config_digest=model_config_digest(raw_config),
placement_plan_digest="9" * 64,
topology_digest="7" * 64,
code_digest="a" * 64,
storage_digest=(
"b" * 64
if mode == "storage_changed"
else storage_mount_digest(Path(staging_root))
),
commit_sha="c" * 40,
max_source_files=32,
max_source_bytes=1 << 20,
max_source_file_bytes=1 << 19,
source_timeout_seconds=10,
available_host_bytes=1 if mode == "resource_denied" and rank == 1 else 1 << 30,
available_staging_bytes=1 << 30,
available_device_bytes=1 << 30,
)
def inject(point: str, sequence: int) -> None:
if mode == "rank_exit" and rank == 1 and point == "tensor_before" and sequence == 0:
os._exit(19)
if mode == "coordinator_exit" and rank == 0 and point == "promotion_before":
os._exit(19)
if (
mode in {"cleanup_failure", "writer_failure", "retry"}
and rank == 1
and point == "writer_before"
and sequence == 1
):
raise OSError("private writer failure")
if mode == "index_failure" and rank == 0 and point == "index_written":
raise OSError("private index failure")
if mode == "promotion_failure" and rank == 0 and point == "promotion_before":
raise OSError("private promotion failure")
if mode == "promotion_uncertain" and rank == 0 and point == "promotion_after":
raise OSError("private confirmation failure")
if mode == "source_changed" and rank == 0 and point == "tensor_reload_complete":
assert source_config is not None
source_config.chmod(0o644)
source_config.write_text('{"model_type":"changed"}', encoding="utf-8")
source_config.chmod(0o444)
if mode == "hash_corruption" and rank == 0 and point == "index_written":
shard = next(Path(staging_root).glob(".canonical-output.staging-*/*.safetensors"))
shard.chmod(0o644)
with shard.open("r+b") as stream:
stream.seek(-1, os.SEEK_END)
value = stream.read(1)
stream.seek(-1, os.SEEK_END)
stream.write(bytes((value[0] ^ 1,)))
shard.chmod(0o444)
def validator(path: Path, request: ReloadValidationRequest) -> ReloadValidationEvidence:
if mode == "reload_failure" and rank == 1:
raise RuntimeError("private reload failure")
return _semantic_validator(path, request)
active_config = ExportConfig(**config_payload)
if mode == "timeout":
active_config = replace(active_config, stage_timeout_seconds=1)
if mode == "cleanup_failure":
persistence_module._cleanup_failed_staging = lambda *_args: None
if mode == "backup_cleanup_failure":
original_remove = persistence_module._remove_checkpoint_path
def fail_backup_cleanup(path: Path) -> None:
if ".backup-" in path.name:
raise OSError("private backup cleanup failure")
original_remove(path)
persistence_module._remove_checkpoint_path = fail_backup_cleanup
cancel_checks = [0]
def cancellation_requested() -> bool:
cancel_checks[0] += 1
if mode == "cancelled" and rank == 1:
return True
if mode == "timeout" and rank == 1 and cancel_checks[0] == 1:
time.sleep(1.2)
return False
first_error = None
if mode == "retry":
try:
run_distributed_export(
runtime,
verified,
active_config,
semantic_reload_validator=validator,
fault_injector=inject,
)
except BaseException as error:
first_error = getattr(error, "code", type(error).__name__)
active_config = ExportConfig(
**{
**config_payload,
"export_id": "2" * 32,
}
)
retry_error = None
try:
run_distributed_export(
runtime,
verified,
active_config,
semantic_reload_validator=validator,
)
except BaseException as error:
retry_error = getattr(error, "code", type(error).__name__)
queue.put(
(
rank,
"retry_refused",
{
"first_error": first_error,
"retry_error": retry_error,
"state": verified.excise_result.transaction.state.value,
"run_id": run_id,
},
)
)
return
result = run_distributed_export(
runtime,
verified,
active_config,
semantic_reload_validator=validator,
cancellation_requested=cancellation_requested,
fault_injector=None if mode == "retry" else inject,
)
queue.put(
(
rank,
"ok",
{
"digest": result.digest,
"state": verified.excise_result.transaction.state.value,
"tensor_names": tuple(item.tensor_name for item in result.tensor_records),
"writers": tuple(item.writer_rank for item in result.assignments),
"file_names": tuple(item.file_name for item in result.assignments),
"peak_host_rss": tuple(
item.peak_host_rss_bytes for item in result.rank_evidence
),
"peak_device": tuple(
item.peak_device_allocated_bytes for item in result.rank_evidence
),
"staging_bytes": tuple(
item.observed_staging_bytes for item in result.rank_evidence
),
"first_error": first_error,
"stages": (
tuple(item.stage.value for item in result.staged_messages),
tuple(item.stage.value for item in result.published_messages),
),
},
)
)
except BaseException as error:
if mode == "source_changed" and rank == 0 and source_config is not None:
assert original_source_config is not None
source_config.chmod(0o644)
source_config.write_text(original_source_config, encoding="utf-8")
source_config.chmod(0o444)
queue.put(
(
rank,
"error",
(
getattr(error, "code", type(error).__name__),
str(error),
),
)
)
finally:
if dist.is_initialized():
dist.destroy_process_group()
def _source(root: Path) -> tuple[Path, SourceIdentity]:
source = root / "source"
source.mkdir()
raw_config = {"architectures": ["TinyPublishedModel"], "model_type": "tiny"}
(source / "config.json").write_text(json.dumps(raw_config), encoding="utf-8")
(source / "tokenizer_config.json").write_text(
json.dumps({"tokenizer_class": "TinyTokenizer"}),
encoding="utf-8",
)
save_file(
{"source.weight": torch.ones((2, 2), dtype=torch.bfloat16)},
source / "model.safetensors",
metadata={"format": "pt"},
)
for path in source.iterdir():
path.chmod(0o444)
source.chmod(0o555)
return source, inspect_source(
source,
max_files=32,
max_total_bytes=1 << 20,
max_file_bytes=1 << 19,
timeout_seconds=10,
)
def _run_export(tmp_path: Path, mode: str, *, export_id: str = "1" * 32):
tmp_path.mkdir(parents=True, exist_ok=True)
source, identity = _source(tmp_path)
staging = tmp_path / "staging"
staging.mkdir()
if mode == "journal_reappeared":
(staging / "removed-journal").mkdir()
output = staging / "canonical-output"
output.mkdir()
(output / "prior.txt").write_text("prior-output", encoding="ascii")
source_digest_before = identity.source_digest
config = ExportConfig(
export_id=export_id,
output_name=output.name,
max_output_bytes=32 << 20,
max_staging_bytes=64 << 20,
max_host_bytes_per_rank=1 << 28,
max_device_bytes_per_rank=1 << 28,
semantic_reload_host_bytes_per_rank=64 << 20,
semantic_reload_device_bytes_per_rank=64 << 20,
max_tensor_bytes=1 << 18,
max_metadata_bytes=1 << 16,
max_metadata_files=16,
safety_margin_bytes=0,
stage_timeout_seconds=20,
)
rendezvous = tmp_path / "rendezvous"
queue: mp.Queue = mp.get_context("spawn").Queue()
processes = [
mp.get_context("spawn").Process(
target=_worker,
args=(
rank,
str(rendezvous),
str(source),
str(staging),
identity,
config.__dict__,
mode,
queue,
),
)
for rank in range(WORLD_SIZE)
]
for process in processes:
process.start()
for process in processes:
process.join(30)
if process.is_alive():
process.kill()
process.join(5)
records = []
for _ in range(WORLD_SIZE):
try:
records.append(queue.get(timeout=2))
except Empty:
break
assert [process.exitcode for process in processes] == [0, 0]
assert len(records) == WORLD_SIZE
assert inspect_source(
source,
max_files=32,
max_total_bytes=1 << 20,
max_file_bytes=1 << 19,
timeout_seconds=10,
).source_digest == source_digest_before
return sorted(records), output, staging
def _run_process_loss(tmp_path: Path, mode: str):
source, identity = _source(tmp_path)
staging = tmp_path / "staging"
staging.mkdir()
output = staging / "canonical-output"
output.mkdir()
(output / "prior.txt").write_text("prior-output", encoding="ascii")
config = ExportConfig(
export_id="1" * 32,
output_name=output.name,
max_output_bytes=32 << 20,
max_staging_bytes=64 << 20,
max_host_bytes_per_rank=1 << 28,
max_device_bytes_per_rank=1 << 28,
semantic_reload_host_bytes_per_rank=64 << 20,
semantic_reload_device_bytes_per_rank=64 << 20,
max_tensor_bytes=1 << 18,
max_metadata_bytes=1 << 16,
max_metadata_files=16,
safety_margin_bytes=0,
stage_timeout_seconds=20,
)
rendezvous = tmp_path / "rendezvous"
queue: mp.Queue = mp.get_context("spawn").Queue()
processes = [
mp.get_context("spawn").Process(
target=_worker,
args=(
rank,
str(rendezvous),
str(source),
str(staging),
identity,
config.__dict__,
mode,
queue,
),
)
for rank in range(WORLD_SIZE)
]
for process in processes:
process.start()
for process in processes:
process.join(30)
if process.is_alive():
process.kill()
process.join(5)
records = []
while True:
try:
records.append(queue.get(timeout=1))
except Empty:
break
return sorted(records), [item.exitcode for item in processes], output, staging
@pytest.mark.parametrize(
("field", "value"),
(
("run_id", "short"),
("world_size", 3),
("manifest", object()),
("tensor_device", "cpu"),
("source_path", Path("relative")),
("source_identity", object()),
("runtime_config_digest", "not-a-digest"),
("commit_sha", "not-a-commit"),
("max_source_files", True),
("available_host_bytes", -1),
),
)
def test_export_runtime_rejects_invalid_capability_fields(tmp_path, field, value):
runtime = _direct_runtime(tmp_path)
with pytest.raises(ContractError):
replace(runtime, **{field: value})
def test_existing_output_measurement_rejects_unsafe_or_oversized_trees(tmp_path):
assert export_module._path_bytes(tmp_path / "absent", maximum=1) == (0, 0)
regular_file = tmp_path / "regular-file"
regular_file.write_bytes(b"x")
with pytest.raises(Exception) as captured:
export_module._path_bytes(regular_file, maximum=1)
assert getattr(captured.value, "code", None) == "LMS_EXPORT_CONFIG_INVALID"
output = tmp_path / "output"
output.mkdir()
(output / "payload").write_bytes(b"xx")
assert export_module._path_bytes(output, maximum=2) == (2, 1)
with pytest.raises(Exception) as captured:
export_module._path_bytes(output, maximum=1)
assert getattr(captured.value, "code", None) == "LMS_EXPORT_RESOURCE_DENIED"
(output / "payload").unlink()
(output / "unsafe-link").symlink_to(regular_file)
with pytest.raises(Exception) as captured:
export_module._path_bytes(output, maximum=10)
assert getattr(captured.value, "code", None) == "LMS_EXPORT_CONFIG_INVALID"
@pytest.mark.parametrize(
("files", "config_changes"),
(
({"tokenizer.json": "{}"}, {}),
({"config.json": "{}"}, {}),
(
{
"config.json": "{}",
"tokenizer.json": "{}",
"distributed-export-manifest.json": "{}",
},
{},
),
(
{"config.json": "{}", "tokenizer.json": "{}", "extra.json": "{}"},
{"max_metadata_files": 2},
),
(
{"config.json": "{}", "tokenizer.json": "{}"},
{"max_metadata_bytes": 1},
),
),
)
def test_metadata_snapshot_rejects_incomplete_or_unbounded_sources(
tmp_path,
files,
config_changes,
):
runtime = _direct_runtime(tmp_path)
for relative, payload in files.items():
path = runtime.source_path / relative
path.write_text(payload, encoding="utf-8")
path.chmod(0o444)
config = replace(_export_config(), **config_changes)
with pytest.raises(Exception) as captured:
export_module._metadata_snapshots(runtime, config)
assert getattr(captured.value, "code", None) == "LMS_EXPORT_MANIFEST_INVALID"
def test_two_rank_export_reconstructs_validates_and_atomically_replaces(tmp_path):
records, output, staging = _run_export(tmp_path, "success")
assert [item[1] for item in records] == ["ok", "ok"], records
evidence = [item[2] for item in records]
assert len({item["digest"] for item in evidence}) == 1
assert all(item["state"] == "published" for item in evidence)
assert all(
item["tensor_names"] == (COLUMN_SHARDED, REPLICATED, SHARDED)
for item in evidence
)
assert all(item["writers"] == (0, 1, 0) for item in evidence)
assert all(
item["file_names"]
== (
"model-00001-of-00003.safetensors",
"model-00002-of-00003.safetensors",
"model-00003-of-00003.safetensors",
)
for item in evidence
)
assert all(min(item["peak_host_rss"]) > 0 for item in evidence)
assert all(item["peak_device"] == (0, 0) for item in evidence)
assert all(0 < max(item["staging_bytes"]) <= 32 << 20 for item in evidence)
assert all(
item["stages"] == (("staged", "staged"), ("published", "published"))
for item in evidence
)
assert not (output / "prior.txt").exists()
assert (output / "distributed-export-manifest.json").is_file()
manifest = json.loads((output / "distributed-export-manifest.json").read_text())
assert manifest["resource_admission"]["journal_bytes"] == 0
assert manifest["resource_admission"]["prior_output_bytes"] > 0
assert manifest["summary"]["excise_digest"]
assert manifest["summary"]["topology_digest"]
assert not tuple(staging.glob(".canonical-output.staging-*"))
with safe_open(output / "model-00003-of-00003.safetensors", framework="pt") as handle:
assert torch.equal(
handle.get_tensor(SHARDED),
torch.tensor(
((1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (7.0, 8.0)),
dtype=torch.bfloat16,
),
)
with safe_open(output / "model-00001-of-00003.safetensors", framework="pt") as handle:
assert torch.equal(
handle.get_tensor(COLUMN_SHARDED),
torch.tensor(
((10.0, 11.0), (20.0, 21.0), (30.0, 31.0), (40.0, 41.0)),
dtype=torch.bfloat16,
),
)
def test_failed_attempt_requires_a_fresh_run_and_export_identity(tmp_path):
records, output, staging = _run_export(tmp_path / "failed", "retry")
assert [item[1] for item in records] == ["retry_refused", "retry_refused"], records
assert {item[2]["first_error"] for item in records} == {"LMS_EXPORT_WRITE_FAILED"}
assert {item[2]["retry_error"] for item in records} == {"LMS_EXPORT_STATE_INVALID"}
assert {item[2]["state"] for item in records} == {"quarantined"}
assert (output / "prior.txt").read_text(encoding="ascii") == "prior-output"
assert not tuple(staging.glob(".canonical-output.staging-*"))
fresh, output, staging = _run_export(
tmp_path / "fresh",
"success",
export_id="2" * 32,
)
assert [item[1] for item in fresh] == ["ok", "ok"], fresh
manifest = json.loads((output / "distributed-export-manifest.json").read_text())
assert manifest["summary"]["export_id"] == "2" * 32
assert manifest["summary"]["run_id"] != records[0][2]["run_id"]
assert not tuple(staging.glob(".canonical-output.staging-*"))
@pytest.mark.parametrize(
("mode", "code"),
(
("cancelled", "LMS_EXPORT_CANCELLED"),
("writer_failure", "LMS_EXPORT_WRITE_FAILED"),
("index_failure", "LMS_EXPORT_INDEX_INVALID"),
("reload_failure", "LMS_EXPORT_RELOAD_FAILED"),
("promotion_failure", "LMS_EXPORT_PROMOTION_FAILED"),
("resource_denied", "LMS_EXPORT_RESOURCE_DENIED"),
("persistent_buffer", "LMS_EXPORT_MANIFEST_INVALID"),
("state_tamper", "LMS_EXPORT_STATE_INVALID"),
("tied_parameter", "LMS_EXPORT_MANIFEST_INVALID"),
("timeout", "LMS_EXPORT_TIMEOUT"),
("hash_corruption", "LMS_EXPORT_HASH_MISMATCH"),
("source_changed", "LMS_EXPORT_SOURCE_CHANGED"),
("journal_reappeared", "LMS_EXPORT_STATE_INVALID"),
("storage_changed", "LMS_EXPORT_STORAGE_CHANGED"),
),
)
def test_export_failures_preserve_prior_output_and_remove_nonloadable_staging(
tmp_path,
mode,
code,
):
records, output, staging = _run_export(tmp_path, mode)
assert [item[1] for item in records] == ["error", "error"], records
assert {item[2][0] for item in records} == {code}
assert (output / "prior.txt").read_text(encoding="ascii") == "prior-output"
assert not tuple(staging.glob(".canonical-output.staging-*"))
def test_cleanup_failure_quarantines_a_hidden_nonloadable_attempt(tmp_path):
records, output, staging = _run_export(tmp_path, "cleanup_failure")
assert [item[1] for item in records] == ["error", "error"], records
assert {item[2][0] for item in records} == {"LMS_EXPORT_CLEANUP_FAILED"}
assert (output / "prior.txt").read_text(encoding="ascii") == "prior-output"
attempts = tuple(staging.glob(".canonical-output.staging-*"))
assert len(attempts) == 1
assert (attempts[0] / ".obliteratus-export-incomplete").is_file()
assert not tuple(attempts[0].glob("*.safetensors"))
assert not (attempts[0] / "model.safetensors.index.json").exists()
def test_post_rename_confirmation_failure_is_uncertain_not_rollback(tmp_path):
records, output, staging = _run_export(tmp_path, "promotion_uncertain")
assert [item[1] for item in records] == ["error", "error"], records
assert {item[2][0] for item in records} == {"LMS_EXPORT_STATE_UNCERTAIN"}
assert not (output / "prior.txt").exists()
assert (output / "distributed-export-manifest.json").is_file()
assert not tuple(staging.glob(".canonical-output.staging-*"))
def test_backup_cleanup_failure_retains_replacement_and_prior_backup_as_uncertain(tmp_path):
records, output, staging = _run_export(tmp_path, "backup_cleanup_failure")
assert [item[1] for item in records] == ["error", "error"], records
assert {item[2][0] for item in records} == {"LMS_EXPORT_STATE_UNCERTAIN"}
assert not (output / "prior.txt").exists()
assert (output / "distributed-export-manifest.json").is_file()
backups = tuple(staging.glob(".canonical-output.backup-*"))
assert len(backups) == 1
assert (backups[0] / "prior.txt").read_text(encoding="ascii") == "prior-output"
assert not tuple(staging.glob(".canonical-output.staging-*"))
def test_rank_loss_aborts_the_coordinator_and_preserves_prior_output(tmp_path):
records, exit_codes, output, staging = _run_process_loss(tmp_path, "rank_exit")
assert exit_codes == [0, 19]
assert len(records) == 1
assert records[0][2][0] == "LMS_EXPORT_COLLECTIVE_FAILED"
assert (output / "prior.txt").read_text(encoding="ascii") == "prior-output"
assert not tuple(staging.glob(".canonical-output.staging-*"))
def test_coordinator_loss_leaves_prior_output_and_only_hidden_nonloadable_staging(tmp_path):
records, exit_codes, output, staging = _run_process_loss(tmp_path, "coordinator_exit")
assert exit_codes == [19, 0]
assert len(records) == 1
assert records[0][2][0] == "LMS_EXPORT_COLLECTIVE_FAILED"
assert (output / "prior.txt").read_text(encoding="ascii") == "prior-output"
attempts = tuple(staging.glob(".canonical-output.staging-*"))
assert len(attempts) == 1
with pytest.raises(Exception) as captured:
inspect_source(
attempts[0],
max_files=32,
max_total_bytes=32 << 20,
max_file_bytes=1 << 20,
timeout_seconds=10,
)
assert getattr(captured.value, "code", None) == "LMS_SOURCE_BOUNDARY_VIOLATION"