"""Offline regression bounds calibrated against the user's RTX 4090 measurements.""" from dataclasses import replace from types import SimpleNamespace from unittest.mock import Mock import pytest import torch from torch._subclasses.fake_tensor import FakeTensorMode from transformers import Qwen2Config from obliteratus.abliterate import AbliterationPipeline from obliteratus.models.loader import ModelHandle from obliteratus.phase_admission import ( ForwardWorkload, GIB, MemoryObservation, PhaseAdmissionError, admit_phase, model_phase_demand, ) @pytest.fixture def qwen_handle(): """Real 1.5B matrix geometry, with no real weight storage or CUDA calls.""" config = Qwen2Config( hidden_size=1536, intermediate_size=8960, num_hidden_layers=28, num_attention_heads=12, num_key_value_heads=2, vocab_size=151936, tie_word_embeddings=True, ) config._attn_implementation = "sdpa" def matrix(rows, columns): module = torch.nn.Module() module.weight = torch.nn.Parameter( torch.empty(rows, columns, device="cuda:0", dtype=torch.bfloat16), ) return module with FakeTensorMode(): model = torch.nn.Module() model.config = config model.model = torch.nn.Module() model.model.embed_tokens = matrix(151936, 1536) model.model.layers = torch.nn.ModuleList() for _ in range(28): layer = torch.nn.Module() layer.self_attn = torch.nn.Module() for name, rows in (("q_proj", 1536), ("k_proj", 256), ("v_proj", 256), ("o_proj", 1536)): setattr(layer.self_attn, name, matrix(rows, 1536)) layer.mlp = torch.nn.Module() layer.mlp.up_proj = matrix(8960, 1536) layer.mlp.gate_proj = matrix(8960, 1536) layer.mlp.down_proj = matrix(1536, 8960) model.model.layers.append(layer) model.lm_head = model.model.embed_tokens model.get_input_embeddings = lambda: model.model.embed_tokens model.get_output_embeddings = lambda: model.lm_head model._supports_logits_to_keep = lambda: True model.generation_config = SimpleNamespace(use_cache=True, num_beams=1) return ModelHandle(model, Mock(), config, "synthetic-qwen2.5-1.5b", "causal_lm") def calibrated_bytes(demand, device): return sum(c.bytes for c in demand.components if c.device == device and c.confidence == "calibrated") @pytest.mark.parametrize("scores,logits", [(False, False), (True, False), (False, True), (True, True)]) @pytest.mark.parametrize("return_dict", [False, True]) def test_generation_retention_is_estimated_without_changing_calibrated_baseline( qwen_handle, monkeypatch, scores, logits, return_dict, ): monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *args: True) monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None) pipeline = AbliterationPipeline( "offline", method="basic", harmful_prompts=["x"], harmless_prompts=["y"], ) pipeline.handle = qwen_handle pipeline._admission_token_lengths = lambda prompts, *args, **kwargs: [8] * len(prompts) pipeline._format_generation_prompt = lambda prompt: prompt generation_config = SimpleNamespace( use_cache=True, num_beams=1, return_dict_in_generate=return_dict, output_scores=scores, output_logits=logits, ) qwen_handle.model.generation_config = generation_config before = vars(generation_config).copy() workloads, _ = pipeline._phase_forward_workloads("baseline", 1, 256) generation, _ = pipeline._phase_forward_workloads("baseline", 1, 256, diagnostic="coherence") workloads += generation demand = model_phase_demand(qwen_handle, "baseline", forward_workloads=workloads) # Exact round-13 value with optional outputs disabled. Keeping this literal # prevents a tier fix from silently lowering the ordinary calibrated floor. baseline = 37_154_944 assert calibrated_bytes(demand, "cuda:0") == baseline retained = [c for c in demand.components if c.name == "retained generation scores/logits"] enabled = return_dict and (scores or logits) assert bool(retained) == enabled assert all(c.confidence == "estimated" and c.bytes > 0 for c in retained) assert sum(c.bytes for c in retained) == demand.devices["cuda:0"].additional_bytes - baseline generation = [w for w in workloads if w.output_hidden_states is not None] assert generation and all(w.logits_copies == 3 for w in generation) assert all(w.retained_logits_copies == (64 * (scores + logits) if return_dict else 0) for w in generation) assert all(c.bytes is not None for c in demand.components) assert sum(c.bytes for c in demand.components) == sum( d.additional_bytes for d in demand.devices.values() ) options = dict( safety_reserve_bytes=0, observer=lambda device: MemoryObservation(device, 48 * 1024**2, 24 * 1024**3), ) report = admit_phase("baseline", demand, mode="default", **options) assert bool(report.warnings) == enabled if enabled: assert any("retained generation scores/logits" in warning for warning in report.warnings) with pytest.raises(PhaseAdmissionError, match="baseline.*cuda:0.*shortfall"): admit_phase("baseline", demand, mode="strict", **options) else: assert demand.devices["cuda:0"].additional_bytes == baseline assert not admit_phase("baseline", demand, mode="strict", **options).warnings assert vars(generation_config) == before def test_generation_retention_shares_sequential_peak(qwen_handle, monkeypatch): monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *args: True) plain = ForwardWorkload(1, 8, logits_tokens=1, logits_copies=3) retained = replace(plain, retained_logits_copies=64) options = dict(sequential_workspace_bytes={"cuda:0": 96 * 1024**2}) baseline = model_phase_demand(qwen_handle, "verify", forward_workloads=[plain], **options) demand = model_phase_demand(qwen_handle, "verify", forward_workloads=[retained], **options) assert demand == baseline @pytest.mark.parametrize("method", ["basic", "diff_means", "svd", "whitened_svd"]) def test_pipeline_basic_distill_calibrates_effective_operation(qwen_handle, method): pipeline = AbliterationPipeline("synthetic", method="basic") pipeline.handle = qwen_handle workload = pipeline._distillation_workload() assert workload.sae_features == 8 and workload.sae_rows_by_layer == () workload = replace(workload, method=method) demand = model_phase_demand(qwen_handle, "distill", distill_workload=workload, mode="default") assert calibrated_bytes(demand, "cpu") == 221_184 assert demand.devices["cpu"].additional_bytes == 221_184 with pytest.raises(PhaseAdmissionError) as caught: admit_phase("distill", demand, mode="default", safety_reserve_bytes=0, observer=lambda dev: MemoryObservation(dev, 0, 0)) assert caught.value.device == "cpu" and caught.value.required_bytes == 221_184 @pytest.mark.parametrize("extra", [ {"harmless_pca": True}, {"rdo_refinement": True}, {"expert_count": 2}, {"sae_expansion": 1, "sae_rows_by_layer": (20,)}, ]) def test_optional_distill_work_preserves_basic_floor(qwen_handle, extra): pipeline = AbliterationPipeline("synthetic", method="basic") pipeline.handle = qwen_handle workload = replace(pipeline._distillation_workload(), **extra) demand = model_phase_demand(qwen_handle, "distill", distill_workload=workload, retained_rows=20, mode="default") assert calibrated_bytes(demand, "cpu") == 221_184 assert any(c.confidence == "estimated" and c.bytes for c in demand.components) def basic_edit_pipeline(handle, **options): pipeline = AbliterationPipeline( "synthetic", method="basic", harmful_prompts=["x"], harmless_prompts=["y"], use_kl_optimization=False, **options, ) pipeline.handle = handle pipeline._strong_layers = [9] pipeline.concept_subspaces[9] = torch.ones(1, 1536) pipeline.concept_directions[9] = torch.ones(1536) return pipeline def pipeline_edit_demand(pipeline): options = pipeline._pipeline_phase_demand_options( "excise", max_shard_size="2GB", mode="default", ) options.pop("gpu_memory_utilization") return model_phase_demand(pipeline.handle, "excise", **options) @pytest.mark.parametrize("flag", [ None, "spectral_cascade", "project_embeddings", "invert_refusal", "safety_neuron_masking", "attention_head_surgery", "expert_transplant", "activation_steering", "use_sae_features", "true_iterative_refinement", ]) def test_optional_edit_does_not_erase_calibrated_projection(qwen_handle, flag): pipeline = basic_edit_pipeline(qwen_handle, **({flag: True} if flag else {})) demand = pipeline_edit_demand(pipeline) assert calibrated_bytes(demand, "cuda:0") == 2_803_988_480 with pytest.raises(PhaseAdmissionError) as caught: admit_phase("excise", demand, mode="default", observer=lambda dev: MemoryObservation(dev, int(1.38 * GIB), 24 * GIB)) assert caught.value.device == "cuda:0" def test_active_spectral_workspace_is_separate_from_ordinary_edit(qwen_handle): pipeline = basic_edit_pipeline(qwen_handle, spectral_cascade=True) pipeline._strong_layers = [6, 7, 8, 9] for idx in pipeline._strong_layers: pipeline.concept_directions[idx] = torch.ones(1536) pipeline.concept_subspaces[idx] = torch.ones(1, 1536) demand = pipeline_edit_demand(pipeline) assert calibrated_bytes(demand, "cuda:0") == 2_803_988_480 assert any(c.device == "cpu" and c.confidence == "estimated" and c.bytes for c in demand.components) def test_optional_untied_embedding_edit_does_not_expand_calibrated_floor(qwen_handle): # An oversized input embedding makes the auxiliary transaction the peak; # the ordinary decoder/head operations retain their own smaller floor. model = qwen_handle.model with FakeTensorMode(): embedding = torch.nn.Module() embedding.weight = torch.nn.Parameter( torch.empty(2 * 151936, 1536, dtype=torch.bfloat16, device="cuda:0"), ) model.model.embed_tokens = embedding pipeline = basic_edit_pipeline(qwen_handle, project_embeddings=True) assert "model.embed_tokens.weight" in pipeline._excise_parameter_names() ordinary = pipeline._excise_parameter_names(ordinary_only=True) assert "model.embed_tokens.weight" not in ordinary assert "lm_head.weight" in ordinary demand = pipeline_edit_demand(pipeline) assert calibrated_bytes(demand, "cuda:0") == 2_803_988_480 floor = calibrated_bytes(demand, "cuda:0") assert 1.9 * floor < demand.devices["cuda:0"].additional_bytes < 2 * floor assert any(c.device == "cuda:0" and c.confidence == "estimated" and c.bytes for c in demand.components) def test_lora_replacement_and_empty_selection_fallback_keep_distinct_tiers( qwen_handle, monkeypatch, ): pipeline = basic_edit_pipeline(qwen_handle, use_lora_ablation=True, lora_rank=1) assert pipeline._lora_adapter_ranks() assert pipeline._excise_parameter_names(ordinary_only=True) == () assert calibrated_bytes(pipeline_edit_demand(pipeline), "cuda:0") == 0 monkeypatch.setattr("obliteratus.lora_ablation.lora_projection_targets", lambda *a: ()) assert pipeline._excise_parameter_names(ordinary_only=True) assert calibrated_bytes(pipeline_edit_demand(pipeline), "cuda:0") == 2_803_988_480 @pytest.mark.parametrize("stage", ["distill", "excise"]) def test_actual_pipeline_calibrated_rejection_precedes_work(qwen_handle, monkeypatch, stage): from obliteratus import phase_admission from obliteratus.abliterate import PipelineFailure pipeline = basic_edit_pipeline(qwen_handle, spectral_cascade=True) monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default") budget = 0 if stage == "distill" else int(1.38 * GIB) monkeypatch.setattr(phase_admission, "observe_memory", lambda dev: MemoryObservation(dev, budget, 24 * GIB)) original = pipeline._admit_pipeline_phase monkeypatch.setattr(pipeline, "_admit_pipeline_phase", lambda name: original(name) if name == stage else None) work = {name: Mock() for name in ( "_summon", "_capture_stock_baseline", "_validate_architecture_surgery_support", "_probe", "_distill", "_validate_concept_gate", "_capture_concept_evaluation", "_capture_concept_repair", "_capture_baseline_kl_logits", "_excise", "_free_gpu_memory", )} for name, method in work.items(): monkeypatch.setattr(pipeline, name, method) with pytest.raises(PipelineFailure) as caught: pipeline._run_pipeline() assert caught.value.stage == stage work["_" + stage].assert_not_called() if stage == "excise": work["_capture_baseline_kl_logits"].assert_not_called() # Actual maximum padded batches measured with the cached tokenizer and built-in # prompts. These constants make tests independent of tokenizer/model downloads. # Basic method, seed7, verify_sample_size4, refusal_max_tokens32. Verify includes # a 16x19 spectral probe as well as serial capability generation up to235 tokens. REFERENCE_WORKLOADS = { "baseline": [ForwardWorkload(1, 35, logits_copies=4), ForwardWorkload(1, 1, cache_tokens=71, logits_tokens=1, logits_copies=3)], "probe": [ForwardWorkload(16, 27, logits_dtype_bytes=2)], "distill": [], "validate": [], "excise": [ForwardWorkload(1, 10, logits_dtype_bytes=2)], "verify": [ForwardWorkload(1, 35, logits_copies=4), ForwardWorkload(16, 19, logits_dtype_bytes=2), ForwardWorkload(4, 42, logits_tokens=1, logits_copies=3), ForwardWorkload(4, 1, cache_tokens=74, logits_tokens=1, logits_copies=3), ForwardWorkload(1, 1, cache_tokens=235, logits_tokens=1, logits_copies=3)], } def test_unknown_forward_cannot_hide_calibrated_dense_edit(qwen_handle, monkeypatch): from obliteratus import phase_admission as admission monkeypatch.setattr(admission, "_attention_compute_dtypes", Mock(side_effect=ValueError("opaque projection contract"))) demand = model_phase_demand( qwen_handle, "excise", forward_workloads=REFERENCE_WORKLOADS["excise"], edit_parameter_names=["model.embed_tokens.weight"], ) assert any(c.bytes is None for c in demand.components) assert sum(c.bytes for c in demand.components if c.confidence == "calibrated") > 2.61 * GIB with pytest.raises(admission.PhaseAdmissionError) as caught: admission.admit_phase("excise", demand, observer=lambda device: admission.MemoryObservation(device, int(1.38 * GIB), 24 * GIB)) assert caught.value.device == "cuda:0" def test_invalid_forward_shape_is_unknown_by_default_and_structured_in_strict(qwen_handle): from obliteratus import phase_admission as admission report = admission.admit_model_phase( qwen_handle, "probe", batch_size=-1, observer=lambda device: admission.MemoryObservation(device, 24 * GIB, 24 * GIB), ) assert report.warnings and any(c.bytes is None for c in report.components) with pytest.raises(admission.PhaseAdmissionError, match="positive integer"): admission.admit_model_phase(qwen_handle, "probe", batch_size=-1, mode="strict") @pytest.mark.parametrize("phase,measured,maximum", [ ("baseline", 0.08, 0.15), ("probe", 0.135, 1.5), ("distill", 0, 0), ("validate", 0, 0), ("excise", 2.61, 5.5), ("verify", 0.095, 1.5), ]) def test_qwen_reference_peak_bounds(qwen_handle, phase, measured, maximum): demand = model_phase_demand( qwen_handle, phase, forward_workloads=REFERENCE_WORKLOADS[phase], retained_rows=1684, tokens=27, edit_parameter_names=["model.embed_tokens.weight"], ) peak = demand.devices["cuda:0"].additional_bytes / GIB assert measured <= peak <= maximum if phase == "probe": assert peak == pytest.approx(0.195580, abs=1e-6) assert demand.devices["cpu"].additional_bytes / GIB == pytest.approx(0.289078, abs=1e-6) if phase == "verify": assert peak == pytest.approx(0.142042, abs=1e-6) def test_excise_sequential_kl_capture_and_edit_take_peak_not_sum(qwen_handle): options = {"edit_parameter_names": ["model.embed_tokens.weight"]} edit = model_phase_demand(qwen_handle, "excise", forward_workloads=[], **options) forward = model_phase_demand(qwen_handle, "probe", forward_workloads=[ForwardWorkload(1, 10)]) together = model_phase_demand(qwen_handle, "excise", **options, forward_workloads=[ForwardWorkload(1, 10)]) assert together.devices["cuda:0"].additional_bytes == max( edit.devices["cuda:0"].additional_bytes, forward.devices["cuda:0"].additional_bytes, ) def test_generation_separates_query_kv_and_output_lengths(qwen_handle): prefill = ForwardWorkload(4, 42, logits_tokens=1, logits_copies=3) decode = ForwardWorkload(4, 1, cache_tokens=74, logits_tokens=1, logits_copies=3) first = model_phase_demand(qwen_handle, "verify", forward_workloads=[prefill]).devices["cuda:0"] last = model_phase_demand(qwen_handle, "verify", forward_workloads=[decode]).devices["cuda:0"] assert first.output_bytes == last.output_bytes == 3 * 4 * 151936 * 4 assert first.kv_bytes == 28 * 2 * 4 * 42 * 2 * 128 * 2 assert last.kv_bytes == 28 * 2 * 4 * 74 * 2 * 128 * 2 no_cache = model_phase_demand(qwen_handle, "probe", forward_workloads=[ ForwardWorkload(4, 42, use_cache=False), ]).devices["cuda:0"] assert no_cache.kv_bytes == 0 def test_sdpa_omits_eager_attention_scores_when_fused_is_proven(qwen_handle, monkeypatch): monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a: True) options = {"forward_workloads": [ForwardWorkload(4, 42)]} fused = model_phase_demand(qwen_handle, "probe", **options).devices["cuda:0"] qwen_handle.model.config._attn_implementation = "eager" eager = model_phase_demand(qwen_handle, "probe", **options).devices["cuda:0"] assert eager.activation_bytes - fused.activation_bytes == 2 * 4 * 12 * 42 * 42 * 4 def test_pipeline_uses_measured_lengths_and_actual_generation_head_contract(qwen_handle): pipeline = AbliterationPipeline( "synthetic", method="basic", harmful_prompts=["x", "long prompt", "last"], harmless_prompts=["safe", "long safe prompt", "last"], verify_sample_size=3, refusal_max_tokens=32, ) pipeline.handle = qwen_handle pipeline.max_seq_length = 8 def tokenize(prompt, **kwargs): prompts = [prompt] if isinstance(prompt, str) else prompt lengths = [min(len(p), kwargs.get("max_length", 10000)) for p in prompts] length = max(lengths) ids = torch.ones(len(prompts), length, dtype=torch.long) mask = torch.stack([torch.arange(length) >= length - n for n in lengths]) return {"input_ids": ids, "attention_mask": mask} qwen_handle.tokenizer.side_effect = tokenize qwen_handle.tokenizer.apply_chat_template.side_effect = ( lambda messages, **kwargs: messages[-1]["content"] ) workloads, _ = pipeline._phase_forward_workloads("probe", 2, 8) assert {(w.batch_size, w.tokens) for w in workloads} == {(2, 8), (1, 4)} workloads, _ = pipeline._phase_forward_workloads("verify", 2, 8, diagnostic="refusal") assert ForwardWorkload(3, 8, cache_tokens=8, logits_tokens=1, logits_copies=3, attention_mask=True, output_hidden_states=False) in workloads assert ForwardWorkload(3, 1, cache_tokens=40, logits_tokens=1, logits_copies=3, attention_mask=True, output_hidden_states=False) in workloads qwen_handle.model._supports_logits_to_keep = lambda: False workloads, _ = pipeline._phase_forward_workloads("verify", 2, 8, diagnostic="refusal") assert ForwardWorkload(3, 8, cache_tokens=8, logits_tokens=8, logits_copies=3, attention_mask=True, output_hidden_states=False) in workloads qwen_handle.model.generation_config.use_cache = False workloads, _ = pipeline._phase_forward_workloads("verify", 2, 8, diagnostic="refusal") assert ForwardWorkload(3, 40, cache_tokens=40, logits_tokens=40, logits_copies=3, use_cache=False, attention_mask=True, output_hidden_states=False) in workloads def test_pipeline_basic_excise_accounts_for_tied_head_without_refinement(qwen_handle, monkeypatch): from obliteratus import phase_admission pipeline = AbliterationPipeline( "synthetic", method="basic", seed=7, verify_sample_size=4, refusal_eval_size=8, refusal_max_tokens=32, ) pipeline.handle = qwen_handle pipeline._strong_layers = [5, 6, 7, 8, 9] pipeline.concept_subspaces[9] = torch.ones(1, 1536) qwen_handle.tokenizer.return_value = {"input_ids": torch.ones(1, 10, dtype=torch.long)} monkeypatch.setattr(pipeline, "_activation_collection_limits", lambda _: (16, 256, None)) admit = Mock() monkeypatch.setattr(phase_admission, "admit_model_phase", admit) pipeline._admit_pipeline_phase("excise") options = admit.call_args.kwargs.copy() options.pop("gpu_memory_utilization") # Basic mode does not need KL for surgery. Capture is optional and gets # its own admission immediately before the serial forwards. assert not pipeline.use_kl_optimization assert options["forward_workloads"] == [] assert "model.embed_tokens.weight" in options["edit_parameter_names"] # tied lm_head demand = model_phase_demand(qwen_handle, "excise", **options) pipeline._admit_pipeline_phase("excise", work_scope="kl_capture") capture_options = admit.call_args.kwargs.copy() capture_options.pop("gpu_memory_utilization") assert capture_options["forward_workloads"] == [ ForwardWorkload(1, 10, attention_mask=False, logits_dtype_bytes=2), ] assert capture_options["edit_parameter_names"] == options["edit_parameter_names"] assert capture_options["host_workspace_bytes"] > options["host_workspace_bytes"] # The capture boundary includes the same forwards, host retention and tied # head edit as the pre-r18 EXCISE plan. Its smaller sequential forward peak # cannot change the calibrated edit floor when moved to its own boundary. with_capture = model_phase_demand(qwen_handle, "excise", **capture_options) assert calibrated_bytes(demand, "cuda:0") == calibrated_bytes(with_capture, "cuda:0") == 2_803_988_480 assert demand.devices["cuda:0"] == with_capture.devices["cuda:0"] assert demand.devices["cuda:0"].additional_bytes == 2_803_988_480 pipeline._admit_pipeline_phase("validate") options = admit.call_args.kwargs assert options["forward_workloads"] == [] assert options["host_workspace_bytes"] == 0 # basic validation is disabled def test_static_generation_cache_warns_by_default_and_rejects_strict( qwen_handle, monkeypatch, caplog, ): from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError pipeline = AbliterationPipeline("synthetic", method="basic") pipeline.handle = qwen_handle qwen_handle.model.generation_config.cache_implementation = "static" qwen_handle.tokenizer.return_value = {"input_ids": torch.ones(1, 10, dtype=torch.long)} monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default") monkeypatch.setattr( "obliteratus.phase_admission.observe_memory", lambda device: MemoryObservation(device, 100 * GIB, 100 * GIB), ) pipeline._admit_pipeline_phase("verify") pipeline._admit_pipeline_phase("verify", work_scope="coherence") assert "generation cache" in caplog.text and "static" in caplog.text assert "PHASE MEMORY ADMISSION WARNING" in caplog.text monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict") pipeline._admit_pipeline_phase("verify") message = "verify.*generation cache.*static.*ADMISSION=off" with pytest.raises(PhaseAdmissionError, match=message): pipeline._admit_pipeline_phase("verify", work_scope="coherence")