Files
OBLITERATUS/tests/test_deepseek_v41_memory.py

259 lines
12 KiB
Python

"""V4.1 admission geometry; full checkpoint shapes require no tensor allocation."""
from types import SimpleNamespace
import pytest
import torch
from obliteratus import phase_admission as admission
from obliteratus.analysis.deepseek_v41_memory import engram_table_metadata, forward_geometry
def config(**changes):
values = dict(
model_type="deepseek_v41_text", num_hidden_layers=40, hidden_size=5120,
vocab_size=129280, num_attention_heads=64, num_key_value_heads=1, head_dim=512,
q_lora_rank=1280, qk_rope_head_dim=64, o_groups=8, o_lora_rank=1024,
sliding_window=128, hc_mult=4, index_n_heads=32, index_head_dim=128,
index_topk=512, compress_ratios=[0, 0] + [2] * 18 + [1] * 20,
kv_source_layer_ids=[2, 8, 14, 20],
index_source_layer_ids=[2, 8, 14, 20, 24, 28, 32, 36],
candidate_source_layer_id=20, candidate_block_size=8, candidate_topk_blocks=2048,
engram_layer_ids=[1, 14], engram_num_embeddings=[100000000, 100000000],
engram_n_heads=8, engram_head_dim=128, engram_max_ngram_size=3,
n_routed_experts=384, num_experts_per_tok=6, moe_intermediate_size=2304,
n_shared_experts=1, _attn_implementation="eager",
)
values.update(changes)
values.setdefault("layer_types", [
"shared_compressed_attention" if i in values["kv_source_layer_ids"] else "sliding_attention"
for i in range(values["num_hidden_layers"])
])
return SimpleNamespace(**values)
def estimate(cfg=None, placement=None, **changes):
cfg = cfg or config()
options = dict(batch_size=1, tokens=256, dtype_bytes=2, resident_bytes={},
largest_tensor_bytes={}, input_device="cuda:0", output_device="cuda:7",
use_cache=False)
options.update(changes)
return admission.transformer_demands(
cfg, placement or [f"cuda:{i // 5}" for i in range(cfg.num_hidden_layers)], **options,
)
def small_config(**changes):
values = dict(num_hidden_layers=4, hidden_size=128, num_attention_heads=4, head_dim=64,
q_lora_rank=32, qk_rope_head_dim=16, o_groups=2, o_lora_rank=32,
hc_mult=2, index_n_heads=2, index_head_dim=32, index_topk=8,
compress_ratios=[0, 2, 2, 2], kv_source_layer_ids=[1],
index_source_layer_ids=[1, 3], candidate_source_layer_id=-1,
engram_layer_ids=[], engram_num_embeddings=[],
n_routed_experts=4, num_experts_per_tok=2, moe_intermediate_size=64)
values.update(changes)
return config(**values)
def geometry(cfg=None, placement=None, **changes):
options = dict(batch_size=1, tokens=16, cache_tokens=1024, dtype_bytes=2,
attention_dtype_bytes=[2] * 4, use_cache=True, input_device="cuda:0")
options.update(changes)
return forward_geometry(cfg or small_config(), placement or [f"cuda:{i}" for i in range(4)], **options)
def test_cache_stores_single_source_kv_and_double_consumer_kv():
demand = geometry()
raw = (16 + 127) * 64 * 2
compressed = 512 * (64 + 32) * 2
partial_and_counters = (64 * 8 + 8) + (16 + 4) * 8
assert demand.cache_bytes == {
"cuda:0": 2 * raw, "cuda:1": raw + compressed + partial_and_counters,
"cuda:2": 2 * raw, "cuda:3": 2 * raw,
}
# Reindex layer 3 owns neither compressed KV nor a second index-key cache.
no_cache = geometry(use_cache=False)
assert not any(no_cache.cache_bytes.values())
assert no_cache.retained_bytes == {"cuda:1": 8 * (64 + 32) * 2}
def test_prefill_keeps_full_sliding_cache_backing():
demand = geometry(tokens=256, cache_tokens=256)
assert demand.cache_bytes["cuda:0"] == 2 * 256 * 64 * 2
assert geometry(small_config(sliding_window=1)).cache_bytes["cuda:0"] == 2 * 1024 * 64 * 2
def test_reindex_transfer_and_split_layer_workspace():
local = geometry(placement=["cuda:0"] * 4)
remote = geometry(placement=["cuda:0", "cuda:0", "cuda:0", "cuda:1"])
assert (remote.layer_workspace[3]["cuda:1"] - local.layer_workspace[3]["cuda:0"]
== 512 * (64 + 32) * 2)
split = geometry(placement=["cuda:0", ("cuda:1", "cuda:2"), "cuda:3", "cuda:3"])
assert split.cache_bytes["cuda:1"] == split.cache_bytes["cuda:2"]
assert split.layer_workspace[1]["cuda:1"] == split.layer_workspace[1]["cuda:2"]
assert split.layer_workspace[2]["cuda:3"] > local.layer_workspace[2]["cuda:0"]
def test_mixed_layer_dtypes_keep_gather_and_attention_cast_alive():
uniform = geometry()
mixed = geometry(attention_dtype_bytes=[2, 4, 2, 2])
assert (mixed.layer_workspace[2]["cuda:2"] - uniform.layer_workspace[2]["cuda:2"]
== 512 * 64 * 2 + 16 * 8 * 64 * 4)
def test_engram_rows_stay_on_entry_and_gather_on_actual_table_device():
cfg = small_config(engram_layer_ids=[1, 3], engram_num_embeddings=[100, 100])
demand = geometry(cfg, engram_tables={1: ("cpu", 2, False), 3: ("cuda:2", 4, True)})
plain = geometry()
elements = 16 * 16 * 128
assert demand.retained_bytes["cuda:0"] >= elements * (2 + 4)
assert demand.workspace_bytes["cpu"] == elements * 2 + 16 * 16 * 8
assert demand.workspace_bytes["cuda:2"] == elements * 4 * 3 + 16 * 16 * 8
assert demand.cache_bytes["cuda:0"] - plain.cache_bytes["cuda:0"] == (16 + 2) * 8
assert demand.layer_workspace[3]["cuda:3"] > plain.layer_workspace[3]["cuda:3"] + elements * 4
total = estimate(cfg, ["cuda:0"] * 4, tokens=16, engram_tables={1: ("cpu", 2, False), 3: ("cpu", 2, False)})
assert total["cpu"].activation_bytes > 0
def test_table_metadata_uses_row_dequantization_dtype():
fp8_table = torch.nn.Module()
fp8_table.weight = torch.nn.Parameter(torch.zeros(2, 32, dtype=torch.float8_e4m3fn))
tables = torch.nn.ModuleDict({
"1": torch.nn.Embedding(2, 32, dtype=torch.bfloat16),
"3": fp8_table,
})
model = SimpleNamespace(model=SimpleNamespace(engram_tables=tables))
assert engram_table_metadata(model) == {1: ("cpu", 2, False), 3: ("cpu", 4, True)}
def test_batched_experts_charge_per_token_weight_gathers():
grouped = geometry(small_config(_experts_implementation="grouped_mm"))
batched = geometry(small_config(_experts_implementation="batched_mm"))
assert (batched.layer_workspace[0]["cuda:0"] - grouped.layer_workspace[0]["cuda:0"]
== 3 * 16 * 2 * 128 * 64 * 4)
with pytest.raises(ValueError, match="expert backends"):
geometry(small_config(_experts_implementation="custom_kernel"))
def test_specialized_geometry_keeps_generic_moe_logits_and_staging():
cfg = small_config()
base = estimate(cfg, ["cuda:0"] * 4, tokens=16)
staged = estimate(cfg, ["cuda:0"] * 4, tokens=16, largest_tensor_bytes={"cuda:0": 1234})
assert staged["cuda:0"].additional_bytes - base["cuda:0"].additional_bytes == 1234
wider = estimate(small_config(moe_intermediate_size=128), ["cuda:0"] * 4, tokens=16)
assert wider["cuda:0"].activation_bytes > base["cuda:0"].activation_bytes
assert wider["cuda:7"].output_bytes == 3 * 16 * 129280 * 4
def test_query_length_and_sparse_selection_bound_decode_workspace():
decode = geometry(tokens=1, cache_tokens=4096)
prefill = geometry(tokens=4096, cache_tokens=4096)
assert prefill.layer_workspace[2]["cuda:2"] > 100 * decode.layer_workspace[2]["cuda:2"]
capped = geometry(small_config(index_topk=4))
wider = geometry(small_config(index_topk=8))
assert capped.layer_workspace[2]["cuda:2"] < wider.layer_workspace[2]["cuda:2"]
def test_later_reindex_source_can_publish_candidates():
demand = geometry(small_config(candidate_source_layer_id=3))
plain = geometry()
assert demand.layer_workspace[2] == plain.layer_workspace[2]
assert demand.layer_workspace[3]["cuda:3"] > plain.layer_workspace[3]["cuda:3"]
def test_optional_hidden_and_attention_outputs_are_retained():
cfg = small_config()
base = estimate(cfg, ["cuda:0"] * 4, tokens=16)
hidden = estimate(cfg, ["cuda:0"] * 4, tokens=16, output_hidden_states=True)
assert hidden["cuda:0"].activation_bytes - base["cuda:0"].activation_bytes == 5 * 16 * 2 * 128 * 2
attention = geometry(output_attentions=True)
assert attention.retained_bytes["cuda:0"] == 4 * 16 * (143 + 1) * 2
def test_realized_dtype_contract_checks_csa_and_single_kv_projection():
cfg = small_config()
model = torch.nn.Module()
model.model = torch.nn.Module()
model.model.embed_tokens = torch.nn.Embedding(16, 128, dtype=torch.bfloat16)
model.model.layers = torch.nn.ModuleList()
for _ in range(4):
layer = torch.nn.Module()
layer.self_attn = torch.nn.Module()
layer.self_attn.q_a_proj = torch.nn.Linear(128, 32, bias=False, dtype=torch.bfloat16)
layer.self_attn.q_b_proj = torch.nn.Linear(32, 256, bias=False, dtype=torch.bfloat16)
layer.self_attn.kv_proj = torch.nn.Linear(128, 64, bias=False, dtype=torch.bfloat16)
model.model.layers.append(layer)
model.get_input_embeddings = lambda: model.model.embed_tokens
handle = SimpleNamespace(model=model, architecture="llama", num_layers=4)
assert admission._attention_compute_dtypes(handle, cfg) == [torch.bfloat16] * 4
model.model.layers[1].self_attn.kv_proj.float()
with pytest.raises(ValueError, match="layer 1.*projection output dtypes"):
admission._attention_compute_dtypes(handle, cfg)
@pytest.mark.parametrize("changes,match", [
({"cache_tokens": 1}, "shorter than"),
({"attention_dtype_bytes": [1] * 4}, "projections"),
({"use_cache": "yes"}, "boolean cache"),
({"batch_size": 0}, "positive integers"),
({"engram_tables": {1: ("cpu", 2, False)}}, "describe each table"),
])
def test_invalid_forward_contracts_raise(changes, match):
with pytest.raises(ValueError, match=match):
geometry(**changes)
def test_full_geometry_admits_csa_without_surrogate_layer_types():
cfg = config()
before = list(cfg.layer_types)
demands = estimate(cfg)
assert cfg.layer_types == before
assert set(demands) == {f"cuda:{i}" for i in range(8)}
assert all(value.activation_bytes > 0 for value in demands.values())
assert all(value.kv_bytes == 0 for value in demands.values())
assert demands["cuda:0"].output_bytes == 3 * 256 * 129280 * 4
@pytest.mark.parametrize("changes,match", [
({"kv_source_layer_ids": [8, 2, 14, 20]}, "sorted unique"),
({"index_source_layer_ids": [8, 14, 20]}, "every KV source"),
({"compress_ratios": [1] * 40}, "preceding CSA source"),
({"compress_ratios": [0, 0] + [2] * 19 + [1] * 19}, "source ratio"),
({"layer_types": ["sliding_attention"] * 40}, "source schedule"),
({"num_key_value_heads": 2}, "single shared KV head"),
({"engram_num_embeddings": [100]}, "table metadata"),
({"_attn_implementation": "sdpa"}, "attention backend"),
])
def test_invalid_topology_is_not_relabelled_as_generic_attention(changes, match):
with pytest.raises(ValueError, match=match):
estimate(config(**changes))
def test_strict_admission_keeps_host_rollback_and_exposes_gpu_shortfall(monkeypatch):
cfg = config()
handle = SimpleNamespace(model=SimpleNamespace(config=cfg), config=cfg)
placement = [(f"cuda:{i // 5}",) for i in range(40)]
monkeypatch.setattr(admission, "_model_inventory", lambda _: (
{"cpu": 400 * admission.GIB, "cuda:0": 128 * admission.GIB}, {}, {},
placement, [10] * 40, "cuda:0", "cuda:7", 2,
))
monkeypatch.setattr(admission, "_attention_compute_dtypes", lambda *_: [torch.bfloat16] * 40)
monkeypatch.setattr(admission, "_edit_workspace", lambda *_: {})
workload = admission.ForwardWorkload(1, 256, use_cache=False)
demand = admission.model_phase_demand(
handle, "excise", forward_workloads=[workload], host_workspace_bytes=12 * admission.GIB,
rollback_layers=2, mode="strict",
)
assert demand.devices["cpu"].activation_bytes >= 12 * admission.GIB + 20
assert demand.components and all(c.bytes is not None for c in demand.components)
assert all(c.confidence == "estimated" for c in demand.components)
with pytest.raises(admission.PhaseAdmissionError) as caught:
admission.admit_phase(
demand.phase, demand.devices, mode="strict", safety_reserve_bytes=0,
observer=lambda device: admission.MemoryObservation(
device, 1 if device == "cuda:0" else 100 * admission.GIB, 100 * admission.GIB,
),
)
assert caught.value.device == "cuda:0"
assert caught.value.shortfall_bytes > 0