mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Fixes eight reported defects in the native tensor-parallel path: - #278 VERIFY admits host memory in a symmetric vote before allocating and broadcasting baseline input tensors. - #280 require_record_consensus canonicalizes inside the poison-protected exchange and compares canonical payloads. - #282 EXCISE votes on fallible digest preparation before the next collective. - #285 preflight, model session, PROBE and EXCISE vote on rank-local preparation failures before the next collective, including the evidence-directory preparation that previously allocated before its all_reduce. - #275 postcondition and VERIFY digest sets stream the full declared operation range (up to MAX_EXCISE_OPERATIONS) while preserving existing identities. - #281 preflight source identity digests stream inventory records instead of using one 64 KiB control record. - #286 the evidence checker requires an integer schema_version and checkout/source inventories are bounded and streamed. - #276 standalone operation planning uses bounded chunked finite checks. Each fix has CPU/Gloo regression tests, including one-rank fault cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1104 lines
45 KiB
Python
1104 lines
45 KiB
Python
"""Pure contracts and private-journal tests for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import stat
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError, PlacementKind, RuntimeContractError, RuntimeStage, contract_digest,
|
|
)
|
|
from obliteratus.distributed import journal as journal_module
|
|
from obliteratus.distributed import surgery as surgery_module
|
|
from obliteratus.distributed.journal import RankJournal
|
|
from obliteratus.distributed.model_manifest import ManifestPlacement, SurgeryRole
|
|
from obliteratus.distributed.probe_contracts import (
|
|
LayerProbeArtifact,
|
|
ProbeDistillResult,
|
|
ProbeDistillSummary,
|
|
tensor_digest,
|
|
)
|
|
from obliteratus.distributed.surgery_contracts import (
|
|
ExciseConfig,
|
|
ExciseOperation,
|
|
ExciseOperationSummary,
|
|
ExciseSummary,
|
|
JournalEntry,
|
|
RankJournalSummary,
|
|
MAX_EXCISE_OPERATIONS,
|
|
excise_digest_set,
|
|
journal_entry_root,
|
|
operation_plan_digest,
|
|
)
|
|
|
|
|
|
DIGEST = "a" * 64
|
|
|
|
|
|
def _operation(**changes) -> ExciseOperation:
|
|
values = {
|
|
"schema_version": 1,
|
|
"sequence": 0,
|
|
"logical_name": "model.layers.0.self_attn.q_proj.weight",
|
|
"site": "model.layers.0",
|
|
"layer_index": 0,
|
|
"surgery_role": SurgeryRole.ATTENTION_INPUT,
|
|
"placement": PlacementKind.COLUMN_WISE,
|
|
"global_shape": (4, 4),
|
|
"local_shape": (2, 4),
|
|
"dtype": "float32",
|
|
"direction_axis": 1,
|
|
"shard_dim": 0,
|
|
"shard_start": 0,
|
|
"shard_end": 2,
|
|
"owner_ranks": (0, 1),
|
|
"tie_id": None,
|
|
"direction_digest": DIGEST,
|
|
"direction_count": 1,
|
|
"local_bytes": 32,
|
|
}
|
|
values.update(changes)
|
|
return ExciseOperation(**values)
|
|
|
|
|
|
def test_rank_journals_restore_and_cleanup_from_independent_host_parents(tmp_path):
|
|
"""EXCISE journals exchange summaries, never another host's file paths."""
|
|
journals = []
|
|
tensors = []
|
|
for rank in (0, 1):
|
|
parent = tmp_path / f"host-{rank}"
|
|
parent.mkdir(mode=0o700)
|
|
operation = _operation(shard_start=rank * 2, shard_end=(rank + 1) * 2)
|
|
tensor = torch.full((2, 4), float(rank + 1))
|
|
journal = RankJournal.create(
|
|
parent, run_id="1" * 32, rank=rank, operations=(operation,),
|
|
parameters={operation.logical_name: tensor}, max_payload_bytes=4096,
|
|
)
|
|
journals.append(journal)
|
|
tensors.append((operation.logical_name, tensor))
|
|
assert journals[0].root.parent != journals[1].root.parent
|
|
assert tuple(item.summary.rank for item in journals) == (0, 1)
|
|
for rank, (journal, (name, tensor)) in enumerate(zip(journals, tensors, strict=True)):
|
|
tensor.zero_()
|
|
journal.restore({name: tensor})
|
|
assert torch.equal(tensor, torch.full((2, 4), float(rank + 1)))
|
|
journal.cleanup()
|
|
assert not journal.root.exists()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
[
|
|
("schema_version", 2),
|
|
("roles", ()),
|
|
("roles", (SurgeryRole.MLP_OUTPUT, SurgeryRole.ATTENTION_INPUT)),
|
|
("norm_preserve", 1),
|
|
("regularization", float("nan")),
|
|
("regularization", 1.1),
|
|
("projection_row_fraction", 0.0),
|
|
("max_norm_ratio", 0.0),
|
|
("project_biases", True),
|
|
("max_journal_bytes", 0),
|
|
("max_update_bytes", False),
|
|
("safety_margin_bytes", -1),
|
|
("stage_timeout_seconds", 0),
|
|
],
|
|
)
|
|
def test_excise_config_rejects_open_unsupported_or_unbounded_values(field, value):
|
|
with pytest.raises(ContractError):
|
|
replace(ExciseConfig(), **{field: value})
|
|
|
|
|
|
def test_excise_config_digest_binds_every_option():
|
|
baseline = ExciseConfig(safety_margin_bytes=0)
|
|
changed = replace(baseline, norm_preserve=True)
|
|
assert baseline.digest != changed.digest
|
|
assert baseline.digest == ExciseConfig(safety_margin_bytes=0).digest
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("changes", "message"),
|
|
[
|
|
({"schema_version": 2}, "schema_version"),
|
|
({"sequence": -1}, "sequence"),
|
|
({"logical_name": "../secret"}, "logical_name"),
|
|
({"site": "model.layers.-1"}, "site"),
|
|
({"layer_index": 1}, "layer index"),
|
|
({"surgery_role": SurgeryRole.IMMUTABLE}, "not mutable"),
|
|
({"placement": "column_wise"}, "placement"),
|
|
({"global_shape": (4,)}, "matrix"),
|
|
({"local_shape": (3, 4)}, "local shape"),
|
|
({"dtype": "int8"}, "dtype"),
|
|
({"direction_axis": 2}, "direction_axis"),
|
|
({"shard_dim": 1}, "shard dimension"),
|
|
({"shard_start": 0, "shard_end": 1}, "uneven or incomplete"),
|
|
({"owner_ranks": (1, 0)}, "owner ranks"),
|
|
({"tie_id": "shared"}, "untied"),
|
|
({"direction_digest": "bad"}, "direction_digest"),
|
|
({"direction_count": 0}, "direction_count"),
|
|
({"local_bytes": 0}, "local_bytes"),
|
|
],
|
|
)
|
|
def test_operation_rejects_unknown_uneven_tied_or_changed_layouts(changes, message):
|
|
with pytest.raises(ContractError, match=message):
|
|
_operation(**changes)
|
|
|
|
|
|
def test_replicated_operation_requires_a_complete_unsharded_view():
|
|
operation = _operation(
|
|
placement=PlacementKind.REPLICATED,
|
|
local_shape=(4, 4),
|
|
shard_dim=None,
|
|
shard_start=0,
|
|
shard_end=0,
|
|
)
|
|
assert operation.logical_digest == replace(operation, local_bytes=64).logical_digest
|
|
with pytest.raises(ContractError, match="replicated"):
|
|
replace(operation, shard_dim=0)
|
|
|
|
|
|
def test_operation_and_journal_roots_require_complete_sequence_order():
|
|
first = _operation()
|
|
second = replace(first, sequence=1, logical_name="model.layers.0.mlp.gate_proj.weight")
|
|
assert len(operation_plan_digest((first, second))) == 64
|
|
with pytest.raises(ContractError, match="out of sequence"):
|
|
operation_plan_digest((second, first))
|
|
entry = JournalEntry(
|
|
schema_version=1,
|
|
sequence=0,
|
|
logical_name=first.logical_name,
|
|
local_shape=first.local_shape,
|
|
dtype=first.dtype,
|
|
placement=first.placement,
|
|
shard_start=first.shard_start,
|
|
shard_end=first.shard_end,
|
|
pristine_digest=DIGEST,
|
|
payload_digest="b" * 64,
|
|
relative_name="entry-0.safetensors",
|
|
payload_bytes=128,
|
|
)
|
|
assert len(journal_entry_root((entry,))) == 64
|
|
with pytest.raises(ContractError, match="out of sequence"):
|
|
journal_entry_root((replace(entry, sequence=1, relative_name="entry-1.safetensors"),))
|
|
|
|
|
|
def test_summary_records_are_digest_bound_and_value_free():
|
|
rank = RankJournalSummary(1, 0, 1, 32, 128, DIGEST)
|
|
operation = ExciseOperationSummary(1, 0, DIGEST, "b" * 64, "c" * 64, "c" * 64, (0.0).hex())
|
|
summary = ExciseSummary(
|
|
1,
|
|
"1" * 32,
|
|
DIGEST,
|
|
"b" * 64,
|
|
"c" * 64,
|
|
"d" * 64,
|
|
"e" * 64,
|
|
"f" * 64,
|
|
1,
|
|
32,
|
|
)
|
|
assert all(len(item.digest) == 64 for item in (rank, operation, summary))
|
|
assert "tensor(" not in repr(summary)
|
|
|
|
|
|
def test_rank_journal_is_private_exact_and_restores_before_cleanup(tmp_path):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
parameter = torch.nn.Parameter(torch.arange(8, dtype=torch.float32).reshape(2, 4))
|
|
original = tensor_digest(parameter)
|
|
journal = RankJournal.create(
|
|
tmp_path,
|
|
run_id="1" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: parameter},
|
|
max_payload_bytes=4096,
|
|
)
|
|
assert oct(journal.root.stat().st_mode & 0o777) == "0o700"
|
|
entry_path = journal.root / journal.entries[0].relative_name
|
|
assert oct(entry_path.stat().st_mode & 0o777) == "0o600"
|
|
assert str(tmp_path) not in repr(journal)
|
|
assert "tensor(" not in repr(journal)
|
|
with torch.no_grad():
|
|
parameter.add_(100)
|
|
journal.restore({operation.logical_name: parameter})
|
|
assert tensor_digest(parameter) == original
|
|
journal.cleanup()
|
|
assert not journal.root.exists()
|
|
journal.cleanup()
|
|
|
|
|
|
@pytest.mark.parametrize("mutation", ["corrupt", "extra", "permission"])
|
|
def test_rank_journal_refuses_corruption_unexpected_files_and_permission_drift(tmp_path, mutation):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
parameter = torch.nn.Parameter(torch.arange(8, dtype=torch.float32).reshape(2, 4))
|
|
journal = RankJournal.create(
|
|
tmp_path,
|
|
run_id=("2" if mutation == "corrupt" else "3" if mutation == "extra" else "4") * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: parameter},
|
|
max_payload_bytes=4096,
|
|
)
|
|
entry_path = journal.root / journal.entries[0].relative_name
|
|
if mutation == "corrupt":
|
|
payload = bytearray(entry_path.read_bytes())
|
|
payload[-1] ^= 1
|
|
entry_path.write_bytes(payload)
|
|
elif mutation == "extra":
|
|
(journal.root / "unexpected").touch(mode=0o600)
|
|
else:
|
|
os.chmod(entry_path, 0o640)
|
|
with pytest.raises(Exception, match="journal"):
|
|
journal.load_pristine()
|
|
|
|
|
|
def test_rank_journal_refuses_nonprivate_parent_and_payload_overflow(tmp_path):
|
|
operation = _operation()
|
|
parameter = torch.nn.Parameter(torch.ones(2, 4))
|
|
os.chmod(tmp_path, 0o755)
|
|
with pytest.raises(Exception, match="not private"):
|
|
RankJournal.create(
|
|
tmp_path,
|
|
run_id="5" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: parameter},
|
|
max_payload_bytes=4096,
|
|
)
|
|
os.chmod(tmp_path, 0o700)
|
|
with pytest.raises(Exception, match="admitted bound"):
|
|
RankJournal.create(
|
|
tmp_path,
|
|
run_id="6" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: parameter},
|
|
max_payload_bytes=1,
|
|
)
|
|
assert not (tmp_path / f".lms-journal-{'6' * 32}-rank-0").exists()
|
|
|
|
|
|
def test_rank_journal_never_removes_a_preexisting_run_directory(tmp_path):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
parameter = torch.nn.Parameter(torch.ones(2, 4))
|
|
existing = tmp_path / f".lms-journal-{'7' * 32}-rank-0"
|
|
existing.mkdir(mode=0o700)
|
|
sentinel = existing / "operator-owned"
|
|
sentinel.touch(mode=0o600)
|
|
|
|
with pytest.raises(Exception, match="creation failed"):
|
|
RankJournal.create(
|
|
tmp_path,
|
|
run_id="7" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: parameter},
|
|
max_payload_bytes=4096,
|
|
)
|
|
|
|
assert sentinel.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["privacy", "fstat"])
|
|
def test_rejected_journal_directory_open_closes_its_descriptor(tmp_path, monkeypatch, failure):
|
|
os.chmod(tmp_path, 0o755 if failure == "privacy" else 0o700)
|
|
opened = []
|
|
original_open = os.open
|
|
original_fstat = os.fstat
|
|
|
|
def record_open(*args, **kwargs):
|
|
descriptor = original_open(*args, **kwargs)
|
|
opened.append(descriptor)
|
|
return descriptor
|
|
|
|
def fail_fstat(descriptor):
|
|
raise OSError("injected fstat failure")
|
|
|
|
monkeypatch.setattr(journal_module.os, "open", record_open)
|
|
if failure == "fstat":
|
|
monkeypatch.setattr(journal_module.os, "fstat", fail_fstat)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
journal_module._open_private_directory(tmp_path)
|
|
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
|
|
assert len(opened) == 1
|
|
with pytest.raises(OSError):
|
|
original_fstat(opened[0])
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["journal_sync", "parent_sync", "constructor"])
|
|
def test_failed_complete_journal_creation_cleans_up_and_syncs_parent(tmp_path, monkeypatch, failure):
|
|
os.chmod(tmp_path, 0o700)
|
|
operations = (_operation(), _operation(sequence=1, logical_name="model.layers.0.mlp.gate_proj.weight"))
|
|
parent_inode = tmp_path.stat().st_ino
|
|
original_fsync = os.fsync
|
|
original_unlink = os.unlink
|
|
syncs = []
|
|
deleted = []
|
|
injected = False
|
|
|
|
def inject_fsync(descriptor):
|
|
nonlocal injected
|
|
item = os.fstat(descriptor)
|
|
if stat.S_ISDIR(item.st_mode):
|
|
syncs.append(item.st_ino)
|
|
target = item.st_ino == parent_inode if failure == "parent_sync" else item.st_ino != parent_inode
|
|
if failure != "constructor" and target and not injected:
|
|
injected = True
|
|
raise OSError("injected final sync failure")
|
|
original_fsync(descriptor)
|
|
|
|
def record_unlink(name, **kwargs):
|
|
deleted.append(name)
|
|
original_unlink(name, **kwargs)
|
|
|
|
def fail_constructor(*args, **kwargs):
|
|
raise ValueError("injected constructor failure")
|
|
|
|
monkeypatch.setattr(journal_module.os, "fsync", inject_fsync)
|
|
monkeypatch.setattr(journal_module.os, "unlink", record_unlink)
|
|
if failure == "constructor":
|
|
monkeypatch.setattr(RankJournal, "__init__", fail_constructor)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
RankJournal.create(
|
|
tmp_path,
|
|
run_id="8" * 32,
|
|
rank=0,
|
|
operations=operations,
|
|
parameters={item.logical_name: torch.ones(2, 4) for item in operations},
|
|
max_payload_bytes=4096,
|
|
)
|
|
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
|
|
assert deleted == ["entry-0.safetensors", "entry-1.safetensors"]
|
|
assert not (tmp_path / f".lms-journal-{'8' * 32}-rank-0").exists()
|
|
assert syncs[-1] == parent_inode
|
|
|
|
|
|
@pytest.mark.parametrize("cleanup_failure", ["unlink", "rmdir", "parent_sync"])
|
|
def test_failed_journal_creation_reports_cleanup_failure_after_all_deletion_attempts(
|
|
tmp_path, monkeypatch, cleanup_failure
|
|
):
|
|
os.chmod(tmp_path, 0o700)
|
|
operations = (_operation(), _operation(sequence=1, logical_name="model.layers.0.mlp.gate_proj.weight"))
|
|
parent_inode = tmp_path.stat().st_ino
|
|
original_fsync = os.fsync
|
|
original_unlink = os.unlink
|
|
original_rmdir = os.rmdir
|
|
deleted = []
|
|
parent_synced = False
|
|
|
|
def fail_journal_sync(descriptor):
|
|
nonlocal parent_synced
|
|
item = os.fstat(descriptor)
|
|
if stat.S_ISDIR(item.st_mode):
|
|
if item.st_ino != parent_inode:
|
|
raise OSError("injected final sync failure")
|
|
parent_synced = True
|
|
if cleanup_failure == "parent_sync":
|
|
raise OSError("injected cleanup parent sync failure")
|
|
original_fsync(descriptor)
|
|
|
|
def fail_one_unlink(name, **kwargs):
|
|
deleted.append(name)
|
|
if cleanup_failure == "unlink" and name == "entry-0.safetensors":
|
|
raise OSError("injected deletion failure")
|
|
original_unlink(name, **kwargs)
|
|
|
|
def fail_rmdir(*args, **kwargs):
|
|
if cleanup_failure == "rmdir":
|
|
raise OSError("injected directory deletion failure")
|
|
original_rmdir(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(journal_module.os, "fsync", fail_journal_sync)
|
|
monkeypatch.setattr(journal_module.os, "unlink", fail_one_unlink)
|
|
monkeypatch.setattr(journal_module.os, "rmdir", fail_rmdir)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
RankJournal.create(
|
|
tmp_path,
|
|
run_id="9" * 32,
|
|
rank=0,
|
|
operations=operations,
|
|
parameters={item.logical_name: torch.ones(2, 4) for item in operations},
|
|
max_payload_bytes=4096,
|
|
)
|
|
assert raised.value.code == "LMS_EXCISE_STATE_UNCERTAIN"
|
|
assert deleted == ["entry-0.safetensors", "entry-1.safetensors"]
|
|
assert parent_synced
|
|
|
|
|
|
def test_journal_creation_and_cleanup_sync_the_parent_after_directory_changes(tmp_path, monkeypatch):
|
|
os.chmod(tmp_path, 0o700)
|
|
parent_inode = tmp_path.stat().st_ino
|
|
original_fsync = os.fsync
|
|
original_rmdir = os.rmdir
|
|
events = []
|
|
|
|
def record_fsync(descriptor):
|
|
events.append(("sync", os.fstat(descriptor).st_ino))
|
|
original_fsync(descriptor)
|
|
|
|
def record_rmdir(*args, **kwargs):
|
|
original_rmdir(*args, **kwargs)
|
|
events.append(("rmdir", None))
|
|
|
|
monkeypatch.setattr(journal_module.os, "fsync", record_fsync)
|
|
monkeypatch.setattr(journal_module.os, "rmdir", record_rmdir)
|
|
operation = _operation()
|
|
journal = RankJournal.create(
|
|
tmp_path,
|
|
run_id="a" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: torch.ones(2, 4)},
|
|
max_payload_bytes=4096,
|
|
)
|
|
assert events[-2:] == [("sync", journal.root.stat().st_ino), ("sync", parent_inode)]
|
|
journal.cleanup()
|
|
assert events[-2:] == [("rmdir", None), ("sync", parent_inode)]
|
|
assert not journal.active
|
|
|
|
|
|
def test_journal_cleanup_does_not_report_success_if_parent_sync_fails(tmp_path, monkeypatch):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
journal = RankJournal.create(
|
|
tmp_path,
|
|
run_id="b" * 32,
|
|
rank=0,
|
|
operations=(operation,),
|
|
parameters={operation.logical_name: torch.ones(2, 4)},
|
|
max_payload_bytes=4096,
|
|
)
|
|
parent_inode = tmp_path.stat().st_ino
|
|
original_fsync = os.fsync
|
|
|
|
def fail_parent_sync(descriptor):
|
|
if os.fstat(descriptor).st_ino == parent_inode:
|
|
raise OSError("injected parent sync failure")
|
|
original_fsync(descriptor)
|
|
|
|
monkeypatch.setattr(journal_module.os, "fsync", fail_parent_sync)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
journal.cleanup()
|
|
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
|
|
assert journal.active
|
|
assert not journal.root.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["clone", "copy", "nonfinite", "unprojected"])
|
|
@pytest.mark.parametrize("remote", [False, True])
|
|
def test_detached_preparation_votes_before_any_subsequent_projection(monkeypatch, failure, remote):
|
|
parameter = torch.nn.Parameter(torch.ones(2, 4))
|
|
directions = torch.eye(4, dtype=torch.float32)[:2]
|
|
operation = _operation(direction_count=2)
|
|
runtime = SimpleNamespace(
|
|
rank=0, world_size=2, tensor_device=parameter.device,
|
|
data_group=object(), control_group=object(),
|
|
)
|
|
events = []
|
|
votes = []
|
|
original_clone = torch.Tensor.clone
|
|
original_to = torch.Tensor.to
|
|
rejection_index = {"clone": 0, "copy": 3, "nonfinite": 2, "unprojected": 2}[failure]
|
|
|
|
def clone(tensor, *args, **kwargs):
|
|
if not remote and failure == "clone" and tensor.data_ptr() == parameter.data_ptr():
|
|
raise RuntimeError("private clone payload")
|
|
return original_clone(tensor, *args, **kwargs)
|
|
|
|
def copy(tensor, *args, **kwargs):
|
|
if not remote and failure == "copy" and tensor.data_ptr() == directions[1].data_ptr():
|
|
raise RuntimeError("private copy payload")
|
|
return original_to(tensor, *args, **kwargs)
|
|
|
|
def vote(sequence, accepted, *, group):
|
|
assert group is runtime.control_group
|
|
votes.append((sequence, accepted))
|
|
events.append("vote")
|
|
return accepted and not (remote and len(votes) - 1 == rejection_index)
|
|
|
|
def project(weight, direction, placement, **kwargs):
|
|
events.append("project")
|
|
result_weight = (
|
|
torch.full_like(weight, float("nan"))
|
|
if not remote and failure == "nonfinite" else weight + 1
|
|
)
|
|
return SimpleNamespace(
|
|
projected=remote or failure != "unprojected",
|
|
weight=result_weight, coefficient_norm_sq=1.0,
|
|
)
|
|
|
|
monkeypatch.setattr(torch.Tensor, "clone", clone)
|
|
monkeypatch.setattr(torch.Tensor, "to", copy)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", vote)
|
|
monkeypatch.setattr(surgery_module, "distributed_project_weight", project)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module._project_detached(
|
|
runtime, operation, parameter, SimpleNamespace(directions=directions), ExciseConfig()
|
|
)
|
|
assert raised.value.code == "LMS_EXCISE_PREPARE_FAILED"
|
|
assert "private" not in str(raised.value)
|
|
assert len(votes) == rejection_index + 1
|
|
assert all(accepted for _sequence, accepted in votes[:-1])
|
|
assert votes[-1][1] is remote
|
|
assert len({sequence for sequence, _accepted in votes}) == len(votes)
|
|
assert events == {
|
|
"clone": ["vote"],
|
|
"copy": ["vote", "vote", "project", "vote", "vote"],
|
|
"nonfinite": ["vote", "vote", "project", "vote"],
|
|
"unprojected": ["vote", "vote", "project", "vote"],
|
|
}[failure]
|
|
torch.testing.assert_close(parameter, torch.ones(2, 4), rtol=0, atol=0)
|
|
|
|
|
|
def test_detached_preparation_accumulates_every_direction_after_unanimous_votes(monkeypatch):
|
|
parameter = torch.nn.Parameter(torch.ones(2, 4))
|
|
directions = torch.eye(4, dtype=torch.float32)[:2]
|
|
runtime = SimpleNamespace(
|
|
rank=0, world_size=2, tensor_device=parameter.device,
|
|
data_group=object(), control_group=object(),
|
|
)
|
|
votes = []
|
|
|
|
def vote(sequence, accepted, **kwargs):
|
|
votes.append((sequence, accepted))
|
|
return accepted
|
|
|
|
def project(weight, direction, placement, **kwargs):
|
|
assert weight.data_ptr() != parameter.data_ptr()
|
|
return SimpleNamespace(projected=True, weight=weight + 1, coefficient_norm_sq=2.0)
|
|
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", vote)
|
|
monkeypatch.setattr(surgery_module, "distributed_project_weight", project)
|
|
update, coefficient = surgery_module._project_detached(
|
|
runtime, _operation(direction_count=2), parameter,
|
|
SimpleNamespace(directions=directions), ExciseConfig(),
|
|
)
|
|
torch.testing.assert_close(update, torch.full_like(parameter, 3))
|
|
torch.testing.assert_close(parameter, torch.ones_like(parameter))
|
|
assert coefficient == 4.0
|
|
assert len(votes) == 5
|
|
assert all(accepted for _sequence, accepted in votes)
|
|
|
|
|
|
@pytest.mark.parametrize("tampered", [False, True])
|
|
def test_operation_plan_binds_directions_to_stored_distilled_summary(tampered):
|
|
operation = _operation()
|
|
parameter = torch.nn.Parameter(torch.ones(2, 4))
|
|
artifact = LayerProbeArtifact(
|
|
operation.site, 0, DIGEST, 0.0, 0.0, 1.0,
|
|
torch.ones(4), torch.zeros(4), torch.tensor([[1.0, 0.0, 0.0, 0.0]]),
|
|
)
|
|
summary = ProbeDistillSummary(
|
|
1, "c" * 32, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST, DIGEST,
|
|
1, 1, 4, (0.0).hex(), (0.0).hex(), (operation.site,), (artifact.summary_record(),),
|
|
)
|
|
distilled = ProbeDistillResult(summary, (artifact,), (), ())
|
|
original_digest = distilled.digest
|
|
entry = SimpleNamespace(
|
|
name=operation.logical_name, global_shape=operation.global_shape,
|
|
local_shape=operation.local_shape, dtype=operation.dtype,
|
|
placement=ManifestPlacement.SHARD, shard_dim=0, shard_start=0, shard_end=2,
|
|
owner_ranks=(0, 1), tie_id=None, hook_site=operation.site,
|
|
surgery_role=operation.surgery_role, local_value_digest=tensor_digest(parameter),
|
|
runtime_storage_kind=surgery_module.RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
|
|
)
|
|
runtime = SimpleNamespace(
|
|
run_id=summary.run_id, distilled_digest=original_digest,
|
|
rank=0, world_size=2, tensor_device=parameter.device,
|
|
manifest=SimpleNamespace(entries=(entry,)),
|
|
model=SimpleNamespace(named_parameters=lambda **kwargs: [(entry.name, parameter)]),
|
|
)
|
|
if tampered:
|
|
artifact.directions[0] = torch.tensor((0.0, 1.0, 0.0, 0.0))
|
|
assert distilled.digest == original_digest
|
|
assert tensor_digest(artifact.directions) == artifact.summary_record().direction_digest
|
|
with pytest.raises(ContractError, match="DISTILLED direction identity changed"):
|
|
surgery_module._build_operation_plan(runtime, distilled, ExciseConfig())
|
|
else:
|
|
plan, _parameters, _artifacts = surgery_module._build_operation_plan(
|
|
runtime, distilled, ExciseConfig()
|
|
)
|
|
assert plan[0].direction_digest == summary.layers[0].direction_digest
|
|
|
|
|
|
@pytest.mark.parametrize("cleanup_failed", [False, True])
|
|
def test_failed_journal_creation_votes_its_actual_cleanup_state(tmp_path, monkeypatch, cleanup_failed):
|
|
runtime = Mock(
|
|
spec=surgery_module.SurgeryRuntime, control_group=object(), run_id="d" * 32, rank=0,
|
|
distilled_digest=DIGEST, manifest=SimpleNamespace(digest=DIGEST), journal_parent=tmp_path,
|
|
)
|
|
distilled = Mock(spec=ProbeDistillResult, digest=DIGEST)
|
|
votes = []
|
|
for name in ("_group_identity", "require_consensus_digest", "_check_continue", "_resource_admission"):
|
|
monkeypatch.setattr(surgery_module, name, lambda *args, **kwargs: None)
|
|
monkeypatch.setattr(surgery_module, "_build_operation_plan", lambda *args: ((_operation(),), {}, {}))
|
|
|
|
def fail_create(*args, **kwargs):
|
|
code = "LMS_EXCISE_STATE_UNCERTAIN" if cleanup_failed else "LMS_EXCISE_JOURNAL_FAILED"
|
|
raise RuntimeContractError(code, "private cleanup payload")
|
|
|
|
def vote(sequence, accepted, **kwargs):
|
|
votes.append((sequence, accepted))
|
|
return accepted
|
|
|
|
monkeypatch.setattr(RankJournal, "create", fail_create)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", vote)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module.run_distributed_excise(runtime, distilled, ExciseConfig())
|
|
expected = "LMS_EXCISE_STATE_UNCERTAIN" if cleanup_failed else "LMS_EXCISE_JOURNAL_FAILED"
|
|
assert raised.value.code == expected
|
|
assert "private cleanup payload" not in str(raised.value)
|
|
assert votes == [(63_002, True), (63_001, True), (63_030, False), (67_901, not cleanup_failed)]
|
|
|
|
|
|
@pytest.mark.parametrize("close_target", ["journal", "parent", "both", "file"])
|
|
@pytest.mark.parametrize("cleanup_failure", [False, True])
|
|
def test_journal_creation_close_failures_keep_ownership_and_attempt_all_finalizers(
|
|
tmp_path, monkeypatch, close_target, cleanup_failure,
|
|
):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
original_close, original_unlink = os.close, os.unlink
|
|
parent_inode = tmp_path.stat().st_ino
|
|
closes, deleted = [], []
|
|
failed_targets = set()
|
|
|
|
def close(descriptor):
|
|
item = os.fstat(descriptor)
|
|
target = "file" if stat.S_ISREG(item.st_mode) else (
|
|
"parent" if item.st_ino == parent_inode else "journal"
|
|
)
|
|
closes.append(target)
|
|
# A close error may occur after the OS has released the descriptor.
|
|
original_close(descriptor)
|
|
if target not in failed_targets and (close_target == target or (
|
|
close_target == "both" and target in {"journal", "parent"}
|
|
)):
|
|
failed_targets.add(target)
|
|
raise OSError("private close failure payload")
|
|
|
|
def unlink(name, **kwargs):
|
|
deleted.append(name)
|
|
if cleanup_failure:
|
|
raise OSError("private cleanup failure payload")
|
|
original_unlink(name, **kwargs)
|
|
|
|
monkeypatch.setattr(journal_module.os, "close", close)
|
|
monkeypatch.setattr(journal_module.os, "unlink", unlink)
|
|
if cleanup_failure:
|
|
monkeypatch.setattr(journal_module, "_write_all", Mock(side_effect=OSError("write failed")))
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
RankJournal.create(
|
|
tmp_path, run_id="e" * 32, rank=0, operations=(operation,),
|
|
parameters={operation.logical_name: torch.ones(2, 4)}, max_payload_bytes=4096,
|
|
)
|
|
assert raised.value.code == "LMS_EXCISE_STATE_UNCERTAIN"
|
|
assert "private" not in str(raised.value)
|
|
assert closes[:3] == ["file", "journal", "parent"]
|
|
assert deleted == ["entry-0.safetensors"]
|
|
assert (tmp_path / f".lms-journal-{'e' * 32}-rank-0").exists() is cleanup_failure
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["pre_digest", "update_digest", "commit_digest", "statistics_digest"])
|
|
def test_excise_digest_failure_votes_before_aggregation_or_rollback_on_both_ranks(
|
|
tmp_path, monkeypatch, failure,
|
|
):
|
|
traces, votes = [[], []], [[], []]
|
|
current_rank = 0
|
|
rejected_sequence = {
|
|
"pre_digest": 64_050, "update_digest": 64_250, "commit_digest": 64_500,
|
|
"statistics_digest": 64_200,
|
|
}[failure]
|
|
parameter, update = torch.ones(2, 4), torch.zeros(2, 4)
|
|
operation = _operation()
|
|
journal = SimpleNamespace(summary=object(), entries=(SimpleNamespace(pristine_digest=DIGEST),))
|
|
distilled = Mock(spec=ProbeDistillResult, digest=DIGEST)
|
|
for name in ("_group_identity", "_check_continue", "_resource_admission"):
|
|
monkeypatch.setattr(surgery_module, name, lambda *args, **kwargs: None)
|
|
monkeypatch.setattr(surgery_module, "_build_operation_plan", lambda *args: (
|
|
(operation,), {operation.logical_name: parameter}, {operation.site: object()},
|
|
))
|
|
monkeypatch.setattr(RankJournal, "create", lambda *args, **kwargs: journal)
|
|
monkeypatch.setattr(surgery_module, "_project_detached", lambda *args: (update, 0.0))
|
|
|
|
def collective(name):
|
|
traces[current_rank].append(("control", name))
|
|
|
|
def consensus(*args, **kwargs):
|
|
collective("consensus")
|
|
|
|
def journal_digest(*args):
|
|
collective("journal")
|
|
return DIGEST
|
|
|
|
def logical_digest(*args, sequence, **kwargs):
|
|
collective(sequence)
|
|
return DIGEST
|
|
|
|
def emit(*args, **kwargs):
|
|
collective("stage")
|
|
return (object(),)
|
|
|
|
def decision(runtime, source):
|
|
collective("decision")
|
|
return {
|
|
"schema_version": 1, "action": "commit", "sequence": 0,
|
|
"operation_digest": operation.logical_digest,
|
|
"logical_pristine_digest": DIGEST, "logical_update_digest": DIGEST,
|
|
"coefficient_norm_sq_hex": float(0).hex(),
|
|
}
|
|
|
|
original_contract_digest = surgery_module.contract_digest
|
|
|
|
def contract_digest(record):
|
|
if (current_rank == 1 and failure == "statistics_digest"
|
|
and isinstance(record, dict) and set(record) == {"coefficient_norm_sq_hex"}):
|
|
raise RuntimeError("private statistics digest payload")
|
|
return original_contract_digest(record)
|
|
|
|
def digest(tensor):
|
|
target = (
|
|
tensor is update if failure == "update_digest" else tensor is parameter
|
|
)
|
|
after_commit = ("control", 64_400) in traces[current_rank]
|
|
if (current_rank == 1 and failure != "statistics_digest" and target
|
|
and (failure != "commit_digest" or after_commit)):
|
|
raise RuntimeError("private digest failure payload")
|
|
return DIGEST
|
|
|
|
def vote(sequence, accepted, **kwargs):
|
|
collective(sequence)
|
|
votes[current_rank].append((sequence, accepted))
|
|
return accepted and sequence != rejected_sequence
|
|
|
|
def restore(transaction, **kwargs):
|
|
collective("rollback")
|
|
transaction.state = surgery_module.ExciseTransactionState.ROLLED_BACK
|
|
|
|
monkeypatch.setattr(surgery_module, "require_consensus_digest", consensus)
|
|
monkeypatch.setattr(surgery_module, "_journal_set_digest", journal_digest)
|
|
monkeypatch.setattr(surgery_module, "_logical_digest", logical_digest)
|
|
monkeypatch.setattr(surgery_module, "_emit_stage", emit)
|
|
monkeypatch.setattr(surgery_module, "_broadcast_decision", decision)
|
|
monkeypatch.setattr(surgery_module, "tensor_digest", digest)
|
|
monkeypatch.setattr(surgery_module, "contract_digest", contract_digest)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", vote)
|
|
monkeypatch.setattr(surgery_module, "_restore_transaction", restore)
|
|
for rank in range(2):
|
|
current_rank = rank
|
|
runtime = Mock(
|
|
spec=surgery_module.SurgeryRuntime, control_group=object(), run_id="f" * 32,
|
|
rank=rank, distilled_digest=DIGEST, manifest=SimpleNamespace(digest=DIGEST),
|
|
journal_parent=tmp_path,
|
|
)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module.run_distributed_excise(runtime, distilled, ExciseConfig())
|
|
expected = "LMS_EXCISE_COMMIT_FAILED" if failure == "commit_digest" else "LMS_EXCISE_PREPARE_FAILED"
|
|
assert raised.value.code == expected
|
|
assert traces[0] == traces[1]
|
|
assert traces[0][-2:] == [("control", rejected_sequence), ("control", "rollback")]
|
|
assert votes[0][-1] == (rejected_sequence, True)
|
|
assert votes[1][-1] == (rejected_sequence, False)
|
|
|
|
|
|
def test_rollback_digest_failure_quarantines_both_ranks_before_aggregation(monkeypatch):
|
|
traces, votes = [[], []], [[], []]
|
|
current_rank = 0
|
|
operation = _operation()
|
|
monkeypatch.setattr(surgery_module, "_copy_pristine", lambda *args, **kwargs: None)
|
|
|
|
def digest(tensor):
|
|
if current_rank == 1:
|
|
raise RuntimeError("private rollback digest payload")
|
|
return DIGEST
|
|
|
|
def vote(sequence, accepted, **kwargs):
|
|
traces[current_rank].append(("control", sequence))
|
|
votes[current_rank].append((sequence, accepted))
|
|
return accepted and sequence != 67_050
|
|
|
|
def logical_digest(*args, **kwargs):
|
|
traces[current_rank].append(("control", "aggregate"))
|
|
return DIGEST
|
|
|
|
monkeypatch.setattr(surgery_module, "tensor_digest", digest)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", vote)
|
|
monkeypatch.setattr(surgery_module, "_logical_digest", logical_digest)
|
|
for rank in range(2):
|
|
current_rank = rank
|
|
transaction = surgery_module.ExciseTransaction(
|
|
runtime=SimpleNamespace(control_group=object()),
|
|
journal=SimpleNamespace(load_pristine=lambda: ()), operations=(operation,),
|
|
parameters={operation.logical_name: torch.ones(2, 4)}, pristine_roots=(DIGEST,),
|
|
operation_summaries=(), state=surgery_module.ExciseTransactionState.MUTATING,
|
|
)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module._restore_transaction(transaction, emit_lifecycle=False)
|
|
assert raised.value.code == "LMS_EXCISE_ROLLBACK_FAILED"
|
|
assert transaction.state is surgery_module.ExciseTransactionState.QUARANTINED
|
|
assert traces[0] == traces[1] == [("control", 67_000), ("control", 67_001), ("control", 67_040), ("control", 67_050)]
|
|
assert votes[0][-1] == (67_050, True)
|
|
assert votes[1][-1] == (67_050, False)
|
|
|
|
|
|
@pytest.fixture
|
|
def mocked_excise(tmp_path, monkeypatch):
|
|
"""Keep real commit, journal, cancellation checks, and rollback without sockets."""
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
parameter = torch.ones(2, 4)
|
|
pristine = parameter.clone()
|
|
update = torch.zeros_like(parameter)
|
|
runtime = Mock(
|
|
spec=surgery_module.SurgeryRuntime, control_group=object(), run_id="b" * 32,
|
|
rank=0, world_size=2, distilled_digest=DIGEST,
|
|
manifest=SimpleNamespace(digest=DIGEST), journal_parent=tmp_path,
|
|
)
|
|
distilled = Mock(spec=ProbeDistillResult, digest=DIGEST)
|
|
stages, transactions, checks = [], [], []
|
|
for name in ("_group_identity", "_resource_admission"):
|
|
monkeypatch.setattr(surgery_module, name, lambda *args, **kwargs: None)
|
|
monkeypatch.setattr(surgery_module, "_build_operation_plan", lambda *args: (
|
|
(operation,), {operation.logical_name: parameter}, {operation.site: object()},
|
|
))
|
|
monkeypatch.setattr(surgery_module, "_project_detached", lambda *args: (update.clone(), 0.0))
|
|
monkeypatch.setattr(surgery_module, "require_consensus_digest", lambda digest, **kwargs: digest)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", lambda sequence, accepted, **kwargs: accepted)
|
|
monkeypatch.setattr(surgery_module, "_journal_set_digest", lambda *args: DIGEST)
|
|
|
|
def logical_digest(*args, local_digest, **kwargs):
|
|
return contract_digest((local_digest, local_digest))
|
|
|
|
def decision(runtime, source):
|
|
return {
|
|
"schema_version": 1, "action": "commit", "sequence": operation.sequence,
|
|
"operation_digest": operation.logical_digest,
|
|
"logical_pristine_digest": logical_digest(local_digest=tensor_digest(pristine)),
|
|
"logical_update_digest": logical_digest(local_digest=tensor_digest(update)),
|
|
"coefficient_norm_sq_hex": float(0).hex(),
|
|
}
|
|
|
|
def emit(*args, requested, **kwargs):
|
|
stages.append(requested)
|
|
return (object(), object())
|
|
|
|
def gather(output, payload, **kwargs):
|
|
checks.append(payload.clone())
|
|
for target in output:
|
|
target.copy_(payload)
|
|
|
|
original_restore = surgery_module._restore_transaction
|
|
|
|
def restore(transaction, **kwargs):
|
|
transactions.append(transaction)
|
|
return original_restore(transaction, **kwargs)
|
|
|
|
monkeypatch.setattr(surgery_module, "_logical_digest", logical_digest)
|
|
monkeypatch.setattr(surgery_module, "_broadcast_decision", decision)
|
|
monkeypatch.setattr(surgery_module, "_emit_stage", emit)
|
|
monkeypatch.setattr(surgery_module.dist, "all_gather", gather)
|
|
monkeypatch.setattr(surgery_module, "_restore_transaction", restore)
|
|
return SimpleNamespace(
|
|
runtime=runtime, distilled=distilled, parameter=parameter, pristine=pristine,
|
|
update=update, stages=stages, transactions=transactions, checks=checks,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("interruption", ["cancellation", "deadline"])
|
|
def test_final_operation_interruption_rolls_back_both_ranks(mocked_excise, monkeypatch, interruption):
|
|
case = mocked_excise
|
|
traces = [[], []]
|
|
config = ExciseConfig(stage_timeout_seconds=1)
|
|
clock = 0.0
|
|
cancelled = False
|
|
monkeypatch.setattr(surgery_module.time, "monotonic", lambda: clock)
|
|
|
|
def gather(output, payload, **kwargs):
|
|
sequence = int(payload[0])
|
|
traces[case.runtime.rank].append((sequence, tuple(payload.shape)))
|
|
expected_flags = [0, 0]
|
|
if sequence == 66_000 and case.runtime.rank == 1:
|
|
expected_flags[0 if interruption == "cancellation" else 1] = 1
|
|
assert payload[1:].tolist() == expected_flags
|
|
for rank, target in enumerate(output):
|
|
target.copy_(payload)
|
|
# Only rank 1 is interrupted, during its last operation.
|
|
target[1:] = 0
|
|
if sequence == 66_000 and rank == 1:
|
|
target[1 if interruption == "cancellation" else 2] = 1
|
|
|
|
def fault(point, sequence):
|
|
nonlocal clock, cancelled
|
|
if point == "commit_after":
|
|
assert sequence == 0
|
|
assert torch.equal(case.parameter, case.update)
|
|
if case.runtime.rank == 1:
|
|
cancelled = interruption == "cancellation"
|
|
clock = 2.0 if interruption == "deadline" else 0.0
|
|
|
|
monkeypatch.setattr(surgery_module.dist, "all_gather", gather)
|
|
for rank in range(2):
|
|
case.runtime.rank = rank
|
|
clock, cancelled = 0.0, False
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module.run_distributed_excise(
|
|
case.runtime, case.distilled, config,
|
|
cancellation_requested=lambda: cancelled, fault_injector=fault,
|
|
)
|
|
expected = "LMS_EXCISE_CANCELLED" if interruption == "cancellation" else "LMS_EXCISE_TIMEOUT"
|
|
assert raised.value.code == expected
|
|
assert torch.equal(case.parameter, case.pristine)
|
|
transaction = case.transactions[-1]
|
|
assert transaction.state is surgery_module.ExciseTransactionState.ROLLED_BACK
|
|
assert not transaction.journal.active
|
|
assert not transaction.journal.root.exists()
|
|
assert case.stages[-2:] == [RuntimeStage.ABORTING, RuntimeStage.ABORTED]
|
|
assert traces[0] == traces[1] == [(sequence, (3,)) for sequence in (63_000, 63_020, 64_000, 66_000)]
|
|
|
|
|
|
@pytest.mark.parametrize("stage", [RuntimeStage.ABORTING, RuntimeStage.ABORTED])
|
|
@pytest.mark.parametrize("failure", ["lifecycle", "process"])
|
|
def test_public_rollback_lifecycle_failure_quarantines_capability(
|
|
mocked_excise, monkeypatch, stage, failure,
|
|
):
|
|
case = mocked_excise
|
|
result = surgery_module.run_distributed_excise(case.runtime, case.distilled, ExciseConfig())
|
|
assert result.transaction.state is surgery_module.ExciseTransactionState.COMMITTED
|
|
assert torch.equal(case.parameter, case.update)
|
|
original_emit = surgery_module._emit_stage
|
|
|
|
def emit(*args, requested, **kwargs):
|
|
if requested is stage:
|
|
if failure == "lifecycle":
|
|
raise RuntimeContractError("LMS_EXCISE_COLLECTIVE_FAILED", "lifecycle failed")
|
|
raise MemoryError("private lifecycle payload")
|
|
return original_emit(*args, requested=requested, **kwargs)
|
|
|
|
monkeypatch.setattr(surgery_module, "_emit_stage", emit)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
surgery_module.rollback_distributed_excise(result)
|
|
expected = "LMS_EXCISE_COLLECTIVE_FAILED" if failure == "lifecycle" else "LMS_EXCISE_STATE_UNCERTAIN"
|
|
assert raised.value.code == expected
|
|
assert "private" not in str(raised.value)
|
|
assert result.transaction.state is surgery_module.ExciseTransactionState.QUARANTINED
|
|
assert torch.equal(case.parameter, case.pristine)
|
|
assert result.transaction.journal.active
|
|
with pytest.raises(RuntimeContractError, match="not an active committed transaction"):
|
|
surgery_module.rollback_distributed_excise(result)
|
|
result.transaction.journal.cleanup()
|
|
|
|
|
|
@pytest.mark.parametrize("caller", ["logical_digest", "emit_stage", "vote"])
|
|
@pytest.mark.parametrize("error", [ContractError("a peer rank failed to prepare its consensus payload"), MemoryError()])
|
|
def test_surgery_consensus_preparation_failures_keep_closed_error_codes(monkeypatch, caller, error):
|
|
runtime = SimpleNamespace(control_group=object(), run_id="c" * 32, rank=0)
|
|
|
|
def fail(*args, **kwargs):
|
|
raise error
|
|
|
|
monkeypatch.setattr(surgery_module, "gloo_all_gather_records", fail)
|
|
monkeypatch.setattr(surgery_module, "unanimous_vote", fail)
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
if caller == "logical_digest":
|
|
surgery_module._logical_digest(runtime, sequence=0, local_digest=DIGEST, replicated=False)
|
|
elif caller == "emit_stage":
|
|
surgery_module._emit_stage(
|
|
runtime, current=RuntimeStage.PREPARED, requested=RuntimeStage.MUTATING,
|
|
sequence=6, identity_digest=DIGEST,
|
|
)
|
|
else:
|
|
surgery_module._collective_accept(
|
|
runtime, 0, True, code="LMS_EXCISE_PREPARE_FAILED", message="prepare failed",
|
|
)
|
|
assert raised.value.code == "LMS_EXCISE_COLLECTIVE_FAILED"
|
|
|
|
|
|
@pytest.mark.parametrize("close_target", ["journal", "parent", "both"])
|
|
@pytest.mark.parametrize("cleanup_failed", [False, True])
|
|
def test_journal_cleanup_close_errors_attempt_both_finalizers_and_remain_uncertain(
|
|
tmp_path, monkeypatch, close_target, cleanup_failed,
|
|
):
|
|
os.chmod(tmp_path, 0o700)
|
|
operation = _operation()
|
|
journal = RankJournal.create(
|
|
tmp_path, run_id="a" * 32, rank=0, operations=(operation,),
|
|
parameters={operation.logical_name: torch.ones(2, 4)}, max_payload_bytes=4096,
|
|
)
|
|
parent_inode = tmp_path.stat().st_ino
|
|
original_close = os.close
|
|
closes = []
|
|
|
|
def close(descriptor):
|
|
target = "parent" if os.fstat(descriptor).st_ino == parent_inode else "journal"
|
|
closes.append(target)
|
|
original_close(descriptor)
|
|
if close_target in {target, "both"}:
|
|
raise OSError("private close failure payload")
|
|
|
|
monkeypatch.setattr(journal_module.os, "close", close)
|
|
if cleanup_failed:
|
|
monkeypatch.setattr(journal_module.os, "unlink", Mock(side_effect=OSError("unlink failed")))
|
|
with pytest.raises(RuntimeContractError) as raised:
|
|
journal.cleanup()
|
|
assert raised.value.code == "LMS_EXCISE_STATE_UNCERTAIN"
|
|
assert closes == ["journal", "parent"]
|
|
assert journal.active
|
|
assert journal.root.exists() is cleanup_failed
|
|
|
|
|
|
@pytest.mark.parametrize("count", [978, 979, MAX_EXCISE_OPERATIONS])
|
|
def test_excise_digest_sets_stream_the_complete_declared_operation_range(count):
|
|
from obliteratus.distributed.verification import _postcondition_set_digest
|
|
|
|
digests = tuple(f"{index:064x}" for index in range(count))
|
|
expected = hashlib.sha256(json.dumps(digests, separators=(",", ":")).encode()).hexdigest()
|
|
assert excise_digest_set(iter(digests)) == expected
|
|
summaries = tuple(SimpleNamespace(digest=digest) for digest in digests)
|
|
assert _postcondition_set_digest(summaries) == expected
|
|
assert excise_digest_set(reversed(digests)) != expected
|
|
assert excise_digest_set(digests[:-1]) != expected
|
|
assert excise_digest_set((*digests[:-1], digests[0])) != expected
|
|
if count <= 978:
|
|
assert expected == contract_digest(digests)
|
|
|
|
|
|
def test_operation_plan_and_journal_roots_support_the_full_declared_count():
|
|
first = _operation()
|
|
operations = tuple(replace(first, sequence=index) for index in range(MAX_EXCISE_OPERATIONS))
|
|
assert operation_plan_digest(operations) == excise_digest_set(
|
|
item.logical_digest for item in operations
|
|
)
|
|
first_entry = JournalEntry(
|
|
schema_version=1, sequence=0, logical_name=first.logical_name,
|
|
local_shape=first.local_shape, dtype=first.dtype, placement=first.placement,
|
|
shard_start=first.shard_start, shard_end=first.shard_end,
|
|
pristine_digest=DIGEST, payload_digest="b" * 64,
|
|
relative_name="entry-0.safetensors", payload_bytes=128,
|
|
)
|
|
entries = tuple(replace(
|
|
first_entry, sequence=index, relative_name=f"entry-{index}.safetensors",
|
|
) for index in range(MAX_EXCISE_OPERATIONS))
|
|
assert journal_entry_root(entries) == excise_digest_set(item.digest for item in entries)
|
|
|
|
|
|
@pytest.mark.parametrize("values", [(), (True,), ("A" * 64,), ("0" * 63,), ("x" * 65_537,)])
|
|
def test_excise_digest_set_rejects_invalid_or_empty_records(values):
|
|
with pytest.raises(ContractError):
|
|
excise_digest_set(iter(values))
|
|
|
|
|
|
def test_excise_digest_set_refuses_one_past_the_operation_bound():
|
|
with pytest.raises(ContractError, match="operation bound"):
|
|
excise_digest_set(DIGEST for _ in range(MAX_EXCISE_OPERATIONS + 1))
|