mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Every rank-local step that can raise inside distributed_project_weight (validation, FP32 promotion, digests, reduction buffers) and the EXCISE pre-update, prepared-update and rollback digests is now followed by a unanimous control-group vote, so a one-rank failure cannot leave its peer in a different data collective. Callers without a control group keep their previous exceptions. RankJournal.create() keeps ownership until both directory descriptors close; every finalizer is attempted, and close failures surface as LMS_EXCISE_STATE_UNCERTAIN instead of masking an earlier cleanup failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
435 lines
18 KiB
Python
435 lines
18 KiB
Python
"""Private safetensors rollback journals for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import os
|
|
import stat
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from typing import Mapping, Sequence
|
|
|
|
import torch
|
|
from safetensors.torch import load as load_safetensors
|
|
from safetensors.torch import save as save_safetensors
|
|
|
|
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
|
|
from obliteratus.distributed.probe_contracts import tensor_digest
|
|
from obliteratus.distributed.surgery_contracts import (
|
|
ExciseOperation,
|
|
JournalEntry,
|
|
RankJournalSummary,
|
|
journal_entry_root,
|
|
)
|
|
|
|
|
|
_DIRECTORY_FLAGS = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
|
|
_FILE_FLAGS = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
|
|
|
|
|
|
def _identity(item: os.stat_result) -> tuple[int, int, int, int, int, int]:
|
|
return (
|
|
item.st_dev,
|
|
item.st_ino,
|
|
item.st_size,
|
|
item.st_mtime_ns,
|
|
item.st_mode,
|
|
item.st_nlink,
|
|
)
|
|
|
|
|
|
def _validate_private_directory(item: os.stat_result) -> None:
|
|
if (
|
|
not stat.S_ISDIR(item.st_mode)
|
|
or item.st_uid != os.getuid()
|
|
or item.st_nlink < 2
|
|
or stat.S_IMODE(item.st_mode) & 0o077
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal directory is not private"
|
|
)
|
|
|
|
|
|
def _open_private_directory(path: Path) -> int:
|
|
descriptor: int | None = None
|
|
accepted = False
|
|
try:
|
|
descriptor = os.open(path, _DIRECTORY_FLAGS)
|
|
_validate_private_directory(os.fstat(descriptor))
|
|
accepted = True
|
|
return descriptor
|
|
except RuntimeContractError:
|
|
raise
|
|
except OSError:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal directory is unavailable"
|
|
) from None
|
|
finally:
|
|
if descriptor is not None and not accepted:
|
|
os.close(descriptor)
|
|
|
|
|
|
def _write_all(descriptor: int, payload: bytes) -> None:
|
|
offset = 0
|
|
while offset < len(payload):
|
|
written = os.write(descriptor, payload[offset:])
|
|
if written <= 0:
|
|
raise OSError("short journal write")
|
|
offset += written
|
|
|
|
|
|
def _read_exact(descriptor: int, size: int) -> bytes:
|
|
chunks: list[bytes] = []
|
|
remaining = size
|
|
while remaining:
|
|
value = os.read(descriptor, min(remaining, 8 * 1024 * 1024))
|
|
if not value:
|
|
break
|
|
chunks.append(value)
|
|
remaining -= len(value)
|
|
payload = b"".join(chunks)
|
|
if len(payload) != size:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal payload is incomplete"
|
|
)
|
|
return payload
|
|
|
|
|
|
@dataclass
|
|
class RankJournal:
|
|
"""A private non-resumable capability containing one rank's pristine shards."""
|
|
|
|
root: Path = field(repr=False)
|
|
rank: int
|
|
entries: tuple[JournalEntry, ...]
|
|
tensor_bytes: int
|
|
active: bool = True
|
|
|
|
@classmethod
|
|
def create(
|
|
cls,
|
|
parent: Path,
|
|
*,
|
|
run_id: str,
|
|
rank: int,
|
|
operations: Sequence[ExciseOperation],
|
|
parameters: Mapping[str, torch.Tensor],
|
|
max_payload_bytes: int,
|
|
) -> "RankJournal":
|
|
"""Create every entry exclusively before any live parameter can change."""
|
|
|
|
if not isinstance(parent, Path) or not parent.is_absolute():
|
|
raise ContractError("journal parent must be an absolute Path")
|
|
if not isinstance(run_id, str) or len(run_id) != 32:
|
|
raise ContractError("journal run_id is invalid")
|
|
if rank not in {0, 1}:
|
|
raise ContractError("journal rank is outside the exact TP2 world")
|
|
ordered = tuple(operations)
|
|
if not ordered or tuple(item.sequence for item in ordered) != tuple(range(len(ordered))):
|
|
raise ContractError("journal operations are empty or out of sequence")
|
|
if set(parameters) != {item.logical_name for item in ordered}:
|
|
raise ContractError("journal parameter set does not match the operation plan")
|
|
if (
|
|
isinstance(max_payload_bytes, bool)
|
|
or not isinstance(max_payload_bytes, int)
|
|
or max_payload_bytes < 1
|
|
):
|
|
raise ContractError("journal payload bound is invalid")
|
|
|
|
parent_descriptor = _open_private_directory(parent)
|
|
directory_name = f".lms-journal-{run_id}-rank-{rank}"
|
|
root = parent / directory_name
|
|
journal_descriptor: int | None = None
|
|
directory_created = False
|
|
journal: RankJournal | None = None
|
|
close_failed = False
|
|
created_names: list[str] = []
|
|
entries: list[JournalEntry] = []
|
|
tensor_bytes = 0
|
|
payload_bytes = 0
|
|
try:
|
|
os.mkdir(directory_name, 0o700, dir_fd=parent_descriptor)
|
|
directory_created = True
|
|
journal_descriptor = os.open(directory_name, _DIRECTORY_FLAGS, dir_fd=parent_descriptor)
|
|
_validate_private_directory(os.fstat(journal_descriptor))
|
|
for operation in ordered:
|
|
tensor = parameters[operation.logical_name]
|
|
if (
|
|
not isinstance(tensor, torch.Tensor)
|
|
or tuple(tensor.shape) != operation.local_shape
|
|
or str(tensor.dtype).removeprefix("torch.") != operation.dtype
|
|
or not tensor.is_floating_point()
|
|
or tensor.layout is not torch.strided
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_MANIFEST_INVALID",
|
|
"journal parameter storage disagrees with the operation plan",
|
|
)
|
|
pristine = tensor.detach().cpu().contiguous()
|
|
local_tensor_bytes = pristine.numel() * pristine.element_size()
|
|
tensor_bytes += local_tensor_bytes
|
|
if tensor_bytes > max_payload_bytes:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_RESOURCE_DENIED", "rollback journal exceeds its admitted bound"
|
|
)
|
|
try:
|
|
payload = save_safetensors({"tensor": pristine})
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal serialization failed"
|
|
) from None
|
|
payload_bytes += len(payload)
|
|
if payload_bytes > max_payload_bytes:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_RESOURCE_DENIED", "rollback journal exceeds its admitted bound"
|
|
)
|
|
relative_name = f"entry-{operation.sequence}.safetensors"
|
|
descriptor = os.open(
|
|
relative_name,
|
|
os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0),
|
|
0o600,
|
|
dir_fd=journal_descriptor,
|
|
)
|
|
created_names.append(relative_name)
|
|
try:
|
|
_write_all(descriptor, payload)
|
|
os.fsync(descriptor)
|
|
item = os.fstat(descriptor)
|
|
if (
|
|
not stat.S_ISREG(item.st_mode)
|
|
or item.st_uid != os.getuid()
|
|
or item.st_nlink != 1
|
|
or stat.S_IMODE(item.st_mode) & 0o177
|
|
or item.st_size != len(payload)
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal file is not private"
|
|
)
|
|
finally:
|
|
try:
|
|
os.close(descriptor)
|
|
except OSError:
|
|
close_failed = True
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_STATE_UNCERTAIN", "rollback journal file close failed"
|
|
) from None
|
|
entries.append(
|
|
JournalEntry(
|
|
schema_version=1,
|
|
sequence=operation.sequence,
|
|
logical_name=operation.logical_name,
|
|
local_shape=operation.local_shape,
|
|
dtype=operation.dtype,
|
|
placement=operation.placement,
|
|
shard_start=operation.shard_start,
|
|
shard_end=operation.shard_end,
|
|
pristine_digest=tensor_digest(pristine),
|
|
payload_digest=hashlib.sha256(payload).hexdigest(),
|
|
relative_name=relative_name,
|
|
payload_bytes=len(payload),
|
|
)
|
|
)
|
|
del payload, pristine
|
|
os.fsync(journal_descriptor)
|
|
os.fsync(parent_descriptor)
|
|
journal = cls(root, rank, tuple(entries), tensor_bytes)
|
|
except (ContractError, RuntimeContractError):
|
|
raise
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal creation failed"
|
|
) from None
|
|
finally:
|
|
cleanup_failed = False
|
|
if journal_descriptor is not None and journal is None:
|
|
for name in created_names:
|
|
try:
|
|
os.unlink(name, dir_fd=journal_descriptor)
|
|
except OSError:
|
|
cleanup_failed = True
|
|
if directory_created and journal is None:
|
|
try:
|
|
os.rmdir(directory_name, dir_fd=parent_descriptor)
|
|
except OSError:
|
|
cleanup_failed = True
|
|
try:
|
|
os.fsync(parent_descriptor)
|
|
except OSError:
|
|
cleanup_failed = True
|
|
# Attempt both finalizers; neither may hide an earlier cleanup error.
|
|
for descriptor in (journal_descriptor, parent_descriptor):
|
|
if descriptor is not None:
|
|
try:
|
|
os.close(descriptor)
|
|
except OSError:
|
|
close_failed = True
|
|
if close_failed and journal is not None:
|
|
# Creation still owns the capability until all closes succeed.
|
|
try:
|
|
journal.cleanup()
|
|
except BaseException:
|
|
cleanup_failed = True
|
|
if cleanup_failed or close_failed:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_STATE_UNCERTAIN", "failed rollback journal cleanup did not finish"
|
|
) from None
|
|
assert journal is not None
|
|
return journal
|
|
|
|
@property
|
|
def summary(self) -> RankJournalSummary:
|
|
if not self.active:
|
|
raise ContractError("rollback journal is no longer active")
|
|
return RankJournalSummary(
|
|
schema_version=1,
|
|
rank=self.rank,
|
|
operation_count=len(self.entries),
|
|
tensor_bytes=self.tensor_bytes,
|
|
payload_bytes=sum(item.payload_bytes for item in self.entries),
|
|
entry_root_digest=journal_entry_root(self.entries),
|
|
)
|
|
|
|
def load_pristine(self) -> tuple[tuple[str, torch.Tensor], ...]:
|
|
"""Validate every entry before returning any tensor for restoration."""
|
|
|
|
if not self.active:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal is no longer active"
|
|
)
|
|
descriptor = _open_private_directory(self.root)
|
|
restored: list[tuple[str, torch.Tensor]] = []
|
|
try:
|
|
if set(os.listdir(descriptor)) != {item.relative_name for item in self.entries}:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal file set changed"
|
|
)
|
|
for entry in self.entries:
|
|
file_descriptor: int | None = None
|
|
try:
|
|
file_descriptor = os.open(entry.relative_name, _FILE_FLAGS, dir_fd=descriptor)
|
|
before = os.fstat(file_descriptor)
|
|
if (
|
|
not stat.S_ISREG(before.st_mode)
|
|
or before.st_uid != os.getuid()
|
|
or before.st_nlink != 1
|
|
or stat.S_IMODE(before.st_mode) & 0o177
|
|
or before.st_size != entry.payload_bytes
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal file identity changed"
|
|
)
|
|
payload = _read_exact(file_descriptor, entry.payload_bytes)
|
|
after = os.fstat(file_descriptor)
|
|
if _identity(before) != _identity(after):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal changed while read"
|
|
)
|
|
except RuntimeContractError:
|
|
raise
|
|
except OSError:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal file is unavailable"
|
|
) from None
|
|
finally:
|
|
if file_descriptor is not None:
|
|
os.close(file_descriptor)
|
|
if hashlib.sha256(payload).hexdigest() != entry.payload_digest:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal payload digest changed"
|
|
)
|
|
try:
|
|
decoded = load_safetensors(payload)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal payload is invalid"
|
|
) from None
|
|
if set(decoded) != {"tensor"}:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal tensor set changed"
|
|
)
|
|
tensor = decoded["tensor"]
|
|
if (
|
|
tuple(tensor.shape) != entry.local_shape
|
|
or str(tensor.dtype).removeprefix("torch.") != entry.dtype
|
|
or tensor_digest(tensor) != entry.pristine_digest
|
|
or not torch.isfinite(tensor).all()
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback journal tensor identity changed"
|
|
)
|
|
restored.append((entry.logical_name, tensor))
|
|
finally:
|
|
os.close(descriptor)
|
|
return tuple(restored)
|
|
|
|
def restore(self, parameters: Mapping[str, torch.Tensor]) -> None:
|
|
"""Restore every local shard after validating the entire private journal."""
|
|
|
|
if set(parameters) != {item.logical_name for item in self.entries}:
|
|
raise RuntimeContractError("LMS_EXCISE_ROLLBACK_FAILED", "rollback target set changed")
|
|
pristine = self.load_pristine()
|
|
try:
|
|
with torch.no_grad():
|
|
for name, tensor in pristine:
|
|
target = parameters[name]
|
|
if tuple(target.shape) != tuple(tensor.shape) or str(target.dtype).removeprefix(
|
|
"torch."
|
|
) != str(tensor.dtype).removeprefix("torch."):
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback target storage changed"
|
|
)
|
|
target.copy_(tensor.to(device=target.device))
|
|
except RuntimeContractError:
|
|
raise
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback copy failed"
|
|
) from None
|
|
for entry in self.entries:
|
|
if tensor_digest(parameters[entry.logical_name]) != entry.pristine_digest:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_ROLLBACK_FAILED", "rollback did not restore pristine identity"
|
|
)
|
|
|
|
def cleanup(self) -> None:
|
|
"""Delete only this journal's exact files, refusing unknown directory contents."""
|
|
|
|
if not self.active:
|
|
return
|
|
parent_descriptor = _open_private_directory(self.root.parent)
|
|
descriptor: int | None = None
|
|
expected = {item.relative_name for item in self.entries}
|
|
try:
|
|
descriptor = _open_private_directory(self.root)
|
|
if set(os.listdir(descriptor)) != expected:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal contains unexpected files"
|
|
)
|
|
for name in sorted(expected):
|
|
os.unlink(name, dir_fd=descriptor)
|
|
os.fsync(descriptor)
|
|
os.rmdir(self.root.name, dir_fd=parent_descriptor)
|
|
os.fsync(parent_descriptor)
|
|
except RuntimeContractError:
|
|
raise
|
|
except OSError:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_JOURNAL_FAILED", "rollback journal cleanup failed"
|
|
) from None
|
|
finally:
|
|
close_failed = False
|
|
for finalizer_descriptor in (descriptor, parent_descriptor):
|
|
if finalizer_descriptor is not None:
|
|
try:
|
|
os.close(finalizer_descriptor)
|
|
except OSError:
|
|
close_failed = True
|
|
if close_failed:
|
|
raise RuntimeContractError(
|
|
"LMS_EXCISE_STATE_UNCERTAIN", "rollback journal directory close failed"
|
|
) from None
|
|
self.active = False
|
|
|
|
|
|
__all__ = ["RankJournal"]
|