Files
OBLITERATUS/tests/test_ui_model_load.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

723 lines
33 KiB
Python

"""Offline UI load cleanup, ownership, placement and shared admission contracts."""
from __future__ import annotations
import gc
import threading
from types import SimpleNamespace
from unittest.mock import Mock
import weakref
import pytest
import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig, LlamaConfig, Qwen2Config
from obliteratus import phase_admission as admission
from obliteratus import ui_model_load as ui
from obliteratus.models import _load_admission, loader
from obliteratus.phase_admission import GIB, MemoryObservation, PhaseAdmissionError
class AttemptFailure(BaseException):
"""Exercise cleanup beyond ordinary Exception handlers."""
class FakeModel:
def __init__(self, moves):
self.moves = moves
def to(self, device):
self.moves.append(device)
return self
def eval(self):
return self
def modules(self):
return ()
def ample_memory(device):
return MemoryObservation(device, 100 * GIB, 100 * GIB)
@pytest.fixture(autouse=True)
def isolated_admission(monkeypatch):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
@pytest.fixture(params=[LlamaConfig, Qwen2Config], ids=["llama", "qwen2"])
def tiny_config(request):
return request.param(
vocab_size=32, hidden_size=16, intermediate_size=32,
num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=2,
max_position_embeddings=32, dtype="float32",
)
@pytest.fixture
def attempt(monkeypatch, tmp_path, tiny_config):
events, moves, refs = [], [], []
tokenizer = SimpleNamespace(pad_token=None, eos_token="<eos>")
def weights(*args, **kwargs):
events.append("weights")
model = FakeModel(moves)
refs.append(weakref.ref(model))
return model
lifecycle = SimpleNamespace(
loading=Mock(side_effect=lambda name: events.append("loading")),
resize=Mock(side_effect=lambda memory: events.append("resize")),
ready=Mock(side_effect=lambda memory: events.append("ready")),
release=Mock(side_effect=lambda **kwargs: events.append(("release", kwargs["reason"]))),
)
device_module = SimpleNamespace(
get_device=Mock(side_effect=lambda device: "cpu" if device == "auto" else device),
supports_device_map_auto=Mock(return_value=False),
supports_bitsandbytes=Mock(return_value=True), device_count=Mock(return_value=2),
free_gpu_memory=Mock(side_effect=gc.collect),
)
config_class = SimpleNamespace(from_pretrained=Mock(return_value=tiny_config))
tokenizer_class = SimpleNamespace(from_pretrained=Mock(
side_effect=lambda *args, **kwargs: (events.append("tokenizer"), tokenizer)[1],
))
weight_loader = Mock(side_effect=weights)
monkeypatch.setattr(loader, "_bounded_max_memory", Mock(
return_value={0: "8192MiB", 1: "4096MiB", "cpu": "16384MiB"},
))
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", weight_loader)
options = dict(
config_class=config_class, model_class=AutoModelForCausalLM,
tokenizer_class=tokenizer_class, lifecycle=lifecycle, device_module=device_module,
torch_module=torch, torch_dtype=torch.float32, admission_observer=ample_memory,
measure_memory=Mock(return_value={"allocated": 0}), local_files_only=True,
)
return SimpleNamespace(
path=tmp_path, config=tiny_config, options=options, events=events, moves=moves,
refs=refs, weights=weight_loader, lifecycle=lifecycle, dev=device_module,
config_class=config_class, tokenizer_class=tokenizer_class, tokenizer=tokenizer,
)
@pytest.mark.parametrize("failure", [
"loading", "config", "admission", "weights", "move", "tokenizer", "hook",
"measurement", "resize", "ready",
])
def test_failed_attempt_clears_references_and_releases_once(attempt, monkeypatch, failure):
error = AttemptFailure(failure)
def fail(*args, **kwargs):
raise error
if failure in {"loading", "resize", "ready"}:
getattr(attempt.lifecycle, failure).side_effect = fail
elif failure == "config":
attempt.config_class.from_pretrained.side_effect = fail
elif failure == "admission":
monkeypatch.setattr(admission, "admit_model_load", fail)
elif failure == "weights":
def allocate_then_fail(*args, **kwargs):
partial = FakeModel(attempt.moves)
attempt.refs.append(weakref.ref(partial))
raise error
attempt.weights.side_effect = allocate_then_fail
elif failure == "move":
monkeypatch.setattr(FakeModel, "to", fail)
elif failure == "tokenizer":
attempt.tokenizer_class.from_pretrained.side_effect = fail
elif failure == "hook":
attempt.options["steering_hook"] = fail
else:
attempt.options["measure_memory"] = fail
with pytest.raises(AttemptFailure) as caught:
ui.load_chat_model(attempt.path, **attempt.options)
assert caught.value is error
attempt.lifecycle.release.assert_called_once_with(reason="load_failed")
attempt.dev.free_gpu_memory.assert_called_once_with()
assert attempt.events[-1] == ("release", "load_failed")
if failure != "ready":
attempt.lifecycle.ready.assert_not_called()
if failure in {"loading", "config", "admission", "tokenizer"}:
attempt.weights.assert_not_called()
assert all(reference() is None for reference in attempt.refs)
@pytest.mark.parametrize("broken_cleanup", ["free_gpu_memory", "release"])
def test_cleanup_errors_do_not_mask_original_error(attempt, broken_cleanup):
error = RuntimeError("configuration failed")
attempt.config_class.from_pretrained.side_effect = error
target = attempt.dev if broken_cleanup == "free_gpu_memory" else attempt.lifecycle
getattr(target, broken_cleanup).side_effect = RuntimeError("cleanup also failed")
with pytest.raises(RuntimeError) as caught:
ui.load_chat_model(attempt.path, **attempt.options)
assert caught.value is error
attempt.dev.free_gpu_memory.assert_called_once_with()
attempt.lifecycle.release.assert_called_once_with(reason="load_failed")
def test_success_is_publishable_before_ready(attempt):
def hook(model):
assert model is attempt.refs[-1]()
attempt.events.append("hook")
attempt.options["steering_hook"] = hook
model, tokenizer = ui.load_chat_model(attempt.path, **attempt.options)
assert model is attempt.refs[-1]() and tokenizer is attempt.tokenizer
assert tokenizer.pad_token == tokenizer.eos_token
assert attempt.events == ["loading", "tokenizer", "weights", "hook", "resize", "ready"]
attempt.lifecycle.release.assert_not_called()
attempt.dev.free_gpu_memory.assert_not_called()
attempt.dev.supports_device_map_auto.assert_called_once_with()
@pytest.mark.parametrize("device,mode,missing,expect_reject", [
("cuda:0", "default", False, True),
("cpu", "default", False, False),
("cpu", "strict", False, True),
("cuda:0", "off", False, False),
("cuda:0", "default", True, True),
("cpu", "default", True, False),
])
def test_real_uncertainty_policy(attempt, monkeypatch, caplog, device, mode, missing,
expect_reject):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
attempt.options.update(
device=device,
admission_observer=lambda name: None if missing else MemoryObservation(name, 0, GIB),
)
if expect_reject:
with pytest.raises(PhaseAdmissionError):
ui.load_chat_model(attempt.path, **attempt.options)
attempt.weights.assert_not_called()
attempt.lifecycle.release.assert_called_once_with(reason="load_failed")
else:
ui.load_chat_model(attempt.path, **attempt.options)
attempt.weights.assert_called_once()
assert caplog.records
if mode == "off":
assert "ADMISSION DISABLED" in caplog.text
else:
assert "WARNING" in caplog.text or "estimated" in caplog.text.lower()
@pytest.mark.parametrize("fits,mode", [(False, "default"), (True, "default"), (True, "strict")])
def test_metadata_failure_retains_calibrated_floor(attempt, monkeypatch, fits, mode):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
monkeypatch.setattr(_load_admission, "_checkpoint_layout", Mock(
side_effect=ValueError("broken shard metadata"),
))
attempt.options.update(device="cuda:0", admission_observer=(
ample_memory if fits else lambda name: MemoryObservation(name, 0, GIB)
))
if fits and mode == "default":
reports = []
original = admission.admit_model_load
def capture(*args, **kwargs):
report = original(*args, **kwargs)
reports.append(report)
return report
monkeypatch.setattr(admission, "admit_model_load", capture)
ui.load_chat_model(attempt.path, **attempt.options)
assert reports[-1].warnings
assert any(component.bytes is None for component in reports[-1].components)
assert any(component.confidence == "calibrated" and component.bytes > 0
for component in reports[-1].components)
attempt.weights.assert_called_once()
else:
with pytest.raises(PhaseAdmissionError) as caught:
ui.load_chat_model(attempt.path, **attempt.options)
if not fits:
assert caught.value.required_bytes > 0
attempt.weights.assert_not_called()
def test_invalid_admission_mode_fails_closed(attempt, monkeypatch):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "invalid")
with pytest.raises(ValueError, match="Invalid OBLITERATUS_PHASE_ADMISSION"):
ui.load_chat_model(attempt.path, **attempt.options)
attempt.weights.assert_not_called()
attempt.lifecycle.release.assert_called_once_with(reason="load_failed")
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
@pytest.mark.parametrize("source", ["declared", "header", "text_config"])
def test_omitted_dtype_uses_saved_floating_dtype(attempt, monkeypatch, dtype, source):
attempt.options["torch_dtype"] = None
if source == "declared":
attempt.config.dtype = dtype
else:
attempt.config.dtype = None
if source == "text_config":
attempt.config.text_config = SimpleNamespace(dtype=dtype)
else:
from safetensors.torch import save_file
save_file({"model.embed_tokens.weight": torch.zeros(32, 16, dtype=dtype)},
str(attempt.path / "model.safetensors"))
spy = Mock(return_value=None)
monkeypatch.setattr(admission, "admit_model_load", spy)
ui.load_chat_model(attempt.path, **attempt.options)
assert spy.call_args.kwargs["dtype"] == str(dtype).removeprefix("torch.")
assert "torch_dtype" not in attempt.weights.call_args.kwargs
@pytest.mark.parametrize("mode,reject", [("default", False), ("strict", True), ("off", False)])
@pytest.mark.parametrize("unknown", [
"dtype", "explicit_dtype", "native_explicit_dtype", "quantizer",
"quantizer_4bit_flags", "quantizer_8bit_flags",
])
def test_unresolvable_plan_uses_unknown_demand(attempt, monkeypatch, mode, reject, unknown):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
if unknown == "dtype":
attempt.options["torch_dtype"] = None
attempt.config.dtype = None
elif unknown in {"explicit_dtype", "native_explicit_dtype"}:
attempt.options["torch_dtype"] = "unresolvable_dtype"
if unknown == "native_explicit_dtype":
attempt.config.quantization_config = {"quant_method": "mxfp4"}
elif unknown.startswith("quantizer_"):
attempt.options["device"] = "cpu"
attempt.dev.supports_bitsandbytes.return_value = False
attempt.options["quantization_config"] = SimpleNamespace(
load_in_4bit=unknown == "quantizer_4bit_flags",
load_in_8bit=unknown == "quantizer_8bit_flags",
)
else:
attempt.options["quantization_config"] = object()
reports, demands = [], []
original = admission.admit_phase
def capture(*args, **kwargs):
assert admission._warning_scope.get() is not None
demands.append(args[1])
report = original(*args, **kwargs)
reports.append(report)
return report
monkeypatch.setattr(admission, "admit_phase", capture)
model_admission = Mock(side_effect=AssertionError("unknown plan must use admit_phase"))
monkeypatch.setattr(admission, "admit_model_load", model_admission)
if reject:
with pytest.raises(PhaseAdmissionError):
ui.load_chat_model(attempt.path, **attempt.options)
attempt.weights.assert_not_called()
else:
ui.load_chat_model(attempt.path, **attempt.options)
assert reports[-1].bypassed == (mode == "off")
if mode != "off":
assert any(component.bytes is None for component in reports[-1].components)
assert reports[-1].warnings
if unknown in {"explicit_dtype", "native_explicit_dtype"}:
expected = "auto" if unknown == "native_explicit_dtype" else "unresolvable_dtype"
assert attempt.weights.call_args.kwargs["torch_dtype"] == expected
if unknown.startswith("quantizer_"):
assert attempt.weights.call_args.kwargs["quantization_config"] is (
attempt.options["quantization_config"]
)
assert demands and all(component.bytes is None for component in demands[-1].components)
assert all(component.device == "cpu" for component in demands[-1].components)
model_admission.assert_not_called()
@pytest.mark.parametrize("dtype", ["float16", "bfloat16", "float32"])
def test_explicit_nonnative_dtype_string_is_forwarded_unchanged(attempt, monkeypatch, dtype):
attempt.options["torch_dtype"] = dtype
original = admission.admit_model_load
spy = Mock(wraps=original)
monkeypatch.setattr(admission, "admit_model_load", spy)
ui.load_chat_model(attempt.path, **attempt.options)
assert attempt.weights.call_args.kwargs["torch_dtype"] == dtype
assert spy.call_args.kwargs["dtype"] == dtype
assert attempt.moves == ["cpu"]
assert attempt.events == ["loading", "tokenizer", "weights", "resize", "ready"]
def write_checkpoint(path):
path.mkdir()
for name in ("config.json", "tokenizer_config.json", "abliteration_metadata.json"):
(path / name).write_text("{}", encoding="utf-8")
(path / "model.safetensors").write_bytes(b"weights")
return path
@pytest.mark.parametrize("kind,recoverable", [
("valid", True), ("missing", False), ("empty", False), ("corrupt", False),
])
def test_checkpoint_recoverability_and_rollback(tmp_path, kind, recoverable):
path = tmp_path / "checkpoint"
if kind in {"valid", "corrupt"}:
write_checkpoint(path)
if kind == "corrupt":
(path / "config.json").write_text("{broken", encoding="utf-8")
elif kind == "empty":
path.mkdir()
assert ui.checkpoint_recoverable(path) is recoverable
state = dict(model=object(), tokenizer=object(), status="ready", output_dir=str(path),
model_name="A", method="advanced", load_settings={"quant": "Auto"},
steering=object())
metadata = {key: state[key] for key in (
"model_name", "method", "load_settings", "steering", "output_dir",
)}
claim = ui.claim_model_slot(state, threading.Lock())
assert claim is not None
assert claim.rollback()
assert state["model"] is state["tokenizer"] is None
assert "load_token" not in state
if recoverable:
assert state["status"] == "ready"
assert all(state[key] == value for key, value in metadata.items())
else:
assert state["status"] == "idle"
assert state["output_dir"] is state["model_name"] is state["steering"] is None
assert state["load_settings"] == {}
def test_exclusive_claim_commit_and_compare_and_set(tmp_path):
path = write_checkpoint(tmp_path / "checkpoint")
lock = threading.Lock()
state = dict(status="ready", output_dir=str(path), model_name="A", model=object())
first = ui.claim_model_slot(state, lock)
assert first is not None and state["load_token"] is first.token
assert ui.claim_model_slot(state, lock) is None
model, tokenizer = object(), object()
assert first.commit(model=model, tokenizer=tokenizer, model_name="B")
assert state["status"] == "ready" and state["model"] is model
second = ui.claim_model_slot(state, lock)
assert second is not None
replacement = object()
state.update(load_token=replacement, model_name="C", model=model)
snapshot = dict(state)
assert not second.commit(model=None)
assert not second.rollback()
assert state == snapshot
state["load_token"] = second.token
state["output_dir"] = "changed-checkpoint"
assert not second.commit(model=None)
@pytest.mark.parametrize("status", ["ready", "idle"])
@pytest.mark.parametrize("entry", ["claim", "failure"])
def test_pre_unload_rollback_preserves_resident_and_previous_status(monkeypatch, status, entry):
model, tokenizer, steering = object(), object(), object()
state = dict(status=status, model=model, tokenizer=tokenizer, model_name="A",
method="advanced", load_settings={"dtype": "float32"}, steering=steering,
output_dir=None)
snapshot = dict(state)
lock = threading.Lock()
claim = ui.claim_model_slot(state, lock)
assert claim is not None
state.update(model_name="B", method="basic", load_settings={}, steering=None,
output_dir="missing-checkpoint")
recoverable = Mock(side_effect=AssertionError("resident needs no checkpoint validation"))
monkeypatch.setattr(ui, "checkpoint_recoverable", recoverable)
lifecycle = SimpleNamespace(release=Mock())
device_module = SimpleNamespace(free_gpu_memory=Mock())
cleanup = Mock()
def rollback():
if entry == "claim":
return claim.rollback(keep_resident=True)
return ui.rollback_load_failure(
claim, RuntimeError("early failure"), lifecycle=lifecycle,
device_module=device_module, cleanup=cleanup, preserve_resident=True,
)
assert rollback()
assert state == snapshot
assert state["model"] is model and state["tokenizer"] is tokenizer
next_claim = ui.claim_model_slot(state, lock)
assert next_claim is not None
next_state = dict(state)
assert not rollback()
assert state == next_state
recoverable.assert_not_called()
lifecycle.release.assert_not_called()
device_module.free_gpu_memory.assert_not_called()
cleanup.assert_not_called()
@pytest.mark.parametrize("blocked_step", ["pipeline", "allocator", "lifecycle"])
def test_failure_cleanup_keeps_slot_exclusive_until_lifecycle_release(tmp_path, blocked_step):
state = dict(status="ready", output_dir=str(write_checkpoint(tmp_path / "checkpoint")),
model=FakeModel([]), tokenizer=object(), model_name="A")
model_ref = weakref.ref(state["model"])
lock = threading.Lock()
claim = ui.claim_model_slot(state, lock)
entered, proceed = threading.Event(), threading.Event()
events, results, errors = [], [], []
def step(name):
assert state["model"] is state["tokenizer"] is None
assert model_ref() is None
assert ui.claim_model_slot(state, lock) is None
if name == blocked_step:
entered.set()
assert proceed.wait(timeout=5), "cleanup barrier was not released"
events.append(name)
lifecycle = SimpleNamespace(release=Mock(side_effect=lambda **kwargs: step("lifecycle")))
device_module = SimpleNamespace(free_gpu_memory=Mock(side_effect=lambda: step("allocator")))
def finish():
try:
results.append(ui.rollback_load_failure(
claim, RuntimeError("failed attempt"), lifecycle=lifecycle,
device_module=device_module, cleanup=lambda: step("pipeline"),
))
except BaseException as error:
errors.append(error)
worker = threading.Thread(target=finish, daemon=True)
worker.start()
try:
assert entered.wait(timeout=5), "cleanup did not reach the barrier"
assert ui.claim_model_slot(state, lock) is None
assert state["status"] == "obliterating"
assert state["load_token"] is claim.token
finally:
proceed.set()
worker.join(timeout=5)
assert not worker.is_alive() and not errors
assert results == [True]
assert events == ["pipeline", "allocator", "lifecycle"]
next_claim = ui.claim_model_slot(state, lock)
assert next_claim is not None
assert not ui.rollback_load_failure(
claim, lifecycle=lifecycle, device_module=device_module,
cleanup=lambda: step("obsolete cleanup"),
)
assert state["load_token"] is next_claim.token
assert events == ["pipeline", "allocator", "lifecycle"]
def test_admission_error_message_includes_structured_details_and_bypass():
error = PhaseAdmissionError("summon", "cuda:1", 100, 20, reason="calibrated weights")
message = ui.format_admission_error(error)
assert message.startswith("Memory admission rejected the load:")
for detail in ("summon", "cuda:1", "100 bytes", "20 bytes", "80 bytes",
"calibrated weights", "OBLITERATUS_PHASE_ADMISSION=off"):
assert detail in message
assert ui.format_admission_error(ValueError("other")) is None
def stub_cli_dependencies(attempt, monkeypatch):
from obliteratus.models.quant_dequant import QuantDetection, QuantScheme
monkeypatch.setattr(loader, "_apply_deferred_shims", lambda: None)
monkeypatch.setattr(loader, "_hf_token", lambda: None)
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=attempt.config))
monkeypatch.setattr(loader.AutoTokenizer, "from_pretrained", Mock(
return_value=attempt.tokenizer,
))
monkeypatch.setattr(loader.qd, "detect_quant_scheme", Mock(
return_value=QuantDetection(QuantScheme.NONE),
))
monkeypatch.setattr(loader, "_select_model_class", lambda *args: AutoModelForCausalLM)
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 0)
monkeypatch.setattr(loader, "_apply_attention_backend_fallback", lambda config, **kw: None)
monkeypatch.setattr(loader.dev, "get_device", attempt.dev.get_device)
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda *args: False)
monkeypatch.setattr(loader.dev, "supports_bitsandbytes", lambda *args: True)
monkeypatch.setattr(loader.dev, "supports_bfloat16", lambda *args: True)
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
monkeypatch.setattr(loader.dev, "empty_cache", lambda: None)
@pytest.mark.parametrize("device", ["cpu", "cuda:0"])
@pytest.mark.parametrize("mode,tight", [
("default", False), ("default", True), ("strict", False), ("strict", True),
("off", True),
])
def test_ui_and_cli_call_same_public_admission_for_identical_plan(
attempt, monkeypatch, device, mode, tight,
):
stub_cli_dependencies(attempt, monkeypatch)
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
observer = (lambda name: MemoryObservation(name, 0, GIB)) if tight else ample_memory
real_function = admission.admit_model_load
calls, reports, errors = [], [], []
def spy(*args, **kwargs):
calls.append((real_function, args, kwargs))
try:
report = real_function(*args, **kwargs)
except PhaseAdmissionError as error:
errors.append((error.device, error.required_bytes, error.available_bytes))
raise
reports.append(report)
return report
monkeypatch.setattr(admission, "admit_model_load", spy)
reject = tight and mode != "off" and (device.startswith("cuda") or mode == "strict")
def cli():
return loader.load_model(
str(attempt.path), device=device, dtype="float32", local_files_only=True,
skip_snapshot=True, phase_admission=True, admission_observer=observer,
)
def frontend():
return ui.load_chat_model(attempt.path, **dict(
attempt.options, device=device, admission_observer=observer,
))
for load in (cli, frontend):
if reject:
with pytest.raises(PhaseAdmissionError):
load()
else:
load()
assert len(calls) == 2
assert calls[0][0] is calls[1][0] is real_function
for _, arguments, kwargs in calls:
assert arguments[0] is attempt.config
assert kwargs["device"] == device and kwargs["dtype"] == "float32"
assert kwargs["observer"] is observer
assert kwargs.get("mode") is None
assert kwargs.get("gpu_memory_utilization") is None
assert "safety_reserve_bytes" not in kwargs and "broker_grant_bytes" not in kwargs
assert kwargs.get("max_memory") is None
assert kwargs["device_map"] == ({"": 0} if device == "cuda:0" else None)
if reject:
assert errors[0] == errors[1]
attempt.weights.assert_not_called()
else:
assert reports[0].demands == reports[1].demands
assert reports[0].components == reports[1].components
assert reports[0].available_bytes == reports[1].available_bytes
assert reports[0].bypassed == reports[1].bypassed == (mode == "off")
assert attempt.weights.call_count == 2
@pytest.mark.parametrize("case", [
"cpu", "explicit_cuda", "single_auto", "multi_auto", "cpu_offload", "disk_offload",
"4bit", "8bit", "native", "qwen_hybrid", "mps",
])
def test_successful_ui_placement_matches_golden_main_kwargs(attempt, monkeypatch, case):
options = dict(attempt.options)
expected = {"torch_dtype": torch.float32, "local_files_only": True}
expected_map, expected_quant, expected_moves = None, None, ["cpu"]
if case in {"explicit_cuda", "4bit", "8bit", "qwen_hybrid"}:
options["device"] = "cuda:1"
expected_map, expected_moves = {"": 1}, []
elif case in {"single_auto", "multi_auto", "cpu_offload", "disk_offload", "native"}:
attempt.dev.get_device.side_effect = lambda device: "cuda:0"
attempt.dev.supports_device_map_auto.return_value = True
attempt.dev.device_count.return_value = 1 if case == "single_auto" else 2
expected_map, expected_moves = "auto", []
elif case == "mps":
options["device"] = "mps"
expected_moves = ["mps"]
if case in {"cpu_offload", "disk_offload"}:
options.update(offload_folder=str(attempt.path / "offload"), low_cpu_mem_usage=True)
expected.update(offload_folder=options["offload_folder"], low_cpu_mem_usage=True)
if case in {"4bit", "8bit"}:
# Avoid BitsAndBytesConfig's package availability check; retain its real type.
quant = object.__new__(BitsAndBytesConfig)
quant.__dict__.update(_load_in_4bit=case == "4bit", _load_in_8bit=case == "8bit")
options["quantization_config"] = quant
expected["quantization_config"] = quant
expected_quant = case
if case == "native":
attempt.config.quantization_config = {"quant_method": "mxfp4"}
expected["torch_dtype"] = "auto"
if case == "qwen_hybrid":
attempt.config.model_type = "qwen3_5_text"
monkeypatch.setattr(loader, "_require_qwen_hybrid_kernels", lambda: None)
monkeypatch.setattr(loader, "_estimate_model_memory_gb", lambda *args: 0.1)
monkeypatch.setattr(loader, "_qwen_explicit_device", lambda *args: 1)
monkeypatch.setattr(loader.dev, "device_count", lambda: 2)
expected.update(config=attempt.config, attn_implementation="sdpa")
if expected_map is not None:
expected["device_map"] = expected_map
if expected_map == "auto":
expected["max_memory"] = loader._bounded_max_memory.return_value
spy = Mock(return_value=None)
monkeypatch.setattr(admission, "admit_model_load", spy)
ui.load_chat_model(attempt.path, **options)
assert attempt.weights.call_args.args == (attempt.path,)
assert attempt.weights.call_args.kwargs == expected
assert attempt.moves == expected_moves
assert attempt.events == ["loading", "tokenizer", "weights", "resize", "ready"]
assert spy.call_args.args == (attempt.config,)
kwargs = spy.call_args.kwargs
assert kwargs["device_map"] == expected.get("device_map")
assert kwargs["quantization"] == expected_quant
assert kwargs["max_memory"] is expected.get("max_memory")
assert kwargs["task"] == "causal_lm" and kwargs["model_class"] is AutoModelForCausalLM
assert kwargs["checkpoint_path"] == str(attempt.path)
assert kwargs["local_files_only"] is True
attempt.lifecycle.release.assert_not_called()
def test_passes_auth_and_remote_code_settings_without_second_config_load(attempt, monkeypatch):
attempt.options.update(trust_remote_code=True, token="test-token")
spy = Mock(return_value=None)
monkeypatch.setattr(admission, "admit_model_load", spy)
ui.load_chat_model(attempt.path, **attempt.options)
expected = dict(trust_remote_code=True, token="test-token", local_files_only=True)
attempt.config_class.from_pretrained.assert_called_once_with(attempt.path, **expected)
attempt.tokenizer_class.from_pretrained.assert_called_once_with(attempt.path, **expected)
for key, value in expected.items():
assert spy.call_args.kwargs[key] == value
assert attempt.weights.call_args.kwargs[key] == value
@pytest.mark.parametrize("devices", [1, 2])
@pytest.mark.parametrize("offload", [None, "cpu", "disk"])
def test_auto_ui_and_cli_admit_the_actual_same_bounded_plan(attempt, monkeypatch, devices, offload):
stub_cli_dependencies(attempt, monkeypatch)
attempt.dev.get_device.side_effect = lambda device: "cuda:0"
attempt.dev.supports_device_map_auto.return_value = True
monkeypatch.setattr(loader.dev, "supports_device_map_auto", lambda *args: True)
monkeypatch.setattr(loader.dev, "device_count", lambda: devices)
monkeypatch.setattr(loader.dev, "is_cuda", lambda: True)
plan = {index: f"{8192 // (index + 1)}MiB" for index in range(devices)}
plan["cpu"] = "16384MiB" if offload else "0MiB"
loader._bounded_max_memory.return_value = plan
spy = Mock(return_value=None)
monkeypatch.setattr(admission, "admit_model_load", spy)
offload_options = ({"offload_folder": str(attempt.path / "offload"),
"low_cpu_mem_usage": True} if offload else {})
ceilings = {"cuda:0": 10 * GIB, "cpu": 20 * GIB}
options = dict(gpu_memory_utilization=0.7, device_memory_ceiling_bytes=ceilings)
loader.load_model(
str(attempt.path), device="auto", dtype="float32", local_files_only=True,
skip_snapshot=True, phase_admission=True, admission_observer=ample_memory,
**options, **{key: value for key, value in offload_options.items()
if key != "low_cpu_mem_usage"},
)
ui.load_chat_model(attempt.path, **attempt.options, **options, **offload_options)
assert spy.call_count == attempt.weights.call_count == 2
for admitted, loaded in zip(spy.call_args_list, attempt.weights.call_args_list):
assert admitted.kwargs["device_map"] == loaded.kwargs["device_map"] == "auto"
assert admitted.kwargs["max_memory"] is loaded.kwargs["max_memory"] is plan
assert admitted.kwargs["gpu_memory_utilization"] == 0.7
assert admitted.kwargs["device_memory_ceiling_bytes"] == ceilings
assert loader._bounded_max_memory.call_count == 2
for call in loader._bounded_max_memory.call_args_list:
assert call.args == (0.7,)
assert call.kwargs == {"device_memory_ceiling_bytes": ceilings}
@pytest.mark.parametrize("explicit_budget", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, "float32"])
def test_auto_quantized_fit_matches_cli_budget_exception(attempt, monkeypatch, explicit_budget, dtype):
attempt.dev.get_device.side_effect = lambda device: "cuda:0"
attempt.dev.supports_device_map_auto.return_value = True
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 80)
quant = object.__new__(BitsAndBytesConfig)
quant.__dict__.update(_load_in_4bit=True, _load_in_8bit=False)
estimate = Mock(return_value=1)
monkeypatch.setattr(loader, "_estimate_model_memory_gb", estimate)
options = dict(attempt.options, quantization_config=quant, torch_dtype=dtype)
if explicit_budget:
options["gpu_memory_utilization"] = 0.8
spy = Mock(return_value=None)
monkeypatch.setattr(admission, "admit_model_load", spy)
ui.load_chat_model(attempt.path, **options)
estimate.assert_called_once_with(attempt.config, torch.float32)
if explicit_budget:
assert attempt.weights.call_args.kwargs["max_memory"] is loader._bounded_max_memory.return_value
loader._bounded_max_memory.assert_called_once()
else:
assert "max_memory" not in attempt.weights.call_args.kwargs
assert spy.call_args.kwargs["max_memory"] is None
loader._bounded_max_memory.assert_not_called()