"""Pipeline callback contracts for native checkpoint export.""" from types import SimpleNamespace from unittest.mock import MagicMock import pytest import torch from transformers import LlamaConfig, LlamaForCausalLM from obliteratus.abliterate import AbliterationPipeline, PipelineFailure from obliteratus.models.checkpoint_export import plan_native_export def test_pipeline_keeps_upstream_positional_arguments_before_new_controls(): from inspect import signature from obliteratus.informed_pipeline import InformedAbliterationPipeline # Public positional tails at upstream 7e8cf557. expected = { AbliterationPipeline: (71, ( "on_stage", "on_log", "cancellation_event", "seed", "concept", "concept_registry", "project_router", "override_gate", "allow_collateral_fail", "validation_reference", "gate_mode", "skip_collateral", "refusal_eval_size", "collateral_margin", "refusal_eval_max_new_tokens", "evaluation_battery", "repair", "repair_config", "repair_export_choice", "fold_post_norms", "layers", "preserve", "refusal_direction", "repair_preserve", "extraction_layer", "snapshot_enabled", "snapshot_max_bytes", )), InformedAbliterationPipeline: (28, ("sparse_surgery_threshold", "seed", "layers")), } for pipeline_class, (offset, names) in expected.items(): api = signature(pipeline_class.__init__) arguments = [object() for _ in range(offset + len(names))] bound = api.bind(*arguments) assert tuple(api.parameters)[offset:offset + len(names)] == names for index, name in enumerate(names, offset): assert bound.arguments[name] is arguments[index] def _pipeline(callback=None, **options): model = LlamaForCausalLM(LlamaConfig( vocab_size=32, hidden_size=16, intermediate_size=32, num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=2, )) pipeline = AbliterationPipeline("offline", method="basic", on_checkpoint_shard=callback, **options) pipeline.handle = SimpleNamespace(model=model, tokenizer=MagicMock()) return pipeline, plan_native_export(model, "16KB") def test_native_checkpoint_callback_receives_finalized_shards(tmp_path): received = [] pipeline, plan = _pipeline(lambda path, name: received.append((path, name))) pipeline._write_local_checkpoint(tmp_path, "{}", None, export_plan=plan, max_shard_size="16KB") assert received assert all(path.name == name and path.is_file() for path, name in received) pipeline.handle.tokenizer.save_pretrained.assert_called_once_with(tmp_path) def test_callback_failure_propagates_before_checkpoint_completion(tmp_path): pipeline, plan = _pipeline(lambda _path, _name: (_ for _ in ()).throw(RuntimeError("publish"))) with pytest.raises(RuntimeError, match="publish"): pipeline._write_local_checkpoint(tmp_path, "{}", None, export_plan=plan, max_shard_size="16KB") pipeline.handle.tokenizer.save_pretrained.assert_not_called() def test_callback_requires_native_export_plan(tmp_path): pipeline, _ = _pipeline(lambda _path, _name: None) with pytest.raises(PipelineFailure, match="requires a native export plan") as caught: pipeline._write_local_checkpoint(tmp_path, "{}", {}) assert caught.value.metric == "checkpoint_callback" def test_pipeline_resumes_verified_export_without_materializing_weights(tmp_path, monkeypatch): from obliteratus.models import checkpoint_export as export receipts = [] saved = {} progress = [] def record(path, receipt): saved[receipt.filename] = path.read_bytes() receipts.append(receipt) first = tmp_path / "first" first.mkdir() pipeline, plan = _pipeline( export_continuation=export.ExportContinuation("a" * 64, record_shard=record), export_progress=progress.append, ) pipeline._write_local_checkpoint(first, "{}", None, export_plan=plan) assert len(receipts) == len(plan.shards) assert len([event for event in progress if event.event == "complete"]) == len(plan.shards) def restore(receipt, destination): destination.write_bytes(saved[receipt.filename]) pipeline.export_continuation = export.ExportContinuation( "a" * 64, receipts=tuple(receipts), restore_shard=restore, ) progress.clear() materialize = MagicMock(side_effect=AssertionError("restored shard must not load weights")) monkeypatch.setattr(export, "_materialize_tensor", materialize) second = tmp_path / "second" second.mkdir() pipeline._write_local_checkpoint(second, "{}", None, export_plan=plan) materialize.assert_not_called() assert {path.name: path.read_bytes() for path in second.iterdir()} == { path.name: path.read_bytes() for path in first.iterdir() } completed = [event for event in progress if event.event == "complete"] assert len(completed) == len(plan.shards) assert all(event.reused for event in completed) @pytest.mark.parametrize("option", ["export_continuation", "export_progress"]) def test_export_controls_reject_unsupported_layout_before_full_gather(tmp_path, monkeypatch, option): from obliteratus.models import checkpoint_export as export value = export.ExportContinuation("a" * 64) if option == "export_continuation" else lambda _: None pipeline, _ = _pipeline(**{option: value}) pipeline.output_dir = tmp_path / "output" monkeypatch.setattr(export, "plan_native_export", lambda *_args, **_kwargs: None) monkeypatch.setattr(pipeline, "_build_metadata", lambda: {}) gather = MagicMock(side_effect=AssertionError("unsupported continuation must not gather weights")) monkeypatch.setattr(pipeline, "_gather_state_dict", gather) with pytest.raises(PipelineFailure, match="requires a native export plan"): pipeline._rebirth() gather.assert_not_called() assert not pipeline.output_dir.exists() @pytest.mark.parametrize("option", ["export_continuation", "export_progress", "on_checkpoint_shard"]) def test_informed_export_controls_require_native_writer_at_construction(option): from obliteratus.informed_pipeline import InformedAbliterationPipeline from obliteratus.models.checkpoint_export import ExportContinuation value = ExportContinuation("a" * 64) if option == "export_continuation" else lambda *_: None with pytest.raises(ValueError, match="require an informed pipeline with native export"): InformedAbliterationPipeline("offline", **{option: value}) class NativeInformed(InformedAbliterationPipeline): def _informed_uses_native_export(self): return True def _rebirth_informed_checkpoint(self): return self._rebirth() pipeline = NativeInformed("offline", **{option: value}) attribute = "_on_checkpoint_shard" if option == "on_checkpoint_shard" else option assert getattr(pipeline, attribute) is value @pytest.mark.parametrize("output", [torch.ones(1, 2, 3), (torch.ones(1, 2, 3), "cache")]) def test_default_activation_readout_preserves_tensor_and_tuple_contract(output): pipeline = AbliterationPipeline("offline", method="basic") assert pipeline._read_activation_hidden(torch.nn.Identity(), output) is ( output[0] if isinstance(output, tuple) else output ) def test_activation_collector_uses_overridable_hidden_readout(monkeypatch): pipeline = AbliterationPipeline("offline", method="basic") seen = [] pipeline._read_activation_hidden = lambda layer, output: seen.append((layer, output)) or output[1] hidden = torch.ones(1, 2, 3) hook = None class Layer: def register_forward_hook(self, callback): nonlocal hook hook = callback return SimpleNamespace(remove=lambda: None) pipeline.handle = SimpleNamespace( model=lambda **_kwargs: hook(None, (), (torch.zeros_like(hidden), hidden)), tokenizer=SimpleNamespace(padding_side="right", pad_token_id=0, eos_token_id=1), ) pipeline._activation_collection_limits = lambda _layers: (1, 8, None) pipeline._get_model_device = lambda _model: "cpu" pipeline._tokenize_activation_batch = lambda _prompts, _limit, **_kwargs: { "input_ids": torch.ones(1, 2, dtype=torch.long), "attention_mask": torch.ones(1, 2, dtype=torch.long), } rows = pipeline._collect_activations([Layer()], ["prompt"], "test") assert len(seen) == 1 assert torch.equal(rows[0][0], hidden[:, -1, :].float())