"""Unified device abstraction for CUDA, MPS (Apple Silicon), and CPU. All device-specific queries (availability, memory, cache management) go through this module so the rest of the codebase never calls ``torch.cuda.*`` directly. """ from __future__ import annotations import gc import logging import os import platform from collections.abc import Callable, Iterator, Sequence from dataclasses import dataclass from typing import Any import torch import torch.nn as nn from obliteratus.runtime_contracts import supports_bfloat16_target logger = logging.getLogger(__name__) # --------------------------------------------------------------------------- # Device detection # --------------------------------------------------------------------------- def is_cuda() -> bool: """True when at least one NVIDIA CUDA GPU is visible.""" return torch.cuda.is_available() def is_mps() -> bool: """True when Apple Metal Performance Shaders backend is usable.""" return ( hasattr(torch.backends, "mps") and torch.backends.mps.is_available() and torch.backends.mps.is_built() ) def is_gpu_available() -> bool: """True if *any* GPU backend (CUDA or MPS) is available.""" return is_cuda() or is_mps() def get_device(preference: str = "auto") -> str: """Resolve a device string. Parameters ---------- preference : str ``"auto"`` picks the best GPU, ``"cuda"``/``"mps"``/``"cpu"`` forces. Returns ------- str A PyTorch device string (``"cuda"``, ``"mps"``, or ``"cpu"``). """ if preference == "auto": if is_cuda(): return "cuda" if is_mps(): return "mps" return "cpu" if preference == "cpu": return preference if preference == "mps": if not is_mps(): raise RuntimeError( "MPS was requested but is unavailable. Use device='auto' for CPU fallback.", ) return preference if preference == "cuda" or ( preference.startswith("cuda:") and preference.removeprefix("cuda:").isdigit() ): if not is_cuda(): raise RuntimeError( "CUDA was requested but is unavailable. Use device='auto' for CPU fallback.", ) return preference raise ValueError( f"Unknown device {preference!r}. Choose 'auto', 'cpu', 'mps', 'cuda', " "or an indexed CUDA device such as 'cuda:0'.", ) def get_device_name(device_index: int = 0) -> str: """Human-readable name of the selected accelerator.""" if is_cuda(): return torch.cuda.get_device_name(device_index) if is_mps(): # Apple doesn't expose a per-chip name via MPS; use platform info. chip = platform.processor() or "Apple Silicon" return f"Apple {chip} (MPS)" return "CPU" def device_count() -> int: """Number of accelerator devices (GPUs or MPS slots).""" if is_cuda(): return torch.cuda.device_count() if is_mps(): return 1 # MPS always exposes a single unified device return 0 # --------------------------------------------------------------------------- # Memory information # --------------------------------------------------------------------------- @dataclass class MemoryInfo: """Snapshot of accelerator memory (in GB).""" used_gb: float = 0.0 reserved_gb: float = 0.0 total_gb: float = 0.0 free_gb: float = 0.0 device_name: str = "CPU" def _system_memory_gb() -> tuple[float, float]: """Return (total_gb, available_gb) of system RAM.""" try: import psutil vm = psutil.virtual_memory() return vm.total / 1024 ** 3, vm.available / 1024 ** 3 except (AttributeError, ImportError, OSError, RuntimeError, ValueError): pass try: total = os.sysconf("SC_PHYS_PAGES") * os.sysconf("SC_PAGE_SIZE") / 1024 ** 3 # Rough estimate: assume 60 % available if we can't query return total, total * 0.6 except (AttributeError, OSError, ValueError): return 16.0, 8.0 # conservative fallback def get_memory_info(device_index: int = 0) -> MemoryInfo: """Query memory for the given accelerator (or system RAM for MPS/CPU).""" name = get_device_name(device_index) if is_cuda() else get_device_name() if is_cuda(): try: free, total = torch.cuda.mem_get_info(device_index) used = torch.cuda.memory_allocated(device_index) reserved = torch.cuda.memory_reserved(device_index) total_gb = total / 1024 ** 3 return MemoryInfo( used_gb=used / 1024 ** 3, reserved_gb=reserved / 1024 ** 3, total_gb=total_gb, free_gb=free / 1024 ** 3, device_name=name, ) except Exception: props = torch.cuda.get_device_properties(device_index) total_gb = props.total_memory / 1024 ** 3 return MemoryInfo(total_gb=total_gb, free_gb=total_gb, device_name=name) if is_mps(): # MPS uses unified memory — report system RAM as a proxy. total, avail = _system_memory_gb() # Apple's unified memory is shared with the OS, so usable fraction # is typically ~65-75 % of total. usable = total * 0.70 return MemoryInfo( used_gb=max(usable - avail, 0.0), reserved_gb=0.0, total_gb=usable, free_gb=min(avail, usable), device_name=name, ) # CPU-only total, avail = _system_memory_gb() return MemoryInfo(total_gb=total, free_gb=avail, device_name=name) def get_total_free_gb() -> float: """Sum of free memory across all accelerator devices, in GB.""" if is_cuda(): total_free = 0.0 for i in range(torch.cuda.device_count()): try: free, _ = torch.cuda.mem_get_info(i) total_free += free / 1024 ** 3 except Exception: props = torch.cuda.get_device_properties(i) total_free += props.total_memory / 1024 ** 3 return total_free if is_mps(): _, avail = _system_memory_gb() return avail * 0.70 # usable fraction return 0.0 # --------------------------------------------------------------------------- # Cache / memory management # --------------------------------------------------------------------------- def empty_cache() -> None: """Release cached allocations on the current accelerator.""" if is_cuda(): torch.cuda.empty_cache() elif is_mps(): # torch.mps.empty_cache() available since PyTorch 2.1 if hasattr(torch.mps, "empty_cache"): torch.mps.empty_cache() def free_gpu_memory() -> None: """Aggressive memory cleanup: GC + accelerator cache flush.""" gc.collect() if is_cuda(): try: torch.cuda.empty_cache() except Exception: try: torch.cuda.synchronize() except Exception: pass try: torch.cuda.reset_peak_memory_stats() except Exception: pass elif is_mps(): if hasattr(torch.mps, "empty_cache"): try: torch.mps.empty_cache() except Exception: pass if hasattr(torch.mps, "synchronize"): try: torch.mps.synchronize() except Exception: pass def set_seed_all(seed: int) -> None: """Set random seed on all available accelerators.""" torch.manual_seed(seed) if is_cuda(): torch.cuda.manual_seed_all(seed) # MPS shares the CPU random state — no separate seed call needed. # --------------------------------------------------------------------------- # Dtype helpers # --------------------------------------------------------------------------- def default_dtype(device: str | None = None) -> torch.dtype: """Sensible default dtype for the given device.""" dev = device or get_device() if dev == "cpu": return torch.float32 return torch.float16 def supports_bfloat16(device: str | None = None) -> bool: """Whether *bfloat16* is natively supported on the target device.""" dev = device or get_device() cuda_available = is_cuda() if dev.startswith("cuda") else False cuda_major = ( torch.cuda.get_device_capability(0)[0] if dev.startswith("cuda") and cuda_available else None ) return supports_bfloat16_target( dev, cuda_available=cuda_available, cuda_major=cuda_major, torch_version=torch.__version__, ) def supports_float64(device: str | None = None) -> bool: """Whether *float64* is supported (MPS does NOT support it).""" dev = device or get_device() return dev != "mps" def safe_svd_dtype(tensor: torch.Tensor) -> torch.dtype: """Return a dtype safe for SVD on the tensor's device. MPS does not support float64, so we cap at float32. """ if tensor.device.type == "mps": return torch.float32 return torch.float64 if tensor.dtype in (torch.float64, torch.float32) else torch.float32 # --------------------------------------------------------------------------- # OOM exception matching # --------------------------------------------------------------------------- def is_oom_error(exc: BaseException) -> bool: """Return True if *exc* is an out-of-memory error on any backend.""" if isinstance(exc, torch.cuda.OutOfMemoryError): return True # MPS raises a generic RuntimeError containing "out of memory" if isinstance(exc, RuntimeError) and "out of memory" in str(exc).lower(): return True return False # --------------------------------------------------------------------------- # Quantization compatibility # --------------------------------------------------------------------------- def supports_bitsandbytes(device: str | None = None) -> bool: """BitsAndBytes requires NVIDIA CUDA — check that.""" dev = device or get_device() return dev.startswith("cuda") def supports_device_map_auto(device: str | None = None) -> bool: """Accelerate's device_map='auto' is only reliable on CUDA.""" dev = device or get_device() return dev.startswith("cuda") # --------------------------------------------------------------------------- # Model input execution-device resolution # --------------------------------------------------------------------------- class InputDeviceResolutionError(RuntimeError): """The model input device cannot be resolved without risking a bad copy.""" def _normalize_input_device(device: Any) -> torch.device: if isinstance(device, int): return torch.device("cuda", device) try: resolved = torch.device(device) except (RuntimeError, TypeError) as exc: raise InputDeviceResolutionError( f"unsupported Accelerate execution device {device!r}" ) from exc if resolved.type == "meta": raise InputDeviceResolutionError( "Accelerate reported meta as the input execution device" ) return resolved def _accelerate_align_hooks(hook: Any) -> tuple[list[Any], bool]: try: from accelerate.hooks import AlignDevicesHook, SequentialHook except Exception: return [], False if isinstance(hook, AlignDevicesHook): return [hook], True if isinstance(hook, SequentialHook): hooks: list[Any] = [] for child in hook.hooks: child_hooks, supported = _accelerate_align_hooks(child) if not supported: return [], False hooks.extend(child_hooks) return hooks, True return [], False _MODULE_TRAVERSAL_LIMIT = 10_000 class _ModuleTraversalBudget: """Bound edge reads and generated DFS states before doing either operation.""" def __init__(self): self.used = 0 def charge(self): if self.used >= _MODULE_TRAVERSAL_LIMIT: raise InputDeviceResolutionError( "alias traversal budget exceeded for module execution-device resolution " f"(limit: {_MODULE_TRAVERSAL_LIMIT} edge reads and traversal states)" ) self.used += 1 def _module_paths( start: nn.Module, target: nn.Module, neighbours: Callable[[nn.Module], Iterator[nn.Module | None]], *, budget: _ModuleTraversalBudget, blocked_modules: Sequence[nn.Module] = (), reverse: bool = False, ) -> list[list[nn.Module]]: """Lazy DFS with one mutable path and at most 64 acyclic results. Reserve work before advancing an iterator, including reads of pruned edges, and before creating each pending state. Exhausted reads are conservatively charged too. Only the current path has pending iterators; width never queues sibling states or copies their paths/ancestor sets. """ budget.charge() chains: list[list[nn.Module]] = [] blocked_ids = {id(module) for module in blocked_modules} - {id(start), id(target)} path = [start] ancestor_ids = {id(start)} if start is target: return [path] pending = [neighbours(start)] while pending: budget.charge() try: child = next(pending[-1]) except StopIteration: pending.pop() ancestor_ids.remove(id(path.pop())) continue if child is None or id(child) in ancestor_ids or id(child) in blocked_ids: continue budget.charge() path.append(child) ancestor_ids.add(id(child)) if child is target: if len(chains) >= 64: raise InputDeviceResolutionError( "too many alias paths for module execution-device resolution (limit: 64)" ) chains.append(list(reversed(path)) if reverse else list(path)) ancestor_ids.remove(id(path.pop())) else: pending.append(neighbours(child)) return chains def _module_ancestor_chains( root: nn.Module, target: nn.Module, *, blocked_modules: Sequence[nn.Module] = (), ) -> list[list[nn.Module]]: """Index registered edges once, then walk upward through target aliases.""" if root is target: return [[target]] parent_index = _ModuleParentIndex(_ModuleTraversalBudget()) chains = parent_index.ancestor_paths( root, target, blocked_modules=blocked_modules, ) return chains or [[target]] class _ModuleParentIndex: """Reusable child->(parent, name) edges, expanding each module only once. Additional roots cover unregistered context anchors without rescanning any already indexed expert subtree. Indexing and upward path enumeration share the caller's work budget; both retain registration aliases and prune cycles. """ def __init__(self, budget: _ModuleTraversalBudget): self.budget = budget self.parents: dict[int, list[tuple[nn.Module, str]]] = {} self.modules: dict[int, nn.Module] = {} def add_root(self, root: nn.Module) -> None: if id(root) in self.modules: return self.budget.charge() self.modules[id(root)] = root if not root._modules: return pending = [(root, iter(root._modules.items()))] while pending: self.budget.charge() parent, children = pending[-1] try: name, child = next(children) except StopIteration: pending.pop() continue if child is None: continue self.parents.setdefault(id(child), []).append((parent, name)) if id(child) in self.modules: continue # Leaf modules need no pending iterator. In particular, a wide # ModuleList of experts contributes edges without queuing experts. if child._modules: self.budget.charge() pending.append((child, iter(child._modules.items()))) self.modules[id(child)] = child def _parents(self, module: nn.Module) -> Iterator[nn.Module]: for parent, _ in self.parents.get(id(module), ()): yield parent def ancestor_paths( self, root: nn.Module, target: nn.Module, *, blocked_modules: Sequence[nn.Module] = (), ) -> list[list[nn.Module]]: self.add_root(root) return _module_paths( target, root, self._parents, budget=self.budget, blocked_modules=blocked_modules, ) def _first_materialized_parameter_device(module: nn.Module) -> torch.device | None: for parameter in module.parameters(): if parameter.device.type != "meta": return parameter.device return None def _has_meta_parameter(module: nn.Module) -> bool: return any(parameter.device.type == "meta" for parameter in module.parameters()) def _hook_execution_device( modules: list[nn.Module], *, require_supported_hooks: bool, ) -> torch.device | None: devices: list[torch.device] = [] unsupported_hooks: list[str] = [] hooks_without_devices = 0 for module in modules: hook = getattr(module, "_hf_hook", None) if hook is None: continue align_hooks, supported = _accelerate_align_hooks(hook) if not supported: unsupported_hooks.append( f"{type(hook).__module__}.{type(hook).__name__}" ) continue for align_hook in align_hooks: execution_device = getattr(align_hook, "execution_device", None) if execution_device is None: hooks_without_devices += 1 continue devices.append(_normalize_input_device(execution_device)) if require_supported_hooks and unsupported_hooks: raise InputDeviceResolutionError( "unsupported Accelerate hook layout for module execution: " + ", ".join(unsupported_hooks) ) unique = [] for candidate in devices: if all(candidate != existing for existing in unique): unique.append(candidate) if len(unique) == 1: return unique[0] if len(unique) > 1: raise InputDeviceResolutionError( "ambiguous Accelerate input execution devices: " + ", ".join(str(device) for device in unique) ) if require_supported_hooks and hooks_without_devices: raise InputDeviceResolutionError( "Accelerate input hook did not expose an execution_device" ) return None def resolve_module_execution_device( root: nn.Module, module: nn.Module, *, require_supported_hooks: bool, ancestors: Sequence[nn.Module] | None = None, ) -> torch.device | None: """Return the nearest hook execution device for the caller's module context. Explicit ``ancestors`` includes the module first, followed by its nearest ancestors; identical objects are checked once, in order. Otherwise the module's own hook is checked before indexing registered edges once. If it has no execution device, upward walks from the module to the root find every acyclic registered path, which is resolved independently within a 10,000-edge/state traversal budget, and up to 64 shared aliases must agree (including when a path has no hook device). Unregistered modules use only their own hook. Each module's hook is checked separately; conflicting devices within one hook still raise. Unsupported or device-less hooks retain ``require_supported_hooks``. Parameter storage is not consulted. """ if ancestors is None: execution = _hook_execution_device( [module], require_supported_hooks=require_supported_hooks, ) if execution is not None: return execution chains = _module_ancestor_chains(root, module) else: chains = [ancestors] devices: list[torch.device | None] = [] for chain in chains: execution = None seen: set[int] = {id(module)} if ancestors is None else set() for ancestor in chain: if id(ancestor) in seen: continue seen.add(id(ancestor)) execution = _hook_execution_device( [ancestor], require_supported_hooks=require_supported_hooks, ) if execution is not None: break if execution not in devices: devices.append(execution) if len(devices) > 1: raise InputDeviceResolutionError( "ambiguous shared module execution devices: " + ", ".join(str(device) if device is not None else "no execution device" for device in devices) ) return devices[0] def _input_embedding(model: nn.Module) -> nn.Module | None: get_input_embeddings = getattr(model, "get_input_embeddings", None) if not callable(get_input_embeddings): return None try: embedding = get_input_embeddings() except Exception: return None return embedding if isinstance(embedding, nn.Module) else None def uses_accelerate_dispatch(model: nn.Module) -> bool: """Return whether Accelerate owns any model placement hooks or maps.""" if hasattr(model, "hf_device_map"): return True return any(getattr(module, "_hf_hook", None) is not None for module in model.modules()) def resolve_input_execution_device(model: nn.Module) -> torch.device: """Return the device where token and mask tensors should enter a model. Accelerate can replace offloaded parameters with ``meta`` tensors while keeping the real forward execution device on ``AlignDevicesHook``. Input tensors must follow that execution device, not the storage device reported by the first parameter or the embedding's meta placeholder. """ embedding = _input_embedding(model) if embedding is not None: hook_device = resolve_module_execution_device( model, embedding, require_supported_hooks=_has_meta_parameter(embedding), ) if hook_device is not None: return hook_device materialized = _first_materialized_parameter_device(embedding) if materialized is not None: return materialized if _has_meta_parameter(embedding): raise InputDeviceResolutionError( "input embedding is meta-resident but has no supported " "Accelerate execution-device hook" ) hook_device = _hook_execution_device( [model], require_supported_hooks=_has_meta_parameter(model), ) if hook_device is not None: return hook_device if embedding is None and _has_meta_parameter(model): raise InputDeviceResolutionError( "ambiguous input layout: meta parameters without an embedding or execution hook" ) model_device = getattr(model, "device", None) if model_device is not None: try: return _normalize_input_device(model_device) except InputDeviceResolutionError: if str(model_device) != "meta": raise materialized = _first_materialized_parameter_device(model) if materialized is not None: return materialized if _has_meta_parameter(model): raise InputDeviceResolutionError( "model is meta-resident but has no supported input execution-device hook" ) return torch.device("cpu") # --------------------------------------------------------------------------- # CUDA env setup (called once at import time of abliterate.py) # --------------------------------------------------------------------------- def configure_cuda_alloc() -> None: """Configure CUDA allocation and optional attention backend safeguards.""" if is_cuda() and "PYTORCH_CUDA_ALLOC_CONF" not in os.environ: os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" if ( is_cuda() and os.environ.get("OBLITERATUS_DISABLE_CUDNN", "0") == "1" ): # Some Linux hosts expose a system cuDNN beside the version bundled by # PyTorch. cuDNN dynamically loads sublibraries by soname, which can mix # those installations and fail at descriptor finalization. Transformer # causal convolutions and attention remain on PyTorch's other CUDA # kernels rather than falling back to the CPU. torch.backends.cudnn.enabled = False if hasattr(torch.backends.cuda, "enable_cudnn_sdp"): torch.backends.cuda.enable_cudnn_sdp(False)