Files
OBLITERATUS/obliteratus/distributed/model_session.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

1008 lines
40 KiB
Python

"""Fail-closed orchestration for one exact distributed model-load session."""
from __future__ import annotations
import json
import multiprocessing
import os
import re
import stat
import threading
from collections.abc import Callable, Iterator, Mapping
from contextlib import ExitStack, contextmanager
from dataclasses import dataclass
from pathlib import Path
from types import MappingProxyType
from typing import Any
from obliteratus.distributed.config import DistributedPreflightConfig
from obliteratus.distributed.consensus import gather_failure_codes, gloo_all_gather_records, unanimous_vote
from obliteratus.distributed.contracts import (
AgreedTopologyRefusal,
ContractError,
RuntimeContractError,
RuntimeStage,
StageMessage,
Vote,
advance_stage,
canonical_record,
contract_digest,
)
from obliteratus.distributed.launcher import (
TorchrunEnvironment,
control_group,
runtime_diagnostic_guard,
)
from obliteratus.distributed.model import load_distributed_model
from obliteratus.distributed.model_identity import observed_external_runtime_bindings
from obliteratus.distributed.model_topology import (
GPU_CANDIDATE_EVIDENCE_TIERS,
TP2DeploymentLayout,
classify_tp2_topology,
configured_tp2_layout,
)
from obliteratus.distributed.model_evidence import (
LOAD_ERROR_CODES,
LOAD_EVIDENCE_BINDING_FIELDS,
LoadEvidenceSummary,
build_rank_load_evidence,
publish_rank_load_evidence,
validate_loaded_summary_set,
)
from obliteratus.distributed.preflight import (
PreflightProbes,
PreflightResult,
SourceMountAttestation,
run_live_preflight,
verify_source_mount_attestation,
)
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
_HARD_EXIT_CODE = 70
_SUMMARY_FIELDS = frozenset(LoadEvidenceSummary.__dataclass_fields__)
EXTERNAL_LOAD_BINDING_FIELDS = frozenset(
{
"api_digest",
"artifact_manifest_digest",
"image_digest",
"lock_digest",
"package_digest",
"runtime_digest",
"torch_wheel_digest",
}
)
@dataclass(frozen=True)
class ExactLoadProfile:
"""Closed external identities; runtime-derived evidence is added by the session."""
schema_version: int
evidence_tier: str
profile_state: str
residual_risk_accepted: bool
transport_security: str
session_timeout_seconds: float
bindings: Mapping[str, str]
source_mount: SourceMountAttestation
def __post_init__(self) -> None:
if self.schema_version != 1:
raise ContractError("exact load profile schema_version must be 1")
if self.evidence_tier not in {"protocol_cpu", *GPU_CANDIDATE_EVIDENCE_TIERS}:
raise ContractError("exact load profile evidence tier is invalid")
if self.profile_state != "candidate_unaccepted":
raise ContractError("exact load profile cannot assert production acceptance")
if self.residual_risk_accepted is not False:
raise ContractError("exact load profile cannot accept residual risk")
if self.transport_security != "none_claimed":
raise ContractError("exact load profile cannot claim transport security")
if (
isinstance(self.session_timeout_seconds, bool)
or not isinstance(self.session_timeout_seconds, (int, float))
or not 0 < self.session_timeout_seconds <= 21_600
):
raise ContractError("exact load profile session timeout is outside its bound")
if not isinstance(self.source_mount, SourceMountAttestation):
raise ContractError("exact load profile source mount attestation is invalid")
if self.source_mount.evidence_tier != self.evidence_tier:
raise ContractError("exact load profile and source mount tiers disagree")
if not isinstance(self.bindings, Mapping) or set(self.bindings) != set(
EXTERNAL_LOAD_BINDING_FIELDS
):
raise ContractError("exact load profile bindings do not match the closed schema")
copied: dict[str, str] = {}
for name in sorted(EXTERNAL_LOAD_BINDING_FIELDS):
value = self.bindings[name]
if not isinstance(value, str) or _DIGEST_RE.fullmatch(value) is None:
raise ContractError("exact load profile contains an invalid digest")
copied[name] = value
object.__setattr__(self, "bindings", MappingProxyType(copied))
@dataclass(frozen=True)
class DataPlane:
"""One exact NCCL group and the one-dimensional CUDA mesh that owns it."""
group: Any
device_mesh: Any
digest: str
def __post_init__(self) -> None:
if not isinstance(self.digest, str) or _DIGEST_RE.fullmatch(self.digest) is None:
raise ContractError("data plane digest is invalid")
@dataclass(frozen=True)
class ModelSessionResult:
"""Terminal load evidence; the model and process groups have been torn down."""
preflight: PreflightResult
summary_set_digest: str
summaries: tuple[LoadEvidenceSummary, ...]
loaded_messages: tuple[StageMessage, ...]
logical_manifest_digest: str
local_manifest_digest: str
def __post_init__(self) -> None:
for value in (
self.summary_set_digest,
self.logical_manifest_digest,
self.local_manifest_digest,
):
if _DIGEST_RE.fullmatch(value) is None:
raise ContractError("model session result contains an invalid digest")
@dataclass(frozen=True)
class LiveModelSession:
"""A validated LOADED capability kept live only inside its context manager."""
preflight: PreflightResult
config: DistributedPreflightConfig
launch: TorchrunEnvironment
handle: Any
control_group: Any
data_plane: DataPlane
bindings: Mapping[str, str]
summary_set_digest: str
summaries: tuple[LoadEvidenceSummary, ...]
loaded_messages: tuple[StageMessage, ...]
def __post_init__(self) -> None:
if _DIGEST_RE.fullmatch(self.summary_set_digest) is None:
raise ContractError("live model session contains an invalid summary digest")
if not isinstance(self.bindings, Mapping) or set(self.bindings) != set(
LOAD_EVIDENCE_BINDING_FIELDS
):
raise ContractError("live model session bindings do not match the evidence schema")
if any(_DIGEST_RE.fullmatch(value) is None for value in self.bindings.values()):
raise ContractError("live model session contains an invalid binding digest")
object.__setattr__(self, "bindings", MappingProxyType(dict(self.bindings)))
class ProcessHardWatchdog:
"""Terminate the worker if native CUDA/NCCL work outlives the session bound."""
def __init__(
self,
timeout_seconds: float,
*,
exit_process: Callable[[int], object] = os._exit,
) -> None:
if (
isinstance(timeout_seconds, bool)
or not isinstance(timeout_seconds, (int, float))
or not 0 < timeout_seconds <= 21_600
):
raise ContractError("hard watchdog timeout is outside its bound")
self._timeout_seconds = float(timeout_seconds)
self._exit_process = exit_process
self._cancelled = threading.Event()
self._fired = threading.Event()
self._thread: threading.Thread | None = None
@property
def fired(self) -> bool:
return self._fired.is_set()
def __enter__(self) -> "ProcessHardWatchdog":
if self._thread is not None:
raise ContractError("hard watchdog cannot be reused")
def supervise() -> None:
if self._cancelled.wait(self._timeout_seconds):
return
self._fired.set()
self._exit_process(_HARD_EXIT_CODE)
self._thread = threading.Thread(
target=supervise,
daemon=True,
name="obliteratus-model-session-watchdog",
)
self._thread.start()
return self
def __exit__(self, _kind: object, _value: object, _traceback: object) -> None:
self._cancelled.set()
assert self._thread is not None
self._thread.join(1)
if self._thread.is_alive():
self._exit_process(_HARD_EXIT_CODE)
raise RuntimeContractError(
"LMS_CLEANUP_INCOMPLETE", "hard watchdog cleanup failed"
) from None
if self._fired.is_set():
raise RuntimeContractError(
"LMS_LOAD_STATE_UNCERTAIN", "hard model-session deadline expired"
) from None
def _run_supervised_entry(entrypoint: Callable[[], object]) -> None:
"""Child boundary that never lets an exception formatter disclose raw state."""
try:
entrypoint()
except BaseException:
os._exit(_HARD_EXIT_CODE)
os._exit(0)
def supervise_model_session_entry(
entrypoint: Callable[[], object],
*,
timeout_seconds: float,
termination_timeout_seconds: float = 5,
) -> None:
"""Run a rank entrypoint below a parent process that can kill native hangs."""
for value, name, maximum in (
(timeout_seconds, "supervisor timeout", 21_600),
(termination_timeout_seconds, "supervisor termination timeout", 60),
):
if (
isinstance(value, bool)
or not isinstance(value, (int, float))
or not 0 < value <= maximum
):
raise ContractError(f"{name} is outside its bound")
if not callable(entrypoint):
raise ContractError("supervisor entrypoint must be callable")
try:
process = multiprocessing.get_context("spawn").Process(
target=_run_supervised_entry,
args=(entrypoint,),
daemon=False,
name="obliteratus-model-session-worker",
)
process.start()
process.join(float(timeout_seconds))
if process.is_alive():
process.terminate()
process.join(float(termination_timeout_seconds))
if process.is_alive():
process.kill()
process.join(float(termination_timeout_seconds))
raise RuntimeContractError(
"LMS_LOAD_STATE_UNCERTAIN",
"supervised model session exceeded its process-hard deadline",
) from None
if process.exitcode != 0:
raise RuntimeContractError(
"LMS_LOAD_STATE_UNCERTAIN", "supervised model session did not exit cleanly"
) from None
except RuntimeContractError:
raise
except BaseException:
raise RuntimeContractError(
"LMS_LOAD_STATE_UNCERTAIN", "model-session supervisor failed"
) from None
def _bounded_destroy_data_group(
group: Any,
*,
timeout_seconds: float,
exit_process: Callable[[int], object],
) -> None:
import torch.distributed as dist
errors: list[BaseException] = []
def destroy() -> None:
try:
dist.destroy_process_group(group)
except BaseException as error:
errors.append(error)
thread = threading.Thread(target=destroy, daemon=True, name="obliteratus-nccl-teardown")
thread.start()
thread.join(float(timeout_seconds))
if thread.is_alive():
exit_process(_HARD_EXIT_CODE)
raise RuntimeContractError(
"LMS_CLEANUP_INCOMPLETE", "NCCL data-group teardown exceeded its bound"
) from None
if errors:
raise RuntimeContractError(
"LMS_CLEANUP_INCOMPLETE", "NCCL data-group teardown failed"
) from None
@contextmanager
def exact_nccl_data_plane(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
*,
exit_process: Callable[[int], object] = os._exit,
) -> Iterator[DataPlane]:
"""Create and destroy the only accepted ranks-[0,1] NCCL CUDA mesh."""
group: Any | None = None
try:
with _local_step(50, code="LMS_LOAD_PROFILE_MISMATCH"):
try:
layout = configured_tp2_layout(config, launch)
except RuntimeContractError:
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "model session requires a closed TP2 topology"
) from None
import torch.distributed as dist
from torch.distributed.device_mesh import init_device_mesh
if not dist.is_initialized() or str(dist.get_backend()).lower() != "gloo":
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "NCCL data plane requires the live Gloo group"
)
mesh = init_device_mesh(
"cuda",
(2,),
mesh_dim_names=("tp",),
backend_override={"tp": "nccl"},
)
with _local_step(51, code="LMS_LOAD_PROFILE_MISMATCH"):
group = mesh.get_group("tp")
ranks = list(dist.get_process_group_ranks(group))
if (
group is dist.group.WORLD
or ranks != [0, 1]
or str(dist.get_backend(group)).lower() != "nccl"
or mesh.ndim != 1
or mesh.size() != 2
or mesh.device_type != "cuda"
or mesh.mesh.tolist() != [0, 1]
or mesh.get_local_rank("tp") != launch.rank
):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "NCCL DeviceMesh identity is outside the profile"
)
digest = contract_digest(
{
"backend": "nccl",
"device_type": "cuda",
"mesh_dim_names": ["tp"],
"ranks": ranks,
"world_size": 2,
"deployment_layout": layout.value,
}
)
data_plane = DataPlane(group=group, device_mesh=mesh, digest=digest)
yield data_plane
except RuntimeContractError:
raise
except BaseException:
raise RuntimeContractError(
"LMS_MODEL_LOAD_FAILED", "NCCL data-plane initialization failed"
) from None
finally:
if group is not None:
_bounded_destroy_data_group(
group,
timeout_seconds=config.teardown_timeout_seconds,
exit_process=exit_process,
)
def _control_group_identity(launch: TorchrunEnvironment) -> tuple[Any, str]:
import torch.distributed as dist
if (
not dist.is_initialized()
or str(dist.get_backend()).lower() != "gloo"
or dist.get_rank() != launch.rank
or dist.get_world_size() != launch.world_size
):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "Gloo control-group identity changed"
)
group = dist.group.WORLD
ranks = list(dist.get_process_group_ranks(group))
if ranks != list(range(launch.world_size)):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "Gloo control-group rank order changed"
)
return group, contract_digest(
{"backend": "gloo", "ranks": ranks, "world_size": launch.world_size}
)
def _hardware_digest(preflight: PreflightResult, source_mount_digest: str) -> str:
return contract_digest(
{
"devices": [
{
"rank": item.rank,
"device_config_digest": item.device_config_digest,
"device_profile_digest": item.device_profile_digest,
}
for item in preflight.attestations
],
"source_mount_attestation_digest": source_mount_digest,
}
)
def _validate_preload_bindings(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
exact: ExactLoadProfile,
*,
source_mount_digest: str,
control_group_digest: str,
) -> dict[str, str]:
selection = config.require_model()
deployment_layout_digest = contract_digest(
{"layout": "protocol_cpu_noncoverage", "topology": preflight.topology}
)
if exact.evidence_tier in GPU_CANDIDATE_EVIDENCE_TIERS:
if config.device_kind != "cuda" or config.evidence_tier != "candidate_preflight":
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "GPU evidence tier disagrees with preflight"
)
try:
observed_runtime = observed_external_runtime_bindings()
except ContractError:
raise RuntimeContractError(
"LMS_DEPENDENCY_DRIFT", "live model runtime identity is unavailable"
) from None
if any(exact.bindings[name] != value for name, value in observed_runtime.items()):
raise RuntimeContractError(
"LMS_DEPENDENCY_DRIFT", "live model runtime identity differs from policy"
)
topology = classify_tp2_topology(config, launch, preflight)
expected_tier = {
TP2DeploymentLayout.LOCAL_TP2: "candidate_local_gpu",
TP2DeploymentLayout.TWO_HOST_TP2: "candidate_two_host_gpu",
}[topology.layout]
if exact.evidence_tier != expected_tier:
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "GPU evidence tier disagrees with TP2 layout"
)
deployment_layout_digest = topology.digest
elif config.device_kind != "cpu" or config.evidence_tier != "protocol_cpu":
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "protocol evidence tier disagrees with preflight"
)
return {
"code_digest": config.code_digest,
"config_digest": config.digest,
"control_group_digest": control_group_digest,
"deployment_layout_digest": deployment_layout_digest,
"hardware_digest": _hardware_digest(preflight, source_mount_digest),
"identity_digest": preflight.identity_digest,
"model_config_digest": selection.hf_config_digest,
"model_digest": preflight.source_identity.model_digest,
"plan_digest": config.placement_plan_digest,
"source_digest": preflight.source_identity.source_digest,
"tokenizer_digest": preflight.source_identity.tokenizer_digest,
"topology_digest": contract_digest(preflight.topology),
}
def _validate_handle_bindings(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
handle: Any,
data_plane: DataPlane,
) -> dict[str, str]:
try:
manifest = handle.manifest
local_record = manifest.to_dict()
expected = {
"data_group_digest": data_plane.digest,
"logical_manifest_digest": handle.manifest_digest,
"model_config_digest": handle.config.config_digest,
"plan_digest": handle.plan_digest,
"profile_digest": handle.profile.digest,
}
except (AttributeError, TypeError):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "loaded model handle is incomplete"
) from None
if (
handle.source_identity != preflight.source_identity
or handle.local_manifest_digest != manifest.local_digest
or local_record.get("rank") != launch.rank
or local_record.get("world_size") != launch.world_size
or config.require_model().hf_config_digest != handle.config.config_digest
):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "loaded model handle identity disagrees"
)
return expected
def _complete_evidence_bindings(
exact: ExactLoadProfile,
*derived_sets: Mapping[str, str],
) -> Mapping[str, str]:
bindings = dict(exact.bindings)
for derived in derived_sets:
for name, value in derived.items():
if name not in LOAD_EVIDENCE_BINDING_FIELDS:
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "model session derived an unknown binding"
)
if (existing := bindings.get(name)) is not None and existing != value:
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "model session binding derivations disagree"
)
bindings[name] = value
if set(bindings) != set(LOAD_EVIDENCE_BINDING_FIELDS) or any(
_DIGEST_RE.fullmatch(value) is None for value in bindings.values()
):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "model session evidence bindings are incomplete"
)
return MappingProxyType(bindings)
def _summary_from_bytes(payload: bytes) -> LoadEvidenceSummary:
def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
result: dict[str, object] = {}
for key, value in pairs:
if key in result:
raise ContractError("load summary contains a duplicate field")
result[key] = value
return result
try:
value = json.loads(
payload.decode("utf-8", errors="strict"), object_pairs_hook=unique_object
)
except (UnicodeError, json.JSONDecodeError, RecursionError):
raise ContractError("load summary is not strict canonical JSON") from None
if not isinstance(value, dict) or set(value) != _SUMMARY_FIELDS:
raise ContractError("load summary fields do not match the closed schema")
try:
summary = LoadEvidenceSummary(**value)
except (ContractError, TypeError):
raise ContractError("load summary fields are invalid") from None
if payload != canonical_record(summary):
raise ContractError("load summary is not canonical")
return summary
@contextmanager
def _local_step(sequence: int, *, code: str) -> Iterator[None]:
"""Vote on fallible session preparation before its next collective."""
error: BaseException | None = None
try:
yield
except BaseException as exc:
error = exc
local_code = translate_model_session_error(error).code if error is not None else None
try:
codes = gather_failure_codes(sequence, local_code)
except BaseException:
raise RuntimeContractError(code, "model session preparation vote failed") from None
if codes:
if error is None or local_code != codes[0]:
error = RuntimeContractError(codes[0], "model session preparation failed")
raise translate_model_session_error(error) from None
def _gather_load_summaries(local: LoadEvidenceSummary) -> tuple[LoadEvidenceSummary, ...]:
try:
payloads = gloo_all_gather_records(local)
with _local_step(48, code="LMS_LOAD_MANIFEST_INVALID"):
summaries = tuple(_summary_from_bytes(item) for item in payloads)
validate_loaded_summary_set(summaries)
return summaries
except ContractError:
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "rank load summaries did not form one fixed world"
) from None
def _emit_loaded_messages(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
summary_set_digest: str,
) -> tuple[StageMessage, ...]:
with _local_step(45, code="LMS_LOAD_MANIFEST_INVALID"):
advance_stage(RuntimeStage.PREFLIGHTED, RuntimeStage.LOADED)
identity_digest = contract_digest(
{
"lifecycle_identity_digest": preflight.lifecycle_identity_digest,
"load_summary_set_digest": summary_set_digest,
}
)
local = StageMessage(
run_id=config.run_id,
identity_digest=identity_digest,
rank=launch.rank,
sequence=2,
stage=RuntimeStage.LOADED,
vote=Vote.COMMITTED,
)
try:
payloads = gloo_all_gather_records(local)
except (ContractError, ValueError):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "LOADED lifecycle records are invalid"
) from None
with _local_step(46, code="LMS_LOAD_MANIFEST_INVALID"):
try:
messages = tuple(StageMessage.from_bytes(item) for item in payloads)
except (ContractError, ValueError):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "LOADED lifecycle records are invalid"
) from None
if len(messages) != config.world_size or [item.rank for item in messages] != list(
range(config.world_size)
):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "LOADED lifecycle records omit a fixed rank"
)
if any(
item.run_id != config.run_id
or item.identity_digest != identity_digest
or item.sequence != 2
or item.stage is not RuntimeStage.LOADED
or item.vote is not Vote.COMMITTED
or item.error_code is not None
for item in messages
):
raise RuntimeContractError("LMS_LOAD_MANIFEST_INVALID", "LOADED lifecycle records disagree")
if not unanimous_vote(3, True):
raise RuntimeContractError(
"LMS_LOAD_MANIFEST_INVALID", "LOADED lifecycle decision was not unanimous"
)
return messages
def _prepare_evidence_directory(
attempt_path: Path,
launch: TorchrunEnvironment,
*,
rank_local: bool = False,
) -> Path:
import torch
import torch.distributed as dist
directory_name = "model-load-evidence"
descriptor: int | None = None
descriptors: ExitStack | None = None
preparation_error: BaseException | None = None
try:
try:
with _local_step(52, code="LMS_LOAD_EVIDENCE_UNAVAILABLE"):
descriptors = ExitStack()
prepared = 1
try:
descriptor = os.open(
attempt_path,
os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0),
)
try:
descriptors.callback(os.close, descriptor)
except BaseException:
os.close(descriptor)
raise
if launch.rank == 0 or rank_local:
os.mkdir(directory_name, mode=0o700, dir_fd=descriptor)
except OSError:
prepared = 0
outcome = torch.tensor([prepared], dtype=torch.int32, device="cpu")
# Local preparation must be voted before any rank enters this collective.
dist.all_reduce(outcome, op=dist.ReduceOp.MIN)
except BaseException as exc:
preparation_error = exc
with _local_step(44, code="LMS_LOAD_EVIDENCE_UNAVAILABLE"):
try:
if preparation_error is not None:
raise preparation_error
if outcome.item() != 1:
raise ContractError("model-load evidence directory preparation failed")
child = os.open(
directory_name,
os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0),
dir_fd=descriptor,
)
try:
descriptors.callback(os.close, child)
except BaseException:
os.close(child)
raise
metadata = os.fstat(child)
effective_uid = getattr(os, "geteuid", lambda: metadata.st_uid)()
if (
not stat.S_ISDIR(metadata.st_mode)
or metadata.st_uid != effective_uid
or metadata.st_mode & 0o077
):
raise ContractError("model-load evidence directory is not private and worker-owned")
directory = attempt_path / directory_name
finally:
if descriptors is not None:
descriptors.close()
except (OSError, ContractError):
raise RuntimeContractError(
"LMS_LOAD_EVIDENCE_UNAVAILABLE", "model-load evidence directory is unavailable"
) from None
return directory
_CODE_TRANSLATION = {
"LMS_ATTEMPT_CANCELLED": "LMS_MODEL_LOAD_FAILED",
"LMS_COLLECTIVE_FAILED": "LMS_MODEL_LOAD_FAILED",
"LMS_CONSENSUS_RECORD_INVALID": "LMS_LOAD_MANIFEST_INVALID",
"LMS_EVIDENCE_UNAVAILABLE": "LMS_LOAD_EVIDENCE_UNAVAILABLE",
"LMS_IDENTITY_MISMATCH": "LMS_LOAD_PROFILE_MISMATCH",
"LMS_LIFECYCLE_INVALID": "LMS_LOAD_MANIFEST_INVALID",
"LMS_RESOURCE_ADMISSION_DENIED": "LMS_MODEL_ARTIFACT_UNSUPPORTED",
"LMS_RUNTIME_PROFILE_MISMATCH": "LMS_LOAD_PROFILE_MISMATCH",
"LMS_SOURCE_BOUNDARY_VIOLATION": "LMS_MODEL_ARTIFACT_UNSUPPORTED",
"LMS_STAGE_TIMEOUT": "LMS_LOAD_TIMEOUT",
"LMS_TOPOLOGY_UNSUPPORTED": "LMS_TP_PLAN_UNSUPPORTED",
}
def translate_model_session_error(error: BaseException) -> RuntimeContractError:
"""Discard raw error text and retain only a closed model-load failure code."""
if isinstance(error, AgreedTopologyRefusal):
return error
if isinstance(error, RuntimeContractError):
code = error.code if error.code in LOAD_ERROR_CODES else _CODE_TRANSLATION.get(error.code)
elif isinstance(error, TimeoutError):
code = "LMS_LOAD_TIMEOUT"
elif isinstance(error, KeyboardInterrupt):
code = "LMS_MODEL_LOAD_FAILED"
else:
code = "LMS_MODEL_LOAD_FAILED"
if code not in LOAD_ERROR_CODES:
code = "LMS_MODEL_LOAD_FAILED"
if code == "LMS_TP_PLAN_UNSUPPORTED":
return RuntimeContractError(
code,
"distributed model session topology is unsupported; live two-host TP2 "
"preflight is not qualified",
)
return RuntimeContractError(code, "distributed model session failed")
@contextmanager
def live_model_session(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
exact: ExactLoadProfile,
*,
probes: PreflightProbes | None = None,
protocol_loader: Callable[..., Any] | None = None,
protocol_data_plane: Callable[..., Any] | None = None,
exit_process: Callable[[int], object] = os._exit,
) -> Iterator[LiveModelSession]:
"""Yield one live LOADED capability under bounded, fail-closed ownership."""
if isinstance(config, DistributedPreflightConfig):
config = config.for_rank(launch.rank)
if not isinstance(exact, ExactLoadProfile):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "exact model-load profile is unavailable"
)
if exact.evidence_tier in GPU_CANDIDATE_EVIDENCE_TIERS:
if (
protocol_loader is not None
or protocol_data_plane is not None
or exit_process is not os._exit
):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "GPU candidate cannot use protocol test injection"
)
loader = load_distributed_model
def data_plane_context() -> Any:
return exact_nccl_data_plane(config, launch, exit_process=exit_process)
else:
if protocol_loader is None or protocol_data_plane is None:
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH",
"protocol-only session requires explicit non-production test adapters",
)
loader = protocol_loader
def data_plane_context() -> Any:
return protocol_data_plane(config, launch, exit_process=exit_process)
yielded = False
topology_refusal: AgreedTopologyRefusal | None = None
try:
with ProcessHardWatchdog(
exact.session_timeout_seconds,
exit_process=exit_process,
):
with runtime_diagnostic_guard(
teardown_timeout_seconds=config.teardown_timeout_seconds,
exit_process=exit_process,
):
with control_group(config, launch):
try:
preflight = run_live_preflight(config, launch, probes=probes)
except AgreedTopologyRefusal as error:
topology_refusal = error
raise
with _local_step(40, code="LMS_LOAD_PROFILE_MISMATCH"):
source_mount_digest = verify_source_mount_attestation(
config, preflight, exact.source_mount
)
control, control_digest = _control_group_identity(launch)
if getattr(config, "rank_storage", None) is None:
preload_bindings = _validate_preload_bindings(
config, launch, preflight, exact,
source_mount_digest=source_mount_digest,
control_group_digest=control_digest,
)
if getattr(config, "rank_storage", None) is not None:
from obliteratus.distributed.rank_local import exchange_summary
mount_summaries = exchange_summary(config.run_id, preflight.identity_digest,
launch.rank, "SOURCE_MOUNTS", 3, {"source_mount_digest": source_mount_digest},
group=control)
with _local_step(53, code="LMS_LOAD_PROFILE_MISMATCH"):
source_mount_digest = contract_digest(tuple(item["payload"] for item in mount_summaries))
with _local_step(54, code="LMS_LOAD_PROFILE_MISMATCH"):
preload_bindings = _validate_preload_bindings(
config, launch, preflight, exact,
source_mount_digest=source_mount_digest,
control_group_digest=control_digest,
)
with data_plane_context() as data_plane:
with _local_step(41, code="LMS_LOAD_PROFILE_MISMATCH"):
if not isinstance(data_plane, DataPlane):
raise RuntimeContractError(
"LMS_LOAD_PROFILE_MISMATCH", "data-plane adapter is invalid"
)
handle = loader(
config,
launch,
preflight,
device_mesh=data_plane.device_mesh,
control_group=control,
)
with _local_step(42, code="LMS_LOAD_MANIFEST_INVALID"):
if getattr(config, "rank_storage", None) is not None:
observed_root = config.staging_path.lstat()
if (observed_root.st_dev, observed_root.st_ino) != preflight.rank_local_root_identities[launch.rank]:
raise ContractError("rank-local root changed after preflight")
observed_attempt = preflight.attempt_path.lstat()
if (observed_attempt.st_dev, observed_attempt.st_ino) != preflight.rank_local_attempt_identities[launch.rank]:
raise ContractError("rank-local attempt changed after preflight")
handle_bindings = _validate_handle_bindings(
config,
launch,
preflight,
handle,
data_plane,
)
bindings = _complete_evidence_bindings(
exact,
preload_bindings,
handle_bindings,
)
if getattr(config, "rank_storage", None) is not None:
evidence_directory = _prepare_evidence_directory(preflight.attempt_path, launch, rank_local=True)
else:
evidence_directory = _prepare_evidence_directory(preflight.attempt_path, launch)
with _local_step(43, code="LMS_LOAD_EVIDENCE_UNAVAILABLE"):
record = build_rank_load_evidence(
result="loaded",
run_id=config.run_id,
rank=launch.rank,
world_size=launch.world_size,
evidence_tier=exact.evidence_tier,
bindings=bindings,
manifest=handle.manifest.to_dict(),
)
local_summary = publish_rank_load_evidence(evidence_directory, record)
summaries = _gather_load_summaries(local_summary)
with _local_step(49, code="LMS_LOAD_MANIFEST_INVALID"):
summary_set_digest = validate_loaded_summary_set(summaries)
messages = _emit_loaded_messages(
config,
launch,
preflight,
summary_set_digest,
)
with _local_step(47, code="LMS_LOAD_MANIFEST_INVALID"):
session = LiveModelSession(
preflight=preflight,
config=config,
launch=launch,
handle=handle,
control_group=control,
data_plane=data_plane,
bindings=bindings,
summary_set_digest=summary_set_digest,
summaries=summaries,
loaded_messages=messages,
)
yielded = True
yield session
del session, handle
except BaseException as error:
if topology_refusal is not None:
raise topology_refusal from None
if (
yielded
and isinstance(error, RuntimeContractError)
and error.code.startswith(
("LMS_PROBE_", "LMS_DISTILL_", "LMS_EXCISE_", "LMS_VERIFY_", "LMS_EXPORT_")
)
):
raise RuntimeContractError(error.code, "distributed downstream stage failed") from None
raise translate_model_session_error(error) from None
def execute_model_session(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
exact: ExactLoadProfile,
*,
probes: PreflightProbes | None = None,
protocol_loader: Callable[..., Any] | None = None,
protocol_data_plane: Callable[..., Any] | None = None,
exit_process: Callable[[int], object] = os._exit,
) -> ModelSessionResult:
"""Prove LOADED and tear down one exact session before returning evidence."""
with live_model_session(
config,
launch,
exact,
probes=probes,
protocol_loader=protocol_loader,
protocol_data_plane=protocol_data_plane,
exit_process=exit_process,
) as session:
result = ModelSessionResult(
preflight=session.preflight,
summary_set_digest=session.summary_set_digest,
summaries=session.summaries,
loaded_messages=session.loaded_messages,
logical_manifest_digest=session.handle.manifest_digest,
local_manifest_digest=session.handle.local_manifest_digest,
)
return result
__all__ = [
"DataPlane",
"EXTERNAL_LOAD_BINDING_FIELDS",
"ExactLoadProfile",
"LiveModelSession",
"ModelSessionResult",
"ProcessHardWatchdog",
"exact_nccl_data_plane",
"execute_model_session",
"live_model_session",
"supervise_model_session_entry",
"translate_model_session_error",
]