mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
864 lines
37 KiB
Python
864 lines
37 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 AutoConfig, AutoModelForCausalLM, LlamaConfig, LlamaForCausalLM, Qwen2Config, Qwen2ForCausalLM
|
|
|
|
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'])
|
|
@pytest.mark.parametrize('allow_unbacked_meta', [False, True])
|
|
def test_declared_tie_checks_both_endpoints(monkeypatch, offloaded_key, allow_unbacked_meta):
|
|
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, allow_unbacked_meta=allow_unbacked_meta) 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())
|
|
|
|
|
|
def test_finalized_shard_callback_follows_tensor_release(tmp_path, monkeypatch):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
original = export._materialize_tensor
|
|
tracked = []
|
|
staged = []
|
|
finalized = []
|
|
|
|
def materialize(*args):
|
|
tensor = original(*args)
|
|
tracked.append(weakref.ref(tensor))
|
|
return tensor
|
|
|
|
def callback(path, name):
|
|
gc.collect()
|
|
assert path.name == name
|
|
assert path.parent.name.startswith('.native.staging-')
|
|
assert path.is_file()
|
|
assert staged[-1] == 0
|
|
assert all(reference() is None for reference in tracked)
|
|
finalized.append((path, name, path.read_bytes()))
|
|
|
|
monkeypatch.setattr(export, '_materialize_tensor', materialize)
|
|
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(
|
|
model, staging, plan, staged_bytes=staged.append, on_shard_finalized=callback,
|
|
)
|
|
|
|
assert [name for _, name, _ in finalized] == [shard.filename for shard in plan.shards]
|
|
assert all((tmp_path / 'native' / name).read_bytes() == data for _, name, data in finalized)
|
|
|
|
|
|
def test_allocator_trim_precedes_zero_staging_and_finalization(tmp_path, monkeypatch):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
events = []
|
|
monkeypatch.setattr(export, '_trim_allocator', lambda: events.append('trim'))
|
|
|
|
def staged(size):
|
|
if size == 0:
|
|
events.append('zero')
|
|
|
|
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(
|
|
model,
|
|
staging,
|
|
plan,
|
|
staged_bytes=staged,
|
|
on_shard_finalized=lambda *_: events.append('finalized'),
|
|
)
|
|
|
|
assert events == [event for _ in plan.shards for event in ('trim', 'zero', 'finalized')]
|
|
|
|
|
|
def test_allocator_trim_unavailable_preserves_export_and_original_failure(tmp_path, monkeypatch):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
monkeypatch.setattr(export.sys, 'platform', 'linux')
|
|
monkeypatch.setattr(export.ctypes, 'CDLL', MagicMock(side_effect=OSError('no libc')))
|
|
|
|
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(model, staging, plan)
|
|
assert (tmp_path / 'native').is_dir()
|
|
|
|
monkeypatch.setattr(export, 'save_file', MagicMock(side_effect=OSError('save failed')))
|
|
with pytest.raises(OSError, match='save failed'):
|
|
with atomic_checkpoint_directory(tmp_path / 'failed') as staging:
|
|
export.write_native_checkpoint(model, staging, plan)
|
|
assert not (tmp_path / 'failed').exists()
|
|
|
|
|
|
@pytest.mark.parametrize('case', ['non_linux', 'missing_symbol', 'call_failure'])
|
|
def test_allocator_trim_ignores_unsupported_or_failing_runtime(monkeypatch, case):
|
|
cdll = MagicMock()
|
|
monkeypatch.setattr(export.ctypes, 'CDLL', cdll)
|
|
|
|
if case == 'non_linux':
|
|
monkeypatch.setattr(export.sys, 'platform', 'darwin')
|
|
else:
|
|
monkeypatch.setattr(export.sys, 'platform', 'linux')
|
|
if case == 'missing_symbol':
|
|
cdll.return_value = SimpleNamespace()
|
|
else:
|
|
def trim(_size):
|
|
raise OSError('trim failed')
|
|
|
|
cdll.return_value = SimpleNamespace(malloc_trim=trim)
|
|
|
|
export._trim_allocator()
|
|
|
|
if case == 'non_linux':
|
|
cdll.assert_not_called()
|
|
else:
|
|
cdll.assert_called_once_with(None)
|
|
|
|
|
|
@pytest.mark.parametrize('failure', ['save', 'verify', 'rename'])
|
|
def test_failed_shard_never_calls_finalized_callback(tmp_path, monkeypatch, failure):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
finalized = []
|
|
|
|
if failure == 'save':
|
|
monkeypatch.setattr(export, 'save_file', MagicMock(side_effect=OSError('save failed')))
|
|
elif failure == 'verify':
|
|
monkeypatch.setattr(
|
|
export, '_verify_shard', MagicMock(side_effect=ValueError('verify failed')),
|
|
)
|
|
else:
|
|
monkeypatch.setattr(export.os, 'replace', MagicMock(side_effect=OSError('rename failed')))
|
|
|
|
with pytest.raises((OSError, ValueError), match=f'{failure} failed'):
|
|
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(
|
|
model, staging, plan, on_shard_finalized=lambda path, name: finalized.append(name),
|
|
)
|
|
|
|
assert finalized == []
|
|
assert not (tmp_path / 'native').exists()
|
|
|
|
|
|
def test_staged_observer_failure_skips_finalized_callback(tmp_path):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
finalized = []
|
|
|
|
def observer(size):
|
|
if size == 0:
|
|
raise RuntimeError('observer failed')
|
|
|
|
with pytest.raises(RuntimeError, match='observer failed'):
|
|
with atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(
|
|
model,
|
|
staging,
|
|
plan,
|
|
staged_bytes=observer,
|
|
on_shard_finalized=lambda path, name: finalized.append(name),
|
|
)
|
|
|
|
assert finalized == []
|
|
assert not (tmp_path / 'native').exists()
|
|
|
|
|
|
def test_finalized_callback_failure_rolls_back_atomic_destination(tmp_path):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
destination = tmp_path / 'native'
|
|
destination.mkdir()
|
|
(destination / 'existing.txt').write_text('preserve me', encoding='utf-8')
|
|
before = files(destination)
|
|
finalized = []
|
|
|
|
def callback(path, name):
|
|
finalized.append((path, name))
|
|
raise RuntimeError('publication failed')
|
|
|
|
with pytest.raises(RuntimeError, match='publication failed'):
|
|
with atomic_checkpoint_directory(destination) as staging:
|
|
export.write_native_checkpoint(model, staging, plan, on_shard_finalized=callback)
|
|
|
|
assert len(finalized) == 1
|
|
assert files(destination) == before
|
|
assert not list(tmp_path.glob('.native.staging-*'))
|
|
|
|
|
|
def test_finalized_callback_must_be_callable(tmp_path):
|
|
model = tiny_model()
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
|
|
with pytest.raises(TypeError, match='must be callable'):
|
|
export.write_native_checkpoint(model, tmp_path, plan, on_shard_finalized=object())
|
|
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize('model_type', ['gpt2', 'llama', 'qwen2'])
|
|
@pytest.mark.parametrize('tied', [False, True])
|
|
def test_config_only_export_plan_never_materializes_or_writes(tmp_path, monkeypatch, model_type, tied):
|
|
options = dict(vocab_size=64, tie_word_embeddings=tied)
|
|
if model_type == 'gpt2':
|
|
options.update(n_embd=32, n_layer=3, n_head=4, n_positions=128)
|
|
else:
|
|
options.update(hidden_size=32, intermediate_size=64, num_hidden_layers=3,
|
|
num_attention_heads=4, num_key_value_heads=2)
|
|
config = AutoConfig.for_model(model_type, **options)
|
|
with torch.device('meta'):
|
|
model = AutoModelForCausalLM.from_config(config)
|
|
model.tie_weights()
|
|
forbidden = MagicMock(side_effect=AssertionError('metadata planning read 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)
|
|
assert export.plan_native_export(model, SHARD_SIZE) is None
|
|
plan = export.plan_native_export(model, SHARD_SIZE, allow_unbacked_meta=True)
|
|
assert plan is not None and plan.metadata_only
|
|
summary = export.native_export_summary(plan)
|
|
assert json.loads(json.dumps(summary)) == summary
|
|
assert summary['status'] == 'capacity_estimate_only'
|
|
assert summary['runtime_check_required'] is True
|
|
assert summary['host_peak_bytes'] == export.native_export_host_demand(plan)
|
|
assert summary['largest_shard_bytes'] < summary['total_bytes']
|
|
resident = AutoModelForCausalLM.from_config(config)
|
|
resident_plan = export.plan_native_export(resident, SHARD_SIZE)
|
|
assert resident_plan is not None and not resident_plan.metadata_only
|
|
assert plan.tensors == resident_plan.tensors
|
|
assert plan.shards == resident_plan.shards
|
|
assert plan.total_bytes == resident_plan.total_bytes
|
|
with pytest.raises(ValueError, match='config-only.*cannot write'):
|
|
export.write_native_checkpoint(model, tmp_path, plan)
|
|
forbidden.assert_not_called()
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize('offload', ['cpu', 'disk'])
|
|
def test_metadata_opt_in_cannot_admit_whole_model_offloaded_ties(tmp_path, offload):
|
|
model = tiny_model(True)
|
|
if offload == 'cpu':
|
|
cpu_offload(model, execution_device=torch.device('cpu'))
|
|
else:
|
|
disk_offload(model, tmp_path / 'offload')
|
|
assert export.plan_native_export(model, SHARD_SIZE, allow_unbacked_meta=True) is None
|
|
|
|
|
|
def test_split_qwen_export_bounds_peak_cpu_tensor_allocations(tmp_path, monkeypatch):
|
|
from torch.multiprocessing.reductions import StorageWeakRef
|
|
from torch.utils._python_dispatch import TorchDispatchMode
|
|
from torch.utils._pytree import tree_leaves
|
|
|
|
torch.manual_seed(17)
|
|
model = Qwen2ForCausalLM(Qwen2Config(
|
|
vocab_size=64, hidden_size=32, intermediate_size=64,
|
|
num_hidden_layers=6, num_attention_heads=4, num_key_value_heads=2,
|
|
tie_word_embeddings=False,
|
|
))
|
|
# Weak storage identities distinguish existing CPU weights/backing from
|
|
# allocations made by export without retaining the original tensor payloads.
|
|
baseline = {StorageWeakRef(tensor.untyped_storage()) for tensor in model.state_dict().values()}
|
|
cpu_offload(model.model.layers[0], execution_device=torch.device('cpu'))
|
|
disk_offload(model.model.layers[1], tmp_path / 'offload', execution_device=torch.device('cpu'))
|
|
plan = export.plan_native_export(model, SHARD_SIZE)
|
|
assert plan is not None and not plan.metadata_only
|
|
assert any(spec.offload_owner is None for spec in plan.tensors.values())
|
|
assert any(spec.offload_owner is not None for spec in plan.tensors.values())
|
|
# A regression to the pipeline's legacy gather must fail before allocating.
|
|
monkeypatch.setattr(
|
|
AbliterationPipeline, '_gather_state_dict',
|
|
MagicMock(side_effect=AssertionError('whole-state gather is forbidden')),
|
|
)
|
|
|
|
class AllocationPeak(TorchDispatchMode):
|
|
def __init__(self):
|
|
self.storages = {}
|
|
self.peak = 0
|
|
|
|
def observe(self, values):
|
|
for tensor in tree_leaves(values):
|
|
if isinstance(tensor, torch.Tensor) and tensor.device.type == 'cpu':
|
|
storage = tensor.untyped_storage()
|
|
identity = StorageWeakRef(storage)
|
|
if identity not in baseline:
|
|
self.storages[identity] = storage.nbytes()
|
|
self.peak = max(self.peak, sum(
|
|
size for identity, size in self.storages.items() if not identity.expired()
|
|
))
|
|
|
|
def __torch_dispatch__(self, func, types, args=(), kwargs=None):
|
|
self.observe((args, kwargs))
|
|
result = func(*args, **(kwargs or {}))
|
|
self.observe(result)
|
|
return result
|
|
|
|
tracker = AllocationPeak()
|
|
with tracker, atomic_checkpoint_directory(tmp_path / 'native') as staging:
|
|
export.write_native_checkpoint(model, staging, plan)
|
|
bound = export.native_export_host_demand(plan) - export.WORKSPACE_BYTES
|
|
assert 0 < tracker.peak <= bound < plan.total_bytes
|
|
gc.collect()
|
|
assert all(identity.expired() for identity in tracker.storages)
|
|
summary = export.native_export_summary(plan)
|
|
assert summary['status'] == 'eligible' and summary['bounded'] is True
|
|
reloaded = Qwen2ForCausalLM.from_pretrained(tmp_path / 'native', local_files_only=True)
|
|
for name, spec in plan.tensors.items():
|
|
torch.testing.assert_close(
|
|
export._materialize_tensor(model, name, spec), reloaded.get_parameter_or_buffer(name),
|
|
rtol=0, atol=0,
|
|
)
|
|
|
|
|
|
@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)
|
|
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:'))
|
|
def reset_hwm():
|
|
gc.collect()
|
|
ctypes.CDLL(None).malloc_trim(0)
|
|
Path('/proc/self/clear_refs').write_text('5')
|
|
return hwm()
|
|
try:
|
|
reset_hwm()
|
|
except OSError:
|
|
print(json.dumps({'skip': 'kernel denies VmHWM reset'}))
|
|
sys.exit(0)
|
|
# One interpreter measures both modes against the same offloaded model, so the
|
|
# torch/transformers import and the disk offload are paid once. Native runs
|
|
# first so its peak is measured before the legacy path ever materializes the
|
|
# state dict; the high-water mark is reset (after GC and malloc_trim) between
|
|
# modes, so each growth only counts that mode's own allocations.
|
|
measurements = {}
|
|
for mode in sys.argv[2:]:
|
|
(root / mode).mkdir()
|
|
base = reset_hwm()
|
|
with atomic_checkpoint_directory(root / mode / 'checkpoint') as staging:
|
|
if mode == '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)
|
|
del state
|
|
measurements[mode] = {'growth': max(0, hwm() - base), 'total': plan.total_bytes,
|
|
'demand': native_export_host_demand(plan)}
|
|
print(json.dumps(measurements))
|
|
'''
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform != 'linux', reason='Linux VmHWM accounting')
|
|
def test_disk_offloaded_subprocess_rss_bound(tmp_path, request):
|
|
result = subprocess.run(
|
|
[sys.executable, '-c', _RSS_SCRIPT, str(tmp_path), 'native', 'legacy'],
|
|
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'},
|
|
)
|
|
measurements = json.loads(result.stdout.splitlines()[-1])
|
|
if 'skip' in measurements:
|
|
pytest.skip(measurements['skip'])
|
|
assert set(measurements) == {'native', 'legacy'}, measurements
|
|
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-*'))
|