mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
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
1261 lines
55 KiB
Python
1261 lines
55 KiB
Python
"""Closed export contract tests for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from obliteratus.distributed.contracts import ContractError, contract_digest
|
|
from obliteratus.distributed.export_contracts import (
|
|
ExportConfig,
|
|
ExportSummary,
|
|
MetadataFileRecord,
|
|
RankExportEvidence,
|
|
ReloadValidationEvidence,
|
|
ReloadValidationRequest,
|
|
ResourceAdmission,
|
|
TensorExportRecord,
|
|
WriterAssignment,
|
|
)
|
|
|
|
|
|
DIGEST = "a" * 64
|
|
|
|
|
|
def _config() -> ExportConfig:
|
|
return ExportConfig(
|
|
export_id="1" * 32,
|
|
output_name="canonical-output",
|
|
max_output_bytes=1 << 20,
|
|
max_staging_bytes=2 << 20,
|
|
max_host_bytes_per_rank=1 << 20,
|
|
max_device_bytes_per_rank=1 << 20,
|
|
semantic_reload_host_bytes_per_rank=1 << 18,
|
|
semantic_reload_device_bytes_per_rank=1 << 18,
|
|
max_tensor_bytes=1 << 19,
|
|
max_metadata_bytes=1 << 16,
|
|
max_metadata_files=16,
|
|
safety_margin_bytes=0,
|
|
stage_timeout_seconds=30,
|
|
)
|
|
|
|
|
|
def _assignment() -> WriterAssignment:
|
|
return WriterAssignment(
|
|
schema_version=1,
|
|
sequence=0,
|
|
tensor_name="model.layers.0.weight",
|
|
writer_rank=0,
|
|
file_name="model.safetensors",
|
|
global_shape=(4, 8),
|
|
dtype="bfloat16",
|
|
placement="shard",
|
|
shard_dim=0,
|
|
logical_bytes=64,
|
|
world_size=2,
|
|
)
|
|
|
|
|
|
def test_export_config_is_fresh_bounded_and_canonical():
|
|
config = _config()
|
|
assert len(config.digest) == 64
|
|
assert config.digest == contract_digest(config)
|
|
assert config.digest != replace(config, export_id="2" * 32).digest
|
|
assert config.digest != replace(config, output_name="different-output").digest
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
(
|
|
("export_id", "a" * 31),
|
|
("output_name", ".hidden"),
|
|
("output_name", "../escape"),
|
|
("max_output_bytes", 0),
|
|
("max_staging_bytes", -1),
|
|
("max_host_bytes_per_rank", True),
|
|
("max_device_bytes_per_rank", 0),
|
|
("semantic_reload_host_bytes_per_rank", 0),
|
|
("semantic_reload_device_bytes_per_rank", 0),
|
|
("max_tensor_bytes", 0),
|
|
("max_metadata_bytes", 0),
|
|
("max_metadata_files", 0),
|
|
("safety_margin_bytes", -1),
|
|
("stage_timeout_seconds", 0),
|
|
("require_semantic_reload", False),
|
|
("schema_version", 2),
|
|
),
|
|
)
|
|
def test_export_config_rejects_open_unsafe_or_unbounded_values(field, value):
|
|
with pytest.raises(ContractError):
|
|
replace(_config(), **{field: value})
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
(
|
|
("sequence", -1),
|
|
("tensor_name", "bad/name"),
|
|
("writer_rank", 2),
|
|
("file_name", "nested/model.safetensors"),
|
|
("file_name", "model.bin"),
|
|
("global_shape", (4, 0)),
|
|
("dtype", "float32"),
|
|
("placement", "partial"),
|
|
("shard_dim", 2),
|
|
("logical_bytes", 63),
|
|
("world_size", 3),
|
|
("schema_version", 2),
|
|
),
|
|
)
|
|
def test_writer_assignment_rejects_ambiguous_or_inconsistent_layout(field, value):
|
|
with pytest.raises(ContractError):
|
|
replace(_assignment(), **{field: value})
|
|
|
|
|
|
def test_replicated_assignment_cannot_claim_a_shard_dimension():
|
|
with pytest.raises(ContractError, match="replicated"):
|
|
replace(_assignment(), placement="replicate")
|
|
replicated = replace(_assignment(), placement="replicate", shard_dim=None)
|
|
assert len(replicated.digest) == 64
|
|
|
|
|
|
def test_export_records_bind_files_resources_reload_and_rank_evidence():
|
|
assignment = _assignment()
|
|
tensor = TensorExportRecord(
|
|
schema_version=1,
|
|
sequence=0,
|
|
assignment_digest=assignment.digest,
|
|
tensor_name=assignment.tensor_name,
|
|
writer_rank=0,
|
|
file_name=assignment.file_name,
|
|
tensor_sha256="b" * 64,
|
|
file_sha256="c" * 64,
|
|
file_bytes=144,
|
|
)
|
|
metadata = MetadataFileRecord("tokenizer/tokenizer.json", 12, "d" * 64)
|
|
admission = ResourceAdmission(
|
|
logical_bytes=64,
|
|
journal_bytes=0,
|
|
metadata_bytes=12,
|
|
header_allowance_bytes=1024,
|
|
prior_output_bytes=4,
|
|
staging_required_bytes=1104,
|
|
largest_tensor_bytes=64,
|
|
largest_local_shard_bytes=32,
|
|
host_required_by_rank=(128, 64),
|
|
device_required_by_rank=(64, 64),
|
|
)
|
|
request = ReloadValidationRequest(
|
|
schema_version=1,
|
|
rank=0,
|
|
world_size=2,
|
|
export_identity_digest="e" * 64,
|
|
key_set_digest="f" * 64,
|
|
tensor_set_digest=tensor.digest,
|
|
config_digest="1" * 64,
|
|
tokenizer_digest="2" * 64,
|
|
)
|
|
reload = ReloadValidationEvidence(
|
|
schema_version=1,
|
|
rank=0,
|
|
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="3" * 64,
|
|
generation_digest="4" * 64,
|
|
passed=True,
|
|
)
|
|
rank = RankExportEvidence(
|
|
schema_version=1,
|
|
rank=0,
|
|
export_identity_digest=request.export_identity_digest,
|
|
assignment_set_digest=assignment.digest,
|
|
tensor_record_set_digest=tensor.digest,
|
|
tensors_written=1,
|
|
logical_bytes_written=64,
|
|
peak_host_rss_bytes=1024,
|
|
peak_device_allocated_bytes=0,
|
|
observed_staging_bytes=2048,
|
|
reload_evidence_digest=reload.digest,
|
|
)
|
|
assert all(
|
|
len(item.digest) == 64
|
|
for item in (tensor, admission, reload, rank)
|
|
)
|
|
assert metadata.sha256 == "d" * 64
|
|
with pytest.raises(ContractError, match="zero remaining"):
|
|
replace(admission, journal_bytes=1)
|
|
|
|
|
|
def test_reload_evidence_cannot_record_failure_or_identity_drift():
|
|
request = ReloadValidationRequest(
|
|
1,
|
|
0,
|
|
2,
|
|
"a" * 64,
|
|
"b" * 64,
|
|
"c" * 64,
|
|
"d" * 64,
|
|
"e" * 64,
|
|
)
|
|
passing = ReloadValidationEvidence(
|
|
1,
|
|
0,
|
|
request.export_identity_digest,
|
|
request.key_set_digest,
|
|
request.tensor_set_digest,
|
|
request.config_digest,
|
|
request.tokenizer_digest,
|
|
"f" * 64,
|
|
"1" * 64,
|
|
True,
|
|
)
|
|
with pytest.raises(ContractError, match="passing"):
|
|
replace(passing, passed=False)
|
|
with pytest.raises(ContractError, match="SHA-256"):
|
|
replace(passing, logits_digest="unknown")
|
|
|
|
|
|
def test_summary_binds_every_runtime_and_publication_identity():
|
|
summary = ExportSummary(
|
|
schema_version=1,
|
|
run_id="1" * 32,
|
|
export_id="2" * 32,
|
|
verification_digest="3" * 64,
|
|
config_digest="4" * 64,
|
|
runtime_config_digest="5" * 64,
|
|
hf_config_digest="6" * 64,
|
|
source_digest="7" * 64,
|
|
model_digest="8" * 64,
|
|
tokenizer_digest="9" * 64,
|
|
code_digest="a" * 64,
|
|
storage_digest="b" * 64,
|
|
commit_sha="c" * 40,
|
|
parameter_manifest_digest="d" * 64,
|
|
placement_plan_digest="e" * 64,
|
|
topology_digest="f" * 64,
|
|
excise_digest="0" * 64,
|
|
excise_config_digest="a" * 64,
|
|
operation_plan_digest="b" * 64,
|
|
postcondition_set_digest="c" * 64,
|
|
assignment_set_digest="f" * 64,
|
|
tensor_record_set_digest="0" * 64,
|
|
metadata_set_digest="1" * 64,
|
|
hf_index_digest="2" * 64,
|
|
admission_digest="3" * 64,
|
|
reload_evidence_set_digest="4" * 64,
|
|
rank_evidence_set_digest="5" * 64,
|
|
output_name="canonical-output",
|
|
tensor_count=2,
|
|
logical_bytes=128,
|
|
)
|
|
assert len(summary.digest) == 64
|
|
assert summary.digest != replace(summary, output_name="other-output").digest
|
|
with pytest.raises(ContractError, match="commit_sha"):
|
|
replace(summary, commit_sha="c" * 39)
|
|
|
|
|
|
@pytest.fixture
|
|
def mocked_export_world(tmp_path, monkeypatch):
|
|
"""Exchange actual primitive payloads between threads without a process group."""
|
|
import json
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from contextlib import contextmanager
|
|
from threading import Barrier, local
|
|
from types import SimpleNamespace
|
|
|
|
import torch
|
|
import obliteratus.distributed.consensus as consensus
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.model import model_config_digest
|
|
from obliteratus.distributed.preflight import storage_mount_digest
|
|
from tests.test_distributed_export_gloo import (
|
|
_TinyPublishedModel, _export_config, _manifest, _semantic_validator,
|
|
_source, _verified,
|
|
)
|
|
|
|
source, identity = _source(tmp_path)
|
|
# Ranks run as threads here; preflight's signal-based source deadline is
|
|
# main-thread only (the Gloo oracle covers it with real processes).
|
|
import contextlib
|
|
import obliteratus.distributed.preflight as preflight_module
|
|
|
|
monkeypatch.setattr(
|
|
preflight_module, "_stage_deadline",
|
|
lambda timeout_seconds, message: contextlib.nullcontext(),
|
|
)
|
|
staging = tmp_path / "staging"
|
|
staging.mkdir()
|
|
output = staging / "canonical-output"
|
|
output.mkdir()
|
|
(output / "prior.txt").write_text("prior-output", encoding="ascii")
|
|
control_group = object()
|
|
context = local()
|
|
barrier = Barrier(2, timeout=10)
|
|
payloads = [None, None]
|
|
traces, votes, transactions = [[], []], [[], []], [None, None]
|
|
original_step = export._local_step
|
|
|
|
@contextmanager
|
|
def step(runtime, sequence, **kwargs):
|
|
previous = getattr(context, "step", None)
|
|
with original_step(runtime, sequence, **kwargs):
|
|
context.step = sequence
|
|
try:
|
|
if getattr(context, "prepare_failure", None) == (sequence, context.rank):
|
|
raise MemoryError("private scalar record allocation failure")
|
|
yield
|
|
finally:
|
|
context.step = previous
|
|
|
|
monkeypatch.setattr(export, "_local_step", step)
|
|
# Older try/vote guards retain their own narrow scopes. Tag their bodies so
|
|
# injections target the real operation that the subsequent vote protects.
|
|
def tag_guard(name, sequence):
|
|
original = getattr(export, name)
|
|
|
|
def tagged(*args, **kwargs):
|
|
previous = getattr(context, "step", None)
|
|
context.step = args[-1] if name == "_check_continue" else kwargs.get("sequence", sequence)
|
|
try:
|
|
return original(*args, **kwargs)
|
|
finally:
|
|
context.step = previous
|
|
|
|
monkeypatch.setattr(export, name, tagged)
|
|
|
|
for name, sequence in (
|
|
("_revalidate_source", 17_000_010), ("_revalidate_storage", 17_000_011),
|
|
("_validate_model_structure", 17_000_030), ("_copy_snapshot", 17_000_898),
|
|
("_write_index", 17_000_899), ("_validate_tensor_tree", 17_000_900),
|
|
("_validate_final_tree", 17_000_905),
|
|
("_check_continue", None),
|
|
):
|
|
tag_guard(name, sequence)
|
|
monkeypatch.setattr(export, "runtime_from_live_session", lambda *args, **kwargs: context.runtime)
|
|
monkeypatch.setattr(
|
|
export, "classify_tp2_topology",
|
|
lambda *args: SimpleNamespace(layout=export.TP2DeploymentLayout.LOCAL_TP2),
|
|
)
|
|
monkeypatch.setattr(consensus, "_require_gloo", lambda group: (context.rank, 2))
|
|
monkeypatch.setattr(export.dist, "is_initialized", lambda: True)
|
|
monkeypatch.setattr(export.dist, "is_available", lambda: True)
|
|
monkeypatch.setattr(export.dist, "get_backend", lambda group: "gloo")
|
|
monkeypatch.setattr(export.dist, "get_rank", lambda group: context.rank)
|
|
monkeypatch.setattr(export.dist, "get_world_size", lambda group: 2)
|
|
|
|
def exchange(kind, tensor=None, output=None, source=None):
|
|
rank = context.rank
|
|
sequence = int(tensor[0]) if tensor is not None and kind == "gather" and tuple(tensor.shape) == (2,) else None
|
|
traces[rank].append((kind, sequence,
|
|
tuple(tensor.shape) if tensor is not None else (),
|
|
tensor.dtype if tensor is not None else None))
|
|
if sequence is not None:
|
|
votes[rank].append((sequence, bool(tensor[1])))
|
|
payloads[rank] = tensor.clone() if tensor is not None else None
|
|
barrier.wait()
|
|
if kind == "gather":
|
|
for target, value in zip(output, payloads, strict=True):
|
|
target.copy_(value)
|
|
elif kind == "broadcast":
|
|
tensor.copy_(payloads[source])
|
|
barrier.wait()
|
|
|
|
monkeypatch.setattr(export.dist, "all_gather", lambda targets, tensor, *, group:
|
|
exchange("gather", tensor, targets))
|
|
monkeypatch.setattr(export.dist, "broadcast", lambda tensor, *, src, group:
|
|
exchange("broadcast", tensor, source=src))
|
|
monkeypatch.setattr(export.dist, "barrier", lambda *, group: exchange("barrier"))
|
|
|
|
def inject(owner, name, *, sequence, rank=1):
|
|
original = getattr(owner, name)
|
|
|
|
def guarded(*args, **kwargs):
|
|
# Global patches (e.g. Path.__truediv__) also serve the main thread
|
|
# and unrelated callers without a mocked rank.
|
|
if getattr(context, "rank", None) == rank and getattr(context, "step", None) == sequence:
|
|
raise RuntimeError("private local export failure")
|
|
return original(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(owner, name, guarded)
|
|
|
|
def run(*, fault=None, cancellation=None, before_export=None, host_budget=1 << 30,
|
|
mode="export", prepare_failure=None, reload_transform=None):
|
|
def worker(rank):
|
|
context.rank, context.step = rank, None
|
|
context.phase, context.prepare_failure = "setup", prepare_failure
|
|
model = _TinyPublishedModel(rank)
|
|
manifest = _manifest(model, rank)
|
|
surgery_runtime, verified = _verified(model, manifest, rank, staging, "1" * 32)
|
|
verified.excise_result.transaction.runtime = replace(
|
|
surgery_runtime, control_group=control_group, data_group=control_group,
|
|
)
|
|
transactions[rank] = verified.excise_result.transaction
|
|
runtime = export.ExportRuntime(
|
|
run_id="1" * 32, rank=rank, world_size=2, model=model,
|
|
manifest=manifest, control_group=control_group,
|
|
tensor_device=torch.device("cpu"), source_path=source,
|
|
source_identity=identity, staging_path=staging,
|
|
runtime_config_digest="8" * 64,
|
|
hf_config_digest=model_config_digest(json.loads((source / "config.json").read_text())),
|
|
placement_plan_digest="9" * 64, topology_digest="7" * 64,
|
|
code_digest="a" * 64, storage_digest=storage_mount_digest(staging),
|
|
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=host_budget[rank] if isinstance(host_budget, tuple) else host_budget,
|
|
available_staging_bytes=1 << 30, available_device_bytes=1 << 30,
|
|
)
|
|
context.runtime = runtime
|
|
|
|
def validator(path, request):
|
|
evidence = _semantic_validator(path, request)
|
|
return reload_transform(rank, evidence) if reload_transform else evidence
|
|
|
|
try:
|
|
context.phase = "export"
|
|
if before_export:
|
|
before_export(rank, verified)
|
|
if mode == "live":
|
|
return export.run_live_distributed_export(
|
|
SimpleNamespace(control_group=control_group, config=object(),
|
|
launch=object(), preflight=object()), verified, _export_config(),
|
|
semantic_reload_validator=validator,
|
|
cancellation_requested=cancellation or (lambda: False),
|
|
)
|
|
return export.run_distributed_export(
|
|
runtime, verified, _export_config(),
|
|
semantic_reload_validator=validator,
|
|
cancellation_requested=cancellation or (lambda: False),
|
|
fault_injector=fault,
|
|
)
|
|
except BaseException as error:
|
|
return error
|
|
|
|
with ThreadPoolExecutor(max_workers=2) as executor:
|
|
results = list(executor.map(worker, range(2)))
|
|
assert traces[0] == traces[1], (traces, results)
|
|
return results
|
|
|
|
return SimpleNamespace(
|
|
run=run, inject=inject, context=context, traces=traces, votes=votes,
|
|
payloads=payloads,
|
|
transactions=transactions, export=export, torch=torch, output=output,
|
|
staging=staging, source=source,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"sequence,owner,name,rank,code",
|
|
[
|
|
(17_000_000, "export", "_group_identity", 1, "CONFIG_INVALID"),
|
|
(17_000_010, "export", "inspect_source", 1, "SOURCE_CHANGED"),
|
|
(17_000_011, "export", "storage_mount_digest", 1, "STORAGE_CHANGED"),
|
|
(17_000_030, "export", "_tensor_all_finite", 1, "MANIFEST_INVALID"),
|
|
(17_000_031, "export", "_assignments", 1, "MANIFEST_INVALID"),
|
|
(17_000_032, "export", "_metadata_snapshots", 1, "MANIFEST_INVALID"),
|
|
(17_000_032, "export", "_source_parser_host_bytes", 1, "MANIFEST_INVALID"),
|
|
(17_000_020, "export", "_path_bytes", 1, "RESOURCE_DENIED"),
|
|
(17_000_033, "export", "contract_digest", 1, "CONFIG_INVALID"),
|
|
(17_000_041, "path", "__truediv__", 1, "COLLECTIVE_FAILED"),
|
|
(18_000_000, "export", "_entry_for", 1, "MANIFEST_INVALID"),
|
|
(18_000_001, "export", "tensor_digest", 1, "MANIFEST_INVALID"),
|
|
(18_000_003, "json", "loads", 1, "STATE_INVALID"),
|
|
(18_000_004, "tensor", "to", 1, "RESOURCE_DENIED"),
|
|
(18_000_004, "torch", "empty", 0, "RESOURCE_DENIED"),
|
|
(18_000_010, "torch", "empty", 1, "RESOURCE_DENIED"),
|
|
(18_000_012, "torch", "empty", 0, "RESOURCE_DENIED"),
|
|
(18_000_011, "tensor", "copy_", 0, "WRITE_FAILED"),
|
|
(18_000_013, "tensor", "copy_", 0, "WRITE_FAILED"),
|
|
(18_000_006, "export", "_write_tensor", 0, "WRITE_FAILED"),
|
|
(18_000_007, "record", "__init__", 1, "HASH_MISMATCH"),
|
|
(17_000_898, "export", "_hash_file", 0, "SOURCE_CHANGED"),
|
|
(17_000_899, "export", "_write_json", 0, "INDEX_INVALID"),
|
|
(17_000_900, "export", "tensor_digest", 0, "HASH_MISMATCH"),
|
|
(17_000_905, "json", "loads", 0, "MANIFEST_INVALID"),
|
|
(17_000_050, "export", "export_aggregate_digest", 1, "MANIFEST_INVALID"),
|
|
(17_000_051, "request", "__init__", 1, "RELOAD_FAILED"),
|
|
(17_000_052, "export", "_staging_bytes", 1, "MANIFEST_INVALID"),
|
|
(17_001_001, "json", "loads", 1, "RELOAD_FAILED"),
|
|
(17_001_002, "json", "loads", 1, "MANIFEST_INVALID"),
|
|
(17_000_053, "summary", "__init__", 1, "MANIFEST_INVALID"),
|
|
(17_000_054, "export", "_manifest_payload", 1, "MANIFEST_INVALID"),
|
|
(17_000_918, "export", "advance_stage", 1, "COLLECTIVE_FAILED"),
|
|
(17_000_919, "export", "advance_stage", 1, "STATE_UNCERTAIN"),
|
|
(17_000_958, "stage", "from_bytes", 1, "COLLECTIVE_FAILED"),
|
|
(17_000_959, "stage", "from_bytes", 1, "STATE_UNCERTAIN"),
|
|
(17_000_060, "result", "__init__", 1, "STATE_UNCERTAIN"),
|
|
],
|
|
)
|
|
def test_one_rank_local_failure_has_identical_next_collective(
|
|
mocked_export_world, sequence, owner, name, rank, code,
|
|
):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
owners = {
|
|
"export": case.export, "torch": case.torch, "tensor": case.torch.Tensor,
|
|
"json": case.export.json,
|
|
"record": TensorExportRecord, "request": ReloadValidationRequest,
|
|
"summary": ExportSummary, "result": case.export.DistributedExportResult,
|
|
"path": case.export.Path,
|
|
"stage": case.export.StageMessage,
|
|
}
|
|
case.inject(owners[owner], name, sequence=sequence, rank=rank)
|
|
results = case.run()
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {f"LMS_EXPORT_{code}"}
|
|
assert all("private" not in str(error) for error in results)
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
for worker_rank in range(2):
|
|
assert (sequence, worker_rank != rank) in case.votes[worker_rank]
|
|
if sequence not in {17_000_919, 17_000_959, 17_000_060}:
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
assert not list(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
@pytest.mark.parametrize("sequence,code", [
|
|
(17_000_000, "CONFIG_INVALID"), (17_000_002, "STATE_INVALID"),
|
|
(17_000_055, "INDEX_INVALID"), (17_000_056, "INDEX_INVALID"),
|
|
(17_000_062, "STATE_UNCERTAIN"), (17_000_063, "STATE_UNCERTAIN"),
|
|
])
|
|
def test_literal_record_or_scalar_allocation_failure_uses_same_vote(mocked_export_world, sequence, code):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
results = case.run(prepare_failure=(sequence, 1))
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {f"LMS_EXPORT_{code}"}
|
|
assert all("private" not in str(error) for error in results)
|
|
assert (sequence, True) in case.votes[0]
|
|
assert (sequence, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
if sequence < 17_000_062:
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_boundary_path_resolution_failure_is_voted_before_source_collectives(mocked_export_world, monkeypatch):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
original = case.export.Path.resolve
|
|
|
|
def resolve(path, *args, **kwargs):
|
|
if (getattr(case.context, "phase", None) == "export" and case.context.rank == 1
|
|
and case.context.step is None):
|
|
raise OSError("private output boundary failure")
|
|
return original(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(case.export.Path, "resolve", resolve)
|
|
results = case.run()
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_CONFIG_INVALID"}
|
|
assert (17_000_003, True) in case.votes[0]
|
|
assert (17_000_003, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert not any(sequence == 17_000_010 for sequence, _ in case.votes[0])
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_local_tensor_root_mismatch_gets_shared_negative_post_gather_vote(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
|
|
def fault(point, sequence):
|
|
if point == "tensor_before" and sequence == 0 and case.context.rank == 1:
|
|
with case.torch.no_grad():
|
|
case.context.runtime.model.column_sharded.add_(1)
|
|
|
|
results = case.run(fault=fault)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_INVALID"}
|
|
assert (18_000_002, False) in case.votes[0]
|
|
assert (18_000_002, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_valid_one_rank_generation_drift_is_rejected_by_reload_evidence_vote(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
|
|
def transform(rank, evidence):
|
|
replacement = "a" * 64 if evidence.generation_digest != "a" * 64 else "b" * 64
|
|
return replace(evidence, generation_digest=replacement) if rank == 1 else evidence
|
|
|
|
results = case.run(reload_transform=transform)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_RELOAD_FAILED"}
|
|
assert (17_000_904, True) in case.votes[0]
|
|
assert (17_000_904, True) in case.votes[1]
|
|
assert (17_000_901, False) in case.votes[0]
|
|
assert (17_000_901, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
@pytest.mark.parametrize("point,rank", [
|
|
("tensor_before", 1), ("writer_before", 0), ("writer_after", 0),
|
|
("tensor_after", 1), ("staging_created", 0), ("index_written", 0),
|
|
("tensor_reload_complete", 0), ("semantic_reload_before", 1),
|
|
("semantic_reload_after", 1), ("manifest_validated", 0),
|
|
("promotion_before", 0), ("promotion_after", 0),
|
|
])
|
|
def test_one_rank_fault_hook_is_voted_and_never_returns_success(mocked_export_world, point, rank):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
|
|
def fault(current, sequence):
|
|
if current == point and case.context.rank == rank:
|
|
raise RuntimeError("private hook failure")
|
|
|
|
results = case.run(fault=fault)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert len({error.code for error in results}) == 1
|
|
assert all("private" not in str(error) for error in results)
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
if point != "promotion_after":
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_mocked_export_success_has_disjoint_unique_export_votes(mocked_export_world):
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
results = case.run()
|
|
assert all(isinstance(result, case.export.DistributedExportResult) for result in results), results
|
|
assert results[0].digest == results[1].digest
|
|
assert all(tx.state is ExciseTransactionState.PUBLISHED for tx in case.transactions)
|
|
sequences = [sequence for sequence, _ in case.votes[0] if sequence >= 17_000_000]
|
|
# Initial and final state revalidation use VERIFY's range; EXPORT does not.
|
|
assert sequences and all(sequence < 24_000_000 for sequence in sequences)
|
|
assert len(sequences) == len(set(sequences))
|
|
|
|
|
|
def test_one_rank_resource_denial_precedes_any_write(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
|
|
case = mocked_export_world
|
|
results = case.run(host_budget=(1 << 30, 1))
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_RESOURCE_DENIED"}
|
|
assert (17_000_021, True) in case.votes[0]
|
|
assert (17_000_021, False) in case.votes[1]
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
assert not list(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
def test_one_rank_live_journal_rejects_verified_capability_symmetrically(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
|
|
def before_export(rank, verified):
|
|
if rank == 1:
|
|
verified.excise_result.transaction.journal.active = True
|
|
|
|
results = case.run(before_export=before_export)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_INVALID"}
|
|
assert (17_000_002, True) in case.votes[0]
|
|
assert (17_000_002, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def test_live_runtime_preparation_failure_votes_then_quarantines_both(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
case.inject(case.export, "runtime_from_live_session", sequence=17_000_070, rank=1)
|
|
results = case.run(mode="live")
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_CONFIG_INVALID"}
|
|
# Both ranks admit the topology before runtime preparation inspects resources
|
|
# and storage; only rank 1's runtime preparation fails.
|
|
assert case.votes == [
|
|
[(17_000_071, True), (17_000_070, True)],
|
|
[(17_000_071, True), (17_000_070, False)],
|
|
]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
@pytest.mark.parametrize("interruption", ["cancellation", "deadline"])
|
|
def test_final_return_check_after_published_never_reports_success(
|
|
mocked_export_world, monkeypatch, interruption,
|
|
):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
original_clock = case.export.time.monotonic
|
|
|
|
def clock():
|
|
now = original_clock()
|
|
if (interruption == "deadline" and getattr(case.context, "rank", None) == 1
|
|
and getattr(case.context, "step", None) == 17_000_086):
|
|
return now + 3600
|
|
return now
|
|
|
|
monkeypatch.setattr(case.export.time, "monotonic", clock)
|
|
results = case.run(cancellation=lambda:
|
|
interruption == "cancellation" and case.context.rank == 1
|
|
and case.context.step == 17_000_086)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_UNCERTAIN"}
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
sequence = 17_000_086 if interruption == "cancellation" else 17_000_087
|
|
assert case.votes[0][-1] == (sequence, True)
|
|
assert case.votes[1][-1] == (sequence, False)
|
|
assert (case.output / "distributed-export-manifest.json").is_file()
|
|
|
|
|
|
@pytest.mark.parametrize("point,code", [
|
|
("manifest_validated", "LMS_EXPORT_CANCELLED"),
|
|
("promotion_after", "LMS_EXPORT_STATE_UNCERTAIN"),
|
|
])
|
|
def test_late_cancellation_recheck_never_returns_published(mocked_export_world, point, code):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
interrupted = [False]
|
|
|
|
def fault(current, sequence):
|
|
if current == point:
|
|
interrupted[0] = True
|
|
|
|
results = case.run(
|
|
fault=fault,
|
|
cancellation=lambda: interrupted[0] and case.context.rank == 1,
|
|
)
|
|
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)
|
|
if point == "manifest_validated":
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
else:
|
|
assert (case.output / "distributed-export-manifest.json").is_file()
|
|
|
|
|
|
def test_one_rank_promotion_record_validation_failure_is_uncertain(mocked_export_world, monkeypatch):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
original = case.export._broadcast_record
|
|
|
|
def broadcast(*args, **kwargs):
|
|
record = original(*args, **kwargs)
|
|
if case.context.rank == 1 and "promoted" in record:
|
|
record["uncertain"] = True
|
|
return record
|
|
|
|
monkeypatch.setattr(case.export, "_broadcast_record", broadcast)
|
|
results = case.run()
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_UNCERTAIN"}
|
|
assert (17_000_063, True) in case.votes[0]
|
|
assert (17_000_063, False) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
|
|
|
|
def test_uncertain_cleanup_is_voted_and_keeps_hidden_nonloadable_attempt(mocked_export_world, monkeypatch):
|
|
import obliteratus.persistence_contracts as persistence
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
monkeypatch.setattr(persistence, "_cleanup_failed_staging", lambda *_args: None)
|
|
|
|
def fault(point, sequence):
|
|
if point == "writer_before" and case.context.rank == 0:
|
|
raise RuntimeError("private writer failure")
|
|
|
|
results = case.run(fault=fault)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_CLEANUP_FAILED"}
|
|
assert (23_999_999, False) in case.votes[0]
|
|
assert (23_999_999, True) in case.votes[1]
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
attempts = tuple(case.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 (case.output / "prior.txt").read_text() == "prior-output"
|
|
|
|
|
|
def _corrupt_staged_export_file(case, artifact):
|
|
attempt, = case.staging.glob(".canonical-output.staging-*")
|
|
path = (
|
|
next(attempt.glob("*.safetensors")) if artifact == "tensor" else
|
|
attempt / "model.safetensors.index.json" if artifact == "index" else
|
|
attempt / "tokenizer_config.json"
|
|
)
|
|
original = path.read_bytes()
|
|
if artifact == "tensor":
|
|
# Change one BF16 fraction bit while preserving headers and file length.
|
|
modified = original[:-2] + bytes((original[-2] ^ 1,)) + original[-1:]
|
|
elif artifact == "index":
|
|
# JSON remains semantically identical, with the same file length.
|
|
modified = original.replace(b"\n", b" ", 1)
|
|
else:
|
|
modified = original.replace(b" ", b"\t", 1)
|
|
assert modified != original and len(modified) == len(original)
|
|
path.chmod(0o644)
|
|
path.write_bytes(modified)
|
|
path.chmod(0o444)
|
|
|
|
|
|
@pytest.mark.parametrize("point", [
|
|
"tensor_reload_complete", "semantic_reload_after", "manifest_validated",
|
|
])
|
|
@pytest.mark.parametrize("artifact", ["tensor", "index", "metadata"])
|
|
def test_post_reload_byte_corruption_never_publishes_clean_success(mocked_export_world, point, artifact):
|
|
from threading import Barrier
|
|
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
source_before = {path.name: path.read_bytes() for path in case.source.iterdir()}
|
|
reload_complete = Barrier(2, timeout=10)
|
|
|
|
def fault(current, sequence):
|
|
if current == point == "semantic_reload_after":
|
|
# Corrupt bytes only after both validators have produced evidence,
|
|
# so this case specifically exercises the later file-digest gate.
|
|
reload_complete.wait()
|
|
if current == point and case.context.rank == 0:
|
|
_corrupt_staged_export_file(case, artifact)
|
|
|
|
results = case.run(fault=fault)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
expected = "STATE_UNCERTAIN" if point == "manifest_validated" else "MANIFEST_INVALID"
|
|
assert {error.code for error in results} == {f"LMS_EXPORT_{expected}"}
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert {path.name: path.read_bytes() for path in case.source.iterdir()} == source_before
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
assert not tuple(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
def test_atomic_validation_failure_with_failed_cleanup_leaves_no_canonical_weight_names(
|
|
mocked_export_world, monkeypatch,
|
|
):
|
|
import obliteratus.persistence_contracts as persistence
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
source_before = {path.name: path.read_bytes() for path in case.source.iterdir()}
|
|
monkeypatch.setattr(persistence, "_cleanup_failed_staging", lambda *_args: None)
|
|
|
|
def fault(point, sequence):
|
|
if point == "manifest_validated" and case.context.rank == 0:
|
|
_corrupt_staged_export_file(case, "index")
|
|
|
|
results = case.run(fault=fault)
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_STATE_UNCERTAIN"}
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert {path.name: path.read_bytes() for path in case.source.iterdir()} == source_before
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
attempt, = case.staging.glob(".canonical-output.staging-*")
|
|
assert not tuple(attempt.glob("*.safetensors"))
|
|
assert not (attempt / "model.safetensors.index.json").exists()
|
|
assert tuple(attempt.glob("*.invalid"))
|
|
|
|
|
|
@pytest.mark.parametrize("retained_bytes", [0, 123_456])
|
|
@pytest.mark.parametrize("source_host_bytes", [0, 2 << 20])
|
|
def test_resource_admission_covers_each_host_overlap(tmp_path, monkeypatch, retained_bytes, source_host_bytes):
|
|
from types import SimpleNamespace
|
|
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.probe_contracts import TENSOR_DIGEST_WORKING_BYTES
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = replace(_direct_runtime(tmp_path), retained_host_bytes=retained_bytes)
|
|
config = _export_config()
|
|
assignments = export._assignments(runtime.manifest)
|
|
snapshots = tuple(SimpleNamespace(size_bytes=size) for size in (17, 31))
|
|
prior = runtime.staging_path / config.output_name
|
|
prior.mkdir()
|
|
(prior / "prior.bin").write_bytes(b"prior")
|
|
(prior / "nested").mkdir()
|
|
(prior / "nested" / "metadata").write_bytes(b"xx")
|
|
observed_votes = []
|
|
monkeypatch.setattr(export, "_collective_accept", lambda runtime, sequence, accepted, **kwargs:
|
|
observed_votes.append((sequence, accepted)))
|
|
admission = export._resource_admission(
|
|
runtime, config, assignments, snapshots, runtime.staging_path / config.output_name,
|
|
source_host_bytes=source_host_bytes,
|
|
)
|
|
assert observed_votes == [(17_000_020, True), (17_000_021, True)]
|
|
logical, local = admission.largest_tensor_bytes, admission.largest_local_shard_bytes
|
|
fixed = TENSOR_DIGEST_WORKING_BYTES + (16 << 20) + 4 * 65_540
|
|
record_bytes = (len(assignments) * (64 << 10) + len(snapshots) * (256 << 10)
|
|
+ 8 * admission.header_allowance_bytes + 3 * (64 << 10))
|
|
common = (fixed + record_bytes + retained_bytes + source_host_bytes
|
|
+ config.semantic_reload_host_bytes_per_rank + config.safety_margin_bytes)
|
|
for required in admission.host_required_by_rank:
|
|
# Reconstruction: complete tensor, local shard and receiving fragment.
|
|
assert required >= common + logical + 2 * local
|
|
# Serialization and immediate full reload overlap the complete tensor.
|
|
assert required >= common + 3 * logical
|
|
# A replicated CPU tensor can alias the source; admission still reserves it.
|
|
assert required >= common + 3 * logical + 2 * local
|
|
assert admission.metadata_bytes == 48
|
|
assert admission.prior_output_bytes == 7
|
|
assert admission.host_required_by_rank == (common + 3 * logical + 2 * local,) * 2
|
|
|
|
|
|
def test_source_parser_admission_sizes_headers_without_parsing_or_loading(tmp_path, monkeypatch):
|
|
import obliteratus.distributed.export as export
|
|
from tests.test_distributed_export_gloo import _direct_runtime
|
|
|
|
runtime = replace(_direct_runtime(tmp_path), max_source_file_bytes=1 << 30)
|
|
encoded_header_bytes = 16 << 20
|
|
# Sparse inert bytes provide an analytic large-header case without tensors
|
|
# or a JSON object graph. Only the eight-byte length prefix may be read.
|
|
weights = runtime.source_path / "model.safetensors"
|
|
with weights.open("wb") as stream:
|
|
stream.write(encoded_header_bytes.to_bytes(8, "little"))
|
|
stream.truncate(8 + encoded_header_bytes)
|
|
metadata = runtime.source_path / "config.json"
|
|
metadata.write_bytes(b"{}")
|
|
original_read = export.os.read
|
|
reads = []
|
|
|
|
def prefix_only(descriptor, size):
|
|
assert size == 8
|
|
reads.append(size)
|
|
return original_read(descriptor, size)
|
|
|
|
monkeypatch.setattr(export.os, "read", prefix_only)
|
|
monkeypatch.setattr(export.json, "loads", lambda *args, **kwargs:
|
|
pytest.fail("source parser admission must not parse JSON"))
|
|
assert export._source_parser_host_bytes(runtime) == 256 * (encoded_header_bytes + 2)
|
|
assert reads == [8]
|
|
assert not tuple(runtime.staging_path.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("announced,size", [(65 << 20, 65 << 20), (4096, 8)])
|
|
def test_source_parser_admission_refuses_oversized_or_truncated_headers(tmp_path, announced, size):
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.contracts import ContractError
|
|
from tests.test_distributed_export_gloo import _direct_runtime
|
|
|
|
runtime = replace(_direct_runtime(tmp_path), max_source_file_bytes=1 << 30)
|
|
with (runtime.source_path / "model.safetensors").open("wb") as stream:
|
|
stream.write(announced.to_bytes(8, "little"))
|
|
stream.truncate(8 + size)
|
|
with pytest.raises(ContractError):
|
|
export._source_parser_host_bytes(runtime)
|
|
assert not tuple(runtime.staging_path.iterdir())
|
|
|
|
|
|
def test_metadata_inventory_streams_tree_then_sorts_only_bounded_metadata(tmp_path, monkeypatch):
|
|
import obliteratus.distributed.export as export
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
nested = runtime.source_path / "nested"
|
|
nested.mkdir()
|
|
for path in (
|
|
runtime.source_path / "tokenizer_config.json", runtime.source_path / "config.json",
|
|
nested / "vocab.json", runtime.source_path / "model.safetensors",
|
|
):
|
|
path.write_bytes(b"{}")
|
|
path.chmod(0o444)
|
|
counter = [0]
|
|
monkeypatch.setattr(export.Path, "rglob", lambda *args, **kwargs:
|
|
pytest.fail("source inventory may not materialize Path.rglob"))
|
|
records = export._metadata_snapshots(runtime, _export_config(), tree_entry_counter=counter)
|
|
assert tuple(record.relative_path for record in records) == (
|
|
"config.json", "nested/vocab.json", "tokenizer_config.json",
|
|
)
|
|
assert counter == [5]
|
|
assert not tuple(runtime.staging_path.iterdir())
|
|
|
|
|
|
def test_overdeep_source_inventory_is_rejected_before_admission_or_write(mocked_export_world):
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
case.source.chmod(0o755)
|
|
path = case.source
|
|
for _ in range(65):
|
|
path = path / "d"
|
|
path.mkdir()
|
|
case.source.chmod(0o555)
|
|
results = case.run()
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_MANIFEST_INVALID"}
|
|
assert (17_000_032, False) in case.votes[0]
|
|
assert (17_000_032, False) in case.votes[1]
|
|
assert not any(sequence == 17_000_020 for sequence, _ in case.votes[0])
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
assert not tuple(case.staging.glob(".canonical-output.staging-*"))
|
|
|
|
|
|
def test_admission_accounts_large_logical_tensor_without_allocating_it(tmp_path, monkeypatch):
|
|
from types import SimpleNamespace
|
|
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.model_manifest import ParameterShard
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
entry = next(entry for entry in runtime.manifest.entries if entry.name == "sharded")
|
|
rows = 1 << 30
|
|
entry = replace(
|
|
entry, global_shape=(rows, 8), local_shape=(rows // 2, 8),
|
|
shard_end=rows // 2,
|
|
rank_shards=(
|
|
ParameterShard(0, 0, rows // 2, (rows // 2, 8)),
|
|
ParameterShard(1, rows // 2, rows, (rows // 2, 8)),
|
|
),
|
|
)
|
|
runtime = replace(
|
|
runtime, manifest=replace(runtime.manifest, entries=(entry,)),
|
|
available_host_bytes=1 << 42, available_staging_bytes=1 << 42,
|
|
)
|
|
config = replace(
|
|
_export_config(), max_tensor_bytes=1 << 34,
|
|
max_host_bytes_per_rank=1 << 42, max_output_bytes=1 << 42,
|
|
max_staging_bytes=1 << 42,
|
|
)
|
|
monkeypatch.setattr(export.shutil, "disk_usage", lambda path: SimpleNamespace(free=1 << 42))
|
|
monkeypatch.setattr(export, "_collective_accept", lambda runtime, sequence, accepted, **kwargs: None)
|
|
|
|
def forbidden_allocation(*args, **kwargs):
|
|
raise AssertionError("admission must remain analytic")
|
|
|
|
monkeypatch.setattr(export.torch, "empty", forbidden_allocation)
|
|
admission = export._resource_admission(
|
|
runtime, config, export._assignments(runtime.manifest), (),
|
|
runtime.staging_path / config.output_name,
|
|
)
|
|
assert admission.largest_tensor_bytes == 1 << 34
|
|
assert admission.largest_local_shard_bytes == 1 << 33
|
|
assert all(required >= (1 << 36) for required in admission.host_required_by_rank)
|
|
|
|
|
|
def test_admission_rejects_hf_index_over_reader_limit_before_writes(tmp_path, monkeypatch):
|
|
from types import SimpleNamespace
|
|
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from obliteratus.distributed.export_contracts import MAX_EXPORT_TENSORS
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = replace(_direct_runtime(tmp_path), available_host_bytes=1 << 42)
|
|
config = replace(
|
|
_export_config(), max_host_bytes_per_rank=1 << 42,
|
|
max_output_bytes=1 << 42, max_staging_bytes=1 << 42,
|
|
)
|
|
template = _assignment()
|
|
assignments = tuple(
|
|
replace(template, sequence=index, tensor_name=f"tensor_{index:05}_" + "w" * 498,
|
|
file_name=f"model-{index + 1:05}-of-{MAX_EXPORT_TENSORS:05}.safetensors")
|
|
for index in range(MAX_EXPORT_TENSORS)
|
|
)
|
|
monkeypatch.setattr(export.shutil, "disk_usage", lambda path: SimpleNamespace(free=1 << 42))
|
|
votes = []
|
|
|
|
def accept(runtime, sequence, accepted, **kwargs):
|
|
votes.append((sequence, accepted))
|
|
if not accepted:
|
|
raise RuntimeContractError(kwargs["code"], kwargs["message"])
|
|
|
|
monkeypatch.setattr(export, "_collective_accept", accept)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._resource_admission(
|
|
runtime, config, assignments, (), runtime.staging_path / config.output_name,
|
|
)
|
|
assert captured.value.code == "LMS_EXPORT_RESOURCE_DENIED"
|
|
assert votes == [(17_000_020, True), (17_000_021, False)]
|
|
assert not tuple(runtime.staging_path.iterdir())
|
|
|
|
|
|
def test_single_tensor_indexed_export_roundtrips_shared_source_reader(tmp_path):
|
|
import json
|
|
|
|
import obliteratus.distributed.export as export
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
entry = next(entry for entry in runtime.manifest.entries if entry.name == "replicated")
|
|
assignments = export._assignments(replace(runtime.manifest, entries=(entry,)))
|
|
assignment, = assignments
|
|
assert assignment.file_name == "model-00001-of-00001.safetensors"
|
|
record = export._write_tensor(runtime.staging_path, assignment, runtime.model.replicated)
|
|
export._write_index(runtime.staging_path, assignments)
|
|
export._validate_tensor_tree(runtime.staging_path, assignments, (record,), _export_config())
|
|
index = json.loads((runtime.staging_path / "model.safetensors.index.json").read_text())
|
|
assert index["weight_map"] == {"replicated": assignment.file_name}
|
|
assert index["metadata"]["total_size"] == assignment.logical_bytes
|
|
|
|
|
|
@pytest.mark.parametrize("corruption", ["announced_header", "tensor_size", "index_bytes"])
|
|
def test_tensor_reload_authenticates_bytes_before_header_parser(tmp_path, monkeypatch, corruption):
|
|
import obliteratus.distributed.export as export
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
|
|
runtime = _direct_runtime(tmp_path)
|
|
entry = next(entry for entry in runtime.manifest.entries if entry.name == "replicated")
|
|
assignments = export._assignments(replace(runtime.manifest, entries=(entry,)))
|
|
assignment, = assignments
|
|
record = export._write_tensor(runtime.staging_path, assignment, runtime.model.replicated)
|
|
index_digest = export._write_index(runtime.staging_path, assignments)
|
|
path = runtime.staging_path / (
|
|
"model.safetensors.index.json" if corruption == "index_bytes" else assignment.file_name
|
|
)
|
|
content = path.read_bytes()
|
|
if corruption == "announced_header":
|
|
# Announce a huge header using only eight changed bytes, without allocating it.
|
|
modified = (1 << 63).to_bytes(8, "little") + content[8:]
|
|
elif corruption == "tensor_size":
|
|
modified = content + b"x"
|
|
else:
|
|
modified = content.replace(b"\n", b" ", 1)
|
|
assert modified != content
|
|
path.chmod(0o644)
|
|
path.write_bytes(modified)
|
|
path.chmod(0o444)
|
|
parser_calls = []
|
|
|
|
def header_parser(*args, **kwargs):
|
|
parser_calls.append((args, kwargs))
|
|
raise AssertionError("unauthenticated header must not be parsed")
|
|
|
|
monkeypatch.setattr(export, "inspect_safetensors_metadata", header_parser)
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_tensor_tree(
|
|
runtime.staging_path, assignments, (record,), _export_config(),
|
|
index_digest=index_digest,
|
|
)
|
|
assert captured.value.code == "LMS_EXPORT_HASH_MISMATCH"
|
|
assert not parser_calls
|
|
|
|
|
|
def test_oversized_final_manifest_is_rejected_before_reading_its_bytes(tmp_path, monkeypatch):
|
|
import json
|
|
|
|
import obliteratus.distributed.export as export
|
|
import obliteratus.distributed.model_manifest as model_manifest
|
|
from obliteratus.distributed.contracts import RuntimeContractError
|
|
from tests.test_distributed_export_gloo import _export_config
|
|
|
|
expected = {
|
|
"tensor_records": [], "metadata_records": [],
|
|
"summary": {"hf_index_digest": DIGEST},
|
|
}
|
|
bound = 1 + len(json.dumps(expected, ensure_ascii=True, allow_nan=False, sort_keys=True, indent=2))
|
|
manifest = tmp_path / "distributed-export-manifest.json"
|
|
manifest.write_bytes(b" " * (bound + 1))
|
|
manifest.chmod(0o444)
|
|
read_calls = []
|
|
|
|
def read(descriptor, count):
|
|
read_calls.append(count)
|
|
raise AssertionError("oversized manifest must fail from its stat size")
|
|
|
|
with monkeypatch.context() as guarded:
|
|
guarded.setattr(model_manifest.os, "read", read)
|
|
guarded.setattr(export.Path, "read_text", lambda *args, **kwargs:
|
|
pytest.fail("final manifest may only use the bounded reader"))
|
|
with pytest.raises(RuntimeContractError) as captured:
|
|
export._validate_final_tree(tmp_path, {manifest.name}, expected, _export_config())
|
|
assert captured.value.code == "LMS_EXPORT_MANIFEST_INVALID"
|
|
assert not read_calls
|
|
|
|
|
|
@pytest.mark.parametrize("limit,domain", [(16_384, "assignments"), (32_768, "metadata")])
|
|
def test_export_aggregates_stream_every_advertised_entry(limit, domain):
|
|
from obliteratus.distributed.contracts import canonical_record
|
|
from obliteratus.distributed.export_contracts import export_aggregate_digest
|
|
|
|
def records():
|
|
for index in range(limit):
|
|
yield {"name": f"tensor_{index:05}", "sha256": DIGEST}
|
|
|
|
# The legacy tuple exceeded the single 64 KiB control-record bound.
|
|
with pytest.raises(ContractError):
|
|
canonical_record(tuple(records()))
|
|
digest = export_aggregate_digest(records(), domain=domain)
|
|
assert len(digest) == 64
|
|
assert digest == export_aggregate_digest(records(), domain=domain)
|
|
assert digest != export_aggregate_digest(records(), domain=domain + "-other")
|
|
assert digest != export_aggregate_digest(reversed(tuple(records())), domain=domain)
|
|
|
|
|
|
def test_export_aggregate_length_prefix_prevents_item_boundary_aliases():
|
|
from obliteratus.distributed.export_contracts import export_aggregate_digest
|
|
|
|
assert export_aggregate_digest(("ab", "c"), domain="keys") != export_aggregate_digest(
|
|
("a", "bc"), domain="keys",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("failed_rank", [0, 1])
|
|
def test_export_poison_record_fails_both_ranks_in_same_fixed_shape_exchange(
|
|
mocked_export_world, monkeypatch, failed_rank,
|
|
):
|
|
import obliteratus.distributed.consensus as consensus
|
|
from obliteratus.distributed.contracts import MAX_CONSENSUS_BYTES, RuntimeContractError
|
|
from obliteratus.distributed.surgery import ExciseTransactionState
|
|
|
|
case = mocked_export_world
|
|
original = consensus.canonical_record
|
|
injected = []
|
|
|
|
def serialize(value, **kwargs):
|
|
# VERIFY revalidation also gathers local_digest records. name_digest
|
|
# identifies EXPORT's tensor evidence, after revalidation has passed.
|
|
if (getattr(case.context, "rank", None) == failed_rank
|
|
and isinstance(value, dict) and "local_digest" in value
|
|
and "name_digest" in value and value.get("sequence") == 0):
|
|
injected.append(failed_rank)
|
|
raise RuntimeError("private record serialization failure")
|
|
return original(value, **kwargs)
|
|
|
|
monkeypatch.setattr(consensus, "canonical_record", serialize)
|
|
results = case.run()
|
|
assert all(isinstance(error, RuntimeContractError) for error in results), results
|
|
assert {error.code for error in results} == {"LMS_EXPORT_COLLECTIVE_FAILED"}
|
|
assert all("private" not in str(error) for error in results)
|
|
assert injected == [failed_rank]
|
|
for rank in range(2):
|
|
assert case.votes[rank][-1] == (18_000_001, True)
|
|
assert case.traces[rank][-1] == (
|
|
"gather", None, (MAX_CONSENSUS_BYTES + 4,), case.torch.uint8,
|
|
)
|
|
assert case.payloads[failed_rank][:4].tolist() == [255] * 4
|
|
peer = case.export.json.loads(consensus.decode_frame(case.payloads[1 - failed_rank]))
|
|
assert set(peer) == {"rank", "sequence", "name_digest", "local_digest"}
|
|
assert peer["rank"] == 1 - failed_rank
|
|
assert peer["sequence"] == 0
|
|
assert all(tx.state is ExciseTransactionState.QUARANTINED for tx in case.transactions)
|
|
assert (case.output / "prior.txt").read_text() == "prior-output"
|
|
assert not list(case.staging.glob(".canonical-output.staging-*"))
|