Files
OBLITERATUS/tests/test_distributed_model_topology.py
Joseph MaglyandClaude Opus 5.5 da1573cb92 fix(distributed): reject malformed TP2 topology identities
(cherry picked from commit 2f164f69b0772db27e878905b26ff3eeb8478f2b)

Ported for #244 (part 6/6).

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

330 lines
11 KiB
Python

"""Closed local and two-host TP2 topology contracts for the experimental stack (#244)."""
from __future__ import annotations
import hashlib
from dataclasses import replace
from types import SimpleNamespace
import pytest
import obliteratus.distributed.model as model_module
import obliteratus.distributed.model_session as session_module
from obliteratus.distributed.contracts import ContractError, RuntimeContractError, TopologyPlan
from obliteratus.distributed.model import LLAMA_BF16_TP2_PROFILE
from obliteratus.distributed.model_session import EXTERNAL_LOAD_BINDING_FIELDS, ExactLoadProfile
from obliteratus.distributed.model_topology import (
GPU_CANDIDATE_EVIDENCE_TIERS,
TP2DeploymentLayout,
TP2TopologyBinding,
classify_tp2_topology,
configured_tp2_layout,
)
from obliteratus.distributed.preflight import SourceIdentity, SourceMountAttestation
def _digest(label: str) -> str:
return hashlib.sha256(label.encode("ascii")).hexdigest()
def _config(local_world_size: int) -> SimpleNamespace:
selection = SimpleNamespace(hf_config_digest=_digest("hf-config"))
return SimpleNamespace(
device_kind="cuda",
evidence_tier="candidate_preflight",
world_size=2,
local_world_size=local_world_size,
tensor_parallel_size=2,
coordinator_rank=0,
code_digest=_digest("code"),
digest=_digest("config"),
placement_plan_digest=_digest("plan"),
require_model=lambda: selection,
)
def _launch(rank: int, local_world_size: int) -> SimpleNamespace:
return SimpleNamespace(
rank=rank,
local_rank=rank if local_world_size == 2 else 0,
world_size=2,
local_world_size=local_world_size,
group_rank=0 if local_world_size == 2 else rank,
)
def _preflight(local_world_size: int) -> SimpleNamespace:
local = local_world_size == 2
records = tuple(
SimpleNamespace(
rank=rank,
local_rank=rank if local else 0,
local_world_size=local_world_size,
group_rank=0 if local else rank,
world_size=2,
host_digest=_digest("host-0" if local else f"host-{rank}"),
device_digest=_digest(f"device-{rank}"),
device_profile_digest=_digest("device-profile"),
device_config_digest=_digest("device-config"),
device_kind="cuda",
)
for rank in range(2)
)
return SimpleNamespace(
topology=TopologyPlan(
world_size=2,
coordinator_rank=0,
backend="gloo",
placement_plan_digest=_digest("plan"),
),
attestations=records,
identity_digest=_digest("identity"),
source_identity=SourceIdentity(
_digest("source"),
_digest("model"),
_digest("tokenizer"),
3,
128,
),
)
@pytest.mark.parametrize(
("local_world_size", "expected"),
(
(2, TP2DeploymentLayout.LOCAL_TP2),
(1, TP2DeploymentLayout.TWO_HOST_TP2),
),
)
def test_closed_tp2_layouts_bind_identically_on_every_rank(local_world_size, expected):
config = _config(local_world_size)
preflight = _preflight(local_world_size)
bindings = tuple(
classify_tp2_topology(config, _launch(rank, local_world_size), preflight)
for rank in range(2)
)
assert {item.layout for item in bindings} == {expected}
assert {item.host_count for item in bindings} == {1 if local_world_size == 2 else 2}
assert len({item.digest for item in bindings}) == 1
assert configured_tp2_layout(config, _launch(0, local_world_size)) is expected
@pytest.mark.parametrize("local_world_size", (2, 1))
def test_llama_adapter_capability_accepts_both_closed_tp2_layouts(local_world_size):
config = _config(local_world_size)
selection = config.require_model()
selection.adapter_id = LLAMA_BF16_TP2_PROFILE.adapter_id
config.placement_plan_digest = LLAMA_BF16_TP2_PROFILE.plan_digest
config.source_digest = _digest("source")
config.model_digest = _digest("model")
config.tokenizer_digest = _digest("tokenizer")
preflight = _preflight(local_world_size)
preflight.topology = TopologyPlan(
world_size=2,
coordinator_rank=0,
backend="gloo",
placement_plan_digest=LLAMA_BF16_TP2_PROFILE.plan_digest,
)
preflight.identity = SimpleNamespace(
config_digest=config.digest,
source_digest=config.source_digest,
model_digest=config.model_digest,
tokenizer_digest=config.tokenizer_digest,
)
for rank in range(2):
model_module._require_capability_binding(
config,
_launch(rank, local_world_size),
preflight,
LLAMA_BF16_TP2_PROFILE,
)
@pytest.mark.parametrize(
("local_world_size", "launch_changes"),
(
(3, {}),
(2, {"group_rank": 1}),
(2, {"local_rank": 0, "rank": 1}),
(1, {"group_rank": 0, "rank": 1}),
(1, {"local_rank": 1}),
),
)
def test_configured_tp2_layout_rejects_open_or_inconsistent_rank_geometry(
local_world_size,
launch_changes,
):
config = _config(local_world_size)
launch = _launch(int(launch_changes.get("rank", 0)), local_world_size)
for name, value in launch_changes.items():
setattr(launch, name, value)
with pytest.raises(RuntimeContractError) as failure:
configured_tp2_layout(config, launch)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
@pytest.mark.parametrize(
("target", "field", "value"),
(
("config", "world_size", 2.0),
("config", "coordinator_rank", False),
("launch", "rank", 0.0),
("launch", "local_rank", False),
),
)
def test_configured_tp2_layout_rejects_numeric_lookalikes(target, field, value):
config = _config(1)
launch = _launch(0, 1)
setattr(config if target == "config" else launch, field, value)
with pytest.raises(RuntimeContractError) as failure:
configured_tp2_layout(config, launch)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
@pytest.mark.parametrize(
("local_world_size", "rank", "field", "value"),
(
(1, 1, "host_digest", _digest("host-0")),
(2, 1, "host_digest", _digest("host-1")),
(1, 1, "device_digest", _digest("device-0")),
(1, 1, "device_profile_digest", _digest("different-profile")),
(1, 1, "device_config_digest", _digest("different-config")),
(1, 1, "group_rank", 0),
(1, 1, "local_rank", 1),
(1, 1, "local_world_size", 2),
(1, 1, "rank", 1.0),
(1, 1, "device_digest", "not-a-digest"),
),
)
def test_tp2_binding_rejects_host_device_and_attestation_drift(
local_world_size,
rank,
field,
value,
):
config = _config(local_world_size)
preflight = _preflight(local_world_size)
setattr(preflight.attestations[rank], field, value)
with pytest.raises(RuntimeContractError) as failure:
classify_tp2_topology(config, _launch(0, local_world_size), preflight)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
@pytest.mark.parametrize(
("backend", "placement_plan_digest"),
(("nccl", _digest("plan")), ("gloo", _digest("different-plan"))),
)
def test_tp2_binding_rejects_preflight_topology_drift(backend, placement_plan_digest):
config = _config(1)
preflight = _preflight(1)
preflight.topology = TopologyPlan(
world_size=2,
coordinator_rank=0,
backend=backend,
placement_plan_digest=placement_plan_digest,
)
with pytest.raises(RuntimeContractError) as failure:
classify_tp2_topology(config, _launch(0, 1), preflight)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
@pytest.mark.parametrize(
("field", "value"),
(
("schema_version", True),
("layout", "two_host_tp2"),
("world_size", 2.0),
("local_world_size", 1.0),
("host_count", 2.0),
("rank_mapping_digest", "not-a-digest"),
),
)
def test_tp2_binding_rejects_noncanonical_field_types(field, value):
binding = classify_tp2_topology(_config(1), _launch(0, 1), _preflight(1))
assert isinstance(binding, TP2TopologyBinding)
with pytest.raises(RuntimeContractError) as failure:
replace(binding, **{field: value})
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
def test_tp2_binding_rejects_incomplete_attestation_set():
config = _config(1)
preflight = _preflight(1)
preflight.attestations = preflight.attestations[:1]
with pytest.raises(RuntimeContractError) as failure:
classify_tp2_topology(config, _launch(0, 1), preflight)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
def test_two_host_evidence_tier_is_unaccepted_and_requires_gpu_mount_controls():
assert GPU_CANDIDATE_EVIDENCE_TIERS == {
"candidate_local_gpu",
"candidate_two_host_gpu",
}
mount = SourceMountAttestation(
schema_version=1,
evidence_tier="candidate_two_host_gpu",
mechanism="read_only_content_addressed",
source_digest=_digest("source"),
mount_digest=_digest("mount"),
content_root_digest=_digest("source"),
external_evidence_digest=_digest("external"),
read_only=True,
content_addressed=True,
production_accepted=False,
)
profile = ExactLoadProfile(
schema_version=1,
evidence_tier="candidate_two_host_gpu",
profile_state="candidate_unaccepted",
residual_risk_accepted=False,
transport_security="none_claimed",
session_timeout_seconds=60,
bindings={name: _digest(name) for name in EXTERNAL_LOAD_BINDING_FIELDS},
source_mount=mount,
)
assert profile.profile_state == "candidate_unaccepted"
assert profile.residual_risk_accepted is False
with pytest.raises(ContractError):
SourceMountAttestation(
**{**mount.__dict__, "read_only": False}
)
def test_preload_binding_rejects_evidence_tier_layout_confusion(monkeypatch):
config = _config(1)
preflight = _preflight(1)
external = {name: _digest(name) for name in EXTERNAL_LOAD_BINDING_FIELDS}
monkeypatch.setattr(
session_module,
"observed_external_runtime_bindings",
lambda: external,
)
exact = SimpleNamespace(evidence_tier="candidate_local_gpu", bindings=external)
with pytest.raises(RuntimeContractError) as failure:
session_module._validate_preload_bindings(
config,
_launch(0, 1),
preflight,
exact,
source_mount_digest=_digest("mount"),
control_group_digest=_digest("control"),
)
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
exact.evidence_tier = "candidate_two_host_gpu"
bindings = session_module._validate_preload_bindings(
config,
_launch(0, 1),
preflight,
exact,
source_mount_digest=_digest("mount"),
control_group_digest=_digest("control"),
)
assert bindings["deployment_layout_digest"] == classify_tp2_topology(
config,
_launch(0, 1),
preflight,
).digest