mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +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>
488 lines
17 KiB
Python
488 lines
17 KiB
Python
"""Closed EXCISE transaction records for the experimental TP2 stack (#244)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import math
|
|
import re
|
|
from collections.abc import Iterable
|
|
from dataclasses import dataclass
|
|
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError, PlacementKind, canonical_record, contract_digest,
|
|
)
|
|
from obliteratus.distributed.model_manifest import SurgeryRole
|
|
|
|
|
|
MAX_EXCISE_OPERATIONS = 16_384
|
|
MAX_EXCISE_BYTES = 1 << 50
|
|
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
|
|
_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]{1,512}$")
|
|
_SITE_RE = re.compile(r"^model\.layers\.(0|[1-9][0-9]{0,5})$")
|
|
_JOURNAL_FILE_RE = re.compile(r"^entry-(0|[1-9][0-9]{0,5})\.safetensors$")
|
|
|
|
|
|
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} must be between {minimum} and {maximum}")
|
|
return value
|
|
|
|
|
|
def _finite(
|
|
value: object,
|
|
name: str,
|
|
*,
|
|
minimum: float,
|
|
maximum: float,
|
|
minimum_inclusive: bool = True,
|
|
) -> float:
|
|
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
|
|
raise ContractError(f"{name} must be finite")
|
|
result = float(value)
|
|
lower_ok = result >= minimum if minimum_inclusive else result > minimum
|
|
if not lower_ok or result > maximum:
|
|
raise ContractError(f"{name} is outside its closed bound")
|
|
return result
|
|
|
|
|
|
def _digest(value: object, name: str) -> str:
|
|
if not isinstance(value, str) or _DIGEST_RE.fullmatch(value) is None:
|
|
raise ContractError(f"{name} has an invalid digest")
|
|
return value
|
|
|
|
|
|
def _shape(value: object, name: str) -> tuple[int, ...]:
|
|
if (
|
|
not isinstance(value, tuple)
|
|
or not 1 <= len(value) <= 2
|
|
or any(isinstance(item, bool) or not isinstance(item, int) or item < 1 for item in value)
|
|
):
|
|
raise ContractError(f"{name} must be a positive one- or two-dimensional shape")
|
|
return value
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExciseConfig:
|
|
"""The only version-1 numerical and resource options for distributed EXCISE."""
|
|
|
|
schema_version: int = 1
|
|
roles: tuple[SurgeryRole, ...] = (
|
|
SurgeryRole.ATTENTION_INPUT,
|
|
SurgeryRole.ATTENTION_OUTPUT,
|
|
SurgeryRole.MLP_INPUT,
|
|
SurgeryRole.MLP_OUTPUT,
|
|
)
|
|
norm_preserve: bool = False
|
|
regularization: float = 0.0
|
|
projection_row_fraction: float = 1.0
|
|
max_norm_ratio: float = 1.10
|
|
project_biases: bool = False
|
|
max_journal_bytes: int = 1 << 40
|
|
max_update_bytes: int = 1 << 40
|
|
safety_margin_bytes: int = 1 << 30
|
|
stage_timeout_seconds: float = 1_800.0
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("EXCISE config schema_version must be 1")
|
|
allowed = {
|
|
SurgeryRole.ATTENTION_INPUT,
|
|
SurgeryRole.ATTENTION_OUTPUT,
|
|
SurgeryRole.MLP_INPUT,
|
|
SurgeryRole.MLP_OUTPUT,
|
|
}
|
|
if (
|
|
not isinstance(self.roles, tuple)
|
|
or not self.roles
|
|
or len(set(self.roles)) != len(self.roles)
|
|
or set(self.roles) - allowed
|
|
or self.roles != tuple(sorted(self.roles, key=lambda item: item.value))
|
|
):
|
|
raise ContractError("EXCISE roles must be the unique canonical mutable-role subset")
|
|
if not isinstance(self.norm_preserve, bool) or not isinstance(self.project_biases, bool):
|
|
raise ContractError("EXCISE boolean options must be booleans")
|
|
if self.project_biases:
|
|
raise ContractError("the exact bias-free Llama profile cannot project biases")
|
|
_finite(self.regularization, "regularization", minimum=0.0, maximum=1.0)
|
|
_finite(
|
|
self.projection_row_fraction,
|
|
"projection_row_fraction",
|
|
minimum=0.0,
|
|
maximum=1.0,
|
|
minimum_inclusive=False,
|
|
)
|
|
_finite(
|
|
self.max_norm_ratio,
|
|
"max_norm_ratio",
|
|
minimum=0.0,
|
|
maximum=100.0,
|
|
minimum_inclusive=False,
|
|
)
|
|
_integer(
|
|
self.max_journal_bytes,
|
|
"max_journal_bytes",
|
|
minimum=1,
|
|
maximum=MAX_EXCISE_BYTES,
|
|
)
|
|
_integer(
|
|
self.max_update_bytes,
|
|
"max_update_bytes",
|
|
minimum=1,
|
|
maximum=MAX_EXCISE_BYTES,
|
|
)
|
|
_integer(
|
|
self.safety_margin_bytes,
|
|
"safety_margin_bytes",
|
|
minimum=0,
|
|
maximum=MAX_EXCISE_BYTES,
|
|
)
|
|
_finite(
|
|
self.stage_timeout_seconds,
|
|
"stage_timeout_seconds",
|
|
minimum=0.0,
|
|
maximum=21_600.0,
|
|
minimum_inclusive=False,
|
|
)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(
|
|
{
|
|
"schema_version": self.schema_version,
|
|
"roles": tuple(item.value for item in self.roles),
|
|
"norm_preserve": self.norm_preserve,
|
|
"regularization_hex": float(self.regularization).hex(),
|
|
"projection_row_fraction_hex": float(self.projection_row_fraction).hex(),
|
|
"max_norm_ratio_hex": float(self.max_norm_ratio).hex(),
|
|
"project_biases": self.project_biases,
|
|
"max_journal_bytes": self.max_journal_bytes,
|
|
"max_update_bytes": self.max_update_bytes,
|
|
"safety_margin_bytes": self.safety_margin_bytes,
|
|
"stage_timeout_seconds_hex": float(self.stage_timeout_seconds).hex(),
|
|
}
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExciseOperation:
|
|
"""One deterministic local view of a logical projection operation."""
|
|
|
|
schema_version: int
|
|
sequence: int
|
|
logical_name: str
|
|
site: str
|
|
layer_index: int
|
|
surgery_role: SurgeryRole
|
|
placement: PlacementKind
|
|
global_shape: tuple[int, int]
|
|
local_shape: tuple[int, int]
|
|
dtype: str
|
|
direction_axis: int
|
|
shard_dim: int | None
|
|
shard_start: int
|
|
shard_end: int
|
|
owner_ranks: tuple[int, ...]
|
|
tie_id: str | None
|
|
direction_digest: str
|
|
direction_count: int
|
|
local_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("EXCISE operation schema_version must be 1")
|
|
_integer(self.sequence, "operation sequence", minimum=0, maximum=MAX_EXCISE_OPERATIONS - 1)
|
|
if not isinstance(self.logical_name, str) or _NAME_RE.fullmatch(self.logical_name) is None:
|
|
raise ContractError("operation logical_name is invalid")
|
|
match = _SITE_RE.fullmatch(self.site) if isinstance(self.site, str) else None
|
|
if match is None or self.layer_index != int(match.group(1)):
|
|
raise ContractError("operation site and layer index disagree")
|
|
if (
|
|
not isinstance(self.surgery_role, SurgeryRole)
|
|
or self.surgery_role is SurgeryRole.IMMUTABLE
|
|
):
|
|
raise ContractError("operation surgery role is not mutable")
|
|
if not isinstance(self.placement, PlacementKind):
|
|
raise ContractError("operation placement is invalid")
|
|
_shape(self.global_shape, "global_shape")
|
|
_shape(self.local_shape, "local_shape")
|
|
if len(self.global_shape) != 2 or len(self.local_shape) != 2:
|
|
raise ContractError("version-1 EXCISE operations require matrix parameters")
|
|
if not isinstance(self.dtype, str) or self.dtype not in {
|
|
"float16",
|
|
"bfloat16",
|
|
"float32",
|
|
"float64",
|
|
}:
|
|
raise ContractError("operation dtype is unsupported")
|
|
_integer(self.direction_axis, "direction_axis", minimum=0, maximum=1)
|
|
if not isinstance(self.owner_ranks, tuple) or self.owner_ranks != (0, 1):
|
|
raise ContractError("operation owner ranks must be the exact TP2 world")
|
|
if self.tie_id is not None:
|
|
raise ContractError("the exact untied Llama profile cannot admit tied operations")
|
|
if self.placement is PlacementKind.REPLICATED:
|
|
if (
|
|
self.local_shape != self.global_shape
|
|
or self.shard_dim is not None
|
|
or self.shard_start != 0
|
|
or self.shard_end != 0
|
|
):
|
|
raise ContractError("replicated operation declares an invalid local view")
|
|
else:
|
|
expected_dim = 0 if self.placement is PlacementKind.COLUMN_WISE else 1
|
|
if self.shard_dim != expected_dim:
|
|
raise ContractError("operation shard dimension disagrees with placement")
|
|
extent = self.global_shape[expected_dim]
|
|
if extent % 2 or self.shard_end - self.shard_start != extent // 2:
|
|
raise ContractError("operation shard extent is uneven or incomplete")
|
|
expected_shape = list(self.global_shape)
|
|
expected_shape[expected_dim] //= 2
|
|
if self.local_shape != tuple(expected_shape):
|
|
raise ContractError("operation local shape disagrees with placement")
|
|
_digest(self.direction_digest, "direction_digest")
|
|
_integer(self.direction_count, "direction_count", minimum=1, maximum=64)
|
|
_integer(self.local_bytes, "local_bytes", minimum=1, maximum=MAX_EXCISE_BYTES)
|
|
|
|
@property
|
|
def logical_digest(self) -> str:
|
|
"""Digest shared by both ranks; rank-local intervals are intentionally absent."""
|
|
return contract_digest(
|
|
{
|
|
"schema_version": self.schema_version,
|
|
"sequence": self.sequence,
|
|
"logical_name": self.logical_name,
|
|
"site": self.site,
|
|
"layer_index": self.layer_index,
|
|
"surgery_role": self.surgery_role,
|
|
"placement": self.placement,
|
|
"global_shape": self.global_shape,
|
|
"dtype": self.dtype,
|
|
"direction_axis": self.direction_axis,
|
|
"shard_dim": self.shard_dim,
|
|
"owner_ranks": self.owner_ranks,
|
|
"tie_id": self.tie_id,
|
|
"direction_digest": self.direction_digest,
|
|
"direction_count": self.direction_count,
|
|
}
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class JournalEntry:
|
|
"""Private journal metadata; tensor values and absolute paths are excluded."""
|
|
|
|
schema_version: int
|
|
sequence: int
|
|
logical_name: str
|
|
local_shape: tuple[int, ...]
|
|
dtype: str
|
|
placement: PlacementKind
|
|
shard_start: int
|
|
shard_end: int
|
|
pristine_digest: str
|
|
payload_digest: str
|
|
relative_name: str
|
|
payload_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("journal entry schema_version must be 1")
|
|
_integer(self.sequence, "journal sequence", minimum=0, maximum=MAX_EXCISE_OPERATIONS - 1)
|
|
if not isinstance(self.logical_name, str) or _NAME_RE.fullmatch(self.logical_name) is None:
|
|
raise ContractError("journal logical_name is invalid")
|
|
_shape(self.local_shape, "journal local_shape")
|
|
if not isinstance(self.dtype, str) or self.dtype not in {
|
|
"float16",
|
|
"bfloat16",
|
|
"float32",
|
|
"float64",
|
|
}:
|
|
raise ContractError("journal dtype is unsupported")
|
|
if not isinstance(self.placement, PlacementKind):
|
|
raise ContractError("journal placement is invalid")
|
|
for name, value in (("shard_start", self.shard_start), ("shard_end", self.shard_end)):
|
|
_integer(value, name, minimum=0, maximum=2**63 - 1)
|
|
_digest(self.pristine_digest, "pristine_digest")
|
|
_digest(self.payload_digest, "payload_digest")
|
|
if (
|
|
not isinstance(self.relative_name, str)
|
|
or _JOURNAL_FILE_RE.fullmatch(self.relative_name) is None
|
|
):
|
|
raise ContractError("journal relative name is invalid")
|
|
_integer(self.payload_bytes, "payload_bytes", minimum=1, maximum=MAX_EXCISE_BYTES)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RankJournalSummary:
|
|
"""Value-free rank-local journal root used in collective evidence."""
|
|
|
|
schema_version: int
|
|
rank: int
|
|
operation_count: int
|
|
tensor_bytes: int
|
|
payload_bytes: int
|
|
entry_root_digest: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("journal summary schema_version must be 1")
|
|
_integer(self.rank, "journal rank", minimum=0, maximum=1)
|
|
_integer(
|
|
self.operation_count,
|
|
"journal operation_count",
|
|
minimum=1,
|
|
maximum=MAX_EXCISE_OPERATIONS,
|
|
)
|
|
_integer(self.tensor_bytes, "journal tensor_bytes", minimum=1, maximum=MAX_EXCISE_BYTES)
|
|
_integer(self.payload_bytes, "journal payload_bytes", minimum=1, maximum=MAX_EXCISE_BYTES)
|
|
_digest(self.entry_root_digest, "entry_root_digest")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExciseOperationSummary:
|
|
"""Common logical pre/update/post roots for one committed operation."""
|
|
|
|
schema_version: int
|
|
sequence: int
|
|
operation_digest: str
|
|
logical_pristine_digest: str
|
|
logical_update_digest: str
|
|
logical_post_digest: str
|
|
coefficient_norm_sq_hex: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("operation summary schema_version must be 1")
|
|
_integer(
|
|
self.sequence,
|
|
"operation summary sequence",
|
|
minimum=0,
|
|
maximum=MAX_EXCISE_OPERATIONS - 1,
|
|
)
|
|
for name in (
|
|
"operation_digest",
|
|
"logical_pristine_digest",
|
|
"logical_update_digest",
|
|
"logical_post_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
if not isinstance(self.coefficient_norm_sq_hex, str):
|
|
raise ContractError("coefficient norm is not a canonical hexadecimal float")
|
|
try:
|
|
value = float.fromhex(self.coefficient_norm_sq_hex)
|
|
except ValueError:
|
|
raise ContractError("coefficient norm is not a canonical hexadecimal float") from None
|
|
if not math.isfinite(value) or value < 0 or value.hex() != self.coefficient_norm_sq_hex:
|
|
raise ContractError("coefficient norm is not a canonical hexadecimal float")
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExciseSummary:
|
|
"""Redaction-safe common result while the private rollback journal remains live."""
|
|
|
|
schema_version: int
|
|
run_id: str
|
|
distilled_digest: str
|
|
manifest_digest: str
|
|
config_digest: str
|
|
operation_plan_digest: str
|
|
journal_set_digest: str
|
|
postcondition_set_digest: str
|
|
operation_count: int
|
|
local_tensor_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1:
|
|
raise ContractError("EXCISE summary schema_version must be 1")
|
|
if not isinstance(self.run_id, str) or re.fullmatch(r"[0-9a-f]{32}", self.run_id) is None:
|
|
raise ContractError("EXCISE summary run_id is invalid")
|
|
for name in (
|
|
"distilled_digest",
|
|
"manifest_digest",
|
|
"config_digest",
|
|
"operation_plan_digest",
|
|
"journal_set_digest",
|
|
"postcondition_set_digest",
|
|
):
|
|
_digest(getattr(self, name), name)
|
|
_integer(
|
|
self.operation_count,
|
|
"operation_count",
|
|
minimum=1,
|
|
maximum=MAX_EXCISE_OPERATIONS,
|
|
)
|
|
_integer(
|
|
self.local_tensor_bytes,
|
|
"local_tensor_bytes",
|
|
minimum=1,
|
|
maximum=MAX_EXCISE_BYTES,
|
|
)
|
|
|
|
@property
|
|
def digest(self) -> str:
|
|
return contract_digest(self)
|
|
|
|
|
|
def excise_digest_set(digests: Iterable[str]) -> str:
|
|
"""Hash an ordered bounded digest set, preserving canonical array identity."""
|
|
digest = hashlib.sha256(b"[")
|
|
count = 0
|
|
for item in digests:
|
|
if count >= MAX_EXCISE_OPERATIONS:
|
|
raise ContractError("EXCISE digest set exceeds the operation bound")
|
|
_digest(item, "EXCISE set item")
|
|
if count:
|
|
digest.update(b",")
|
|
digest.update(canonical_record(item))
|
|
count += 1
|
|
if not count:
|
|
raise ContractError("EXCISE digest set is empty")
|
|
digest.update(b"]")
|
|
return digest.hexdigest()
|
|
|
|
|
|
def operation_plan_digest(operations: tuple[ExciseOperation, ...]) -> str:
|
|
if (
|
|
not operations
|
|
or len(operations) > MAX_EXCISE_OPERATIONS
|
|
or tuple(item.sequence for item in operations) != tuple(range(len(operations)))
|
|
):
|
|
raise ContractError("EXCISE operation plan is empty, oversized, or out of sequence")
|
|
return excise_digest_set(item.logical_digest for item in operations)
|
|
|
|
|
|
def journal_entry_root(entries: tuple[JournalEntry, ...]) -> str:
|
|
if (
|
|
not entries
|
|
or len(entries) > MAX_EXCISE_OPERATIONS
|
|
or tuple(item.sequence for item in entries) != tuple(range(len(entries)))
|
|
):
|
|
raise ContractError("journal entries are empty, oversized, or out of sequence")
|
|
return excise_digest_set(item.digest for item in entries)
|
|
|
|
|
|
__all__ = [
|
|
"ExciseConfig",
|
|
"ExciseOperation",
|
|
"ExciseOperationSummary",
|
|
"ExciseSummary",
|
|
"JournalEntry",
|
|
"MAX_EXCISE_BYTES",
|
|
"MAX_EXCISE_OPERATIONS",
|
|
"RankJournalSummary",
|
|
"excise_digest_set",
|
|
"journal_entry_root",
|
|
"operation_plan_digest",
|
|
]
|