mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Brings the ported canonical export up to the rules from parts 3 and 4: - Every rank-local step that can raise is followed by a unanimous control vote before the next collective. This covers reconstruction, file I/O, safetensors write and reload, digests, validation, fault hooks and record construction. EXPORT votes use [17_000_000, 24_000_000). - Tensor, file and index aggregates use streamed, domain-separated, length-prefixed digests (streamed_contract_digest), so every advertised limit works. Oversized HF indexes are refused before any write. - One combined, voted host admission covers reconstructed tensors, gather buffers, serialization and reload copies, chunked digest and finite checks, metadata and the still-live VERIFY state. - VERIFIED revalidation uses the guarded #274 helpers and logical roots, and requires the released journal. - Cancellation and the deadline are rechecked before promotion and before return. Files are rehashed at the final gate. Cleanup or confirmation failure quarantines as LMS_EXPORT_STATE_UNCERTAIN, and strict backup cleanup never restores a partially deleted backup. - Single-tensor output uses indexed naming. The HF index payload helper is shared with checkpoint_writer, with identical output. - Docs use the upstream #244 framing and are marked EXPERIMENTAL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
469 lines
16 KiB
Python
469 lines
16 KiB
Python
"""Closed canonical export records for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from collections.abc import Iterable
|
|
from dataclasses import dataclass
|
|
from pathlib import PurePosixPath
|
|
|
|
from obliteratus.distributed.contracts import ContractError, contract_digest, streamed_contract_digest
|
|
|
|
|
|
MAX_EXPORT_TENSORS = 16_384
|
|
MAX_EXPORT_FILES = 32_768
|
|
MAX_EXPORT_BYTES = (1 << 63) - 1
|
|
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
|
|
_ATTEMPT_RE = re.compile(r"^[0-9a-f]{32}$")
|
|
_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]{1,512}$")
|
|
_OUTPUT_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$")
|
|
_DTYPES = frozenset({"bfloat16"})
|
|
_PLACEMENTS = frozenset({"replicate", "shard"})
|
|
|
|
|
|
def export_aggregate_digest(records: Iterable[object], *, domain: str) -> str:
|
|
"""Stream ordered export records without the bounded control-frame limit."""
|
|
return streamed_contract_digest(
|
|
records, prefix=b"obliteratus-export-aggregate-v1\0", domain=domain,
|
|
)
|
|
|
|
|
|
def _integer(value: object, name: str, minimum: int, maximum: int) -> int:
|
|
if isinstance(value, bool) or not isinstance(value, int) or not minimum <= value <= maximum:
|
|
raise ContractError(f"{name} is outside its bounded integer contract")
|
|
return value
|
|
|
|
|
|
def _digest(value: object, name: str) -> str:
|
|
if not isinstance(value, str) or _DIGEST_RE.fullmatch(value) is None:
|
|
raise ContractError(f"{name} must be a lowercase SHA-256 digest")
|
|
return value
|
|
|
|
|
|
def _attempt(value: object, name: str) -> str:
|
|
if not isinstance(value, str) or _ATTEMPT_RE.fullmatch(value) is None:
|
|
raise ContractError(f"{name} must be a 128-bit lowercase hexadecimal identifier")
|
|
return value
|
|
|
|
|
|
def _name(value: object, name: str, pattern: re.Pattern[str] = _NAME_RE) -> str:
|
|
if not isinstance(value, str) or pattern.fullmatch(value) is None:
|
|
raise ContractError(f"{name} is invalid")
|
|
return value
|
|
|
|
|
|
def _relative(value: object, name: str) -> str:
|
|
if not isinstance(value, str) or not value or len(value.encode("utf-8")) > 4096:
|
|
raise ContractError(f"{name} is invalid")
|
|
path = PurePosixPath(value)
|
|
if path.is_absolute() or any(part in {"", ".", ".."} for part in path.parts):
|
|
raise ContractError(f"{name} is unsafe")
|
|
return value
|
|
|
|
|
|
def _shape(value: object, name: str) -> tuple[int, ...]:
|
|
if not isinstance(value, tuple) or not 1 <= len(value) <= 8:
|
|
raise ContractError(f"{name} is invalid")
|
|
result = tuple(_integer(item, name, 1, 2**63 - 1) for item in value)
|
|
elements = 1
|
|
for item in result:
|
|
if elements > (2**63 - 1) // item:
|
|
raise ContractError(f"{name} overflows the signed 64-bit bound")
|
|
elements *= item
|
|
return result
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExportConfig:
|
|
"""Caller-bound limits and fresh identity for one non-resumable export attempt."""
|
|
|
|
export_id: str
|
|
output_name: str
|
|
max_output_bytes: int = 1 << 40
|
|
max_staging_bytes: int = 2 << 40
|
|
max_host_bytes_per_rank: int = 32 << 30
|
|
max_device_bytes_per_rank: int = 32 << 30
|
|
semantic_reload_host_bytes_per_rank: int = 8 << 30
|
|
semantic_reload_device_bytes_per_rank: int = 8 << 30
|
|
max_tensor_bytes: int = 16 << 30
|
|
max_metadata_bytes: int = 64 << 20
|
|
max_metadata_files: int = 4096
|
|
safety_margin_bytes: int = 1 << 30
|
|
stage_timeout_seconds: int = 3600
|
|
schema_version: int = 1
|
|
require_semantic_reload: bool = True
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("export config schema_version must be 1")
|
|
_attempt(self.export_id, "export_id")
|
|
_name(self.output_name, "output_name", _OUTPUT_RE)
|
|
if self.output_name.startswith("."):
|
|
raise ContractError("output_name cannot be hidden")
|
|
for name in (
|
|
"max_output_bytes",
|
|
"max_staging_bytes",
|
|
"max_host_bytes_per_rank",
|
|
"max_device_bytes_per_rank",
|
|
"semantic_reload_host_bytes_per_rank",
|
|
"semantic_reload_device_bytes_per_rank",
|
|
"max_tensor_bytes",
|
|
"max_metadata_bytes",
|
|
"safety_margin_bytes",
|
|
):
|
|
_integer(
|
|
getattr(self, name),
|
|
name,
|
|
0 if name == "safety_margin_bytes" else 1,
|
|
MAX_EXPORT_BYTES,
|
|
)
|
|
_integer(self.max_metadata_files, "max_metadata_files", 1, MAX_EXPORT_FILES)
|
|
_integer(self.stage_timeout_seconds, "stage_timeout_seconds", 1, 21_600)
|
|
if self.require_semantic_reload is not True:
|
|
raise ContractError("semantic reload validation cannot be disabled")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WriterAssignment:
|
|
"""One deterministic writer and unique file for one complete logical tensor."""
|
|
|
|
schema_version: int
|
|
sequence: int
|
|
tensor_name: str
|
|
writer_rank: int
|
|
file_name: str
|
|
global_shape: tuple[int, ...]
|
|
dtype: str
|
|
placement: str
|
|
shard_dim: int | None
|
|
logical_bytes: int
|
|
world_size: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("writer assignment schema_version must be 1")
|
|
_integer(self.sequence, "sequence", 0, MAX_EXPORT_TENSORS - 1)
|
|
_name(self.tensor_name, "tensor_name")
|
|
_integer(self.world_size, "world_size", 2, 2)
|
|
_integer(self.writer_rank, "writer_rank", 0, self.world_size - 1)
|
|
_name(self.file_name, "file_name")
|
|
if PurePosixPath(self.file_name).name != self.file_name or not self.file_name.endswith(
|
|
".safetensors"
|
|
):
|
|
raise ContractError("writer file_name must be a root-level safetensors file")
|
|
shape = _shape(self.global_shape, "global_shape")
|
|
if self.dtype not in _DTYPES or self.placement not in _PLACEMENTS:
|
|
raise ContractError("writer assignment dtype or placement is unsupported")
|
|
if self.placement == "replicate":
|
|
if self.shard_dim is not None:
|
|
raise ContractError("replicated writer assignment cannot name a shard dimension")
|
|
elif (
|
|
isinstance(self.shard_dim, bool)
|
|
or not isinstance(self.shard_dim, int)
|
|
or not 0 <= self.shard_dim < len(shape)
|
|
):
|
|
raise ContractError("sharded writer assignment requires a valid shard dimension")
|
|
expected = 2
|
|
for item in shape:
|
|
if expected > MAX_EXPORT_BYTES // item:
|
|
raise ContractError("logical tensor bytes overflow the export contract")
|
|
expected *= item
|
|
if _integer(self.logical_bytes, "logical_bytes", 1, MAX_EXPORT_BYTES) != expected:
|
|
raise ContractError("logical_bytes disagree with BF16 shape")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TensorExportRecord:
|
|
"""Content and file identity independently checked after one writer finishes."""
|
|
|
|
schema_version: int
|
|
sequence: int
|
|
assignment_digest: str
|
|
tensor_name: str
|
|
writer_rank: int
|
|
file_name: str
|
|
tensor_sha256: str
|
|
file_sha256: str
|
|
file_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("tensor export record schema_version must be 1")
|
|
_integer(self.sequence, "sequence", 0, MAX_EXPORT_TENSORS - 1)
|
|
_digest(self.assignment_digest, "assignment_digest")
|
|
_name(self.tensor_name, "tensor_name")
|
|
_integer(self.writer_rank, "writer_rank", 0, 1)
|
|
_name(self.file_name, "file_name")
|
|
_digest(self.tensor_sha256, "tensor_sha256")
|
|
_digest(self.file_sha256, "file_sha256")
|
|
_integer(self.file_bytes, "file_bytes", 1, MAX_EXPORT_BYTES)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MetadataFileRecord:
|
|
"""One immutable configuration or tokenizer artifact copied from the source."""
|
|
|
|
relative_path: str
|
|
size_bytes: int
|
|
sha256: str
|
|
|
|
def __post_init__(self) -> None:
|
|
_relative(self.relative_path, "metadata relative_path")
|
|
_integer(self.size_bytes, "metadata size_bytes", 1, MAX_EXPORT_BYTES)
|
|
_digest(self.sha256, "metadata sha256")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ResourceAdmission:
|
|
"""Conservative pre-write resource proof bound into the publication manifest."""
|
|
|
|
logical_bytes: int
|
|
journal_bytes: int
|
|
metadata_bytes: int
|
|
header_allowance_bytes: int
|
|
prior_output_bytes: int
|
|
staging_required_bytes: int
|
|
largest_tensor_bytes: int
|
|
largest_local_shard_bytes: int
|
|
host_required_by_rank: tuple[int, ...]
|
|
device_required_by_rank: tuple[int, ...]
|
|
|
|
def __post_init__(self) -> None:
|
|
for name in (
|
|
"logical_bytes",
|
|
"journal_bytes",
|
|
"metadata_bytes",
|
|
"header_allowance_bytes",
|
|
"prior_output_bytes",
|
|
"staging_required_bytes",
|
|
"largest_tensor_bytes",
|
|
"largest_local_shard_bytes",
|
|
):
|
|
_integer(getattr(self, name), name, 0, MAX_EXPORT_BYTES)
|
|
for field_name in ("host_required_by_rank", "device_required_by_rank"):
|
|
values = getattr(self, field_name)
|
|
if not isinstance(values, tuple) or len(values) != 2:
|
|
raise ContractError(f"{field_name} must cover the exact TP2 world")
|
|
for value in values:
|
|
_integer(value, field_name, 0, MAX_EXPORT_BYTES)
|
|
if self.journal_bytes != 0:
|
|
raise ContractError("export requires zero remaining rollback journal bytes")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReloadValidationRequest:
|
|
"""Closed expectations supplied to an independent semantic reload oracle."""
|
|
|
|
schema_version: int
|
|
rank: int
|
|
world_size: int
|
|
export_identity_digest: str
|
|
key_set_digest: str
|
|
tensor_set_digest: str
|
|
config_digest: str
|
|
tokenizer_digest: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("reload request schema_version must be 1")
|
|
_integer(self.rank, "rank", 0, 1)
|
|
_integer(self.world_size, "world_size", 2, 2)
|
|
for name in (
|
|
"export_identity_digest",
|
|
"key_set_digest",
|
|
"tensor_set_digest",
|
|
"config_digest",
|
|
"tokenizer_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReloadValidationEvidence:
|
|
"""Rank-local proof from an independent canonical reload and semantic oracle."""
|
|
|
|
schema_version: int
|
|
rank: int
|
|
export_identity_digest: str
|
|
key_set_digest: str
|
|
tensor_set_digest: str
|
|
config_digest: str
|
|
tokenizer_digest: str
|
|
logits_digest: str
|
|
generation_digest: str
|
|
passed: bool
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("reload evidence schema_version must be 1")
|
|
_integer(self.rank, "rank", 0, 1)
|
|
for name in (
|
|
"export_identity_digest",
|
|
"key_set_digest",
|
|
"tensor_set_digest",
|
|
"config_digest",
|
|
"tokenizer_digest",
|
|
"logits_digest",
|
|
"generation_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
if self.passed is not True:
|
|
raise ContractError("reload evidence must record a passing semantic oracle")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RankExportEvidence:
|
|
"""Bounded rank evidence for actual memory, writes, and reload outcome."""
|
|
|
|
schema_version: int
|
|
rank: int
|
|
export_identity_digest: str
|
|
assignment_set_digest: str
|
|
tensor_record_set_digest: str
|
|
tensors_written: int
|
|
logical_bytes_written: int
|
|
peak_host_rss_bytes: int
|
|
peak_device_allocated_bytes: int
|
|
observed_staging_bytes: int
|
|
reload_evidence_digest: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("rank export evidence schema_version must be 1")
|
|
_integer(self.rank, "rank", 0, 1)
|
|
for name in (
|
|
"export_identity_digest",
|
|
"assignment_set_digest",
|
|
"tensor_record_set_digest",
|
|
"reload_evidence_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
_integer(self.tensors_written, "tensors_written", 0, MAX_EXPORT_TENSORS)
|
|
for name in (
|
|
"logical_bytes_written",
|
|
"peak_host_rss_bytes",
|
|
"peak_device_allocated_bytes",
|
|
"observed_staging_bytes",
|
|
):
|
|
_integer(getattr(self, name), name, 0, MAX_EXPORT_BYTES)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExportSummary:
|
|
"""One globally agreed identity for a fully validated atomic publication."""
|
|
|
|
schema_version: int
|
|
run_id: str
|
|
export_id: str
|
|
verification_digest: str
|
|
config_digest: str
|
|
runtime_config_digest: str
|
|
hf_config_digest: str
|
|
source_digest: str
|
|
model_digest: str
|
|
tokenizer_digest: str
|
|
code_digest: str
|
|
storage_digest: str
|
|
commit_sha: str
|
|
parameter_manifest_digest: str
|
|
placement_plan_digest: str
|
|
topology_digest: str
|
|
excise_digest: str
|
|
excise_config_digest: str
|
|
operation_plan_digest: str
|
|
postcondition_set_digest: str
|
|
assignment_set_digest: str
|
|
tensor_record_set_digest: str
|
|
metadata_set_digest: str
|
|
hf_index_digest: str
|
|
admission_digest: str
|
|
reload_evidence_set_digest: str
|
|
rank_evidence_set_digest: str
|
|
output_name: str
|
|
tensor_count: int
|
|
logical_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("export summary schema_version must be 1")
|
|
_attempt(self.run_id, "run_id")
|
|
_attempt(self.export_id, "export_id")
|
|
_name(self.output_name, "output_name", _OUTPUT_RE)
|
|
for name in (
|
|
"verification_digest",
|
|
"config_digest",
|
|
"runtime_config_digest",
|
|
"hf_config_digest",
|
|
"source_digest",
|
|
"model_digest",
|
|
"tokenizer_digest",
|
|
"code_digest",
|
|
"storage_digest",
|
|
"parameter_manifest_digest",
|
|
"placement_plan_digest",
|
|
"topology_digest",
|
|
"excise_digest",
|
|
"excise_config_digest",
|
|
"operation_plan_digest",
|
|
"postcondition_set_digest",
|
|
"assignment_set_digest",
|
|
"tensor_record_set_digest",
|
|
"metadata_set_digest",
|
|
"hf_index_digest",
|
|
"admission_digest",
|
|
"reload_evidence_set_digest",
|
|
"rank_evidence_set_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
if (
|
|
not isinstance(self.commit_sha, str)
|
|
or re.fullmatch(r"[0-9a-f]{40}", self.commit_sha) is None
|
|
):
|
|
raise ContractError("commit_sha must be a lowercase Git SHA-1")
|
|
_integer(self.tensor_count, "tensor_count", 1, MAX_EXPORT_TENSORS)
|
|
_integer(self.logical_bytes, "logical_bytes", 1, MAX_EXPORT_BYTES)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
__all__ = [
|
|
"ExportConfig",
|
|
"ExportSummary",
|
|
"MAX_EXPORT_BYTES",
|
|
"MAX_EXPORT_FILES",
|
|
"MAX_EXPORT_TENSORS",
|
|
"MetadataFileRecord",
|
|
"RankExportEvidence",
|
|
"ReloadValidationEvidence",
|
|
"ReloadValidationRequest",
|
|
"ResourceAdmission",
|
|
"TensorExportRecord",
|
|
"WriterAssignment",
|
|
]
|