mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Admit VALIDATE before the iterative-refinement gate, and re-admit EXCISE afterwards, so strict mode can reject the gate before it materialises. Treat forward workspace as estimate-only when a decoder layer is split across devices, since the estimator would charge every participant with the whole layer; unsplit single- and multi-GPU numbers are unchanged. Admit the stages that informed/Ouroboros loops, Concept Lab, concept baseline and evaluation, checkpoint verification and the benchmark script run outside the main pipeline. Work without a qualified bound is unmodelled: default mode warns and proceeds, strict mode rejects. Refs #245 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
9.6 KiB
Python
182 lines
9.6 KiB
Python
"""Unknown SDPA dispatch and optional returned outputs never become calibrated."""
|
|
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
import torch
|
|
from accelerate.hooks import AlignDevicesHook
|
|
from torch._subclasses.fake_tensor import FakeTensorMode
|
|
from transformers import LlamaConfig, LlamaForCausalLM, Qwen2MoeConfig, Qwen2MoeForCausalLM
|
|
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.phase_admission import (
|
|
ForwardWorkload,
|
|
MemoryObservation,
|
|
PhaseAdmissionError,
|
|
admit_phase,
|
|
model_phase_demand,
|
|
)
|
|
|
|
|
|
MIB = 1024 ** 2
|
|
|
|
|
|
def cuda_metadata_handle(*, moe=False):
|
|
"""Real model/config contracts with fake CUDA storage; no driver is needed."""
|
|
config_type, model_type = ((Qwen2MoeConfig, Qwen2MoeForCausalLM) if moe
|
|
else (LlamaConfig, LlamaForCausalLM))
|
|
config = config_type(
|
|
vocab_size=32, hidden_size=64, intermediate_size=96, num_hidden_layers=2,
|
|
num_attention_heads=8, num_key_value_heads=2, max_position_embeddings=8192,
|
|
**({"num_experts": 2, "num_experts_per_tok": 1, "moe_intermediate_size": 32,
|
|
"shared_expert_intermediate_size": 96} if moe else {}),
|
|
)
|
|
with torch.device("meta"):
|
|
model = model_type(config).to(dtype=torch.bfloat16).eval()
|
|
config._attn_implementation = "sdpa"
|
|
with FakeTensorMode():
|
|
for owner in model.modules():
|
|
for name, parameter in list(owner._parameters.items()):
|
|
if parameter is not None:
|
|
owner._parameters[name] = torch.nn.Parameter(
|
|
torch.empty(parameter.shape, dtype=parameter.dtype, device="cuda:0"),
|
|
requires_grad=False,
|
|
)
|
|
for name, buffer in list(owner._buffers.items()):
|
|
if buffer is not None:
|
|
owner._buffers[name] = torch.empty(
|
|
buffer.shape, dtype=buffer.dtype, device="cuda:0",
|
|
)
|
|
model.hf_device_map = {"": "cuda:0"}
|
|
return ModelHandle(model, None, config, "offline-confidence-metadata", "causal_lm")
|
|
|
|
|
|
def component_bytes(demand, confidence):
|
|
return sum(item.bytes or 0 for item in demand.components if item.confidence == confidence)
|
|
|
|
|
|
@pytest.mark.parametrize("attention_mask", [None, False], ids=["unknown-mask", "unknown-dispatch"])
|
|
def test_unverified_sdpa_quadratic_workspace_is_estimated(attention_mask, monkeypatch):
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: False)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
handle = cuda_metadata_handle()
|
|
plan = model_phase_demand(
|
|
handle, "probe", mode="default", forward_workloads=[
|
|
ForwardWorkload(1, 4096, attention_mask=attention_mask, logits_dtype_bytes=2),
|
|
],
|
|
)
|
|
calibrated = component_bytes(plan, "calibrated")
|
|
assert 0 < calibrated < 64 * MIB
|
|
assert component_bytes(plan, "estimated") > 512 * MIB
|
|
calibrated_names = {item.name for item in plan.components if item.confidence == "calibrated"}
|
|
assert any(name.endswith("kv_bytes") for name in calibrated_names)
|
|
assert any(name.endswith("output_bytes") for name in calibrated_names)
|
|
assert any(name.endswith("activation_bytes") for name in calibrated_names)
|
|
observer = lambda device: MemoryObservation(device, 64 * MIB, 2 ** 30)
|
|
report = admit_phase("probe", plan, observer=observer, safety_reserve_bytes=0, mode="default")
|
|
assert len(report.warnings) == 1
|
|
assert "cuda:0" in report.warnings[0]
|
|
with pytest.raises(PhaseAdmissionError):
|
|
admit_phase("probe", plan, observer=observer, safety_reserve_bytes=0, mode="strict")
|
|
|
|
|
|
@pytest.mark.parametrize("backend,output", [
|
|
("sdpa", "hidden_states"), ("sdpa", "generation_hidden_states"),
|
|
("eager", "attentions"), ("eager", "hidden_states"), ("eager", "generation_hidden_states"),
|
|
])
|
|
def test_optional_returned_outputs_do_not_promote_retention(output, backend, monkeypatch):
|
|
# Transformers rejects output_attentions=True with SDPA at configuration
|
|
# time. Returned attention matrices therefore exercise the valid eager path.
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: False)
|
|
handle = cuda_metadata_handle()
|
|
handle.model.config._attn_implementation = backend
|
|
workload = ForwardWorkload(1, 128, attention_mask=False, logits_dtype_bytes=2)
|
|
baseline = model_phase_demand(handle, "verify", forward_workloads=[workload], mode="default")
|
|
if output == "attentions":
|
|
handle.model.config.output_attentions = True
|
|
elif output == "hidden_states":
|
|
handle.model.config.output_hidden_states = True
|
|
else:
|
|
workload = replace(workload, output_hidden_states=True, retained_hidden_tokens=256)
|
|
returned = model_phase_demand(handle, "verify", forward_workloads=[workload], mode="default")
|
|
assert component_bytes(returned, "calibrated") == component_bytes(baseline, "calibrated")
|
|
assert component_bytes(returned, "estimated") > component_bytes(baseline, "estimated")
|
|
|
|
|
|
@pytest.mark.parametrize("backend", ["sdpa", "eager"])
|
|
def test_qualified_dense_forward_keeps_calibrated_base(backend, monkeypatch):
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: True)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
handle = cuda_metadata_handle()
|
|
handle.model.config._attn_implementation = backend
|
|
plan = model_phase_demand(handle, "probe", mode="default", forward_workloads=[
|
|
ForwardWorkload(1, 128, attention_mask=False, logits_dtype_bytes=2),
|
|
])
|
|
assert component_bytes(plan, "calibrated") == sum(
|
|
value.additional_bytes for value in plan.devices.values()
|
|
)
|
|
assert component_bytes(plan, "estimated") == 0
|
|
observer = lambda device: MemoryObservation(device, 64 * MIB, 2 ** 30)
|
|
report = admit_phase("probe", plan, observer=observer, safety_reserve_bytes=0, mode="default")
|
|
assert report.warnings == ()
|
|
|
|
|
|
def test_router_retention_stays_estimated_for_moe(monkeypatch):
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: False)
|
|
handle = cuda_metadata_handle(moe=True)
|
|
workload = ForwardWorkload(1, 128, attention_mask=False, logits_dtype_bytes=2)
|
|
baseline = model_phase_demand(handle, "probe", forward_workloads=[workload], mode="default")
|
|
handle.model.config.output_router_logits = True
|
|
returned = model_phase_demand(handle, "probe", forward_workloads=[workload], mode="default")
|
|
assert component_bytes(baseline, "calibrated") == component_bytes(returned, "calibrated") == 0
|
|
assert component_bytes(returned, "estimated") > component_bytes(baseline, "estimated")
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["baseline", "probe", "verify", "excise"])
|
|
def test_split_projection_does_not_calibrate_another_gpus_ffn_workspace(phase, monkeypatch):
|
|
# Reviewer's reproducer: cuda:1 hosts only an 8 KiB query projection. Its
|
|
# inputs/outputs are 32 KiB each, so 20 MiB generously covers its real work,
|
|
# including the fixed 16 MiB runtime allowance. The wide FFN is on cuda:0.
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: True)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
handle = cuda_metadata_handle()
|
|
handle.model.config.intermediate_size = 65536
|
|
with FakeTensorMode():
|
|
for layer in handle.model.model.layers:
|
|
for name, shape in (("gate_proj", (65536, 64)), ("up_proj", (65536, 64)),
|
|
("down_proj", (64, 65536))):
|
|
module = getattr(layer.mlp, name)
|
|
module.in_features, module.out_features = shape[1], shape[0]
|
|
module.weight = torch.nn.Parameter(
|
|
torch.empty(shape, device="cuda:0", dtype=torch.bfloat16), requires_grad=False,
|
|
)
|
|
query = handle.model.model.layers[0].self_attn.q_proj
|
|
query.weight = torch.nn.Parameter(
|
|
torch.empty(query.weight.shape, device="cuda:1", dtype=query.weight.dtype),
|
|
requires_grad=False,
|
|
)
|
|
query._hf_hook = AlignDevicesHook(execution_device="cuda:1", io_same_device=True)
|
|
handle.model.hf_device_map = {"": "cuda:0", "model.layers.0.self_attn.q_proj": "cuda:1"}
|
|
plan = model_phase_demand(handle, phase, mode="default", forward_workloads=[
|
|
ForwardWorkload(1, 256, attention_mask=False, logits_dtype_bytes=2),
|
|
])
|
|
assert plan.devices["cuda:1"].additional_bytes == 117821440
|
|
assert component_bytes(plan, "estimated") > 100 * MIB
|
|
calibrated = [item for item in plan.components if item.confidence == "calibrated"]
|
|
if phase == "excise":
|
|
# The ordinary edit floor survives even when its optional forward is
|
|
# split. A smaller real budget still rejects that independently known work.
|
|
assert calibrated and all(item.name == "basic edit.edit_bytes" for item in calibrated)
|
|
with pytest.raises(PhaseAdmissionError):
|
|
admit_phase(phase, plan, mode="default", safety_reserve_bytes=0,
|
|
observer=lambda d: MemoryObservation(d, 0, 1024 * MIB))
|
|
else:
|
|
assert not calibrated
|
|
observer = lambda d: MemoryObservation(d, 20 * MIB if d == "cuda:1" else 1024 * MIB,
|
|
1024 * MIB)
|
|
report = admit_phase(phase, plan, safety_reserve_bytes=0, observer=observer, mode="default")
|
|
assert any("cuda:1" in warning for warning in report.warnings)
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_phase(phase, plan, safety_reserve_bytes=0, observer=observer, mode="strict")
|
|
assert caught.value.device == "cuda:1"
|