Files
OBLITERATUS/tests/test_checkpoint_processor_configs.py
Joseph Magly a897159ad8 fix(export): copy source processor configs into abliteration checkpoints
Checkpoints saved weights, configs and tokenizer files but not the
source model's processor configuration, so models packaged as
multimodal wrappers (Qwen3.5, Gemma 4 E-models) could not be served
from an exported checkpoint (vLLM: "Can't load image processor").
Export now copies processor-side config files present in the local
source directory or the exact cached Hub revision, records their
SHA-256 digests in the checkpoint metadata and projection resolution
records, and checkpoint validation rejects a listed file that is
missing or changed. No network access; a source without these files
exports exactly as before.

Fixes #585
2026-11-21 06:24:28 -05:00

137 lines
5.7 KiB
Python

"""CPU coverage for processor artifacts exported through REBIRTH."""
import json
from hashlib import sha256
import pytest
from huggingface_hub import constants
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models import checkpoint_export
from obliteratus.models.loader import ModelHandle
from obliteratus.persistence_contracts import (
serialize_checkpoint_metadata,
validate_local_checkpoint,
validate_reloadable_checkpoint,
)
from obliteratus.recipes.resolution_records import read_checkpoint_resolution
from tests.fixtures.tiny_arch_models import tiny_llama
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
pytestmark = pytest.mark.cpu
_PROCESSOR_FILES = {
"preprocessor_config.json": b'{ "size": 32 }\n',
"video_preprocessor_config.json": b'{\r\n "num_frames": 2\r\n}\r\n',
"processor_config.json": b'{"processor_class": "ExampleProcessor"}',
"image_processor_settings.json": b'{"format": "rgb"}\n',
"feature_extractor_config.json": b'{"sampling_rate": 16000}',
"audio_config.json": b'{"channels": 1}\n',
"audio_tokenizer_config.json": b'{"frame_rate": 25}\n',
}
_BASE_FILES = {
"abliteration_metadata.json", "config.json", "generation_config.json",
"model.safetensors", "recipe-resolution.json", "tokenizer.json", "tokenizer_config.json",
}
def _pipeline(tmp_path, model_name, *, revision=None):
model = tiny_llama()
pipeline = AbliterationPipeline(
str(model_name), output_dir=str(tmp_path / "checkpoint"), method="basic",
device="cpu", on_log=lambda _message: None,
)
pipeline.handle = ModelHandle(
model, _fixture_tokenizer(), model.config, str(model_name), "causal_lm",
source_model_revision=revision,
)
return pipeline
@pytest.mark.parametrize("legacy_writer", [False, True])
def test_rebirth_copies_processor_configs_and_records_digests(tmp_path, monkeypatch, legacy_writer):
source = tmp_path / "stock"
source.mkdir()
for name, payload in _PROCESSOR_FILES.items():
(source / name).write_bytes(payload)
(source / "training_config.json").write_text("{}", encoding="utf-8")
(source / "processor.py").write_text("# unrelated source code\n", encoding="utf-8")
(source / "nested_processor.json").mkdir()
if legacy_writer:
monkeypatch.setattr(checkpoint_export, "plan_native_export", lambda *_args, **_kwargs: None)
checkpoint = _pipeline(tmp_path, source)._rebirth()
assert (checkpoint / "preprocessor_config.json").is_file()
assert set(path.name for path in checkpoint.iterdir()) == _BASE_FILES | _PROCESSOR_FILES.keys()
for name, payload in _PROCESSOR_FILES.items():
assert (checkpoint / name).read_bytes() == payload
digests = {name: sha256(payload).hexdigest() for name, payload in _PROCESSOR_FILES.items()}
metadata = json.loads((checkpoint / "abliteration_metadata.json").read_bytes())
assert metadata["processor_config_sha256"] == digests
record = read_checkpoint_resolution(checkpoint)
assert record["steps"][0]["artifacts"] == [
{"path": name, "sha256": digest} for name, digest in sorted(digests.items())
]
def test_rebirth_uses_loaded_hub_revision_and_copies_regular_files(tmp_path, monkeypatch):
cache = tmp_path / "hub"
repo = cache / "models--example--model"
revision = "a" * 40
newer_revision = "b" * 40
for commit in (revision, newer_revision):
snapshot = repo / "snapshots" / commit
snapshot.mkdir(parents=True)
(snapshot / "config.json").write_text("{}", encoding="utf-8")
(repo / "refs").mkdir()
(repo / "refs" / "main").write_text(newer_revision, encoding="utf-8")
(repo / "blobs").mkdir()
blob = repo / "blobs" / "processor"
blob.write_bytes(_PROCESSOR_FILES["preprocessor_config.json"])
cached = repo / "snapshots" / revision / "preprocessor_config.json"
cached.symlink_to(blob)
(repo / "snapshots" / newer_revision / cached.name).write_bytes(b'{"size": 64}')
monkeypatch.setattr(constants, "HF_HUB_CACHE", str(cache))
checkpoint = _pipeline(tmp_path, "example/model", revision=revision)._rebirth()
exported = checkpoint / cached.name
assert not exported.is_symlink()
assert exported.read_bytes() == blob.read_bytes()
def test_rebirth_without_processor_configs_preserves_file_set_and_metadata(tmp_path):
source = tmp_path / "stock"
source.mkdir()
pipeline = _pipeline(tmp_path, source)
expected_metadata = serialize_checkpoint_metadata(pipeline._build_metadata())
checkpoint = pipeline._rebirth()
assert set(path.name for path in checkpoint.iterdir()) == _BASE_FILES
assert (checkpoint / "abliteration_metadata.json").read_text(encoding="utf-8") == expected_metadata
assert read_checkpoint_resolution(checkpoint)["steps"][0]["artifacts"] == []
@pytest.mark.parametrize("change", ["remove", "tamper"])
def test_checkpoint_validation_rejects_altered_processor_config(tmp_path, change):
source = tmp_path / "stock"
source.mkdir()
name = "preprocessor_config.json"
(source / name).write_bytes(_PROCESSOR_FILES[name])
checkpoint = _pipeline(tmp_path, source)._rebirth()
metadata = (checkpoint / "abliteration_metadata.json").read_text(encoding="utf-8")
validate_local_checkpoint(checkpoint, metadata)
assert validate_reloadable_checkpoint(checkpoint) == checkpoint.resolve()
processor = checkpoint / name
if change == "remove":
processor.unlink(missing_ok=True)
else:
processor.write_bytes(b'{"size": 64}')
with pytest.raises(ValueError, match=r"processor config.*preprocessor_config\.json"):
validate_local_checkpoint(checkpoint, metadata)
with pytest.raises(ValueError, match=r"processor config.*preprocessor_config\.json"):
validate_reloadable_checkpoint(checkpoint)