mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Resolve attention compute dtype per projection, account for Mamba2 torch-fallback scan intermediates and the LoRA construct/merge path, and flag Bayesian optimization as unmodelled before optimizer work. Refs #245 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
449 lines
15 KiB
Python
449 lines
15 KiB
Python
"""Configuration-only memory bounds for the supported decoder architecture families."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
from functools import wraps
|
|
import sys
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from transformers.models.auto.configuration_auto import CONFIG_MAPPING
|
|
|
|
from obliteratus.phase_admission import transformer_demands
|
|
|
|
|
|
_FAMILIES = (
|
|
"gpt2",
|
|
"gpt_neo",
|
|
"gpt_neox",
|
|
"llama",
|
|
"mistral",
|
|
"mistral3",
|
|
"mistral4",
|
|
"gemma",
|
|
"gemma2",
|
|
"gemma3",
|
|
"gemma3n",
|
|
"phi",
|
|
"phi3",
|
|
"qwen2",
|
|
"qwen2_moe",
|
|
"qwen3",
|
|
"qwen3_moe",
|
|
"qwen3_next",
|
|
"qwen3_5",
|
|
"qwen3_5_moe",
|
|
"qwen3_5_text",
|
|
"minimax_m2",
|
|
"minicpm3",
|
|
"glm_moe_dsa",
|
|
"deepseek_v2",
|
|
"deepseek_v3",
|
|
"deepseek_v32",
|
|
"glm4",
|
|
"glm4_moe",
|
|
"glm4_moe_lite",
|
|
"falcon",
|
|
"opt",
|
|
"bloom",
|
|
"mpt",
|
|
"stablelm",
|
|
"glm",
|
|
"gpt_oss",
|
|
"smollm3",
|
|
"cohere",
|
|
"cohere2",
|
|
"olmo",
|
|
"olmo2",
|
|
"olmo3",
|
|
"olmoe",
|
|
"granite",
|
|
"granitemoe",
|
|
"granitemoeshared",
|
|
"granitemoehybrid",
|
|
"mixtral",
|
|
"dbrx",
|
|
"llama4",
|
|
"nemotron_h",
|
|
"falcon_h1",
|
|
"jamba",
|
|
"lfm2",
|
|
"lfm2_moe",
|
|
"mamba",
|
|
"mamba2",
|
|
"zamba",
|
|
"zamba2",
|
|
"recurrent_gemma",
|
|
"rwkv",
|
|
)
|
|
|
|
|
|
def _config(family):
|
|
if family not in CONFIG_MAPPING:
|
|
pytest.skip(f"installed Transformers does not provide {family} configuration")
|
|
options = {}
|
|
if family.startswith("lfm2"):
|
|
options = {
|
|
"num_hidden_layers": 4,
|
|
"layer_types": ["conv", "full_attention", "conv", "full_attention"],
|
|
}
|
|
elif family == "deepseek_v2":
|
|
# HF's bare defaults leave routed top-k unset; real MoE checkpoints provide it.
|
|
options = {"num_experts_per_tok": 2}
|
|
return CONFIG_MAPPING[family](**options)
|
|
|
|
|
|
def _text(config):
|
|
return getattr(config, "text_config", None) or config
|
|
|
|
|
|
def _demand(config, *, tokens=32, placements=None, **options):
|
|
text = _text(config)
|
|
count = getattr(text, "num_hidden_layers", None)
|
|
if count is None:
|
|
count = getattr(text, "num_transformer_layers", None) or getattr(text, "num_layers")
|
|
return transformer_demands(
|
|
config,
|
|
placements or ["cpu"] * count,
|
|
batch_size=1,
|
|
tokens=tokens,
|
|
dtype_bytes=2,
|
|
resident_bytes={},
|
|
largest_tensor_bytes={},
|
|
output_device="cpu",
|
|
input_device="cpu",
|
|
**options,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("family", _FAMILIES)
|
|
def test_supported_family_configuration_has_finite_demand_without_loading_weights(family):
|
|
config = _config(family)
|
|
config_before = deepcopy(config.to_dict())
|
|
demand = _demand(config)["cpu"]
|
|
assert type(demand.additional_bytes) is int
|
|
assert demand.additional_bytes > 0
|
|
assert demand.activation_bytes > 0
|
|
assert demand.output_bytes > 0
|
|
assert config.to_dict() == config_before
|
|
|
|
|
|
def _tiny_geometry(**kwargs):
|
|
values = dict(
|
|
model_type="llama",
|
|
hidden_size=32,
|
|
intermediate_size=64,
|
|
vocab_size=64,
|
|
num_hidden_layers=2,
|
|
num_attention_heads=4,
|
|
num_key_value_heads=2,
|
|
_attn_implementation="sdpa",
|
|
)
|
|
values.update(kwargs)
|
|
return SimpleNamespace(**values)
|
|
|
|
|
|
def test_gemma3n_ffn_width_is_per_layer_and_embedded_inputs_are_accounted():
|
|
config = _tiny_geometry(
|
|
model_type="gemma3n_text",
|
|
intermediate_size=[64, 512],
|
|
hidden_size_per_layer_input=8,
|
|
layer_types=["sliding_attention", "full_attention"],
|
|
sliding_window=16,
|
|
)
|
|
placements = ["cuda:0", "cuda:1"]
|
|
report = _demand(config, placements=placements)
|
|
assert report["cuda:1"].activation_bytes > report["cuda:0"].activation_bytes
|
|
without_embedded = deepcopy(config)
|
|
without_embedded.hidden_size_per_layer_input = 0
|
|
assert (
|
|
report["cpu"].activation_bytes
|
|
> _demand(
|
|
without_embedded,
|
|
placements=placements,
|
|
)["cpu"].activation_bytes
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kind,window_name",
|
|
[
|
|
("sliding_attention", "sliding_window"),
|
|
("chunked_attention", "attention_chunk_size"),
|
|
],
|
|
)
|
|
def test_local_attention_cache_is_bounded_by_configured_window(kind, window_name):
|
|
config = _tiny_geometry(layer_types=[kind, "full_attention"], **{window_name: 16})
|
|
demands = _demand(config, tokens=1, cache_tokens=64, placements=["cuda:0", "cuda:1"])
|
|
assert demands["cuda:1"].kv_bytes == 4 * demands["cuda:0"].kv_bytes
|
|
assert demands["cuda:0"].kv_bytes > 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"backend", ["flash_attention_2", "flash_attention_3", "flex_attention"]
|
|
)
|
|
def test_fused_attention_does_not_charge_eager_quadratic_scores(backend):
|
|
config = _tiny_geometry(_attn_implementation=backend)
|
|
short = _demand(config, tokens=64)["cpu"]
|
|
long = _demand(config, tokens=128)["cpu"]
|
|
assert long.activation_bytes == 2 * short.activation_bytes
|
|
config._attn_implementation = "eager"
|
|
eager = _demand(config, tokens=128)["cpu"]
|
|
assert eager.activation_bytes > long.activation_bytes
|
|
|
|
|
|
def test_requested_attention_maps_add_quadratic_retention_even_with_sdpa_config():
|
|
config = _tiny_geometry()
|
|
normal = _demand(config, tokens=64)["cpu"]
|
|
requested = _demand(config, tokens=64, output_attentions=True)["cpu"]
|
|
assert requested.activation_bytes > normal.activation_bytes
|
|
|
|
|
|
def test_dbrx_nested_router_expert_count_and_top_k_are_modelled():
|
|
config = _config("dbrx")
|
|
first = _demand(config)["cpu"]
|
|
config.ffn_config.moe_top_k += 1
|
|
routed = _demand(config)["cpu"]
|
|
assert routed.activation_bytes > first.activation_bytes
|
|
config.ffn_config.moe_num_experts *= 2
|
|
routers = _demand(config)["cpu"]
|
|
assert routers.activation_bytes > routed.activation_bytes
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"family,attribute",
|
|
[
|
|
("granitemoehybrid", "shared_intermediate_size"),
|
|
("nemotron_h", "moe_shared_expert_intermediate_size"),
|
|
("qwen3_next", "shared_expert_intermediate_size"),
|
|
],
|
|
)
|
|
def test_shared_expert_aliases_contribute_workspace(family, attribute):
|
|
config = _config(family)
|
|
if family == "nemotron_h":
|
|
# Isolate FFN blocks: the real torch Mamba2 broadcast peak can dominate
|
|
# the whole hybrid even after a large increase in shared FFN width.
|
|
config.layer_types = ["moe"] * config.num_hidden_layers
|
|
first = _demand(config)["cpu"]
|
|
setattr(config, attribute, getattr(config, attribute) + 100000)
|
|
assert _demand(config)["cpu"].activation_bytes > first.activation_bytes
|
|
|
|
|
|
@pytest.mark.parametrize("family", ["granitemoehybrid", "nemotron_h", "jamba"])
|
|
def test_mamba_named_linear_attention_uses_its_ssm_state_geometry(family):
|
|
config = _config(family)
|
|
first = _demand(config)["cpu"]
|
|
attribute = "ssm_state_size" if family == "nemotron_h" else "mamba_d_state"
|
|
setattr(config, attribute, getattr(config, attribute) * 2)
|
|
assert _demand(config)["cpu"].kv_bytes > first.kv_bytes
|
|
|
|
|
|
def test_falcon_h1_combines_attention_kv_and_parallel_ssm_state():
|
|
config = _config("falcon_h1")
|
|
base = _demand(config)["cpu"]
|
|
config.mamba_d_state *= 2
|
|
assert _demand(config)["cpu"].kv_bytes > base.kv_bytes
|
|
config.num_key_value_heads *= 2
|
|
assert _demand(config)["cpu"].kv_bytes > base.kv_bytes
|
|
assert _demand(config, output_attentions=True)["cpu"].activation_bytes > base.activation_bytes
|
|
|
|
|
|
@pytest.mark.parametrize("family", ["lfm2", "lfm2_moe"])
|
|
def test_lfm_short_convolution_history_is_not_treated_as_attention(family):
|
|
config = _config(family)
|
|
demands = _demand(config, placements=["cuda:0", "cuda:1", "cuda:0", "cuda:1"])
|
|
config.conv_L_cache *= 2
|
|
larger = _demand(config, placements=["cuda:0", "cuda:1", "cuda:0", "cuda:1"])
|
|
assert larger["cuda:0"].kv_bytes == 2 * demands["cuda:0"].kv_bytes
|
|
assert larger["cuda:1"].kv_bytes == demands["cuda:1"].kv_bytes
|
|
|
|
|
|
def test_deepseek_mla_includes_expanded_key_and_value_dimensions():
|
|
config = _tiny_geometry(
|
|
model_type="deepseek_v3",
|
|
kv_lora_rank=16,
|
|
qk_nope_head_dim=64,
|
|
qk_rope_head_dim=32,
|
|
v_head_dim=128,
|
|
)
|
|
mla = _demand(config)["cpu"]
|
|
dense = _demand(_tiny_geometry())["cpu"]
|
|
assert mla.kv_bytes == config.num_hidden_layers * 32 * (16 + 32) * 2
|
|
assert mla.activation_bytes > dense.activation_bytes
|
|
|
|
|
|
def test_dsa_indexer_workspace_remains_quadratic_for_fused_attention():
|
|
config = _tiny_geometry(index_n_heads=4, index_head_dim=16)
|
|
small = _demand(config, tokens=32)["cpu"].activation_bytes
|
|
large = _demand(config, tokens=64)["cpu"].activation_bytes
|
|
assert large > 2 * small
|
|
|
|
|
|
def test_openelm_per_layer_query_and_kv_head_aliases():
|
|
config = SimpleNamespace(
|
|
model_type="openelm",
|
|
model_dim=32,
|
|
num_transformer_layers=2,
|
|
vocab_size=64,
|
|
num_query_heads=[2, 4],
|
|
num_kv_heads=[1, 2],
|
|
head_dim=16,
|
|
)
|
|
demands = _demand(config, placements=["cuda:0", "cuda:1"])
|
|
assert demands["cuda:1"].kv_bytes == 2 * demands["cuda:0"].kv_bytes
|
|
|
|
|
|
def test_dictionary_composite_config_uses_text_dimensions():
|
|
text = vars(_tiny_geometry())
|
|
report = transformer_demands(
|
|
{"model_type": "multimodal", "text_config": text},
|
|
["cpu", "cpu"],
|
|
batch_size=1,
|
|
tokens=8,
|
|
dtype_bytes=2,
|
|
resident_bytes={},
|
|
largest_tensor_bytes={},
|
|
output_device="cpu",
|
|
)
|
|
assert report["cpu"].additional_bytes > 0
|
|
|
|
|
|
def test_unknown_recurrent_geometry_is_explicitly_rejected():
|
|
config = _tiny_geometry(model_type="unpublished_recurrent", layer_types=["recurrent"] * 2)
|
|
with pytest.raises(
|
|
ValueError, match="unmodelled recurrent state geometry.*unpublished_recurrent"
|
|
):
|
|
_demand(config)
|
|
|
|
|
|
def test_missing_lfm_moe_layer_schedule_is_explicitly_rejected():
|
|
config = _tiny_geometry(model_type="lfm2_moe", conv_L_cache=3)
|
|
with pytest.raises(ValueError, match="lfm2_moe requires explicit recurrent layer types"):
|
|
_demand(config)
|
|
|
|
|
|
def test_unknown_family_with_state_geometry_cannot_silently_become_dense_attention():
|
|
config = _tiny_geometry(model_type="custom_decoder", state_size=16)
|
|
with pytest.raises(ValueError, match="custom_decoder requires explicit recurrent layer types"):
|
|
_demand(config)
|
|
|
|
|
|
def test_missing_routed_top_k_fails_closed():
|
|
config = _tiny_geometry(num_experts=8)
|
|
with pytest.raises(ValueError, match="num_experts_per_tok"):
|
|
_demand(config)
|
|
|
|
|
|
def test_logits_copy_count_is_explicit_and_does_not_change_other_demands():
|
|
config = _tiny_geometry()
|
|
one = _demand(config, logits_copies=1)["cpu"]
|
|
default = _demand(config)["cpu"]
|
|
assert one.output_bytes * 3 == default.output_bytes
|
|
assert one.activation_bytes == default.activation_bytes
|
|
assert one.kv_bytes == default.kv_bytes
|
|
|
|
|
|
@pytest.mark.parametrize("family", ["mamba", "jamba", "zamba"])
|
|
@pytest.mark.parametrize("device", ["cpu", "cuda:0"])
|
|
def test_mamba1_selective_scan_fallback_bounds_full_sequence_state_arrays(family, device):
|
|
config = _tiny_geometry(
|
|
model_type=family,
|
|
layer_types=["mamba"] * 2,
|
|
mamba_d_state=16,
|
|
mamba_d_conv=4,
|
|
mamba_expand=2,
|
|
use_mamba_kernels=True,
|
|
)
|
|
tokens = 64
|
|
demand = _demand(config, tokens=tokens, placements=[device] * 2)[device]
|
|
# The CUDA preference cannot prove an installed/selected fused implementation.
|
|
# Both CPU and unverified CUDA must cover discrete_A, discrete_B and deltaB_u.
|
|
sequence_state_bytes = (
|
|
tokens * (config.mamba_expand * config.hidden_size) * config.mamba_d_state * 4
|
|
)
|
|
assert demand.activation_bytes >= 3 * sequence_state_bytes
|
|
config.use_mambapy = True
|
|
assert _demand(config, tokens=tokens, placements=[device] * 2)[device].activation_bytes >= (
|
|
6 * sequence_state_bytes
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("backend", ["torch", "mamba_ssm", "unknown"])
|
|
def test_mamba_scan_backend_follows_frozen_callable_not_config_preference(monkeypatch, backend):
|
|
from obliteratus.phase_admission import _mamba_scan_backend
|
|
|
|
config = _tiny_geometry(model_type="mamba", layer_types=["mamba"] * 2,
|
|
mamba_d_state=16, mamba_d_conv=4, mamba_expand=2)
|
|
source = "transformers.models.mamba.modeling_mamba"
|
|
|
|
def mamba_selective_scan():
|
|
pass
|
|
|
|
mamba_selective_scan.__module__ = source
|
|
implementation = (mamba_selective_scan if backend == "torch" else lambda: None)
|
|
if backend != "torch":
|
|
implementation.__module__ = "mamba_ssm.ops.selective_scan_interface" if (
|
|
backend == "mamba_ssm"
|
|
) else "custom_kernel"
|
|
|
|
@wraps(mamba_selective_scan)
|
|
def wrapped(*args, **kwargs):
|
|
return implementation(*args, **kwargs)
|
|
|
|
mixer = type("MambaMixer", (), {"__module__": source})()
|
|
model = SimpleNamespace(modules=lambda: [mixer])
|
|
monkeypatch.setitem(sys.modules, source, SimpleNamespace(mamba_selective_scan=wrapped))
|
|
if backend == "unknown":
|
|
with pytest.raises(ValueError, match="cannot identify mamba selective-scan backend"):
|
|
_mamba_scan_backend(model, config)
|
|
return
|
|
assert _mamba_scan_backend(model, config) == backend
|
|
actual = _demand(config, tokens=512, mamba_scan_backend=backend)["cpu"]
|
|
fallback = _demand(config, tokens=512, mamba_scan_backend="torch")["cpu"]
|
|
if backend == "mamba_ssm":
|
|
sequence_state = 512 * 2 * config.hidden_size * 16 * 4
|
|
assert fallback.activation_bytes - actual.activation_bytes >= 3 * sequence_state
|
|
else:
|
|
assert actual == fallback
|
|
|
|
|
|
@pytest.mark.parametrize("family", ["internlm2", "internlm3", "arctic", "grok"])
|
|
def test_remote_causal_config_schema_representatives_have_finite_demand(family):
|
|
# Remote implementations are not installed Transformers config classes;
|
|
# these exercise their decoder schema rather than claiming runtime coverage.
|
|
config = _tiny_geometry(model_type=family)
|
|
if family in {"arctic", "grok"}:
|
|
config.num_local_experts = 8
|
|
config.num_experts_per_tok = 2
|
|
if family == "arctic":
|
|
config.enable_residual_moe = True
|
|
assert _demand(config)["cpu"].additional_bytes > 0
|
|
|
|
|
|
def test_chatglm_remote_schema_aliases_and_multi_query_geometry():
|
|
config = SimpleNamespace(
|
|
model_type="chatglm",
|
|
num_layers=2,
|
|
hidden_size=32,
|
|
padded_vocab_size=64,
|
|
ffn_hidden_size=96,
|
|
num_attention_heads=4,
|
|
kv_channels=8,
|
|
multi_query_attention=True,
|
|
multi_query_group_num=2,
|
|
_attn_implementation="sdpa",
|
|
)
|
|
grouped = _demand(config)["cpu"]
|
|
config.multi_query_attention = False
|
|
ordinary = _demand(config)["cpu"]
|
|
assert ordinary.kv_bytes == 2 * grouped.kv_bytes
|
|
assert grouped.output_bytes > 0
|
|
|
|
|
|
def test_residual_moe_dense_branch_is_included():
|
|
config = _tiny_geometry(num_local_experts=8, num_experts_per_tok=2)
|
|
normal = _demand(config)["cpu"]
|
|
config.enable_residual_moe = True
|
|
assert _demand(config)["cpu"].activation_bytes > normal.activation_bytes
|