"""Single-process admission and post-load validation contracts for the native-TP boundary. The real two-GPU NCCL load is qualified by the llama-tp-local-gpu gate. These tests exercise every CPU-decidable refusal and binding around it: config admission, runtime and capability binding, safetensors metadata admission against a tiny immutable BF16 checkpoint, the bounded load deadline, caller mesh validation, loader result handling, and the post-load manifest/hook/replica validation logic. """ from __future__ import annotations import hashlib import importlib.metadata import json import math import signal import threading from dataclasses import replace from pathlib import Path from types import MappingProxyType, SimpleNamespace import pytest import torch from safetensors.torch import save_file from transformers import LlamaForCausalLM import obliteratus.distributed.model as model_module from obliteratus.distributed.contracts import ContractError, RuntimeContractError from obliteratus.distributed.model import ( LLAMA_BF16_TP2_PROFILE, PreparedDistributedModel, build_parameter_manifest, load_distributed_model, model_config_digest, prepare_distributed_model, validate_model_config, ) from obliteratus.distributed.model_manifest import ManifestPlacement, RuntimeStorageKind from obliteratus.distributed.preflight import inspect_source from obliteratus.distributed.probe_contracts import tensor_digest from tests.test_distributed_model_contracts import _llama_config pytestmark = pytest.mark.cpu PROFILE = LLAMA_BF16_TP2_PROFILE DIVISORS = (8, 16, 32) @pytest.fixture(autouse=True) def _local_votes(monkeypatch): monkeypatch.setattr( model_module, "gather_failure_codes", lambda _sequence, code, **_kwargs: (code,) if code else (), ) @pytest.mark.parametrize("local_failure", [False, True]) def test_model_preparation_preserves_asymmetric_refusal(monkeypatch, local_failure): group = object() code = "LMS_RESOURCE_ADMISSION_DENIED" def vote(sequence, local_code, **kwargs): assert sequence == 31 and kwargs == {"group": group} assert local_code == (code if local_failure else None) return (code,) monkeypatch.setattr(model_module, "gather_failure_codes", vote) with pytest.raises(RuntimeContractError) as error: with model_module._local_step(31, group, code="LMS_RUNTIME_PROFILE_MISMATCH"): if local_failure: raise RuntimeContractError(code, "resource refused") assert error.value.code == code def _config_dict(**overrides: object) -> dict[str, object]: raw = json.loads(json.dumps(_llama_config().to_dict())) raw.update(overrides) return raw def _write_checkpoint(root: Path, *, config: dict[str, object] | None = None) -> Path: source = root / "source" source.mkdir() raw = _config_dict() if config is None else config (source / "config.json").write_text(json.dumps(raw), encoding="utf-8") (source / "tokenizer.json").write_text("{}", encoding="utf-8") manifest = build_parameter_manifest(_llama_config(), PROFILE, rank=0, world_size=2) tensors = { entry.name: torch.full(entry.global_shape, index + 1, dtype=torch.bfloat16) for index, entry in enumerate(manifest.entries) } save_file(tensors, str(source / "model.safetensors"), metadata={"format": "pt"}) for item in source.iterdir(): item.chmod(0o444) source.chmod(0o555) return source @pytest.fixture def checkpoint(tmp_path): source = _write_checkpoint(tmp_path) yield source source.chmod(0o755) def _installed_versions() -> tuple[tuple[str, str], ...]: return tuple( (name, importlib.metadata.version(name)) for name in ("torch", "transformers", "accelerate", "safetensors") ) def _admission(source: Path, **overrides: object): raw = json.loads((source / "config.json").read_text(encoding="utf-8")) selection = SimpleNamespace( hf_config_digest=overrides.pop("hf_config_digest", model_config_digest(raw)), adapter_id=PROFILE.adapter_id, load_timeout_seconds=5, artifact_revision="a" * 40, ) values = { "validate": lambda: None, "source_path": source, "require_model": lambda: selection, "dimension_divisors": DIVISORS, "software_versions": _installed_versions(), "max_source_files": 100, "max_source_bytes": 1 << 30, "max_source_file_bytes": 1 << 30, "source_timeout_seconds": 5, } values.update(overrides) config = SimpleNamespace(**values) preflight = SimpleNamespace(source_identity=inspect_source(source)) launch = SimpleNamespace(rank=0, local_rank=0, world_size=2) return config, launch, preflight @pytest.fixture def consensus(monkeypatch): calls: list[tuple[str, object]] = [] def agree(digest, *, group=None): calls.append((digest, group)) return digest monkeypatch.setattr(model_module, "require_consensus_digest", agree) monkeypatch.setattr( model_module, "_require_capability_binding", lambda *_args: calls.append(("bound", None)) ) return calls # --------------------------------------------------------------------------- # Configuration admission # --------------------------------------------------------------------------- def test_config_digest_is_canonical_and_refuses_non_json_values(): assert model_config_digest({"b": 1, "a": [2]}) == model_config_digest({"a": [2], "b": 1}) with pytest.raises(ContractError, match="canonical JSON"): model_config_digest({"eps": math.nan}) with pytest.raises(ContractError, match="canonical JSON"): model_config_digest({"value": object()}) def test_validated_config_is_deeply_frozen_and_thaws_to_the_admitted_mapping(): raw = _config_dict() validated = validate_model_config(raw) assert isinstance(validated.raw, MappingProxyType) assert isinstance(validated.raw["rope_parameters"], MappingProxyType) assert validated.raw["architectures"] == ("LlamaForCausalLM",) assert model_module._thaw_json(validated.raw) == raw assert validated.config_digest == model_config_digest(raw) # Revalidation of an admitted config is an identity, not a second parse. assert validate_model_config(validated) is validated assert model_module._mapping_from_config(validated) == dict(validated.raw) def test_config_admission_requires_mapping_or_object_serialization(): with pytest.raises(ContractError, match="mapping or Hugging Face"): validate_model_config(object()) with pytest.raises(ContractError, match="must be an object"): validate_model_config(SimpleNamespace(to_dict=lambda: ["not", "a", "mapping"])) def test_instance_identifiers_override_normalized_serialization(): class Normalizing: model_type = "mistral" def to_dict(self): return _config_dict() # ``to_dict`` claims llama, but the object presented to admission says mistral. with pytest.raises(ContractError, match="type does not match"): validate_model_config(Normalizing()) def test_non_registered_profile_object_is_refused_even_when_equal(): with pytest.raises(ContractError, match="registered exact profile"): validate_model_config(_config_dict(), replace(PROFILE)) @pytest.mark.parametrize( ("overrides", "message"), [ ({"attention_dropout": True}, "dropout must be zero"), ({"attention_dropout": "0"}, "dropout must be zero"), ({"attention_dropout": 0.1}, "dropout must be zero"), ({"rope_scaling": {"type": "linear"}}, "unqualified attention layout"), ({"attention_sink_size": 4}, "unqualified attention layout"), ({"head_dim": True}, "head_dim must be a positive integer"), ({"head_dim": 0}, "head_dim must be a positive integer"), ({"head_dim": 8}, "attention dimensions are inconsistent"), ({"num_key_value_heads": 3}, "attention dimensions are inconsistent"), ({"num_hidden_layers": 0}, "bounded positive integer"), ({"vocab_size": True}, "bounded positive integer"), ({"rms_norm_eps": 0}, "normalization or rotary"), ({"rms_norm_eps": math.inf}, "normalization or rotary"), ({"rms_norm_eps": True}, "normalization or rotary"), ({"rope_parameters": {"rope_type": "default"}}, "normalization or rotary"), ( {"rope_parameters": {"rope_type": "yarn", "rope_theta": 10000.0}}, "normalization or rotary", ), ( {"rope_parameters": {"rope_type": "default", "rope_theta": True}}, "normalization or rotary", ), ( {"rope_parameters": {"rope_type": "default", "rope_theta": -1.0}}, "normalization or rotary", ), ({"rope_parameters": ["default", 10000.0]}, "normalization or rotary"), ({"dtype": None, "torch_dtype": None}, "dtype must be bfloat16"), ({"torch_dtype": "float16"}, "dtype must be bfloat16"), ({"hidden_act": "gelu"}, "exact dense Llama envelope"), ], ) def test_mapping_config_refuses_each_unqualified_layout(overrides, message): with pytest.raises(ContractError, match=message): validate_model_config(_config_dict(**overrides)) def test_head_dim_is_derived_when_absent_and_refused_when_underivable(): raw = _config_dict() del raw["head_dim"] assert validate_model_config(raw).head_dim == 4 raw.update(hidden_size=18) with pytest.raises(ContractError, match="head_dim must be a positive integer"): validate_model_config(raw) def test_build_parameter_manifest_accepts_a_loaded_model_like_object(): holder = SimpleNamespace(config=_config_dict()) manifest = build_parameter_manifest(holder, PROFILE, rank=1, world_size=2) assert manifest.rank == 1 assert manifest.digest == build_parameter_manifest( _llama_config(), PROFILE, rank=0, world_size=2 ).digest # --------------------------------------------------------------------------- # Runtime and capability binding # --------------------------------------------------------------------------- def test_runtime_versions_accept_local_builds_of_registered_releases(): config = SimpleNamespace(software_versions=_installed_versions()) assert model_module._require_runtime_versions(config, PROFILE) is None def test_missing_runtime_distribution_refuses(monkeypatch): def missing(_name): raise importlib.metadata.PackageNotFoundError("accelerate") configured = SimpleNamespace(software_versions=_installed_versions()) monkeypatch.setattr(model_module.importlib.metadata, "version", missing) with pytest.raises(RuntimeContractError) as failure: model_module._require_runtime_versions(configured, PROFILE) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert str(failure.value) == "required model runtime is unavailable" def test_unconfigured_runtime_distribution_refuses(): configured = tuple(item for item in _installed_versions() if item[0] != "safetensors") with pytest.raises(RuntimeContractError, match="outside the registry"): model_module._require_runtime_versions( SimpleNamespace(software_versions=configured), PROFILE ) def _binding_fixture(): digests = {name: hashlib.sha256(name.encode()).hexdigest() for name in ("s", "m", "t", "c")} identity = SimpleNamespace( config_digest=digests["c"], source_digest=digests["s"], model_digest=digests["m"], tokenizer_digest=digests["t"], ) source = SimpleNamespace( source_digest=digests["s"], model_digest=digests["m"], tokenizer_digest=digests["t"] ) preflight = SimpleNamespace( topology=SimpleNamespace(placement_plan_digest=PROFILE.plan_digest, world_size=2), identity=identity, source_identity=source, ) config = SimpleNamespace( require_model=lambda: SimpleNamespace(adapter_id=PROFILE.adapter_id), evidence_tier="candidate_preflight", placement_plan_digest=PROFILE.plan_digest, digest=digests["c"], source_digest=digests["s"], model_digest=digests["m"], tokenizer_digest=digests["t"], ) return config, preflight def test_capability_binding_accepts_only_the_preflighted_exact_identity(monkeypatch): topology_calls: list[object] = [] monkeypatch.setattr( model_module, "classify_tp2_topology", lambda *args: topology_calls.append(args) ) config, preflight = _binding_fixture() launch = SimpleNamespace(rank=0) model_module._require_capability_binding(config, launch, preflight, PROFILE) assert topology_calls == [(config, launch, preflight)] drifted = SimpleNamespace(**{**vars(config), "tokenizer_digest": "0" * 64}) with pytest.raises(RuntimeContractError) as failure: model_module._require_capability_binding(drifted, launch, preflight, PROFILE) assert failure.value.code == "LMS_IDENTITY_MISMATCH" moved = SimpleNamespace( **{**vars(preflight), "topology": SimpleNamespace( placement_plan_digest=PROFILE.plan_digest, world_size=4 )} ) with pytest.raises(RuntimeContractError) as failure: model_module._require_capability_binding(config, launch, moved, PROFILE) assert failure.value.code == "LMS_IDENTITY_MISMATCH" def test_capability_binding_refuses_adapter_swap_and_protocol_tier(monkeypatch): monkeypatch.setattr( model_module, "classify_tp2_topology", lambda *_args: pytest.fail("refusal must precede topology classification"), ) config, preflight = _binding_fixture() with pytest.raises(RuntimeContractError) as failure: model_module._require_capability_binding(config, object(), preflight, replace(PROFILE)) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" protocol = SimpleNamespace(**{**vars(config), "evidence_tier": "protocol_cpu"}) with pytest.raises(RuntimeContractError) as failure: model_module._require_capability_binding(protocol, object(), preflight, PROFILE) assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED" def test_manifest_consensus_failure_is_a_stable_runtime_code(monkeypatch): def disagree(_digest, *, group=None): raise ContractError("rank digests disagree") monkeypatch.setattr(model_module, "require_consensus_digest", disagree) manifest = build_parameter_manifest(_llama_config(), PROFILE, rank=0, world_size=2) with pytest.raises(RuntimeContractError) as failure: model_module._manifest_consensus(manifest, object()) assert failure.value.code == "LMS_CONSENSUS_RECORD_INVALID" # --------------------------------------------------------------------------- # Pre-allocation admission against a real immutable checkpoint # --------------------------------------------------------------------------- def test_prepare_admits_exact_checkpoint_and_commits_manifest_consensus(checkpoint, consensus): config, launch, preflight = _admission(checkpoint) control = object() prepared = prepare_distributed_model(config, launch, preflight, control_group=control) expected = build_parameter_manifest(_llama_config(), PROFILE, rank=0, world_size=2) assert isinstance(prepared, PreparedDistributedModel) assert prepared.profile is PROFILE assert prepared.manifest_digest == expected.digest assert prepared.plan_digest == PROFILE.plan_digest assert prepared.source_identity == preflight.source_identity assert prepared.config.config_digest == config.require_model().hf_config_digest assert consensus == [("bound", None), (expected.digest, control)] def test_prepare_refuses_hf_config_digest_drift(checkpoint, consensus): config, launch, preflight = _admission(checkpoint, hf_config_digest="0" * 64) with pytest.raises(RuntimeContractError) as failure: prepare_distributed_model(config, launch, preflight) assert failure.value.code == "LMS_IDENTITY_MISMATCH" assert len(consensus) == 1 # Bound, but no manifest consensus was entered. def test_prepare_refuses_divisors_that_do_not_exactly_match_the_model(checkpoint, consensus): config, launch, preflight = _admission(checkpoint, dimension_divisors=(2, 4)) with pytest.raises(RuntimeContractError) as failure: prepare_distributed_model(config, launch, preflight) assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED" def test_prepare_refuses_source_replaced_after_preflight(checkpoint, consensus): config, launch, preflight = _admission(checkpoint) stale = SimpleNamespace(source_identity=replace(preflight.source_identity, file_count=99)) with pytest.raises(RuntimeContractError) as failure: prepare_distributed_model(config, launch, stale) assert failure.value.code == "LMS_SOURCE_CHANGED" assert consensus == [("bound", None)] def test_prepare_translates_config_contract_errors(tmp_path, consensus): source = _write_checkpoint(tmp_path, config=_config_dict(mlp_bias=True)) try: config, launch, preflight = _admission(source) with pytest.raises(RuntimeContractError) as failure: prepare_distributed_model(config, launch, preflight) finally: source.chmod(0o755) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert str(failure.value) == "distributed model admission failed" assert isinstance(failure.value.__cause__, ContractError) def test_prepare_refuses_checkpoint_whose_tensors_do_not_match_the_manifest( tmp_path, consensus ): # The config admits a two-layer model but the checkpoint holds one layer. source = _write_checkpoint(tmp_path, config=_config_dict(num_hidden_layers=2)) try: config, launch, preflight = _admission(source) with pytest.raises(RuntimeContractError) as failure: prepare_distributed_model(config, launch, preflight) finally: source.chmod(0o755) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert "parameter names" in str(failure.value) # --------------------------------------------------------------------------- # Bounded load deadline # --------------------------------------------------------------------------- def test_load_deadline_interrupts_a_stalled_load_and_restores_the_handler(): previous = signal.getsignal(signal.SIGALRM) with pytest.raises(TimeoutError, match="deadline expired"): with model_module._load_deadline(0.05): threading.Event().wait(2) assert signal.getsignal(signal.SIGALRM) is previous assert signal.getitimer(signal.ITIMER_REAL) == (0.0, 0.0) def test_load_deadline_rearms_an_outer_timer_with_its_remaining_budget(): fired: list[int] = [] previous = signal.signal(signal.SIGALRM, lambda signum, _frame: fired.append(signum)) try: signal.setitimer(signal.ITIMER_REAL, 30) with model_module._load_deadline(5): pass remaining, interval = signal.getitimer(signal.ITIMER_REAL) assert 0 < remaining <= 30 assert interval == 0 assert fired == [] finally: signal.setitimer(signal.ITIMER_REAL, 0) signal.signal(signal.SIGALRM, previous) def test_load_deadline_requires_the_main_thread(): failures: list[BaseException] = [] def worker(): try: with model_module._load_deadline(1): pass except BaseException as error: # captured for assertion in the main thread failures.append(error) thread = threading.Thread(target=worker) thread.start() thread.join(5) assert len(failures) == 1 assert isinstance(failures[0], RuntimeContractError) assert failures[0].code == "LMS_RUNTIME_PROFILE_MISMATCH" # --------------------------------------------------------------------------- # Caller-owned device mesh validation # --------------------------------------------------------------------------- class _Mesh: def __init__(self, *, size=2, ndim=1, device_type="cuda", rank=0, rank_error=None): self._size = size self.ndim = ndim self.device_type = device_type self._rank = rank self._rank_error = rank_error self.group = object() def size(self): return self._size def get_group(self): return self.group def get_local_rank(self): if self._rank_error is not None: raise self._rank_error return self._rank @pytest.fixture def nccl_backend(monkeypatch): import torch.distributed as dist backends: dict[object, str] = {} monkeypatch.setattr(dist, "get_backend", lambda group: backends.get(group, "nccl")) return backends def test_device_mesh_must_be_the_exact_one_dimensional_cuda_nccl_tp_plane(nccl_backend): launch = SimpleNamespace(rank=1) assert model_module._require_device_mesh(_Mesh(rank=1), launch, PROFILE) is None for mesh in (_Mesh(size=4, rank=1), _Mesh(ndim=2, rank=1), _Mesh(device_type="cpu", rank=1)): with pytest.raises(RuntimeContractError, match="outside the exact TP data plane"): model_module._require_device_mesh(mesh, launch, PROFILE) gloo = _Mesh(rank=1) nccl_backend[gloo.group] = "gloo" with pytest.raises(RuntimeContractError, match="outside the exact TP data plane"): model_module._require_device_mesh(gloo, launch, PROFILE) def test_device_mesh_without_the_mesh_api_or_rank_refuses(nccl_backend): launch = SimpleNamespace(rank=0) with pytest.raises(RuntimeContractError, match="caller device mesh is invalid"): model_module._require_device_mesh(object(), launch, PROFILE) with pytest.raises(RuntimeContractError, match="rank is unavailable"): model_module._require_device_mesh( _Mesh(rank_error=RuntimeError("no rank")), launch, PROFILE ) with pytest.raises(RuntimeContractError, match="disagrees with torchrun"): model_module._require_device_mesh(_Mesh(rank=1), launch, PROFILE) # --------------------------------------------------------------------------- # Loader result and post-load validation # --------------------------------------------------------------------------- @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32]) def test_load_tensor_digest_binds_dtype_shape_and_storage_bytes(dtype): tensor = torch.arange(6, dtype=dtype).reshape(2, 3).t() # non-contiguous CPU view expected = hashlib.sha256( str(tensor.dtype).encode("ascii") + repr(tuple(tensor.shape)).encode("ascii") + tensor.contiguous().reshape(-1).view(torch.uint8).numpy().tobytes() ).hexdigest() assert model_module.tensor_digest(tensor) == tensor_digest(tensor) == expected @pytest.mark.parametrize( "loading_info", [ None, {"missing_keys": ["lm_head.weight"]}, {"error_msgs": ["size mismatch"]}, {"conversion_errors": ["fused"]}, {"missing_keys": [], "adapter_keys": []}, ], ) def test_loading_info_must_report_a_complete_unconverted_load(loading_info): with pytest.raises(RuntimeContractError) as failure: model_module._validate_loading_info(loading_info) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" def test_empty_loading_info_is_accepted(): model_module._validate_loading_info( {"missing_keys": [], "unexpected_keys": [], "mismatched_keys": [], "error_msgs": []} ) class _CudaReportedParameter(torch.nn.Parameter): """CPU storage that reports the CUDA placement of a real native-TP shard. Only the post-load validator's device check is satisfied by this shim; it lets the hook-binding, replica-consensus and manifest-commit logic run on CPU. The real sharded placement is qualified by the llama-tp-local-gpu gate. """ reported_index = 0 @property def device(self): # type: ignore[override] return torch.device("cuda", self.reported_index) class _LoadedModel: def __init__(self, manifest, mesh, *, fill=1.0, hook_plan=None, parameter_type=None): parameter_type = parameter_type or _CudaReportedParameter self.tp_plan = PROFILE.plan_dict self._tp_size = PROFILE.tp_size self._device_mesh = mesh self._parameters = { entry.name: parameter_type( torch.full(entry.local_shape, fill, dtype=torch.bfloat16), requires_grad=False ) for entry in manifest.entries } self._hook_plan = dict(PROFILE.plan_dict) if hook_plan is None else hook_plan def named_parameters(self, *, remove_duplicate=True): assert remove_duplicate is False return iter(self._parameters.items()) def get_submodule(self, name): generic = ".".join("*" if item.isdecimal() else item for item in name.split(".")) return SimpleNamespace(_hf_tp_plan=self._hook_plan.get(generic)) @pytest.fixture def manifest(): return build_parameter_manifest(_llama_config(), PROFILE, rank=0, world_size=2) def _validate(model, manifest, mesh, *, launch=None, control=None): return model_module._validate_loaded_model( model, manifest, PROFILE, launch or SimpleNamespace(rank=0, local_rank=0), mesh, control, ) def test_post_load_validation_binds_replicas_shards_and_final_manifest(monkeypatch, manifest): agreed: list[tuple[str, object]] = [] monkeypatch.setattr( model_module, "require_consensus_digest", lambda digest, *, group=None: agreed.append((digest, group)) or digest, ) mesh, control = object(), object() model = _LoadedModel(manifest, mesh) final, replicas = _validate(model, manifest, mesh, control=control) replicated = [e for e in manifest.entries if e.placement is ManifestPlacement.REPLICATE] assert [name for name, _digest in replicas] == [entry.name for entry in replicated] for entry in final.entries: expected = tensor_digest(model._parameters[entry.name]) assert entry.local_value_digest == expected if entry.placement is ManifestPlacement.REPLICATE: assert entry.replicated_value_digest == expected else: assert entry.replicated_value_digest is None # Replica digests are agreed one by one, then the value-bound manifest root. assert [digest for digest, _group in agreed[:-1]] == [digest for _name, digest in replicas] assert agreed[-1] == (final.digest, control) assert final.digest != manifest.digest assert {entry.name for entry in final.entries} == {entry.name for entry in manifest.entries} def test_post_load_validation_wraps_canonical_digest_failure(monkeypatch, manifest): cause = ContractError("parameter cannot be represented canonically") def fail_digest(_parameter): raise cause monkeypatch.setattr(model_module, "tensor_digest", fail_digest) mesh = object() with pytest.raises(RuntimeContractError, match="replicated parameter could not be hashed") as error: _validate(_LoadedModel(manifest, mesh), manifest, mesh) assert error.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert error.value.__cause__ is cause def test_post_load_validation_refuses_disagreeing_replica_values(monkeypatch, manifest): def disagree(_digest, *, group=None): raise ContractError("rank digests disagree") monkeypatch.setattr(model_module, "require_consensus_digest", disagree) mesh = object() with pytest.raises(RuntimeContractError) as failure: _validate(_LoadedModel(manifest, mesh), manifest, mesh) assert failure.value.code == "LMS_CONSENSUS_RECORD_INVALID" def test_post_load_validation_refuses_missing_tp_hooks(monkeypatch, manifest): monkeypatch.setattr(model_module, "require_consensus_digest", lambda d, *, group=None: d) mesh = object() hooks = {**PROFILE.plan_dict, "model.layers.*.mlp.down_proj": "colwise"} with pytest.raises(RuntimeContractError, match="TP hooks do not match"): _validate(_LoadedModel(manifest, mesh, hook_plan=hooks), manifest, mesh) def test_post_load_validation_refuses_non_local_tensor_storage_kind(monkeypatch, manifest): monkeypatch.setattr(model_module, "require_consensus_digest", lambda d, *, group=None: d) mesh = object() entries = tuple( replace(entry, runtime_storage_kind=SimpleNamespace(value="dtensor")) for entry in manifest.entries ) foreign = replace(manifest, entries=entries) monkeypatch.setattr(model_module, "_manifest_consensus", lambda *_args: None) with pytest.raises(RuntimeContractError, match="storage kind is not admitted"): _validate(_LoadedModel(manifest, mesh), foreign, mesh) assert all( entry.runtime_storage_kind is RuntimeStorageKind.LOCAL_TENSOR_PARAMETER for entry in manifest.entries ) def test_post_load_validation_refuses_parameter_api_name_and_tp_identity_drift(manifest): mesh = object() with pytest.raises(RuntimeContractError, match="exact parameter API"): _validate(object(), manifest, mesh) partial = _LoadedModel(manifest, mesh) partial._parameters.pop("lm_head.weight") with pytest.raises(RuntimeContractError, match="names do not match"): _validate(partial, manifest, mesh) for attribute, value in (("tp_plan", {}), ("_tp_size", 1), ("_device_mesh", object())): drifted = _LoadedModel(manifest, mesh) setattr(drifted, attribute, value) with pytest.raises(RuntimeContractError, match="native TP identity"): _validate(drifted, manifest, mesh) @pytest.mark.parametrize("violation", ["cpu", "device_index", "dtype", "shape", "plain"]) def test_post_load_validation_refuses_parameter_storage_violations( monkeypatch, manifest, violation ): monkeypatch.setattr(model_module, "require_consensus_digest", lambda d, *, group=None: d) mesh = object() launch = SimpleNamespace(rank=0, local_rank=0) model = _LoadedModel( manifest, mesh, parameter_type=torch.nn.Parameter if violation == "cpu" else None ) name = "model.norm.weight" if violation == "device_index": launch = SimpleNamespace(rank=0, local_rank=1) elif violation == "dtype": model._parameters[name] = _CudaReportedParameter( torch.ones(16, dtype=torch.float32), requires_grad=False ) elif violation == "shape": model._parameters[name] = _CudaReportedParameter( torch.ones(8, dtype=torch.bfloat16), requires_grad=False ) elif violation == "plain": model._parameters[name] = torch.ones(16, dtype=torch.bfloat16) with pytest.raises(RuntimeContractError, match="storage violates the manifest"): _validate(model, manifest, mesh, launch=launch) # --------------------------------------------------------------------------- # load_distributed_model orchestration around the native loader call # --------------------------------------------------------------------------- @pytest.fixture def load_case(checkpoint, consensus, nccl_backend, monkeypatch): config, launch, preflight = _admission(checkpoint) prepared = prepare_distributed_model(config, launch, preflight) mesh = _Mesh(rank=0) calls: list[dict[str, object]] = [] case = SimpleNamespace( config=config, launch=launch, preflight=preflight, prepared=prepared, mesh=mesh, calls=calls, result=None, error=None, ) def from_pretrained(path, **kwargs): calls.append({"path": path, **kwargs}) if case.error is not None: raise case.error if case.result is not None: return case.result return _LoadedModel(prepared.manifest, mesh), {"missing_keys": []} monkeypatch.setattr(LlamaForCausalLM, "from_pretrained", from_pretrained) return case def _load(case, **kwargs): return load_distributed_model( case.config, case.launch, case.preflight, device_mesh=case.mesh, prepared=case.prepared, **kwargs, ) def test_native_load_is_offline_exact_and_returns_a_value_bound_handle(load_case): handle = _load(load_case, control_group="control") (call,) = load_case.calls assert call["path"] == str(load_case.config.source_path) assert call["device_mesh"] is load_case.mesh assert call["dtype"] is torch.bfloat16 assert call["distributed_config"].to_dict()["tp_plan"] == PROFILE.plan_dict for name, expected in ( ("local_files_only", True), ("trust_remote_code", False), ("use_safetensors", True), ("weights_only", True), ("use_kernels", False), ("force_download", False), ("token", None), ("revision", "a" * 40), ): assert call[name] == expected, name assert call["config"].to_dict()["num_hidden_layers"] == 1 assert handle.profile is PROFILE assert handle.config is load_case.prepared.config assert handle.manifest_digest == handle.manifest.digest assert handle.local_manifest_digest == handle.manifest.local_digest assert handle.manifest_digest != load_case.prepared.manifest_digest # value-bound now assert handle.plan_digest == PROFILE.plan_digest assert handle.source_identity == load_case.preflight.source_identity assert len(handle.replicated_value_digests) == 4 def test_load_prepares_admission_when_no_prepared_capability_is_supplied(load_case): load_case.prepared = None handle = _load(load_case) assert handle.manifest.rank == 0 assert len(load_case.calls) == 1 def test_prepared_capability_from_another_attempt_is_refused(load_case): load_case.prepared = replace( load_case.prepared, source_identity=replace(load_case.preflight.source_identity, total_bytes=1), ) with pytest.raises(RuntimeContractError) as failure: _load(load_case) assert failure.value.code == "LMS_IDENTITY_MISMATCH" assert load_case.calls == [] @pytest.mark.parametrize( ("target", "attribute", "value", "message"), [ ( "model", "__module__", "transformers.models.mistral.modeling_mistral", "implementation identity changed", ), ("model", "_tp_plan", {"lm_head": "colwise"}, "TP plan changed"), ], ) def test_upstream_llama_drift_refuses_before_loading( load_case, monkeypatch, target, attribute, value, message ): monkeypatch.setattr(LlamaForCausalLM, attribute, value) with pytest.raises(RuntimeContractError, match=message): _load(load_case) assert load_case.calls == [] def test_upstream_distributed_config_drift_refuses_before_loading(load_case, monkeypatch): from transformers.distributed import DistributedConfig original = DistributedConfig.to_dict monkeypatch.setattr( DistributedConfig, "to_dict", lambda self: {**original(self), "fsdp_size": 2} ) with pytest.raises(RuntimeContractError, match="distributed configuration changed"): _load(load_case) assert load_case.calls == [] def test_loader_timeout_is_reported_as_a_stage_timeout(load_case): load_case.error = TimeoutError("model load deadline expired") with pytest.raises(RuntimeContractError) as failure: _load(load_case) assert failure.value.code == "LMS_STAGE_TIMEOUT" def test_loader_failure_discards_raw_detail(load_case): load_case.error = OSError("/private/path/model.safetensors: secret detail") with pytest.raises(RuntimeContractError) as failure: _load(load_case) assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert str(failure.value) == "native TP model loading failed" def test_loader_failure_after_source_replacement_reports_the_source_change( load_case, monkeypatch ): def replaced(_config, _expected): raise RuntimeContractError("LMS_SOURCE_CHANGED", "source identity changed") load_case.error = RuntimeError("partial read") monkeypatch.setattr(model_module, "revalidate_source", replaced) with pytest.raises(RuntimeContractError) as failure: _load(load_case) assert failure.value.code == "LMS_SOURCE_CHANGED" @pytest.mark.parametrize("result", [None, ("model",), ("model", {}, "extra")]) def test_loader_must_return_model_and_loading_info(load_case, result): load_case.result = result if result is not None else object() with pytest.raises(RuntimeContractError, match="unexpected result"): _load(load_case) def test_incomplete_load_report_refuses_before_parameter_validation(load_case, monkeypatch): monkeypatch.setattr( model_module, "_validate_loaded_model", lambda *_args: pytest.fail("incomplete loads must not be validated as loaded"), ) load_case.result = (object(), {"unexpected_keys": ["model.extra.weight"]}) with pytest.raises(RuntimeContractError, match="incomplete or converted"): _load(load_case) def test_runtime_contract_errors_from_the_loader_are_preserved(load_case): load_case.error = RuntimeContractError("LMS_STAGE_TIMEOUT", "bounded") with pytest.raises(RuntimeContractError) as failure: _load(load_case) assert failure.value.code == "LMS_STAGE_TIMEOUT" assert str(failure.value) == "bounded"