mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
203 lines
11 KiB
Python
203 lines
11 KiB
Python
"""Incremental host-memory bounds for cached concept validation.
|
|
|
|
The gate retains all concatenations, FP64 primary/auxiliary copies and refitted
|
|
directions, calibration means and low-rank projector factors. Refit, lexical
|
|
ridge and calibration run sequentially: their peaks
|
|
are compared, not multiplied by layers or folds. Bounds include indexed training
|
|
rows, the two-pass component removal, thin SVD/LAPACK work, and dense covariance
|
|
work only for Fisher/LEACE recipes. Existing activation chunks are already resident.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import torch
|
|
|
|
from obliteratus.analysis.direction_methods import effective_direction_method
|
|
from obliteratus.concepts.extraction import validate_recipe
|
|
from obliteratus.concepts.gate import (
|
|
GateCacheInputs, _fold_indices, _lexical_prompt_groups,
|
|
auxiliary_calibration_recipe,
|
|
tested_neighbour_names, validation_recipe_and_folds,
|
|
)
|
|
|
|
|
|
def _group_layout(group):
|
|
"""Return layer -> (rows, hidden, concatenated element width, needs FP64 copy)."""
|
|
result = {}
|
|
for layer, chunks in group.items():
|
|
if not chunks:
|
|
continue
|
|
rows, hidden, dtype = 0, None, chunks[0].dtype
|
|
for value in chunks:
|
|
if (value.device.type != "cpu" or value.ndim != 2 or not all(value.shape)
|
|
or not value.is_floating_point()):
|
|
raise ValueError("Gate admission requires nonempty floating-point CPU chunks")
|
|
if hidden is not None and hidden != value.shape[1]:
|
|
raise ValueError("Gate activation chunk hidden dimensions disagree")
|
|
hidden = value.shape[1]
|
|
rows += value.shape[0]
|
|
dtype = torch.promote_types(dtype, value.dtype)
|
|
result[layer] = (rows, hidden, torch.finfo(dtype).bits // 8, dtype != torch.float64)
|
|
return result
|
|
|
|
|
|
def _pair_layout(pair, layers):
|
|
positive, controls = pair
|
|
groups = {"positive": _group_layout(positive),
|
|
**{f"control:{name}": _group_layout(group) for name, group in controls.items()}}
|
|
if not controls or any(set(group) != set(layers) for group in groups.values()):
|
|
raise ValueError("Validation cache must match direction layers and contain controls")
|
|
hidden = groups["positive"][layers[0]][1]
|
|
sizes = {}
|
|
for name, group in groups.items():
|
|
sizes[name] = group[layers[0]][0]
|
|
if any(row[0] != sizes[name] or row[1] != hidden for row in group.values()):
|
|
raise ValueError("Validation cache dimensions/counts disagree")
|
|
return groups, sizes, hidden
|
|
|
|
|
|
def _bytes(groups, *, fp64=False):
|
|
return sum(rows * hidden * (8 if fp64 else width)
|
|
for group in groups.values() for rows, hidden, width, copy in group.values()
|
|
if not fp64 or copy)
|
|
|
|
|
|
def _svd_workspace(rows, columns):
|
|
# Thin outputs, destructive LAPACK input copy, divide-and-conquer workspace,
|
|
# and centered/weighted operands. No full hidden x hidden V is requested.
|
|
rank = min(rows, columns)
|
|
return 8 * (3 * rows * columns + 2 * rank * (rows + columns)
|
|
+ 4 * rank * rank + 64 * rank)
|
|
|
|
|
|
def _extraction_workspace(positive, control, hidden, recipe, control_groups=1):
|
|
"""One full or indexed refit, including the previous loop's control samples."""
|
|
rank = min(control, hidden) if recipe.denoise else 0
|
|
# CV indexed inputs, pooled controls, H/B and the two-pass subtraction peak.
|
|
linear = 8 * (max(4 * positive + 3 * control, 2 * positive + 6 * control) * hidden
|
|
+ max(positive, control) * rank + 24 * hidden + 12 * (positive + control))
|
|
method = effective_direction_method(recipe.method, warn_alias=False)
|
|
decomposition = 0
|
|
if method == "svd":
|
|
decomposition = _svd_workspace(positive, hidden)
|
|
elif method in {"fisher_lda", "leace_exact"}:
|
|
# Centered H/B, covariance products and sum, regularizer / outer product,
|
|
# solve or eigh output/workspace, and LEACE's supported-eigenvector copy.
|
|
matrices = 5 if method == "fisher_lda" else 7
|
|
decomposition = 8 * (matrices * hidden * hidden + (positive + control) * hidden)
|
|
if method == "leace_exact" and recipe.n_directions > 1:
|
|
classes = control_groups + 1
|
|
# Named-class means/contrasts, supported whitening and low-rank
|
|
# factors coexist with covariance/eigh and the thin contrast SVD.
|
|
decomposition += (8 * (2 * hidden * hidden + 4 * classes * hidden
|
|
+ 4 * recipe.n_directions * hidden)
|
|
+ _svd_workspace(hidden, classes))
|
|
if recipe.denoise and recipe.denoise["control_pc_variance"] > 0:
|
|
decomposition = max(decomposition, _svd_workspace(control, hidden))
|
|
return linear + decomposition
|
|
|
|
|
|
def lexical_metadata_bytes(rows, characters):
|
|
# Casefold can triple character counts. Reserve feature strings (including
|
|
# wide Unicode), set unions, Counter entries, sorting keys and row objects;
|
|
# the 1024-column cap does not bound any of these untruncated allocations.
|
|
return 2048 * characters + 1024 * rows
|
|
|
|
|
|
def _lexical_workspace(texts, sizes, splits):
|
|
if texts is None:
|
|
return 0
|
|
groups = _lexical_prompt_groups(texts, sizes)
|
|
total = sum(sizes.values())
|
|
characters = sum(len(prompt) for prompts in groups.values() for prompt in prompts)
|
|
metadata = lexical_metadata_bytes(total, characters)
|
|
maximum = 0
|
|
for train, _ in splits:
|
|
training, columns = sum(len(indices) for indices in train.values()), 1024
|
|
rank = min(training, columns)
|
|
# Per-group presence matrices, indexed/concatenated/centered training X,
|
|
# old fold X/gram retained during reassignment, ridge/solve and AUC scratch.
|
|
maximum = max(maximum, 8 * (4 * total * columns + 3 * training * columns
|
|
+ 4 * rank * rank + 16 * total + 6 * columns))
|
|
return metadata + maximum
|
|
|
|
|
|
def estimate_gate_host_bytes(direction, inputs: GateCacheInputs, *, random_directions=1000) -> int:
|
|
"""Bound new host bytes before ``inputs.materialize()`` and gate execution.
|
|
|
|
Only tensor shape/dtype and prompt lengths are inspected. No feature sets, matrix
|
|
concatenation, finite-value scan, extraction, covariance or factorization is
|
|
run by admission. The private fold generator leaves the caller's RNG intact.
|
|
The returned demand excludes resident chunks/directions and includes Python
|
|
random-calibration scores (32 bytes each). Invalid layouts reject explicitly.
|
|
"""
|
|
if type(random_directions) is not int or random_directions < 1:
|
|
raise ValueError("random_directions must be a positive integer")
|
|
layers = sorted(direction.directions)
|
|
if not layers:
|
|
raise ValueError("Gate admission requires at least one direction layer")
|
|
primary, sizes, hidden = _pair_layout((inputs.positive, inputs.controls), layers)
|
|
if any(tuple(direction.directions[layer].shape) != (hidden,) for layer in layers):
|
|
raise ValueError("Direction and activation hidden dimensions must agree")
|
|
recipe, folds = validation_recipe_and_folds(direction, sizes, inputs.sample_groups)
|
|
validate_recipe(recipe)
|
|
seed = 0 if direction.seed is None else direction.seed
|
|
splits = _fold_indices(sizes, folds, seed, inputs.sample_groups) if folds >= 2 else []
|
|
concatenations, primary_copies = _bytes(primary), _bytes(primary, fp64=True)
|
|
auxiliary_copies = 0
|
|
for pair in [*inputs.readouts.values(), *([inputs.masked] if inputs.masked else [])]:
|
|
groups, counts, width = _pair_layout(pair, layers)
|
|
if counts != sizes or width != hidden:
|
|
raise ValueError("Readout/masked caches must match primary sample shapes")
|
|
concatenations += _bytes(groups)
|
|
auxiliary_copies += _bytes(groups, fp64=True)
|
|
neighbour_sizes = []
|
|
for name, group in inputs.neighbours.items():
|
|
layout = _group_layout(group)
|
|
concatenations += _bytes({name: layout})
|
|
if name in tested_neighbour_names(inputs.neighbours):
|
|
groups, counts, width = _pair_layout((group, inputs.controls), layers)
|
|
if width != hidden:
|
|
raise ValueError("Neighbour hidden dimensions must match primary activations")
|
|
auxiliary_copies += _bytes({name: groups["positive"]}, fp64=True)
|
|
neighbour_sizes.append(counts["positive"])
|
|
vectors = sum(value.numel() * 8 for value in direction.directions.values()
|
|
if value.device.type != "cpu" or value.dtype != torch.float64)
|
|
positive = sizes["positive"]
|
|
controls = [count for name, count in sizes.items() if name != "positive"]
|
|
control = sum(controls)
|
|
method = effective_direction_method(recipe.method, warn_alias=False)
|
|
rank = min(recipe.n_directions, hidden if method == "leace_exact" else positive, hidden)
|
|
subspaces = len(layers) * rank * hidden * 8
|
|
geometry = len(layers) * hidden * 8 * (1 + (2 * rank if method == "leace_exact" else 0))
|
|
# A selected PC prefix is a view into a full thin Vh; full-fit results for
|
|
# every layer coexist during CV, even if the retained prefix has zero rows.
|
|
pcs = (len(layers) * min(control, hidden) * hidden * 8
|
|
if recipe.denoise and recipe.denoise["control_pc_variance"] > 0 else 0)
|
|
refit = (subspaces + pcs + geometry
|
|
+ _extraction_workspace(positive, control, hidden, recipe, len(controls))
|
|
if folds >= 2 else 0)
|
|
# _pair's finite scan, fixed-vector fold indexing/orientation, AUC ranks and
|
|
# several H-vectors; reference conversion is only one vector at a time.
|
|
calibration = (8 * ((positive + 2 * control) * hidden + 16 * hidden)
|
|
+ 128 * (positive + max(controls)))
|
|
scan = max(row[0] for group in primary.values() for row in group.values()) * hidden * 10
|
|
calibration = max(calibration, scan)
|
|
calibration_recipe = auxiliary_calibration_recipe(recipe)
|
|
for count in controls if len(controls) > 1 else []:
|
|
calibration = max(
|
|
calibration, _extraction_workspace(count, control - count, hidden,
|
|
calibration_recipe, len(controls) - 1),
|
|
)
|
|
for count in neighbour_sizes:
|
|
calibration = max(calibration, _extraction_workspace(
|
|
count, control, hidden, calibration_recipe, len(controls),
|
|
))
|
|
# Both validate_direction and extract_directions retain fold index tensors.
|
|
indices = 8 * (max(folds, 1) + 3) * sum(sizes.values())
|
|
records = 32 * random_directions * len(layers)
|
|
peak = max(_lexical_workspace(inputs.texts, sizes, splits), refit,
|
|
auxiliary_copies + (subspaces + geometry if folds >= 2 else 0)
|
|
+ calibration + records)
|
|
return int(concatenations + primary_copies + vectors + indices + peak)
|