Files
OBLITERATUS/obliteratus/capacity_planning.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

1080 lines
69 KiB
Python

"""Config-only capacity estimates for whole-decoder-block placement.
No tokenizer, checkpoint tensor, accelerator observation or Hub request is made.
These conservative estimates complement runtime admission and measured qualification.
"""
from __future__ import annotations
import hashlib
import json
import math
import re
from dataclasses import asdict, dataclass
from pathlib import Path
from obliteratus.analysis.direction_methods import effective_direction_method
from obliteratus.qualification import canonical_json, normalize_device, parameter_device, validate_plan
GIB = 1024**3
PHASES = ("load", "baseline", "activations", "direction", "edit", "verify", "save", "reload")
@dataclass(frozen=True)
class Workload:
batch_size: int = 1
sequence_length: int = 128
generation_tokens: int = 16
samples: int = 32
n_directions: int = 1
attention_backend: str = "eager"
auxiliary_readouts: int = 0
neighbour_samples: int = 0
activation_datasets: int = 2
collection_copies: int = 0
preserve_refusal: bool = False
concept_extraction: bool = False
comparison_prompts: int = 0
direction_method: str = "diff_means"
validation_gate: bool = False
prompt_characters: int | None = None
def __post_init__(self):
for name, value in asdict(self).items():
if name == "prompt_characters":
if value is not None and (type(value) is not int or value < 0):
raise ValueError("prompt_characters must be a nonnegative integer or None")
elif name == "attention_backend":
if value != "eager":
raise ValueError("capacity planning currently bounds eager attention")
elif name == "direction_method":
if not isinstance(value, str):
raise ValueError("direction_method must be a supported method")
effective_direction_method(value, warn_alias=False)
elif name in {"concept_extraction", "validation_gate", "preserve_refusal"}:
if type(value) is not bool:
raise ValueError(f"{name} must be boolean")
elif name in {"auxiliary_readouts", "neighbour_samples", "collection_copies", "comparison_prompts"}:
if type(value) is not int or value < 0:
raise ValueError(f"{name} must be a nonnegative integer")
elif type(value) is not int or value < 1:
raise ValueError(f"{name} must be a positive integer")
class CapacityPlanningError(ValueError):
def __init__(self, plan):
self.plan = plan
super().__init__("Capacity plan refused: " + "; ".join(plan["refusals"]))
def meta_inventory(config: dict, dtype: str = "bfloat16", index: dict | None = None,
generation_config: dict | None = None) -> dict:
"""Instantiate native model metadata on meta and discard all tensor objects.
An index supplies names, source shard grouping and source byte count; it
contains no shapes. Shapes always come from the native config implementation.
"""
import torch
from accelerate import init_empty_weights
from accelerate.utils import find_tied_parameters
from transformers import AutoConfig, AutoModelForCausalLM, AutoModelForImageTextToText, GenerationConfig
from obliteratus.models.checkpoint_export import native_export_summary, plan_native_export
if dtype not in {"float32", "float16", "bfloat16"}:
raise ValueError("capacity planning requires an ordinary floating dtype")
if config.get("quantization_config"):
raise ValueError("quantized checkpoint capacity requires a separate storage contract")
raw = dict(config)
family = raw.pop("model_type")
if family not in {"llama", "qwen2", "gpt2", "qwen3_5", "qwen3_5_text"}:
raise ValueError(f"no capacity contract for model_type {family!r}")
cfg = AutoConfig.for_model(family, **raw)
constructor = AutoModelForImageTextToText if family == "qwen3_5" else AutoModelForCausalLM
with init_empty_weights(include_buffers=True):
model = constructor.from_config(cfg, torch_dtype=getattr(torch, dtype), attn_implementation="eager")
if generation_config is None:
# Checkpoint loading falls back to raw config.json; native model config
# parsers can discard generation flags before from_config sees them.
try:
generation = GenerationConfig.from_model_config(dict(config))
except AttributeError:
# Older Transformers accepts only the parsed model config here.
generation = model.generation_config
else:
generation = GenerationConfig.from_dict(generation_config)
model.tie_weights()
if any(t.device.type != "meta" for t in (*model.parameters(), *model.buffers())):
raise ValueError("native constructor allocated outside the meta device")
names = {id(module): name for name, module in model.named_modules()}
text = getattr(cfg, "text_config", cfg)
prefix = "model.language_model.layers" if family == "qwen3_5" else "transformer.h" if family == "gpt2" else "model.layers"
blocks = [f"{prefix}.{i}" for i in range(text.num_hidden_layers)]
for block in blocks:
model.get_submodule(block)
state = model.state_dict()
all_sizes = {name: tensor.numel() * tensor.element_size() for name, tensor in state.items()}
resident_sizes = dict(all_sizes)
resident_sizes.update((name, tensor.numel() * tensor.element_size())
for name, tensor in model.named_buffers(remove_duplicate=False))
tied = find_tied_parameters(model)
omitted = {name for group in tied for name in sorted(group)[1:]}
sizes = {name: size for name, size in resident_sizes.items() if name not in omitted}
modules = {}
for name, size in sizes.items():
module = next((block for block in blocks if name.startswith(block + ".")), name.rpartition(".")[0])
modules[module] = modules.get(module, 0) + size
output = names[id(model.get_output_embeddings())]
embedding = names[id(model.get_input_embeddings())]
text_config = text.to_dict()
for name in ("hidden_size", "intermediate_size", "num_hidden_layers", "num_attention_heads", "num_key_value_heads", "vocab_size", "max_position_embeddings"):
value = getattr(text, name, None)
if value is not None:
text_config[name] = value
export = native_export_summary(plan_native_export(model, allow_unbacked_meta=True))
refusals = []
source_config = config.get("text_config", {})
source_dtype = (config.get("dtype") or config.get("torch_dtype")
or source_config.get("dtype") or source_config.get("torch_dtype"))
# Without source metadata, reserve FP32 storage rather than assuming the
# requested load dtype also describes the checkpoint on disk.
source_width = {"float64": 8, "float32": 4, "float16": 2, "bfloat16": 2}.get(str(source_dtype), 4)
source_sizes = {name: tensor.numel() * (source_width if tensor.is_floating_point() else tensor.element_size())
for name, tensor in state.items()}
source_shards = {}
if index is not None:
mapping = index.get("weight_map", {})
if not isinstance(mapping, dict) or not mapping:
raise ValueError("safetensors index requires a nonempty weight_map")
missing = set(mapping) - all_sizes.keys()
absent = all_sizes.keys() - omitted - mapping.keys()
# A checkpoint can omit either member of a declared tie.
absent = {name for name in absent if not any(name in group and set(group) & mapping.keys() for group in tied)}
if missing or absent:
refusals.append(f"index/config tensor inventory mismatch: {len(missing)} unknown, {len(absent)} absent")
for name, shard in mapping.items():
if not isinstance(shard, str) or Path(shard).name != shard:
raise ValueError("index shard names must be local filenames")
source_shards[shard] = source_shards.get(shard, 0) + source_sizes.get(name, 0)
source_bytes = (index or {}).get("metadata", {}).get("total_size", sum(source_sizes.values()))
if type(source_bytes) not in {int, float} or not math.isfinite(source_bytes) or source_bytes < 0:
raise ValueError("invalid index total_size")
if source_shards and source_bytes > sum(source_shards.values()):
# An index has no per-tensor dtypes. A larger authoritative total may
# describe wider or mixed storage; bound each shard by that total.
source_shards = {name: math.ceil(source_bytes) for name in source_shards}
return {"config": cfg.to_dict(), "text_config": text_config, "dtype": dtype,
"generation_config": generation.to_dict(),
"dtype_bytes": torch.empty((), device="meta", dtype=getattr(torch, dtype)).element_size(),
"tensor_sizes": sizes, "snapshot_tensor_sizes": all_sizes, "module_sizes": modules, "blocks": blocks,
"other_modules": [name for name in modules if name not in blocks],
"output_module": output, "embedding_module": embedding, "tied_parameters": tied,
"total_weight_bytes": sum(sizes.values()), "source_weight_bytes": math.ceil(source_bytes),
"source_shards": source_shards, "export": export, "refusals": refusals,
"metadata_only": True, "config_sha256": hashlib.sha256(canonical_json(config)).hexdigest(),
"index_sha256": hashlib.sha256(canonical_json(index)).hexdigest() if index else None,
"generation_config_sha256": hashlib.sha256(canonical_json(generation_config)).hexdigest()
if generation_config is not None else None}
def _module_map(inventory, devices, offload="none"):
mapping = {}
for module in inventory["other_modules"]:
mapping[module] = devices[-1] if module == inventory["output_module"] or module.endswith(("norm", "ln_f")) else devices[0]
mapping.setdefault(inventory["output_module"], devices[-1])
if offload != "none":
mapping[inventory["output_module"]] = offload
for group in inventory["tied_parameters"]:
for name in group:
mapping[name.rpartition(".")[0]] = devices[0]
return mapping
def _edit_size(inventory, block):
# Include every decoder matrix, covering ordinary and concept projection.
return max((size for name, size in inventory["tensor_sizes"].items()
if name.startswith(block + ".") and name.endswith("weight")), default=0)
def _generation_batch_size(inventory, workload):
counts = []
for name in ("num_beams", "num_return_sequences"):
value = inventory.get("generation_config", {}).get(name)
value = 1 if value is None else value
if type(value) is not int or value < 1:
raise ValueError(f"{name} must be a positive integer or None")
counts.append(value)
if counts[0] == 1 and counts[1] > 1:
raise ValueError("Deterministic generation requires num_return_sequences=1 when num_beams=1")
if counts[1] > counts[0]:
raise ValueError("num_return_sequences cannot exceed num_beams for deterministic generation")
return workload.batch_size * max(counts)
def _generation_branches(inventory):
scale = inventory.get("generation_config", {}).get("guidance_scale")
if scale is not None and (type(scale) not in {int, float} or not math.isfinite(scale)):
raise ValueError("guidance_scale must be a finite number or None")
return 2 if scale is not None and scale != 1 else 1
def _generation_outputs(inventory):
outputs = {}
for name in ("output_hidden_states", "output_attentions", "output_scores", "output_logits", "return_dict_in_generate"):
value = inventory.get("generation_config", {}).get(name)
if value is not None and type(value) is not bool:
raise ValueError(f"{name} must be boolean or None")
outputs[name] = bool(value)
return outputs
def _generation_mode(inventory):
from transformers import GenerationConfig
generation = GenerationConfig()
for name, value in inventory.get("generation_config", {}).items():
setattr(generation, name, value)
# Transformers 5 fills unset fields at generate time; older versions set
# these defaults in the constructor. Resolve them before selecting a mode.
defaults = getattr(generation, "_get_default_generation_params", lambda: {})()
for name, value in defaults.items():
if getattr(generation, name) is None:
setattr(generation, name, value)
generation.do_sample = False # Baseline and verification diagnostics override sampling.
return generation.get_generation_mode().value
def _forward(inventory, mapping, workload, *, generation, use_cache):
from obliteratus.phase_admission import transformer_demands
devices = [parameter_device(block, mapping) for block in inventory["blocks"]]
execution = next((device for device in mapping.values() if device.startswith("cuda:")), "cpu")
placement = [execution if device in {"cpu", "disk"} and execution != "cpu" else "cpu" if device == "disk" else device for device in devices]
staging = {}
for block, stored, target in zip(inventory["blocks"], devices, placement):
if stored != target:
staging[target] = max(staging.get(target, 0), inventory["module_sizes"][block])
for module, size in inventory["module_sizes"].items():
stored = parameter_device(module, mapping)
if stored == "disk" or (stored == "cpu" and execution != "cpu"):
staging[execution] = max(staging.get(execution, 0), size)
cfg = dict(inventory["text_config"], _attn_implementation="eager")
entry = parameter_device(inventory["embedding_module"], mapping)
output = parameter_device(inventory["output_module"], mapping)
entry = execution if entry in {"cpu", "disk"} else entry
output = execution if output in {"cpu", "disk"} else output
batch = _generation_batch_size(inventory, workload) if generation else workload.batch_size
options = dict(dtype_bytes=inventory["dtype_bytes"], resident_bytes={},
largest_tensor_bytes=staging, input_device=entry, output_device=output,
attention_mask=True,
mamba_scan_backend="torch")
branches = _generation_branches(inventory) if generation else 1
outputs = _generation_outputs(inventory) if generation else {}
policies = {(use_cache, batch, branches, False)}
if generation and workload.comparison_prompts:
policies.add((True, workload.batch_size, 1, True)) # Comparisons explicitly use unguided greedy dynamic caching.
demands = []
def add_demand(batch, branches, *, retained_attention_cells=0, **kwargs):
candidate = transformer_demands(cfg, placement, batch_size=batch, **kwargs, **options)
attention = {}
if kwargs.get("output_attentions", cfg.get("output_attentions", False)):
per_cell = batch * cfg["num_attention_heads"] * 4
current = kwargs["tokens"] * kwargs.get("cache_tokens", kwargs["tokens"])
for device in placement:
attention[device] = attention.get(device, 0) + per_cell * retained_attention_cells
if device != entry:
# Accelerate returns tuples to the input device while their
# decoder-device copies may still be live.
attention[entry] = attention.get(entry, 0) + per_cell * (retained_attention_cells + current)
demands.append((candidate, branches, attention))
forward_cache = cfg.get("use_cache")
if forward_cache is None:
forward_cache = True
if generation:
add_demand(workload.batch_size, 1, tokens=workload.sequence_length,
use_cache=forward_cache, logits_copies=3)
for cached, batch, branches, comparison in policies:
returned = {}
if generation:
# A false generation flag can still leave the model's forward
# default enabled. Comparisons explicitly disable both outputs.
returned = {name: False if comparison else bool(cfg.get(name)) or outputs[name]
for name in ("output_hidden_states", "output_attentions")}
retain = generation and not comparison and outputs["return_dict_in_generate"]
logits = (workload.generation_tokens * (outputs["output_scores"] + outputs["output_logits"])
if retain else 0)
add_demand(batch, branches, tokens=workload.sequence_length,
use_cache=cached, cache_tokens=workload.sequence_length,
logits_copies=3 if generation else 1, retained_logits_copies=logits, **returned)
if generation:
final = workload.sequence_length + workload.generation_tokens
query = 1 if cached else final
previous = workload.generation_tokens - 1
hidden_tokens = attention_cells = 0
if retain and previous:
prompt = workload.sequence_length
if cached:
hidden_tokens = prompt + previous - 1
attention_cells = prompt**2 + (previous - 1) * prompt + previous * (previous - 1) // 2
else:
hidden_tokens = previous * prompt + previous * (previous - 1) // 2
attention_cells = (previous * prompt**2 + prompt * previous * (previous - 1)
+ previous * (previous - 1) * (2 * previous - 1) // 6)
add_demand(batch, branches, tokens=query, use_cache=cached,
cache_tokens=final, logits_tokens=query, logits_copies=3,
retained_hidden_tokens=hidden_tokens if retain and outputs["output_hidden_states"] else 0,
retained_attention_cells=attention_cells if retain and outputs["output_attentions"] else 0,
retained_logits_copies=logits, **returned)
result = {}
for candidate, branches, attention in demands:
for device, demand in candidate.items():
# Guidance retains a separate unconditional cache. Bound both
# branches' outputs/workspace too; sequential onload staging is shared.
components = {"activations": branches * (demand.activation_bytes + attention.get(device, 0)),
"kv_cache": branches * demand.kv_bytes,
"logits": branches * demand.output_bytes, "onload": demand.staging_bytes}
if sum(components.values()) > sum(result.get(device, {}).values()):
result[device] = components
return result
def plan_capacity(inventory: dict, free_memory: dict[str, int], *, workload: Workload = Workload(),
host_memory_bytes: int, disk_memory_bytes: int, device_map: dict | None = None,
offload: str = "none", reserve_bytes: int = 2 * GIB,
host_reserve_bytes: int = GIB, source_shard_bytes: int = 5 * GIB,
source_staged: bool = False, snapshot_enabled: bool = False,
snapshot_max_bytes: int = 64 * GIB, unsupported_reason: str | None = None,
concept_edit: bool = False, allow_unbounded_export: bool = False,
quantization: str | None = None) -> dict:
"""Minimize peak utilization over contiguous whole-block partitions, then admit.
Budgets are current free bytes after other users' allocations, not total VRAM.
CPU and disk budgets are independent and must include cgroup/filesystem limits.
"""
from obliteratus.models.snapshot import plan_snapshot
if not free_memory or any(not isinstance(key, str) or not re.fullmatch(r"cuda:\d+|cpu", key) for key in free_memory):
raise ValueError("free_memory needs execution-device byte budgets")
for value in (*free_memory.values(), host_memory_bytes, disk_memory_bytes, reserve_bytes,
host_reserve_bytes, source_shard_bytes, snapshot_max_bytes):
if type(value) is not int or value < 0:
raise ValueError("memory budgets must be nonnegative integer bytes")
if offload not in {"none", "cpu", "disk"}:
raise ValueError("offload must be none, cpu or disk")
if quantization not in {None, "4bit", "8bit"}:
raise ValueError("quantization must be None, 4bit or 8bit")
devices = list(free_memory)
if "cpu" in devices and len(devices) > 1:
raise ValueError("pass CPU offload through offload, not as a GPU execution target")
host_memory_bytes = min(host_memory_bytes, free_memory.get("cpu", host_memory_bytes))
targets = devices + ([offload] if offload != "none" and offload not in devices else [])
blocks = inventory["blocks"]
cfg = inventory["text_config"]
forward_cache = cfg.get("use_cache")
if forward_cache is None:
forward_cache = True
generation_config = inventory.get("generation_config", {})
generation_cache = generation_config.get("use_cache")
if generation_cache is None:
generation_cache = forward_cache
if type(forward_cache) is not bool or type(generation_cache) is not bool:
raise ValueError("use_cache must be boolean or None")
cache_implementation = generation_config.get("cache_implementation")
generation_batch = _generation_batch_size(inventory, workload)
generation_branches = _generation_branches(inventory)
generation_outputs = _generation_outputs(inventory)
generation_mode = _generation_mode(inventory)
generation_assistance = {name: generation_config.get(name)
for name in ("prompt_lookup_num_tokens", "assistant_early_exit", "use_mtp")}
mapping = _module_map(inventory, devices, offload)
hidden = cfg["hidden_size"]
dataset_bytes = workload.samples * len(blocks) * hidden * 4
retained = (workload.activation_datasets * workload.samples * (1 + workload.auxiliary_readouts)
+ workload.neighbour_samples) * len(blocks) * hidden * 4
kl_samples = min(16, workload.samples) if workload.samples >= 10 else 0
baseline_kl = kl_samples * (workload.sequence_length + 1) * cfg["vocab_size"] * 4
kl_workspace = max(7 * workload.sequence_length, kl_samples) * cfg["vocab_size"] * 4 if kl_samples else 0
preservation = None
preservation_retained = preservation_direction = 0
if workload.preserve_refusal:
from dataclasses import replace
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
preservation = replace(workload, batch_size=16, sequence_length=256,
samples=max(len(BUILTIN_HARMFUL), len(BUILTIN_HARMLESS)), n_directions=1,
concept_extraction=False, preserve_refusal=False,
auxiliary_readouts=0, neighbour_samples=0, activation_datasets=2)
preservation_retained = 2 * preservation.samples * len(blocks) * hidden * 4
if inventory["dtype_bytes"] == 4 and any(t in {"cpu", "disk"} for t in targets):
preservation_retained *= preservation.sequence_length
preservation_direction = 2 * len(blocks) * hidden * 4
preservation_rank = min(2 * preservation.samples, hidden)
preservation_workspace = 4 * (6 * preservation.samples * hidden
+ 2 * preservation_rank * (2 * preservation.samples + hidden)
+ preservation_rank**2 + 9 * hidden**2)
comparison_bytes = workload.comparison_prompts * (cfg["vocab_size"] * 4
+ (workload.sequence_length + workload.generation_tokens) * 8)
# Both edit and reference workers persist stock/edited outputs, activation
# means and directions. Reserve both files even when no reference is selected.
observation_disk = (4 * comparison_bytes + 4 * len(blocks) * hidden * 4
if workload.comparison_prompts else 0)
# Distillation concatenates every primary layer while the chunks remain live.
# Validation materializes all readouts, then retains their FP64 copies.
validation_gate = workload.validation_gate or workload.auxiliary_readouts > 0
materialization = max(workload.activation_datasets * dataset_bytes if workload.concept_extraction else 0,
3 * retained if validation_gate else 0)
lexical = 0
lexical_metadata = 0
if validation_gate:
from obliteratus.concepts.gate_memory import lexical_metadata_bytes
rows = workload.activation_datasets * workload.samples
columns = 1024
rank = min(rows, columns)
# Bound gate_memory._lexical_workspace without texts or fold metadata:
# full feature cap, every row in training, FP64 matrices and ridge scratch.
lexical = 8 * (7 * rows * columns + 4 * rank**2 + 16 * rows + 6 * columns)
if workload.prompt_characters is not None:
lexical_metadata = lexical_metadata_bytes(rows, workload.prompt_characters)
# SVD subspace slices retain the complete Vh allocation, plus normalized
# primary directions. Both remain live through editing and export.
direction_storage = len(blocks) * hidden * 4
if workload.n_directions > 1:
direction_storage += len(blocks) * min(workload.samples, hidden) * hidden * 4
# Concept recipes may denoise pooled controls. Even an empty PC prefix can
# retain the complete thin Vh through LayerExtraction.removed_pcs.
if workload.concept_extraction:
control_rows = (workload.activation_datasets - 1) * workload.samples
direction_storage += len(blocks) * min(control_rows, hidden) * hidden * 4
concept_rollback = sum(size for name, size in inventory["tensor_sizes"].items()
if any(name.endswith(suffix) for suffix in (
".o_proj.weight", ".out_proj.weight", ".down_proj.weight",
".c_proj.weight", ".dense_4h_to_h.weight"))) if concept_edit else 0
head_dim = cfg.get("head_dim", hidden // cfg["num_attention_heads"])
final = workload.sequence_length + workload.generation_tokens
cache_rows = max(generation_branches * generation_batch * final if generation_cache else 0,
workload.batch_size * final if workload.comparison_prompts else 0,
workload.batch_size * workload.sequence_length if forward_cache else 0,
preservation.batch_size * preservation.sequence_length if preservation and forward_cache else 0)
kv_per_block = 2 * cache_rows * cfg.get("num_key_value_heads", cfg["num_attention_heads"]) * head_dim * inventory["dtype_bytes"]
prefix = [0]
edit_sizes = [_edit_size(inventory, block) for block in blocks]
edit_ranges = {(start, end): max(edit_sizes[start:end])
for start in range(len(blocks)) for end in range(start + 1, len(blocks) + 1)}
for block in blocks:
prefix.append(prefix[-1] + inventory["module_sizes"][block])
if device_map is None:
# Search untied head placement alongside contiguous block partitions.
# Prefer the default head target when candidates have equal cost.
head_targets = [parameter_device(inventory["output_module"], mapping)]
if not any(name.startswith(inventory["output_module"] + ".")
for group in inventory["tied_parameters"] for name in group):
head_targets.extend(target for target in targets if target not in head_targets)
head_size = max((size for name, size in inventory["tensor_sizes"].items()
if name.startswith(inventory["output_module"] + ".")), default=0)
best = None
for head_target in head_targets:
candidate_map = dict(mapping)
candidate_map[inventory["output_module"]] = head_target
base = dict(candidate_map, **{block: devices[0] for block in blocks})
overheads = {target: 0 for target in targets}
for owner in devices:
base.update({block: owner for block in blocks})
probes = [_forward(inventory, base, workload, generation=True, use_cache=generation_cache)]
if preservation is not None:
probes.append(_forward(inventory, base, preservation, generation=False, use_cache=forward_cache))
for forward in probes:
for target, value in forward.items():
if target in overheads:
overheads[target] = max(overheads[target], sum(value.values()) - value.get("kv_cache", 0))
if offload != "none":
for target in overheads:
overheads[target] += max(inventory["module_sizes"][block] for block in blocks)
fixed = {device: sum(size for name, size in inventory["tensor_sizes"].items()
if not any(name.startswith(block + ".") for block in blocks)
and parameter_device(name, candidate_map) == device) for device in targets}
states = {0: (0.0, [])}
for slot, target in enumerate(targets):
following = {}
budget = disk_memory_bytes if target == "disk" else host_memory_bytes if target == "cpu" else free_memory[target]
if target == "disk":
budget = min(budget, host_memory_bytes)
for start, (score, spans) in states.items():
for end in range(start + 1, len(blocks) - (len(targets) - slot - 1) + 1):
largest = max(edit_ranges[start, end], head_size if target == head_target else 0)
edit = largest * (12 // inventory["dtype_bytes"] + 2)
workspace = max(overheads[target] + (end - start) * kv_per_block, edit)
storage = fixed[target] + prefix[end] - prefix[start]
cost = storage + workspace + reserve_bytes
if target in {"cpu", "disk"}:
# Offloaded edits keep CPU backing overlays after each
# transaction. Disk capacity cannot replace this RAM.
overlays = storage if target == "disk" or offload == "cpu" or any(d.startswith("cuda:") for d in devices) else 0
host_storage = 0 if target == "disk" else storage
cost = (host_storage + overlays + edit + host_reserve_bytes
+ 2 * max(inventory["module_sizes"].values())
+ concept_rollback
+ direction_storage + 2 * comparison_bytes
+ retained * (workload.sequence_length if inventory["dtype_bytes"] == 4
and not workload.concept_extraction else 1))
if target == "disk":
cost = max(cost, storage * host_memory_bytes // max(disk_memory_bytes, 1))
candidate = max(score, cost / max(budget, 1))
if end not in following or candidate < following[end][0]:
following[end] = candidate, spans + [(start, end, target)]
states = following
if len(blocks) in states and (best is None or states[len(blocks)][0] < best[0]):
best = states[len(blocks)][0], candidate_map, states[len(blocks)][1]
if best is None:
raise ValueError("not enough decoder blocks for every requested target")
_, mapping, spans = best
for start, end, target in spans:
mapping.update({block: target for block in blocks[start:end]})
else:
mapping = dict(device_map)
placement = validate_plan(inventory["tensor_sizes"], blocks, mapping, devices,
tied_parameters=inventory["tied_parameters"])
mapping = placement["device_map"]
resident = placement["bytes_by_device"]
activation = _forward(inventory, mapping, workload, generation=False, use_cache=forward_cache)
verification = _forward(inventory, mapping, workload, generation=True, use_cache=generation_cache)
preservation_forward = (_forward(inventory, mapping, preservation, generation=False, use_cache=forward_cache)
if preservation is not None else {})
# Classic CPU FP32 final-token views retain sequence backing; concept
# readouts use advanced indexing or pooling and own compact row storage.
if (inventory["dtype_bytes"] == 4 and not workload.concept_extraction
and ("cpu" in resident or "disk" in resident)):
retained *= workload.sequence_length
rank = min(2 * workload.samples, hidden)
# Exact LEACE fits in FP64 regardless of model dtype. Nine matrix equivalents
# cover covariance, whitening, projection, eigensolver work and the previous
# layer's FP32 eraser. Gated validation also refits on FP64 matrices.
direction_width = (8 if validation_gate
or effective_direction_method(workload.direction_method, warn_alias=False) == "leace_exact" else 4)
direction = direction_width * (6 * workload.samples * hidden
+ 2 * rank * (2 * workload.samples + hidden) + rank**2
+ 9 * hidden**2)
edit_by_device = {}
overlays = 0
cuda_execution = any(device.startswith("cuda:") for device in mapping.values())
for block in blocks:
target = parameter_device(block, mapping)
largest = _edit_size(inventory, block)
target = "cpu" if target == "disk" else target
edit_by_device[target] = max(edit_by_device.get(target, 0), largest * (12 // inventory["dtype_bytes"] + 2))
stored = parameter_device(block, mapping)
if stored == "disk" or stored == "cpu" and (cuda_execution or offload == "cpu"):
overlays += inventory["module_sizes"][block]
head_size = max((size for name, size in inventory["tensor_sizes"].items()
if name.startswith(inventory["output_module"] + ".")), default=0)
head_stored = parameter_device(inventory["output_module"], mapping)
head_edit_device = "cpu" if head_stored == "disk" else head_stored
edit_by_device[head_edit_device] = max(edit_by_device.get(head_edit_device, 0),
head_size * (12 // inventory["dtype_bytes"] + 2))
if head_stored == "disk" or head_stored == "cpu" and cuda_execution:
overlays += head_size
export = dict(inventory["export"])
if any(parameter_device(name, mapping) == "disk"
or parameter_device(name, mapping) == "cpu" and (cuda_execution or offload == "cpu")
for group in inventory["tied_parameters"] for name in group):
export = {"eligible": False, "bounded": False, "status": "ineligible",
"reason": "Offloaded tied parameters require whole-state export"}
if quantization is not None:
export = {"eligible": False, "bounded": False, "status": "ineligible",
"reason": "Requested quantization creates unsupported parameter storage; dense config export eligibility does not apply"}
if not export.get("eligible") and allow_unbounded_export:
export.update(status="operator_fallback", operator_allowed_fallback=True,
host_peak_bytes=4 * inventory["total_weight_bytes"],
reason="Operator explicitly allowed whole-state fallback; four state equivalents reserved on host")
largest_tensor = max(inventory["tensor_sizes"].values(), default=0)
# Each offload owner may load a whole block while a shard is staged.
export_host = export.get("host_peak_bytes", 0) + 2 * max(
(size for module, size in inventory["module_sizes"].items()
if parameter_device(module, mapping) in {"disk", "cpu"}), default=0)
source_shard = max(inventory.get("source_shards", {}).values(), default=source_shard_bytes)
# Snapshots serialize each logical entry, including both members of a tie.
snapshot_sizes = inventory["snapshot_tensor_sizes"]
# Accelerate dispatch uses a disk-restorable loader even for CPU backing.
# CPU-only cpu_offload rehearsals instead retain plain-mapping overlays.
snapshot_overlays = sum(size for name, size in snapshot_sizes.items()
if parameter_device(name, mapping) == "cpu" and not cuda_execution and offload == "cpu")
snapshot_restore = sum(size for name, size in snapshot_sizes.items()
if parameter_device(name, mapping) == "disk"
or parameter_device(name, mapping) == "cpu" and cuda_execution)
# Runtime snapshot admission observes free host memory after weights load.
snapshot_host = max(0, host_memory_bytes - resident.get("cpu", 0))
snapshot = plan_snapshot(sum(snapshot_sizes.values()), max(snapshot_sizes.values(), default=0), len(snapshot_sizes),
enabled=snapshot_enabled, disabled_reason=None if snapshot_enabled else
"Optional pristine snapshot disabled; retain immutable source and reload to restore. No in-process restore is claimed.",
max_bytes=snapshot_max_bytes, overlay_bytes=snapshot_overlays,
disk_restore_bytes=snapshot_restore,
available_host=snapshot_host, available_disk=disk_memory_bytes)
snapshot_disk = snapshot["required_disk_bytes"] if snapshot_enabled else 0
source_disk = 0 if source_staged else inventory.get("source_weight_bytes", inventory["total_weight_bytes"])
phases = {}
for phase in PHASES:
rows = {}
for device in {*devices, "cpu"}:
pieces = {"weights": resident.get(device, 0), "reserve": host_reserve_bytes if device == "cpu" else reserve_bytes}
if phase in {"baseline", "verify", "reload", "activations"}:
pieces.update((activation if phase == "activations" else verification).get(device, {}))
if phase == "direction" and preservation is not None:
pieces.update(preservation_forward.get(device, {}))
if device == "cpu":
if phase in {"edit", "verify"}:
pieces["retained_baseline_kl_logits"] = baseline_kl
pieces["sequence_kl_workspace"] = kl_workspace
if preservation is not None and phase in {"direction", "edit", "verify", "save"}:
pieces["preserved_directions"] = preservation_direction
if preservation is not None and phase == "direction":
pieces["preservation_activations"] = preservation_retained
pieces["preservation_distillation"] = preservation_workspace
pieces["preservation_materialization"] = preservation_retained
if phase != "load" and workload.comparison_prompts:
# Stock outputs survive editing; verify/save retain both sets.
# Reload also loads saved sets. Four sets bound the subsequent
# supervisor comparison of edit and reference observations.
copies = 4 if phase == "reload" else 2 if phase in {"verify", "save"} else 1
pieces["retained_comparisons"] = copies * comparison_bytes
if phase in {"load", "reload"}:
pieces["checkpoint_staging"] = 2 * source_shard + 2 * largest_tensor
if phase in {"activations", "direction", "edit", "verify", "save"}:
pieces["retained_activations"] = retained
if phase in {"direction", "edit", "verify", "save"}:
pieces["retained_directions"] = direction_storage
if phase == "direction":
pieces["direction_extraction"] = direction
pieces["activation_materialization"] = materialization
if validation_gate:
pieces["lexical_probe"] = lexical
pieces["lexical_metadata"] = lexical_metadata
if phase == "activations":
pieces["collection_materialization"] = workload.collection_copies * dataset_bytes
if phase in {"edit", "verify", "save"}:
pieces["offload_edit_overlays"] = overlays
if phase == "edit":
pieces["transaction_rollback"] = 2 * max(inventory["module_sizes"].values())
pieces["concept_transaction_rollback"] = concept_rollback
if phase == "save":
pieces["bounded_export"] = export_host
if phase == "load" and snapshot_enabled:
pieces["snapshot_staging"] = snapshot.get("host_peak_bytes", 0)
if phase == "edit":
pieces["edit_workspace"] = edit_by_device.get(device, 0)
if phase in {"load", "reload"} and device != "cpu":
pieces["checkpoint_device_staging"] = max(
(size for name, size in inventory["tensor_sizes"].items()
if parameter_device(name, mapping) == device), default=0)
budget = host_memory_bytes if device == "cpu" else free_memory[device]
peak = sum(pieces.values())
rows[device] = {"components": pieces, "peak_bytes": peak, "budget_bytes": budget,
"headroom_bytes": budget - peak}
# Reload reads the completed export; only new offload files need space.
reload = phase == "reload"
disk_components = {"source_download": 0 if reload else source_disk,
"output_and_staging": 0 if reload else 2 * inventory["total_weight_bytes"],
"offload": resident.get("disk", 0),
"edit_backing": 0 if reload else resident.get("disk", 0),
"reload_reservation": 0 if reload else resident.get("disk", 0), "snapshot": 0 if reload else snapshot_disk,
"reserve": host_reserve_bytes}
if observation_disk and not reload:
disk_components["qualification_observations"] = observation_disk
disk_peak = sum(disk_components.values())
rows["disk"] = {"components": disk_components, "peak_bytes": disk_peak,
"budget_bytes": disk_memory_bytes, "headroom_bytes": disk_memory_bytes - disk_peak}
phases[phase] = rows
refusals = list(inventory.get("refusals", []))
if offload != "none" and not resident.get(offload):
refusals.append(f"requested {offload} offload has no assigned weights")
if unsupported_reason:
refusals.append(unsupported_reason)
if validation_gate and workload.prompt_characters is None:
refusals.append("Gated lexical metadata requires an untruncated primary/control prompt-character bound; supply --plan-prompt-characters")
if generation_cache and cache_implementation not in {None, "dynamic"}:
refusals.append(f"Generation cache implementation {cache_implementation!r} requires a separate workspace contract")
if generation_mode not in {"greedy_search", "beam_search", "assisted_generation"}:
refusals.append(f"Generation mode {generation_mode!r} requires a separate workspace contract; inherited decoding settings are not covered by ordinary generation estimates")
if (generation_assistance["prompt_lookup_num_tokens"] is not None
or generation_assistance["assistant_early_exit"] is not None
or generation_assistance["use_mtp"]):
refusals.append("Assisted generation requires a separate workspace contract; single-token cached decoding estimates do not cover candidate verification or assistant state")
if quantization is not None:
refusals.append(f"Requested {quantization} quantization requires a separate storage and workspace contract; dense capacity estimates do not cover this execution path")
if inventory["config"]["model_type"] in {"qwen3_5", "qwen3_5_text"}:
refusals.append("Hybrid model capacity is metadata-only: split execution and vision/recurrent kernel workspace require separate qualification under the current single-device loader policy")
if not export.get("eligible") and not allow_unbounded_export:
refusals.append("Native bounded export is ineligible; whole-state fallback is not admitted")
if snapshot.get("status") == "refused":
refusals.append("Snapshot policy refused: " + snapshot["reason"])
for phase, rows in phases.items():
for device, row in rows.items():
if row["headroom_bytes"] < 0:
refusals.append(f"{phase}/{device} shortfall {-row['headroom_bytes']} bytes")
return {"schema_version": 1, "status": "refused" if refusals else "feasible", "refusals": refusals,
"claim": "config_only_capacity_estimate", "runtime_check_required": True,
"workload": asdict(workload), "quantization": quantization,
"generation_batch_size": generation_batch,
"generation_mode": generation_mode,
"generation_output_policy": generation_outputs,
"generation_assistance_policy": generation_assistance,
"cache_policy": {"forward_use_cache": forward_cache, "generation_use_cache": generation_cache,
"generation_cache_implementation": cache_implementation,
"generation_guidance_scale": generation_config.get("guidance_scale"),
"generation_cache_copies": generation_branches if generation_cache else 0,
"comparison_use_cache": True if workload.comparison_prompts else None},
**placement, "phases": phases, "snapshot": snapshot,
"edit_contract": "concept_output_transactions" if concept_edit else "basic_sequential_projection",
"export": dict(export, host_peak_with_offload_bytes=export_host),
"total_weight_bytes": inventory["total_weight_bytes"], "module_sizes": inventory["module_sizes"],
"config_sha256": inventory.get("config_sha256"), "index_sha256": inventory.get("index_sha256"),
"generation_config_sha256": inventory.get("generation_config_sha256"),
"assumptions": ["One model, sequential phases, eager inference, no training or quantization",
"FP64 direction workspace for exact LEACE or gated validation; FP32 otherwise, with retained SVD backing storage and per-layer sample rows",
"Gated validation reserves FP64 lexical presence/training matrices and ridge workspace at the 1024-feature cap, with every primary/control row treated as a training row",
"Gated lexical feature sets and full vocabulary metadata use the untruncated prompt-character bound independently of token sequence length",
"Concept extraction reserves pooled-control PCA backing storage without acquiring the recipe",
"Up to sixteen sequence-wide pristine KL logits and last-token copies are retained on host through editing/verification, bounded at FP32",
"Uncached refusal preservation uses its own batch-16, sequence-256 built-in corpus probe while primary activation caches remain live",
"Qualification comparison outputs retain FP32 vocabulary rows and generated token IDs",
"Default surgery includes the LM head and excludes input embeddings; offloaded edits retain host overlays",
"Source shard bound is operator supplied without an index; index grouping otherwise",
"Model and generation output flags bound per-layer return tensors; structured generation retains outputs across decoding steps",
"Generation inherits local generation_config.json or model config, including beam/return-sequence batch expansion; uncached decoding bounds the full growing sequence",
"Disk includes download, prior output, staging, offload, retained edit backing, reload reservation, optional snapshot and both qualification observation files",
"Runtime free-memory admission and realized export eligibility remain required"]}
def plan_qualification_capacity(inventory: dict, free_memory: dict[str, int], *,
reference: str = "auto", reference_max_bytes: int = GIB,
**options) -> dict:
"""Admit the edit and optional reference workers against sequential budgets."""
if reference not in {"auto", "none", "cpu", "cuda:0"}:
raise ValueError("reference must be auto, none, cpu or cuda:0")
if type(reference_max_bytes) is not int or reference_max_bytes < 0:
raise ValueError("reference limit must be nonnegative integer bytes")
plan = plan_capacity(inventory, free_memory, **options)
host = min(options["host_memory_bytes"], free_memory.get("cpu", options["host_memory_bytes"]))
weight_bytes = inventory["total_weight_bytes"]
selected = reference
if reference == "auto":
selected = "cpu" if weight_bytes <= min(reference_max_bytes, host // 6) else "none"
plan["reference_policy"] = {"selected": selected, "weight_bytes": weight_bytes,
"reason": "automatic host headroom and operator byte limit"
if reference == "auto" else "operator selection"}
if selected != "none":
if selected != "cpu" and selected not in free_memory:
raise ValueError(f"reference device {selected} has no supplied memory budget")
reference_options = dict(options, device_map=None, offload="none", snapshot_enabled=False)
reference_plan = plan_capacity(inventory, {selected: host if selected == "cpu" else free_memory[selected]},
**reference_options)
plan["reference_plan"] = reference_plan
for worker in (plan, reference_plan):
for phase, rows in worker["phases"].items():
if phase == "reload":
continue # Reload admission measures remaining free space.
row = rows["disk"]
row["components"]["retained_reference_export"] = weight_bytes
row["peak_bytes"] += weight_bytes
row["headroom_bytes"] -= weight_bytes
if row["headroom_bytes"] < 0:
worker["refusals"].append(f"{phase}/disk shortfall {-row['headroom_bytes']} bytes")
worker["status"] = "refused" if worker["refusals"] else "feasible"
plan["refusals"].extend(f"reference/{reason}" for reason in reference_plan["refusals"])
if reference_plan["status"] != "feasible":
plan["status"] = "refused"
return plan
def add_capacity_arguments(parser, *, snapshot_execution=False):
parser.add_argument("--plan-only", "--dry-run", action="store_true", help="print a config-only capacity plan; never load weights")
parser.add_argument("--plan-config", type=Path, help="local config JSON or model directory (no Hub requests)")
parser.add_argument("--plan-index", type=Path, help="optional local safetensors index JSON")
parser.add_argument("--plan-free-memory", nargs="+", type=float, help="current free GiB for each GPU, in visible order")
parser.add_argument("--plan-host-memory", type=float, help="available host/cgroup memory in GiB")
parser.add_argument("--plan-disk-memory", type=float, help="available disk in GiB")
parser.add_argument("--plan-batch-size", type=int)
parser.add_argument("--plan-sequence-length", type=int)
parser.add_argument("--plan-generation-tokens", type=int)
parser.add_argument("--plan-samples", type=int, help="samples per class; required when the corpus cannot be counted offline")
parser.add_argument("--plan-prompt-characters", type=int,
help="total untruncated primary/control prompt characters; required for gated plans when texts cannot be counted offline")
parser.add_argument("--plan-neighbour-samples", type=int,
help="total retained neighbour rows; gated concept probes default to three groups bounded by --plan-samples each")
parser.add_argument("--plan-directions", type=int, help="override the number of extracted directions")
parser.add_argument("--plan-offload", choices=("none", "cpu", "disk"), default=None,
help="allow whole blocks and the LM head on this backing tier")
parser.add_argument("--plan-source-shard-gib", type=float, default=5.0, help="source shard bound when index is unavailable")
parser.add_argument("--plan-snapshot", *(("--snapshot",) if snapshot_execution else ()),
choices=("disabled", "disk"), default="disabled",
help="pristine snapshot policy" + (" for planning and execution" if snapshot_execution else " for planning"))
parser.add_argument("--plan-snapshot-max-gib", *(("--snapshot-max-gib",) if snapshot_execution else ()),
type=float, default=64.0)
parser.add_argument("--plan-output", type=Path, help="write plan evidence JSON")
def snapshot_options(args):
"""Use the same explicit snapshot policy for a plan and its execution."""
value = getattr(args, "plan_snapshot_max_gib", 64.0)
if not math.isfinite(value) or value <= 0 or int(value * GIB) < 1:
raise ValueError("snapshot cap must be a positive finite GiB budget")
return {"snapshot_enabled": getattr(args, "plan_snapshot", "disabled") == "disk",
"snapshot_max_bytes": int(value * GIB)}
def print_plan(plan):
print(json.dumps({key: value for key, value in plan.items() if key not in {"phases", "reference_plan"}}, indent=2))
print("phase device peak GiB budget GiB headroom GiB")
for phase, rows in plan["phases"].items():
for device, row in sorted(rows.items()):
print(f"{phase:13} {device:10} {row['peak_bytes']/GIB:10.3f} {row['budget_bytes']/GIB:12.3f} {row['headroom_bytes']/GIB:14.3f}")
if "reference_plan" in plan:
print("reference capacity plan:")
print_plan(plan["reference_plan"])
def _plan_prompt_counts(args):
samples = None
prompt_characters = None
comparison_prompts = 0
if getattr(args, "command", None) in {"obliterate", "abliterate"}:
if getattr(args, "prompt_pairs_file", None):
from obliteratus.prompts import load_prompt_pairs_file
harmful, harmless = load_prompt_pairs_file(args.prompt_pairs_file)
samples = max(len(harmful), len(harmless))
prompt_characters = sum(map(len, harmful)) + sum(map(len, harmless))
elif (not getattr(args, "residue_file", None)
and getattr(args, "concept", None) in {None, "refusal", "core/refusal"}):
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
samples = max(len(BUILTIN_HARMFUL), len(BUILTIN_HARMLESS))
prompt_characters = sum(map(len, BUILTIN_HARMFUL)) + sum(map(len, BUILTIN_HARMLESS))
elif (getattr(args, "concepts_command", None) == "baseline"
and getattr(args, "source", None) in {"refusal", "core/refusal"}):
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
samples = max(len(BUILTIN_HARMFUL), len(BUILTIN_HARMLESS))
prompt_characters = sum(map(len, BUILTIN_HARMFUL)) + sum(map(len, BUILTIN_HARMLESS))
elif hasattr(args, "workload"):
if args.workload is None:
samples = 3 # The qualification driver's offline fixture workload.
comparison_prompts = 2
else:
workload = json.loads(Path(args.workload).read_text())
counts = []
for name in ("harmful_prompts", "harmless_prompts", "comparison_prompts"):
values = workload.get(name) if isinstance(workload, dict) else None
if not isinstance(values, list) or len(values) < 2 or any(not isinstance(s, str) or not s.strip() for s in values):
raise ValueError(f"{name} must contain at least two nonempty strings")
counts.append(len(values))
samples = max(counts[:2])
comparison_prompts = counts[2]
prompt_characters = sum(len(prompt) for name in ("harmful_prompts", "harmless_prompts")
for prompt in workload[name])
explicit_characters = getattr(args, "plan_prompt_characters", None)
if explicit_characters is not None:
if type(explicit_characters) is not int or explicit_characters < 0:
raise ValueError("--plan-prompt-characters must be a nonnegative integer")
if prompt_characters is not None and explicit_characters < prompt_characters:
raise ValueError(f"--plan-prompt-characters cannot be smaller than the selected corpus ({prompt_characters} characters)")
prompt_characters = explicit_characters
explicit = args.plan_samples
if explicit is not None:
if type(explicit) is not int or explicit < 1:
raise ValueError("--plan-samples must be a positive integer")
if samples is not None and explicit < samples:
raise ValueError(f"--plan-samples cannot be smaller than the selected corpus ({samples} samples per class)")
return explicit, comparison_prompts, prompt_characters
if samples is None:
raise ValueError("plan-only requires --plan-samples when the selected corpus cannot be counted offline")
return samples, comparison_prompts, prompt_characters
def maybe_plan(args, model_name: str, *, dtype=None, sequence_length=None, batch_size=None,
generation_tokens=None, unsupported_reason=None, concept_edit=False,
activation_datasets=2, collection_copies=0, device=None, uses_device_map=False,
placement=None):
if not getattr(args, "plan_only", False):
return None
source = getattr(args, "plan_config", None) or Path(model_name)
source = Path(source)
source = source / "config.json" if source.is_dir() else source
if not source.is_file():
raise ValueError("plan-only requires a local --plan-config JSON; weights and remote metadata are never downloaded")
execution_device = device if device is not None else getattr(args, "device", "auto")
requested_placement = None
device_map = None
strategy = None
if placement is not None:
from obliteratus.model_placement import placement_metadata
requested_placement = placement_metadata(None, **placement)
selected_map = requested_placement["requested"].get("device_map")
if isinstance(selected_map, dict):
device_map = {name: normalize_device("cuda:0" if value == "cuda" else value)
for name, value in selected_map.items()}
elif selected_map in {None, "auto", "balanced", "balanced_low_0", "sequential"}:
strategy = selected_map or "auto"
else:
device_map = {"": normalize_device("cuda:0" if selected_map == "cuda" else selected_map)}
targets = set(device_map.values()) if device_map is not None else {
normalize_device(key) for key in requested_placement["requested"].get("max_memory", {})
}
gpus = sorted(target for target in targets if target.startswith("cuda:"))
execution_device = (gpus[0] if len(gpus) == 1 else "auto") if gpus else (
"cpu" if targets and targets <= {"cpu", "disk"} else "auto")
uses_device_map = True
execution_device = "cuda:0" if execution_device == "cuda" else execution_device
if execution_device not in {"auto", "cpu"} and not re.fullmatch(r"cuda:\d+", execution_device):
raise ValueError(f"no capacity contract for execution device {execution_device!r}")
required = ["plan_host_memory", "plan_disk_memory"]
if execution_device != "cpu":
required.append("plan_free_memory")
for name in required:
if getattr(args, name, None) is None:
raise ValueError(f"plan-only requires --{name.replace('_', '-')}")
def bytes_from_gib(value):
if not math.isfinite(value) or value < 0:
raise ValueError("GiB budgets must be finite and nonnegative")
return int(value * GIB)
index_path = getattr(args, "plan_index", None)
if index_path is None and (source.parent / "model.safetensors.index.json").is_file():
index_path = source.parent / "model.safetensors.index.json"
generation_path = source.parent / "generation_config.json"
inventory = meta_inventory(json.loads(source.read_text()), dtype or getattr(args, "dtype", None) or "bfloat16",
json.loads(Path(index_path).read_text()) if index_path else None,
json.loads(generation_path.read_text()) if generation_path.is_file() else None)
def selected(explicit, inherited, default):
return explicit if explicit is not None else inherited if inherited is not None else default
context_length = inventory["text_config"].get("max_position_embeddings", 2048)
samples, comparison_prompts, prompt_characters = _plan_prompt_counts(args)
primary_probe = getattr(args, "command", None) in {"obliterate", "abliterate"}
baseline_probe = getattr(args, "concepts_command", None) == "baseline"
custom_probe = ((primary_probe and getattr(args, "concept", None) not in {None, "refusal", "core/refusal"})
or (baseline_probe and getattr(args, "source", None) not in {"refusal", "core/refusal"}))
gate_mode = getattr(args, "gate_mode", None)
validation_gate = baseline_probe or (primary_probe and gate_mode != "off"
and (custom_probe or gate_mode is not None))
gated_probe = custom_probe and validation_gate
workload = Workload(batch_size=selected(args.plan_batch_size, batch_size, 1),
sequence_length=selected(args.plan_sequence_length, sequence_length, context_length),
generation_tokens=selected(args.plan_generation_tokens, generation_tokens, 16),
samples=samples,
n_directions=selected(args.plan_directions, getattr(args, "n_directions", None), 1),
auxiliary_readouts=3 if gated_probe else 0,
neighbour_samples=selected(getattr(args, "plan_neighbour_samples", None), None,
3 * samples if gated_probe else 0),
activation_datasets=activation_datasets, collection_copies=collection_copies,
concept_extraction=custom_probe,
preserve_refusal=("refusal" in (getattr(args, "preserve", None) or [])
and not getattr(args, "refusal_direction", None)),
comparison_prompts=comparison_prompts,
direction_method=getattr(args, "direction_method", None) or "diff_means",
validation_gate=validation_gate, prompt_characters=prompt_characters)
limitations = [unsupported_reason] if unsupported_reason else []
if strategy not in {None, "auto"}:
limitations.append(f"Placement strategy {strategy!r} is resolved at load time; "
"the capacity candidate does not model this strategy")
if getattr(args, "method", "basic") != "basic":
limitations.append("Only basic sequential projection is budgeted; optional method workspaces require a separate estimate")
repair = getattr(args, "repair", None)
if repair is None:
repair = (getattr(args, "command", None) in {"obliterate", "abliterate"}
and getattr(args, "concept", None) not in {None, "refusal", "core/refusal"})
if repair:
limitations.append("Coherence repair snapshots and candidate workspaces require a separate estimate; protected repair can retain two full model copies on host")
if getattr(args, "intervention_kwargs", ""):
options = json.loads(args.intervention_kwargs)
if options.get("project_embeddings") or options.get("mode") == "lora":
limitations.append("Embedding edits and LoRA intervention workspace require a separate estimate")
if placement is None:
device_map = getattr(args, "device_map", None)
if device_map is not None and placement is None:
if isinstance(device_map, (str, Path)):
device_map = json.loads(Path(device_map).read_text())
if not isinstance(device_map, dict):
raise ValueError("device map JSON must be an object")
from obliteratus.phase_admission import resolve_device_memory_ceilings
ceiling_path = getattr(args, "device_ceilings", None)
ceilings = resolve_device_memory_ceilings(json.loads(Path(ceiling_path).read_text()) if ceiling_path else None)
utilization = getattr(args, "gpu_memory_utilization", None)
if utilization is not None and (isinstance(utilization, bool) or not isinstance(utilization, (int, float))
or not math.isfinite(utilization) or not 0 < utilization <= 1):
raise ValueError("gpu_memory_utilization must be finite and within (0, 1]")
budgets = {f"cuda:{i}": bytes_from_gib(value) for i, value in enumerate(args.plan_free_memory or [])}
caps = {}
if requested_placement is not None:
from accelerate.utils import convert_file_size_to_int
caps = {normalize_device(key): convert_file_size_to_int(value) if isinstance(value, str) else value
for key, value in requested_placement["requested"].get("max_memory", {}).items()}
if device_map is not None:
selected_gpus = {value for value in device_map.values() if value.startswith("cuda:")}
if selected_gpus - budgets.keys():
raise ValueError("--plan-free-memory must include the visible-order budget for every mapped GPU")
budgets = {key: value for key, value in budgets.items() if key in selected_gpus}
elif caps:
selected_gpus = {key for key in caps if key.startswith("cuda:")}
if selected_gpus - budgets.keys():
raise ValueError("--plan-free-memory must include the visible-order budget for every max_memory GPU")
budgets = {key: value for key, value in budgets.items() if key in selected_gpus}
if execution_device.startswith("cuda:"):
if execution_device not in budgets:
raise ValueError(f"--plan-free-memory must include the visible-order budget for {execution_device}")
budgets = {execution_device: budgets[execution_device]}
# Planning precedes CUDA initialization in a fresh process. Scaling free
# bytes also bounds qualification's fraction of total bytes conservatively.
for device, free in budgets.items():
budgets[device] = min(free, ceilings.get(device, free), caps.get(device, free),
int(free * utilization) if utilization is not None else free)
host = min(bytes_from_gib(args.plan_host_memory), ceilings.get("cpu", 2**63), caps.get("cpu", 2**63))
if execution_device == "cpu":
budgets = {"cpu": host}
offload = args.plan_offload if args.plan_offload is not None else getattr(args, "offload", "none")
if execution_device != "auto" and not uses_device_map:
if offload != "none":
limitations.append("Explicit execution devices do not use CPU/disk offload; a separate placement is not admitted")
offload = "none"
if device_map is not None and any(normalize_device(value) != execution_device for value in device_map.values()):
raise ValueError("device map conflicts with the requested execution device")
planner = plan_capacity
reference_options = {}
if hasattr(args, "reference_max_bytes"):
planner = plan_qualification_capacity
reference_options = {"reference": args.reference, "reference_max_bytes": args.reference_max_bytes}
plan = planner(inventory, budgets,
workload=workload, host_memory_bytes=host,
disk_memory_bytes=bytes_from_gib(args.plan_disk_memory),
device_map=device_map or None,
source_shard_bytes=bytes_from_gib(args.plan_source_shard_gib),
offload=offload,
**snapshot_options(args),
concept_edit=concept_edit or bool(getattr(args, "concept", None) not in {None, "refusal", "core/refusal"}
or getattr(args, "preserve", None)),
allow_unbounded_export=getattr(args, "allow_unbounded_export", False),
quantization=getattr(args, "quantization", None),
**reference_options,
unsupported_reason="; ".join(limitations) or None)
plan["metadata_source"] = {"config": str(source), "config_kind": "local_json", "weights_loaded": False}
plan["metadata_source"]["generation_config"] = str(generation_path) if generation_path.is_file() else None
plan["execution_device"] = execution_device
if requested_placement is not None:
plan["placement"] = requested_placement
if strategy is not None:
plan["assumptions"].append("The proposed map is a capacity candidate; Transformers resolves "
"the requested placement strategy when weights are loaded")
plan["assumptions"].append("Fresh process with no existing accelerator reservations; utilization scales supplied free bytes")
print_plan(plan)
if args.plan_output:
args.plan_output.parent.mkdir(parents=True, exist_ok=True)
with args.plan_output.open("x") as stream:
json.dump(plan, stream, indent=2, allow_nan=False)
stream.write("\n")
if plan["status"] == "refused":
raise CapacityPlanningError(plan)
return plan