Files
OBLITERATUS/tests/test_load_storage_admission.py

328 lines
14 KiB
Python

"""Real local checkpoint loading verifies source aliases and consumed keys."""
import json
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from safetensors.torch import save_file
from transformers import GPT2Config, GPT2LMHeadModel
from transformers import conversion_mapping, core_model_loading, modeling_utils
from obliteratus.models import _load_admission
from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError, admit_model_load
MIB = 1024 ** 2
def _roomy(device):
return MemoryObservation(device, 1024 * MIB, 1024 * MIB)
def _model():
return GPT2LMHeadModel(GPT2Config(
n_embd=64, n_head=2, n_layer=1, n_positions=16, vocab_size=32768,
bos_token_id=0, eos_token_id=1, dtype="float32",
)).eval()
def _write_checkpoint(directory, model, format, *, extra=None, distinct_alias=False):
directory.mkdir(exist_ok=True)
model.config.save_pretrained(directory)
state = dict(model.named_parameters()) if format == "safe" else model.state_dict()
if distinct_alias:
state["lm_head.weight"] = state["transformer.wte.weight"].view_as(model.lm_head.weight)
if extra:
state.update(extra)
if format == "safe":
save_file(state, directory / "model.safetensors")
else:
torch.save(state, directory / "pytorch_model.bin")
def _admit(directory, **kwargs):
return admit_model_load(
directory, device="cpu", dtype="float32", local_files_only=True,
observer=_roomy, safety_reserve_bytes=0, **kwargs,
)
@pytest.mark.parametrize("distinct_alias", [False, True])
def test_tied_zip_storage_matches_safetensors_stage_and_real_local_load(tmp_path, distinct_alias):
model = _model()
safe, zipped = tmp_path / "safe", tmp_path / "zip"
_write_checkpoint(safe, model, "safe")
_write_checkpoint(zipped, model, "zip", distinct_alias=distinct_alias)
safe_report, zip_report = _admit(safe), _admit(zipped)
assert safe_report.demands["cpu"].staging_bytes == 8 * MIB
assert zip_report.demands["cpu"] == safe_report.demands["cpu"]
loaded = GPT2LMHeadModel.from_pretrained(zipped, local_files_only=True, dtype=torch.float32)
assert loaded.transformer.wte.weight.data_ptr() == loaded.lm_head.weight.data_ptr()
tokens = torch.tensor([[1, 2, 3]])
with torch.inference_mode():
torch.testing.assert_close(loaded(tokens).logits, model(tokens).logits)
def test_zip_metadata_records_storage_ranges_for_distinct_views_without_payloads(tmp_path):
source = torch.zeros(16)
path = tmp_path / "pytorch_model.bin"
torch.save({"a": source[:12], "b": source[4:]}, path)
shards, eager, exact = _load_admission._checkpoint_layout(path, GPT2Config(), [], None)
a, b = shards[0]["a"], shards[0]["b"]
assert exact and eager == 0
assert a.storage_key == b.storage_key
assert a.byte_range == (0, 48)
assert b.byte_range == (16, 64)
assert _load_admission._range_bytes([a.byte_range, b.byte_range]) == source.numel() * 4
def test_zip_shared_source_keeps_separate_gpu_destinations(tmp_path):
model = _model()
_write_checkpoint(tmp_path, model, "zip", distinct_alias=True)
placement = {"transformer.wte": "cuda:0", "lm_head": "cuda:1", "transformer": "cpu"}
report = _admit(tmp_path, device_map=placement)
assert report.demands["cuda:0"].weight_bytes == 8 * MIB
assert report.demands["cuda:1"].weight_bytes == 8 * MIB
# One 8 MiB mapped source plus one transfer tensor, not two source aliases.
assert report.demands["cpu"].staging_bytes == 16 * MIB
def busy(device):
return MemoryObservation(device, 1 if device == "cuda:1" else 1024 * MIB, 1024 * MIB)
with pytest.raises(PhaseAdmissionError, match="cuda:1.*shortfall"):
admit_model_load(
tmp_path, device="cpu", device_map=placement, observer=busy,
local_files_only=True, safety_reserve_bytes=0, mode="strict",
)
@pytest.mark.parametrize("format", ["safe", "zip"])
def test_unused_checkpoint_tensor_neither_inflates_admission_nor_materializes(
tmp_path, monkeypatch, format,
):
model = _model()
_write_checkpoint(tmp_path, model, format)
baseline = _admit(tmp_path)
unused = torch.zeros(8 * MIB, dtype=torch.float32) # 32 MiB unused payload
_write_checkpoint(tmp_path, model, format, extra={"unused_checkpoint_blob": unused})
report = _admit(tmp_path)
assert report.demands == baseline.demands
shapes = []
spawn = core_model_loading.spawn_materialize
def record(pool, tensor, *args, **kwargs):
shape = tensor.get_shape() if hasattr(tensor, "get_shape") else tensor.shape
shapes.append(tuple(shape))
return spawn(pool, tensor, *args, **kwargs)
monkeypatch.setattr(core_model_loading, "spawn_materialize", record)
loaded, info = GPT2LMHeadModel.from_pretrained(
tmp_path, local_files_only=True, dtype=torch.float32, output_loading_info=True,
)
assert "unused_checkpoint_blob" in info["unexpected_keys"]
assert tuple(unused.shape) not in shapes
assert tuple(model.transformer.wte.weight.shape) in shapes
tokens = torch.tensor([[1, 2]])
with torch.inference_mode():
torch.testing.assert_close(loaded(tokens).logits, model(tokens).logits)
def test_registered_renaming_consumes_source_under_resolved_target(tmp_path, monkeypatch):
model = _model()
_write_checkpoint(tmp_path, model, "safe")
baseline = _admit(tmp_path)
state = dict(model.named_parameters())
state["old_embedding.weight"] = state.pop("transformer.wte.weight")
save_file(state, tmp_path / "model.safetensors")
original = conversion_mapping.get_model_conversion_mapping
def mappings(*args, **kwargs):
return [core_model_loading.WeightRenaming(
"old_embedding.weight", "transformer.wte.weight",
)] + original(*args, **kwargs)
monkeypatch.setattr(conversion_mapping, "get_model_conversion_mapping", mappings)
monkeypatch.setattr(modeling_utils, "get_model_conversion_mapping", mappings)
report = _admit(tmp_path)
assert report.demands == baseline.demands
loaded = GPT2LMHeadModel.from_pretrained(tmp_path, local_files_only=True, dtype=torch.float32)
torch.testing.assert_close(loaded.transformer.wte.weight, model.transformer.wte.weight)
def test_real_mixtral_source_experts_use_registered_fusion_mapping(tmp_path):
from transformers import MixtralConfig, MixtralForCausalLM
model = MixtralForCausalLM(MixtralConfig(
hidden_size=16, intermediate_size=32, num_hidden_layers=1,
num_attention_heads=2, num_key_value_heads=1, vocab_size=32,
num_local_experts=2, num_experts_per_tok=1,
)).eval()
# HF's native writer emits the per-expert checkpoint keys; execution
# merges them into the current Transformers fused-expert tensors.
model.save_pretrained(tmp_path)
report = _admit(tmp_path)
assert report.demands["cpu"].staging_bytes >= 2 * 2 * 32 * 16 * 4
loaded = MixtralForCausalLM.from_pretrained(
tmp_path, local_files_only=True, dtype=torch.float32,
)
tokens = torch.tensor([[1, 2]])
with torch.inference_mode():
torch.testing.assert_close(loaded(tokens).logits, model(tokens).logits)
def test_unresolved_conversion_target_rejects_before_any_materialization(tmp_path, monkeypatch):
model = _model()
model.config.save_pretrained(tmp_path)
header = json.dumps({"checkpoint.fused": {
"dtype": "F32", "shape": [2, 32768, 64], "data_offsets": [0, 16 * MIB],
}}).encode()
(tmp_path / "model.safetensors").write_bytes(len(header).to_bytes(8, "little") + header)
monkeypatch.setattr(conversion_mapping, "get_model_conversion_mapping", lambda *a, **k: [
core_model_loading.WeightConverter(
"checkpoint.fused", ["transformer.wte.weight", "unknown.destination"],
[core_model_loading.Chunk(dim=0)],
),
])
spawn = Mock(side_effect=AssertionError("materialization must not start"))
monkeypatch.setattr(core_model_loading, "spawn_materialize", spawn)
report = _admit(tmp_path)
assert report.warnings
assert any(component.bytes is None for component in report.components)
with pytest.raises(PhaseAdmissionError, match="unresolved target 'unknown.destination'"):
_admit(tmp_path, mode="strict")
spawn.assert_not_called()
def test_custom_conversion_workspace_rejects_without_executing_operation(tmp_path, monkeypatch):
model = _model()
_write_checkpoint(tmp_path, model, "safe")
class UnknownWorkspace(core_model_loading.ConversionOps):
def convert(self, *args, **kwargs):
raise AssertionError("custom conversion must not execute during metadata planning")
monkeypatch.setattr(conversion_mapping, "get_model_conversion_mapping", lambda *a, **k: [
core_model_loading.WeightConverter(
"transformer.wte.weight", "transformer.wte.weight", [UnknownWorkspace()],
),
])
report = _admit(tmp_path)
assert report.warnings
assert any(component.bytes is None for component in report.components)
with pytest.raises(
PhaseAdmissionError, match="unmodelled operation.*UnknownWorkspace",
) as caught:
_admit(tmp_path, mode="strict")
assert "OBLITERATUS_PHASE_ADMISSION=off" in str(caught.value)
@pytest.mark.parametrize("asynchronous", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
def test_direct_loader_assigns_materialized_storage_without_second_allocation(
tmp_path, monkeypatch, asynchronous, dtype,
):
model = _model().to(dtype)
_write_checkpoint(tmp_path, model, "safe")
monkeypatch.setenv("HF_DEACTIVATE_ASYNC_LOAD", "0" if asynchronous else "1")
materialized, assigned = {}, set()
copy = core_model_loading._materialize_copy
assign = core_model_loading.set_param_for_module
def record_copy(*args, **kwargs):
value = copy(*args, **kwargs)
materialized[value.untyped_storage().data_ptr()] = value.untyped_storage().nbytes()
return value
def record_assignment(loaded, name, value, *args, **kwargs):
pointer = value.untyped_storage().data_ptr()
assert pointer in materialized
assign(loaded, name, value, *args, **kwargs)
parameter = loaded.get_parameter(name)
assert parameter.untyped_storage().data_ptr() == pointer
assigned.add(pointer)
monkeypatch.setattr(core_model_loading, "_materialize_copy", record_copy)
monkeypatch.setattr(core_model_loading, "set_param_for_module", record_assignment)
loaded = GPT2LMHeadModel.from_pretrained(
tmp_path, local_files_only=True, dtype=dtype,
)
assert assigned == set(materialized)
assert sum(materialized.values()) == sum(p.numel() * p.element_size() for p in loaded.parameters())
tokens = torch.tensor([[1, 2]])
with torch.inference_mode():
torch.testing.assert_close(loaded(tokens).logits, model(tokens).logits)
def test_eight_gpu_direct_load_does_not_double_count_largest_weight(tmp_path, monkeypatch):
gib = 1024 ** 3
model = torch.nn.Module()
model.layers = torch.nn.ModuleList([torch.nn.ParameterList([
torch.nn.Parameter(torch.empty(8 * gib, dtype=torch.bfloat16, device="meta"))
for _ in range(8)
]) for _ in range(8)])
model.table = torch.nn.Parameter(torch.empty(192 * gib, dtype=torch.bfloat16, device="meta"))
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: model)
header = {}
offset = 0
for name, tensor in model.named_parameters():
end = offset + tensor.numel() * tensor.element_size()
header[name] = {"dtype": "BF16", "shape": list(tensor.shape), "data_offsets": [offset, end]}
offset = end
data = json.dumps(header).encode()
(tmp_path / "model.safetensors").write_bytes(len(data).to_bytes(8, "little") + data)
placement = {f"layers.{i}": f"cuda:{i}" for i in range(8)} | {"table": "cpu"}
def observer(device):
available = (140 if device.startswith("cuda:") else 4096) * gib
return MemoryObservation(device, available, available)
kwargs = dict(
device="cuda:0", dtype="bfloat16", checkpoint_path=tmp_path,
device_map=placement, observer=observer, safety_reserve_bytes=0, mode="strict",
)
report = admit_model_load(SimpleNamespace(dtype="bfloat16"), **kwargs)
for index in range(8):
demand = report.demands[f"cuda:{index}"]
assert demand.weight_bytes == 128 * gib
assert demand.staging_bytes == 0
assert report.demands["cpu"].weight_bytes == 384 * gib
assert report.demands["cpu"].staging_bytes > 0
# A real resident shortfall must still reject in strict mode.
kwargs["observer"] = lambda device: MemoryObservation(
device, (127 if device.startswith("cuda:") else 4096) * gib, 4096 * gib,
)
with pytest.raises(PhaseAdmissionError, match="cuda:0.*shortfall"):
admit_model_load(SimpleNamespace(dtype="bfloat16"), **kwargs)
@pytest.mark.parametrize("format", ["safe", "zip"])
def test_direct_gpu_transfer_reduction_requires_matching_dtype_and_unique_destination(tmp_path, format):
model = _model()
_write_checkpoint(tmp_path, model, format)
direct = _admit(tmp_path, device_map={"": "cuda:0"})
if format == "safe":
assert direct.demands["cuda:0"].staging_bytes < 8 * MIB
else:
# Both aliases are materialized separately before the final tie.
assert direct.demands["cuda:0"].staging_bytes >= 8 * MIB
cast = admit_model_load(
tmp_path, device="cuda:0", dtype="bfloat16", device_map={"": "cuda:0"},
local_files_only=True, observer=_roomy, safety_reserve_bytes=0,
)
assert cast.demands["cuda:0"].staging_bytes >= 8 * MIB
def test_fused_expert_conversion_keeps_cuda_workspace(tmp_path):
from transformers import MixtralConfig, MixtralForCausalLM
model = MixtralForCausalLM(MixtralConfig(
hidden_size=16, intermediate_size=32, num_hidden_layers=1,
num_attention_heads=2, num_key_value_heads=1, vocab_size=32,
num_local_experts=2, num_experts_per_tok=1,
))
model.save_pretrained(tmp_path)
report = _admit(tmp_path, device_map={"": "cuda:0"})
assert report.demands["cuda:0"].staging_bytes >= 2 * 2 * 32 * 16 * 4