Files
OBLITERATUS/tests/test_distributed_rank_local_gloo.py

483 lines
25 KiB
Python

"""Actual two-rank Gloo candidate pipeline and symmetric failure matrix (LMS-E1)."""
from __future__ import annotations
import json
import hashlib
import multiprocessing as mp
import os
import time
from contextlib import contextmanager, nullcontext
from dataclasses import replace
from datetime import timedelta
from pathlib import Path
from types import SimpleNamespace
import pytest
import torch
import torch.distributed as dist
import obliteratus.distributed.consensus as consensus
import obliteratus.distributed.export as export_module
import obliteratus.distributed.rank_local as local
from obliteratus.distributed.contracts import ContractError, contract_digest
from obliteratus.distributed.export import ExportRuntime, run_distributed_export
from obliteratus.distributed.launcher import TorchrunEnvironment
from obliteratus.distributed.model import model_config_digest
from obliteratus.distributed.model_session import DataPlane, live_model_session
from obliteratus.distributed.model_topology import TP2DeploymentLayout
from obliteratus.distributed.preflight import LocalSnapshot, _attestation
from obliteratus.distributed.model_registry import LLAMA_BF16_TP2_PROFILE
from tests.test_distributed_export_gloo import (
_TinyPublishedModel, _export_config, _manifest, _semantic_validator, _source, _verified,
)
from tests.test_distributed_rank_local import install_review, rank_config, review_record
from tests.test_distributed_model_session_contracts import _exact
from tests.test_distributed_model_session import _protocol_mount
pytestmark = pytest.mark.gloo
def launch_for(config, rank):
return TorchrunEnvironment(rank=rank, local_rank=0, world_size=2, local_world_size=1,
group_rank=rank, role_rank=rank, role_world_size=2,
master_endpoint_digest="a" * 64, network_interface=config.network_interface,
rendezvous_id=config.rendezvous_id, run_id=config.run_id)
def snapshot_for(config, rank, identity):
return LocalSnapshot(host_identity=f"synthetic-host-{rank}", device_identity=f"synthetic-device-{rank}",
device_name="cpu", compute_capability="none", device_kind="cpu",
total_device_memory_bytes=4 << 30, free_device_memory_bytes=2 << 30,
total_host_memory_bytes=4 << 30, free_host_memory_bytes=2 << 30,
free_staging_bytes=2 << 30, storage_identity=config.for_rank(rank).storage_digest,
source=identity, software_versions=config.software_versions, commit_sha=config.commit_sha,
code_digest=config.code_digest)
def _candidate_worker(rank, rendezvous, config, identities, mode, queue):
monkeypatch = pytest.MonkeyPatch()
session_context = None
try:
dist.init_process_group("gloo", init_method=f"file://{rendezvous}", rank=rank, world_size=2,
timeout=timedelta(seconds=4))
launch = launch_for(config, rank)
snapshots = tuple(snapshot_for(config, item, identities[item]) for item in range(2))
attestations = tuple(_attestation(config, launch_for(config, item), snapshots[item]) for item in range(2))
install_review(monkeypatch, review_record(config, attestations))
local_config = config.for_rank(rank)
peer_root = config.for_rank(1 - rank).staging_path
original_open = os.open
def owned_open(path, flags, *args, **kwargs):
if isinstance(path, (str, Path)) and Path(path).is_absolute() and peer_root in (Path(path), *Path(path).parents):
raise AssertionError("candidate attempted to open a peer storage path")
return original_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, "open", owned_open)
def collect(_config, _launch):
if mode == "prep_failure" and rank == 1:
raise OSError("synthetic local preparation failure")
if mode == "prep_rank_death" and rank == 1:
os._exit(19)
return snapshots[rank]
if mode == "wrong_storage" and rank == 1:
snapshots = (snapshots[0], replace(snapshots[1], storage_identity="f" * 64))
if mode == "wrong_profile" and rank == 1:
local_config = replace(local_config, code_digest="f" * 64)
model = _TinyPublishedModel(rank)
manifest = _manifest(model, rank)
import obliteratus.distributed.model_session as session_module
monkeypatch.setattr(session_module, "control_group", lambda *_args: nullcontext())
monkeypatch.setattr(session_module, "runtime_diagnostic_guard", lambda **_kwargs: nullcontext())
@contextmanager
def plane(*_args, **_kwargs):
yield DataPlane(dist.group.WORLD, object(), "d" * 64)
def loader(*_args, **_kwargs):
if mode == "load_failure" and rank == 1:
raise ContractError("synthetic model preparation failed")
return SimpleNamespace(model=model, manifest=manifest, manifest_digest=manifest.digest,
local_manifest_digest=manifest.local_digest, source_identity=identities[rank],
config=SimpleNamespace(config_digest=config.model.hf_config_digest),
plan_digest=config.placement_plan_digest, profile=LLAMA_BF16_TP2_PROFILE)
session_context = live_model_session(local_config, launch,
_exact(source_mount=_protocol_mount(config.source_digest), session_timeout_seconds=60),
probes=SimpleNamespace(collect=collect), protocol_loader=loader, protocol_data_plane=plane)
session = session_context.__enter__()
preflight = session.preflight
_, verified = _verified(model, manifest, rank, local_config.staging_path, config.run_id)
runtime = ExportRuntime(run_id=config.run_id, rank=rank, world_size=2, model=model,
manifest=manifest, control_group=dist.group.WORLD, tensor_device=torch.device("cpu"),
source_path=local_config.source_path, source_identity=identities[rank],
staging_path=local_config.staging_path, runtime_config_digest=config.digest,
hf_config_digest=config.model.hf_config_digest, placement_plan_digest=config.placement_plan_digest,
topology_digest=contract_digest(preflight.topology), code_digest=config.code_digest,
storage_digest=local_config.storage_digest, commit_sha=config.commit_sha,
max_source_files=32, max_source_bytes=1 << 20, max_source_file_bytes=1 << 19,
source_timeout_seconds=5, available_host_bytes=1 if mode == "resource_denied" and rank == 1 else 2 << 30,
available_staging_bytes=2 << 30, available_device_bytes=2 << 30,
deployment_layout=TP2DeploymentLayout.TWO_HOST_TP2,
rank_local_config=local_config, rank_local_attestations=attestations,
rank_local_root_identity=preflight.rank_local_root_identities[rank])
stream_cancelled = [False]
stream_delayed = [False]
def inject(point, sequence):
if rank == 1 and point == "replication_before":
if mode == "stream_cancel":
stream_cancelled[0] = True
if mode == "stream_timeout" and not stream_delayed[0]:
stream_delayed[0] = True
time.sleep(1.1)
if mode == "writer_failure" and rank == 0 and point == "writer_before":
raise OSError("coordinator writer failed")
if mode == "writer_rank_death" and rank == 0 and point == "writer_before":
os._exit(19)
if mode == "replication_rank_death" and rank == 1 and point == "replication_before":
os._exit(19)
if mode == "reload_rank_death" and rank == 1 and point == "semantic_reload_before":
os._exit(19)
if mode == "root_replacement" and rank == 0 and point == "promotion_before":
root = local_config.staging_path
root.rename(root.with_name("retained-owned-root"))
root.mkdir(mode=0o700)
(root / "foreign").write_text("preserve")
if mode == "promotion_ack_lost" and rank == 0 and point == "promotion_after":
raise OSError("promotion acknowledgement lost")
if mode == "peer_cleanup_failure" and point == "peer_cleanup_before":
if rank == 1:
return
competitor = local.DurablePublication(local_config.staging_path, "canonical-output", "2" * 32)
try:
try:
competitor.__enter__()
except FileExistsError:
local.write_owned_json(local_config.staging_path / "competing-publisher.json", {"blocked": True})
else:
raise AssertionError("competing publisher crossed an undecided publication")
finally:
competitor.close()
if mode.startswith("crash_") and rank == 0 and point == "publication_" + mode.removeprefix("crash_"):
os._exit(19)
original_gather = local.validate_chunk
def corrupt_chunk(record, **kwargs):
if rank == 1 and mode in {"corrupt_chunk", "truncated_chunk", "replayed_chunk"}:
if mode == "corrupt_chunk":
record["chunk_digest"] = "0" * 64
elif mode == "truncated_chunk":
kwargs["data"] = kwargs["data"][:-1]
else:
record["offset"] += local.CHUNK_BYTES
return original_gather(record, **kwargs)
monkeypatch.setattr(local, "validate_chunk", corrupt_chunk)
if mode == "peer_cleanup_failure" and rank == 1:
original_remove = export_module.shutil.rmtree
def remove(path, *args, **kwargs):
if ".rank-local-validation-" in Path(path).name:
raise OSError("injected independent peer validation cleanup failure")
return original_remove(path, *args, **kwargs)
monkeypatch.setattr(export_module.shutil, "rmtree", remove)
if mode == "post_decision_ack_lost":
original_vote = export_module.unanimous_vote
def vote(sequence, accepted, **kwargs):
result = original_vote(sequence, accepted, **kwargs)
if rank == 1 and sequence == 17_000_077:
raise OSError("lost final local acknowledgement after durable decision")
return result
monkeypatch.setattr(export_module, "unanimous_vote", vote)
def validator(path, request):
if mode == "reload_failure" and rank == 1:
raise ContractError("independent rank reload failed")
evidence = _semantic_validator(path, request)
if mode == "reload_disagreement" and rank == 1:
return replace(evidence, logits_digest="f" * 64)
if mode == "tokenizer_corruption" and rank == 1:
target = path / "tokenizer_config.json"
target.chmod(0o600)
target.write_text("{}")
if mode == "index_corruption" and rank == 1:
target = path / "model.safetensors.index.json"
target.chmod(0o600)
target.write_text("{}")
return evidence
active_config = replace(_export_config(), stage_timeout_seconds=1) if mode == "stream_timeout" else _export_config()
result = run_distributed_export(runtime, verified, active_config,
semantic_reload_validator=validator, fault_injector=inject,
cancellation_requested=lambda: stream_cancelled[0])
queue.put((rank, "ok", {"digest": result.digest, "writers": [row.writer_rank for row in result.assignments],
"attempt": str(preflight.attempt_path), "reload": [row.logits_digest for row in result.reload_evidence]}))
except BaseException as error:
queue.put((rank, "error", getattr(error, "code", type(error).__name__)))
finally:
if session_context is not None:
session_context.__exit__(None, None, None)
monkeypatch.undo()
if dist.is_initialized():
dist.destroy_process_group()
def run_candidate(tmp_path, mode, *, worker=_candidate_worker):
sources = []
identities = []
for rank in range(2):
source_root = tmp_path / f"source-{rank}"
source_root.mkdir()
source, identity = _source(source_root)
sources.append(source)
identities.append(identity)
config = rank_config(tmp_path, tuple(sources))
config = replace(config, source_digest=identities[0].source_digest, model_digest=identities[0].model_digest,
tokenizer_digest=identities[0].tokenizer_digest,
model=replace(config.model, hf_config_digest=model_config_digest(json.loads((sources[0] / "config.json").read_text()))))
output = config.staging_path / "canonical-output"
output.mkdir()
(output / "prior.txt").write_text("prior")
context = mp.get_context("spawn")
queue = context.Queue()
children = [context.Process(target=worker,
args=(rank, str(tmp_path / "rendezvous"), config, tuple(identities), mode, queue)) for rank in range(2)]
for child in children:
child.start()
rows = []
expected = 1 if "rank_death" in mode or mode.startswith("crash_") else 2
try:
for _ in range(expected):
rows.append(queue.get(timeout=45))
for child in children:
child.join(10)
assert not child.is_alive()
return config, sorted(rows), [child.exitcode for child in children]
finally:
for child in children:
if child.is_alive():
child.terminate()
child.join(5)
queue.close()
def test_complete_rank_local_preflight_load_export_and_reload(tmp_path):
config, rows, exits = run_candidate(tmp_path, "success")
assert exits == [0, 0]
assert [row[1] for row in rows] == ["ok", "ok"]
assert rows[0][2]["digest"] == rows[1][2]["digest"]
assert rows[0][2]["writers"] == rows[1][2]["writers"] == [0, 0, 0]
assert rows[0][2]["attempt"] != rows[1][2]["attempt"]
assert len(set(rows[0][2]["reload"])) == 1
manifest = json.loads((config.staging_path / "canonical-output" / "distributed-export-manifest.json").read_text())
assert manifest["publication"]["physical_status"] == "not_qualified"
assert manifest["publication"]["evidence_tier"] == "LMS-E1"
assert not (config.for_rank(1).staging_path / "canonical-output").exists()
assert not list(config.for_rank(1).staging_path.glob(".*validation-*"))
@pytest.mark.parametrize("mode", ("prep_failure", "wrong_storage", "wrong_profile", "resource_denied",
"load_failure", "writer_failure", "corrupt_chunk", "truncated_chunk", "replayed_chunk", "reload_failure",
"reload_disagreement", "tokenizer_corruption", "index_corruption", "root_replacement", "promotion_ack_lost",
"stream_timeout", "stream_cancel"))
def test_candidate_failure_matrix_fails_closed(tmp_path, mode):
config, rows, exits = run_candidate(tmp_path, mode)
assert exits == [0, 0]
assert [row[1] for row in rows] == ["error", "error"]
if mode == "promotion_ack_lost":
assert {row[2] for row in rows} == {"LMS_EXPORT_STATE_UNCERTAIN"}
assert local.recovery_inventory(config.staging_path, "canonical-output", "1" * 32)["outcome"] == "uncertain"
elif mode == "root_replacement":
assert (config.staging_path / "foreign").read_text() == "preserve"
else:
assert (config.staging_path / "canonical-output" / "prior.txt").read_text() == "prior"
if mode in {"stream_timeout", "stream_cancel"}:
expected = "LMS_EXPORT_TIMEOUT" if mode == "stream_timeout" else "LMS_EXPORT_CANCELLED"
assert {row[2] for row in rows} == {expected}
def test_peer_cleanup_failure_retains_backup_and_fence_against_competing_publisher(tmp_path):
config, rows, exits = run_candidate(tmp_path, "peer_cleanup_failure")
assert exits == [0, 0]
assert [row[1:] for row in rows] == [("error", "LMS_EXPORT_STATE_UNCERTAIN")] * 2
assert json.loads((config.staging_path / "competing-publisher.json").read_text()) == {"blocked": True}
backup = config.staging_path / f".canonical-output.rank-local-backup-{'1' * 32}"
assert (backup / "prior.txt").read_text() == "prior"
state = local.recovery_inventory(config.staging_path, "canonical-output", "1" * 32)
assert state["decision"] == "undecided" and state["fence_present"]
competitor = local.DurablePublication(config.staging_path, "canonical-output", "3" * 32)
try:
with pytest.raises(FileExistsError):
competitor.__enter__()
finally:
competitor.close()
def test_post_decision_lost_ack_is_uncertain_without_claiming_atomic_returns(tmp_path):
config, rows, exits = run_candidate(tmp_path, "post_decision_ack_lost")
assert exits == [0, 0]
assert rows[0][1] == "ok"
assert rows[1][1:] == ("error", "LMS_EXPORT_STATE_UNCERTAIN")
state = local.recovery_inventory(config.staging_path, "canonical-output", "1" * 32)
assert state["decision"] == "commit" and state["outcome"] == "complete"
assert not state["fence_present"]
competitor = local.DurablePublication(config.staging_path, "canonical-output", "2" * 32)
try:
competitor.__enter__()
competitor.abort()
finally:
competitor.close()
@pytest.mark.parametrize("mode", ("prep_rank_death", "writer_rank_death", "replication_rank_death",
"reload_rank_death", "crash_PROMOTION_INTENT", "crash_BACKED_UP", "crash_RENAMED", "crash_PROMOTED", "crash_COMMIT_DECIDED", "crash_ACKNOWLEDGED"))
def test_rank_death_is_bounded_and_never_claims_success(tmp_path, mode):
config, rows, exits = run_candidate(tmp_path, mode)
assert sorted(exits) == [0, 19]
assert len(rows) == 1 and rows[0][1] == "error"
if mode.startswith("crash_"):
assert rows[0][2] == "LMS_EXPORT_STATE_UNCERTAIN"
assert local.recovery_inventory(config.staging_path, "canonical-output", "1" * 32)["outcome"] == "uncertain"
def _summary_worker(rank, rendezvous, mode, queue):
try:
dist.init_process_group("gloo", init_method=f"file://{rendezvous}", rank=rank, world_size=2,
timeout=timedelta(seconds=3))
if mode == "poison":
consensus.gloo_all_gather_records(b"not-a-record" if rank else {"ready": True})
else:
local.exchange_summary("1" * 32, ("b" if mode == "forged" and rank else "a") * 64,
rank, "LOADED", 3 if mode == "stale" and rank else 4, {})
queue.put((rank, "unexpected-success"))
except BaseException:
queue.put((rank, "refused"))
finally:
if dist.is_initialized():
dist.destroy_process_group()
@pytest.mark.parametrize("mode", ("poison", "forged", "stale"))
def test_collective_summary_rejects_poison_stale_and_forged_records(tmp_path, mode):
context = mp.get_context("spawn")
queue = context.Queue()
children = [context.Process(target=_summary_worker, args=(rank, str(tmp_path / "init"), mode, queue)) for rank in range(2)]
try:
for child in children:
child.start()
rows = [queue.get(timeout=30) for _ in children]
for child in children:
child.join(5)
assert child.exitcode == 0
assert sorted(rows) == [(0, "refused"), (1, "refused")]
finally:
for child in children:
if child.is_alive():
child.terminate()
child.join(5)
queue.close()
def _stream_worker(rank, rendezvous, root, digest, size, mode, queue):
patcher = pytest.MonkeyPatch()
offsets = []
relative = "nested/tensor.bin" if mode == "intermediate_symlink" else "tensor.bin"
started = None
try:
dist.init_process_group("gloo", init_method=f"file://{rendezvous}", rank=rank, world_size=2,
timeout=timedelta(seconds=3))
started = time.monotonic()
stopped = [False]
if mode == "intermediate_symlink" and rank == 1:
original_open = os.open
attacked = [False]
def open_file(path, flags, *args, **kwargs):
if not attacked[0] and flags & os.O_CREAT and str(path) in {"tensor.bin", str(Path(root) / relative)}:
attacked[0] = True
directory = Path(root) / "nested"
directory.rename(Path(root) / "retained-owned-parent")
directory.symlink_to(Path(root).parent / "outside-canary", target_is_directory=True)
return original_open(path, flags, *args, **kwargs)
patcher.setattr(os, "open", open_file)
def fault(point, offset):
offsets.append(offset)
if mode == "truncate" and rank == 0 and offset == local.CHUNK_BYTES:
with (Path(root) / relative).open("r+b") as stream:
stream.truncate(offset)
if mode == "timeout" and rank == 1 and offset == 0:
time.sleep(0.3)
if mode in {"cancel", "cancel_callback_failure"} and rank == 1 and offset == local.CHUNK_BYTES:
stopped[0] = True
def cancelled():
if mode == "cancel_callback_failure" and stopped[0]:
raise ValueError("injected cancellation callback failure")
return stopped[0]
local.replicate_file(Path(root), relative, size, digest, "a" * 64, rank,
group=dist.group.WORLD, sequence=100, fault=fault,
deadline=started + 0.15 if mode == "timeout" else None,
cancellation_requested=cancelled)
queue.put((rank, "ok", hashlib.sha256((Path(root) / relative).read_bytes()).hexdigest()))
except BaseException as error:
target = Path(root) / relative
queue.put((rank, "refused", {"code": getattr(error, "code", type(error).__name__),
"offsets": offsets, "size": target.stat().st_size if target.exists() else -1,
"elapsed": time.monotonic() - started if started is not None else None}))
finally:
patcher.undo()
if dist.is_initialized():
dist.destroy_process_group()
@pytest.mark.parametrize("mode", ("success", "truncate", "timeout", "cancel", "cancel_callback_failure", "intermediate_symlink"))
def test_streaming_multiple_fixed_frames_and_midstream_truncation(tmp_path, mode):
roots = [tmp_path / f"stream-{rank}" for rank in range(2)]
for root in roots:
root.mkdir(mode=0o700)
relative = "nested/tensor.bin" if mode == "intermediate_symlink" else "tensor.bin"
if mode == "intermediate_symlink":
for root in roots:
(root / "nested").mkdir(mode=0o700)
outside = tmp_path / "outside-canary"
outside.mkdir()
(outside / "canary.txt").write_text("unchanged outside directory")
content = bytes(range(256)) * 513
digest = hashlib.sha256(content).hexdigest()
(roots[0] / relative).write_bytes(content)
context = mp.get_context("spawn")
queue = context.Queue()
children = [context.Process(target=_stream_worker,
args=(rank, str(tmp_path / "stream-init"), str(roots[rank]), digest, len(content), mode, queue)) for rank in range(2)]
try:
for child in children:
child.start()
rows = sorted(queue.get(timeout=30) for _ in children)
for child in children:
child.join(5)
assert child.exitcode == 0
if mode == "success":
assert rows == [(0, "ok", digest), (1, "ok", digest)]
assert (roots[1] / "tensor.bin").read_bytes() == content
else:
assert [row[1] for row in rows] == ["refused", "refused"]
if mode in {"timeout", "cancel", "cancel_callback_failure"}:
expected_code = "LMS_EXPORT_TIMEOUT" if mode == "timeout" else "LMS_EXPORT_CANCELLED"
assert {row[2]["code"] for row in rows} == {expected_code}
assert rows[1][2]["size"] < len(content)
assert all(row[2]["elapsed"] < 6 for row in rows)
expected_offsets = [0] if mode == "timeout" else [0, local.CHUNK_BYTES]
assert all(row[2]["offsets"] == expected_offsets for row in rows)
assert rows[1][2]["size"] == (0 if mode == "timeout" else local.CHUNK_BYTES)
if mode == "intermediate_symlink":
assert (outside / "canary.txt").read_text() == "unchanged outside directory"
assert sorted(path.name for path in outside.iterdir()) == ["canary.txt"]
assert (roots[1] / "nested").is_symlink()
assert (roots[1] / "retained-owned-parent" / "tensor.bin").stat().st_size == 0
finally:
for child in children:
if child.is_alive():
child.terminate()
child.join(5)
queue.close()