"""Offline differential matrix: metadata shapes and main-style, independent budgets. The matrix does not infer a budget from admission's answer. Tiny stored weights, short unpadded forwards, sequential edits, and retained rows define an independent main-style budget below. Quantizers are metadata contracts, never CUDA kernels; all parameter storage is fake, so this covers placement without CUDA/downloads. The table supports explicit invalid-combination skip reasons; none are needed for these metadata contracts. This does not certify optional quantizer kernels for every architecture or imply their dependencies are installed. """ from __future__ import annotations from copy import deepcopy from functools import lru_cache from itertools import product from types import SimpleNamespace import pytest import torch from accelerate.hooks import AlignDevicesHook from torch._subclasses.fake_tensor import FakeTensorMode from transformers import AutoModelForCausalLM from transformers.models.auto.configuration_auto import CONFIG_MAPPING from obliteratus.models.loader import ModelHandle from obliteratus.phase_admission import ( DistillationWorkload, ForwardWorkload, GIB, MemoryObservation, PhaseAdmissionError, admit_model_load, admit_phase, model_phase_demand, ) from obliteratus.strategies.utils import get_layer_modules _ARCHITECTURES = ( "llama", "qwen2", "mixtral", "qwen2_moe", "qwen3_moe", "qwen3_next", "mamba", "mamba2", "falcon_old", "falcon_new", ) _QUANTIZATIONS = ("none", "bnb4", "bnb8", "gptq", "awq", "fp8") _PLACEMENTS = ("single_gpu", "multi_gpu", "split_layer", "cpu_offload", "disk_offload") # These are all extraction methods accepted by DistillationWorkload; basic and # leace are aliases, included to protect public callers as well as pipeline use. _DISTILL_METHODS = ( "basic", "diff_means", "svd", "whitened_svd", "fisher_lda", "leace", "leace_exact", "wasserstein", ) # Every listed combination is meaningful as metadata: lack of a local quantizer # kernel is precisely an estimate-only case, not an invalid test combination. # Future genuinely invalid combinations go here with their explicit reason. _INVALID_COMBINATIONS: dict[tuple[str, str, str, str], str] = {} _MATRIX = [ pytest.param( *case, id="-".join(case), marks=pytest.mark.skip(reason=_INVALID_COMBINATIONS[case]) if case in _INVALID_COMBINATIONS else (), ) for case in product(_ARCHITECTURES, _QUANTIZATIONS, _PLACEMENTS, _DISTILL_METHODS) ] # Keep the full Cartesian product above as the coverage contract, but collect # one item per immutable handle. Explicit invalid combinations still collect as # individually skipped items with their own IDs and reasons. _GROUPED_MATRIX = [ pytest.param(*key, methods, id="-".join(key)) for key in product(_ARCHITECTURES, _QUANTIZATIONS, _PLACEMENTS) if (methods := tuple(method for method in _DISTILL_METHODS if (*key, method) not in _INVALID_COMBINATIONS)) ] + [ pytest.param(*case[:3], (case[3],), id="-".join(case), marks=case_param.marks) for case_param in _MATRIX if (case := case_param.values) in _INVALID_COMBINATIONS ] _PHASES = ("baseline", "probe", "distill", "validate", "excise", "verify", "rebirth") def _tiny_config(architecture, layers=2): family = "falcon" if architecture.startswith("falcon_") else architecture kwargs = dict( vocab_size=64, hidden_size=32, intermediate_size=64, num_hidden_layers=layers, num_attention_heads=4, num_key_value_heads=2, max_position_embeddings=128, tie_word_embeddings=False, dtype="bfloat16", use_cache=True, ) if family in {"mixtral", "qwen2_moe", "qwen3_moe", "qwen3_next"}: kwargs.update( # Two routed experts plus one unrouted expert retain sparse routing # while minimizing repeated metadata inventory traversal. num_local_experts=3, num_experts=3, num_experts_per_tok=2, moe_intermediate_size=32, shared_expert_intermediate_size=32, ) if family == "qwen3_next": kwargs.update( layer_types=["linear_attention", "full_attention"], linear_num_key_heads=2, linear_num_value_heads=2, linear_key_head_dim=8, linear_value_head_dim=8, linear_conv_kernel_dim=4, ) elif family == "mamba": kwargs.update(state_size=8, expand=2, conv_kernel=4) elif family == "mamba2": kwargs.update( state_size=8, expand=2, num_heads=4, head_dim=16, n_groups=1, chunk_size=16, conv_kernel=4, ) elif family == "falcon": kwargs.update( multi_query=architecture == "falcon_old", new_decoder_architecture=architecture == "falcon_new", num_kv_heads=2, ) config = CONFIG_MAPPING[family](**kwargs) config._attn_implementation = "sdpa" if family not in {"mamba", "mamba2"} else "eager" return config @lru_cache(maxsize=None) def _base_model(architecture, layers=2): config = _tiny_config(architecture, layers) with torch.device("meta"): return AutoModelForCausalLM.from_config(config, dtype=torch.bfloat16).eval() class _QuantizerMetadata: """Stored-size contract at the same boundary as the optional HF quantizer.""" pre_quantized = True def __init__(self, quantization): self.quantization_config = SimpleNamespace( quant_method={"bnb4": "bitsandbytes", "bnb8": "bitsandbytes"}.get( quantization, quantization, ), load_in_4bit=quantization == "bnb4", load_in_8bit=quantization == "bnb8", dequantize=False, ) def param_element_size(self, model, name, parameter): return parameter.element_size() def param_needs_quantization(self, model, name): return False def update_weight_conversions(self, mappings): return mappings class Linear4bit(torch.nn.Module): pass class Linear8bitLt(torch.nn.Module): pass class FP8Linear(torch.nn.Module): pass def _quantize_metadata(model, quantization): if quantization == "none": return bits = 4 if quantization in {"bnb4", "gptq", "awq"} else 8 contract = {"bnb4": Linear4bit, "bnb8": Linear8bitLt, "fp8": FP8Linear}.get( quantization, torch.nn.Module, ) for name, module in list(model.named_modules()): if not isinstance(module, torch.nn.Linear) or name.endswith("lm_head"): continue packed = contract() packed.in_features, packed.out_features = module.in_features, module.out_features packed.weight = torch.nn.Parameter( torch.empty( module.out_features, (module.in_features * bits + 7) // 8, dtype=torch.uint8, device="meta", ), requires_grad=False, ) packed.weight.quant_state = SimpleNamespace( shape=tuple(module.weight.shape), dtype=torch.bfloat16, ) if module.bias is not None: packed.bias = module.bias parent, _, local = name.rpartition(".") setattr(model.get_submodule(parent), local, packed) model.hf_quantizer = _QuantizerMetadata(quantization) model.config.quantization_config = { "quant_method": model.hf_quantizer.quantization_config.quant_method, "load_in_4bit": quantization == "bnb4", "load_in_8bit": quantization == "bnb8", } def _put_fake_tensors(module, device): for owner in module.modules(): for name, value in list(owner._parameters.items()): if value is None: continue replacement = torch.nn.Parameter( torch.empty(value.shape, dtype=value.dtype, device=device), requires_grad=value.requires_grad, ) if hasattr(value, "quant_state"): replacement.quant_state = value.quant_state owner._parameters[name] = replacement for name, value in list(owner._buffers.items()): if value is not None: owner._buffers[name] = torch.empty(value.shape, dtype=value.dtype, device=device) @lru_cache(maxsize=None) def _matrix_template(architecture, quantization, layers): model = _base_model(architecture, layers) if quantization != "none": model = deepcopy(model) _quantize_metadata(model, quantization) # Parameter.__deepcopy__ does not preserve custom tensor attributes. Keep # the immutable logical-shape contract explicitly when cloning templates. quant_states = {id(parameter): parameter.quant_state for parameter in model.parameters() if hasattr(parameter, "quant_state")} return model, quant_states @pytest.fixture(scope="module", autouse=True) def release_matrix_templates(): yield _matrix_template.cache_clear() _base_model.cache_clear() def _matrix_handle(architecture, quantization, placement, *, minimal=False): # One decoder still distinguishes whole-device and intra-layer placement; # multi-device/offload and mixed Qwen3-Next blocks require two. Families # without a registered layer path also need two for the navigation fallback # to recognize their ModuleList. Exact-number regressions keep two by default. single_layer = (minimal and placement in {"single_gpu", "split_layer"} and architecture in {"llama", "qwen2", "qwen3_moe", "falcon_old", "falcon_new"}) template, quant_states = _matrix_template(architecture, quantization, 1 if single_layer else 2) memo = {} model = deepcopy(template, memo) for identity, quant_state in quant_states.items(): memo[identity].quant_state = quant_state handle = ModelHandle(model, None, model.config, "offline-matrix", "causal_lm") layers = get_layer_modules(handle) paths = {id(module): name for name, module in model.named_modules()} device_map = {"": "cuda:0"} with FakeTensorMode(): _put_fake_tensors(model, "cuda:0") if placement == "multi_gpu": _put_fake_tensors(layers[1], "cuda:1") device_map[paths[id(layers[1])]] = "cuda:1" elif placement == "split_layer": projection = next(module for module in layers[0].modules() if hasattr(module, "in_features")) _put_fake_tensors(projection, "cuda:1") projection._hf_hook = AlignDevicesHook(execution_device="cuda:1", io_same_device=True) device_map[paths[id(projection)]] = "cuda:1" elif placement in {"cpu_offload", "disk_offload"}: layer = layers[1] weights = { name: torch.empty_like(value, device="cpu") for name, value in (*layer.named_parameters(), *layer.named_buffers()) } if placement == "cpu_offload" else {} _put_fake_tensors(layer, "meta") layer._hf_hook = AlignDevicesHook( execution_device="cuda:0", offload=True, place_submodules=True, weights_map=weights, ) device_map[paths[id(layer)]] = "cpu" if placement == "cpu_offload" else "disk" model.hf_device_map = device_map return handle def main_style_headroom(handle, phase): """Independent tiny-run budget, excluding already-resident model weights. Main keeps short B=2/T=16 forwards, FP32 direction rows and one edited matrix at a time. We give 8 MiB host and 32 MiB CUDA runtime headroom, including cuBLAS workspace and the 1 MiB edit allocator allowance, plus the stored payload for summon and export. This scales with actual checkpoint storage; it never reads an estimated demand or grants 100 GiB. """ stored = sum(tensor.numel() * tensor.element_size() for tensor in handle.model.parameters()) overhead = 8 * 1024**2 # Source shard mmap plus streamed target/conversion windows during load/save. if phase in {"summon", "rebirth"}: overhead += 3 * stored return {"cpu": overhead, "cuda:0": overhead + 24 * 1024**2, "cuda:1": overhead + 24 * 1024**2} def _observer(budget): return lambda device: MemoryObservation(device, budget[device], budget[device]) @pytest.mark.parametrize("architecture,quantization,placement,methods", _GROUPED_MATRIX) def test_default_admission_preserves_fitting_matrix( architecture, quantization, placement, methods, monkeypatch, ): combo_prefix = "-".join((architecture, quantization, placement)) failures = [] try: handle, budgets, reports = _matrix_shared_reports( architecture, quantization, placement, monkeypatch, ) except Exception as exc: failures.extend(f"{combo_prefix}-{method}: {type(exc).__name__}: {exc}" for method in methods) else: for method in methods: reports["distill"] = _matrix_phase_report( handle, "distill", budgets["baseline"], method=method, ) try: _assert_matrix_reports(reports, architecture, quantization, placement) except Exception as exc: failures.append(f"{combo_prefix}-{method}: {type(exc).__name__}: {exc}") assert not failures, "Admission matrix failures:\n" + "\n".join(failures) def _matrix_shared_reports(architecture, quantization, placement, monkeypatch): from obliteratus.models import _load_admission handle = _matrix_handle(architecture, quantization, placement, minimal=True) # Kernel/module replacement is the optional quantizer boundary; source # mapping, stored-size accounting, placement and admission remain real. monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *a, **k: handle.model) monkeypatch.setattr( _load_admission, "_make_quantizer", lambda *a, **k: getattr(handle.model, "hf_quantizer", None), ) budgets = {phase: main_style_headroom(handle, phase) for phase in ("summon", "baseline", "rebirth")} reports = {} try: reports["summon"] = admit_model_load( handle.config, device="cuda:0", dtype="bfloat16", device_map=handle.model.hf_device_map, local_files_only=True, observer=_observer(budgets["summon"]), safety_reserve_bytes=0, mode="default", ) except Exception as exc: reports["summon"] = exc # These phases do not consume a distillation workload. Estimate their # identical inputs once per handle, then check their real reports for every # method below. Only distill varies with the eight extraction methods. for phase in _PHASES: if phase != "distill": reports[phase] = _matrix_phase_report( handle, phase, budgets.get(phase, budgets["baseline"]), ) return handle, budgets, reports def _matrix_phase_report(handle, phase, budget, *, method=None): try: demand = model_phase_demand( handle, phase, batch_size=2, tokens=16, retained_rows=20, distill_workload=(DistillationWorkload(method=method, n_directions=2) if method is not None else None), host_workspace_bytes=20 * 32 * 4 * 2 * 3 if phase == "validate" else 0, forward_workloads=([] if phase in {"distill", "validate", "excise", "rebirth"} else [ForwardWorkload(2, 16, logits_dtype_bytes=2)]), mode="default", ) return admit_phase( phase, demand, observer=_observer(budget), safety_reserve_bytes=0, mode="default", ) except Exception as exc: # Preserve failures from shared phases for every affected combination; # one bad method must not prevent the others from running. return exc def _assert_matrix_reports(reports, architecture, quantization, placement): load_report = reports["summon"] if isinstance(load_report, Exception): raise load_report assert load_report.phase == "summon" assert not load_report.bypassed assert sum(item.weight_bytes for item in load_report.demands.values()) > 0 if (placement == "split_layer" and architecture in {"llama", "qwen2"} and quantization == "none"): assert any(item.confidence == "calibrated" and item.device == "cuda:1" for item in load_report.components) for phase in _PHASES: report = reports[phase] if isinstance(report, Exception): raise report assert report.phase == phase assert not report.bypassed assert report.components if any(item.bytes is None for item in report.components): assert report.warnings if ( architecture in {"llama", "qwen2"} and quantization == "none" and placement in {"single_gpu", "multi_gpu"} and phase in {"baseline", "probe", "excise", "verify"} ): assert any(item.confidence == "calibrated" for item in report.components) if quantization != "none" or architecture not in {"llama", "qwen2"}: assert any(item.confidence == "estimated" for item in report.components) if placement == "split_layer" and phase in {"baseline", "probe", "verify"}: assert all(item.confidence == "estimated" for item in report.components) if (placement == "split_layer" and architecture in {"llama", "qwen2"} and quantization == "none" and phase == "excise"): assert any(item.confidence == "calibrated" for item in report.components) def test_matrix_covers_all_public_distillation_methods(): assert len(_MATRIX) == 2400 assert {case.values[-1] for case in _MATRIX} == set(_DISTILL_METHODS) grouped_cases = [(*group.values[:3], method) for group in _GROUPED_MATRIX for method in group.values[-1]] assert len(grouped_cases) == len(_MATRIX) assert set(grouped_cases) == {case.values for case in _MATRIX} @pytest.mark.parametrize("placement,expected", [ ("single_gpu", {"cuda:0": (45408, 16918528, 16384, 16384)}), ("multi_gpu", {"cuda:0": (26848, 16918528, 8192, 16384), "cuda:1": (18560, 16918528, 8192, 0)}), ]) def test_unsplit_cuda_forward_calibrated_numbers_are_unchanged(placement, expected, monkeypatch): # Exact pre-round-15 numbers: whole decoder layers on one/two GPUs. Fake # storage makes this a metadata non-regression, not a new GPU measurement. monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a, **kw: True) handle = _matrix_handle("llama", "none", placement) demand = model_phase_demand(handle, "probe", mode="default", forward_workloads=[ ForwardWorkload(1, 128, attention_mask=False, logits_dtype_bytes=2), ]) assert {d: (v.resident_bytes, v.activation_bytes, v.kv_bytes, v.output_bytes) for d, v in demand.devices.items() if d != "cpu"} == expected assert all(item.confidence == "calibrated" for item in demand.components) for device, (_, activation, kv, output) in expected.items(): assert sum(item.bytes for item in demand.components if item.device == device) == ( activation + kv + output ) def test_calibrated_qwen_basic_excise_still_rejects_measured_shortfall(): # Operator GPU evidence is 2.61 GiB additional peak on Qwen2.5-1.5B. # Only metadata is built here; the full-vocabulary head is the peak edit. from tests.test_phase_edit_admission import _matrix config = CONFIG_MAPPING["qwen2"]( vocab_size=151936, hidden_size=1536, intermediate_size=8960, num_hidden_layers=28, num_attention_heads=12, num_key_value_heads=2, dtype="bfloat16", tie_word_embeddings=False, ) config._attn_implementation = "sdpa" model = torch.nn.Module() model.config = config model.model = torch.nn.Module() model.model.layers = torch.nn.ModuleList() with FakeTensorMode(): model.model.embed_tokens = _matrix(151936, 1536, dtype=torch.bfloat16, device="cuda:0") for _ in range(28): layer = torch.nn.Module() layer.self_attn = torch.nn.Module() projections = (("q_proj", 1536), ("k_proj", 256), ("v_proj", 256), ("o_proj", 1536)) for name, rows in projections: setattr( layer.self_attn, name, _matrix(rows, 1536, dtype=torch.bfloat16, device="cuda:0"), ) model.model.layers.append(layer) model.lm_head = _matrix(151936, 1536, dtype=torch.bfloat16, device="cuda:0") model.get_input_embeddings = lambda: model.model.embed_tokens model.get_output_embeddings = lambda: model.lm_head model.hf_device_map = {"": "cuda:0"} handle = ModelHandle(model, None, config, "measured-qwen-geometry", "causal_lm") demand = model_phase_demand(handle, "excise", mode="default", edit_method="basic") required = sum( item.bytes for item in demand.components if item.device == "cuda:0" and item.confidence == "calibrated" ) assert int(2.61 * GIB) <= required <= int(2.7 * GIB) budget = int(1.38 * GIB) with pytest.raises(PhaseAdmissionError) as caught: admit_phase( "excise", demand, observer=lambda device: MemoryObservation(device, budget, 12 * GIB), safety_reserve_bytes=0, mode="default", ) assert caught.value.device == "cuda:0" assert caught.value.required_bytes == required assert caught.value.shortfall_bytes == required - budget def test_matrix_estimated_workspace_warns_by_default_and_rejects_strict(caplog): handle = _matrix_handle("llama", "none", "single_gpu") demand = model_phase_demand( handle, "distill", retained_rows=20, distill_workload=DistillationWorkload(method="leace_exact"), mode="default", ) host = demand.devices["cpu"].additional_bytes assert host > 0 budget = {"cpu": host // 2, "cuda:0": 8 * 1024**2, "cuda:1": 8 * 1024**2} report = admit_phase( "distill", demand, observer=_observer(budget), safety_reserve_bytes=0, mode="default", ) assert report.warnings assert "distill" in caplog.text and "cpu" in caplog.text with pytest.raises(PhaseAdmissionError, match="distill.*cpu.*shortfall"): admit_phase( "distill", demand, observer=_observer(budget), safety_reserve_bytes=0, mode="strict", )