Files
OBLITERATUS/tests/test_distributed_model_contracts.py

436 lines
15 KiB
Python

"""Closed Llama BF16 tensor-parallel adapter contracts for issue 60."""
from __future__ import annotations
import json
import re
from dataclasses import asdict, is_dataclass, replace
from types import SimpleNamespace
import pytest
import torch
import transformers
import accelerate
import safetensors
from transformers import LlamaConfig
from obliteratus.distributed.contracts import ContractError, contract_digest
from obliteratus.distributed.contracts import RuntimeContractError
import obliteratus.distributed.model as model_module
from obliteratus.distributed.model import (
LLAMA_BF16_TP2_PROFILE,
build_parameter_manifest,
validate_model_config,
)
from obliteratus.distributed.model_evidence import (
LOAD_EVIDENCE_BINDING_FIELDS,
LoadEvidenceSummary,
build_rank_load_evidence,
publish_rank_load_evidence,
read_rank_load_evidence,
validate_loaded_summary_set,
verify_rank_load_evidence,
)
EXPECTED_EFFECTIVE_PLAN = {
"lm_head": "colwise_gather_output",
"model.layers.*.mlp.down_proj": "rowwise",
"model.layers.*.mlp.gate_proj": "colwise",
"model.layers.*.mlp.up_proj": "colwise",
"model.layers.*.self_attn.k_proj": "colwise",
"model.layers.*.self_attn.o_proj": "rowwise",
"model.layers.*.self_attn.q_proj": "colwise",
"model.layers.*.self_attn.v_proj": "colwise",
}
def _llama_config(**overrides: object) -> LlamaConfig:
values: dict[str, object] = {
"vocab_size": 32,
"hidden_size": 16,
"intermediate_size": 32,
"num_hidden_layers": 1,
"num_attention_heads": 4,
"num_key_value_heads": 2,
"max_position_embeddings": 32,
"attention_dropout": 0.0,
"tie_word_embeddings": False,
"bos_token_id": 1,
"eos_token_id": 2,
"pad_token_id": 0,
}
values.update(overrides)
config = LlamaConfig(**values)
config.architectures = ["LlamaForCausalLM"]
config.dtype = "bfloat16"
return config
def _entry_value(entry: object, name: str) -> object:
value = getattr(entry, name)
return getattr(value, "value", value)
def _entries(manifest: object) -> tuple[object, ...]:
return tuple(getattr(manifest, "entries"))
def test_static_registry_is_one_exact_native_llama_bf16_tp2_profile():
profile = LLAMA_BF16_TP2_PROFILE
assert profile.model_type == "llama"
assert profile.model_class == "LlamaForCausalLM"
assert profile.dtype == "bfloat16"
assert profile.artifact_type == "hf_safetensors"
assert profile.tp_size == 2
assert dict(profile.plan) == EXPECTED_EFFECTIVE_PLAN
assert profile.plan_digest == contract_digest(EXPECTED_EFFECTIVE_PLAN)
assert tuple(profile.torch_versions) == ("2.13.0",)
assert tuple(profile.transformers_versions) == ("5.15.0",)
assert tuple(profile.accelerate_versions) == ("1.14.0",)
assert tuple(profile.safetensors_versions) == ("0.8.0",)
assert profile.runtime_storage_kind == "local_tensor_parameter"
def test_exact_llama_config_is_validated_without_mutating_it():
config = _llama_config()
before = config.to_dict()
validated = validate_model_config(config, profile=LLAMA_BF16_TP2_PROFILE)
assert validated is not None
assert config.to_dict() == before
@pytest.mark.parametrize(
("field", "value"),
[
("model_type", "qwen2"),
("architectures", ["MistralForCausalLM"]),
("architectures", None),
("architectures", ["LlamaForCausalLM", "OtherModel"]),
("tie_word_embeddings", True),
("dtype", "float16"),
("quantization_config", {"quant_method": "bitsandbytes"}),
("auto_map", {"AutoModelForCausalLM": "custom.Model"}),
("vision_config", {"hidden_size": 16}),
("audio_config", {"hidden_size": 16}),
("num_local_experts", 8),
("num_experts_per_tok", 2),
("layer_types", ["linear_attention"]),
],
)
def test_unsupported_family_feature_precision_and_tie_configs_refuse(field, value):
config = _llama_config()
setattr(config, field, value)
with pytest.raises(ContractError):
validate_model_config(config, profile=LLAMA_BF16_TP2_PROFILE)
@pytest.mark.parametrize(
("field", "value"),
[
("hidden_size", 15),
("intermediate_size", 31),
("num_attention_heads", 3),
("num_key_value_heads", 3),
("vocab_size", 31),
],
)
def test_nondivisible_tp2_dimensions_refuse_before_loading(field, value):
config = _llama_config()
setattr(config, field, value)
with pytest.raises(ContractError):
validate_model_config(config, profile=LLAMA_BF16_TP2_PROFILE)
@pytest.mark.parametrize(
("dependency", "version"),
[
("torch", "2.12.9"),
("torch", "2.13.1"),
("transformers", "5.14.9"),
("transformers", "5.15.1"),
("accelerate", "1.13.9"),
("accelerate", "1.14.1"),
("safetensors", "0.7.9"),
("safetensors", "0.8.1"),
],
)
def test_dependency_version_drift_refuses(monkeypatch, dependency, version):
installed = {
"torch": torch.__version__,
"transformers": transformers.__version__,
"accelerate": accelerate.__version__,
"safetensors": safetensors.__version__,
}
configured = dict(installed)
configured[dependency] = version
monkeypatch.setattr(
model_module.importlib.metadata,
"version",
lambda name: installed[name],
)
config = SimpleNamespace(software_versions=tuple(configured.items()))
with pytest.raises(RuntimeContractError) as failure:
model_module._require_runtime_versions(config, LLAMA_BF16_TP2_PROFILE)
assert failure.value.code == "LMS_RUNTIME_PROFILE_MISMATCH"
def test_plan_mapping_order_does_not_change_its_identity():
reversed_plan = dict(reversed(tuple(EXPECTED_EFFECTIVE_PLAN.items())))
assert contract_digest(reversed_plan) == LLAMA_BF16_TP2_PROFILE.plan_digest
@pytest.mark.parametrize(
"changed_plan",
[
{key: value for key, value in EXPECTED_EFFECTIVE_PLAN.items() if key != "lm_head"},
{**EXPECTED_EFFECTIVE_PLAN, "model.embed_tokens": "rowwise"},
{**EXPECTED_EFFECTIVE_PLAN, "model.layers.*.self_attn.q_proj": "rowwise"},
{**EXPECTED_EFFECTIVE_PLAN, "model.layers.*.self_attn.q_proj": "unknown"},
],
)
def test_missing_extra_changed_or_unknown_effective_plan_refuses(changed_plan):
changed_profile = replace(
LLAMA_BF16_TP2_PROFILE,
plan=tuple(changed_plan.items()),
)
with pytest.raises(ContractError):
validate_model_config(_llama_config(), profile=changed_profile)
def test_expected_manifest_is_complete_canonical_and_rank_specific():
config = _llama_config()
rank_zero = build_parameter_manifest(
config,
profile=LLAMA_BF16_TP2_PROFILE,
rank=0,
world_size=2,
)
rank_one = build_parameter_manifest(
config,
profile=LLAMA_BF16_TP2_PROFILE,
rank=1,
world_size=2,
)
zero = {entry.name: entry for entry in _entries(rank_zero)}
one = {entry.name: entry for entry in _entries(rank_one)}
expected_names = {
"model.embed_tokens.weight",
"model.layers.0.self_attn.q_proj.weight",
"model.layers.0.self_attn.k_proj.weight",
"model.layers.0.self_attn.v_proj.weight",
"model.layers.0.self_attn.o_proj.weight",
"model.layers.0.mlp.gate_proj.weight",
"model.layers.0.mlp.up_proj.weight",
"model.layers.0.mlp.down_proj.weight",
"model.layers.0.input_layernorm.weight",
"model.layers.0.post_attention_layernorm.weight",
"model.norm.weight",
"lm_head.weight",
}
assert set(zero) == set(one) == expected_names
assert tuple(entry.name for entry in _entries(rank_zero)) == tuple(sorted(expected_names))
assert rank_zero.digest == rank_one.digest
assert rank_zero.local_digest != rank_one.local_digest
shape_cases = {
"model.layers.0.self_attn.q_proj.weight": ((16, 16), (8, 16), 0),
"model.layers.0.self_attn.k_proj.weight": ((8, 16), (4, 16), 0),
"model.layers.0.self_attn.v_proj.weight": ((8, 16), (4, 16), 0),
"model.layers.0.self_attn.o_proj.weight": ((16, 16), (16, 8), 1),
"model.layers.0.mlp.gate_proj.weight": ((32, 16), (16, 16), 0),
"model.layers.0.mlp.up_proj.weight": ((32, 16), (16, 16), 0),
"model.layers.0.mlp.down_proj.weight": ((16, 32), (16, 16), 1),
"lm_head.weight": ((32, 16), (16, 16), 0),
}
for name, (global_shape, local_shape, shard_dim) in shape_cases.items():
assert tuple(zero[name].global_shape) == global_shape
assert tuple(zero[name].local_shape) == local_shape
assert _entry_value(zero[name], "placement") == "shard"
assert zero[name].shard_dim == shard_dim
assert zero[name].owner_ranks == (0, 1)
assert zero[name].dtype == "bfloat16"
for name in expected_names - set(shape_cases):
assert tuple(zero[name].global_shape) == tuple(zero[name].local_shape)
assert _entry_value(zero[name], "placement") == "replicate"
assert zero[name].shard_dim is None
assert zero[name].owner_ranks == (0, 1)
def test_manifest_records_bind_hook_sites_roles_ties_and_closed_serialization():
manifest = build_parameter_manifest(
_llama_config(),
profile=LLAMA_BF16_TP2_PROFILE,
rank=0,
world_size=2,
)
entries = _entries(manifest)
assert all(entry.tie_id is None for entry in entries)
assert {entry.hook_site for entry in entries if entry.hook_site} == {"model.layers.0"}
assert all(_entry_value(entry, "surgery_role") for entry in entries)
assert is_dataclass(manifest)
assert manifest.consensus_record() == {
"schema_version": 1,
"adapter_id": LLAMA_BF16_TP2_PROFILE.adapter_id,
"world_size": 2,
"parameter_count": len(entries),
"buffer_count": 0,
"hook_count": 1,
"root_digest": manifest.digest,
}
assert manifest.local_digest == contract_digest(manifest.to_dict())
assert asdict(manifest)["rank"] == 0
def _load_bindings() -> dict[str, str]:
return {
name: f"{index:064x}"
for index, name in enumerate(sorted(LOAD_EVIDENCE_BINDING_FIELDS), start=1)
}
def _loaded_evidence(
rank: int = 0,
*,
evidence_tier: str = "protocol_cpu",
) -> tuple[dict[str, object], object]:
manifest = build_parameter_manifest(
_llama_config(),
profile=LLAMA_BF16_TP2_PROFILE,
rank=rank,
world_size=2,
)
return (
build_rank_load_evidence(
result="loaded",
run_id="1" * 32,
rank=rank,
world_size=2,
evidence_tier=evidence_tier,
bindings=_load_bindings(),
manifest=manifest.to_dict(),
),
manifest,
)
def test_private_load_evidence_roundtrip_is_exclusive_and_summary_bound(tmp_path):
directory = tmp_path / "private"
directory.mkdir(mode=0o700)
record, manifest = _loaded_evidence()
summary = publish_rank_load_evidence(directory, record)
assert summary.manifest_digest == manifest.local_digest
assert LoadEvidenceSummary.from_bytes(summary.to_bytes()) == summary
assert read_rank_load_evidence(directory, run_id="1" * 32, rank=0) == record
assert verify_rank_load_evidence(directory, summary) == record
evidence_file = directory / f"load-evidence-{'1' * 32}-rank-0000.json"
assert evidence_file.stat().st_mode & 0o777 == 0o600
with pytest.raises(ContractError, match="already exists"):
publish_rank_load_evidence(directory, record)
def test_two_host_candidate_load_evidence_schema_is_distinct_and_closed():
record, _manifest = _loaded_evidence(evidence_tier="candidate_two_host_gpu")
assert record["evidence_tier"] == "candidate_two_host_gpu"
assert set(record["bindings"]) == set(LOAD_EVIDENCE_BINDING_FIELDS)
@pytest.mark.parametrize("unsafe_sink", ["world_readable", "symlink"])
def test_private_load_evidence_refuses_unsafe_destination(tmp_path, unsafe_sink):
record, _manifest = _loaded_evidence()
directory = tmp_path / "evidence"
if unsafe_sink == "world_readable":
directory.mkdir(mode=0o755)
# Exercise the unsafe mode even when the test runner uses umask 077.
directory.chmod(0o755)
assert directory.stat().st_mode & 0o777 == 0o755
else:
target = tmp_path / "target"
target.mkdir(mode=0o700)
directory.symlink_to(target, target_is_directory=True)
with pytest.raises(ContractError):
publish_rank_load_evidence(directory, record)
def test_private_load_evidence_detects_post_publish_tampering(tmp_path):
directory = tmp_path / "private"
directory.mkdir(mode=0o700)
record, _manifest = _loaded_evidence()
summary = publish_rank_load_evidence(directory, record)
evidence_file = directory / f"load-evidence-{'1' * 32}-rank-0000.json"
payload = json.loads(evidence_file.read_text(encoding="utf-8"))
payload["bindings"]["source_digest"] = "f" * 64
evidence_file.write_text(
json.dumps(payload, sort_keys=True, separators=(",", ":")) + "\n",
encoding="utf-8",
)
evidence_file.chmod(0o600)
with pytest.raises(ContractError, match="does not match"):
verify_rank_load_evidence(directory, summary)
@pytest.mark.parametrize(
("mutation", "message"),
[
(lambda value: value.pop("hooks"), "fields"),
(lambda value: value.update(rank=1), "rank identity"),
(lambda value: value.update(buffers=[{"name": "buffer"}]), "no persistent buffers"),
(lambda value: value["parameters"][0].update(path="private"), "forbidden"),
(lambda value: value["parameters"][0].update(offset=1.5), "floating-point"),
],
)
def test_loaded_evidence_refuses_incomplete_private_or_rank_drift(mutation, message):
_record, manifest = _loaded_evidence()
changed = manifest.to_dict()
mutation(changed)
with pytest.raises(ContractError, match=message):
build_rank_load_evidence(
result="loaded",
run_id="1" * 32,
rank=0,
world_size=2,
evidence_tier="protocol_cpu",
bindings=_load_bindings(),
manifest=changed,
)
@pytest.mark.parametrize(
("result", "error_code", "manifest", "message"),
[
("failed", "LMS_MODEL_LOAD_FAILED", {"partial": True}, "partial manifest"),
("failed", "LMS_LOAD_STATE_UNCERTAIN", None, "quarantine"),
("quarantined", "LMS_MODEL_LOAD_FAILED", None, "quarantine"),
("failed", "LMS_NOT_REGISTERED", None, "stable load error"),
],
)
def test_failure_and_quarantine_evidence_never_claim_partial_success(
result, error_code, manifest, message
):
with pytest.raises(ContractError, match=message):
build_rank_load_evidence(
result=result,
run_id="1" * 32,
rank=0,
world_size=2,
evidence_tier="protocol_cpu",
bindings=_load_bindings(),
manifest=manifest,
error_code=error_code,
)
def test_two_rank_loaded_summaries_require_distinct_private_records(tmp_path):
directory = tmp_path / "private"
directory.mkdir(mode=0o700)
summaries = []
for rank in range(2):
record, _manifest = _loaded_evidence(rank)
summaries.append(publish_rank_load_evidence(directory, record))
aggregate = validate_loaded_summary_set(summaries)
assert re.fullmatch(r"[0-9a-f]{64}", aggregate)
with pytest.raises(ContractError, match="reuse"):
validate_loaded_summary_set([summaries[0], replace(summaries[0], rank=1)])