"""Exercise actual quantized surgery helpers with CPU tensor and kernel surrogates.""" from types import SimpleNamespace from unittest.mock import Mock import sys import warnings import pytest import torch from obliteratus.abliterate import AbliterationPipeline, PipelineFailure from tests.test_excise_failures import editing_pipeline as editing_pipeline, prepare_public_run KINDS = ("dense", "fused", "granular", "inverted") def quantized_surgery(pipeline, monkeypatch, kind, failure=None, error=None): """Use packed storage and real helper dispatch, replacing only bnb kernels.""" pipeline.use_sae_features = False layer = pipeline.handle.model.model.layers[0] if kind == "dense": owner, name, shape = layer.self_attn.o_proj, "weight", (32, 32) else: layer.mlp = torch.nn.Module() layer.mlp.experts = torch.nn.Module() owner, name, shape = layer.mlp.experts, "down_proj", (2, 64, 32) if kind == "granular": pipeline.per_expert_directions = True pipeline._expert_directions = {0: {0: pipeline.concept_directions[0]}} elif kind == "inverted": pipeline.invert_refusal = True pipeline._expert_safety_scores = {0: [(0, 1.0), (1, 0.1)]} parameter = torch.nn.Parameter(torch.ones(shape, dtype=torch.uint8), requires_grad=False) parameter.quant_state = SimpleNamespace(values=torch.ones(shape), shape=shape) setattr(owner, name, parameter) dequantize = Mock(side_effect=lambda _data, state: state.values.clone()) quantize = Mock(side_effect=lambda data, **_kw: ( data.to(torch.uint8), SimpleNamespace(values=data.clone(), shape=data.shape))) if failure == "dequantize": dequantize.side_effect = error elif failure == "quantize": quantize.side_effect = error monkeypatch.setitem(sys.modules, "bitsandbytes", SimpleNamespace(functional=SimpleNamespace( dequantize_4bit=dequantize, quantize_4bit=quantize))) return SimpleNamespace(owner=owner, name=name, parameter=parameter, dequantize=dequantize, quantize=quantize) @pytest.mark.parametrize("kind", KINDS) @pytest.mark.parametrize("failure", ["dequantize", "quantize"]) def test_quantized_helper_oom_keeps_original_cause_and_never_falls_back_or_exports( editing_pipeline, monkeypatch, kind, failure, ): pipeline, events = editing_pipeline handle = pipeline.handle error = torch.OutOfMemoryError(f"injected {kind} {failure} OOM") state = quantized_surgery(pipeline, monkeypatch, kind, failure, error) before = state.parameter.clone() verify, export = prepare_public_run(pipeline, monkeypatch) with warnings.catch_warnings(record=True) as observed: warnings.simplefilter("always") with pytest.raises(PipelineFailure) as caught: pipeline.run() assert caught.value.metric == "memory_exhaustion" and caught.value.stage == "excise" assert caught.value.__cause__ is error assert state.dequantize.call_count == 1 assert state.quantize.call_count == (failure == "quantize") assert getattr(state.owner, state.name) is state.parameter assert torch.equal(state.parameter, before) assert not observed # In particular, no float fallback after memory exhaustion. assert handle.model is None and handle.tokenizer is None assert [event.status for event in events if event.stage == "excise"] == ["running", "error"] assert pipeline._excise_modified_count is None verify.assert_not_called() export.assert_not_called() @pytest.mark.parametrize("kind", KINDS) @pytest.mark.parametrize("error_type", [RuntimeError, AttributeError, ImportError]) def test_failed_dequantization_is_terminal_even_when_other_weights_were_edited( editing_pipeline, monkeypatch, kind, error_type, ): pipeline, events = editing_pipeline state = quantized_surgery(pipeline, monkeypatch, kind, "dequantize", error_type("bad quantization")) before = state.parameter.clone() attention = pipeline.handle.model.model.layers[0].self_attn.q_proj.weight pristine_attention = attention.clone() verify, export = prepare_public_run(pipeline, monkeypatch) with pytest.warns(UserWarning, match="projection failed"): with pytest.raises(PipelineFailure, match="1 projection operation") as caught: pipeline.run() assert caught.value.metric == "projection" assert state.dequantize.call_count == 1 state.quantize.assert_not_called() assert torch.equal(state.parameter, before) if kind != "dense": assert not torch.equal(attention, pristine_attention) assert events[-1].details["failed_projection_count"] == 1 assert not any(event.status == "done" for event in events) assert pipeline.handle.model is None verify.assert_not_called() export.assert_not_called() @pytest.mark.parametrize("kind", KINDS) def test_healthy_quantized_surgery_preserves_packed_storage(editing_pipeline, monkeypatch, kind): pipeline, events = editing_pipeline state = quantized_surgery(pipeline, monkeypatch, kind) before = state.parameter.quant_state.values.clone() pipeline._excise() assert events[-1].status == "done" assert state.dequantize.call_count == state.quantize.call_count == 1 assert getattr(state.owner, state.name) is state.parameter assert state.parameter.dtype == torch.uint8 assert not torch.equal(state.parameter.quant_state.values, before) @pytest.mark.parametrize("kind", KINDS) def test_non_oom_repacking_retains_existing_float_fallback(editing_pipeline, monkeypatch, kind): pipeline, events = editing_pipeline state = quantized_surgery(pipeline, monkeypatch, kind, "quantize", RuntimeError("unavailable repacker")) with warnings.catch_warnings(record=True) as observed: warnings.simplefilter("always") pipeline._excise() assert events[-1].status == "done" assert state.quantize.call_count == 1 assert getattr(state.owner, state.name).dtype == torch.float32 assert getattr(state.owner, state.name) is not state.parameter if kind == "dense": assert len(observed) == 1 and "float weight replacement" in str(observed[0].message) def test_gptq_repacking_oom_is_not_converted_to_float_fallback(): packed = type("QuantLinear", (), {})() packed.qweight = torch.ones(1, dtype=torch.uint8) packed.scales = torch.ones(1) packed.weight = torch.nn.Parameter(torch.ones(1, 2, dtype=torch.uint8), requires_grad=False) original = packed.weight error = torch.OutOfMemoryError("injected GPTQ pack OOM") packed.pack = Mock(side_effect=error) with pytest.raises(torch.OutOfMemoryError) as caught: AbliterationPipeline._replace_quantized_weight(packed, torch.ones(1, 2)) assert caught.value is error assert packed.weight is original