Files
OBLITERATUS/tests/test_distributed_manifest_digest_contract.py
Joseph MaglyandClaude Opus 5.5 62acc92034 fix(distributed): use one canonical digest for manifest parameter values
Native TP load set ParameterManifestEntry.local_value_digest and
replicated_value_digest with model._tensor_digest (sha256 of raw bytes),
while EXCISE (_build_operation_plan) and VERIFY (_logical_state_roots with
require_manifest_values) compare probe_contracts.tensor_digest (dtype +
shape + bytes). The pristine-digest check could therefore never pass on
the native TP path: both ranks of llama-tp-local-gpu on 2x A100 failed
EXCISE with "mutable parameter storage or pristine digest changed".

Load now uses probe_contracts.tensor_digest and the raw-bytes helper is
deleted; hashing failures keep LMS_RUNTIME_PROFILE_MISMATCH. A CPU
contract test builds the manifest through the real load-validation path
and feeds it to the real EXCISE admission and VERIFY value checks
(accept unchanged, reject one-element mutation), plus BF16/FP32 digest
identity.

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

167 lines
6.8 KiB
Python

"""Native-load manifest values must pass the real EXCISE and VERIFY admission checks.
All tensors live on CPU. The admission suite's CUDA-reporting Parameter shim
satisfies the native loader's placement contract without allocating CUDA storage;
only collective transport is replaced, leaving parameter admission unchanged.
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
import torch
import obliteratus.distributed.model as model_module
import obliteratus.distributed.surgery as surgery
import obliteratus.distributed.verification as verification
from obliteratus.distributed.contracts import (
ContractError, RuntimeContractError, canonical_record, contract_digest,
)
from obliteratus.distributed.model_manifest import ManifestPlacement, SurgeryRole
from obliteratus.distributed.probe_contracts import (
LayerProbeArtifact, ProbeDistillResult, ProbeDistillSummary,
)
from obliteratus.distributed.surgery_contracts import ExciseConfig
from tests.test_distributed_model_admission import (
_LoadedModel, _validate, manifest as manifest,
)
pytestmark = pytest.mark.cpu
MUTABLE = "model.layers.0.self_attn.q_proj.weight"
REPLICATED = "model.norm.weight"
DIGEST = "a" * 64
@pytest.fixture
def loaded_runtime(monkeypatch, manifest):
monkeypatch.setattr(
model_module, "gather_failure_codes",
lambda _sequence, code, **_kwargs: (code,) if code else (),
)
monkeypatch.setattr(
model_module, "require_consensus_digest", lambda digest, *, group=None: digest,
)
mesh = object()
model = _LoadedModel(manifest, mesh)
final, replicas = _validate(model, manifest, mesh)
parameters = dict(model.named_parameters(remove_duplicate=False))
for entry in final.entries:
parameter = parameters[entry.name]
assert parameter.detach().device == torch.device("cpu")
assert entry.dtype == "bfloat16" and str(parameter.dtype) == "torch.bfloat16"
if entry.placement is ManifestPlacement.REPLICATE:
assert entry.replicated_value_digest == entry.local_value_digest
assert dict(replicas) == {
entry.name: entry.replicated_value_digest
for entry in final.entries if entry.placement is ManifestPlacement.REPLICATE
}
return SimpleNamespace(
model=model, manifest=final, rank=final.rank, world_size=final.world_size,
tensor_device=torch.device("cuda", 0), control_group=object(),
)
def _distilled(runtime):
entry = next(item for item in runtime.manifest.entries if item.name == MUTABLE)
width = entry.global_shape[1]
direction = torch.zeros((1, width), dtype=torch.float32)
direction[0, 0] = 1.0
artifact = LayerProbeArtifact(
site=entry.hook_site, layer_index=0, activation_digest=DIGEST,
max_abs_error=0.0, max_rel_error=0.0, score=1.0,
harmful_mean=direction[0].clone(), harmless_mean=torch.zeros(width, dtype=torch.float32),
directions=direction,
)
summary = ProbeDistillSummary(
schema_version=1, run_id="1" * 32, loaded_identity_digest=DIGEST,
config_digest=DIGEST, corpus_digest=DIGEST, sample_ids_digest=DIGEST,
tokenizer_contract_digest=DIGEST, tokenization_digest=DIGEST,
harmful_count=1, harmless_count=1, input_width=1,
activation_atol_hex=(0.0).hex(), activation_rtol_hex=(0.0).hex(),
selected_sites=(artifact.site,), layers=(artifact.summary_record(),),
)
result = ProbeDistillResult(summary, (artifact,), (), ())
runtime.run_id = summary.run_id
runtime.distilled_digest = result.digest
return result
@pytest.mark.parametrize("mutated", [False, True])
def test_native_load_manifest_passes_full_excise_admission_unless_mutated(loaded_runtime, mutated):
runtime = loaded_runtime
distilled = _distilled(runtime)
parameter = runtime.model._parameters[MUTABLE]
if mutated:
with torch.no_grad():
parameter[0, 0] += 1
with pytest.raises(ContractError, match="mutable parameter storage or pristine digest changed"):
surgery._build_operation_plan(runtime, distilled, ExciseConfig())
else:
operations, parameters, artifacts = surgery._build_operation_plan(
runtime, distilled, ExciseConfig(),
)
expected = {
entry.name for entry in runtime.manifest.entries
if entry.hook_site in distilled.summary.selected_sites
and entry.surgery_role in ExciseConfig().roles
}
assert {operation.logical_name for operation in operations} == expected
assert MUTABLE in parameters and parameters[MUTABLE] is parameter
assert {item.surgery_role for item in operations} == {
SurgeryRole.ATTENTION_INPUT, SurgeryRole.ATTENTION_OUTPUT,
SurgeryRole.MLP_INPUT, SurgeryRole.MLP_OUTPUT,
}
assert set(artifacts) == set(distilled.summary.selected_sites)
@pytest.mark.parametrize("name", [MUTABLE, REPLICATED])
@pytest.mark.parametrize("mutated", [False, True])
def test_native_load_manifest_passes_verify_manifest_values_unless_mutated(
monkeypatch, loaded_runtime, name, mutated,
):
runtime = loaded_runtime
votes = []
def vote(sequence, accepted, *, group):
assert group is runtime.control_group
votes.append((sequence, accepted))
return accepted
def gather(record, *, group):
assert group is runtime.control_group
# Equal rank values satisfy replicated consensus; fixture shards are
# also identical, so the exact two-rank state root remains deterministic.
return [
canonical_record({**record, "rank": rank} if "rank" in record else record)
for rank in range(runtime.world_size)
]
monkeypatch.setattr(verification, "unanimous_vote", vote)
monkeypatch.setattr(verification, "gloo_all_gather_records", gather)
monkeypatch.setattr(
verification, "require_consensus_digest", lambda digest, *, group: digest,
)
if mutated:
parameter = runtime.model._parameters[name]
with torch.no_grad():
parameter.reshape(-1)[0] += 1
with pytest.raises(RuntimeContractError) as error:
verification._logical_state_roots(
runtime, sequence_base=100, require_manifest_values=True,
)
assert error.value.code == "LMS_VERIFY_STATE_DIVERGED"
assert votes[-1][1] is False
else:
roots, state_digest = verification._logical_state_roots(
runtime, sequence_base=100, require_manifest_values=True,
)
assert roots == {
entry.name: contract_digest((entry.local_value_digest,) * runtime.world_size)
for entry in runtime.manifest.entries
}
assert state_digest == verification._state_roots_digest(roots)
assert all(accepted for _sequence, accepted in votes)