Files
OBLITERATUS/tests/test_distributed_export_cuda_decisions.py

100 lines
5.2 KiB
Python

"""CUDA dispatch decisions with CPU-backed weights and explicit backend seams.
These tests establish guard ordering and exact reload budgets, not GPU support.
"""
import json
from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
import obliteratus.distributed.export as export
from obliteratus.distributed.contracts import RuntimeContractError, canonical_record
from obliteratus.distributed.preflight import inspect_source, storage_mount_digest
from obliteratus.distributed.rank_local_reload import StreamingLlamaReload
from obliteratus.distributed.surgery import ExciseTransactionState
from tests.test_distributed_export_gloo import _direct_runtime, _export_config, _source, _verified
@pytest.mark.parametrize("decision", ["wrong-strategy", "one-byte-short", "exact-budget"])
def test_cuda_reload_guard_precedes_staging_and_device_work(tmp_path, monkeypatch, decision):
live, source_root = tmp_path / "live", tmp_path / "immutable"
live.mkdir()
source_root.mkdir()
runtime = _direct_runtime(live)
runtime.staging_path.chmod(0o700)
source, _ = _source(source_root)
config_path = source / "config.json"
config_path.chmod(0o600)
config_path.write_text(json.dumps({"model_type": "llama", "tie_word_embeddings": False, "hidden_size": 4,
"intermediate_size": 8, "num_attention_heads": 2, "vocab_size": 8}))
config_path.chmod(0o444)
identity = inspect_source(source)
runtime = replace(runtime, source_path=source, source_identity=identity,
storage_digest=storage_mount_digest(runtime.staging_path),
tensor_device=torch.device("cuda:0"),
rank_local_config=SimpleNamespace(device_kind="cuda"))
_, verified = _verified(runtime.model, runtime.manifest, 0, live, runtime.run_id)
before = {name: parameter.detach().clone() for name, parameter in runtime.model.named_parameters()}
output = runtime.staging_path / "canonical-output"
output.mkdir()
(output / "prior.txt").write_text("prior")
# Physical authorization and placement are covered separately. Here the
# data-plane seam uses real CPU tensor validation for CUDA-tagged metadata.
validate = export._validate_model_structure
monkeypatch.setattr(export, "_rank_local_authorized", lambda _: True)
monkeypatch.setattr(export, "_group_identity", lambda _: None)
monkeypatch.setattr(export, "_validate_model_structure",
lambda value: validate(replace(value, tensor_device=torch.device("cpu"))))
monkeypatch.setattr(export, "revalidate_distributed_verified_state", lambda _: (
{name: export.tensor_digest(value) for name, value in before.items()}, verified.summary.post_state_digest,
))
monkeypatch.setattr(export, "unanimous_vote", lambda _, accepted, **kwargs: accepted)
monkeypatch.setattr(export, "require_consensus_digest", lambda digest, **kwargs: digest)
monkeypatch.setattr(export, "gloo_all_gather_records",
lambda record, **kwargs: (canonical_record(record),) * 2)
monkeypatch.setattr(export, "_broadcast_record", lambda runtime, rank, record: record)
reset = Mock()
monkeypatch.setattr(torch.cuda, "reset_peak_memory_stats", reset)
validator = StreamingLlamaReload(torch.ones((1, 1), dtype=torch.int64),
torch.zeros((1, 1, 8)), torch.ones((1, 2), dtype=torch.int64))
config = _export_config()
assignments = export._assignments(runtime.manifest, coordinator_only=True)
snapshots = export._metadata_snapshots(runtime, config)
required = validator.required_host_bytes(source, max(item.logical_bytes for item in assignments),
sum(item.size_bytes for item in snapshots), len(assignments))
assert 0 < required < config.semantic_reload_host_bytes_per_rank
if decision == "wrong-strategy":
validator = lambda *args: None
else:
config = replace(config, semantic_reload_host_bytes_per_rank=required - (decision == "one-byte-short"))
reached = []
def stop_before_transfer(point, sequence):
reached.append(point)
if point == "tensor_before":
raise RuntimeError("intentional stop before device transfer")
with pytest.raises(RuntimeContractError) as failure:
export.run_distributed_export(runtime, verified, config,
semantic_reload_validator=validator, fault_injector=stop_before_transfer)
if decision == "exact-budget":
assert failure.value.code == "LMS_EXPORT_MANIFEST_INVALID"
assert "staging_created" in reached and "tensor_before" in reached
reset.assert_called_once_with(runtime.tensor_device)
else:
assert failure.value.code == "LMS_EXPORT_RESOURCE_DENIED"
assert "staging_created" not in reached
reset.assert_not_called()
assert verified.excise_result.transaction.state is ExciseTransactionState.QUARANTINED
assert (output / "prior.txt").read_text() == "prior"
assert not list(runtime.staging_path.glob(".*rank-local-stage-*"))
for name, value in runtime.model.named_parameters():
assert torch.equal(value.detach(), before[name])