Files
OBLITERATUS/tests/test_distributed_rank_local_reload.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

116 lines
5.1 KiB
Python

"""Actual CPU Llama leaf-streaming reload with explicit optimized-Python guards."""
from __future__ import annotations
import json
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
import torch
from safetensors.torch import save_file
from obliteratus.distributed.contracts import ContractError
from obliteratus.distributed.export_contracts import ReloadValidationRequest, export_aggregate_digest
from obliteratus.distributed.model import model_config_digest
from obliteratus.distributed.rank_local_reload import StreamingLlamaReload
def fixture(tmp_path):
from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from transformers import LlamaConfig, LlamaForCausalLM, PreTrainedTokenizerFast
torch.manual_seed(287)
config = LlamaConfig(vocab_size=16, hidden_size=8, intermediate_size=16, num_hidden_layers=1,
num_attention_heads=2, num_key_value_heads=1, max_position_embeddings=32,
tie_word_embeddings=False, pad_token_id=0, bos_token_id=1, eos_token_id=2)
model = LlamaForCausalLM(config).to(dtype=torch.bfloat16).eval()
config.save_pretrained(tmp_path)
tokenizer = PreTrainedTokenizerFast(tokenizer_object=Tokenizer(WordLevel(
{f"t{index}": index for index in range(16)}, unk_token="t3")),
pad_token="t0", bos_token="t1", eos_token="t2", unk_token="t3")
tokenizer.save_pretrained(tmp_path)
mapping = {}
for index, (name, tensor) in enumerate(model.named_parameters()):
filename = f"model-{index:05d}.safetensors"
save_file({name: tensor.detach().contiguous()}, tmp_path / filename, metadata={"format": "pt"})
mapping[name] = filename
(tmp_path / "model.safetensors.index.json").write_text(json.dumps({"metadata": {}, "weight_map": mapping}))
tokens = torch.tensor([[1, 4, 5]], dtype=torch.int64)
with torch.inference_mode():
logits = model(input_ids=tokens, use_cache=False).logits.float()
generation = tokens
for _ in range(2):
step = model(input_ids=generation, use_cache=False).logits
generation = torch.cat((generation, step[:, -1].argmax(-1, keepdim=True)), dim=1)
request = ReloadValidationRequest(schema_version=1, rank=0, world_size=2,
export_identity_digest="a" * 64, key_set_digest=export_aggregate_digest(sorted(mapping), domain="keys"),
tensor_set_digest="b" * 64,
config_digest=model_config_digest(json.loads((tmp_path / "config.json").read_text())),
tokenizer_digest="c" * 64)
for path in tmp_path.iterdir():
if path.is_file():
path.chmod(0o444)
return StreamingLlamaReload(tokens, logits, generation), request
def test_streaming_llama_reopens_independent_weights_and_matches_reference(tmp_path, monkeypatch):
validator, request = fixture(tmp_path)
assert validator.required_host_bytes(tmp_path, 4096, 8192, 16) > 0
import obliteratus.distributed.rank_local_reload as module
original = module.safe_open
opened = []
def count(path, **kwargs):
opened.append(Path(path).name)
return original(path, **kwargs)
monkeypatch.setattr(module, "safe_open", count)
evidence = validator(tmp_path, request)
assert evidence.passed
assert len(opened) > len(set(opened))
@pytest.mark.parametrize("failure", ("weight", "index", "config", "tokenizer", "logits", "generation"))
def test_streaming_reload_rejects_independent_disagreement(tmp_path, failure):
validator, request = fixture(tmp_path)
for path in tmp_path.iterdir():
if path.is_file():
path.chmod(0o644)
if failure == "weight":
index = json.loads((tmp_path / "model.safetensors.index.json").read_text())
name, filename = next(iter(index["weight_map"].items()))
save_file({name: torch.zeros((1,), dtype=torch.bfloat16)}, tmp_path / filename, metadata={"format": "pt"})
elif failure == "index":
(tmp_path / "model.safetensors.index.json").write_text('{"weight_map":{}}')
elif failure == "config":
(tmp_path / "config.json").write_text('{"model_type":"changed"}')
elif failure == "tokenizer":
shutil.rmtree(tmp_path / "absent", ignore_errors=True)
for path in tmp_path.glob("tokenizer*"):
path.unlink()
elif failure == "logits":
validator.expected_logits.add_(32)
else:
validator.expected_generation[:, -1] = (validator.expected_generation[:, -1] + 1) % 16
for path in tmp_path.iterdir():
if path.is_file():
path.chmod(0o444)
with pytest.raises((ContractError, OSError, ValueError)):
validator(tmp_path, request)
def test_streaming_guards_reject_under_optimized_python():
script = """
import torch
from obliteratus.distributed.contracts import ContractError
from obliteratus.distributed.rank_local_reload import StreamingLlamaReload
try:
StreamingLlamaReload(torch.ones((1, 129), dtype=torch.int64), torch.ones((1, 129, 4)),
torch.ones((1, 130), dtype=torch.int64))
except ContractError:
raise SystemExit(0)
raise SystemExit('guard bypassed')
"""
subprocess.run([sys.executable, "-O", "-c", script], check=True, timeout=30)