Files
OBLITERATUS/tests/test_phase_mamba_admission.py
Joseph MaglyandClaude Opus 5.5 e5db2ed089 test(gpu): fit admission tests in the PR core budget
Run real-peak admission tests at four CPU threads in the PR suite, with the
one- and eight-thread variants marked slow, and shrink their geometry to
the smallest size that still discriminates the modelled workspace terms.
Evaluate the differential matrix per handle so every combination keeps its
invariant at a fraction of the cost. The PR core selection had grown past
its ten-minute limit.

The smaller geometries exposed CPU estimate gaps, now modelled: autocast
parameter-copy staging, SDPA operand casts under CPU autocast and per-worker
reduced-precision GEMM scratch. These stay estimate-only; CUDA calibrated
numbers are unchanged.

Refs #245

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

250 lines
11 KiB
Python

"""Real CPU scan peaks, selected backends and chunk-padding admission."""
from functools import wraps
import importlib
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from transformers import Mamba2Config, Mamba2ForCausalLM
from obliteratus import phase_admission as admission
from obliteratus.models.loader import ModelHandle
from tests.test_phase_memory_helpers import cpu_memory_profile, cpu_peak_bytes
from tests.test_phase_memory_helpers import cpu_threads as cpu_threads
pytestmark = pytest.mark.cpu
def _handle(dtype=torch.float32, *, original_geometry=False):
config = Mamba2Config(
vocab_size=32, hidden_size=64 if original_geometry else 32,
state_size=64 if original_geometry else 16, num_hidden_layers=2,
num_heads=4 if original_geometry else 2, head_dim=32, expand=2,
n_groups=1, chunk_size=128 if original_geometry else 16,
)
model = Mamba2ForCausalLM(config).to(dtype).eval()
return ModelHandle(model, Mock(), config, "random-local", "causal_lm")
@pytest.mark.parametrize("batch,tokens,dtype", [
(4, 32, torch.float32), (2, 17, torch.float32),
(1, 8, torch.float32), (2, 17, torch.bfloat16),
])
@pytest.mark.usefixtures("cpu_threads")
def test_mamba2_real_cpu_peak_is_bounded_without_cumulative_broadcast_overcharge(
batch, tokens, dtype, monkeypatch,
):
handle = _handle(dtype)
inputs = torch.zeros(batch, tokens, dtype=torch.long)
demand = admission.model_phase_demand(
handle, "probe", forward_workloads=[admission.ForwardWorkload(batch, tokens)],
)
with torch.inference_mode(), cpu_memory_profile() as profile:
result = handle.model(inputs)
assert torch.isfinite(result.logits).all()
peak = cpu_peak_bytes(profile)
estimated = demand.devices["cpu"].additional_bytes
assert 0 < peak <= estimated <= 1.15 * peak + 64 * 1024
# Shrinking the fixture must still detect omission of the scan products,
# rather than letting unrelated linear work hide the original defect.
with monkeypatch.context() as omitted:
omitted.setattr(admission, "_mamba2_torch_scan_workspace", lambda *args: 0)
without_scan = admission.model_phase_demand(
handle, "probe", forward_workloads=[admission.ForwardWorkload(batch, tokens)],
)
assert peak > without_scan.devices["cpu"].additional_bytes
def test_original_mamba2_review_geometry_exceeds_96_mib_budget_without_running_scan():
# Real scans above preserve multi-chunk, padded, short and BF16 coverage.
# Keep the original 138.47 MiB regression's policy threshold metadata-only.
handle = _handle(original_geometry=True)
demand = admission.model_phase_demand(
handle, "probe", forward_workloads=[admission.ForwardWorkload(4, 256)],
)
assert 138 * 1024**2 <= demand.devices["cpu"].additional_bytes < 160 * 1024**2
budget = 96 * 1024**2
observer = lambda device: admission.MemoryObservation(device, budget, budget)
report = admission.admit_phase("probe", demand, observer=observer)
assert report.warnings
assert all(c.confidence == "estimated" for c in report.components)
with pytest.raises(admission.PhaseAdmissionError, match="probe.*cpu.*shortfall"):
admission.admit_phase("probe", demand, observer=observer, mode="strict")
@pytest.mark.usefixtures("cpu_threads")
def test_mamba2_cached_single_token_uses_recurrent_update_not_padded_chunk_scan():
handle = _handle()
with torch.inference_mode():
previous = handle.model(torch.zeros(2, 32, dtype=torch.long)).cache_params
inputs = torch.zeros(2, 1, dtype=torch.long)
demand = admission.model_phase_demand(
handle, "verify",
forward_workloads=[admission.ForwardWorkload(2, 1, cache_tokens=33)],
)
with torch.inference_mode(), cpu_memory_profile() as profile:
result = handle.model(inputs, cache_params=previous)
assert torch.isfinite(result.logits).all()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= demand.devices["cpu"].additional_bytes < 2 * 1024**2
def test_explicit_compiled_scan_request_does_not_silently_receive_torch_bound():
config = _handle().config
with pytest.raises(ValueError, match="mamba2.*mamba_ssm.*OBLITERATUS_PHASE_ADMISSION=off"):
admission.transformer_demands(
config, ["cpu"] * config.num_hidden_layers, batch_size=1, tokens=8,
dtype_bytes=4, resident_bytes={}, largest_tensor_bytes={}, output_device="cpu",
mamba_scan_backend="mamba_ssm",
)
@pytest.mark.parametrize("family", sorted(admission._MAMBA2_FAMILIES))
@pytest.mark.usefixtures("cpu_threads")
def test_hybrid_mamba2_fallbacks_share_measured_broadcast_bound(family):
module = importlib.import_module(f"transformers.models.{family}.modeling_{family}")
batch, tokens, heads, dim, state, groups, chunk = 1, 65, 4, 8, 16, 1, 64
values = torch.randn(batch, tokens, heads, dim)
delta = torch.rand(batch, tokens, heads)
a = -torch.ones(heads)
bc = torch.randn(batch, tokens, groups, state)
with torch.no_grad(), cpu_memory_profile() as profile:
output = module.mamba2_chunk_scan(values, delta, a, bc, bc, chunk, D=torch.ones(heads))
assert torch.isfinite(output).all()
peak = cpu_peak_bytes(profile)
bound = admission._mamba2_torch_scan_workspace(batch, tokens, heads, dim, state, chunk)
assert 0 < peak <= bound < 1.1 * peak
@pytest.mark.parametrize("geometry", [
(1, 3, 2, 2, 2, 1, 8), (2, 16, 4, 16, 8, 2, 8),
(2, 67, 4, 64, 8, 2, 16), (2, 129, 4, 8, 64, 2, 16),
(1, 257, 4, 8, 8, 1, 8),
])
@pytest.mark.usefixtures("cpu_threads")
def test_scan_bound_covers_state_products_diagonal_and_chunk_recurrence_peaks(geometry):
from transformers.models.mamba2.modeling_mamba2 import mamba2_chunk_scan
batch, tokens, heads, dim, state, groups, chunk = geometry
values = torch.randn(batch, tokens, heads, dim)
delta = torch.rand(batch, tokens, heads)
a = -torch.ones(heads)
bc = torch.randn(batch, tokens, groups, state)
with torch.no_grad(), cpu_memory_profile() as profile:
output, final = mamba2_chunk_scan(
values, delta, a, bc, bc, chunk, D=torch.ones(heads), return_final_states=True,
)
assert torch.isfinite(output).all() and torch.isfinite(final).all()
peak = cpu_peak_bytes(profile)
bound = admission._mamba2_torch_scan_workspace(batch, tokens, heads, dim, state, chunk)
assert 0 < peak <= bound < 1.1 * peak
@pytest.mark.parametrize("family", ["qwen3_next", "qwen3_5", "qwen3_5_moe"])
@pytest.mark.parametrize("tokens", [1, 32, 129])
@pytest.mark.usefixtures("cpu_threads")
def test_delta_rule_fallback_bound_covers_real_chunk_and_decode_peaks(family, tokens):
module = importlib.import_module(f"transformers.models.{family}.modeling_{family}")
batch, heads, key_dim, value_dim = 1, 4, 8, 16
query = torch.randn(batch, tokens, heads, key_dim)
values = torch.randn(batch, tokens, heads, value_dim)
gate = -torch.rand(batch, tokens, heads)
beta = torch.rand(batch, tokens, heads)
decode = tokens == 1
scan = (module.torch_recurrent_gated_delta_rule if decode
else module.torch_chunk_gated_delta_rule)
kwargs = {"initial_state": None, "output_final_state": True}
with torch.no_grad(), cpu_memory_profile() as profile:
output, final = scan(query, query, values, gate, beta, **kwargs)
assert torch.isfinite(output).all() and torch.isfinite(final).all()
peak = cpu_peak_bytes(profile)
bound = admission._delta_rule_torch_workspace(
batch, tokens, heads, key_dim, value_dim, decode=decode,
)
assert 0 < peak <= bound < 2 * peak
@pytest.mark.parametrize("selected", ["unknown", "mamba_ssm", "hub", "not_callable"])
def test_unmodelled_mamba2_backend_rejects_explicitly_before_forward(monkeypatch, selected):
from transformers.models.mamba2 import modeling_mamba2
handle = _handle()
original = modeling_mamba2.mamba2_chunk_scan
def implementation(*args, **kwargs):
raise AssertionError("admission must not run a scan")
implementation.__module__ = f"{selected}.kernels"
@wraps(original)
def wrapper(*args, **kwargs):
return implementation(*args, **kwargs)
if selected == "hub":
handle.model._use_kernels = True
else:
monkeypatch.setattr(modeling_mamba2, "mamba2_chunk_scan",
Mock() if selected == "not_callable" else wrapper)
work = Mock()
report = admission.admit_model_phase(handle, "probe", observer=lambda d: None)
assert report.warnings and any(c.bytes is None for c in report.components)
work()
work.assert_called_once()
work.reset_mock()
message = "probe.*unmodelled mamba2 recurrent scan.*OBLITERATUS_PHASE_ADMISSION=off"
with pytest.raises(admission.PhaseAdmissionError, match=message):
admission.admit_model_phase(handle, "probe", observer=lambda d: None, mode="strict")
work()
work.assert_not_called()
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
assert admission.admit_model_phase(handle, "probe", observer=lambda d: None).bypassed
@pytest.mark.parametrize("family", ["qwen3_next", "qwen3_5", "qwen3_5_moe"])
def test_delta_rule_backend_inspects_frozen_callable_not_geometry(monkeypatch, family):
module = importlib.import_module(f"transformers.models.{family}.modeling_{family}")
model = SimpleNamespace(modules=lambda: [type("Mixer", (), {"__module__": module.__name__})()])
config = SimpleNamespace(model_type=family if family == "qwen3_next" else f"{family}_text")
assert admission._mamba_scan_backend(model, config) == "torch"
monkeypatch.setattr(module, "torch_chunk_gated_delta_rule", Mock())
message = "unmodelled.*recurrent scan.*OBLITERATUS_PHASE_ADMISSION=off"
with pytest.raises(ValueError, match=message):
admission._mamba_scan_backend(model, config)
@pytest.mark.parametrize("family", ["lfm2", "recurrent_gemma", "rwkv"])
@pytest.mark.usefixtures("cpu_threads")
def test_other_sequential_recurrent_fallbacks_cover_real_tiny_forward_peaks(family):
from transformers import AutoModelForCausalLM, Lfm2Config, RecurrentGemmaConfig, RwkvConfig
small = family == "rwkv"
common = dict(vocab_size=32, hidden_size=32 if small else 64,
intermediate_size=48 if small else 96, num_hidden_layers=2)
configs = {
"lfm2": lambda: Lfm2Config(
**common, num_attention_heads=4, num_key_value_heads=2,
layer_types=["conv", "full_attention"],
),
"recurrent_gemma": lambda: RecurrentGemmaConfig(
**common, num_attention_heads=4, block_types=["recurrent", "attention"],
),
"rwkv": lambda: RwkvConfig(**common, attention_hidden_size=32, context_length=32),
}
config = configs[family]()
model = AutoModelForCausalLM.from_config(config).eval()
handle = ModelHandle(model, Mock(), config, "random-local", "causal_lm")
batch, tokens = (1, 8) if small else (2, 64)
inputs = torch.zeros(batch, tokens, dtype=torch.long)
report = admission.model_phase_demand(
handle, "probe", forward_workloads=[
admission.ForwardWorkload(batch, tokens, attention_mask=False),
],
)
with torch.inference_mode(), cpu_memory_profile() as profile:
result = model(inputs)
assert torch.isfinite(result.logits).all()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= report.devices["cpu"].additional_bytes <= 3 * peak