Files
OBLITERATUS/tests/test_checkpoint_export.py
T
Joseph MaglyandClaude Opus 5.5 c43a8a5c70 fix: fall back to full-gather export for offloaded tied weights
With accelerate cpu_offload, tied parameters become separate meta
placeholders that hide their shared backing storage, so tie removal on
metadata kept lm_head.weight and the native export diverged from the
legacy checkpoint. Decline the native plan whenever a declared tie
(transformers' expanded tied-weight keys, or the embedding tie contract
as a fallback) involves a meta tensor, and document that partial offload
follows model order rather than the legacy resident-first gather order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 12:02:23 -04:00

563 lines
24 KiB
Python

"""Offline contracts for byte-identical, bounded native checkpoint export."""
from __future__ import annotations
import gc
import json
import os
import subprocess
import sys
from types import SimpleNamespace
from unittest.mock import MagicMock
import weakref
import pytest
import torch
from accelerate import cpu_offload, disk_offload
from transformers import LlamaConfig, LlamaForCausalLM
from obliteratus.abliterate import (
AbliterationPipeline, PipelineCancelledError, PipelineValidationError,
)
from obliteratus.models import checkpoint_export as export
from obliteratus.persistence_contracts import atomic_checkpoint_directory
pytestmark = pytest.mark.cpu
SHARD_SIZE = 12_000
def tiny_model(tied=False):
torch.manual_seed(247)
return LlamaForCausalLM(LlamaConfig(
vocab_size=64, hidden_size=32, intermediate_size=64,
num_hidden_layers=3, num_attention_heads=4, num_key_value_heads=4,
tie_word_embeddings=tied,
))
def files(directory):
return {
p.relative_to(directory).as_posix(): p.read_bytes()
for p in directory.rglob('*') if p.is_file()
}
def gather(model):
# Exercise the exact existing pipeline gather rather than a substitute.
return AbliterationPipeline._gather_state_dict(SimpleNamespace(
handle=SimpleNamespace(model=model), log=lambda _message: None,
))
@pytest.mark.parametrize('tied', [False, True])
@pytest.mark.parametrize('offloaded', [False, True])
@pytest.mark.parametrize('max_shard_size', [SHARD_SIZE, '2GB'])
def test_byte_identity(tmp_path, tied, offloaded, max_shard_size):
model = tiny_model(tied)
legacy_model = tiny_model(tied)
if offloaded:
disk_offload(model, tmp_path / 'offload')
disk_offload(legacy_model, tmp_path / 'legacy-offload')
plan = export.plan_native_export(model, max_shard_size)
if tied and offloaded:
assert plan is None
else:
assert plan is not None
legacy = tmp_path / 'legacy'
legacy_model.save_pretrained(
legacy, state_dict=gather(legacy_model), max_shard_size=max_shard_size,
save_original_format=False,
)
destination = tmp_path / 'native'
with atomic_checkpoint_directory(destination) as staging:
if plan is None:
model.save_pretrained(
staging, state_dict=gather(model), max_shard_size=max_shard_size,
save_original_format=False,
)
else:
export.write_native_checkpoint(model, staging, plan)
assert files(destination) == files(legacy)
assert model.config.dtype == 'float32'
assert model.config.architectures == ['LlamaForCausalLM']
assert {'config.json', 'generation_config.json'} <= files(destination).keys()
if max_shard_size == SHARD_SIZE:
assert len(list(destination.glob('*.safetensors'))) >= 3
assert 'model.safetensors.index.json' in files(destination)
if tied and not offloaded:
assert 'lm_head.weight' not in plan.tensors
if offloaded:
assert all(p.device.type == 'meta' for p in model.parameters())
def test_cpu_offloaded_tied_pipeline_export_byte_identity(tmp_path):
model = tiny_model(True)
legacy_model = tiny_model(True)
for candidate in (model, legacy_model):
cpu_offload(candidate, execution_device=torch.device('cpu'))
# Separate placeholders conceal the shared CPU storage behind this tie.
assert (
candidate.get_input_embeddings().weight is not candidate.get_output_embeddings().weight
)
assert all(p.device.type == 'meta' for p in candidate.parameters())
assert export.plan_native_export(model, SHARD_SIZE) is None
legacy = tmp_path / 'legacy'
legacy_model.save_pretrained(
legacy, state_dict=gather(legacy_model), max_shard_size=SHARD_SIZE,
save_original_format=False,
)
pipeline = AbliterationPipeline(
'offline-tiny', output_dir=str(tmp_path / 'checkpoint'), method='basic',
)
pipeline._on_log = lambda _message: None
pipeline._on_stage = lambda _event: None
pipeline.handle = SimpleNamespace(model=model, tokenizer=SimpleNamespace(
save_pretrained=lambda path: (path / 'tokenizer_config.json').write_text('{}'),
))
pipeline._build_metadata = lambda: {'schema': 1}
pipeline._free_gpu_memory = MagicMock()
pipeline._cleanup_offload_dir = MagicMock()
pipeline._gather_state_dict = MagicMock(wraps=pipeline._gather_state_dict)
pipeline._rebirth(max_shard_size=SHARD_SIZE)
pipeline._gather_state_dict.assert_called_once_with()
actual = files(pipeline.output_dir)
# Compare every model artifact, excluding the pipeline's additional files.
actual.pop('abliteration_metadata.json')
actual.pop('tokenizer_config.json')
assert actual == files(legacy)
index = json.loads(actual['model.safetensors.index.json'])
assert len(set(index['weight_map'].values())) >= 3
assert 'lm_head.weight' not in index['weight_map']
@pytest.mark.parametrize('offloaded_key', ['alias', 'target'])
def test_declared_tie_checks_both_endpoints(monkeypatch, offloaded_key):
model = tiny_model()
keys = {
'alias': 'model.layers.0.self_attn.q_proj.weight',
'target': 'model.layers.0.self_attn.k_proj.weight',
}
cpu_offload(
model.get_submodule(keys[offloaded_key].rsplit('.', 1)[0]),
execution_device=torch.device('cpu'),
)
reporting = MagicMock(return_value={keys['alias']: keys['target']})
monkeypatch.setattr(model, 'get_expanded_tied_weights_keys', reporting)
remove_ties = MagicMock(wraps=export.remove_tied_weights_from_state_dict)
monkeypatch.setattr(export, 'remove_tied_weights_from_state_dict', remove_ties)
assert export.plan_native_export(model, SHARD_SIZE) is None
reporting.assert_called_once_with(all_submodels=True)
remove_ties.assert_not_called()
@pytest.mark.parametrize('reporting', ['unavailable', 'raises'])
@pytest.mark.parametrize('tied,offloaded_module', [
(True, None), (True, 'model.embed_tokens'), (True, 'lm_head'),
(False, None), (False, ''),
])
def test_embedding_tie_fallback(monkeypatch, reporting, offloaded_module, tied):
model = tiny_model(tied)
if offloaded_module is not None:
cpu_offload(model.get_submodule(offloaded_module), execution_device=torch.device('cpu'))
if reporting == 'unavailable':
monkeypatch.delattr(export.PreTrainedModel, 'get_expanded_tied_weights_keys')
else:
monkeypatch.setattr(
model, 'get_expanded_tied_weights_keys', MagicMock(side_effect=RuntimeError('unavailable')),
)
remove_ties = MagicMock(wraps=export.remove_tied_weights_from_state_dict)
monkeypatch.setattr(export, 'remove_tied_weights_from_state_dict', remove_ties)
plan = export.plan_native_export(model, SHARD_SIZE)
if tied and offloaded_module is not None:
assert plan is None
remove_ties.assert_not_called()
else:
assert plan is not None
remove_ties.assert_called_once()
def test_planning_and_live_tensor_accounting(tmp_path, monkeypatch):
model = tiny_model()
disk_offload(model, tmp_path / 'offload')
original = export._materialize_tensor
forbidden = MagicMock(side_effect=AssertionError('planning materialized weights'))
with monkeypatch.context() as planning:
planning.setattr(export, '_materialize_tensor', forbidden)
planning.setattr(export, 'align_module_device', forbidden)
planning.setattr(torch.Tensor, 'to', forbidden)
planning.setattr(torch.Tensor, 'clone', forbidden)
plan = export.plan_native_export(model, SHARD_SIZE)
forbidden.assert_not_called()
assert plan is not None and len(plan.shards) >= 3
assert all(p.device.type == 'meta' for p in model.parameters())
# The plan must retain no real or meta tensors.
assert all(isinstance(spec, export.TensorSpec) for spec in plan.tensors.values())
tracked = []
live = []
def materialize(*args):
tensor = original(*args)
tracked.append((weakref.ref(tensor), tensor.numel() * tensor.element_size()))
live.append(sum(size for ref, size in tracked if ref() is not None))
return tensor
monkeypatch.setattr(export, '_materialize_tensor', materialize)
observed = []
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
export.write_native_checkpoint(model, staging, plan, staged_bytes=observed.append)
gc.collect()
largest = max(shard.size_bytes for shard in plan.shards)
demand = export.native_export_host_demand(plan)
assert max(live) <= largest < plan.total_bytes
assert max(observed) == largest <= demand
assert all(ref() is None for ref, _ in tracked)
assert observed.count(0) == len(plan.shards)
assert all(p.device.type == 'meta' for p in model.parameters())
@pytest.mark.parametrize('existing', [False, True])
@pytest.mark.parametrize('error', [RuntimeError, KeyboardInterrupt])
def test_interrupt_after_first_shard_rolls_back(tmp_path, existing, error):
model = tiny_model()
plan = export.plan_native_export(model, SHARD_SIZE)
destination = tmp_path / 'checkpoint'
if existing:
model.save_pretrained(destination)
before = files(destination) if existing else None
calls = 0
def cancel():
nonlocal calls
calls += 1
if calls == 2:
staging = next(tmp_path.glob('.checkpoint.staging-*'))
assert [p.name for p in staging.glob('*.safetensors')] == [plan.shards[0].filename]
raise error('interrupted')
with pytest.raises(error, match='interrupted'):
with atomic_checkpoint_directory(destination) as staging:
export.write_native_checkpoint(model, staging, plan, check_cancelled=cancel)
assert files(destination) == before if existing else not destination.exists()
assert not list(tmp_path.glob('.checkpoint.staging-*'))
def test_host_budget_rejects_before_materialization(tmp_path, monkeypatch):
model = tiny_model()
plan = export.plan_native_export(model, SHARD_SIZE)
materialize = MagicMock(side_effect=AssertionError('not admitted'))
monkeypatch.setattr(export, '_materialize_tensor', materialize)
destination = tmp_path / 'checkpoint'
with pytest.raises(export.NativeExportCapacityError, match='needs .* host bytes'):
with atomic_checkpoint_directory(destination) as staging:
export.write_native_checkpoint(
model, staging, plan, host_budget=lambda: plan.shards[0].host_demand_bytes - 1,
)
materialize.assert_not_called()
assert not destination.exists()
assert not list(tmp_path.glob('.checkpoint.staging-*'))
@pytest.mark.parametrize('unsupported', [
'quantizer', 'quantization_config', 'peft', 'tp', 'distributed',
'model_distributed', 'ignore', 'remote', 'hook', 'pre_hook', 'custom_state',
'non_model', 'helper_missing', 'bare_meta', 'shared_view', 'non_tensor',
'custom_save', 'meta_alias', 'custom_save_state', 'extra_state',
])
def test_planner_fallback(tmp_path, monkeypatch, unsupported):
model = tiny_model()
if unsupported == 'quantizer':
model.hf_quantizer = object()
elif unsupported == 'quantization_config':
model.config.quantization_config = {}
elif unsupported == 'peft':
model._hf_peft_config_loaded = True
elif unsupported == 'tp':
model._tp_size = 1
elif unsupported == 'distributed':
model.config.distributed_config = {}
elif unsupported == 'model_distributed':
model.distributed_config = {}
elif unsupported == 'ignore':
model._keys_to_ignore_on_save = ['lm_head.weight']
elif unsupported == 'remote':
monkeypatch.setattr(model, 'is_remote_code', lambda: True)
elif unsupported == 'hook':
model.register_state_dict_post_hook(lambda *args: None)
elif unsupported == 'pre_hook':
model.register_state_dict_pre_hook(lambda *args: None)
elif unsupported == 'custom_state':
monkeypatch.setattr(
model, 'state_dict', MagicMock(side_effect=AssertionError('custom hook called')),
)
elif unsupported == 'custom_save_state':
model._save_to_state_dict = MagicMock(side_effect=AssertionError('hook called'))
elif unsupported == 'extra_state':
monkeypatch.setattr(LlamaForCausalLM, 'get_extra_state', MagicMock())
elif unsupported == 'custom_save':
model.save_pretrained = MagicMock()
elif unsupported == 'non_model':
model = torch.nn.Linear(4, 4)
elif unsupported == 'helper_missing':
monkeypatch.setattr(export, 'remove_tied_weights_from_state_dict', None)
elif unsupported == 'bare_meta':
model.to('meta')
elif unsupported == 'meta_alias':
model.to('meta')
model.lm_head.weight = model.model.embed_tokens.weight
elif unsupported == 'shared_view':
model.lm_head.weight = torch.nn.Parameter(model.model.embed_tokens.weight[:, :16])
elif unsupported == 'non_tensor':
monkeypatch.setattr(LlamaForCausalLM, 'state_dict', lambda self: {'extra': 'text'})
assert export.plan_native_export(model, SHARD_SIZE) is None
def test_planner_declines_fsdp_named_model(monkeypatch):
model = tiny_model()
monkeypatch.setattr(model, '__class__', type('FSDPLlamaForCausalLM', (type(model),), {}))
assert export.plan_native_export(model, SHARD_SIZE) is None
@pytest.mark.parametrize('kind', ['parameter', 'buffer'])
def test_planner_declines_tensor_subclasses_before_detach(monkeypatch, kind):
class CustomTensor(torch.Tensor):
def detach(self):
return self.as_subclass(torch.Tensor).detach()
class CustomParameter(torch.nn.Parameter):
def detach(self):
return self.as_subclass(torch.Tensor).detach()
model = tiny_model()
if kind == 'parameter':
model.lm_head.weight = CustomParameter(model.lm_head.weight.detach())
else:
model.register_buffer('custom_buffer', CustomTensor(torch.ones(1)))
# A state-only check would accept these tensors after detaching them.
assert all(type(value) is torch.Tensor for value in model.state_dict().values())
state_dict = MagicMock(side_effect=AssertionError('subclass reached state_dict'))
monkeypatch.setattr(torch.nn.Module, 'state_dict', state_dict)
assert export.plan_native_export(model, SHARD_SIZE) is None
state_dict.assert_not_called()
@pytest.mark.parametrize('corruption', ['keys', 'shape', 'dtype'])
def test_verify_rejects_corrupt_shard(tmp_path, monkeypatch, corruption):
model = tiny_model()
plan = export.plan_native_export(model, SHARD_SIZE)
real_save = export.save_file
def corrupt(tensors, path, metadata):
if corruption == 'keys':
tensors = {'wrong': torch.ones(1)}
else:
name = next(iter(tensors))
tensors[name] = (torch.ones(1) if corruption == 'shape'
else tensors[name].to(torch.float64))
real_save(tensors, path, metadata=metadata)
monkeypatch.setattr(export, 'save_file', corrupt)
destination = tmp_path / 'checkpoint'
with pytest.raises(ValueError, match='incorrect keys|metadata mismatch'):
with atomic_checkpoint_directory(destination) as staging:
export.write_native_checkpoint(model, staging, plan)
assert not destination.exists()
assert not list(tmp_path.glob('.checkpoint.staging-*'))
@pytest.mark.parametrize('native', [False, True])
def test_pipeline_routes_export_and_promotes_before_cleanup(tmp_path, monkeypatch, native):
pipeline = AbliterationPipeline(
'offline-tiny', output_dir=str(tmp_path / 'checkpoint'), method='basic',
)
pipeline._on_log = lambda _message: None
pipeline._on_stage = lambda _event: None
pipeline.log = MagicMock()
model = tiny_model(True)
model.config.quantization_config = {'quant_method': 'obsolete'}
model._obliteratus_dequantized_scheme = 'fp8'
model._weight_conversions = object()
def strip_quantizer(model):
model.hf_quantizer = None
remove_quantization_config = MagicMock(side_effect=strip_quantizer)
model.hf_quantizer = SimpleNamespace(remove_quantization_config=remove_quantization_config)
pipeline.handle = SimpleNamespace(model=model, tokenizer=SimpleNamespace(
save_pretrained=lambda path: (path / 'tokenizer_config.json').write_text('{}'),
))
pipeline._build_metadata = lambda: {'schema': 1}
pipeline._free_gpu_memory = MagicMock()
pipeline._cleanup_offload_dir = MagicMock(side_effect=lambda: (
assert_promoted(pipeline.output_dir)
))
pipeline._gather_state_dict = MagicMock(wraps=pipeline._gather_state_dict)
if not native:
monkeypatch.setattr(export, 'plan_native_export', lambda *args, **kwargs: None)
pipeline._rebirth(max_shard_size=SHARD_SIZE)
assert pipeline._gather_state_dict.call_count == (0 if native else 1)
assert model.hf_quantizer is None
remove_quantization_config.assert_called_once_with(model)
messages = [call.args[0] for call in pipeline.log.call_args_list]
assert sum(message.startswith('Stripping native quantization') for message in messages) == 1
assert sum(message.startswith('Input checkpoint was fp8') for message in messages) == 1
assert not hasattr(model, '_weight_conversions')
config = json.loads((pipeline.output_dir / 'config.json').read_text())
assert 'quantization_config' not in config
pipeline._cleanup_offload_dir.assert_called_once()
metadata = json.loads((pipeline.output_dir / 'abliteration_metadata.json').read_text())
assert metadata == {'schema': 1}
assert not list(tmp_path.glob('.checkpoint.staging-*'))
@pytest.mark.parametrize('failure', ['cancellation', 'capacity'])
def test_pipeline_native_export_failure_preserves_offload(tmp_path, monkeypatch, failure):
pipeline = AbliterationPipeline(
'offline-tiny', output_dir=str(tmp_path / 'checkpoint'), method='basic',
)
pipeline._on_log = lambda _message: None
pipeline._on_stage = lambda _event: None
model = tiny_model()
offload = tmp_path / 'offload'
disk_offload(model, offload)
before = files(offload)
pipeline.handle = SimpleNamespace(
model=model, tokenizer=MagicMock(), _offload_dir=str(offload), _owns_offload_dir=True,
)
pipeline._build_metadata = lambda: {'schema': 1}
pipeline._free_gpu_memory = MagicMock()
pipeline._cleanup_offload_dir = MagicMock(wraps=pipeline._cleanup_offload_dir)
if failure == 'cancellation':
check_cancelled = pipeline._check_cancelled
cancelled_with_shards = []
def cancel_after_first_shard(*args, **kwargs):
# log() also polls cancellation without a stage; cancel at the first
# poll that observes a promoted shard inside the staging directory.
staging = next(tmp_path.glob('.checkpoint.staging-*'), None)
promoted = [] if staging is None else list(staging.glob('*.safetensors'))
if promoted and not cancelled_with_shards:
cancelled_with_shards.append(len(promoted))
pipeline.cancel()
check_cancelled(*args, **kwargs)
monkeypatch.setattr(pipeline, '_check_cancelled', cancel_after_first_shard)
error_type = PipelineCancelledError
else:
monkeypatch.setattr(export.host_resources, 'available_host_bytes', lambda: 0)
error_type = export.NativeExportCapacityError
with pytest.raises(error_type) as caught:
pipeline._rebirth(max_shard_size=SHARD_SIZE)
assert type(caught.value) is error_type
assert caught.value.__cause__ is None
if failure == 'cancellation':
assert cancelled_with_shards == [1]
assert caught.value.stage == 'rebirth'
assert caught.value.metric == 'cancellation'
assert not pipeline.output_dir.exists()
assert not list(tmp_path.glob('.checkpoint.staging-*'))
assert files(offload) == before
assert pipeline.handle._offload_dir == str(offload)
assert pipeline.handle._owns_offload_dir
pipeline._cleanup_offload_dir.assert_not_called()
pipeline._free_gpu_memory.assert_not_called()
pipeline.handle.tokenizer.save_pretrained.assert_not_called()
def test_pipeline_native_export_propagates_structured_failure(tmp_path, monkeypatch):
pipeline = AbliterationPipeline('offline-tiny', method='basic')
pipeline.handle = SimpleNamespace(model=tiny_model(), tokenizer=MagicMock())
failure = PipelineValidationError('invalid export', stage='rebirth', metric='export')
monkeypatch.setattr(export, 'write_native_checkpoint', MagicMock(side_effect=failure))
plan = export.plan_native_export(pipeline.handle.model, SHARD_SIZE)
with pytest.raises(PipelineValidationError) as caught:
with atomic_checkpoint_directory(tmp_path / 'checkpoint') as staging:
pipeline._write_local_checkpoint(staging, '{}', None, export_plan=plan)
assert caught.value is failure
def assert_promoted(path):
assert (path / 'model.safetensors.index.json').is_file()
_RSS_SCRIPT = r'''
import ctypes, gc, json, sys
from pathlib import Path
import torch
from accelerate import disk_offload
from transformers import LlamaConfig, LlamaForCausalLM
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models.checkpoint_export import (
plan_native_export, write_native_checkpoint, native_export_host_demand,
)
from obliteratus.persistence_contracts import atomic_checkpoint_directory
from types import SimpleNamespace
torch.set_num_threads(1)
torch.manual_seed(247)
root = Path(sys.argv[1])
model = LlamaForCausalLM(LlamaConfig(vocab_size=1024, hidden_size=512, intermediate_size=1024,
num_hidden_layers=8, num_attention_heads=8, num_key_value_heads=8))
disk_offload(model, root / 'offload')
plan = plan_native_export(model, 8 * 1024**2)
gc.collect()
ctypes.CDLL(None).malloc_trim(0)
def hwm():
lines = Path('/proc/self/status').read_text().splitlines()
return next(int(line.split()[1]) * 1024 for line in lines if line.startswith('VmHWM:'))
try:
Path('/proc/self/clear_refs').write_text('5')
except OSError:
print(json.dumps({'skip': 'kernel denies VmHWM reset'}))
sys.exit(0)
base = hwm()
with atomic_checkpoint_directory(root / 'checkpoint') as staging:
if sys.argv[2] == 'native':
write_native_checkpoint(model, staging, plan)
else:
state = AbliterationPipeline._gather_state_dict(SimpleNamespace(
handle=SimpleNamespace(model=model), log=lambda message: None))
model.save_pretrained(staging, state_dict=state, max_shard_size=8 * 1024**2,
save_original_format=False)
print(json.dumps({'growth': max(0, hwm() - base), 'total': plan.total_bytes,
'demand': native_export_host_demand(plan)}))
'''
@pytest.mark.skipif(sys.platform != 'linux', reason='Linux VmHWM accounting')
def test_disk_offloaded_subprocess_rss_bound(tmp_path, request):
measurements = {}
for mode in ('native', 'legacy'):
root = tmp_path / mode
root.mkdir()
result = subprocess.run(
[sys.executable, '-c', _RSS_SCRIPT, str(root), mode],
capture_output=True, text=True, check=True, timeout=180,
env={**os.environ, 'HF_HUB_OFFLINE': '1', 'TRANSFORMERS_OFFLINE': '1',
'OMP_NUM_THREADS': '1', 'MALLOC_ARENA_MAX': '1'},
)
measurement = json.loads(result.stdout.splitlines()[-1])
if 'skip' in measurement:
pytest.skip(measurement['skip'])
measurements[mode] = measurement
native, legacy = measurements['native'], measurements['legacy']
for mode, measurement in measurements.items():
for key, value in measurement.items():
request.node.user_properties.append((f'rss_{mode}_{key}', value))
bound = native['demand'] + 12 * 1024**2
assert bound < native['total'] * 0.6, measurements
assert native['growth'] <= bound, measurements
assert legacy['growth'] > bound, measurements
def test_materialization_detects_changed_tensor(tmp_path):
model = tiny_model()
plan = export.plan_native_export(model, SHARD_SIZE)
model.model.embed_tokens.weight = torch.nn.Parameter(torch.ones(64, 16))
with pytest.raises(ValueError, match='no longer matches plan'):
with atomic_checkpoint_directory(tmp_path / 'checkpoint') as staging:
export.write_native_checkpoint(model, staging, plan)
assert not (tmp_path / 'checkpoint').exists()
assert not list(tmp_path.glob('.checkpoint.staging-*'))