Files
OBLITERATUS/obliteratus/distributed/model.py
Joseph MaglyandClaude Opus 5.5 62acc92034 fix(distributed): use one canonical digest for manifest parameter values
Native TP load set ParameterManifestEntry.local_value_digest and
replicated_value_digest with model._tensor_digest (sha256 of raw bytes),
while EXCISE (_build_operation_plan) and VERIFY (_logical_state_roots with
require_manifest_values) compare probe_contracts.tensor_digest (dtype +
shape + bytes). The pristine-digest check could therefore never pass on
the native TP path: both ranks of llama-tp-local-gpu on 2x A100 failed
EXCISE with "mutable parameter storage or pristine digest changed".

Load now uses probe_contracts.tensor_digest and the raw-bytes helper is
deleted; hashing failures keep LMS_RUNTIME_PROFILE_MISMATCH. A CPU
contract test builds the manifest through the real load-validation path
and feeds it to the real EXCISE admission and VERIFY value checks
(accept unchanged, reject one-element mutation), plus BF16/FP32 digest
identity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:18:36 -04:00

810 lines
31 KiB
Python

"""Exact, opt-in Llama BF16 native tensor-parallel model boundary."""
from __future__ import annotations
import hashlib
import importlib.metadata
import json
import math
import signal
import threading
import time
from contextlib import ExitStack, contextmanager
from dataclasses import dataclass, replace
from types import MappingProxyType
from typing import Any, Iterator, Mapping
from obliteratus.distributed.config import DistributedPreflightConfig
from obliteratus.distributed.consensus import gather_failure_codes, require_consensus_digest
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
from obliteratus.distributed.launcher import TorchrunEnvironment
from obliteratus.distributed.model_manifest import (
ManifestPlacement,
ParameterManifest,
ParameterManifestEntry,
RuntimeStorageKind,
build_llama_parameter_manifest,
inspect_safetensors_metadata,
read_bounded_json,
require_safetensors_match,
)
from obliteratus.distributed.model_registry import (
LLAMA_BF16_TP2_PROFILE,
ModelAdapterProfile,
get_model_adapter,
)
from obliteratus.distributed.model_topology import classify_tp2_topology
from obliteratus.distributed.preflight import PreflightResult, SourceIdentity, revalidate_source
from obliteratus.distributed.probe_contracts import tensor_digest
_FORBIDDEN_CONFIG_FIELDS = frozenset(
{
"adapters",
"_attn_implementation",
"_attn_implementation_internal",
"audio_config",
"attn_implementation",
"attn_implementation_internal",
"auto_map",
"compression_config",
"custom_pipelines",
"device_map",
"distributed_config",
"expert_capacity",
"experts_implementation",
"fbgemm_fp8",
"layer_types",
"kernel_config",
"load_in_4bit",
"load_in_8bit",
"moe_intermediate_size",
"num_experts",
"num_local_experts",
"num_experts_per_tok",
"per_layer_config",
"pp_plan",
"quantization_config",
"sliding_window",
"text_config",
"tp_plan",
"use_flash_attention_2",
"vision_config",
}
)
@dataclass(frozen=True)
class ValidatedLlamaConfig:
"""Only semantic fields that determine the admitted parameter layout."""
raw: Mapping[str, Any]
config_digest: str
model_type: str
architecture: str
dtype: str
vocab_size: int
hidden_size: int
intermediate_size: int
num_hidden_layers: int
num_attention_heads: int
num_key_value_heads: int
head_dim: int
@dataclass(frozen=True)
class PreparedDistributedModel:
"""Pre-allocation admission result bound to preflight and source identities."""
profile: ModelAdapterProfile
config: ValidatedLlamaConfig
manifest: ParameterManifest
source_identity: SourceIdentity
@property
def manifest_digest(self) -> str:
return self.manifest.digest
@property
def plan_digest(self) -> str:
return self.profile.plan_digest
@dataclass(frozen=True)
class DistributedModelHandle:
"""A validated local shard view; the caller retains all group/mesh ownership."""
model: Any
profile: ModelAdapterProfile
config: ValidatedLlamaConfig
manifest: ParameterManifest
manifest_digest: str
local_manifest_digest: str
plan_digest: str
source_identity: SourceIdentity
replicated_value_digests: tuple[tuple[str, str], ...]
def _canonical_json_digest(value: object) -> str:
try:
raw = json.dumps(
value,
ensure_ascii=True,
allow_nan=False,
sort_keys=True,
separators=(",", ":"),
).encode("ascii")
except (TypeError, ValueError, RecursionError) as exc:
raise ContractError("model configuration is not canonical JSON") from exc
if not 1 <= len(raw) <= 8 << 20:
raise ContractError("model configuration exceeds its bounded digest contract")
return hashlib.sha256(raw).hexdigest()
def model_config_digest(config: Mapping[str, Any]) -> str:
"""Return the canonical semantic digest used by schema-v2 profiles."""
return _canonical_json_digest(config)
def _freeze_json(value: Any) -> Any:
if isinstance(value, dict):
return MappingProxyType({key: _freeze_json(item) for key, item in value.items()})
if isinstance(value, list):
return tuple(_freeze_json(item) for item in value)
return value
def _thaw_json(value: Any) -> Any:
if isinstance(value, Mapping):
return {key: _thaw_json(item) for key, item in value.items()}
if isinstance(value, tuple):
return [_thaw_json(item) for item in value]
return value
def _positive_integer(config: Mapping[str, Any], name: str, *, maximum: int) -> int:
value = config.get(name)
if isinstance(value, bool) or not isinstance(value, int) or not 1 <= value <= maximum:
raise ContractError(f"model config {name} must be a bounded positive integer")
return value
def _mapping_from_config(config: object) -> dict[str, Any]:
if isinstance(config, ValidatedLlamaConfig):
return dict(config.raw)
if isinstance(config, Mapping):
return dict(config)
to_dict = getattr(config, "to_dict", None)
if not callable(to_dict):
raise ContractError("model config must be a mapping or Hugging Face configuration")
value = to_dict()
if not isinstance(value, dict):
raise ContractError("model config serialization must be an object")
# Some Transformers identifiers are class variables and ``to_dict`` may
# normalize an instance override back to the class default. Admission must
# inspect the actual object presented by the caller.
for name in (
"model_type",
"architectures",
"dtype",
"torch_dtype",
"tie_word_embeddings",
"attention_bias",
"mlp_bias",
"pretraining_tp",
"hidden_act",
"attention_dropout",
"vocab_size",
"hidden_size",
"intermediate_size",
"num_hidden_layers",
"num_attention_heads",
"num_key_value_heads",
"head_dim",
):
if hasattr(config, name):
value[name] = getattr(config, name)
return value
def validate_model_config(
config: object,
profile: ModelAdapterProfile = LLAMA_BF16_TP2_PROFILE,
) -> ValidatedLlamaConfig:
"""Admit only the dense, bias-free, untied BF16 Llama layout in the registry."""
if profile is not LLAMA_BF16_TP2_PROFILE:
raise ContractError("model adapter profile is not the registered exact profile")
if isinstance(config, ValidatedLlamaConfig):
return config
raw = _mapping_from_config(config)
if any(
name in raw and raw[name] is not None and raw[name] is not False and raw[name] != 0
for name in _FORBIDDEN_CONFIG_FIELDS
):
raise ContractError("model config enables an excluded architecture or runtime feature")
if raw.get("model_type") != profile.model_type:
raise ContractError("model config type does not match the exact adapter")
if raw.get("architectures") != [profile.model_class]:
raise ContractError("model config architecture does not match the exact adapter")
dtype_values = [raw[name] for name in ("dtype", "torch_dtype") if raw.get(name) is not None]
if not dtype_values or any(value != profile.dtype for value in dtype_values):
raise ContractError("model config dtype must be bfloat16")
closed_values = {
"tie_word_embeddings": False,
"attention_bias": False,
"mlp_bias": False,
"pretraining_tp": 1,
"hidden_act": "silu",
}
if any(raw.get(name, expected) != expected for name, expected in closed_values.items()):
raise ContractError("model config is outside the exact dense Llama envelope")
dropout = raw.get("attention_dropout", 0.0)
if isinstance(dropout, bool) or not isinstance(dropout, (int, float)) or dropout != 0:
raise ContractError("model config attention dropout must be zero")
for name in ("rope_scaling", "attention_sink_size", "attention_sink_window_size"):
value = raw.get(name)
if value is not None and value is not False and value != 0:
raise ContractError("model config uses an unqualified attention layout")
vocab = _positive_integer(raw, "vocab_size", maximum=2**31 - 1)
hidden = _positive_integer(raw, "hidden_size", maximum=2**31 - 1)
intermediate = _positive_integer(raw, "intermediate_size", maximum=2**31 - 1)
layers = _positive_integer(raw, "num_hidden_layers", maximum=100_000)
heads = _positive_integer(raw, "num_attention_heads", maximum=2**20)
kv_heads = _positive_integer(raw, "num_key_value_heads", maximum=heads)
head_dim = raw.get("head_dim", hidden // heads if hidden % heads == 0 else None)
if isinstance(head_dim, bool) or not isinstance(head_dim, int) or head_dim <= 0:
raise ContractError("model config head_dim must be a positive integer")
if hidden != heads * head_dim or heads % kv_heads:
raise ContractError("model config attention dimensions are inconsistent")
if any(
value % profile.tp_size
for value in (vocab, hidden, intermediate, heads * head_dim, kv_heads * head_dim)
):
raise ContractError("model config dimensions are not evenly divisible by the TP size")
eps = raw.get("rms_norm_eps", 1e-6)
rope = raw.get("rope_parameters", {"rope_type": "default", "rope_theta": 10000.0})
if (
isinstance(eps, bool)
or not isinstance(eps, (int, float))
or not math.isfinite(eps)
or eps <= 0
or not isinstance(rope, dict)
or set(rope) != {"rope_type", "rope_theta"}
or rope.get("rope_type") != "default"
or isinstance(rope.get("rope_theta"), bool)
or not isinstance(rope.get("rope_theta"), (int, float))
or not math.isfinite(rope["rope_theta"])
or rope["rope_theta"] <= 0
):
raise ContractError("model config normalization or rotary settings are invalid")
return ValidatedLlamaConfig(
raw=_freeze_json(raw),
config_digest=model_config_digest(raw),
model_type=profile.model_type,
architecture=profile.model_class,
dtype=profile.dtype,
vocab_size=vocab,
hidden_size=hidden,
intermediate_size=intermediate,
num_hidden_layers=layers,
num_attention_heads=heads,
num_key_value_heads=kv_heads,
head_dim=head_dim,
)
def build_parameter_manifest(
model_or_config: object,
profile: ModelAdapterProfile = LLAMA_BF16_TP2_PROFILE,
*,
rank: int,
world_size: int,
) -> ParameterManifest:
"""Build the expected manifest from an admitted config or loaded model."""
candidate = getattr(model_or_config, "config", model_or_config)
validated = validate_model_config(candidate, profile)
return build_llama_parameter_manifest(
validated,
profile,
rank=rank,
world_size=world_size,
)
def _base_version(value: str) -> str:
return value.split("+", 1)[0]
def _require_runtime_versions(
config: DistributedPreflightConfig, profile: ModelAdapterProfile
) -> None:
configured = dict(config.software_versions)
requirements = (
("torch", profile.torch_versions),
("transformers", profile.transformers_versions),
("accelerate", profile.accelerate_versions),
("safetensors", profile.safetensors_versions),
)
for distribution, allowed in requirements:
try:
installed = importlib.metadata.version(distribution)
except importlib.metadata.PackageNotFoundError as exc:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "required model runtime is unavailable"
) from exc
configured_value = configured.get(distribution)
if (
configured_value is None
or _base_version(configured_value) not in allowed
or _base_version(installed) not in allowed
or _base_version(configured_value) != _base_version(installed)
):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "model runtime version is outside the registry"
)
def _require_capability_binding(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
profile: ModelAdapterProfile,
) -> None:
model = config.require_model()
if get_model_adapter(model.adapter_id) is not profile:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "model adapter selection changed"
)
if config.evidence_tier != "candidate_preflight":
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "model adapter requires candidate GPU preflight"
)
classify_tp2_topology(config, launch, preflight)
if (
profile.plan_digest != config.placement_plan_digest
or preflight.topology.placement_plan_digest != profile.plan_digest
or preflight.topology.world_size != profile.tp_size
or preflight.identity.config_digest != config.digest
or preflight.identity.source_digest != config.source_digest
or preflight.identity.model_digest != config.model_digest
or preflight.identity.tokenizer_digest != config.tokenizer_digest
or preflight.source_identity.source_digest != config.source_digest
or preflight.source_identity.model_digest != config.model_digest
or preflight.source_identity.tokenizer_digest != config.tokenizer_digest
):
raise RuntimeContractError(
"LMS_IDENTITY_MISMATCH", "model load capability does not match preflight"
)
@contextmanager
def _local_step(sequence: int, control_group: Any, *, code: str) -> Iterator[None]:
"""Finish local work on every rank before entering a model collective."""
error: BaseException | None = None
try:
yield
except BaseException as exc:
error = exc
local_code = None
if error is not None:
local_code = error.code if isinstance(error, RuntimeContractError) else code
try:
codes = gather_failure_codes(sequence, local_code, group=control_group)
except BaseException:
raise RuntimeContractError(code, "model preparation vote failed") from None
if codes:
if isinstance(error, RuntimeContractError) and error.code == codes[0]:
raise error
raise RuntimeContractError(codes[0], "model preparation failed") from None
def _manifest_consensus(manifest: ParameterManifest, control_group: Any) -> str:
with _local_step(30, control_group, code="LMS_CONSENSUS_RECORD_INVALID"):
digest = manifest.digest
try:
return require_consensus_digest(digest, group=control_group)
except ContractError as exc:
raise RuntimeContractError(
"LMS_CONSENSUS_RECORD_INVALID", "parameter manifest consensus failed"
) from exc
def prepare_distributed_model(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
*,
control_group: Any = None,
profile: ModelAdapterProfile = LLAMA_BF16_TP2_PROFILE,
) -> PreparedDistributedModel:
"""Validate identity, versions, config, and safetensors metadata before allocation."""
with _local_step(31, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
prepared = _prepare_local_model(config, launch, preflight, profile)
_manifest_consensus(prepared.manifest, control_group)
return prepared
def _prepare_local_model(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
profile: ModelAdapterProfile,
) -> PreparedDistributedModel:
try:
config.validate()
_require_capability_binding(config, launch, preflight, profile)
_require_runtime_versions(config, profile)
revalidate_source(config, preflight.source_identity)
raw_config = read_bounded_json(config.source_path / "config.json")
validated = validate_model_config(raw_config, profile)
model_selection = config.require_model()
if validated.config_digest != model_selection.hf_config_digest:
raise RuntimeContractError(
"LMS_IDENTITY_MISMATCH", "Hugging Face config digest does not match the profile"
)
required = {
validated.vocab_size,
validated.hidden_size,
validated.intermediate_size,
validated.num_attention_heads * validated.head_dim,
validated.num_key_value_heads * validated.head_dim,
}
if tuple(sorted(config.dimension_divisors)) != tuple(sorted(required)):
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "dimension divisors do not exactly match the model"
)
manifest = build_llama_parameter_manifest(
validated, profile, rank=launch.rank, world_size=launch.world_size
)
metadata = inspect_safetensors_metadata(
config.source_path, max_file_bytes=config.max_source_file_bytes
)
require_safetensors_match(manifest, metadata)
_ = manifest.local_digest
revalidate_source(config, preflight.source_identity)
return PreparedDistributedModel(
profile=profile,
config=validated,
manifest=manifest,
source_identity=preflight.source_identity,
)
except RuntimeContractError:
raise
except ContractError as exc:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "distributed model admission failed"
) from exc
@contextmanager
def _load_deadline(seconds: int) -> Iterator[None]:
if threading.current_thread() is not threading.main_thread() or not hasattr(signal, "SIGALRM"):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "bounded model loading requires the main POSIX thread"
)
def expired(_signum: int, _frame: object) -> None:
raise TimeoutError("model load deadline expired")
previous_handler = signal.getsignal(signal.SIGALRM)
previous_timer = signal.getitimer(signal.ITIMER_REAL)
started = time.monotonic()
signal.signal(signal.SIGALRM, expired)
signal.setitimer(signal.ITIMER_REAL, seconds)
try:
yield
finally:
signal.setitimer(signal.ITIMER_REAL, 0)
signal.signal(signal.SIGALRM, previous_handler)
if previous_timer[0] > 0:
remaining = previous_timer[0] - (time.monotonic() - started)
if remaining > 0:
signal.setitimer(signal.ITIMER_REAL, remaining, previous_timer[1])
def _require_device_mesh(
device_mesh: Any, launch: TorchrunEnvironment, profile: ModelAdapterProfile
) -> None:
try:
size = device_mesh.size()
ndim = device_mesh.ndim
device_type = device_mesh.device_type
group = device_mesh.get_group()
import torch.distributed as dist
backend = str(dist.get_backend(group)).lower()
except (AttributeError, RuntimeError, TypeError) as exc:
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "caller device mesh is invalid"
) from exc
if size != profile.tp_size or ndim != 1 or device_type != "cuda" or backend != "nccl":
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "caller device mesh is outside the exact TP data plane"
)
try:
mesh_rank = device_mesh.get_local_rank()
except (AttributeError, RuntimeError) as exc:
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "caller device mesh rank is unavailable"
) from exc
if mesh_rank != launch.rank:
raise RuntimeContractError(
"LMS_TOPOLOGY_UNSUPPORTED", "caller device mesh rank disagrees with torchrun"
)
def _validate_loading_info(loading_info: object) -> None:
if not isinstance(loading_info, dict):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "model loader did not return bounded load information"
)
allowed = {
"missing_keys",
"unexpected_keys",
"mismatched_keys",
"error_msgs",
"conversion_errors",
}
if set(loading_info) - allowed or any(loading_info.get(name) for name in allowed):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "model loader reported an incomplete or converted state"
)
def _validate_loaded_model(
model: Any,
manifest: ParameterManifest,
profile: ModelAdapterProfile,
launch: TorchrunEnvironment,
device_mesh: Any,
control_group: Any,
) -> tuple[ParameterManifest, tuple[tuple[str, str], ...]]:
with _local_step(32, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
final, replicas = _validate_local_model(model, manifest, profile, launch, device_mesh)
for _name, digest in replicas:
try:
require_consensus_digest(digest, group=control_group)
except ContractError as exc:
raise RuntimeContractError(
"LMS_CONSENSUS_RECORD_INVALID", "replicated parameter values disagree"
) from exc
_manifest_consensus(final, control_group)
return final, replicas
def _validate_local_model(
model: Any,
manifest: ParameterManifest,
profile: ModelAdapterProfile,
launch: TorchrunEnvironment,
device_mesh: Any,
) -> tuple[ParameterManifest, tuple[tuple[str, str], ...]]:
try:
import torch
from torch.distributed.tensor import DTensor
parameters = dict(model.named_parameters(remove_duplicate=False))
except (ImportError, AttributeError, TypeError) as exc:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded model does not expose the exact parameter API"
) from exc
if set(parameters) != {entry.name for entry in manifest.entries}:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded parameter names do not match the manifest"
)
if (
getattr(model, "tp_plan", None) != profile.plan_dict
or getattr(model, "_tp_size", None) != profile.tp_size
or getattr(model, "_device_mesh", None) is not device_mesh
):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded native TP identity does not match the registry"
)
final_entries: list[ParameterManifestEntry] = []
replicas: list[tuple[str, str]] = []
for entry in manifest.entries:
parameter = parameters[entry.name]
if (
not isinstance(parameter, torch.nn.Parameter)
or isinstance(parameter, DTensor)
or tuple(parameter.shape) != entry.local_shape
or parameter.dtype is not torch.bfloat16
or parameter.device.type != "cuda"
or parameter.device.index != launch.local_rank
):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded parameter storage violates the manifest"
)
try:
digest = tensor_digest(parameter)
except ContractError as exc:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "replicated parameter could not be hashed"
) from exc
if entry.placement is ManifestPlacement.SHARD:
module_name = entry.name.rsplit(".", 1)[0]
module = model.get_submodule(module_name)
generic = ".".join("*" if item.isdecimal() else item for item in module_name.split("."))
expected_plan = profile.plan_dict.get(generic)
if expected_plan is None or getattr(module, "_hf_tp_plan", None) != expected_plan:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded TP hooks do not match the registry"
)
final_entries.append(replace(entry, local_value_digest=digest))
continue
replicas.append((entry.name, digest))
final_entries.append(
replace(
entry,
local_value_digest=digest,
replicated_value_digest=digest,
)
)
final = replace(manifest, entries=tuple(final_entries))
if any(
entry.runtime_storage_kind is not RuntimeStorageKind.LOCAL_TENSOR_PARAMETER
for entry in final.entries
):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "loaded parameter storage kind is not admitted"
)
return final, tuple(replicas)
def load_distributed_model(
config: DistributedPreflightConfig,
launch: TorchrunEnvironment,
preflight: PreflightResult,
*,
device_mesh: Any,
control_group: Any = None,
profile: ModelAdapterProfile = LLAMA_BF16_TP2_PROFILE,
prepared: PreparedDistributedModel | None = None,
) -> DistributedModelHandle:
"""Load one local native-TP shard without taking ownership of groups or meshes."""
admitted = prepared or prepare_distributed_model(
config, launch, preflight, control_group=control_group, profile=profile
)
with _local_step(33, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
if admitted.profile is not profile or admitted.source_identity != preflight.source_identity:
raise RuntimeContractError(
"LMS_IDENTITY_MISMATCH", "prepared model capability does not match this attempt"
)
_require_device_mesh(device_mesh, launch, profile)
selection = config.require_model()
load_error: BaseException | None = None
try:
with _local_step(34, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
import torch
from transformers import GenerationConfig, LlamaConfig, LlamaForCausalLM
from transformers.distributed import DistributedConfig
if (
LlamaForCausalLM.__module__ != "transformers.models.llama.modeling_llama"
or LlamaForCausalLM.__name__ != profile.model_class
or LlamaConfig.__module__ != "transformers.models.llama.configuration_llama"
or LlamaConfig.__name__ != profile.config_class
):
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "upstream Llama implementation identity changed"
)
hf_config = LlamaConfig.from_dict(_thaw_json(admitted.config.raw))
effective_plan = {
**dict(getattr(LlamaForCausalLM, "_tp_plan", {}) or {}),
**{
f"model.{name}": style
for name, style in dict(hf_config.base_model_tp_plan or {}).items()
},
}
if effective_plan != profile.plan_dict:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "upstream Llama TP plan changed"
)
distributed_config = DistributedConfig(
tp_size=profile.tp_size,
tp_plan=profile.plan_dict,
enable_sequence_parallel=False,
enable_expert_parallel=False,
fsdp_size=1,
fsdp_cpu_offload=False,
fsdp_mixed_precision=False,
)
exact_distributed = {
"tp_size": profile.tp_size,
"tp_plan": profile.plan_dict,
"enable_sequence_parallel": False,
"enable_expert_parallel": False,
"fsdp_size": 1,
"fsdp_cpu_offload": False,
"fsdp_mixed_precision": False,
}
if distributed_config.to_dict() != exact_distributed:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "upstream distributed configuration changed"
)
generation_config = GenerationConfig.from_model_config(hf_config)
deadline = ExitStack()
with deadline:
with _local_step(35, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
deadline.enter_context(_load_deadline(selection.load_timeout_seconds))
source_path = str(config.source_path)
revision = selection.artifact_revision
loaded = LlamaForCausalLM.from_pretrained(
source_path,
config=hf_config,
dtype=torch.bfloat16,
distributed_config=distributed_config,
device_mesh=device_mesh,
local_files_only=True,
trust_remote_code=False,
use_safetensors=True,
weights_only=True,
output_loading_info=True,
generation_config=generation_config,
use_kernels=False,
allow_all_kernels=False,
fusion_config=None,
disable_mmap=False,
revision=revision,
token=None,
force_download=False,
)
except RuntimeContractError as exc:
load_error = exc
except TimeoutError as exc:
load_error = RuntimeContractError(
"LMS_STAGE_TIMEOUT", "model loading exceeded its timeout"
)
load_error.__cause__ = exc
except Exception as exc:
load_error = RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "native TP model loading failed"
)
load_error.__cause__ = exc
with _local_step(36, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
if load_error is not None:
revalidate_source(config, preflight.source_identity)
raise load_error
if not isinstance(loaded, tuple) or len(loaded) != 2:
raise RuntimeContractError(
"LMS_RUNTIME_PROFILE_MISMATCH", "model loader returned an unexpected result"
)
model, loading_info = loaded
revalidate_source(config, preflight.source_identity)
_validate_loading_info(loading_info)
manifest, replicas = _validate_loaded_model(
model,
admitted.manifest,
profile,
launch,
device_mesh,
control_group,
)
with _local_step(37, control_group, code="LMS_RUNTIME_PROFILE_MISMATCH"):
handle = DistributedModelHandle(
model=model,
profile=profile,
config=admitted.config,
manifest=manifest,
manifest_digest=manifest.digest,
local_manifest_digest=manifest.local_digest,
plan_digest=profile.plan_digest,
source_identity=preflight.source_identity,
replicated_value_digests=replicas,
)
return handle
__all__ = [
"DistributedModelHandle",
"LLAMA_BF16_TP2_PROFILE",
"PreparedDistributedModel",
"ValidatedLlamaConfig",
"build_parameter_manifest",
"load_distributed_model",
"model_config_digest",
"prepare_distributed_model",
"validate_model_config",
]