mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Section D of #675: the neuron lab's load() drops the previous handle and its state even when its cleanup fails (the failure is raised and nothing new is loaded); `experiments trace` cleans up its handle on success and failure without masking the trace error; default_model_factory cleans up the handle when .to(device) fails, keeping the move error; a snapshot error raised through a handle names the restore-point path once. Adds tests for the sequence refusal branch and write_step(changed=False), and registers the cleanup-site tests for inputs.py and trace_cli.py in the risk map. Refs #675
2285 lines
95 KiB
Python
2285 lines
95 KiB
Python
"""Offline model-loader contracts at provider, device, and quantization boundaries."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import builtins
|
|
import gc
|
|
import logging
|
|
import os
|
|
import runpy
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
from types import ModuleType, SimpleNamespace
|
|
from unittest.mock import MagicMock, Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.models import loader
|
|
|
|
|
|
def _execute_loader_source() -> dict:
|
|
return runpy.run_path(str(Path(loader.__file__).resolve()), run_name="_loader_contract_probe")
|
|
|
|
|
|
def test_compatibility_shims_tolerate_independent_missing_or_broken_imports(monkeypatch):
|
|
original_import = builtins.__import__
|
|
failure_keys = {
|
|
("transformers", ("AutoModelForImageTextToText",)): ImportError,
|
|
("transformers", ("PreTrainedModel",)): RuntimeError,
|
|
("transformers.utils", ("output_capturing",)): ImportError,
|
|
("transformers.utils.import_utils", ()): RuntimeError,
|
|
("transformers.utils", ()): RuntimeError,
|
|
("transformers.pytorch_utils", ()): RuntimeError,
|
|
("transformers.generation_utils", ()): ModuleNotFoundError,
|
|
("transformers.generation", ()): RuntimeError,
|
|
("transformers.deepspeed", ()): ModuleNotFoundError,
|
|
("transformers.integrations.deepspeed", ()): RuntimeError,
|
|
("transformers.cache_utils", ("DynamicCache",)): RuntimeError,
|
|
("transformers.generation.logits_process", ()): RuntimeError,
|
|
("transformers.processing_utils", ()): RuntimeError,
|
|
("transformers.file_utils", ()): RuntimeError,
|
|
}
|
|
|
|
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
exception = failure_keys.get((name, tuple(fromlist or ())))
|
|
if exception is not None:
|
|
raise exception(f"injected loader compatibility failure for {name}")
|
|
return original_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", rejecting_import)
|
|
namespace = _execute_loader_source()
|
|
|
|
assert namespace["TASK_MODEL_MAP"]
|
|
assert namespace["AutoModelForImageTextToText"] is None
|
|
|
|
|
|
def test_compatibility_shims_tolerate_missing_generic_module(monkeypatch):
|
|
original_import = builtins.__import__
|
|
|
|
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if name == "transformers.utils.generic":
|
|
raise RuntimeError("generic module unavailable")
|
|
return original_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", rejecting_import)
|
|
assert _execute_loader_source()["TASK_MODEL_MAP"]
|
|
|
|
|
|
def test_compatibility_shims_tolerate_top_level_utils_patch_failure(monkeypatch):
|
|
original_import = builtins.__import__
|
|
|
|
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if name == "transformers.utils" and not fromlist:
|
|
raise RuntimeError("top-level utils module unavailable")
|
|
return original_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", rejecting_import)
|
|
assert _execute_loader_source()["TASK_MODEL_MAP"]
|
|
|
|
|
|
def test_dynamic_cache_compatibility_alias_is_installed_when_needed(monkeypatch):
|
|
original_import = builtins.__import__
|
|
|
|
class LegacyDynamicCache:
|
|
def get_max_cache_shape(self):
|
|
return 7
|
|
|
|
cache_module = SimpleNamespace(DynamicCache=LegacyDynamicCache)
|
|
|
|
def cache_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if name == "transformers.cache_utils" and tuple(fromlist or ()) == ("DynamicCache",):
|
|
return cache_module
|
|
return original_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", cache_import)
|
|
assert _execute_loader_source()["TASK_MODEL_MAP"]
|
|
assert LegacyDynamicCache().get_max_length() == 7
|
|
|
|
|
|
def test_compatibility_shims_tolerate_rejected_generic_attribute_patch(monkeypatch):
|
|
import transformers.utils as transformers_utils
|
|
|
|
class RejectWorkingDirectory(ModuleType):
|
|
def __setattr__(self, name, value):
|
|
if name == "working_or_temp_dir":
|
|
raise RuntimeError("read-only compatibility module")
|
|
super().__setattr__(name, value)
|
|
|
|
fake_generic = RejectWorkingDirectory("transformers.utils.generic")
|
|
monkeypatch.setitem(sys.modules, "transformers.utils.generic", fake_generic)
|
|
monkeypatch.setattr(transformers_utils, "generic", fake_generic)
|
|
|
|
assert _execute_loader_source()["TASK_MODEL_MAP"]
|
|
|
|
|
|
def test_all_tied_weights_compat_aggregates_filters_and_caches():
|
|
class LegacyModel(torch.nn.Module):
|
|
pass
|
|
|
|
assert loader._install_all_tied_weights_keys_compat(LegacyModel) is True
|
|
assert loader._install_all_tied_weights_keys_compat(LegacyModel) is False
|
|
|
|
model = LegacyModel()
|
|
model.lm_head = torch.nn.Linear(2, 2)
|
|
model._tied_weights_keys = ["lm_head.weight", "missing.weight"]
|
|
child = torch.nn.Module()
|
|
child._tied_weights_keys = {"lm_head.bias": "lm_head.weight"}
|
|
model.child = child
|
|
|
|
assert model.all_tied_weights_keys == {
|
|
"lm_head.weight": "lm_head.weight",
|
|
"lm_head.bias": "lm_head.weight",
|
|
}
|
|
model.all_tied_weights_keys = {"cached.weight": "source.weight"}
|
|
assert model.all_tied_weights_keys == {"cached.weight": "source.weight"}
|
|
|
|
|
|
def test_all_tied_weights_compat_prefers_transformers_expansion_contract():
|
|
class ExpandableModel(torch.nn.Module):
|
|
def get_expanded_tied_weights_keys(self, *, all_submodels):
|
|
assert all_submodels is True
|
|
return {"lm_head.weight": "model.embed_tokens.weight"}
|
|
|
|
assert loader._install_all_tied_weights_keys_compat(ExpandableModel) is True
|
|
model = ExpandableModel()
|
|
|
|
assert model.all_tied_weights_keys == {
|
|
"lm_head.weight": "model.embed_tokens.weight",
|
|
}
|
|
assert model.__dict__["_all_tied_weights_keys_store"] == {
|
|
"lm_head.weight": "model.embed_tokens.weight",
|
|
}
|
|
|
|
|
|
def test_all_tied_weights_compat_falls_back_when_expansion_is_legacy():
|
|
class LegacyExpandableModel(torch.nn.Module):
|
|
def get_expanded_tied_weights_keys(self, *, all_submodels):
|
|
raise TypeError("legacy list contract")
|
|
|
|
assert loader._install_all_tied_weights_keys_compat(LegacyExpandableModel) is True
|
|
model = LegacyExpandableModel()
|
|
model.lm_head = torch.nn.Linear(2, 2)
|
|
model._tied_weights_keys = ["lm_head.weight"]
|
|
|
|
assert model.all_tied_weights_keys == {"lm_head.weight": "lm_head.weight"}
|
|
|
|
|
|
def test_flash_attention_config_helpers_recurse_into_transformers_subconfigs():
|
|
from transformers import PretrainedConfig
|
|
|
|
outer = PretrainedConfig()
|
|
inner = PretrainedConfig()
|
|
outer.inner = inner
|
|
inner._attn_implementation = "flash_attention_2"
|
|
|
|
assert loader._config_uses_flash_attention_2(outer) is True
|
|
loader._force_eager_config(outer)
|
|
assert inner._attn_implementation == "eager"
|
|
assert loader._config_uses_flash_attention_2(outer) is False
|
|
|
|
|
|
def _config(**overrides):
|
|
values = {
|
|
"model_type": "gpt2",
|
|
"architectures": ["GPT2LMHeadModel"],
|
|
"num_hidden_layers": 2,
|
|
"num_attention_heads": 4,
|
|
"hidden_size": 8,
|
|
"intermediate_size": 16,
|
|
"vocab_size": 32,
|
|
"quantization_config": None,
|
|
}
|
|
values.update(overrides)
|
|
return SimpleNamespace(**values)
|
|
|
|
|
|
def _model():
|
|
model = MagicMock()
|
|
model._obliteratus_surgery_invalid = False
|
|
storage = torch.nn.Module()
|
|
storage.register_parameter("weight", torch.nn.Parameter(torch.ones(2)))
|
|
storage.register_parameter("bias", torch.nn.Parameter(torch.ones(3)))
|
|
model.named_modules.return_value = [("", storage)]
|
|
model.to.return_value = model
|
|
model.state_dict.return_value = {"weight": torch.ones(2)}
|
|
model.parameters.return_value = iter([torch.ones(2), torch.ones(3)])
|
|
return model
|
|
|
|
|
|
@pytest.fixture
|
|
def loader_boundary(monkeypatch):
|
|
monkeypatch.delenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raising=False)
|
|
config = _config()
|
|
model = _model()
|
|
tokenizer = SimpleNamespace(pad_token=None, eos_token="<eos>")
|
|
model_class = SimpleNamespace(from_pretrained=Mock(return_value=model))
|
|
image_text_class = SimpleNamespace(from_pretrained=Mock(return_value=model))
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=config))
|
|
monkeypatch.setattr(loader.AutoTokenizer, "from_pretrained", Mock(return_value=tokenizer))
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "causal_lm", model_class)
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "classification", model_class)
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", image_text_class)
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda preference="auto": "cpu" if preference == "auto" else preference)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: False)
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: False)
|
|
monkeypatch.setattr(loader.dev, "supports_bfloat16", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "get_total_free_gb", lambda: 0.0)
|
|
monkeypatch.setattr(loader.dev, "empty_cache", Mock())
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: False)
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda, "mem_get_info",
|
|
lambda index: (loader.torch.cuda.get_device_properties(index).total_memory,) * 2,
|
|
)
|
|
monkeypatch.setattr(loader.torch.cuda, "memory_reserved", lambda _index: 0)
|
|
monkeypatch.setattr("obliteratus.models.host_resources.available_host_bytes", lambda: 40 * 1024**3)
|
|
return SimpleNamespace(config=config, model=model, tokenizer=tokenizer,
|
|
model_class=model_class, image_text_class=image_text_class)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("kwargs", "message"),
|
|
[
|
|
({"model_name": ""}, "model_name must be"),
|
|
({"model_name": "x", "task": "embedding"}, "Unknown task"),
|
|
({"model_name": "x", "dtype": "int9"}, "Unknown dtype"),
|
|
({"model_name": "x", "quantization": "3bit"}, "Unknown quantization"),
|
|
({"model_name": "x", "gpu_memory_utilization": 0}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": -0.1}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": 1.1}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": float("nan")}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": float("inf")}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": True}, "gpu_memory_utilization"),
|
|
({"model_name": "x", "gpu_memory_utilization": "0.8"}, "gpu_memory_utilization"),
|
|
],
|
|
)
|
|
def test_invalid_requests_fail_before_provider_access(loader_boundary, monkeypatch, kwargs, message):
|
|
config_load = Mock(side_effect=AssertionError("provider should not be called"))
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", config_load)
|
|
with pytest.raises(ValueError, match=message):
|
|
loader.load_model(**kwargs)
|
|
config_load.assert_not_called()
|
|
|
|
|
|
def test_revision_trust_and_offline_flags_reach_every_provider(loader_boundary):
|
|
handle = loader.load_model(
|
|
"local/model",
|
|
revision="deadbeef",
|
|
trust_remote_code=True,
|
|
local_files_only=True,
|
|
skip_snapshot=True,
|
|
)
|
|
expected = {
|
|
"trust_remote_code": True,
|
|
"token": None,
|
|
"revision": "deadbeef",
|
|
"local_files_only": True,
|
|
}
|
|
loader.AutoConfig.from_pretrained.assert_called_once_with("local/model", **expected)
|
|
loader_boundary.model_class.from_pretrained.assert_called_once()
|
|
for key, value in expected.items():
|
|
assert loader_boundary.model_class.from_pretrained.call_args.kwargs[key] == value
|
|
loader.AutoTokenizer.from_pretrained.assert_called_once_with("local/model", **expected)
|
|
assert handle.tokenizer.pad_token == "<eos>"
|
|
loader_boundary.model.eval.assert_called_once_with()
|
|
loader.dev.empty_cache.assert_called_once_with()
|
|
|
|
|
|
def test_config_validator_rejects_before_conversion_and_weights(loader_boundary, monkeypatch):
|
|
def reject(config):
|
|
assert config is loader_boundary.config
|
|
raise ValueError("config rejected")
|
|
|
|
conversion = Mock(side_effect=AssertionError("conversion reached"))
|
|
monkeypatch.setattr(loader.qd, "materialize_dequantized_checkpoint", conversion)
|
|
with pytest.raises(ValueError, match="config rejected"):
|
|
loader.load_model(
|
|
"owner/model", config_validator=reject, revision="pinned",
|
|
local_files_only=True, cache_dir="custom-cache", trust_remote_code=True,
|
|
)
|
|
loader.AutoConfig.from_pretrained.assert_called_once_with(
|
|
"owner/model", revision="pinned", local_files_only=True,
|
|
cache_dir="custom-cache", trust_remote_code=True, token=None,
|
|
)
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
loader.AutoTokenizer.from_pretrained.assert_not_called()
|
|
conversion.assert_not_called()
|
|
|
|
|
|
def test_source_revision_captures_resolved_commit_not_requested_tag(loader_boundary, monkeypatch):
|
|
commit = "a" * 40
|
|
loader_boundary.config._commit_hash = commit
|
|
monkeypatch.setattr(
|
|
loader.qd, "detect_quant_scheme",
|
|
lambda *args, **kwargs: loader.qd.QuantDetection(loader.qd.QuantScheme.NONE),
|
|
)
|
|
handle = loader.load_model(
|
|
"org/model", revision="release", skip_snapshot=True, phase_admission=False,
|
|
)
|
|
assert handle.source_model_revision == commit
|
|
|
|
|
|
@pytest.mark.parametrize("snapshot_source", [True, False])
|
|
def test_dequantized_source_revision_uses_materialized_snapshot(
|
|
loader_boundary, monkeypatch, tmp_path, snapshot_source,
|
|
):
|
|
initial_config = _config(_commit_hash="a" * 40)
|
|
loader.AutoConfig.from_pretrained.side_effect = [initial_config, loader_boundary.config]
|
|
source = tmp_path / "snapshots" / ("b" * 40) if snapshot_source else tmp_path / "local"
|
|
source.mkdir(parents=True)
|
|
converted = tmp_path / "converted"
|
|
converted.mkdir()
|
|
monkeypatch.setattr(
|
|
loader.qd, "detect_quant_scheme",
|
|
lambda *args, **kwargs: loader.qd.QuantDetection(loader.qd.QuantScheme.FP8_BLOCKWISE),
|
|
)
|
|
monkeypatch.setattr(
|
|
loader.qd, "materialize_dequantized_checkpoint",
|
|
lambda *args, **kwargs: (str(converted), str(source)),
|
|
)
|
|
|
|
handle = loader.load_model(
|
|
"org/model", revision="release", skip_snapshot=True, phase_admission=False,
|
|
)
|
|
|
|
assert handle.source_model_revision == ("b" * 40 if snapshot_source else None)
|
|
assert handle.model_name == "org/model"
|
|
assert not converted.exists()
|
|
|
|
|
|
def test_unavailable_flash_attention_falls_back_to_eager(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
import transformers.utils
|
|
|
|
loader_boundary.config._attn_implementation = "flash_attention_2"
|
|
monkeypatch.setattr(
|
|
transformers.utils,
|
|
"is_flash_attn_2_available",
|
|
lambda: False,
|
|
)
|
|
monkeypatch.setattr(
|
|
loader.qd,
|
|
"detect_quant_scheme",
|
|
lambda *args, **kwargs: loader.qd.QuantDetection(loader.qd.QuantScheme.NONE),
|
|
)
|
|
|
|
loader.load_model("local/model", skip_snapshot=True)
|
|
|
|
assert loader_boundary.config._attn_implementation == "eager"
|
|
|
|
|
|
def test_broken_flash_attention_probe_falls_back_to_eager(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
loader_boundary.config._attn_implementation = "flash_attention_2"
|
|
real_import = builtins.__import__
|
|
|
|
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if (
|
|
name == "transformers.utils"
|
|
and "is_flash_attn_2_available" in tuple(fromlist or ())
|
|
):
|
|
raise RuntimeError("flash-attention availability probe failed")
|
|
return real_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", rejecting_import)
|
|
loader.load_model("local/model", skip_snapshot=True)
|
|
|
|
assert loader_boundary.config._attn_implementation == "eager"
|
|
|
|
|
|
def test_available_flash_attention_preserves_requested_implementation(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
import transformers.utils
|
|
|
|
loader_boundary.config._attn_implementation = "flash_attention_2"
|
|
monkeypatch.setattr(
|
|
transformers.utils,
|
|
"is_flash_attn_2_available",
|
|
lambda: True,
|
|
)
|
|
|
|
loader.load_model("local/model", skip_snapshot=True)
|
|
|
|
assert loader_boundary.config._attn_implementation == "flash_attention_2"
|
|
|
|
|
|
def test_multimodal_unwrap_generation_cache_and_legacy_ties(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
class FakeLanguageModel(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config = _config(tie_word_embeddings=False)
|
|
self._tied_weights_keys = ["lm_head.weight"]
|
|
self.last_cache_position = None
|
|
|
|
def prepare_inputs_for_generation(
|
|
self, input_ids, *args, cache_position=None, **kwargs,
|
|
):
|
|
self.last_cache_position = cache_position
|
|
return {"input_ids": input_ids, "cache_position": cache_position}
|
|
|
|
class MultimodalWrapper(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.language_model = FakeLanguageModel()
|
|
|
|
def forward(self, input_ids, pixel_values):
|
|
return input_ids, pixel_values
|
|
|
|
wrapper = MultimodalWrapper()
|
|
loader_boundary.model_class.from_pretrained.return_value = wrapper
|
|
monkeypatch.setattr(loader, "PreTrainedModel", FakeLanguageModel)
|
|
monkeypatch.setattr(
|
|
loader.qd,
|
|
"detect_quant_scheme",
|
|
lambda *args, **kwargs: loader.qd.QuantDetection(loader.qd.QuantScheme.NONE),
|
|
)
|
|
|
|
handle = loader.load_model("local/model", skip_snapshot=True)
|
|
|
|
assert handle.model is wrapper.language_model
|
|
assert handle.model._tied_weights_keys == {}
|
|
input_ids = torch.ones(1, 2, dtype=torch.long)
|
|
|
|
class Cache:
|
|
@staticmethod
|
|
def get_seq_length():
|
|
return 4
|
|
|
|
prepared = handle.model.prepare_inputs_for_generation(
|
|
input_ids, past_key_values=Cache(),
|
|
)
|
|
assert prepared["cache_position"].tolist() == [4, 5]
|
|
|
|
legacy_cache = ((torch.zeros(1, 1, 3, 1),),)
|
|
prepared = handle.model.prepare_inputs_for_generation(
|
|
input_ids, past_key_values=legacy_cache,
|
|
)
|
|
assert prepared["cache_position"].tolist() == [3, 4]
|
|
|
|
prepared = handle.model.prepare_inputs_for_generation(
|
|
input_ids, past_key_values=object(),
|
|
)
|
|
assert prepared["cache_position"].tolist() == [0, 1]
|
|
|
|
prepared = handle.model.prepare_inputs_for_generation(input_ids)
|
|
assert prepared["cache_position"].tolist() == [0, 1]
|
|
|
|
explicit = torch.tensor([7, 8])
|
|
prepared = handle.model.prepare_inputs_for_generation(
|
|
input_ids, cache_position=explicit,
|
|
)
|
|
assert prepared["cache_position"] is explicit
|
|
|
|
|
|
def test_multimodal_signature_probe_failure_preserves_wrapper(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
class FakeLanguageModel(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config = _config()
|
|
|
|
class WrapperWithUninspectableForward(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.language_model = FakeLanguageModel()
|
|
self.forward = object()
|
|
|
|
wrapper = WrapperWithUninspectableForward()
|
|
loader_boundary.model_class.from_pretrained.return_value = wrapper
|
|
monkeypatch.setattr(loader, "PreTrainedModel", FakeLanguageModel)
|
|
|
|
handle = loader.load_model("local/model", skip_snapshot=True)
|
|
|
|
assert handle.model is wrapper
|
|
|
|
|
|
def test_hf_token_is_forwarded_without_logging_value(loader_boundary, monkeypatch):
|
|
monkeypatch.setenv("HF_TOKEN", "secret-token")
|
|
loader.load_model("x", skip_snapshot=True)
|
|
assert loader.AutoConfig.from_pretrained.call_args.kwargs["token"] == "secret-token"
|
|
assert loader_boundary.model_class.from_pretrained.call_args.kwargs["token"] == "secret-token"
|
|
assert loader.AutoTokenizer.from_pretrained.call_args.kwargs["token"] == "secret-token"
|
|
|
|
|
|
def test_device_and_dtype_failures_precede_provider_access(loader_boundary, monkeypatch):
|
|
provider = Mock(side_effect=AssertionError("provider should not be called"))
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", provider)
|
|
monkeypatch.setattr(loader.dev, "get_device", Mock(side_effect=RuntimeError("CUDA unavailable")))
|
|
with pytest.raises(RuntimeError, match="CUDA unavailable"):
|
|
loader.load_model("x", device="cuda")
|
|
provider.assert_not_called()
|
|
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "mps")
|
|
monkeypatch.setattr(loader.dev, "supports_bfloat16", lambda _device=None: False)
|
|
with pytest.raises(RuntimeError, match="bfloat16 is not supported on 'mps'"):
|
|
loader.load_model("x", device="mps", dtype="bfloat16")
|
|
provider.assert_not_called()
|
|
|
|
|
|
def test_config_permission_error_retries_in_temp_cache(loader_boundary, monkeypatch, tmp_path):
|
|
monkeypatch.setattr(loader.tempfile, "gettempdir", lambda: str(tmp_path))
|
|
loader.AutoConfig.from_pretrained.side_effect = [PermissionError("cache"), loader_boundary.config]
|
|
loader.load_model("x", skip_snapshot=True)
|
|
assert loader.AutoConfig.from_pretrained.call_count == 2
|
|
assert loader.AutoConfig.from_pretrained.call_args.kwargs["cache_dir"] == str(tmp_path / "hf_home" / "hub")
|
|
assert (tmp_path / "hf_home" / "hub").is_dir()
|
|
|
|
|
|
@pytest.mark.parametrize("message", ["Gated repo denied", "Access to model is restricted"])
|
|
def test_config_gated_repo_failure_is_actionable(loader_boundary, monkeypatch, message):
|
|
loader.AutoConfig.from_pretrained.side_effect = OSError(message)
|
|
with pytest.raises(RuntimeError, match=r"(?s)Accept the license.*HF_TOKEN"):
|
|
loader.load_model("owner/gated", skip_snapshot=True)
|
|
|
|
|
|
def test_non_gated_config_oserror_is_preserved(loader_boundary):
|
|
loader.AutoConfig.from_pretrained.side_effect = OSError("offline cache miss")
|
|
with pytest.raises(OSError, match="offline cache miss"):
|
|
loader.load_model("x", local_files_only=True)
|
|
|
|
|
|
@pytest.mark.parametrize("exc", [ValueError("unknown"), KeyError("model_type")])
|
|
def test_malformed_or_unknown_config_has_stable_guidance(loader_boundary, exc):
|
|
loader.AutoConfig.from_pretrained.side_effect = exc
|
|
with pytest.raises(RuntimeError, match=r"(?s)not recognized by transformers.*trust_remote_code=True"):
|
|
loader.load_model("new/model")
|
|
|
|
|
|
def test_task_model_selection_and_gemma_contract(monkeypatch):
|
|
causal = object()
|
|
classification = object()
|
|
image_text = object()
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "causal_lm", causal)
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "classification", classification)
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", image_text)
|
|
assert loader._select_model_class("causal_lm", _config()) is causal
|
|
assert loader._select_model_class("classification", _config()) is classification
|
|
assert loader._select_model_class("causal_lm", _config(model_type="gemma4")) is image_text
|
|
assert loader._select_model_class(
|
|
"causal_lm",
|
|
_config(model_type="unknown", architectures=["Gemma4ForConditionalGeneration"]),
|
|
) is image_text
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", None)
|
|
with pytest.raises(RuntimeError, match="Upgrade transformers"):
|
|
loader._select_model_class("causal_lm", _config(model_type="gemma4"))
|
|
with pytest.raises(ValueError, match="Unknown task"):
|
|
loader._select_model_class("embedding", _config())
|
|
|
|
|
|
def test_mistral3_load_uses_image_text_class_and_keeps_remote_code_disabled(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
loader_boundary.config.model_type = "mistral3"
|
|
loader_boundary.config.architectures = ["Mistral3ForConditionalGeneration"]
|
|
image_text = SimpleNamespace(
|
|
from_pretrained=Mock(return_value=loader_boundary.model),
|
|
)
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", image_text)
|
|
|
|
handle = loader.load_model("local/mistral3", skip_snapshot=True)
|
|
|
|
assert handle.config is loader_boundary.config
|
|
assert handle.architecture == "mistral3"
|
|
assert loader.AutoConfig.from_pretrained.call_args.kwargs["trust_remote_code"] is False
|
|
assert image_text.from_pretrained.call_args.kwargs["trust_remote_code"] is False
|
|
assert loader.AutoTokenizer.from_pretrained.call_args.kwargs["trust_remote_code"] is False
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize(("config_values", "image_text"), [
|
|
({"model_type": "qwen3_5", "architectures": None}, True),
|
|
({"model_type": "unknown", "architectures": ["Qwen3_5ForConditionalGeneration"]}, True),
|
|
({"model_type": "qwen3_5", "architectures": ["Qwen3_5ForConditionalGeneration"]}, True),
|
|
({"model_type": "qwen3_5_text", "architectures": ["Qwen3_5ForCausalLM"]}, False),
|
|
({"model_type": "qwen3_5", "architectures": ["Qwen3_5ForCausalLM"]}, False),
|
|
({"model_type": "qwen3_5", "language_model_only": True}, False),
|
|
({"model_type": "qwen3_5_text", "architectures": ["Qwen3_5ForConditionalGeneration"]}, False),
|
|
])
|
|
def test_qwen35_model_class_selection(monkeypatch, config_values, image_text):
|
|
causal_class, image_text_class, classification_class = object(), object(), object()
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "causal_lm", causal_class)
|
|
monkeypatch.setitem(loader.TASK_MODEL_MAP, "classification", classification_class)
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", image_text_class)
|
|
config = _config(**config_values)
|
|
|
|
assert loader._select_model_class("causal_lm", config) is (
|
|
image_text_class if image_text else causal_class
|
|
)
|
|
assert loader._select_model_class("classification", config) is classification_class
|
|
monkeypatch.setattr(loader, "AutoModelForImageTextToText", None)
|
|
if image_text:
|
|
with pytest.raises(RuntimeError, match="Upgrade transformers"):
|
|
loader._select_model_class("causal_lm", config)
|
|
else:
|
|
assert loader._select_model_class("causal_lm", config) is causal_class
|
|
|
|
|
|
def test_qwen35_load_uses_image_text_class_and_sdpa(
|
|
loader_boundary, caplog, monkeypatch,
|
|
):
|
|
caplog.set_level("INFO")
|
|
loader_boundary.config.model_type = "qwen3_5"
|
|
loader_boundary.config.architectures = ["Qwen3_5ForConditionalGeneration"]
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_qwen_single_device", Mock(return_value=2))
|
|
|
|
handle = loader.load_model("Qwen/Qwen3.8-27B", skip_snapshot=True)
|
|
|
|
assert handle.architecture == "qwen3_5"
|
|
kwargs = loader_boundary.image_text_class.from_pretrained.call_args.kwargs
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
assert kwargs["attn_implementation"] == "sdpa"
|
|
assert kwargs["device_map"] == {"": 2}
|
|
assert "device_map" not in kwargs or kwargs["device_map"] != "auto"
|
|
assert "through AutoModelForImageTextToText" in caplog.text
|
|
assert "surgery targets the text backbone only" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize("model_type", ["qwen3_5", "qwen3_5_text"])
|
|
def test_qwen35_text_only_load_keeps_causal_lm_class(loader_boundary, monkeypatch, caplog, model_type):
|
|
caplog.set_level("INFO")
|
|
loader_boundary.config.model_type = model_type
|
|
loader_boundary.config.architectures = ["Qwen3_5ForCausalLM"]
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_qwen_single_device", Mock(return_value=2))
|
|
|
|
loader.load_model("local/qwen-text-only", skip_snapshot=True)
|
|
|
|
loader_boundary.image_text_class.from_pretrained.assert_not_called()
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["attn_implementation"] == "sdpa"
|
|
assert kwargs["device_map"] == {"": 2}
|
|
assert "through AutoModelForCausalLM" in caplog.text
|
|
|
|
|
|
def test_qwen35_explicit_indexed_cuda_quantized_load_preserves_runtime_constraints(
|
|
loader_boundary,
|
|
monkeypatch,
|
|
):
|
|
made = []
|
|
|
|
def fake_bnb(**kwargs):
|
|
made.append(kwargs)
|
|
return kwargs
|
|
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,0")
|
|
loader_boundary.config.model_type = "qwen3_5"
|
|
loader_boundary.config.architectures = ["Qwen3_5ForConditionalGeneration"]
|
|
monkeypatch.setattr(
|
|
loader.dev,
|
|
"get_device",
|
|
lambda preference="auto": "cuda" if preference == "auto" else preference,
|
|
)
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.torch.cuda, "device_count", lambda: 2)
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"mem_get_info",
|
|
lambda index: ([24, 80][index] * 1024 ** 3, 80 * 1024 ** 3),
|
|
)
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_estimate_model_memory_gb", Mock(return_value=54.0))
|
|
monkeypatch.setitem(sys.modules, "bitsandbytes", ModuleType("bitsandbytes"))
|
|
monkeypatch.setattr("transformers.BitsAndBytesConfig", fake_bnb)
|
|
|
|
handle = loader.load_model(
|
|
"Qwen/Qwen3.8-27B",
|
|
device="cuda:1",
|
|
quantization="4bit",
|
|
dtype="bfloat16",
|
|
skip_snapshot=True,
|
|
)
|
|
|
|
kwargs = loader_boundary.image_text_class.from_pretrained.call_args.kwargs
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
assert kwargs["attn_implementation"] == "sdpa"
|
|
assert kwargs["device_map"] == {"": 1}
|
|
assert kwargs["quantization_config"] == made[0]
|
|
assert made[0]["load_in_4bit"] is True
|
|
loader_boundary.model.to.assert_not_called()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_qwen35_missing_fast_kernels_fails_before_weight_load(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
loader_boundary.config.model_type = "qwen3_5"
|
|
monkeypatch.setattr(
|
|
loader,
|
|
"_require_qwen_hybrid_kernels",
|
|
Mock(side_effect=RuntimeError("missing or incompatible: causal-conv1d")),
|
|
)
|
|
|
|
with pytest.raises(RuntimeError, match="causal-conv1d"):
|
|
loader.load_model("Qwen/Qwen3.8-27B", skip_snapshot=True)
|
|
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
loader_boundary.image_text_class.from_pretrained.assert_not_called()
|
|
|
|
|
|
def test_qwen_kernel_probe_reports_each_incompatible_extension(monkeypatch):
|
|
def import_module(name):
|
|
if name == "fla":
|
|
raise ImportError("missing")
|
|
raise OSError("ABI mismatch")
|
|
|
|
monkeypatch.setattr(loader.importlib, "import_module", import_module)
|
|
|
|
with pytest.raises(RuntimeError, match="flash-linear-attention, causal-conv1d"):
|
|
loader._require_qwen_hybrid_kernels()
|
|
|
|
|
|
def test_qwen_kernel_probe_accepts_compatible_extensions(monkeypatch):
|
|
monkeypatch.setattr(loader.importlib, "import_module", lambda _name: object())
|
|
|
|
loader._require_qwen_hybrid_kernels()
|
|
|
|
|
|
def test_qwen_single_device_uses_per_device_free_memory(monkeypatch):
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.torch.cuda, "device_count", lambda: 3)
|
|
free = [40, 76, 72]
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"mem_get_info",
|
|
lambda index: (free[index] * 1024 ** 3, 80 * 1024 ** 3),
|
|
)
|
|
|
|
assert loader._qwen_single_device(54.0, None) == 1
|
|
assert loader._qwen_single_device(216.0, "4bit") == 1
|
|
|
|
|
|
def test_qwen_single_device_rejects_non_cuda_runtime(monkeypatch):
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: False)
|
|
with pytest.raises(RuntimeError, match="requires an NVIDIA CUDA device"):
|
|
loader._qwen_single_device(54.0, None)
|
|
|
|
|
|
def test_qwen_runtime_overrides_ignore_other_architectures():
|
|
assert loader.qwen_hybrid_runtime_overrides(_config(model_type="llama"), torch.float32) == {}
|
|
|
|
|
|
def test_qwen_runtime_overrides_require_supported_transformers(monkeypatch):
|
|
import transformers
|
|
|
|
monkeypatch.setattr(transformers, "__version__", "5.7.9")
|
|
with pytest.raises(RuntimeError, match="transformers>=5.8"):
|
|
loader.qwen_hybrid_runtime_overrides(
|
|
_config(model_type="qwen3_5_text"),
|
|
torch.bfloat16,
|
|
)
|
|
|
|
|
|
def test_qwen_explicit_indexed_cuda_uses_requested_visible_ordinal(monkeypatch):
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,0")
|
|
config = _config(model_type="qwen3_5")
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda preference="auto": preference)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setattr(loader.torch.cuda, "device_count", lambda: 2)
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"mem_get_info",
|
|
lambda index: ([24, 80][index] * 1024 ** 3, 80 * 1024 ** 3),
|
|
)
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_estimate_model_memory_gb", Mock(return_value=54.0))
|
|
|
|
assert loader.qwen_hybrid_runtime_overrides(
|
|
config,
|
|
torch.bfloat16,
|
|
device="cuda:1",
|
|
resolved_device="cuda:1",
|
|
) == {
|
|
"attn_implementation": "sdpa",
|
|
"device_map": {"": 1},
|
|
}
|
|
|
|
|
|
def test_qwen_explicit_indexed_cuda_rejects_insufficient_single_device(monkeypatch):
|
|
config = _config(model_type="qwen3_5")
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setattr(loader.torch.cuda, "device_count", lambda: 2)
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"mem_get_info",
|
|
lambda index: ([80, 24][index] * 1024 ** 3, 80 * 1024 ** 3),
|
|
)
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_estimate_model_memory_gb", Mock(return_value=54.0))
|
|
|
|
with pytest.raises(RuntimeError, match="explicit device cuda:1"):
|
|
loader.qwen_hybrid_runtime_overrides(
|
|
config,
|
|
torch.bfloat16,
|
|
device="cuda:1",
|
|
resolved_device="cuda:1",
|
|
)
|
|
|
|
|
|
def test_qwen_saved_text_derivative_reuses_single_device_runtime(monkeypatch):
|
|
config = _config(model_type="qwen3_5_text")
|
|
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", Mock())
|
|
monkeypatch.setattr(loader, "_estimate_model_memory_gb", Mock(return_value=54.0))
|
|
monkeypatch.setattr(loader, "_qwen_single_device", Mock(return_value=2))
|
|
|
|
assert loader.qwen_hybrid_runtime_overrides(
|
|
config,
|
|
torch.bfloat16,
|
|
) == {
|
|
"attn_implementation": "sdpa",
|
|
"device_map": {"": 2},
|
|
}
|
|
|
|
|
|
def test_model_handle_metadata_snapshot_restore_summary_and_cleanup(tmp_path):
|
|
model = _model()
|
|
nested = SimpleNamespace(
|
|
num_hidden_layers=3,
|
|
num_attention_heads=6,
|
|
hidden_size=12,
|
|
intermediate_size=24,
|
|
)
|
|
config = _config(
|
|
num_hidden_layers=0,
|
|
num_attention_heads=0,
|
|
hidden_size=0,
|
|
intermediate_size=0,
|
|
text_config=nested,
|
|
)
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
(offload / "weight").write_text("x")
|
|
handle = loader.ModelHandle(
|
|
model,
|
|
SimpleNamespace(),
|
|
config,
|
|
"x",
|
|
"causal_lm",
|
|
_offload_dir=str(offload),
|
|
_owns_offload_dir=True,
|
|
)
|
|
assert (handle.num_layers, handle.num_heads, handle.hidden_size, handle.intermediate_size) == (3, 6, 12, 24)
|
|
with pytest.raises(RuntimeError, match="call .snapshot"):
|
|
handle.restore()
|
|
handle.snapshot()
|
|
storage = model.named_modules()[0][1]
|
|
with torch.no_grad():
|
|
storage.weight.zero_()
|
|
handle.restore()
|
|
assert torch.equal(storage.weight, torch.ones(2))
|
|
snapshot_path = handle._restore_point.path
|
|
assert handle.summary() == {
|
|
"model_name": "x",
|
|
"architecture": "gpt2",
|
|
"task": "causal_lm",
|
|
"num_layers": 3,
|
|
"num_heads": 6,
|
|
"hidden_size": 12,
|
|
"intermediate_size": 24,
|
|
"total_params": 5,
|
|
}
|
|
handle.cleanup()
|
|
assert not offload.exists()
|
|
assert not snapshot_path.exists()
|
|
assert handle._offload_dir is None
|
|
|
|
|
|
def test_model_handle_cleanup_records_unavailable_restore_policy(tmp_path):
|
|
handle = loader.ModelHandle(torch.nn.Linear(1, 1), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot(directory=tmp_path)
|
|
restore_point = handle._restore_point
|
|
policy = dict(handle.snapshot_policy)
|
|
assert policy["status"] == "disk_backed"
|
|
assert policy["restore_available"] is True
|
|
|
|
handle.cleanup()
|
|
|
|
expected = {**policy, "status": "closed", "restore_available": False,
|
|
"reason": "restore point closed"}
|
|
assert not restore_point.path.exists()
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy == restore_point.policy == expected
|
|
with pytest.raises(RuntimeError, match="call .snapshot"):
|
|
handle.restore()
|
|
handle.cleanup()
|
|
assert handle.snapshot_policy == expected
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["model", "module", "shape"])
|
|
def test_model_handle_rejects_stale_restore_identity(change):
|
|
model = torch.nn.Sequential(torch.nn.Linear(3, 2))
|
|
handle = loader.ModelHandle(model, SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot()
|
|
if change == "model":
|
|
handle.model = torch.nn.Sequential(torch.nn.Linear(3, 2))
|
|
elif change == "module":
|
|
model[0] = torch.nn.Linear(3, 2)
|
|
else:
|
|
model[0].weight = torch.nn.Parameter(torch.ones(2, 4))
|
|
with pytest.raises(RuntimeError, match="identity"):
|
|
handle.restore()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_model_handle_failed_snapshot_replacement_preserves_original():
|
|
model = torch.nn.Linear(3, 2)
|
|
handle = loader.ModelHandle(model, SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.ensure_snapshot()
|
|
original = model.weight.detach().clone()
|
|
with torch.no_grad():
|
|
model.weight.zero_()
|
|
with pytest.raises(RuntimeError, match="staging"):
|
|
handle.snapshot(staging_bytes=1)
|
|
handle.restore()
|
|
torch.testing.assert_close(model.weight, original)
|
|
handle.cleanup()
|
|
|
|
|
|
def test_model_memory_estimation_handles_dense_moe_nested_and_unknown():
|
|
dense = loader._estimate_model_memory_gb(_config(), torch.float32)
|
|
moe = loader._estimate_model_memory_gb(_config(num_local_experts=4), torch.float32)
|
|
assert dense > 0
|
|
assert moe > dense
|
|
nested = _config(hidden_size=0, num_hidden_layers=0, intermediate_size=0, vocab_size=0)
|
|
nested.text_config = _config(hidden_size=8, num_hidden_layers=2, intermediate_size=16, vocab_size=32)
|
|
assert loader._estimate_model_memory_gb(nested, torch.float16) > 0
|
|
assert loader._estimate_model_memory_gb(_config(hidden_size=0), torch.float16) == 0
|
|
|
|
|
|
@pytest.mark.parametrize("quantization", ["4bit", "8bit"])
|
|
def test_quantization_rejects_non_cuda_instead_of_silent_degradation(loader_boundary, quantization):
|
|
with pytest.raises(RuntimeError, match="requires an available NVIDIA CUDA device"):
|
|
loader.load_model("x", quantization=quantization)
|
|
loader_boundary.model_class.from_pretrained.assert_not_called()
|
|
|
|
|
|
def test_quantization_requires_bitsandbytes(loader_boundary, monkeypatch):
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
real_import = builtins.__import__
|
|
|
|
def reject_bitsandbytes(name, *args, **kwargs):
|
|
if name == "bitsandbytes":
|
|
raise ImportError
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", reject_bitsandbytes)
|
|
with pytest.raises(RuntimeError, match="pip install -U bitsandbytes"):
|
|
loader.load_model("x", quantization="4bit")
|
|
|
|
|
|
@pytest.mark.parametrize("quantization", ["4bit", "8bit"])
|
|
def test_cuda_quantization_builds_explicit_config(loader_boundary, monkeypatch, quantization):
|
|
made = []
|
|
|
|
def fake_bnb(**kwargs):
|
|
made.append(kwargs)
|
|
return kwargs
|
|
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setitem(sys.modules, "bitsandbytes", ModuleType("bitsandbytes"))
|
|
monkeypatch.setattr("transformers.BitsAndBytesConfig", fake_bnb)
|
|
handle = loader.load_model("x", quantization=quantization, skip_snapshot=True)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["device_map"] == "auto"
|
|
assert kwargs["quantization_config"] == made[0]
|
|
assert made[0][f"load_in_{quantization}"] is True
|
|
assert made[0]["llm_int8_enable_fp32_cpu_offload"] is True
|
|
handle.cleanup()
|
|
|
|
|
|
def test_native_quantization_wins_and_skips_dtype(loader_boundary, monkeypatch):
|
|
loader_boundary.config.quantization_config = SimpleNamespace()
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
handle = loader.load_model("x", quantization="4bit", skip_snapshot=True)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert "torch_dtype" not in kwargs
|
|
assert kwargs["device_map"] == "auto"
|
|
assert "quantization_config" not in kwargs
|
|
handle.cleanup()
|
|
|
|
|
|
@pytest.mark.parametrize("quantization", ["4bit", "8bit"])
|
|
def test_explicit_indexed_cuda_quantization_uses_root_device_map(
|
|
loader_boundary,
|
|
monkeypatch,
|
|
quantization,
|
|
):
|
|
made = []
|
|
|
|
def fake_bnb(**kwargs):
|
|
made.append(kwargs)
|
|
return kwargs
|
|
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,0")
|
|
monkeypatch.setattr(
|
|
loader.dev,
|
|
"get_device",
|
|
lambda preference="auto": "cuda" if preference == "auto" else preference,
|
|
)
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setitem(sys.modules, "bitsandbytes", ModuleType("bitsandbytes"))
|
|
monkeypatch.setattr("transformers.BitsAndBytesConfig", fake_bnb)
|
|
|
|
handle = loader.load_model(
|
|
"x",
|
|
device="cuda:1",
|
|
quantization=quantization,
|
|
skip_snapshot=True,
|
|
)
|
|
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["device_map"] == {"": 1}
|
|
assert kwargs["quantization_config"] == made[0]
|
|
assert made[0][f"load_in_{quantization}"] is True
|
|
loader_boundary.model.to.assert_not_called()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_explicit_indexed_cuda_native_quantization_uses_root_device_map(
|
|
loader_boundary,
|
|
monkeypatch,
|
|
):
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,0")
|
|
loader_boundary.config.quantization_config = SimpleNamespace()
|
|
monkeypatch.setattr(
|
|
loader.dev,
|
|
"get_device",
|
|
lambda preference="auto": "cuda" if preference == "auto" else preference,
|
|
)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
|
|
handle = loader.load_model(
|
|
"x",
|
|
device="cuda:1",
|
|
quantization="4bit",
|
|
skip_snapshot=True,
|
|
)
|
|
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["device_map"] == {"": 1}
|
|
assert "torch_dtype" not in kwargs
|
|
assert "quantization_config" not in kwargs
|
|
loader_boundary.model.to.assert_not_called()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_explicit_indexed_cuda_unquantized_load_uses_root_device_map(
|
|
loader_boundary,
|
|
monkeypatch,
|
|
):
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,0")
|
|
monkeypatch.setattr(
|
|
loader.dev,
|
|
"get_device",
|
|
lambda preference="auto": "cuda" if preference == "auto" else preference,
|
|
)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
|
|
handle = loader.load_model("x", device="cuda:1", skip_snapshot=True)
|
|
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["device_map"] == {"": 1}
|
|
assert kwargs["torch_dtype"] is torch.float32
|
|
loader_boundary.model.to.assert_not_called()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_cuda_auto_map_has_bounded_memory_and_requested_offload(loader_boundary, monkeypatch, tmp_path):
|
|
gib = 1024**3
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
|
|
monkeypatch.setattr(loader.dev, "_system_memory_gb", lambda: (64.0, 40.0))
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"get_device_properties",
|
|
lambda _index: SimpleNamespace(total_memory=20 * gib),
|
|
)
|
|
handle = loader.load_model("x", offload_folder=str(tmp_path), skip_snapshot=True)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["offload_folder"] == str(tmp_path)
|
|
assert kwargs["max_memory"] == {0: "17408MiB", 1: "17408MiB", "cpu": "34816MiB"}
|
|
handle.cleanup()
|
|
assert tmp_path.exists(), "cleanup must not remove an operator-owned directory"
|
|
|
|
|
|
def test_explicit_gpu_memory_utilization_overrides_default_headroom(
|
|
loader_boundary, monkeypatch,
|
|
):
|
|
gib = 1024**3
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 1)
|
|
monkeypatch.setattr(loader.dev, "_system_memory_gb", lambda: (64.0, 40.0))
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"get_device_properties",
|
|
lambda _index: SimpleNamespace(total_memory=20 * gib),
|
|
)
|
|
handle = loader.load_model(
|
|
"x", gpu_memory_utilization=0.95, skip_snapshot=True,
|
|
)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["max_memory"] == {0: "19456MiB", "cpu": "34816MiB"}
|
|
handle.cleanup()
|
|
|
|
|
|
@pytest.mark.parametrize("device", ["cpu", "mps"])
|
|
def test_gpu_memory_utilization_is_safe_when_accelerate_auto_map_is_unavailable(
|
|
loader_boundary, monkeypatch, device,
|
|
):
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": device)
|
|
handle = loader.load_model(
|
|
"x", device=device, gpu_memory_utilization=0.95, skip_snapshot=True,
|
|
)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert "device_map" not in kwargs
|
|
assert "max_memory" not in kwargs
|
|
loader_boundary.model.to.assert_called_once_with(device)
|
|
handle.cleanup()
|
|
|
|
|
|
def _enable_cuda_quantization(monkeypatch, estimate_gb: float) -> None:
|
|
gib = 1024**3
|
|
monkeypatch.setattr(loader, "_estimate_model_memory_gb", lambda *_args: estimate_gb)
|
|
monkeypatch.setattr(loader.dev, "get_device", lambda _preference="auto": "cuda")
|
|
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda _device=None: True)
|
|
monkeypatch.setattr(loader.dev, "get_total_free_gb", lambda: 16.0)
|
|
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: 1)
|
|
monkeypatch.setattr(loader.dev, "_system_memory_gb", lambda: (64.0, 40.0))
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda,
|
|
"get_device_properties",
|
|
lambda _index: SimpleNamespace(total_memory=16 * gib),
|
|
)
|
|
monkeypatch.setitem(sys.modules, "bitsandbytes", ModuleType("bitsandbytes"))
|
|
monkeypatch.setattr("transformers.BitsAndBytesConfig", lambda **kwargs: kwargs)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("quantization", "estimate_gb", "expects_budget"),
|
|
[
|
|
("4bit", 40.0, False),
|
|
("4bit", 48.0, True),
|
|
("8bit", 20.0, False),
|
|
("8bit", 24.0, True),
|
|
],
|
|
)
|
|
def test_quantized_single_gpu_memory_budget_uses_effective_weight_size(
|
|
loader_boundary, monkeypatch, quantization, estimate_gb, expects_budget,
|
|
):
|
|
_enable_cuda_quantization(monkeypatch, estimate_gb)
|
|
handle = loader.load_model("x", quantization=quantization, skip_snapshot=True)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert ("max_memory" in kwargs) is expects_budget
|
|
if expects_budget:
|
|
assert kwargs["max_memory"] == {0: "13926MiB", "cpu": "34816MiB"}
|
|
handle.cleanup()
|
|
|
|
|
|
def test_explicit_gpu_budget_overrides_quantized_fit_shortcut(loader_boundary, monkeypatch):
|
|
_enable_cuda_quantization(monkeypatch, estimate_gb=40.0)
|
|
handle = loader.load_model(
|
|
"x",
|
|
quantization="4bit",
|
|
gpu_memory_utilization=0.5,
|
|
skip_snapshot=True,
|
|
)
|
|
kwargs = loader_boundary.model_class.from_pretrained.call_args.kwargs
|
|
assert kwargs["max_memory"] == {0: "8192MiB", "cpu": "34816MiB"}
|
|
handle.cleanup()
|
|
|
|
|
|
def test_device_memory_ceiling_overrides_quantized_fit_shortcut(loader_boundary, monkeypatch):
|
|
_enable_cuda_quantization(monkeypatch, estimate_gb=40.0)
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", '{"cuda:0": 4294967296}')
|
|
guard = Mock()
|
|
monkeypatch.setattr("obliteratus.phase_admission.admit_model_load", guard)
|
|
handle = loader.load_model("x", quantization="4bit", skip_snapshot=True)
|
|
assert loader_boundary.model_class.from_pretrained.call_args.kwargs["max_memory"] == {
|
|
0: "4096MiB", "cpu": "34816MiB",
|
|
}
|
|
guard.assert_called_once()
|
|
assert guard.call_args.kwargs["device_memory_ceiling_bytes"] == {"cuda:0": 4294967296}
|
|
handle.cleanup()
|
|
|
|
|
|
def test_empty_ceiling_mapping_clears_environment_without_enabling_admission(loader_boundary, monkeypatch):
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", '{"cpu": 0}')
|
|
guard = Mock(side_effect=AssertionError("empty ceilings must not enable admission"))
|
|
monkeypatch.setattr("obliteratus.phase_admission.admit_model_load", guard)
|
|
handle = loader.load_model("x", device="cpu", skip_snapshot=True, device_memory_ceiling_bytes={})
|
|
guard.assert_not_called()
|
|
loader_boundary.model_class.from_pretrained.assert_called_once()
|
|
handle.cleanup()
|
|
|
|
|
|
def test_ceiling_load_retains_explicit_admission_bypass(loader_boundary, monkeypatch, caplog):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
|
|
observer = Mock(side_effect=AssertionError("explicit bypass must not observe memory"))
|
|
handle = loader.load_model("x", device="cpu", skip_snapshot=True,
|
|
device_memory_ceiling_bytes={"cpu": 0}, admission_observer=observer)
|
|
observer.assert_not_called()
|
|
loader_boundary.model_class.from_pretrained.assert_called_once()
|
|
assert "ADMISSION DISABLED" in caplog.text
|
|
handle.cleanup()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("quantization", "estimate_gb", "snapshots"),
|
|
[("4bit", 16.0, 1), ("4bit", 40.0, 0), ("8bit", 8.0, 1), ("8bit", 20.0, 0)],
|
|
)
|
|
def test_quantized_snapshot_policy_uses_effective_weight_size(
|
|
loader_boundary, monkeypatch, quantization, estimate_gb, snapshots,
|
|
):
|
|
_enable_cuda_quantization(monkeypatch, estimate_gb)
|
|
snapshot = Mock()
|
|
monkeypatch.setattr(loader.ModelHandle, "snapshot", snapshot)
|
|
handle = loader.load_model("x", quantization=quantization)
|
|
assert snapshot.call_count == snapshots
|
|
handle.cleanup()
|
|
|
|
|
|
def test_model_permission_error_retries_and_explicit_device_moves(loader_boundary, monkeypatch, tmp_path):
|
|
monkeypatch.setattr(loader.tempfile, "gettempdir", lambda: str(tmp_path))
|
|
loader_boundary.model_class.from_pretrained.side_effect = [PermissionError("cache"), loader_boundary.model]
|
|
loader.load_model("x", device="cpu", skip_snapshot=True)
|
|
assert loader_boundary.model_class.from_pretrained.call_count == 2
|
|
assert loader_boundary.model_class.from_pretrained.call_args.kwargs["cache_dir"].endswith("hf_home/hub")
|
|
loader_boundary.model.to.assert_called_once_with("cpu")
|
|
|
|
|
|
def test_model_gated_and_unknown_architecture_errors_are_actionable(loader_boundary):
|
|
loader_boundary.model_class.from_pretrained.side_effect = OSError("gated repo")
|
|
with pytest.raises(RuntimeError, match="Accept the license"):
|
|
loader.load_model("owner/gated")
|
|
|
|
loader_boundary.model_class.from_pretrained.side_effect = ValueError("does not recognize this architecture")
|
|
with pytest.raises(RuntimeError, match="pip install --upgrade transformers"):
|
|
loader.load_model("new/model")
|
|
|
|
loader_boundary.model_class.from_pretrained.side_effect = KeyError("unrelated")
|
|
with pytest.raises(KeyError, match="unrelated"):
|
|
loader.load_model("x")
|
|
|
|
|
|
def test_tokenizer_permission_retry_and_forced_snapshot(loader_boundary, monkeypatch, tmp_path):
|
|
monkeypatch.setattr(loader.tempfile, "gettempdir", lambda: str(tmp_path))
|
|
loader.AutoTokenizer.from_pretrained.side_effect = [PermissionError("cache"), loader_boundary.tokenizer]
|
|
handle = loader.load_model("x", skip_snapshot=False)
|
|
assert loader.AutoTokenizer.from_pretrained.call_count == 2
|
|
assert loader.AutoTokenizer.from_pretrained.call_args.kwargs["cache_dir"].endswith("hf_home/hub")
|
|
assert handle._restore_point is not None
|
|
assert handle.snapshot_policy['status'] == 'disk_backed'
|
|
assert handle.snapshot_policy['restore_available'] is True
|
|
|
|
|
|
def test_loader_records_explicitly_disabled_snapshot_policy(loader_boundary):
|
|
handle = loader.load_model('x', device='cpu', skip_snapshot=True)
|
|
assert handle.snapshot_policy['status'] == 'disabled_with_reason'
|
|
assert handle.snapshot_policy['restore_available'] is False
|
|
assert 'explicitly disabled' in handle.snapshot_policy['reason']
|
|
assert 'reload immutable source' in handle.snapshot_policy['reason']
|
|
policy = dict(handle.snapshot_policy)
|
|
handle.cleanup()
|
|
handle.cleanup()
|
|
assert handle.snapshot_policy == policy
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("native", "initial_free", "remaining_free", "snapshots"),
|
|
[
|
|
(True, 10.0, 3.0, 0),
|
|
(True, 10.0, 6.0, 1),
|
|
(False, 0.000001, 0.0, 0),
|
|
(False, 0.0, 0.0, 1),
|
|
],
|
|
)
|
|
def test_automatic_snapshot_memory_policy(
|
|
loader_boundary, monkeypatch, native, initial_free, remaining_free, snapshots,
|
|
):
|
|
if native:
|
|
loader_boundary.config.quantization_config = SimpleNamespace()
|
|
monkeypatch.setattr(loader.dev, "get_total_free_gb", Mock(side_effect=[initial_free, remaining_free]))
|
|
snapshot = Mock()
|
|
monkeypatch.setattr(loader.ModelHandle, "snapshot", snapshot)
|
|
handle = loader.load_model("x")
|
|
assert snapshot.call_count == snapshots
|
|
handle.cleanup()
|
|
|
|
|
|
def test_classification_load_sets_requested_label_count(loader_boundary):
|
|
handle = loader.load_model(
|
|
"x",
|
|
task="classification",
|
|
num_labels=7,
|
|
skip_snapshot=True,
|
|
)
|
|
assert loader_boundary.config.num_labels == 7
|
|
assert loader_boundary.model_class.from_pretrained.call_args.kwargs["config"] is loader_boundary.config
|
|
handle.cleanup()
|
|
|
|
|
|
def test_classification_load_keeps_checkpoint_label_count_by_default(loader_boundary):
|
|
"""#614: an omitted label count must not overwrite the checkpoint's head size."""
|
|
loader_boundary.config.num_labels = 3
|
|
handle = loader.load_model("x", task="classification", skip_snapshot=True)
|
|
assert loader_boundary.config.num_labels == 3
|
|
handle.cleanup()
|
|
|
|
|
|
def test_non_gated_model_oserror_is_preserved(loader_boundary):
|
|
loader_boundary.model_class.from_pretrained.side_effect = OSError("offline weight cache miss")
|
|
with pytest.raises(OSError, match="offline weight cache miss"):
|
|
loader.load_model("x", local_files_only=True)
|
|
|
|
|
|
def test_cached_path_compatibility_resolves_local_hub_and_fallback(monkeypatch, tmp_path):
|
|
local = tmp_path / "weights.bin"
|
|
local.write_bytes(b"weights")
|
|
assert loader._cached_path_shim(local) == str(local)
|
|
|
|
download = Mock(return_value="/cache/config.json")
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
assert loader._cached_path_shim("owner/model/config.json", cache_dir="/cache") == "/cache/config.json"
|
|
download.assert_called_once_with(
|
|
repo_id="owner/model",
|
|
filename="config.json",
|
|
cache_dir="/cache",
|
|
)
|
|
|
|
download.side_effect = RuntimeError("network unavailable")
|
|
assert loader._cached_path_shim("owner/model/config.json") == "owner/model/config.json"
|
|
download.reset_mock(side_effect=True)
|
|
assert loader._cached_path_shim("single-name") == "single-name"
|
|
download.assert_not_called()
|
|
|
|
|
|
def test_working_or_temp_dir_compatibility_preserves_and_cleans_paths(tmp_path):
|
|
with loader._working_or_temp_dir(tmp_path) as selected:
|
|
assert selected == tmp_path
|
|
|
|
with loader._working_or_temp_dir() as generated:
|
|
generated_path = Path(generated)
|
|
assert generated_path.is_dir()
|
|
assert not generated_path.exists()
|
|
|
|
|
|
def test_deferred_transformers_shims_are_additive_and_idempotent(monkeypatch):
|
|
fake_transformers = SimpleNamespace(_objects={})
|
|
monkeypatch.setitem(loader._sys.modules, "transformers", fake_transformers)
|
|
monkeypatch.setattr(loader, "_DEFERRED_SHIMS_APPLIED", False)
|
|
loader._apply_deferred_shims()
|
|
|
|
assert fake_transformers.LogitsWarper is fake_transformers._objects["LogitsWarper"]
|
|
assert fake_transformers.is_tf_available() is False
|
|
assert fake_transformers.is_flax_available() is False
|
|
assert fake_transformers.is_safetensors_available() is True
|
|
before = dict(fake_transformers.__dict__)
|
|
loader._apply_deferred_shims()
|
|
assert fake_transformers.__dict__ == before
|
|
|
|
|
|
def test_deferred_transformers_shims_tolerate_missing_module(monkeypatch):
|
|
monkeypatch.delitem(loader._sys.modules, "transformers", raising=False)
|
|
monkeypatch.setattr(loader, "_DEFERRED_SHIMS_APPLIED", False)
|
|
loader._apply_deferred_shims()
|
|
assert loader._DEFERRED_SHIMS_APPLIED is True
|
|
|
|
|
|
def test_deferred_transformers_shims_isolate_lazy_module_failures(monkeypatch):
|
|
class BrokenLazyModule:
|
|
def __getattribute__(self, name):
|
|
if name in {"LogitsWarper", "is_tf_available"}:
|
|
raise RuntimeError("lazy import failed")
|
|
return object.__getattribute__(self, name)
|
|
|
|
monkeypatch.setitem(loader._sys.modules, "transformers", BrokenLazyModule())
|
|
monkeypatch.setattr(loader, "_DEFERRED_SHIMS_APPLIED", False)
|
|
loader._apply_deferred_shims()
|
|
assert loader._DEFERRED_SHIMS_APPLIED is True
|
|
|
|
|
|
def test_image_text_input_compatibility_shim_preserves_argument_order():
|
|
images = object()
|
|
text = object()
|
|
assert loader._validate_images_text_input_order(
|
|
images=images,
|
|
text=text,
|
|
ignored="compatibility",
|
|
) == (images, text)
|
|
|
|
|
|
def _owned_handle(offload):
|
|
return loader.ModelHandle(
|
|
_model(), SimpleNamespace(), _config(), "x", "causal_lm",
|
|
_offload_dir=str(offload), _owns_offload_dir=True,
|
|
)
|
|
|
|
|
|
def test_cleanup_is_best_effort_when_directory_removal_fails(monkeypatch, tmp_path, caplog):
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
handle = _owned_handle(offload)
|
|
monkeypatch.setattr("shutil.rmtree", Mock(side_effect=OSError("busy")))
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup() # best effort: offload removal failures are not raised
|
|
# Attempted once: the directory stays on disk and is no longer tracked.
|
|
assert handle._offload_dir is None
|
|
assert handle._owns_offload_dir is False
|
|
assert offload.exists()
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any(str(offload) in m and "OSError: busy" in m for m in messages), messages
|
|
|
|
|
|
def test_finalizer_never_raises_when_restore_point_cleanup_fails(monkeypatch):
|
|
"""Garbage collection may finalize a handle at any later point, including
|
|
inside unrelated code that patches filesystem calls or during shutdown;
|
|
``rmtree(ignore_errors=True)`` only suppresses ``OSError``."""
|
|
unraisable = []
|
|
monkeypatch.setattr(sys, "unraisablehook", unraisable.append)
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle._restore_point = SimpleNamespace(
|
|
policy={}, close=Mock(side_effect=AssertionError("patched os.open")),
|
|
)
|
|
with pytest.raises(AssertionError, match="patched os.open"):
|
|
handle.cleanup() # explicit cleanup still reports the failure
|
|
del handle
|
|
gc.collect()
|
|
assert unraisable == []
|
|
|
|
|
|
def test_cleanup_attempts_every_stage_and_finalizer_logs_each_failure(
|
|
monkeypatch, tmp_path, caplog,
|
|
):
|
|
"""A restore-point failure must not leave the owned offload directory behind,
|
|
and the finalizer reports each failed stage at WARNING instead of hiding it."""
|
|
unraisable = []
|
|
monkeypatch.setattr(sys, "unraisablehook", unraisable.append)
|
|
|
|
def handle_with_failing_restore_point():
|
|
offload = tmp_path / f"offload-{len(list(tmp_path.iterdir()))}"
|
|
offload.mkdir()
|
|
(offload / "weights").write_bytes(b"x")
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
close = Mock(side_effect=AssertionError("patched os.open"))
|
|
handle._restore_point = SimpleNamespace(policy={}, close=close)
|
|
handle._offload_dir = str(offload)
|
|
handle._owns_offload_dir = True
|
|
return handle, offload, close
|
|
|
|
handle, offload, close = handle_with_failing_restore_point()
|
|
with pytest.raises(AssertionError, match="patched os.open"):
|
|
handle.cleanup()
|
|
assert not offload.exists() # the later stage still ran
|
|
# Closed at most once: the restore point is detached and reported unavailable.
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
handle.cleanup()
|
|
assert close.call_count == 1
|
|
del handle
|
|
|
|
handle, offload, close = handle_with_failing_restore_point()
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
del handle
|
|
gc.collect()
|
|
assert unraisable == []
|
|
assert not offload.exists()
|
|
assert close.call_count == 1
|
|
warnings = [r for r in caplog.records if "finalizer could not clean up" in r.getMessage()]
|
|
assert len(warnings) == 1
|
|
assert "restore point" in warnings[0].getMessage()
|
|
assert "AssertionError" in warnings[0].getMessage()
|
|
|
|
|
|
@pytest.mark.parametrize("device", ["cpu", "mps"])
|
|
def test_qwen_explicit_non_cuda_never_selects_another_device(monkeypatch, device):
|
|
selector = Mock(side_effect=AssertionError("must not override explicit placement"))
|
|
monkeypatch.setattr(loader, "_qwen_single_device", selector)
|
|
with pytest.raises(RuntimeError, match="explicit device"):
|
|
loader.qwen_hybrid_runtime_overrides(
|
|
_config(model_type="qwen3_5"), torch.bfloat16,
|
|
device=device, resolved_device=device,
|
|
)
|
|
selector.assert_not_called()
|
|
|
|
|
|
def _chmod_restricts_unlink(directory: Path) -> bool:
|
|
"""Probe whether a read-only directory really blocks unlinking here
|
|
(root, some network or FUSE filesystems ignore the mode bits)."""
|
|
probe = directory / "chmod-probe"
|
|
probe.mkdir()
|
|
(probe / "entry").write_bytes(b"x")
|
|
probe.chmod(0o500)
|
|
try:
|
|
os.unlink(probe / "entry")
|
|
except PermissionError:
|
|
return True
|
|
else:
|
|
return False
|
|
finally:
|
|
probe.chmod(0o700)
|
|
for child in probe.iterdir():
|
|
child.unlink()
|
|
probe.rmdir()
|
|
|
|
|
|
def _assert_left_on_disk_and_reported(handle, offload, caplog, error_name):
|
|
assert offload.exists()
|
|
assert handle._offload_dir is None and handle._owns_offload_dir is False
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("could not clean up the offload directory" in m and str(offload) in m
|
|
and error_name in m for m in messages), messages
|
|
|
|
|
|
@pytest.mark.skipif(os.name != "posix", reason="needs POSIX permissions")
|
|
def test_real_offload_removal_failure_is_reported_once_and_never_retried(tmp_path, caplog):
|
|
"""A genuine filesystem failure (not a patched rmtree) is reported at WARNING
|
|
with the path; the directory is left on disk and never touched again."""
|
|
if not _chmod_restricts_unlink(tmp_path):
|
|
pytest.skip("chmod does not restrict unlinking on this filesystem or user")
|
|
offload = tmp_path / "offload"
|
|
locked = offload / "locked"
|
|
locked.mkdir(parents=True)
|
|
(locked / "weights").write_bytes(b"x")
|
|
locked.chmod(0o500) # entries inside cannot be unlinked
|
|
handle = _owned_handle(offload)
|
|
try:
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
_assert_left_on_disk_and_reported(handle, offload, caplog, "PermissionError")
|
|
finally:
|
|
if locked.exists(): # never mask an unexpected successful removal
|
|
locked.chmod(0o700)
|
|
handle.cleanup() # removal is now possible, but it is never retried
|
|
assert (locked / "weights").read_bytes() == b"x"
|
|
|
|
|
|
@pytest.mark.parametrize(("function_name", "failing_name"), [("unlink", "weights"), ("rmdir", "locked")])
|
|
def test_offload_removal_errors_beneath_rmtree_are_collected_and_reported(
|
|
monkeypatch, tmp_path, caplog, function_name, failing_name,
|
|
):
|
|
"""Exercise the real rmtree error collector without depending on chmod."""
|
|
offload = tmp_path / "offload"
|
|
locked = offload / "locked"
|
|
locked.mkdir(parents=True)
|
|
(locked / "weights").write_bytes(b"x")
|
|
handle = _owned_handle(offload)
|
|
original = getattr(os, function_name)
|
|
|
|
def refusing(path, *args, **kwargs):
|
|
if os.path.basename(os.fsdecode(path)) == failing_name:
|
|
raise PermissionError(13, "Permission denied", os.fsdecode(path))
|
|
return original(path, *args, **kwargs)
|
|
|
|
with monkeypatch.context() as patched:
|
|
patched.setattr(os, function_name, refusing)
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
_assert_left_on_disk_and_reported(handle, offload, caplog, "PermissionError")
|
|
handle.cleanup()
|
|
assert offload.exists() # never retried
|
|
|
|
|
|
def _fail_first_offload_removal(monkeypatch):
|
|
"""Make the first removal attempt fail without removing anything."""
|
|
real = loader._remove_tree_collecting_errors
|
|
calls = []
|
|
|
|
def remove(path):
|
|
calls.append(path)
|
|
if len(calls) == 1:
|
|
return [PermissionError(13, "denied", path)]
|
|
return real(path)
|
|
|
|
monkeypatch.setattr(loader, "_remove_tree_collecting_errors", remove)
|
|
return calls
|
|
|
|
|
|
def _replace_path(offload, tmp_path, replacement):
|
|
"""Replace ``offload`` with a new directory or a symlink; return a file that must survive."""
|
|
if replacement == "symlink":
|
|
offload.rename(tmp_path / "moved-original")
|
|
target = tmp_path / "symlink-target"
|
|
target.mkdir()
|
|
(target / "other-owner").write_bytes(b"keep")
|
|
offload.symlink_to(target, target_is_directory=True)
|
|
return target / "other-owner"
|
|
if replacement == "directory":
|
|
# The original is kept alive elsewhere, so the replacement has a new inode.
|
|
offload.rename(tmp_path / "moved-original")
|
|
offload.mkdir()
|
|
else: # "reused-inode": on ext4 the new directory usually reuses the inode
|
|
original_inode = os.lstat(offload).st_ino
|
|
shutil.rmtree(offload)
|
|
for _ in range(20):
|
|
offload.mkdir()
|
|
if os.lstat(offload).st_ino == original_inode:
|
|
break
|
|
offload.rmdir()
|
|
else:
|
|
offload.mkdir()
|
|
(offload / "other-owner").write_bytes(b"keep")
|
|
return offload / "other-owner"
|
|
|
|
|
|
@pytest.mark.parametrize("replacement", ["directory", "symlink", "reused-inode"])
|
|
def test_failed_offload_removal_never_touches_a_later_replacement(
|
|
monkeypatch, tmp_path, caplog, replacement,
|
|
):
|
|
"""After one failed removal the handle lets go of the path: neither a later
|
|
cleanup nor the finalizer can remove whatever appears there afterwards."""
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
(offload / "weights").write_bytes(b"x")
|
|
handle = _owned_handle(offload)
|
|
calls = _fail_first_offload_removal(monkeypatch)
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
_assert_left_on_disk_and_reported(handle, offload, caplog, "PermissionError")
|
|
|
|
survivor = _replace_path(offload, tmp_path, replacement)
|
|
handle.cleanup()
|
|
assert handle.release_offload_dir() is None
|
|
del handle
|
|
gc.collect()
|
|
assert survivor.read_bytes() == b"keep"
|
|
assert calls == [str(offload)] # removal was attempted exactly once
|
|
|
|
|
|
def test_cleanup_and_copies_hold_no_descriptors_and_never_remove_twice(monkeypatch, tmp_path):
|
|
import copy
|
|
|
|
fd_dir = "/proc/self/fd"
|
|
open_fds = (lambda: len(os.listdir(fd_dir))) if os.path.isdir(fd_dir) else (lambda: 0)
|
|
gc.collect()
|
|
baseline = open_fds()
|
|
|
|
offload = tmp_path / "failed"
|
|
offload.mkdir()
|
|
handle = _owned_handle(offload)
|
|
assert open_fds() == baseline
|
|
with monkeypatch.context() as patched:
|
|
_fail_first_offload_removal(patched)
|
|
handle.cleanup()
|
|
assert open_fds() == baseline
|
|
del handle
|
|
gc.collect()
|
|
assert open_fds() == baseline and offload.exists()
|
|
|
|
# A shallow copy holds no path (``__getstate__`` drops ownership): the
|
|
# original's cleanup removes it and the copy has nothing to release, with
|
|
# no warning and no descriptor left open.
|
|
offload = tmp_path / "copied"
|
|
offload.mkdir()
|
|
handle = _owned_handle(offload)
|
|
twin = copy.copy(handle)
|
|
handle.cleanup()
|
|
assert not offload.exists()
|
|
assert twin.release_offload_dir() is None
|
|
del handle, twin
|
|
gc.collect()
|
|
assert open_fds() == baseline
|
|
|
|
|
|
def test_restore_point_close_failure_after_removal_is_never_retried(tmp_path):
|
|
"""DiskSnapshot.close() removes its storage before updating its policy; an
|
|
error after the removal must not lead to a second close (which would remove
|
|
whatever was created at that path since)."""
|
|
storage = tmp_path / "snapshot"
|
|
storage.mkdir()
|
|
|
|
def close():
|
|
shutil.rmtree(storage)
|
|
raise RuntimeError("policy update failed after removal")
|
|
|
|
restore_point = SimpleNamespace(policy={}, close=Mock(side_effect=close))
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot_policy = {"status": "created", "restore_available": True, "reason": "ok"}
|
|
handle._restore_point = restore_point
|
|
with pytest.raises(RuntimeError, match="after removal"):
|
|
handle.cleanup()
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy["status"] == "closed"
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
storage.mkdir()
|
|
(storage / "other-owner").write_bytes(b"keep")
|
|
handle.cleanup()
|
|
del handle
|
|
gc.collect()
|
|
assert restore_point.close.call_count == 1
|
|
assert (storage / "other-owner").read_bytes() == b"keep"
|
|
|
|
|
|
class _BrokenPolicy:
|
|
def __iter__(self):
|
|
raise RuntimeError("policy copy failed")
|
|
|
|
|
|
def test_restore_policy_copy_failure_never_recloses_and_reports_closed(tmp_path):
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
restore_point = SimpleNamespace(policy=_BrokenPolicy(), close=Mock(), restore=Mock())
|
|
handle = _owned_handle(offload)
|
|
handle.snapshot_policy = {"status": "created", "restore_available": True, "reason": "ok"}
|
|
handle._restore_point = restore_point
|
|
with pytest.raises(RuntimeError, match="policy copy failed"):
|
|
handle.cleanup()
|
|
assert not offload.exists() # the offload stage still ran
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy["status"] == "closed"
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
assert "could not be copied" in handle.snapshot_policy["reason"]
|
|
with pytest.raises(RuntimeError, match="No snapshot to restore"):
|
|
handle.restore()
|
|
restore_point.restore.assert_not_called()
|
|
restore_point.policy = {"status": "closed", "staging_bytes": 1}
|
|
handle.cleanup()
|
|
del handle
|
|
gc.collect()
|
|
assert restore_point.close.call_count == 1
|
|
|
|
|
|
def _copy_handle(handle, how):
|
|
import copy
|
|
import pickle
|
|
|
|
if how == "copy":
|
|
return copy.copy(handle)
|
|
if how == "deepcopy":
|
|
return copy.deepcopy(handle)
|
|
return pickle.loads(pickle.dumps(handle))
|
|
|
|
|
|
@pytest.mark.parametrize("first_removal", ["fails", "succeeds"])
|
|
@pytest.mark.parametrize("how", ["copy", "deepcopy", "pickle"])
|
|
def test_handle_copies_own_no_offload_dir_or_restore_point(monkeypatch, tmp_path, how, first_removal):
|
|
"""A copy (or unpickled handle) must not inherit cleanup ownership: after the
|
|
original lets go of the path, the copy's cleanup and finalizer leave whatever
|
|
replaced it, and the shared restore point is closed exactly once."""
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
(offload / "weights").write_bytes(b"x")
|
|
close_calls = []
|
|
handle = loader.ModelHandle(
|
|
torch.nn.Linear(1, 1), SimpleNamespace(), _config(), "x", "causal_lm",
|
|
_offload_dir=str(offload), _owns_offload_dir=True,
|
|
)
|
|
handle._restore_point = _CountingRestorePoint(close_calls)
|
|
handle.snapshot_policy = {"status": "created", "restore_available": True, "reason": "ok"}
|
|
twin = _copy_handle(handle, how)
|
|
assert twin._offload_dir is None and twin._owns_offload_dir is False
|
|
assert twin._restore_point is None
|
|
assert twin.snapshot_policy["restore_available"] is False
|
|
assert handle._owns_offload_dir is True and handle._restore_point is not None
|
|
|
|
with monkeypatch.context() as patched:
|
|
if first_removal == "fails":
|
|
_fail_first_offload_removal(patched)
|
|
handle.cleanup()
|
|
survivor = _replace_path(offload, tmp_path, "directory") if offload.exists() else None
|
|
if survivor is None:
|
|
offload.mkdir()
|
|
survivor = offload / "other-owner"
|
|
survivor.write_bytes(b"keep")
|
|
twin.cleanup()
|
|
del handle, twin
|
|
gc.collect()
|
|
assert survivor.read_bytes() == b"keep"
|
|
assert len(close_calls) == 1
|
|
|
|
|
|
class _CountingRestorePoint:
|
|
"""Picklable stand-in for a restore point that records close() calls."""
|
|
|
|
def __init__(self, calls):
|
|
self.calls = calls
|
|
self.policy = {"status": "created", "restore_available": True}
|
|
|
|
def close(self):
|
|
self.calls.append(1)
|
|
self.policy = {"status": "closed", "restore_available": False}
|
|
|
|
|
|
def test_unreadable_snapshot_policy_never_skips_close_or_offload_stage(tmp_path, caplog):
|
|
class UnreadablePolicy:
|
|
def keys(self):
|
|
raise RuntimeError("corrupt policy")
|
|
|
|
def __getitem__(self, key):
|
|
raise RuntimeError("corrupt policy")
|
|
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
restore_point = SimpleNamespace(policy={"status": "closed", "staging_bytes": 1}, close=Mock())
|
|
handle = _owned_handle(offload)
|
|
handle.snapshot_policy = UnreadablePolicy()
|
|
handle._restore_point = restore_point
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup() # the unreadable old policy is reported, not raised
|
|
restore_point.close.assert_called_once_with()
|
|
assert not offload.exists() # the offload stage still ran
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy == {"status": "closed", "staging_bytes": 1}
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("snapshot policy" in m and "corrupt policy" in m for m in messages), messages
|
|
handle.cleanup()
|
|
del handle
|
|
gc.collect()
|
|
assert restore_point.close.call_count == 1
|
|
|
|
|
|
def test_unreadable_snapshot_policy_with_failed_close_still_reports_closed(tmp_path):
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
restore_point = SimpleNamespace(policy={}, close=Mock(side_effect=RuntimeError("close failed")))
|
|
handle = _owned_handle(offload)
|
|
handle.snapshot_policy = None # not a mapping
|
|
handle._restore_point = restore_point
|
|
with pytest.raises(RuntimeError, match="close failed"):
|
|
handle.cleanup()
|
|
assert not offload.exists()
|
|
assert handle.snapshot_policy["status"] == "closed"
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
assert restore_point.close.call_count == 1
|
|
|
|
|
|
def test_finalizer_logs_failures_from_both_stages(monkeypatch, tmp_path, caplog):
|
|
unraisable = []
|
|
monkeypatch.setattr(sys, "unraisablehook", unraisable.append)
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
monkeypatch.setattr(loader, "_remove_tree_collecting_errors",
|
|
lambda path: [PermissionError(13, "denied", path)])
|
|
handle = loader.ModelHandle(
|
|
_model(), SimpleNamespace(), _config(), "x", "causal_lm",
|
|
_offload_dir=str(offload), _owns_offload_dir=True,
|
|
)
|
|
handle._restore_point = SimpleNamespace(policy={}, close=Mock(side_effect=AssertionError("close")))
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
del handle
|
|
gc.collect()
|
|
assert unraisable == []
|
|
messages = [r.getMessage() for r in caplog.records if "finalizer could not clean up" in r.getMessage()]
|
|
assert len(messages) == 2
|
|
assert "restore point" in messages[0] and "AssertionError: close" in messages[0]
|
|
assert "offload directory" in messages[1] and "PermissionError" in messages[1]
|
|
|
|
|
|
class _InterruptingPolicyRestorePoint:
|
|
"""A restore point whose final policy read is interrupted after close()."""
|
|
|
|
def __init__(self):
|
|
self.close_calls = 0
|
|
|
|
def close(self):
|
|
self.close_calls += 1
|
|
|
|
@property
|
|
def policy(self):
|
|
raise KeyboardInterrupt("interrupted policy copy")
|
|
|
|
|
|
@pytest.mark.parametrize("where", ["close", "policy"])
|
|
def test_base_exception_mid_cleanup_keeps_offload_owned_and_names_it(tmp_path, caplog, where):
|
|
"""#651: an interrupt in the restore-point stage skips the offload stage of
|
|
that call; ownership is kept (so a later cleanup removes it) and a WARNING
|
|
names the path that is still on disk."""
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
(offload / "weights").write_bytes(b"x")
|
|
handle = _owned_handle(offload)
|
|
if where == "close":
|
|
restore_point = SimpleNamespace(policy={}, close=Mock(side_effect=KeyboardInterrupt("stop")))
|
|
else:
|
|
restore_point = _InterruptingPolicyRestorePoint()
|
|
handle._restore_point = restore_point
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
with pytest.raises(KeyboardInterrupt):
|
|
handle.cleanup()
|
|
assert offload.exists()
|
|
assert handle._offload_dir == str(offload) and handle._owns_offload_dir is True
|
|
assert handle._restore_point is None # never closed a second time
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("interrupted" in m and str(offload) in m for m in messages), messages
|
|
|
|
handle.cleanup()
|
|
assert not offload.exists()
|
|
calls = restore_point.close.call_count if where == "close" else restore_point.close_calls
|
|
assert calls == 1
|
|
|
|
|
|
def test_top_level_symlink_offload_dir_is_never_followed_on_first_attempt(tmp_path, caplog):
|
|
"""#651: if the owned path is a symlink at the first (only) removal attempt,
|
|
rmtree refuses it; the target's contents survive and the failure is reported."""
|
|
target = tmp_path / "target"
|
|
target.mkdir()
|
|
(target / "other-owner").write_bytes(b"keep")
|
|
offload = tmp_path / "offload"
|
|
offload.symlink_to(target, target_is_directory=True)
|
|
handle = _owned_handle(offload)
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
assert offload.is_symlink()
|
|
assert (target / "other-owner").read_bytes() == b"keep"
|
|
_assert_left_on_disk_and_reported(handle, offload, caplog, "OSError")
|
|
handle.cleanup()
|
|
del handle
|
|
gc.collect()
|
|
assert (target / "other-owner").read_bytes() == b"keep"
|
|
|
|
|
|
def test_handle_unpickled_in_a_subprocess_never_removes_the_original_storage(tmp_path):
|
|
"""#651: a handle unpickled in another process owns no offload directory or
|
|
restore point; its cleanup and finalizer there leave the original's files."""
|
|
import pickle
|
|
import subprocess
|
|
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
(offload / "weights").write_bytes(b"x")
|
|
close_calls = []
|
|
handle = loader.ModelHandle(
|
|
torch.nn.Linear(1, 1), SimpleNamespace(), _config(), "x", "causal_lm",
|
|
_offload_dir=str(offload), _owns_offload_dir=True,
|
|
)
|
|
handle._restore_point = _CountingRestorePoint(close_calls)
|
|
payload = tmp_path / "handle.pickle"
|
|
payload.write_bytes(pickle.dumps(handle))
|
|
child = (
|
|
"import gc, pickle, sys\n"
|
|
"handle = pickle.loads(open(sys.argv[1], 'rb').read())\n"
|
|
"assert handle._offload_dir is None and handle._owns_offload_dir is False\n"
|
|
"assert handle._restore_point is None\n"
|
|
"assert handle.snapshot_policy['restore_available'] is False\n"
|
|
"handle.cleanup()\n"
|
|
"del handle\n"
|
|
"gc.collect()\n"
|
|
)
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", child, str(payload)], capture_output=True, text=True,
|
|
cwd=Path(__file__).resolve().parents[1], timeout=300, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert (offload / "weights").read_bytes() == b"x"
|
|
assert handle._owns_offload_dir is True and close_calls == []
|
|
handle.cleanup()
|
|
assert not offload.exists() and close_calls == [1]
|
|
|
|
|
|
class _FalsyUnreadablePolicy:
|
|
def __len__(self):
|
|
return 0
|
|
|
|
def __iter__(self):
|
|
raise RuntimeError("corrupt falsy policy")
|
|
|
|
|
|
def test_falsy_unreadable_snapshot_policy_is_reported_but_none_is_not(tmp_path, caplog):
|
|
"""#651: only a missing (None) policy starts from empty silently; a falsy
|
|
object that cannot be read as a mapping is recorded as a policy failure."""
|
|
for policy, reported in ((None, False), ({}, False), (_FalsyUnreadablePolicy(), True)):
|
|
caplog.clear()
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot_policy = policy
|
|
handle._restore_point = SimpleNamespace(policy={"status": "closed"}, close=Mock())
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("snapshot policy" in m and "corrupt falsy policy" in m for m in messages) is reported
|
|
assert handle.snapshot_policy == {"status": "closed"}
|
|
|
|
|
|
def test_real_snapshot_close_failure_is_raised_once_and_finalizer_never_raises(
|
|
monkeypatch, tmp_path, caplog,
|
|
):
|
|
"""#639 through the handle: a DiskSnapshot that cannot remove its files makes
|
|
explicit cleanup() raise (naming the path) and the finalizer only log."""
|
|
from obliteratus.models.snapshot import SnapshotError
|
|
|
|
unraisable = []
|
|
monkeypatch.setattr(sys, "unraisablehook", unraisable.append)
|
|
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
|
|
original = os.unlink
|
|
|
|
def refusing(path, *args, **kwargs):
|
|
if os.fsdecode(path).endswith(".safetensors"):
|
|
raise PermissionError(13, "Permission denied", os.fsdecode(path))
|
|
return original(path, *args, **kwargs)
|
|
|
|
for explicit in (True, False):
|
|
handle = loader.ModelHandle(torch.nn.Linear(2, 2), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot()
|
|
storage = handle._restore_point.path
|
|
with monkeypatch.context() as patched:
|
|
patched.setattr(os, "unlink", refusing)
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
if explicit:
|
|
with pytest.raises(SnapshotError, match="could not be fully removed") as raised:
|
|
handle.cleanup()
|
|
assert str(storage) in str(raised.value)
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
assert "close failed" in handle.snapshot_policy["reason"]
|
|
handle.cleanup() # never retried through the handle
|
|
else:
|
|
del handle
|
|
gc.collect()
|
|
assert storage.exists()
|
|
shutil.rmtree(storage)
|
|
assert unraisable == []
|
|
assert any("finalizer could not clean up the restore point" in r.getMessage()
|
|
and str(storage) in r.getMessage() for r in caplog.records)
|
|
|
|
|
|
def test_snapshot_replacement_reports_but_survives_old_close_failure(monkeypatch, tmp_path, caplog):
|
|
"""Replacing a restore point installs the new one even when the old one's
|
|
files cannot be removed; the leftover path is logged at WARNING."""
|
|
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
|
|
handle = loader.ModelHandle(torch.nn.Linear(2, 2), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle.snapshot()
|
|
old = handle._restore_point
|
|
original = os.unlink
|
|
|
|
def refusing(path, *args, **kwargs):
|
|
# Creating the replacement unlinks nothing; only the old close() does.
|
|
if os.fsdecode(path).endswith(".safetensors"):
|
|
raise PermissionError(13, "Permission denied", os.fsdecode(path))
|
|
return original(path, *args, **kwargs)
|
|
|
|
with monkeypatch.context() as patched:
|
|
patched.setattr(os, "unlink", refusing)
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.snapshot()
|
|
assert handle._restore_point is not old and handle._restore_point.state == "ready"
|
|
assert handle.snapshot_policy["restore_available"] is True
|
|
assert old.path.exists() and old.state != "closed"
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any(str(old.path) in m for m in messages), messages
|
|
old.close()
|
|
handle.cleanup()
|
|
assert not old.path.exists()
|
|
|
|
|
|
class _InterruptingReadPolicy:
|
|
"""A mapping-like policy whose copy is interrupted."""
|
|
|
|
def __init__(self, values):
|
|
self.values = values
|
|
|
|
def keys(self):
|
|
raise KeyboardInterrupt("interrupted initial policy copy")
|
|
|
|
def __getitem__(self, key):
|
|
return self.values[key]
|
|
|
|
|
|
@pytest.mark.parametrize("where", ["initial_policy", "close"])
|
|
def test_interrupted_restore_point_close_never_reports_an_available_restore_point(
|
|
tmp_path, caplog, where,
|
|
):
|
|
"""#659 review: an interrupt while copying the old policy or inside close()
|
|
still leaves ``snapshot_policy`` closed/unavailable with an incomplete-close
|
|
reason, and the WARNING names the snapshot path."""
|
|
storage = tmp_path / "obliteratus-snapshot-x"
|
|
storage.mkdir()
|
|
close = Mock(side_effect=KeyboardInterrupt("stop") if where == "close" else None)
|
|
restore_point = SimpleNamespace(path=storage, policy={"status": "closed"}, close=close)
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
policy = {"status": "disk_backed", "restore_available": True, "reason": "ok"}
|
|
handle.snapshot_policy = _InterruptingReadPolicy(policy) if where == "initial_policy" else policy
|
|
handle._restore_point = restore_point
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
with pytest.raises(KeyboardInterrupt):
|
|
handle.cleanup()
|
|
assert handle._restore_point is None
|
|
assert handle.snapshot_policy["status"] == "closed"
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
assert "incomplete" in handle.snapshot_policy["reason"]
|
|
assert str(storage) in handle.snapshot_policy["reason"]
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("interrupted" in m and str(storage) in m for m in messages), messages
|
|
handle.cleanup()
|
|
assert close.call_count == (1 if where == "close" else 0)
|
|
|
|
|
|
def test_cleanup_helpers_never_replace_an_inflight_error_and_raise_otherwise(caplog):
|
|
from obliteratus.models.snapshot import SnapshotError
|
|
|
|
failing = SimpleNamespace(cleanup=Mock(side_effect=SnapshotError("left at /x/snap")))
|
|
original = ValueError("primary")
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
with pytest.raises(ValueError, match="primary") as raised:
|
|
with loader.cleanup_on_exit(failing):
|
|
raise original
|
|
assert raised.value is original
|
|
assert any("/x/snap" in note for note in original.__notes__)
|
|
assert any("/x/snap" in r.getMessage() for r in caplog.records)
|
|
with pytest.raises(SnapshotError, match="/x/snap"):
|
|
with loader.cleanup_on_exit(failing):
|
|
pass
|
|
assert failing.cleanup.call_count == 2
|
|
with loader.cleanup_on_exit(SimpleNamespace(cleanup=None)):
|
|
pass # handles without a callable cleanup are ignored
|
|
|
|
|
|
@pytest.mark.parametrize("owned", [1, "yes"])
|
|
def test_non_true_ownership_flag_drops_the_path_with_a_warning(tmp_path, caplog, owned):
|
|
"""#659 review: a truthy non-True flag is never treated as ownership, and
|
|
letting go of the path is logged with it instead of happening silently."""
|
|
offload = tmp_path / "offload"
|
|
offload.mkdir()
|
|
handle = _owned_handle(offload)
|
|
handle._owns_offload_dir = owned
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.cleanup()
|
|
assert offload.exists()
|
|
assert handle._offload_dir is None and handle._owns_offload_dir is False
|
|
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
|
|
assert any("without removing it" in m and str(offload) in m for m in messages), messages
|
|
|
|
|
|
class _InterruptOnSecondDetachStore(loader.ModelHandle):
|
|
"""Raise KeyboardInterrupt instead of the second of the two stores that
|
|
detach the restore point and publish the closed policy (#669)."""
|
|
|
|
def __setattr__(self, name, value):
|
|
stores = self.__dict__.get("_stores")
|
|
if stores is not None and name in ("_restore_point", "snapshot_policy"):
|
|
stores.append(name)
|
|
if len(stores) == 2:
|
|
raise KeyboardInterrupt("between detach stores")
|
|
object.__setattr__(self, name, value)
|
|
|
|
|
|
def test_closed_policy_is_stored_before_the_restore_point_is_detached(tmp_path):
|
|
"""#669: an interrupt between the two stores never leaves a detached restore
|
|
point whose policy still claims restore is available."""
|
|
close = Mock()
|
|
restore_point = SimpleNamespace(path=tmp_path / "snap", policy={"status": "closed"},
|
|
close=close)
|
|
handle = _InterruptOnSecondDetachStore(_model(), SimpleNamespace(), _config(), "x",
|
|
"causal_lm")
|
|
handle.snapshot_policy = {"status": "disk_backed", "restore_available": True}
|
|
handle._restore_point = restore_point
|
|
handle.__dict__["_stores"] = []
|
|
with pytest.raises(KeyboardInterrupt):
|
|
handle.cleanup()
|
|
assert handle.snapshot_policy["restore_available"] is False
|
|
assert handle.snapshot_policy["status"] == "closed"
|
|
# The restore point is still tracked, so a later cleanup closes it once.
|
|
assert handle._restore_point is restore_point
|
|
del handle.__dict__["_stores"]
|
|
handle.cleanup()
|
|
close.assert_called_once_with()
|
|
assert handle._restore_point is None
|
|
|
|
|
|
def test_handle_surfaced_snapshot_error_names_the_path_instead_of_a_retry(tmp_path, monkeypatch):
|
|
"""#669: a handle cannot retry DiskSnapshot.close(), so the error it raises
|
|
tells the user which path to remove instead."""
|
|
from obliteratus.models.snapshot import DiskSnapshot, SnapshotError
|
|
|
|
model = torch.nn.Linear(1, 1, bias=False)
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
snapshot = DiskSnapshot(model, directory=tmp_path)
|
|
handle._restore_point = snapshot
|
|
original = os.unlink
|
|
|
|
def refusing(path, *args, **kwargs):
|
|
if os.fsdecode(path).endswith(".safetensors"):
|
|
raise PermissionError(13, "Permission denied", os.fsdecode(path))
|
|
return original(path, *args, **kwargs)
|
|
|
|
with monkeypatch.context() as patched:
|
|
patched.setattr(os, "unlink", refusing)
|
|
with pytest.raises(SnapshotError) as raised:
|
|
handle.cleanup()
|
|
text = "\n".join([str(raised.value), *getattr(raised.value, "__notes__", [])])
|
|
assert "retry DiskSnapshot.close()" not in text
|
|
# #675 D4: the reason already names the path, so the hint does not repeat it.
|
|
assert f"remove {snapshot.path} manually" not in str(raised.value)
|
|
assert str(raised.value).endswith("; remove the restore point directory manually")
|
|
assert "retry" not in handle.snapshot_policy["reason"]
|
|
assert str(snapshot.path) in handle.snapshot_policy["reason"]
|
|
snapshot.close()
|
|
|
|
|
|
def test_interrupt_raised_by_cleanup_propagates_with_primary_error_as_context():
|
|
"""#669: cleanup_during_error notes cleanup Exceptions, but an interrupt
|
|
raised by cleanup itself propagates (documented choice); the primary error
|
|
stays reachable as its __context__."""
|
|
handle = SimpleNamespace(cleanup=Mock(side_effect=KeyboardInterrupt("stop")))
|
|
original = ValueError("primary")
|
|
with pytest.raises(KeyboardInterrupt) as raised:
|
|
with loader.cleanup_on_exit(handle):
|
|
raise original
|
|
assert raised.value.__context__ is original
|
|
assert "KeyboardInterrupt" in loader.cleanup_during_error.__doc__
|
|
|
|
|
|
def test_replaced_restore_point_warning_names_the_path_instead_of_a_retry(monkeypatch, caplog):
|
|
"""#671 review: the replaced restore point is unreachable after snapshot(),
|
|
so its logged close failure names the path to remove, not a retry."""
|
|
from obliteratus.models import snapshot as snapshot_module
|
|
from obliteratus.models.snapshot import SnapshotError
|
|
|
|
error = SnapshotError("files could not be fully removed from /x/old: 1 error(s); "
|
|
"retry DiskSnapshot.close() or remove the path manually")
|
|
error.close_failure_reason = "files could not be fully removed from /x/old: 1 error(s)"
|
|
error.close_failure_path = "/x/old"
|
|
previous = SimpleNamespace(close=Mock(side_effect=error))
|
|
replacement = SimpleNamespace(policy={"status": "disk_backed", "restore_available": True})
|
|
monkeypatch.setattr(snapshot_module, "DiskSnapshot", lambda *_a, **_k: replacement)
|
|
handle = loader.ModelHandle(_model(), SimpleNamespace(), _config(), "x", "causal_lm")
|
|
handle._restore_point = previous
|
|
with caplog.at_level(logging.WARNING, logger=loader.logger.name):
|
|
handle.snapshot()
|
|
messages = [r.getMessage() for r in caplog.records if "replaced restore point" in r.getMessage()]
|
|
assert messages and all("retry" not in m and m.count("/x/old") == 1
|
|
and "remove the restore point directory manually" in m
|
|
for m in messages)
|
|
assert handle._restore_point is replacement
|
|
|
|
|
|
@pytest.mark.parametrize("reason_names_path", [True, False])
|
|
def test_rephrased_snapshot_error_names_the_path_once(reason_names_path):
|
|
"""#675 D4: the handle's rewording names the path exactly once, adding it
|
|
only when the close-failure reason does not already name it."""
|
|
from obliteratus.models.snapshot import SnapshotError
|
|
|
|
reason = ("restore point files could not be fully removed from /x/old: path still exists"
|
|
if reason_names_path else "restore point removal did not complete")
|
|
error = SnapshotError(reason + "; retry DiskSnapshot.close() or remove the path manually")
|
|
error.close_failure_reason, error.close_failure_path = reason, "/x/old"
|
|
loader._rephrase_for_handle(error)
|
|
message = str(error)
|
|
assert message.count("/x/old") == 1 and "retry" not in message
|
|
assert message.startswith(reason) and message.endswith(" manually")
|