mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
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.
1244 lines
51 KiB
Python
1244 lines
51 KiB
Python
"""Fail-closed distributed PROBE and coordinator-only DISTILL runtime."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import time
|
|
from collections.abc import Callable, Iterator
|
|
from contextlib import ExitStack, contextmanager
|
|
from dataclasses import dataclass
|
|
from typing import Any, cast
|
|
|
|
import torch
|
|
import torch.distributed as dist
|
|
|
|
from obliteratus.distributed.consensus import (
|
|
decode_frame,
|
|
encode_frame,
|
|
gloo_all_gather_records,
|
|
require_consensus_digest,
|
|
unanimous_vote,
|
|
)
|
|
from obliteratus.distributed.contracts import (
|
|
MAX_CONSENSUS_BYTES,
|
|
ContractError,
|
|
RuntimeContractError,
|
|
RuntimeStage,
|
|
StageMessage,
|
|
Vote,
|
|
advance_stage,
|
|
canonical_record,
|
|
contract_digest,
|
|
)
|
|
from obliteratus.distributed.model_registry import LLAMA_BF16_TP2_PROFILE
|
|
from obliteratus.distributed.model_session import LiveModelSession
|
|
from obliteratus.distributed.preflight import _host_memory, revalidate_source
|
|
from obliteratus.distributed.probe_contracts import (
|
|
CoordinatorProbeCorpus,
|
|
LayerProbeArtifact,
|
|
LayerProbeSummary,
|
|
MAX_PROBE_LAYERS,
|
|
ProbeDistillConfig,
|
|
ProbeDistillResult,
|
|
ProbeDistillSummary,
|
|
ProbeSample,
|
|
compute_layer_artifacts,
|
|
layer_summary_set_digest,
|
|
prepare_probe_corpus,
|
|
tensor_digest,
|
|
)
|
|
|
|
|
|
PROBE_ERROR_CODES = frozenset(
|
|
{
|
|
"LMS_PROBE_ACTIVATION_DIVERGED",
|
|
"LMS_PROBE_CANCELLED",
|
|
"LMS_PROBE_COLLECTIVE_FAILED",
|
|
"LMS_PROBE_FAILED",
|
|
"LMS_PROBE_HOOK_UNSUPPORTED",
|
|
"LMS_PROBE_INPUT_INVALID",
|
|
"LMS_PROBE_PROFILE_MISMATCH",
|
|
"LMS_PROBE_RESOURCE_DENIED",
|
|
"LMS_PROBE_TIMEOUT",
|
|
"LMS_DISTILL_INVALID",
|
|
"LMS_DISTILL_DIVERGED",
|
|
}
|
|
)
|
|
_HOOK_SITE_RE = re.compile(r"^model\.layers\.(0|[1-9][0-9]{0,5})$")
|
|
_PLAN_FIELDS = frozenset(
|
|
{
|
|
"schema_version",
|
|
"tokenizer_contract_digest",
|
|
"corpus_digest",
|
|
"sample_ids_digest",
|
|
"tokenization_digest",
|
|
"harmful_count",
|
|
"harmless_count",
|
|
"vocab_size",
|
|
"pad_token_id",
|
|
"input_width",
|
|
"config_digest",
|
|
"loaded_identity_digest",
|
|
}
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProbeRuntime:
|
|
"""Minimal live capability needed by the stage adapter and its E1 oracle."""
|
|
|
|
run_id: str
|
|
loaded_identity_digest: str
|
|
rank: int
|
|
world_size: int
|
|
model: Any
|
|
manifest: Any
|
|
control_group: Any
|
|
data_group: Any
|
|
tensor_device: torch.device
|
|
hidden_size: int
|
|
available_host_bytes: int
|
|
|
|
def __post_init__(self) -> None:
|
|
if not isinstance(self.run_id, str) or re.fullmatch(r"[0-9a-f]{32}", self.run_id) is None:
|
|
raise ContractError("probe runtime run_id is invalid")
|
|
if (
|
|
not isinstance(self.loaded_identity_digest, str)
|
|
or re.fullmatch(r"[0-9a-f]{64}", self.loaded_identity_digest) is None
|
|
):
|
|
raise ContractError("probe runtime loaded identity is invalid")
|
|
if self.world_size != 2 or self.rank not in {0, 1}:
|
|
raise ContractError("probe runtime requires the exact TP2 world")
|
|
if not isinstance(self.tensor_device, torch.device) or self.tensor_device.type not in {
|
|
"cpu",
|
|
"cuda",
|
|
}:
|
|
raise ContractError("probe runtime tensor device is invalid")
|
|
if (
|
|
isinstance(self.hidden_size, bool)
|
|
or not isinstance(self.hidden_size, int)
|
|
or self.hidden_size < 1
|
|
or isinstance(self.available_host_bytes, bool)
|
|
or not isinstance(self.available_host_bytes, int)
|
|
or self.available_host_bytes < 1
|
|
):
|
|
raise ContractError("probe runtime resource inventory is invalid")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class _ProbePlan:
|
|
schema_version: int
|
|
tokenizer_contract_digest: str
|
|
corpus_digest: str
|
|
sample_ids_digest: str
|
|
tokenization_digest: str
|
|
harmful_count: int
|
|
harmless_count: int
|
|
vocab_size: int
|
|
pad_token_id: int
|
|
input_width: int
|
|
config_digest: str
|
|
loaded_identity_digest: str
|
|
|
|
|
|
def runtime_from_live_session(session: LiveModelSession) -> ProbeRuntime:
|
|
"""Narrow an exact LOADED capability into the PROBE runtime contract."""
|
|
|
|
if not isinstance(session, LiveModelSession):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "PROBE requires a live model session"
|
|
)
|
|
try:
|
|
handle = session.handle
|
|
manifest = handle.manifest
|
|
rank = manifest.rank
|
|
world_size = manifest.world_size
|
|
embedding = handle.model.get_input_embeddings()
|
|
tensor_device = next(embedding.parameters()).device
|
|
inventory = next(item for item in session.preflight.attestations if item.rank == rank)
|
|
except (AttributeError, StopIteration, TypeError):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "LOADED capability is incomplete"
|
|
) from None
|
|
if (
|
|
handle.profile is not LLAMA_BF16_TP2_PROFILE
|
|
or handle.manifest_digest != manifest.digest
|
|
or rank not in {0, 1}
|
|
or world_size != 2
|
|
or tensor_device.type != "cuda"
|
|
or tensor_device.index != session.launch.local_rank
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "LOADED capability is outside the exact profile"
|
|
)
|
|
loaded_identity = contract_digest(
|
|
{
|
|
"preflight_identity_digest": session.preflight.identity_digest,
|
|
"load_summary_set_digest": session.summary_set_digest,
|
|
"logical_manifest_digest": handle.manifest_digest,
|
|
"plan_digest": handle.plan_digest,
|
|
"data_group_digest": session.data_plane.digest,
|
|
}
|
|
)
|
|
return ProbeRuntime(
|
|
run_id=session.preflight.identity.run_id,
|
|
loaded_identity_digest=loaded_identity,
|
|
rank=rank,
|
|
world_size=world_size,
|
|
model=handle.model,
|
|
manifest=manifest,
|
|
control_group=session.control_group,
|
|
data_group=session.data_plane.group,
|
|
tensor_device=tensor_device,
|
|
hidden_size=handle.config.hidden_size,
|
|
available_host_bytes=inventory.free_host_memory_bytes,
|
|
)
|
|
|
|
|
|
def load_exact_probe_tokenizer(session: LiveModelSession) -> object:
|
|
"""Load the explicit fast tokenizer on rank zero from the attested local source."""
|
|
|
|
runtime = runtime_from_live_session(session)
|
|
if runtime.rank != 0:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "only rank zero may load the probe tokenizer"
|
|
)
|
|
try:
|
|
from transformers import PreTrainedTokenizerFast
|
|
|
|
tokenizer = PreTrainedTokenizerFast.from_pretrained(
|
|
str(session.config.source_path),
|
|
local_files_only=True,
|
|
trust_remote_code=False,
|
|
revision=session.config.require_model().artifact_revision,
|
|
token=None,
|
|
)
|
|
revalidate_source(session.config, session.preflight.source_identity)
|
|
except RuntimeContractError:
|
|
raise
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "the exact local tokenizer could not be loaded"
|
|
) from None
|
|
if (
|
|
type(tokenizer).__module__ != "transformers.tokenization_utils_tokenizers"
|
|
or type(tokenizer).__name__ != "TokenizersBackend"
|
|
or not bool(getattr(tokenizer, "is_fast", False))
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "tokenizer implementation is outside the exact profile"
|
|
)
|
|
return tokenizer
|
|
|
|
|
|
def _group_identity(runtime: ProbeRuntime) -> None:
|
|
try:
|
|
if (
|
|
not dist.is_available()
|
|
or not dist.is_initialized()
|
|
or str(dist.get_backend(runtime.control_group)).lower() != "gloo"
|
|
or dist.get_rank(runtime.control_group) != runtime.rank
|
|
or dist.get_world_size(runtime.control_group) != runtime.world_size
|
|
or list(dist.get_process_group_ranks(runtime.control_group)) != [0, 1]
|
|
or dist.get_rank(runtime.data_group) != runtime.rank
|
|
or dist.get_world_size(runtime.data_group) != runtime.world_size
|
|
or list(dist.get_process_group_ranks(runtime.data_group)) != [0, 1]
|
|
):
|
|
raise RuntimeError
|
|
data_backend = str(dist.get_backend(runtime.data_group)).lower()
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "probe process-group identity is invalid"
|
|
) from None
|
|
expected = "nccl" if runtime.tensor_device.type == "cuda" else "gloo"
|
|
if data_backend != expected:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "probe tensor device and data group disagree"
|
|
)
|
|
|
|
|
|
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("broadcast record contains a duplicate field")
|
|
result[key] = value
|
|
return result
|
|
|
|
|
|
def _broadcast_record(runtime: ProbeRuntime, record: object | None) -> dict[str, object]:
|
|
with _local_step(runtime, 6_300_000, code="LMS_PROBE_COLLECTIVE_FAILED"):
|
|
if runtime.rank == 0:
|
|
if record is None:
|
|
raise ContractError("coordinator broadcast record is missing")
|
|
payload = canonical_record(record)
|
|
frame = encode_frame(payload)
|
|
else:
|
|
if record is not None:
|
|
raise ContractError("non-coordinator supplied a coordinator record")
|
|
frame = torch.zeros(4 + MAX_CONSENSUS_BYTES, dtype=torch.uint8)
|
|
try:
|
|
dist.broadcast(frame, src=0, group=runtime.control_group)
|
|
except RuntimeContractError:
|
|
raise
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "bounded control-record broadcast failed"
|
|
) from None
|
|
with _local_step(runtime, 6_300_001, code="LMS_PROBE_COLLECTIVE_FAILED"):
|
|
payload = decode_frame(frame)
|
|
value = json.loads(
|
|
payload.decode("utf-8", errors="strict"), object_pairs_hook=_unique_object
|
|
)
|
|
if not isinstance(value, dict) or canonical_record(value) != payload:
|
|
raise ContractError("broadcast record is not canonical")
|
|
return value
|
|
|
|
|
|
def _collective_accept(
|
|
runtime: ProbeRuntime,
|
|
sequence: int,
|
|
accepted: bool,
|
|
*,
|
|
code: str,
|
|
message: str,
|
|
) -> None:
|
|
try:
|
|
if not unanimous_vote(sequence, accepted, group=runtime.control_group):
|
|
raise RuntimeContractError(code, message)
|
|
except RuntimeContractError:
|
|
raise
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "probe rank vote failed"
|
|
) from None
|
|
|
|
|
|
@contextmanager
|
|
def _local_step(
|
|
runtime: ProbeRuntime,
|
|
sequence: int,
|
|
*,
|
|
code: str = "LMS_PROBE_FAILED",
|
|
message: str = "not every rank completed local PROBE preparation",
|
|
) -> Iterator[None]:
|
|
"""Vote on fallible local work; the body must contain no collectives."""
|
|
ready = True
|
|
try:
|
|
yield
|
|
except BaseException:
|
|
ready = False
|
|
_collective_accept(runtime, sequence, ready, code=code, message=message)
|
|
|
|
|
|
def _validate_plan(
|
|
record: dict[str, object], runtime: ProbeRuntime, config: ProbeDistillConfig
|
|
) -> _ProbePlan:
|
|
if set(record) != _PLAN_FIELDS:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "probe input plan fields do not match the closed schema"
|
|
)
|
|
digest_fields = (
|
|
"tokenizer_contract_digest",
|
|
"corpus_digest",
|
|
"sample_ids_digest",
|
|
"tokenization_digest",
|
|
"config_digest",
|
|
"loaded_identity_digest",
|
|
)
|
|
for name in digest_fields:
|
|
value = record[name]
|
|
if not isinstance(value, str) or re.fullmatch(r"[0-9a-f]{64}", value) is None:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "probe input plan contains an invalid digest"
|
|
)
|
|
integers = (
|
|
"schema_version",
|
|
"harmful_count",
|
|
"harmless_count",
|
|
"vocab_size",
|
|
"pad_token_id",
|
|
"input_width",
|
|
)
|
|
if any(
|
|
isinstance(record[name], bool) or not isinstance(record[name], int) for name in integers
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "probe input plan contains an invalid integer"
|
|
)
|
|
harmful = cast(int, record["harmful_count"])
|
|
harmless = cast(int, record["harmless_count"])
|
|
vocab = cast(int, record["vocab_size"])
|
|
input_width = cast(int, record["input_width"])
|
|
pad_token_id = cast(int, record["pad_token_id"])
|
|
if (
|
|
record["schema_version"] != 1
|
|
or not 1 <= harmful == harmless <= config.max_samples_per_class
|
|
or not 1 <= input_width <= config.max_tokens
|
|
or not 1 <= vocab <= 2**31 - 1
|
|
or not 0 <= pad_token_id < vocab
|
|
or record["config_digest"] != config.digest
|
|
or record["loaded_identity_digest"] != runtime.loaded_identity_digest
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "probe input plan is outside the exact profile"
|
|
)
|
|
return _ProbePlan(
|
|
schema_version=cast(int, record["schema_version"]),
|
|
tokenizer_contract_digest=cast(str, record["tokenizer_contract_digest"]),
|
|
corpus_digest=cast(str, record["corpus_digest"]),
|
|
sample_ids_digest=cast(str, record["sample_ids_digest"]),
|
|
tokenization_digest=cast(str, record["tokenization_digest"]),
|
|
harmful_count=harmful,
|
|
harmless_count=harmless,
|
|
vocab_size=vocab,
|
|
pad_token_id=pad_token_id,
|
|
input_width=input_width,
|
|
config_digest=cast(str, record["config_digest"]),
|
|
loaded_identity_digest=cast(str, record["loaded_identity_digest"]),
|
|
)
|
|
|
|
|
|
def _hook_sites(runtime: ProbeRuntime) -> tuple[str, ...]:
|
|
with _local_step(runtime, 6_300_010, code="LMS_PROBE_HOOK_UNSUPPORTED"):
|
|
sites = tuple(runtime.manifest.activation_sites)
|
|
manifest_digest = runtime.manifest.digest
|
|
expected = tuple(f"model.layers.{index}" for index in range(len(sites)))
|
|
if (
|
|
not sites
|
|
or len(sites) > MAX_PROBE_LAYERS
|
|
or sites != expected
|
|
or any(_HOOK_SITE_RE.fullmatch(site) is None for site in sites)
|
|
):
|
|
raise ContractError("model activation sites are partial or unsupported")
|
|
sites_digest = contract_digest(sites)
|
|
try:
|
|
require_consensus_digest(manifest_digest, group=runtime.control_group)
|
|
require_consensus_digest(sites_digest, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_HOOK_UNSUPPORTED", "rank activation-site manifests disagree"
|
|
) from None
|
|
return sites
|
|
|
|
|
|
def _resource_admission(
|
|
runtime: ProbeRuntime,
|
|
config: ProbeDistillConfig,
|
|
*,
|
|
sample_count: int,
|
|
site_count: int,
|
|
input_width: int = 0,
|
|
) -> None:
|
|
with _local_step(
|
|
runtime,
|
|
61_001,
|
|
code="LMS_PROBE_RESOURCE_DENIED",
|
|
message="probe activation working set was not admitted",
|
|
):
|
|
required = _probe_peak_host_bytes(
|
|
runtime, config, sample_count=sample_count, site_count=site_count,
|
|
input_width=input_width,
|
|
)
|
|
total, available = _host_memory()
|
|
if (
|
|
isinstance(total, bool) or not isinstance(total, int) or total < 1
|
|
or isinstance(available, bool) or not isinstance(available, int)
|
|
or not 0 <= available <= total
|
|
):
|
|
raise ContractError("fresh probe host memory observation is invalid")
|
|
# DISTILL retains received CPU means/directions on every rank. On two hosts
|
|
# rank zero's inventory cannot admit the worker's independent RAM budget;
|
|
# conservatively admit the full bounded working set on each local host.
|
|
accepted = (
|
|
config.selected_layer_count <= site_count
|
|
and config.n_directions <= min(sample_count // 2, runtime.hidden_size)
|
|
and required <= config.max_activation_bytes
|
|
and required <= min(runtime.available_host_bytes, available) // 2
|
|
)
|
|
if not accepted:
|
|
raise ContractError("probe activation working set exceeds the admitted bound")
|
|
|
|
|
|
def _probe_peak_host_bytes(
|
|
runtime: ProbeRuntime,
|
|
config: ProbeDistillConfig,
|
|
*,
|
|
sample_count: int,
|
|
site_count: int,
|
|
input_width: int = 0,
|
|
) -> int:
|
|
"""Conservative tensor payload allowance, not an allocator peak guarantee.
|
|
|
|
Includes retained batches plus concatenated matrices, source plus received
|
|
artifacts, captured CPU batches, reference/error temporaries and token
|
|
sources plus broadcasts. CUDA runs are charged the same CPU upper bound.
|
|
Float64 DISTILL gets space for successive layer temporaries and SVD's
|
|
reduced U/S/Vh and workspace. The separate half-free-RAM rule reserves
|
|
allocator/library headroom and covers both ranks when they share a host.
|
|
"""
|
|
width = runtime.hidden_size
|
|
batch = min(sample_count, config.batch_size)
|
|
activation = sample_count * site_count * width * 4
|
|
artifacts = site_count * (2 + config.n_directions) * width * 4
|
|
capture = batch * site_count * width * 4
|
|
references_and_errors = batch * width * 64
|
|
tokens = batch * input_width * 64
|
|
distill = 64 * (sample_count + 2) * width
|
|
if config.direction_method.value == "svd":
|
|
paired = sample_count // 2
|
|
reduced = min(paired, width)
|
|
distill += 32 * (paired * reduced + reduced * width + reduced * reduced)
|
|
return 2 * activation + 2 * artifacts + capture + references_and_errors + tokens + distill
|
|
|
|
|
|
def _broadcast_tensor(
|
|
runtime: ProbeRuntime,
|
|
source: torch.Tensor | None,
|
|
*,
|
|
shape: tuple[int, ...],
|
|
dtype: torch.dtype,
|
|
) -> torch.Tensor:
|
|
with _local_step(runtime, 6_300_020, code="LMS_PROBE_COLLECTIVE_FAILED"):
|
|
if runtime.rank == 0:
|
|
if not isinstance(source, torch.Tensor) or tuple(source.shape) != shape:
|
|
raise ContractError("coordinator tensor shape disagrees")
|
|
tensor = source.detach().to(device=runtime.tensor_device, dtype=dtype).contiguous()
|
|
else:
|
|
if source is not None:
|
|
raise ContractError("non-coordinator supplied a coordinator tensor")
|
|
tensor = torch.empty(shape, dtype=dtype, device=runtime.tensor_device)
|
|
try:
|
|
dist.broadcast(tensor, src=0, group=runtime.data_group)
|
|
return tensor
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "bounded data tensor broadcast failed"
|
|
) from None
|
|
|
|
|
|
def _update_token_hasher(
|
|
hasher: Any,
|
|
input_ids: torch.Tensor,
|
|
attention_mask: torch.Tensor,
|
|
) -> None:
|
|
for index in range(input_ids.shape[0]):
|
|
hasher.update(bytes.fromhex(tensor_digest(input_ids[index])))
|
|
hasher.update(bytes.fromhex(tensor_digest(attention_mask[index])))
|
|
|
|
|
|
def _update_activation_hasher(hasher: Any, activation: torch.Tensor) -> None:
|
|
hasher.update(bytes.fromhex(tensor_digest(activation)))
|
|
|
|
|
|
def _max_error(
|
|
local: torch.Tensor,
|
|
reference: torch.Tensor,
|
|
runtime: ProbeRuntime,
|
|
) -> tuple[float, float]:
|
|
with _local_step(runtime, 6_300_030, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
difference = (local - reference).abs().to(dtype=torch.float64)
|
|
maximum_absolute = difference.max().reshape(1).cpu()
|
|
relative = difference / reference.detach().abs().to(dtype=torch.float64).clamp_min(1e-12)
|
|
maximum_relative = relative.max().reshape(1).cpu()
|
|
try:
|
|
dist.all_reduce(maximum_absolute, op=dist.ReduceOp.MAX, group=runtime.control_group)
|
|
dist.all_reduce(maximum_relative, op=dist.ReduceOp.MAX, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "activation error aggregation failed"
|
|
) from None
|
|
with _local_step(runtime, 6_300_031, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
errors = float(maximum_absolute.item()), float(maximum_relative.item())
|
|
return errors
|
|
|
|
|
|
def _emit_stage(
|
|
runtime: ProbeRuntime,
|
|
*,
|
|
current: RuntimeStage,
|
|
requested: RuntimeStage,
|
|
sequence: int,
|
|
identity_digest: str,
|
|
) -> tuple[StageMessage, ...]:
|
|
with _local_step(
|
|
runtime, 6_300_040, code="LMS_PROBE_COLLECTIVE_FAILED",
|
|
message="probe lifecycle consensus failed",
|
|
):
|
|
advance_stage(current, requested)
|
|
local = StageMessage(
|
|
run_id=runtime.run_id,
|
|
identity_digest=identity_digest,
|
|
rank=runtime.rank,
|
|
sequence=sequence,
|
|
stage=requested,
|
|
vote=Vote.COMMITTED,
|
|
)
|
|
try:
|
|
payloads = gloo_all_gather_records(local, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "probe lifecycle consensus failed"
|
|
) from None
|
|
with _local_step(
|
|
runtime, 6_300_041, code="LMS_PROBE_COLLECTIVE_FAILED",
|
|
message="probe lifecycle records disagree or omitted a fixed rank",
|
|
):
|
|
messages = tuple(
|
|
StageMessage.from_bytes(payload)
|
|
for payload in payloads
|
|
)
|
|
if len(messages) != runtime.world_size or tuple(item.rank for item in messages) != (0, 1):
|
|
raise ContractError("probe lifecycle omitted a fixed rank")
|
|
if any(
|
|
item.run_id != runtime.run_id
|
|
or item.identity_digest != identity_digest
|
|
or item.sequence != sequence
|
|
or item.stage is not requested
|
|
or item.vote is not Vote.COMMITTED
|
|
or item.error_code is not None
|
|
for item in messages
|
|
):
|
|
raise ContractError("probe lifecycle records disagree")
|
|
return messages
|
|
|
|
|
|
def _check_continue(
|
|
runtime: ProbeRuntime,
|
|
config: ProbeDistillConfig,
|
|
*,
|
|
started: float,
|
|
sequence: int,
|
|
cancellation_requested: Callable[[], bool],
|
|
) -> None:
|
|
try:
|
|
cancelled = bool(cancellation_requested())
|
|
except BaseException:
|
|
cancelled = True
|
|
try:
|
|
timed_out = time.monotonic() - started > config.stage_timeout_seconds
|
|
except BaseException:
|
|
timed_out = True
|
|
with _local_step(runtime, 6_300_050, code="LMS_PROBE_COLLECTIVE_FAILED"):
|
|
flags = torch.tensor((int(cancelled), int(timed_out), sequence), dtype=torch.int64)
|
|
gathered = [torch.zeros_like(flags) for _rank in range(runtime.world_size)]
|
|
try:
|
|
dist.all_gather(gathered, flags, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "probe cancellation consensus failed"
|
|
) from None
|
|
with _local_step(
|
|
runtime, 6_300_051, code="LMS_PROBE_COLLECTIVE_FAILED",
|
|
message="probe cancellation sequence disagrees",
|
|
):
|
|
if any(int(item[2]) != sequence for item in gathered):
|
|
raise ContractError("probe cancellation sequence disagrees")
|
|
cancelled = any(bool(int(item[0])) for item in gathered)
|
|
timed_out = any(bool(int(item[1])) for item in gathered)
|
|
if cancelled:
|
|
raise RuntimeContractError("LMS_PROBE_CANCELLED", "distributed PROBE was cancelled")
|
|
if timed_out:
|
|
raise RuntimeContractError("LMS_PROBE_TIMEOUT", "distributed PROBE exceeded its bound")
|
|
|
|
|
|
@contextmanager
|
|
def _install_hooks(
|
|
runtime: ProbeRuntime,
|
|
sites: tuple[str, ...],
|
|
active_mask: list[torch.Tensor | None],
|
|
captured: dict[str, torch.Tensor],
|
|
) -> Iterator[None]:
|
|
handles: list[Any] = []
|
|
try:
|
|
for site in sites:
|
|
try:
|
|
module = runtime.model.get_submodule(site)
|
|
except (AttributeError, KeyError):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_HOOK_UNSUPPORTED", "an allowlisted hook module is unavailable"
|
|
) from None
|
|
|
|
def capture(
|
|
_module: object, _inputs: object, output: object, *, name: str = site
|
|
) -> None:
|
|
if name in captured:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_HOOK_UNSUPPORTED", "an allowlisted hook fired more than once"
|
|
)
|
|
hidden = output[0] if isinstance(output, tuple) and output else output
|
|
mask = active_mask[0]
|
|
if (
|
|
not isinstance(hidden, torch.Tensor)
|
|
or mask is None
|
|
or hidden.layout is not torch.strided
|
|
or hidden.ndim != 3
|
|
or hidden.shape[:2] != mask.shape
|
|
or hidden.shape[2] != runtime.hidden_size
|
|
or hidden.device != runtime.tensor_device
|
|
or not hidden.is_floating_point()
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_HOOK_UNSUPPORTED", "hook output is partial or has changed layout"
|
|
)
|
|
positions = mask.sum(dim=1, dtype=torch.int64) - 1
|
|
rows = torch.arange(hidden.shape[0], device=hidden.device)
|
|
value = hidden[rows, positions].detach().to(dtype=torch.float32).contiguous()
|
|
if not torch.isfinite(value).all():
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_HOOK_UNSUPPORTED", "hook output contains a non-finite value"
|
|
)
|
|
captured[name] = value
|
|
|
|
handles.append(module.register_forward_hook(capture))
|
|
yield
|
|
finally:
|
|
for handle in handles:
|
|
try:
|
|
handle.remove()
|
|
except BaseException:
|
|
pass
|
|
|
|
|
|
def _translate_probe_error(error: BaseException) -> RuntimeContractError:
|
|
if isinstance(error, RuntimeContractError) and error.code in PROBE_ERROR_CODES:
|
|
return error
|
|
if isinstance(error, TimeoutError):
|
|
return RuntimeContractError("LMS_PROBE_TIMEOUT", "distributed PROBE failed")
|
|
if isinstance(error, ContractError):
|
|
return RuntimeContractError("LMS_DISTILL_INVALID", "distributed PROBE failed")
|
|
return RuntimeContractError("LMS_PROBE_FAILED", "distributed PROBE failed")
|
|
|
|
|
|
def run_distributed_probe_distill(
|
|
runtime: ProbeRuntime,
|
|
config: ProbeDistillConfig,
|
|
*,
|
|
coordinator_corpus: CoordinatorProbeCorpus | None,
|
|
cancellation_requested: Callable[[], bool] = lambda: False,
|
|
) -> ProbeDistillResult:
|
|
"""Run identical inputs on every rank and compute decisions only on rank zero."""
|
|
|
|
try:
|
|
if not isinstance(runtime, ProbeRuntime):
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "probe runtime or config is invalid"
|
|
)
|
|
with _local_step(runtime, 6_300_100, code="LMS_PROBE_PROFILE_MISMATCH"):
|
|
if not isinstance(config, ProbeDistillConfig):
|
|
raise ContractError("probe config is invalid")
|
|
started = time.monotonic()
|
|
_group_identity(runtime)
|
|
config_digest = config.digest
|
|
try:
|
|
require_consensus_digest(config_digest, group=runtime.control_group)
|
|
require_consensus_digest(runtime.loaded_identity_digest, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_PROFILE_MISMATCH", "rank probe profiles disagree"
|
|
) from None
|
|
corpus_role_ok = (
|
|
isinstance(coordinator_corpus, CoordinatorProbeCorpus)
|
|
if runtime.rank == 0
|
|
else coordinator_corpus is None
|
|
)
|
|
_collective_accept(
|
|
runtime,
|
|
61_000,
|
|
corpus_role_ok,
|
|
code="LMS_PROBE_INPUT_INVALID",
|
|
message="only rank zero may own the probe corpus",
|
|
)
|
|
sites = _hook_sites(runtime)
|
|
with _local_step(runtime, 6_300_101, code="LMS_PROBE_INPUT_INVALID"):
|
|
plan_source = None
|
|
if runtime.rank == 0:
|
|
assert coordinator_corpus is not None
|
|
plan_source = {
|
|
**coordinator_corpus.plan_record(),
|
|
"config_digest": config_digest,
|
|
"loaded_identity_digest": runtime.loaded_identity_digest,
|
|
}
|
|
plan_record = _broadcast_record(runtime, plan_source)
|
|
with _local_step(runtime, 6_300_102, code="LMS_PROBE_INPUT_INVALID"):
|
|
plan = _validate_plan(plan_record, runtime, config)
|
|
total_samples = plan.harmful_count + plan.harmless_count
|
|
_resource_admission(
|
|
runtime,
|
|
config,
|
|
sample_count=total_samples,
|
|
site_count=len(sites),
|
|
input_width=plan.input_width,
|
|
)
|
|
_check_continue(
|
|
runtime,
|
|
config,
|
|
started=started,
|
|
sequence=61_002,
|
|
cancellation_requested=cancellation_requested,
|
|
)
|
|
|
|
hooks: ExitStack | None = None
|
|
try:
|
|
with _local_step(runtime, 6_300_103, code="LMS_PROBE_HOOK_UNSUPPORTED"):
|
|
hooks = ExitStack()
|
|
token_hasher = hashlib.sha256()
|
|
token_hasher.update(b"obliteratus-probe-token-rows-v1\0")
|
|
activation_hashers = {site: hashlib.sha256() for site in sites}
|
|
for site, hasher in activation_hashers.items():
|
|
hasher.update(b"obliteratus-probe-activation-stream-v1\0")
|
|
hasher.update(site.encode("ascii"))
|
|
hasher.update(b"\0")
|
|
activation_errors = {site: [0.0, 0.0] for site in sites}
|
|
coordinator_activations: dict[str, list[torch.Tensor]] = {
|
|
site: [] for site in sites
|
|
}
|
|
active_mask: list[torch.Tensor | None] = [None]
|
|
captured: dict[str, torch.Tensor] = {}
|
|
hooks.enter_context(_install_hooks(runtime, sites, active_mask, captured))
|
|
for batch_index, start in enumerate(range(0, total_samples, config.batch_size)):
|
|
_check_continue(
|
|
runtime,
|
|
config,
|
|
started=started,
|
|
sequence=61_100 + batch_index * 4,
|
|
cancellation_requested=cancellation_requested,
|
|
)
|
|
with _local_step(runtime, 6_300_104, code="LMS_PROBE_INPUT_INVALID"):
|
|
stop = min(total_samples, start + config.batch_size)
|
|
size = stop - start
|
|
width = plan.input_width
|
|
ids_source = mask_source = None
|
|
if runtime.rank == 0:
|
|
assert coordinator_corpus is not None
|
|
ids_source = torch.tensor(
|
|
coordinator_corpus.input_ids[start:stop], dtype=torch.int64
|
|
)
|
|
mask_source = torch.tensor(
|
|
coordinator_corpus.attention_mask[start:stop], dtype=torch.int64
|
|
)
|
|
input_ids = _broadcast_tensor(
|
|
runtime, ids_source, shape=(size, width), dtype=torch.int64
|
|
)
|
|
attention_mask = _broadcast_tensor(
|
|
runtime, mask_source, shape=(size, width), dtype=torch.int64
|
|
)
|
|
with _local_step(runtime, 6_300_105, code="LMS_PROBE_INPUT_INVALID"):
|
|
valid_input = bool(
|
|
torch.all((attention_mask == 0) | (attention_mask == 1))
|
|
and torch.all(attention_mask.sum(dim=1) > 0)
|
|
and torch.all(attention_mask[:, 1:] <= attention_mask[:, :-1])
|
|
and torch.all(input_ids >= 0)
|
|
and torch.all(input_ids < plan.vocab_size)
|
|
and torch.all(
|
|
input_ids.masked_select(attention_mask == 0) == plan.pad_token_id
|
|
)
|
|
)
|
|
if not valid_input:
|
|
raise ContractError("broadcast token tensors violate the input contract")
|
|
_update_token_hasher(token_hasher, input_ids, attention_mask)
|
|
captured.clear()
|
|
active_mask[0] = attention_mask
|
|
with _local_step(runtime, 6_300_115, code="LMS_PROBE_HOOK_UNSUPPORTED"):
|
|
if bool(getattr(runtime.model, "training", True)):
|
|
raise ContractError("probe model must remain in eval mode")
|
|
forward_succeeded = True
|
|
try:
|
|
with torch.inference_mode():
|
|
runtime.model(
|
|
input_ids=input_ids,
|
|
attention_mask=attention_mask,
|
|
use_cache=False,
|
|
)
|
|
except BaseException:
|
|
forward_succeeded = False
|
|
finally:
|
|
active_mask[0] = None
|
|
with _local_step(
|
|
runtime,
|
|
90_000 + batch_index,
|
|
code="LMS_PROBE_HOOK_UNSUPPORTED",
|
|
message="not every allowlisted hook produced one finite complete value",
|
|
):
|
|
if not forward_succeeded or set(captured) != set(sites):
|
|
raise ContractError("probe forward or hook capture failed")
|
|
for site_index, site in enumerate(sites):
|
|
with _local_step(runtime, 6_300_106, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
local = captured[site]
|
|
reference = local.clone() if runtime.rank == 0 else torch.empty_like(local)
|
|
try:
|
|
dist.broadcast(reference, src=0, group=runtime.data_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_COLLECTIVE_FAILED", "activation reference broadcast failed"
|
|
) from None
|
|
with _local_step(runtime, 6_300_107, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
within_rule = bool(
|
|
torch.allclose(
|
|
local,
|
|
reference,
|
|
atol=config.activation_atol,
|
|
rtol=config.activation_rtol,
|
|
)
|
|
)
|
|
maximum_absolute, maximum_relative = _max_error(local, reference, runtime)
|
|
with _local_step(runtime, 6_300_127, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
activation_errors[site][0] = max(
|
|
activation_errors[site][0], maximum_absolute
|
|
)
|
|
activation_errors[site][1] = max(
|
|
activation_errors[site][1], maximum_relative
|
|
)
|
|
_collective_accept(
|
|
runtime,
|
|
100_000 + batch_index * MAX_PROBE_LAYERS + site_index,
|
|
within_rule,
|
|
code="LMS_PROBE_ACTIVATION_DIVERGED",
|
|
message="replicated hook activations exceeded the preregistered rule",
|
|
)
|
|
with _local_step(runtime, 6_300_108, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
reference_cpu = reference.detach().cpu().contiguous()
|
|
_update_activation_hasher(activation_hashers[site], reference_cpu)
|
|
if runtime.rank == 0:
|
|
coordinator_activations[site].append(reference_cpu)
|
|
_check_continue(
|
|
runtime,
|
|
config,
|
|
started=started,
|
|
sequence=61_103 + batch_index * 4,
|
|
cancellation_requested=cancellation_requested,
|
|
)
|
|
with _local_step(runtime, 6_300_116, code="LMS_PROBE_HOOK_UNSUPPORTED"):
|
|
hooks.close()
|
|
finally:
|
|
if hooks is not None:
|
|
try:
|
|
hooks.close()
|
|
except BaseException:
|
|
pass
|
|
with _local_step(runtime, 6_300_109, code="LMS_PROBE_INPUT_INVALID"):
|
|
tokenization_digest = token_hasher.hexdigest()
|
|
_collective_accept(
|
|
runtime,
|
|
61_900,
|
|
tokenization_digest == plan.tokenization_digest,
|
|
code="LMS_PROBE_INPUT_INVALID",
|
|
message="broadcast token content disagrees with the coordinator plan",
|
|
)
|
|
with _local_step(runtime, 6_300_110, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
activation_digests = {site: activation_hashers[site].hexdigest() for site in sites}
|
|
try:
|
|
for digest in activation_digests.values():
|
|
require_consensus_digest(digest, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_ACTIVATION_DIVERGED", "rank activation streams disagree"
|
|
) from None
|
|
with _local_step(runtime, 6_300_111, code="LMS_PROBE_ACTIVATION_DIVERGED"):
|
|
probe_identity = contract_digest(
|
|
{
|
|
"loaded_identity_digest": runtime.loaded_identity_digest,
|
|
"config_digest": config_digest,
|
|
"corpus_digest": plan.corpus_digest,
|
|
"tokenization_digest": tokenization_digest,
|
|
"activation_digests": activation_digests,
|
|
}
|
|
)
|
|
probed_messages = _emit_stage(
|
|
runtime,
|
|
current=RuntimeStage.LOADED,
|
|
requested=RuntimeStage.PROBED,
|
|
sequence=3,
|
|
identity_digest=probe_identity,
|
|
)
|
|
_check_continue(
|
|
runtime,
|
|
config,
|
|
started=started,
|
|
sequence=61_901,
|
|
cancellation_requested=cancellation_requested,
|
|
)
|
|
|
|
coordinator_artifacts: tuple[LayerProbeArtifact, ...] | None = None
|
|
selected_sites: tuple[str, ...] | None = None
|
|
layer_summaries: tuple[LayerProbeSummary, ...] | None = None
|
|
coordinator_decision_valid = True
|
|
if runtime.rank == 0:
|
|
try:
|
|
matrices = {site: torch.cat(coordinator_activations[site], dim=0) for site in sites}
|
|
coordinator_artifacts, selected_sites = compute_layer_artifacts(
|
|
matrices,
|
|
harmful_count=plan.harmful_count,
|
|
harmless_count=plan.harmless_count,
|
|
config=config,
|
|
activation_digests=activation_digests,
|
|
activation_errors={
|
|
site: (activation_errors[site][0], activation_errors[site][1])
|
|
for site in sites
|
|
},
|
|
)
|
|
layer_summaries = tuple(item.summary_record() for item in coordinator_artifacts)
|
|
except BaseException:
|
|
coordinator_decision_valid = False
|
|
_collective_accept(
|
|
runtime,
|
|
61_902,
|
|
coordinator_decision_valid,
|
|
code="LMS_DISTILL_INVALID",
|
|
message="coordinator DISTILL computation failed the closed numeric contract",
|
|
)
|
|
with _local_step(runtime, 6_300_112, code="LMS_DISTILL_INVALID"):
|
|
header_source = None
|
|
if runtime.rank == 0:
|
|
header_source = {
|
|
"selected_sites": selected_sites,
|
|
"layer_count": len(layer_summaries or ()),
|
|
"layer_summary_set_digest": layer_summary_set_digest(layer_summaries or ()),
|
|
}
|
|
decision_header = _broadcast_record(runtime, header_source)
|
|
with _local_step(runtime, 6_300_113, code="LMS_DISTILL_INVALID"):
|
|
if set(decision_header) != {
|
|
"selected_sites",
|
|
"layer_count",
|
|
"layer_summary_set_digest",
|
|
}:
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL decision header fields are invalid"
|
|
)
|
|
received_selected = decision_header["selected_sites"]
|
|
if not isinstance(received_selected, list) or any(
|
|
not isinstance(site, str) or site not in sites for site in received_selected
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL selected layer record is invalid"
|
|
)
|
|
selected_tuple = tuple(received_selected)
|
|
if len(selected_tuple) != config.selected_layer_count or len(set(selected_tuple)) != len(
|
|
selected_tuple
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL selected layer count is invalid"
|
|
)
|
|
if decision_header["layer_count"] != len(sites):
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL layer record count is incomplete"
|
|
)
|
|
received_layers: list[LayerProbeArtifact] = []
|
|
received_summaries: list[LayerProbeSummary] = []
|
|
for index, site in enumerate(sites):
|
|
with _local_step(runtime, 6_300_114, code="LMS_DISTILL_INVALID"):
|
|
source_summary = None
|
|
harmful_source = harmless_source = directions_source = None
|
|
if runtime.rank == 0:
|
|
assert coordinator_artifacts is not None and layer_summaries is not None
|
|
source_summary = layer_summaries[index]
|
|
source_artifact = coordinator_artifacts[index]
|
|
harmful_source = source_artifact.harmful_mean
|
|
harmless_source = source_artifact.harmless_mean
|
|
directions_source = source_artifact.directions
|
|
raw_summary = _broadcast_record(runtime, source_summary)
|
|
with _local_step(runtime, 6_300_120, code="LMS_DISTILL_INVALID"):
|
|
try:
|
|
if set(raw_summary) != set(LayerProbeSummary.__dataclass_fields__):
|
|
raise ContractError("DISTILL layer summary fields changed")
|
|
layer_summary = LayerProbeSummary(
|
|
site=cast(str, raw_summary["site"]),
|
|
layer_index=cast(int, raw_summary["layer_index"]),
|
|
activation_digest=cast(str, raw_summary["activation_digest"]),
|
|
max_abs_error_hex=cast(str, raw_summary["max_abs_error_hex"]),
|
|
max_rel_error_hex=cast(str, raw_summary["max_rel_error_hex"]),
|
|
score_hex=cast(str, raw_summary["score_hex"]),
|
|
hidden_size=cast(int, raw_summary["hidden_size"]),
|
|
direction_count=cast(int, raw_summary["direction_count"]),
|
|
harmful_mean_digest=cast(str, raw_summary["harmful_mean_digest"]),
|
|
harmless_mean_digest=cast(str, raw_summary["harmless_mean_digest"]),
|
|
direction_digest=cast(str, raw_summary["direction_digest"]),
|
|
)
|
|
except (ContractError, TypeError):
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL layer summary is invalid"
|
|
) from None
|
|
if (
|
|
layer_summary.site != site
|
|
or layer_summary.hidden_size != runtime.hidden_size
|
|
or layer_summary.direction_count != config.n_directions
|
|
):
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_INVALID", "DISTILL layer order or direction count changed"
|
|
)
|
|
harmful_mean = _broadcast_tensor(
|
|
runtime,
|
|
harmful_source,
|
|
shape=(runtime.hidden_size,),
|
|
dtype=torch.float32,
|
|
)
|
|
with _local_step(runtime, 6_300_121, code="LMS_DISTILL_DIVERGED"):
|
|
harmful_mean = harmful_mean.cpu()
|
|
harmless_mean = _broadcast_tensor(
|
|
runtime,
|
|
harmless_source,
|
|
shape=(runtime.hidden_size,),
|
|
dtype=torch.float32,
|
|
)
|
|
with _local_step(runtime, 6_300_122, code="LMS_DISTILL_DIVERGED"):
|
|
harmless_mean = harmless_mean.cpu()
|
|
directions = _broadcast_tensor(
|
|
runtime,
|
|
directions_source,
|
|
shape=(config.n_directions, runtime.hidden_size),
|
|
dtype=torch.float32,
|
|
)
|
|
with _local_step(runtime, 6_300_123, code="LMS_DISTILL_DIVERGED"):
|
|
directions = directions.cpu()
|
|
with _local_step(runtime, 6_300_124, code="LMS_DISTILL_DIVERGED"):
|
|
valid = (
|
|
tensor_digest(harmful_mean) == layer_summary.harmful_mean_digest
|
|
and tensor_digest(harmless_mean) == layer_summary.harmless_mean_digest
|
|
and tensor_digest(directions) == layer_summary.direction_digest
|
|
and torch.isfinite(harmful_mean).all()
|
|
and torch.isfinite(harmless_mean).all()
|
|
and torch.isfinite(directions).all()
|
|
and torch.allclose(
|
|
directions.norm(dim=1),
|
|
torch.ones(config.n_directions),
|
|
atol=1e-5,
|
|
rtol=1e-5,
|
|
)
|
|
)
|
|
valid = bool(valid)
|
|
_collective_accept(
|
|
runtime,
|
|
62_000 + index,
|
|
valid,
|
|
code="LMS_DISTILL_DIVERGED",
|
|
message="DISTILL tensor digest or numeric invariant disagrees",
|
|
)
|
|
with _local_step(runtime, 6_300_125, code="LMS_DISTILL_INVALID"):
|
|
received_summaries.append(layer_summary)
|
|
received_layers.append(
|
|
LayerProbeArtifact(
|
|
site=layer_summary.site,
|
|
layer_index=layer_summary.layer_index,
|
|
activation_digest=layer_summary.activation_digest,
|
|
max_abs_error=float.fromhex(layer_summary.max_abs_error_hex),
|
|
max_rel_error=float.fromhex(layer_summary.max_rel_error_hex),
|
|
score=float.fromhex(layer_summary.score_hex),
|
|
harmful_mean=harmful_mean,
|
|
harmless_mean=harmless_mean,
|
|
directions=directions,
|
|
)
|
|
)
|
|
with _local_step(runtime, 6_300_126, code="LMS_DISTILL_DIVERGED"):
|
|
summary_set_digest = layer_summary_set_digest(tuple(received_summaries))
|
|
if summary_set_digest != decision_header["layer_summary_set_digest"]:
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_DIVERGED", "DISTILL summary-set digest disagrees"
|
|
)
|
|
result_summary = ProbeDistillSummary(
|
|
schema_version=1,
|
|
run_id=runtime.run_id,
|
|
loaded_identity_digest=runtime.loaded_identity_digest,
|
|
config_digest=config_digest,
|
|
corpus_digest=plan.corpus_digest,
|
|
sample_ids_digest=plan.sample_ids_digest,
|
|
tokenizer_contract_digest=plan.tokenizer_contract_digest,
|
|
tokenization_digest=tokenization_digest,
|
|
harmful_count=plan.harmful_count,
|
|
harmless_count=plan.harmless_count,
|
|
input_width=plan.input_width,
|
|
activation_atol_hex=float(config.activation_atol).hex(),
|
|
activation_rtol_hex=float(config.activation_rtol).hex(),
|
|
selected_sites=selected_tuple,
|
|
layers=tuple(received_summaries),
|
|
)
|
|
result_digest = result_summary.digest
|
|
try:
|
|
require_consensus_digest(result_digest, group=runtime.control_group)
|
|
except BaseException:
|
|
raise RuntimeContractError(
|
|
"LMS_DISTILL_DIVERGED", "rank DISTILL summaries disagree"
|
|
) from None
|
|
distilled_messages = _emit_stage(
|
|
runtime,
|
|
current=RuntimeStage.PROBED,
|
|
requested=RuntimeStage.DISTILLED,
|
|
sequence=4,
|
|
identity_digest=result_digest,
|
|
)
|
|
with _local_step(runtime, 6_300_128, code="LMS_DISTILL_INVALID"):
|
|
result = ProbeDistillResult(
|
|
summary=result_summary,
|
|
layers=tuple(received_layers),
|
|
probed_messages=probed_messages,
|
|
distilled_messages=distilled_messages,
|
|
)
|
|
return result
|
|
except BaseException as error:
|
|
raise _translate_probe_error(error) from None
|
|
|
|
|
|
def run_live_probe_distill(
|
|
session: LiveModelSession,
|
|
config: ProbeDistillConfig,
|
|
*,
|
|
harmful: tuple[ProbeSample, ...] | None = None,
|
|
harmless: tuple[ProbeSample, ...] | None = None,
|
|
tokenizer: object | None = None,
|
|
cancellation_requested: Callable[[], bool] = lambda: False,
|
|
) -> ProbeDistillResult:
|
|
"""Prepare rank-zero inputs and run PROBE/DISTILL inside a live session."""
|
|
|
|
with _local_step(cast(ProbeRuntime, session), 6_300_200, code="LMS_PROBE_PROFILE_MISMATCH"):
|
|
runtime = runtime_from_live_session(session)
|
|
corpus = None
|
|
local_input_valid = True
|
|
if runtime.rank == 0:
|
|
if harmful is None or harmless is None:
|
|
local_input_valid = False
|
|
else:
|
|
try:
|
|
exact_tokenizer = (
|
|
tokenizer if tokenizer is not None else load_exact_probe_tokenizer(session)
|
|
)
|
|
corpus = prepare_probe_corpus(
|
|
exact_tokenizer,
|
|
harmful,
|
|
harmless,
|
|
source_tokenizer_digest=session.preflight.source_identity.tokenizer_digest,
|
|
config=config,
|
|
)
|
|
except BaseException:
|
|
local_input_valid = False
|
|
elif harmful is not None or harmless is not None or tokenizer is not None:
|
|
local_input_valid = False
|
|
_collective_accept(
|
|
runtime,
|
|
60_900,
|
|
local_input_valid,
|
|
code="LMS_PROBE_INPUT_INVALID",
|
|
message="rank-owned raw probe inputs are unavailable or invalid",
|
|
)
|
|
if runtime.rank == 0 and corpus is None:
|
|
# The collective vote above always raises on every rank in this state.
|
|
raise RuntimeContractError(
|
|
"LMS_PROBE_INPUT_INVALID", "rank zero probe corpus is unavailable"
|
|
)
|
|
return run_distributed_probe_distill(
|
|
runtime,
|
|
config,
|
|
coordinator_corpus=corpus,
|
|
cancellation_requested=cancellation_requested,
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
"PROBE_ERROR_CODES",
|
|
"ProbeRuntime",
|
|
"load_exact_probe_tokenizer",
|
|
"run_distributed_probe_distill",
|
|
"run_live_probe_distill",
|
|
"runtime_from_live_session",
|
|
]
|