mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
1190 lines
54 KiB
Python
1190 lines
54 KiB
Python
"""Metadata-only load admission, with no accelerator or network dependencies."""
|
|
|
|
import logging
|
|
import json
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, GPT2Config, GPT2LMHeadModel, LlamaConfig
|
|
|
|
from obliteratus.models import _load_admission, loader
|
|
from obliteratus.phase_admission import (
|
|
MemoryObservation, PhaseAdmissionError, admit_model_load,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def config():
|
|
return GPT2Config(
|
|
n_embd=8, n_head=2, n_layer=1, n_positions=16, vocab_size=32,
|
|
bos_token_id=0, eos_token_id=1,
|
|
)
|
|
|
|
|
|
def roomy(device):
|
|
return MemoryObservation(device, 100 * 1024 ** 3, 100 * 1024 ** 3)
|
|
|
|
|
|
@pytest.mark.parametrize("destination", ["cuda", torch.device("cuda")], ids=["string", "device"])
|
|
def test_explicit_unindexed_cuda_map_uses_current_device(monkeypatch, destination):
|
|
config = LlamaConfig(hidden_size=64, intermediate_size=128, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=2, vocab_size=128)
|
|
monkeypatch.setattr(torch.cuda, "current_device", lambda: 1)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
placement = {"": destination}
|
|
seen = []
|
|
|
|
def observe(device):
|
|
seen.append(device)
|
|
return MemoryObservation(device, 0 if device == "cuda:0" else 2**30, 2**30)
|
|
|
|
report = admit_model_load(
|
|
config, device="cuda:1", device_map=placement, observer=observe,
|
|
safety_reserve_bytes=0, mode="default",
|
|
)
|
|
assert report.demands["cuda:1"].weight_bytes > 0
|
|
assert "cuda:0" not in report.demands and "cuda:0" not in seen
|
|
assert placement == {"": destination}
|
|
# The same destination must reject if current GPU 1, rather than GPU 0, is full.
|
|
with pytest.raises(PhaseAdmissionError, match="cuda:1"):
|
|
admit_model_load(
|
|
config, device="cuda:1", device_map=placement, mode="default",
|
|
observer=lambda d: MemoryObservation(d, 0 if d == "cuda:1" else 2**30, 2**30),
|
|
safety_reserve_bytes=0,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("destination", [0, "cuda:0", torch.device("cuda:0")],
|
|
ids=["integer", "string", "device"])
|
|
def test_indexed_cuda_map_does_not_consult_current_device(config, monkeypatch, destination):
|
|
current = Mock(side_effect=AssertionError("indexed destinations must stay indexed"))
|
|
monkeypatch.setattr(torch.cuda, "current_device", current)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
report = admit_model_load(
|
|
config, device="cuda:1", device_map={"": destination}, observer=roomy,
|
|
)
|
|
assert report.demands["cuda:0"].weight_bytes > 0
|
|
assert "cuda:1" not in report.demands
|
|
current.assert_not_called()
|
|
|
|
|
|
def test_implicit_cuda_load_keeps_loader_cli_gpu_zero_policy(config, monkeypatch):
|
|
monkeypatch.setattr(torch.cuda, "current_device", lambda: 1)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
report = admit_model_load(config, device="cuda", observer=roomy)
|
|
assert report.demands["cuda:0"].weight_bytes > 0
|
|
assert "cuda:1" not in report.demands
|
|
|
|
|
|
def test_standalone_cpu_config_preserves_rng_and_never_loads_weights(config, monkeypatch):
|
|
load = Mock(side_effect=AssertionError("weight loading must not start"))
|
|
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", load)
|
|
state = torch.random.get_rng_state().clone()
|
|
before = config.to_dict()
|
|
report = admit_model_load(config, device="cpu", observer=roomy)
|
|
assert report.phase == "summon"
|
|
assert set(report.demands) == {"cpu"}
|
|
assert report.demands["cpu"].weight_bytes > 0
|
|
assert 0 < report.demands["cpu"].staging_bytes < report.demands["cpu"].weight_bytes
|
|
assert torch.equal(state, torch.random.get_rng_state())
|
|
assert config.to_dict() == before
|
|
load.assert_not_called()
|
|
|
|
|
|
def test_standalone_local_config_and_concrete_class(config, tmp_path):
|
|
config.save_pretrained(tmp_path)
|
|
report = admit_model_load(
|
|
tmp_path, device="cpu", dtype=torch.float16, observer=roomy,
|
|
model_class=GPT2LMHeadModel, local_files_only=True,
|
|
)
|
|
assert report.demands["cpu"].weight_bytes > 0
|
|
|
|
|
|
def test_cpu_shortfall_reports_before_any_weight_load(config):
|
|
with pytest.raises(PhaseAdmissionError, match="cpu.*shortfall") as caught:
|
|
admit_model_load(
|
|
config, device="cpu", observer=lambda device: MemoryObservation(device, 1, 1),
|
|
mode="strict",
|
|
)
|
|
assert caught.value.phase == "summon"
|
|
assert caught.value.required_bytes > caught.value.available_bytes
|
|
|
|
|
|
def test_explicit_multi_gpu_map_uses_each_local_budget_without_cuda(config):
|
|
placement = {"transformer.wte": 0, "transformer": 1, "lm_head": 0}
|
|
|
|
def busy(device):
|
|
if device == "cuda:0":
|
|
return MemoryObservation(device, 0, 100 * 1024 ** 3)
|
|
return roomy(device)
|
|
|
|
with pytest.raises(PhaseAdmissionError, match="cuda:0.*shortfall"):
|
|
admit_model_load(
|
|
config, device="cuda:1", device_map=placement, observer=busy, mode="strict",
|
|
)
|
|
report = admit_model_load(config, device="cuda:1", device_map=placement, observer=roomy)
|
|
assert set(report.demands) == {"cpu", "cuda:0", "cuda:1"}
|
|
|
|
|
|
def test_tied_embedding_and_output_aliases_charge_each_destination(config):
|
|
together = admit_model_load(
|
|
config, device="cuda:0", observer=roomy,
|
|
device_map={"transformer.wte": 0, "transformer": 1, "lm_head": 0},
|
|
)
|
|
split = admit_model_load(
|
|
config, device="cuda:0", observer=roomy,
|
|
device_map={"transformer.wte": 0, "transformer": 1, "lm_head": 1},
|
|
)
|
|
assert split.demands["cuda:0"].weight_bytes == together.demands["cuda:0"].weight_bytes
|
|
assert (
|
|
split.demands["cuda:1"].weight_bytes - together.demands["cuda:1"].weight_bytes
|
|
== config.vocab_size * config.n_embd * 4
|
|
)
|
|
|
|
|
|
def test_auto_map_delegates_to_transformers_planner(config, monkeypatch):
|
|
import transformers.modeling_utils
|
|
|
|
calls = []
|
|
|
|
def planner(model, device_map, max_memory, hf_quantizer):
|
|
calls.append((model, device_map, max_memory, hf_quantizer))
|
|
return {"": 1}
|
|
|
|
monkeypatch.setattr(transformers.modeling_utils, "_get_device_map", planner)
|
|
report = admit_model_load(
|
|
config, device="cuda:0", device_map="auto", max_memory={1: "10GiB"},
|
|
observer=roomy,
|
|
)
|
|
assert calls[0][1:] == ("auto", {1: "10GiB"}, None)
|
|
assert all(value.device.type == "meta" for value in calls[0][0].parameters())
|
|
assert report.demands["cuda:1"].weight_bytes > 0
|
|
|
|
|
|
def test_planning_hides_only_duplicate_announcement_from_own_thread(config, monkeypatch, caplog):
|
|
import transformers.modeling_utils
|
|
|
|
logger = logging.getLogger("accelerate.utils.modeling")
|
|
message = "We will use 90% of the memory on device 0 for storing the model"
|
|
before_filters = list(logger.filters)
|
|
|
|
def planner(**kwargs):
|
|
logger.info(message)
|
|
other_thread = logger.makeRecord(logger.name, logging.INFO, "", 0, message, (), None)
|
|
other_thread.thread += 1
|
|
logger.handle(other_thread)
|
|
logger.info("unrelated info")
|
|
return {"": "cpu"}
|
|
|
|
monkeypatch.setattr(transformers.modeling_utils, "_get_device_map", planner)
|
|
with caplog.at_level(logging.INFO, logger=logger.name):
|
|
admit_model_load(config, device="cpu", device_map="auto", observer=roomy)
|
|
logger.info(message)
|
|
assert caplog.messages.count(message) == 2
|
|
assert "unrelated info" in caplog.messages
|
|
assert logger.filters == before_filters
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["moe", "hybrid"])
|
|
def test_real_moe_and_hybrid_meta_models_admit_and_preserve_rng(kind):
|
|
import transformers
|
|
|
|
geometry = dict(
|
|
hidden_size=16, intermediate_size=32, num_hidden_layers=2,
|
|
num_attention_heads=2, num_key_value_heads=1, vocab_size=32,
|
|
bos_token_id=0, eos_token_id=1,
|
|
)
|
|
if kind == "moe":
|
|
config = transformers.MixtralConfig(
|
|
**geometry, num_local_experts=4, num_experts_per_tok=2,
|
|
)
|
|
else:
|
|
config_class = getattr(transformers, "Qwen3_5TextConfig", None)
|
|
if config_class is None:
|
|
pytest.skip("installed Transformers predates Qwen3.5 config support")
|
|
config = config_class(
|
|
**geometry, head_dim=8, linear_num_key_heads=2, linear_num_value_heads=2,
|
|
linear_key_head_dim=8, linear_value_head_dim=8, linear_conv_kernel_dim=4,
|
|
layer_types=["linear_attention", "full_attention"],
|
|
)
|
|
before = torch.random.get_rng_state().clone()
|
|
report = admit_model_load(config, device="cuda:0", device_map={"": 0}, observer=roomy)
|
|
assert report.demands["cuda:0"].weight_bytes > 0
|
|
assert torch.equal(before, torch.random.get_rng_state())
|
|
if kind == "moe":
|
|
assert report.demands["cuda:0"].weight_bytes >= 2 * 4 * 3 * 16 * 32 * 4
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kwargs, message",
|
|
[
|
|
({"device_map": {"nonexistent": 0}}, "no device for tensor"),
|
|
({"device_map": "balanced"}, "unsupported load device map"),
|
|
({"device_map": {"": "xpu"}}, "does not model accelerator"),
|
|
({"model_class": SimpleNamespace(
|
|
from_config=Mock(side_effect=ValueError("custom layout")),
|
|
)}, "custom layout"),
|
|
],
|
|
)
|
|
def test_unknown_metadata_or_placement_fails_explicitly(config, kwargs, message):
|
|
report = admit_model_load(config, device="cpu", observer=roomy, **kwargs)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match=message):
|
|
admit_model_load(config, device="cpu", observer=roomy, **kwargs, mode="strict")
|
|
|
|
|
|
def test_no_placement_params_add_cpu_accounting_without_mutating_loader_map(monkeypatch):
|
|
class Engram(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.weight = torch.nn.Parameter(torch.empty(8, device="meta"))
|
|
self.register_buffer("weight_scale_inv", torch.empty(8, device="meta"))
|
|
|
|
class Model(torch.nn.Module):
|
|
_no_split_modules = ["Engram"]
|
|
_no_placement_params = ["weight", "weight_scale_inv"]
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.tables = torch.nn.ModuleDict({"1": Engram()})
|
|
|
|
model = Model()
|
|
loader_map = {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
fallback = Mock(return_value="cpu")
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: fallback)
|
|
|
|
accounting = _load_admission._accounting_placement(model, loader_map)
|
|
|
|
assert loader_map == {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
assert accounting == {
|
|
_load_admission._ADMISSION_FALLBACK_KEY: "cpu",
|
|
"tables.1.weight": "cpu",
|
|
"tables.1.weight_scale_inv": "cpu",
|
|
}
|
|
assert fallback.call_args_list == [
|
|
((loader_map, "tables.1.weight"), {}),
|
|
((loader_map, "tables.1.weight_scale_inv"), {}),
|
|
]
|
|
assert not any(hasattr(module, "_hf_hook") for module in model.modules())
|
|
|
|
|
|
def test_no_placement_params_charge_cpu_weights_and_staging(monkeypatch):
|
|
class Engram(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.weight = torch.nn.Parameter(torch.empty(8, device="meta"))
|
|
|
|
class Model(torch.nn.Module):
|
|
_no_split_modules = ["Engram"]
|
|
_no_placement_params = ["weight"]
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.tables = torch.nn.ModuleDict({"1": Engram()})
|
|
|
|
model = Model()
|
|
planned = {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: model)
|
|
monkeypatch.setattr(_load_admission, "_plan_map", lambda *args: planned)
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: lambda *_: "cpu")
|
|
|
|
report = admit_model_load(
|
|
SimpleNamespace(dtype=torch.float32), device="cpu", observer=roomy, model_class=Model,
|
|
)
|
|
|
|
expected = model.tables["1"].weight.numel() * model.tables["1"].weight.element_size()
|
|
assert report.demands["cpu"].weight_bytes >= expected
|
|
assert report.demands["cpu"].staging_bytes >= expected
|
|
assert planned == {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
assert not any(hasattr(module, "_hf_hook") for module in model.modules())
|
|
|
|
|
|
def test_no_placement_params_respect_explicit_and_ancestor_maps(monkeypatch):
|
|
class Engram(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.weight = torch.nn.Parameter(torch.empty(1, device="meta"))
|
|
|
|
class Model(torch.nn.Module):
|
|
_no_split_modules = ["Engram"]
|
|
_no_placement_params = ["weight"]
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.tables = torch.nn.ModuleDict({"1": Engram()})
|
|
|
|
fallback = Mock(side_effect=AssertionError("explicit placement must win"))
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: fallback)
|
|
model = Model()
|
|
for placement in ({"": "disk"}, {"tables.1": "cuda:0"}, {"tables.1.weight": "cpu"}):
|
|
assert _load_admission._accounting_placement(model, placement) == placement
|
|
fallback.assert_not_called()
|
|
|
|
|
|
def test_no_placement_capability_probe_uses_nonempty_unrelated_map(monkeypatch):
|
|
from transformers.integrations import accelerate
|
|
|
|
def infer_auto_device_map(*, no_placement_params=None):
|
|
return no_placement_params
|
|
|
|
def get_device(device_map, name):
|
|
if not device_map:
|
|
raise KeyError("")
|
|
assert device_map == {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
assert name == _load_admission._ADMISSION_FALLBACK_PROBE
|
|
return "cpu"
|
|
|
|
monkeypatch.setattr(accelerate, "infer_auto_device_map", infer_auto_device_map)
|
|
monkeypatch.setattr(accelerate, "get_device", get_device)
|
|
assert _load_admission._no_placement_get_device() is get_device
|
|
|
|
|
|
def test_no_placement_params_rejects_unrelated_or_unsupported_omissions(monkeypatch):
|
|
class Engram(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.weight = torch.nn.Parameter(torch.empty(1, device="meta"))
|
|
self.nested = torch.nn.Linear(1, 1, bias=False, device="meta")
|
|
|
|
class Ordinary(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.weight = torch.nn.Parameter(torch.empty(1, device="meta"))
|
|
|
|
class Model(torch.nn.Module):
|
|
_no_split_modules = ["Engram"]
|
|
_no_placement_params = ["nested.weight"]
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.tables = torch.nn.ModuleDict({"1": Engram()})
|
|
self.typo = Ordinary()
|
|
|
|
model = Model()
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: lambda *_: "cpu")
|
|
placement = {_load_admission._ADMISSION_FALLBACK_KEY: "cpu"}
|
|
assert _load_admission._accounting_placement(model, {}) == {}
|
|
accounting = _load_admission._accounting_placement(model, placement)
|
|
assert accounting == {
|
|
_load_admission._ADMISSION_FALLBACK_KEY: "cpu",
|
|
"tables.1.nested.weight": "cpu",
|
|
}
|
|
with pytest.raises(ValueError, match="no device for tensor 'tables.1.weight'"):
|
|
_load_admission._tensor_device("tables.1.weight", accounting)
|
|
with pytest.raises(ValueError, match="no device for tensor 'typo.weight'"):
|
|
_load_admission._tensor_device("typo.weight", accounting)
|
|
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: None)
|
|
unsupported = _load_admission._accounting_placement(model, placement)
|
|
assert unsupported == placement
|
|
with pytest.raises(ValueError, match="no device for tensor 'tables.1.nested.weight'"):
|
|
_load_admission._tensor_device("tables.1.nested.weight", unsupported)
|
|
|
|
monkeypatch.setattr(_load_admission, "_no_placement_get_device", lambda: lambda *_: "cuda:0")
|
|
non_cpu = _load_admission._accounting_placement(model, placement)
|
|
assert non_cpu == placement
|
|
|
|
|
|
@pytest.mark.parametrize("dtype, quantization", [("int8", None), ("float32", "2bit")])
|
|
def test_invalid_load_contract(config, dtype, quantization):
|
|
report = admit_model_load(config, device="cpu", dtype=dtype, quantization=quantization)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'summon'"):
|
|
admit_model_load(
|
|
config, device="cpu", dtype=dtype, quantization=quantization, mode="strict",
|
|
)
|
|
|
|
|
|
def test_quantized_storage_includes_metadata_and_float_staging(config, monkeypatch):
|
|
config.n_embd = 128
|
|
config.vocab_size = 1024
|
|
def width(model, name, tensor):
|
|
return 0.5 if name.endswith(".weight") and tensor.ndim == 2 else tensor.element_size()
|
|
|
|
quantizer = SimpleNamespace(
|
|
param_element_size=width, update_weight_conversions=lambda mappings: mappings,
|
|
)
|
|
monkeypatch.setattr(_load_admission, "_make_quantizer", lambda *args: quantizer)
|
|
report = admit_model_load(
|
|
config, device="cuda:0", quantization="4bit", observer=roomy,
|
|
)
|
|
full = _load_admission._make_meta_model(config, AutoModelForCausalLM, torch.float32, False)
|
|
elements = sum(value.numel() for value in full.parameters())
|
|
assert report.demands["cuda:0"].weight_bytes >= elements * 0.5
|
|
assert report.demands["cuda:0"].weight_bytes < elements * 4
|
|
assert report.demands["cuda:0"].staging_bytes >= 2 * config.vocab_size * config.n_embd * 4
|
|
|
|
|
|
def test_dequant_admits_target_gpu_before_host_conversion(config, tmp_path):
|
|
from safetensors.torch import save_file
|
|
|
|
save_file({"weight": torch.zeros(8, 16, dtype=torch.uint8)}, tmp_path / "model.safetensors")
|
|
|
|
def observer(device):
|
|
return MemoryObservation(device, 0, 1000) if device == "cuda:1" else roomy(device)
|
|
|
|
with pytest.raises(PhaseAdmissionError, match="cuda:1.*shortfall"):
|
|
admit_model_load(
|
|
config, device="cuda:1", observer=observer, dequantize=True,
|
|
checkpoint_path=tmp_path, mode="strict",
|
|
)
|
|
|
|
|
|
def test_unknown_native_quantization_rejects_specifically(config):
|
|
config.dtype = torch.float32
|
|
config.quantization_config = {"quant_method": "test-native"}
|
|
message = "native quantization 'test-native'.*OBLITERATUS_PHASE_ADMISSION=off"
|
|
report = admit_model_load(config, device="cuda:0", observer=roomy)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match=message):
|
|
admit_model_load(config, device="cuda:0", observer=roomy, mode="strict")
|
|
|
|
|
|
def test_native_mxfp4_rejects_unmodelled_kernel_storage_before_kernel_lookup(config, monkeypatch):
|
|
from transformers.integrations import hub_kernels
|
|
|
|
config.dtype = torch.bfloat16
|
|
config.quantization_config = {"quant_method": "mxfp4"}
|
|
lookup = Mock(side_effect=AssertionError("kernel lookup must not run"))
|
|
monkeypatch.setattr(hub_kernels, "get_kernel", lookup)
|
|
report = admit_model_load(config, device="cuda:0", observer=roomy)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="native MXFP4 kernel swizzle storage/scales"):
|
|
admit_model_load(config, device="cuda:0", observer=roomy, mode="strict")
|
|
lookup.assert_not_called()
|
|
|
|
|
|
def test_explicitly_dequantized_gpt_oss_models_without_kernel_lookup(monkeypatch):
|
|
from transformers import GptOssConfig, Mxfp4Config
|
|
from transformers.integrations import hub_kernels
|
|
|
|
config = GptOssConfig(
|
|
hidden_size=64, intermediate_size=128, num_hidden_layers=1,
|
|
num_attention_heads=4, num_key_value_heads=2, num_local_experts=4,
|
|
num_experts_per_tok=2, vocab_size=32, dtype="bfloat16",
|
|
)
|
|
config.quantization_config = Mxfp4Config(dequantize=True)
|
|
lookup = Mock(side_effect=AssertionError("kernel lookup must not run"))
|
|
monkeypatch.setattr(hub_kernels, "get_kernel", lookup)
|
|
report = admit_model_load(config, device="cuda:0", dtype="bfloat16", observer=roomy)
|
|
assert report.demands["cuda:0"].weight_bytes > 0
|
|
lookup.assert_not_called()
|
|
|
|
|
|
def test_dequant_headers_include_checkpoint_only_fused_tensor(config, tmp_path):
|
|
from safetensors.torch import save_file
|
|
|
|
save_file({"mtp.experts.gate_up_proj": torch.zeros(8, 16, 32, dtype=torch.uint8)},
|
|
tmp_path / "model.safetensors")
|
|
report = admit_model_load(
|
|
config, device="cpu", observer=roomy, dequantize=True, checkpoint_path=tmp_path,
|
|
)
|
|
# Packed nibbles can expand to two FP32 values; fused tensor counts in full.
|
|
assert report.demands["cpu"].additional_bytes >= 8 * 16 * 32 * 8 * 5
|
|
|
|
|
|
def test_dequant_missing_or_malformed_local_metadata_fails(config, tmp_path):
|
|
report = admit_model_load(
|
|
config, device="cpu", observer=roomy, dequantize=True, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="requires safetensors"):
|
|
admit_model_load(
|
|
config, device="cpu", observer=roomy, dequantize=True, checkpoint_path=tmp_path,
|
|
mode="strict",
|
|
)
|
|
(tmp_path / "model.safetensors").write_bytes(b"bad header")
|
|
report = admit_model_load(
|
|
config, device="cpu", observer=roomy, dequantize=True, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="invalid safetensors header"):
|
|
admit_model_load(
|
|
config, device="cpu", observer=roomy, dequantize=True, checkpoint_path=tmp_path,
|
|
mode="strict",
|
|
)
|
|
|
|
|
|
def test_load_escape_hatch_skips_unknown_metadata_and_observation(monkeypatch, caplog):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
|
|
observer = Mock(side_effect=AssertionError("must not observe"))
|
|
report = admit_model_load(object(), observer=observer)
|
|
assert report.bypassed
|
|
observer.assert_not_called()
|
|
assert "ADMISSION DISABLED" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize("dequantize", [False, True])
|
|
@pytest.mark.parametrize("ceilings", [None, {"cpu": 1}])
|
|
def test_loader_rejects_before_weight_or_dequant_work(config, monkeypatch, dequantize, ceilings):
|
|
import obliteratus.phase_admission as admission
|
|
|
|
monkeypatch.delenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raising=False)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=config))
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _: "cpu")
|
|
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 0)
|
|
monkeypatch.setattr(loader, "_hf_token", lambda: None)
|
|
scheme = loader.qd.QuantScheme.NVFP4_MODELOPT if dequantize else loader.qd.QuantScheme.NONE
|
|
monkeypatch.setattr(loader.qd, "detect_quant_scheme", Mock(return_value=SimpleNamespace(
|
|
scheme=scheme,
|
|
)))
|
|
weights = Mock(side_effect=AssertionError("weight work started"))
|
|
convert = Mock(side_effect=AssertionError("dequantization work started"))
|
|
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", weights)
|
|
monkeypatch.setattr(loader.qd, "materialize_dequantized_checkpoint", convert)
|
|
rejection = PhaseAdmissionError("summon", "cpu", 10, 1)
|
|
guard = Mock(side_effect=rejection)
|
|
monkeypatch.setattr(admission, "admit_model_load", guard)
|
|
with pytest.raises(PhaseAdmissionError, match="shortfall"):
|
|
loader.load_model("local/test", device="cpu", phase_admission=ceilings is None,
|
|
device_memory_ceiling_bytes=ceilings, local_files_only=True)
|
|
weights.assert_not_called()
|
|
convert.assert_not_called()
|
|
assert guard.call_args.kwargs.get("dequantize", False) is dequantize
|
|
assert guard.call_args.kwargs["device_memory_ceiling_bytes"] == (ceilings or {})
|
|
|
|
|
|
@pytest.mark.parametrize("ceilings", [None, {"cpu": 1}])
|
|
def test_loader_checks_fresh_memory_before_permission_retry(config, monkeypatch, ceilings):
|
|
import obliteratus.phase_admission as admission
|
|
|
|
monkeypatch.delenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raising=False)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=config))
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _: "cpu")
|
|
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 0)
|
|
monkeypatch.setattr(loader, "_hf_token", lambda: None)
|
|
monkeypatch.setattr(loader.qd, "detect_quant_scheme", Mock(return_value=SimpleNamespace(
|
|
scheme=loader.qd.QuantScheme.NONE,
|
|
)))
|
|
weights = Mock(side_effect=PermissionError("cache unavailable"))
|
|
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", weights)
|
|
guard = Mock(side_effect=[None, PhaseAdmissionError("summon", "cpu", 10, 1)])
|
|
monkeypatch.setattr(admission, "admit_model_load", guard)
|
|
with pytest.raises(PhaseAdmissionError):
|
|
loader.load_model("local/test", device="cpu", phase_admission=ceilings is None,
|
|
device_memory_ceiling_bytes=ceilings, local_files_only=True)
|
|
assert guard.call_count == 2
|
|
assert weights.call_count == 1
|
|
assert all(call.kwargs["device_memory_ceiling_bytes"] == (ceilings or {})
|
|
for call in guard.call_args_list)
|
|
|
|
|
|
@pytest.mark.parametrize("ceiling_source", ["argument", "environment"])
|
|
def test_explicit_cuda_load_checks_ceiling_without_phase_opt_in(monkeypatch, ceiling_source):
|
|
config = LlamaConfig(hidden_size=64, intermediate_size=128, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=2, vocab_size=128)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
monkeypatch.delenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raising=False)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=config))
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda device: device)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 0)
|
|
monkeypatch.setattr(loader, "_hf_token", lambda: None)
|
|
monkeypatch.setattr(loader.qd, "detect_quant_scheme", Mock(return_value=SimpleNamespace(
|
|
scheme=loader.qd.QuantScheme.NONE,
|
|
)))
|
|
weights = Mock(side_effect=AssertionError("weight loading must not start"))
|
|
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", weights)
|
|
ceilings = {"cuda:1": 64 * 1024 ** 2}
|
|
if ceiling_source == "environment":
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", json.dumps(ceilings))
|
|
with pytest.raises(PhaseAdmissionError, match="cuda:1.*shortfall") as caught:
|
|
loader.load_model("local/test", device="cuda:1", skip_snapshot=True, local_files_only=True,
|
|
admission_observer=roomy,
|
|
**({"device_memory_ceiling_bytes": ceilings} if ceiling_source == "argument" else {}))
|
|
assert caught.value.required_bytes > 0
|
|
assert caught.value.available_bytes == 0
|
|
weights.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("size", ["1.5b", "7b"])
|
|
@pytest.mark.parametrize("device", ["cpu", "cuda:0"])
|
|
def test_qwen_bfloat16_realistic_host_headroom_from_config(size, device):
|
|
from transformers import Qwen2Config
|
|
|
|
geometry = dict(
|
|
hidden_size=3584, intermediate_size=18944, num_attention_heads=28,
|
|
num_key_value_heads=4, vocab_size=152064, tie_word_embeddings=False,
|
|
) if size == "7b" else dict(
|
|
hidden_size=1536, intermediate_size=8960, num_attention_heads=12,
|
|
num_key_value_heads=2, vocab_size=151936, tie_word_embeddings=True,
|
|
)
|
|
config = Qwen2Config(**geometry, num_hidden_layers=28, dtype="bfloat16")
|
|
|
|
def realistic(target):
|
|
gib = 16 if device == "cpu" else (21 if target == "cuda:0" else 22)
|
|
return MemoryObservation(target, gib * 1024 ** 3, gib * 1024 ** 3)
|
|
|
|
report = admit_model_load(config, device=device, dtype="bfloat16", observer=realistic)
|
|
weight_gib = report.demands[device].weight_bytes / 1024 ** 3
|
|
assert (14 < weight_gib < 15) if size == "7b" else (2 < weight_gib < 3)
|
|
assert report.demands["cpu"].additional_bytes < (16 if device == "cpu" else 22) * 1024 ** 3
|
|
|
|
|
|
def _header_only_checkpoint(path, entries):
|
|
"""Synthetic metadata represents large shards without allocating weights."""
|
|
encoded = json.dumps(entries).encode()
|
|
path.write_bytes(len(encoded).to_bytes(8, "little") + encoded)
|
|
|
|
|
|
@pytest.fixture
|
|
def metadata_bnb(monkeypatch):
|
|
"""Run HF's actual BNB replacement/quantizer without an optional backend.
|
|
|
|
Only BNB's meta Linear constructors are substituted. Quantizer selection,
|
|
skipped modules, parameter widths and native uint8/int8 dtype handling are
|
|
the installed Transformers implementation.
|
|
"""
|
|
import importlib.metadata
|
|
import sys
|
|
from types import ModuleType
|
|
import transformers.integrations.bitsandbytes as integration
|
|
|
|
class Linear4bit(torch.nn.Linear):
|
|
def __init__(self, in_features, out_features, bias, *args, **kwargs):
|
|
super().__init__(in_features, out_features, bias, device="meta")
|
|
self.requires_grad_(False)
|
|
|
|
class Linear8bitLt(Linear4bit):
|
|
pass
|
|
|
|
bnb = ModuleType("bitsandbytes")
|
|
bnb.nn = SimpleNamespace(Linear4bit=Linear4bit, Linear8bitLt=Linear8bitLt)
|
|
monkeypatch.setitem(sys.modules, "bitsandbytes", bnb)
|
|
monkeypatch.setattr(integration, "bnb", bnb, raising=False)
|
|
real_version = importlib.metadata.version
|
|
monkeypatch.setattr(
|
|
importlib.metadata, "version",
|
|
lambda name: "0.49.2" if name == "bitsandbytes" else real_version(name),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("bits, headroom", [(4, 8), (8, 12)])
|
|
@pytest.mark.parametrize("requested_dtype", ["bfloat16", "float32"])
|
|
def test_native_bnb_seven_billion_model_fits_packed_budget(
|
|
bits, headroom, requested_dtype, metadata_bnb,
|
|
):
|
|
from transformers import BitsAndBytesConfig, Qwen2Config
|
|
|
|
config = Qwen2Config(
|
|
hidden_size=3584, intermediate_size=18944, num_hidden_layers=28,
|
|
num_attention_heads=28, num_key_value_heads=4, vocab_size=152064,
|
|
tie_word_embeddings=False, dtype="bfloat16",
|
|
)
|
|
config.quantization_config = BitsAndBytesConfig(load_in_4bit=bits == 4, load_in_8bit=bits == 8)
|
|
before = config.to_dict()
|
|
rng = torch.random.get_rng_state().clone()
|
|
|
|
def observer(device):
|
|
free = (headroom if device == "cuda:0" else 16) * 1024 ** 3
|
|
return MemoryObservation(device, free, free)
|
|
|
|
report = admit_model_load(config, device="cuda:0", dtype=requested_dtype, observer=observer)
|
|
demand = report.demands["cuda:0"]
|
|
assert 4 * 1024 ** 3 < demand.weight_bytes < (6 if bits == 4 else 9) * 1024 ** 3
|
|
assert demand.additional_bytes < headroom * 1024 ** 3
|
|
assert report.demands["cpu"].additional_bytes < 12 * 1024 ** 3
|
|
assert config.to_dict() == before
|
|
assert torch.equal(torch.random.get_rng_state(), rng)
|
|
|
|
|
|
@pytest.mark.parametrize("checkpoint_dtype, requested_dtype", [
|
|
("bfloat16", "float32"), ("float32", "bfloat16"),
|
|
])
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
def test_native_loader_and_admission_share_checkpoint_dtype_policy(
|
|
checkpoint_dtype, requested_dtype, metadata_bnb, monkeypatch, mode, caplog,
|
|
):
|
|
from transformers import BitsAndBytesConfig, Qwen2Config
|
|
|
|
config = Qwen2Config(
|
|
hidden_size=3584, intermediate_size=18944, num_hidden_layers=28,
|
|
num_attention_heads=28, num_key_value_heads=4, vocab_size=152064,
|
|
tie_word_embeddings=False, dtype=checkpoint_dtype,
|
|
)
|
|
config.quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=config))
|
|
monkeypatch.setattr(loader, "_hf_token", lambda: None)
|
|
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 8)
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _: "cuda:0")
|
|
monkeypatch.setattr(loader.dev, "supports_bfloat16", lambda _: True)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 1)
|
|
monkeypatch.setattr(loader.qd, "detect_quant_scheme", Mock(return_value=SimpleNamespace(
|
|
scheme=loader.qd.QuantScheme.NONE,
|
|
)))
|
|
weights = Mock(side_effect=RuntimeError("weight load reached"))
|
|
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", weights)
|
|
|
|
def observer(device):
|
|
free = (8 if device == "cuda:0" else 16) * 1024 ** 3
|
|
return MemoryObservation(device, free, free)
|
|
|
|
if checkpoint_dtype == "bfloat16" or mode == "default":
|
|
with pytest.raises(RuntimeError, match="^weight load reached$"):
|
|
loader.load_model(
|
|
"offline-native", device="cuda:0", dtype=requested_dtype,
|
|
phase_admission=True, admission_observer=observer, local_files_only=True,
|
|
)
|
|
assert weights.call_args.kwargs["dtype"] == "auto"
|
|
assert "torch_dtype" not in weights.call_args.kwargs
|
|
if checkpoint_dtype == "float32":
|
|
assert "summon" in caplog.text and "cuda:0" in caplog.text
|
|
else:
|
|
with pytest.raises(PhaseAdmissionError, match="cuda:0.*shortfall") as caught:
|
|
loader.load_model(
|
|
"offline-native", device="cuda:0", dtype=requested_dtype,
|
|
phase_admission=True, admission_observer=observer, local_files_only=True,
|
|
)
|
|
assert caught.value.required_bytes > 8 * 1024 ** 3
|
|
weights.assert_not_called()
|
|
|
|
|
|
def test_native_quantizer_dtype_override_precedes_final_meta_geometry(config, monkeypatch):
|
|
config.dtype = torch.float32
|
|
config.quantization_config = {"quant_method": "fp8"}
|
|
quantizer = SimpleNamespace(
|
|
quantization_config=SimpleNamespace(quant_method="fp8", dequantize=False),
|
|
update_dtype=Mock(return_value=torch.float16),
|
|
update_weight_conversions=lambda mappings: mappings,
|
|
param_element_size=lambda model, name, tensor: tensor.element_size(),
|
|
)
|
|
monkeypatch.setattr(_load_admission, "_make_quantizer", lambda *args: quantizer)
|
|
build = Mock(wraps=_load_admission._make_meta_model)
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", build)
|
|
report = admit_model_load(config, device="cuda:0", dtype="bfloat16", observer=roomy)
|
|
assert [call.args[2] for call in build.call_args_list] == [torch.float32, torch.float16]
|
|
quantizer.update_dtype.assert_called_once_with(torch.float32)
|
|
expected = _load_admission._make_meta_model(config, AutoModelForCausalLM, torch.float16, False)
|
|
total = sum(t.numel() * t.element_size() for t in expected.parameters())
|
|
total += sum(t.numel() * t.element_size() for t in expected.buffers())
|
|
assert report.demands["cuda:0"].weight_bytes == total
|
|
assert config.dtype == torch.float32
|
|
|
|
|
|
def test_native_missing_dtype_and_local_headers_rejects_before_work(config, monkeypatch):
|
|
config.quantization_config = {"quant_method": "bitsandbytes", "load_in_4bit": True}
|
|
model = Mock(side_effect=AssertionError("model construction must not begin"))
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", model)
|
|
report = admit_model_load(config, device="cuda:0", observer=roomy)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="no declared dtype or readable local"):
|
|
admit_model_load(config, device="cuda:0", observer=roomy, mode="strict")
|
|
model.assert_not_called()
|
|
|
|
|
|
def test_native_bnb_headers_supply_packed_statistics_exactly(config, tmp_path, metadata_bnb):
|
|
from transformers import BitsAndBytesConfig
|
|
|
|
config.quantization_config = BitsAndBytesConfig(
|
|
load_in_4bit=True, bnb_4bit_use_double_quant=True,
|
|
)
|
|
model = _load_admission._make_meta_model(config, AutoModelForCausalLM, torch.float16, False)
|
|
quantizer = _load_admission._make_quantizer(config, None, torch.float16, model, {"": "cuda:0"})
|
|
tensors = list(model.named_parameters(remove_duplicate=False)) + list(
|
|
model.named_buffers(remove_duplicate=False)
|
|
)
|
|
sizes, quantized = _load_admission._tensor_storage_sizes(model, tensors, quantizer, None)
|
|
entries = {}
|
|
expected = 0
|
|
credited = set()
|
|
for name, tensor in tensors:
|
|
if id(tensor) in credited:
|
|
continue
|
|
credited.add(id(tensor))
|
|
size = tensor.numel() // 2 if name in quantized else tensor.numel() * tensor.element_size()
|
|
entries[name] = {"dtype": "U8" if name in quantized else "F16", "shape": [size],
|
|
"data_offsets": [0, size]}
|
|
expected += size
|
|
if name in quantized:
|
|
entries[name + ".absmax"] = {"dtype": "U8", "shape": [3], "data_offsets": [0, 3]}
|
|
entries[name + ".quant_state.bitsandbytes__nf4"] = {
|
|
"dtype": "U8", "shape": [17], "data_offsets": [0, 17],
|
|
}
|
|
expected += 20
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", entries)
|
|
report = admit_model_load(
|
|
config, device="cuda:0", dtype="float16", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.demands["cuda:0"].weight_bytes == expected
|
|
assert report.demands["cuda:0"].weight_bytes < sum(sizes.values())
|
|
|
|
|
|
@pytest.mark.parametrize("missing", [
|
|
pytest.param("packed weight", id="missing-packed-weight"),
|
|
pytest.param("quantization state", id="missing-quantization-state"),
|
|
])
|
|
def test_native_bnb_missing_quantization_state_fails_closed(config, tmp_path, metadata_bnb, missing):
|
|
from transformers import BitsAndBytesConfig
|
|
|
|
config.dtype = torch.float16
|
|
config.quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
|
model = _load_admission._make_meta_model(config, AutoModelForCausalLM, torch.float16, False)
|
|
quantizer = _load_admission._make_quantizer(config, None, torch.float16, model, {"": "cuda:0"})
|
|
tensors = list(model.named_parameters()) + list(model.named_buffers())
|
|
_, quantized = _load_admission._tensor_storage_sizes(model, tensors, quantizer, None)
|
|
assert quantized
|
|
# Every packed weight must be present to isolate missing statistics. A
|
|
# single weight made coverage depend on iteration order of the name set:
|
|
# some processes rejected another missing weight before inspecting state.
|
|
names = sorted(quantized) if missing == "quantization state" else ["transformer.wte.weight"]
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", {
|
|
name: {"dtype": "U8", "shape": [96], "data_offsets": [0, 96]} for name in names
|
|
})
|
|
report = admit_model_load(config, device="cuda:0", observer=roomy, checkpoint_path=tmp_path)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match=f"native bitsandbytes checkpoint has no {missing}"):
|
|
admit_model_load(
|
|
config, device="cuda:0", observer=roomy, checkpoint_path=tmp_path, mode="strict",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("expand", [False, True])
|
|
def test_actual_fp8_quantizer_uses_backend_selected_meta_layout(expand, monkeypatch, tmp_path):
|
|
from transformers import FineGrainedFP8Config, LlamaConfig
|
|
|
|
config = LlamaConfig(
|
|
hidden_size=128, intermediate_size=256, num_hidden_layers=1,
|
|
num_attention_heads=4, num_key_value_heads=2, vocab_size=32,
|
|
)
|
|
config.quantization_config = FineGrainedFP8Config()
|
|
monkeypatch.setattr(torch.cuda, "is_available", lambda: expand)
|
|
monkeypatch.setattr(torch.cuda, "get_device_capability", lambda: (8, 0))
|
|
model = _load_admission._make_meta_model(config, AutoModelForCausalLM, torch.bfloat16, False)
|
|
_load_admission._make_quantizer(config, None, torch.bfloat16, model, {"": "cuda:0"})
|
|
tensors = dict(model.named_parameters()) | dict(model.named_buffers())
|
|
assert any(t.dtype == torch.float8_e4m3fn for t in tensors.values()) is not expand
|
|
sizes = {name: tensor.numel() * tensor.element_size() for name, tensor in tensors.items()}
|
|
header = {
|
|
name: {"shape": list(tensor.shape), "data_offsets": [0, sizes[name]],
|
|
"dtype": "F8_E4M3" if tensor.dtype == torch.float8_e4m3fn else (
|
|
"F32" if tensor.dtype == torch.float32 else "BF16"
|
|
)}
|
|
for name, tensor in tensors.items()
|
|
}
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", header)
|
|
report = admit_model_load(
|
|
config, device="cuda:0", dtype="bfloat16", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.demands["cuda:0"].weight_bytes == sum(sizes.values())
|
|
assert report.demands["cuda:0"].staging_bytes == (4 if expand else 1) * max(sizes.values())
|
|
assert config.quantization_config.dequantize is False
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["gptq", "awq", "fp8", "compressed-tensors"])
|
|
def test_native_packed_post_quantizer_tensors_replace_float_layout(method, tmp_path, monkeypatch):
|
|
from transformers.quantizers.base import HfQuantizer
|
|
|
|
class Model(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.layer = torch.nn.Linear(
|
|
4096, 4096, bias=False, device="meta", dtype=torch.bfloat16,
|
|
)
|
|
|
|
original = Model()
|
|
config = SimpleNamespace(quantization_config={"quant_method": method})
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: original)
|
|
|
|
def replace(config, quantization, dtype, model, device_map):
|
|
del model.layer.weight
|
|
model.layer.register_buffer(
|
|
"qweight", torch.empty((512, 4096), dtype=torch.int32, device="meta"),
|
|
)
|
|
model.layer.register_buffer(
|
|
"scales", torch.empty((32, 4096), dtype=torch.float16, device="meta"),
|
|
)
|
|
return SimpleNamespace(
|
|
quantization_config=SimpleNamespace(quant_method=method, dequantize=False),
|
|
param_element_size=HfQuantizer.param_element_size.__get__(object()),
|
|
update_weight_conversions=lambda mappings: mappings,
|
|
)
|
|
|
|
monkeypatch.setattr(_load_admission, "_make_quantizer", replace)
|
|
entries = {
|
|
"layer.qweight": {
|
|
"dtype": "I32", "shape": [512, 4096], "data_offsets": [0, 512 * 4096 * 4],
|
|
},
|
|
"layer.scales": {"dtype": "F16", "shape": [32, 4096], "data_offsets": [0, 32 * 4096 * 2]},
|
|
}
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", entries)
|
|
report = admit_model_load(
|
|
config, device="cuda:0", dtype="bfloat16", observer=roomy,
|
|
checkpoint_path=tmp_path, model_class=Model,
|
|
)
|
|
expected = sum(value["data_offsets"][1] for value in entries.values())
|
|
assert report.demands["cuda:0"].weight_bytes == expected
|
|
assert report.demands["cuda:0"].staging_bytes == 512 * 4096 * 4
|
|
|
|
|
|
@pytest.mark.parametrize("checkpoint", ["config", "safetensors", "zip_bin", "legacy_bin"])
|
|
def test_disk_offload_sixty_gib_uses_streaming_host_workspace(checkpoint, tmp_path, monkeypatch):
|
|
class Model(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.layers = torch.nn.ParameterList([
|
|
torch.nn.Parameter(torch.empty(1024 ** 3 // 2, device="meta", dtype=torch.bfloat16))
|
|
for _ in range(60)
|
|
])
|
|
self.embedding = torch.nn.Parameter(
|
|
torch.empty(32, 32, device="meta", dtype=torch.bfloat16),
|
|
)
|
|
self.register_buffer(
|
|
"small_buffer", torch.empty(16, device="meta", dtype=torch.float32),
|
|
)
|
|
|
|
model = Model()
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: model)
|
|
config = SimpleNamespace(dtype="bfloat16")
|
|
placement = {"": "disk", "embedding": "cuda:0"}
|
|
source = dict(model.named_parameters())
|
|
if checkpoint == "safetensors":
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", {
|
|
name: {"dtype": "BF16", "shape": list(tensor.shape),
|
|
"data_offsets": [0, tensor.numel() * 2]}
|
|
for name, tensor in source.items()
|
|
})
|
|
elif checkpoint == "zip_bin":
|
|
torch.save(source, tmp_path / "pytorch_model.bin")
|
|
elif checkpoint == "legacy_bin":
|
|
(tmp_path / "pytorch_model.bin").write_bytes(b"legacy pickle storage")
|
|
|
|
def observer(device):
|
|
assert device in {"cpu", "cuda:0"}
|
|
return MemoryObservation(device, 16 * 1024 ** 3, 16 * 1024 ** 3)
|
|
|
|
kwargs = dict(
|
|
device="cuda:0", dtype="bfloat16", observer=observer,
|
|
checkpoint_path=tmp_path if checkpoint != "config" else None,
|
|
model_class=Model, device_map=placement,
|
|
)
|
|
if checkpoint == "legacy_bin":
|
|
with pytest.raises(PhaseAdmissionError, match="cpu.*shortfall") as caught:
|
|
admit_model_load(config, **kwargs, mode="strict")
|
|
assert caught.value.required_bytes > 60 * 1024 ** 3
|
|
else:
|
|
report = admit_model_load(config, **kwargs)
|
|
assert report.demands["cpu"].weight_bytes == 16 * 4
|
|
assert 2 * 1024 ** 3 <= report.demands["cpu"].staging_bytes < 3 * 1024 ** 3
|
|
assert report.demands["cuda:0"].weight_bytes == 32 * 32 * 2
|
|
# Disk materialization is on CPU during loading. GPU onload happens
|
|
# under the later forward phase's Accelerate-hook staging admission.
|
|
assert report.demands["cuda:0"].staging_bytes == (
|
|
32 * 32 * 2 if checkpoint == "config" else 0
|
|
)
|
|
assert "disk" not in report.demands
|
|
|
|
|
|
def test_disk_fused_expert_conversion_reserves_fused_tensor_not_source_shard(tmp_path, monkeypatch):
|
|
from transformers import conversion_mapping, core_model_loading
|
|
|
|
model = torch.nn.Module()
|
|
model.register_parameter("experts", torch.nn.Parameter(
|
|
torch.empty(3, 1024 ** 3 // 2, device="meta", dtype=torch.bfloat16),
|
|
))
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: model)
|
|
monkeypatch.setattr(conversion_mapping, "get_model_conversion_mapping", lambda *a, **k: [
|
|
core_model_loading.WeightConverter(
|
|
"experts.*.weight", "experts", [core_model_loading.MergeModulelist(dim=0)],
|
|
),
|
|
])
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", {
|
|
f"experts.{i}.weight": {
|
|
"dtype": "BF16", "shape": [1024 ** 3 // 2], "data_offsets": [0, 1024 ** 3],
|
|
}
|
|
for i in range(3)
|
|
})
|
|
report = admit_model_load(
|
|
SimpleNamespace(dtype="bfloat16"), model_class=torch.nn.Module,
|
|
dtype="bfloat16", device="cpu", device_map={"": "disk"},
|
|
observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.demands["cpu"].weight_bytes == 0
|
|
assert report.demands["cpu"].staging_bytes == 6 * 1024 ** 3
|
|
|
|
|
|
def test_safetensors_host_staging_uses_largest_shard_not_total(config, tmp_path, monkeypatch):
|
|
sizes = (3 * 1024 ** 3, 5 * 1024 ** 3)
|
|
model = torch.nn.Module()
|
|
for name, size in zip(("extra_a", "extra_b"), sizes):
|
|
model.register_parameter(name, torch.nn.Parameter(
|
|
torch.empty(size // 2, dtype=torch.bfloat16, device="meta"),
|
|
))
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", lambda *args: model)
|
|
index = {"weight_map": {"extra_a": "a.safetensors", "extra_b": "b.safetensors"}}
|
|
(tmp_path / "model.safetensors.index.json").write_text(json.dumps(index))
|
|
for name, filename, size in zip(index["weight_map"], index["weight_map"].values(), sizes):
|
|
_header_only_checkpoint(tmp_path / filename, {
|
|
name: {"dtype": "BF16", "shape": [size // 2], "data_offsets": [0, size]},
|
|
})
|
|
report = admit_model_load(
|
|
config, device="cuda:0", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
assert report.demands["cpu"].staging_bytes == 2 * max(sizes)
|
|
assert report.demands["cpu"].staging_bytes < sum(sizes) + max(sizes)
|
|
|
|
|
|
def test_same_dtype_cpu_mmap_has_no_second_resident_copy(config, tmp_path):
|
|
from safetensors.torch import save_file
|
|
|
|
config.dtype = torch.bfloat16
|
|
model = GPT2LMHeadModel(config).to(torch.bfloat16)
|
|
# Safetensors omits the tied output alias, matching save_pretrained.
|
|
tensors = dict(model.named_parameters())
|
|
save_file(tensors, tmp_path / "model.safetensors")
|
|
report = admit_model_load(
|
|
config, device="cpu", dtype="bfloat16", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
largest = max(value.numel() * value.element_size() for value in tensors.values())
|
|
assert report.demands["cpu"].staging_bytes == largest
|
|
|
|
|
|
def test_source_dtype_conversion_is_accounted_from_headers(config, tmp_path):
|
|
from safetensors.torch import save_file
|
|
|
|
config.dtype = torch.bfloat16
|
|
model = GPT2LMHeadModel(config).to(torch.float32)
|
|
source = dict(model.named_parameters())
|
|
save_file(source, tmp_path / "model.safetensors")
|
|
report = admit_model_load(
|
|
config, device="cpu", dtype="bfloat16", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
total = sum(value.numel() * value.element_size() for value in source.values())
|
|
largest = max(value.numel() * value.element_size() for value in source.values())
|
|
assert report.demands["cpu"].staging_bytes >= total + largest
|
|
|
|
|
|
def test_legacy_bin_eager_loading_counts_all_shards(config, tmp_path):
|
|
index = {"weight_map": {
|
|
"transformer.wte.weight": "a.bin", "transformer.wpe.weight": "b.bin",
|
|
}}
|
|
(tmp_path / "pytorch_model.bin.index.json").write_text(json.dumps(index))
|
|
(tmp_path / "a.bin").write_bytes(b"legacy storage a")
|
|
(tmp_path / "b.bin").write_bytes(b"legacy storage b")
|
|
report = admit_model_load(
|
|
config, device="cuda:0", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
source = (config.vocab_size + config.n_positions) * config.n_embd * 4
|
|
largest = config.vocab_size * config.n_embd * 4
|
|
assert report.demands["cpu"].staging_bytes == source + largest
|
|
|
|
|
|
@pytest.mark.parametrize("legacy", [False, True])
|
|
def test_renamed_checkpoint_index_without_readable_metadata_rejects(config, tmp_path, legacy):
|
|
filename = "unreadable.bin" if legacy else "missing.safetensors"
|
|
index_name = "pytorch_model.bin.index.json" if legacy else "model.safetensors.index.json"
|
|
(tmp_path / index_name).write_text(json.dumps({
|
|
"weight_map": {"fused.experts.packed": filename},
|
|
}))
|
|
if legacy:
|
|
(tmp_path / filename).write_bytes(b"legacy pickle without readable tensor headers")
|
|
message = "renamed/fused key 'fused.experts.packed'"
|
|
report = admit_model_load(config, device="cpu", observer=roomy, checkpoint_path=tmp_path)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match=message) as caught:
|
|
admit_model_load(
|
|
config, device="cpu", observer=roomy, checkpoint_path=tmp_path, mode="strict",
|
|
)
|
|
assert filename in str(caught.value)
|
|
assert "OBLITERATUS_PHASE_ADMISSION=off" in str(caught.value)
|
|
|
|
|
|
def test_zip_bin_metadata_uses_mmap_and_source_dtype(config, tmp_path, monkeypatch):
|
|
source = {"transformer.wte.weight": torch.zeros(32, 8, dtype=torch.float32)}
|
|
torch.save(source, tmp_path / "pytorch_model.bin")
|
|
real_load = torch.load
|
|
metadata_load = Mock(side_effect=real_load)
|
|
monkeypatch.setattr(torch, "load", metadata_load)
|
|
report = admit_model_load(
|
|
config, device="cpu", dtype="bfloat16", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
assert metadata_load.call_args.kwargs == {
|
|
"map_location": "cpu", "weights_only": True, "mmap": True,
|
|
}
|
|
assert report.demands["cpu"].staging_bytes == 2 * 32 * 8 * 4
|
|
|
|
|
|
def test_eager_safetensors_mount_charges_all_shards(config, tmp_path, monkeypatch):
|
|
import transformers.modeling_utils
|
|
|
|
monkeypatch.setattr(transformers.modeling_utils, "_is_on_hf_mount", lambda _: True)
|
|
index = {"weight_map": {"extra_a": "a.safetensors", "extra_b": "b.safetensors"}}
|
|
(tmp_path / "model.safetensors.index.json").write_text(json.dumps(index))
|
|
for name, filename in index["weight_map"].items():
|
|
_header_only_checkpoint(tmp_path / filename, {
|
|
name: {"dtype": "BF16", "shape": [1024], "data_offsets": [0, 2048]},
|
|
})
|
|
report = admit_model_load(
|
|
config, device="cuda:0", observer=roomy, checkpoint_path=tmp_path,
|
|
)
|
|
# Eager mode reads unexpected tensors too: all source shards plus one raw
|
|
# read buffer, but there is no conversion workspace for unused keys.
|
|
assert report.demands["cpu"].staging_bytes == 3 * 2048
|
|
|
|
|
|
def test_cached_checkpoint_lookup_never_downloads(config, tmp_path, monkeypatch):
|
|
import huggingface_hub
|
|
|
|
_header_only_checkpoint(tmp_path / "model.safetensors", {
|
|
"transformer.wte.weight": {
|
|
"dtype": "BF16", "shape": [32, 8], "data_offsets": [0, 512],
|
|
},
|
|
})
|
|
cached = Mock(side_effect=lambda repo, name, **kwargs: (
|
|
str(tmp_path / name) if name == "model.safetensors" else None
|
|
))
|
|
monkeypatch.setattr(huggingface_hub, "try_to_load_from_cache", cached)
|
|
monkeypatch.setattr(
|
|
huggingface_hub, "hf_hub_download", Mock(side_effect=AssertionError("network")),
|
|
)
|
|
admit_model_load(
|
|
config, device="cuda:0", observer=roomy,
|
|
checkpoint_path="Qwen/offline-fixture", local_files_only=True, revision="test-revision",
|
|
)
|
|
assert cached.call_args.kwargs["revision"] == "test-revision"
|
|
|
|
|
|
def test_mps_load_counts_unified_resident_weights_once(config):
|
|
config.dtype = torch.float32
|
|
report = admit_model_load(config, device="mps", observer=roomy)
|
|
assert report.demands["mps"].weight_bytes > 0
|
|
assert report.demands["cpu"].weight_bytes == 0
|
|
assert report.demands["cpu"].staging_bytes < report.demands["mps"].weight_bytes
|
|
|
|
|
|
def test_legacy_cpu_loader_does_not_assume_mmap_parameter_assignment(config, monkeypatch):
|
|
import transformers
|
|
|
|
monkeypatch.setattr(transformers, "__version__", "4.40.0")
|
|
report = admit_model_load(config, device="cpu", observer=roomy)
|
|
mapped = admit_model_load(config, device="cpu", device_map={"": "cpu"}, observer=roomy)
|
|
assert report.demands["cpu"].staging_bytes > mapped.demands["cpu"].staging_bytes
|
|
|
|
|
|
def test_configuration_read_failure_is_a_summon_admission_error(monkeypatch):
|
|
from obliteratus.models import loader
|
|
|
|
failure = OSError("cached config missing or invalid")
|
|
reader = Mock(side_effect=failure)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", reader)
|
|
report = admit_model_load("offline/config", device="cpu", local_files_only=True)
|
|
assert report.warnings
|
|
assert any(component.bytes is None for component in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'summon'") as caught:
|
|
admit_model_load("offline/config", device="cpu", local_files_only=True, mode="strict")
|
|
assert str(failure) in caught.value.reason
|
|
assert "Check model configuration" in str(caught.value)
|
|
assert reader.call_args.kwargs["local_files_only"] is True
|