mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
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.
302 lines
13 KiB
Python
302 lines
13 KiB
Python
"""Guarded UI loads and exclusive, transactional model-slot ownership."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import traceback
|
|
|
|
from obliteratus import phase_admission as _pa
|
|
from obliteratus.gpu_lifecycle import measure_torch_memory
|
|
from obliteratus.persistence_contracts import validate_reloadable_checkpoint
|
|
from obliteratus.runtime_contracts import resolve_model_load_policy
|
|
|
|
|
|
def checkpoint_recoverable(checkpoint) -> bool:
|
|
"""Check reload-critical artifacts without loading a model."""
|
|
if not checkpoint:
|
|
return False
|
|
try:
|
|
validate_reloadable_checkpoint(checkpoint)
|
|
except (OSError, ValueError, TypeError):
|
|
return False
|
|
return True
|
|
|
|
|
|
def clear_stale_state(state) -> None:
|
|
"""Clear an unrecoverable slot; caller holds its lock."""
|
|
state.update(model=None, tokenizer=None, model_name=None, method=None,
|
|
status="idle", steering=None, output_dir=None, load_settings={})
|
|
state.pop("load_token", None)
|
|
|
|
|
|
@dataclass
|
|
class ModelSlotClaim:
|
|
"""A token owns the slot until a compare-and-set commit or rollback."""
|
|
|
|
state: dict
|
|
lock: object
|
|
token: object
|
|
snapshot: dict
|
|
|
|
def commit(self, *, status="ready", **publication) -> bool:
|
|
with self.lock:
|
|
if (self.state.get("load_token") is not self.token
|
|
or self.state.get("output_dir") != self.snapshot["output_dir"]):
|
|
return False
|
|
self.state.update(publication)
|
|
self.state["status"] = status
|
|
self.state.pop("load_token", None)
|
|
return True
|
|
|
|
def rollback(self, *, keep_resident=False) -> bool:
|
|
with self.lock:
|
|
if self.state.get("load_token") is not self.token:
|
|
return False
|
|
if keep_resident:
|
|
self.state.update(self.snapshot)
|
|
self.state.pop("load_token", None)
|
|
return True
|
|
self.state.update(self.snapshot, model=None, tokenizer=None)
|
|
if checkpoint_recoverable(self.snapshot["output_dir"]):
|
|
self.state["status"] = "ready"
|
|
self.state.pop("load_token", None)
|
|
else:
|
|
clear_stale_state(self.state)
|
|
return True
|
|
|
|
def restore_loaded(self, model, tokenizer) -> bool:
|
|
"""Finish a temporary A/B load while retaining the resident model."""
|
|
return self.commit(model=model, tokenizer=tokenizer)
|
|
|
|
|
|
def claim_model_slot(state, lock) -> ModelSlotClaim | None:
|
|
"""Atomically refuse an occupied load slot, or capture its metadata."""
|
|
with lock:
|
|
if state.get("status") == "obliterating" or state.get("load_token") is not None:
|
|
return None
|
|
snapshot = {key: state.get(key) for key in (
|
|
"model_name", "method", "load_settings", "steering", "output_dir", "status",
|
|
)}
|
|
token = object()
|
|
state.update(status="obliterating", load_token=token)
|
|
return ModelSlotClaim(state, lock, token, snapshot)
|
|
|
|
|
|
def clear_exception_frames(error) -> None:
|
|
"""Remove traceback-held references throughout an exception chain."""
|
|
try:
|
|
seen = set()
|
|
pending = [error]
|
|
while pending:
|
|
current = pending.pop()
|
|
if current is None or id(current) in seen:
|
|
continue
|
|
seen.add(id(current))
|
|
traceback.clear_frames(current.__traceback__)
|
|
pending.extend((current.__cause__, current.__context__))
|
|
current.__cause__ = current.__context__ = None
|
|
except BaseException:
|
|
pass
|
|
|
|
|
|
def cleanup_load_failure(error, *, lifecycle, device_module, reason="load_failed") -> None:
|
|
"""Clear traceback-held tensors and release without replacing the error."""
|
|
# Each step is independent: even a broken allocator must not suppress release.
|
|
clear_exception_frames(error)
|
|
try:
|
|
device_module.free_gpu_memory()
|
|
except BaseException:
|
|
pass
|
|
try:
|
|
lifecycle.release(reason=reason)
|
|
except BaseException:
|
|
pass
|
|
|
|
|
|
def rollback_load_failure(claim, error=None, *, lifecycle, device_module,
|
|
cleanup=None, cleanup_required=True, preserve_resident=False,
|
|
reason="load_failed") -> bool:
|
|
"""Keep ownership through reference dropping, cleanup and lifecycle release.
|
|
|
|
Callers drop their local model references first. A cleanup callback may drop
|
|
pipeline references; it returns before allocator/lifecycle cleanup starts.
|
|
Already-cleaned guarded loads pass cleanup_required=False. The token check
|
|
makes repeated finally cleanup harmless after another caller claims the slot.
|
|
Failures before unloading pass preserve_resident=True: only metadata/status
|
|
and ownership are restored, with no reference dropping or lifecycle cleanup.
|
|
"""
|
|
with claim.lock:
|
|
if claim.state.get("load_token") is not claim.token:
|
|
return False
|
|
if not preserve_resident:
|
|
claim.state.update(model=None, tokenizer=None)
|
|
if preserve_resident:
|
|
clear_exception_frames(error)
|
|
return claim.rollback(keep_resident=True)
|
|
if cleanup is not None:
|
|
try:
|
|
cleanup()
|
|
except BaseException as cleanup_error:
|
|
clear_exception_frames(cleanup_error)
|
|
if cleanup_required:
|
|
cleanup_load_failure(error, lifecycle=lifecycle, device_module=device_module, reason=reason)
|
|
else:
|
|
clear_exception_frames(error)
|
|
return claim.rollback()
|
|
|
|
|
|
def format_admission_error(error) -> str | None:
|
|
"""Describe shared admission rejection with its explicit bypass."""
|
|
if isinstance(error, _pa.PhaseAdmissionError):
|
|
return (f"Memory admission rejected the load: {error} "
|
|
"OBLITERATUS_PHASE_ADMISSION=off explicitly bypasses memory admission.")
|
|
return None
|
|
|
|
|
|
def _admission_dtype(config, requested, checkpoint):
|
|
from obliteratus.models.loader import _checkpoint_source_dtype, _resolve_model_load_dtype
|
|
|
|
if requested is not None and requested != "auto":
|
|
return _resolve_model_load_dtype(config, requested, resolve_native=True,
|
|
checkpoint_path=str(checkpoint))
|
|
text = getattr(config, "text_config", None)
|
|
dtype = getattr(config, "dtype", None) or vars(config).get("torch_dtype")
|
|
if dtype is None and text is not None:
|
|
dtype = getattr(text, "dtype", None) or vars(text).get("torch_dtype")
|
|
if dtype is None:
|
|
dtype = _checkpoint_source_dtype(str(checkpoint), config, None, None)
|
|
# Reuse floating-point validation, including the native auto policy.
|
|
return _resolve_model_load_dtype(config, dtype, resolve_native=True,
|
|
checkpoint_path=str(checkpoint))
|
|
|
|
|
|
def load_chat_model(
|
|
pretrained_path, *, config_class, model_class, tokenizer_class, lifecycle,
|
|
device_module, torch_module, device="auto", torch_dtype=None,
|
|
trust_remote_code=False, quantization_config=None, offload_folder=None,
|
|
low_cpu_mem_usage=False, token=None, local_files_only=False,
|
|
steering_hook=None, admission_observer=None, measure_memory=measure_torch_memory,
|
|
gpu_memory_utilization=None, device_memory_ceiling_bytes=None,
|
|
):
|
|
"""Return a publishable model/tokenizer after one fully guarded attempt."""
|
|
model = tokenizer = None
|
|
try:
|
|
lifecycle.loading(str(pretrained_path))
|
|
kwargs = {}
|
|
config_kwargs = {key: value for key, value in {
|
|
"trust_remote_code": trust_remote_code or None, "token": token,
|
|
"local_files_only": local_files_only or None,
|
|
}.items() if value is not None}
|
|
config = config_class.from_pretrained(pretrained_path, **config_kwargs)
|
|
from obliteratus.models.loader import (
|
|
_available_gpu_memory_gb, _bounded_max_memory, _estimate_model_memory_gb,
|
|
_resolve_model_load_dtype, qwen_hybrid_runtime_overrides,
|
|
)
|
|
from obliteratus.runtime_contracts import quantized_model_fits_gpu
|
|
from transformers import BitsAndBytesConfig
|
|
|
|
unknown_quantizer = quantization_config is not None and not isinstance(
|
|
quantization_config, BitsAndBytesConfig,
|
|
)
|
|
quantization = None
|
|
if getattr(quantization_config, "load_in_4bit", False):
|
|
quantization = "4bit"
|
|
elif getattr(quantization_config, "load_in_8bit", False):
|
|
quantization = "8bit"
|
|
for key, value in {
|
|
"trust_remote_code": trust_remote_code or None,
|
|
"quantization_config": quantization_config, "offload_folder": offload_folder,
|
|
"low_cpu_mem_usage": low_cpu_mem_usage or None, "token": token,
|
|
"local_files_only": local_files_only or None,
|
|
}.items():
|
|
if value is not None:
|
|
kwargs[key] = value
|
|
if torch_dtype is not None:
|
|
if getattr(config, "quantization_config", None) is not None:
|
|
try:
|
|
kwargs["torch_dtype"] = _resolve_model_load_dtype(config, torch_dtype)
|
|
except (ValueError, AttributeError):
|
|
kwargs["torch_dtype"] = "auto"
|
|
else:
|
|
kwargs["torch_dtype"] = torch_dtype
|
|
resolved_device = device_module.get_device(device)
|
|
native_quant = getattr(config, "quantization_config", None)
|
|
policy = resolve_model_load_policy(
|
|
device=device, resolved_device=resolved_device,
|
|
dtype="float32" if torch_dtype is None else str(torch_dtype).removeprefix("torch."),
|
|
quantization=quantization,
|
|
has_native_quantization=native_quant is not None and quantization_config is None,
|
|
device_map_auto_supported=device_module.supports_device_map_auto(),
|
|
bitsandbytes_supported=(device_module.supports_bitsandbytes(resolved_device)
|
|
or unknown_quantizer),
|
|
cuda_device_count=(device_module.device_count()
|
|
if resolved_device.startswith("cuda") else None),
|
|
)
|
|
overrides = qwen_hybrid_runtime_overrides(
|
|
config, torch_dtype or torch_module.float32, quantization,
|
|
device=device, resolved_device=resolved_device,
|
|
)
|
|
if overrides:
|
|
kwargs["config"] = config
|
|
kwargs.update(overrides)
|
|
elif policy.device_map is not None:
|
|
kwargs["device_map"] = policy.device_map
|
|
|
|
reason = None
|
|
try:
|
|
dtype = str(_admission_dtype(config, torch_dtype, pretrained_path))
|
|
dtype = dtype.removeprefix("torch.")
|
|
except (ValueError, AttributeError, TypeError, KeyError, OSError) as error:
|
|
reason = f"Cannot resolve UI load dtype: {error}"
|
|
if unknown_quantizer:
|
|
reason = "UI quantization_config is not BitsAndBytesConfig"
|
|
|
|
ceilings = _pa.resolve_device_memory_ceilings(device_memory_ceiling_bytes)
|
|
if kwargs.get("device_map") == "auto" and resolved_device.startswith("cuda"):
|
|
estimate_dtype = (getattr(torch_module, dtype) if reason is None
|
|
else torch_module.float32)
|
|
estimate = (_estimate_model_memory_gb(config, estimate_dtype)
|
|
if native_quant is None else 0.0)
|
|
quantized_fit = (
|
|
gpu_memory_utilization is None and not ceilings
|
|
and quantized_model_fits_gpu(estimate, quantization, _available_gpu_memory_gb())
|
|
)
|
|
if not quantized_fit:
|
|
kwargs["max_memory"] = _bounded_max_memory(
|
|
gpu_memory_utilization, device_memory_ceiling_bytes=ceilings,
|
|
)
|
|
|
|
with _pa.admission_warning_scope():
|
|
if reason is not None:
|
|
_pa.admit_phase(
|
|
"summon", _pa.unknown_phase_demand("summon", (resolved_device,), reason),
|
|
observer=admission_observer, mode=None,
|
|
)
|
|
else:
|
|
_pa.admit_model_load(
|
|
config, device=resolved_device, dtype=dtype, quantization=quantization,
|
|
task="causal_lm", model_class=model_class,
|
|
device_map=kwargs.get("device_map"), max_memory=kwargs.get("max_memory"),
|
|
checkpoint_path=str(pretrained_path), trust_remote_code=trust_remote_code,
|
|
token=token, local_files_only=local_files_only,
|
|
gpu_memory_utilization=gpu_memory_utilization,
|
|
device_memory_ceiling_bytes=ceilings,
|
|
observer=admission_observer, mode=None,
|
|
)
|
|
tokenizer = tokenizer_class.from_pretrained(pretrained_path, **config_kwargs)
|
|
model = model_class.from_pretrained(pretrained_path, **kwargs)
|
|
if policy.move_to_resolved_device:
|
|
model = model.to(resolved_device)
|
|
if tokenizer.pad_token is None:
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
if steering_hook is not None:
|
|
steering_hook(model)
|
|
memory = measure_memory(torch_module)
|
|
lifecycle.resize(memory)
|
|
lifecycle.ready(memory)
|
|
return model, tokenizer
|
|
except BaseException as error:
|
|
model = tokenizer = None
|
|
cleanup_load_failure(error, lifecycle=lifecycle, device_module=device_module)
|
|
raise error from None
|