mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Selection scoring stacked up to 64 activations per chunk to compute their norms; compute each activation's norm directly instead, so scoring adds only scalars. The admitted DISTILL workspace also never counted COSMIC's four FP64 copies of the class means, which can overlap the last layer's extraction vectors; include them. tests/test_phase_optional_admission.py measured a 1308-byte peak against a 1152-byte admission before this change.
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Activation-relative strength scores, fused layer caps, and subspace ranks."""
|
|
|
|
import math
|
|
from collections.abc import Mapping, Sequence
|
|
|
|
import torch
|
|
|
|
|
|
def normalize_strength(
|
|
strength: float,
|
|
scale_power: int,
|
|
harmful: Sequence[torch.Tensor],
|
|
harmless: Sequence[torch.Tensor],
|
|
) -> float:
|
|
"""Divide amplitudes by mean activation norm and energies by its square."""
|
|
if scale_power == 0:
|
|
# Covariance- and distortion-normalized extractors supply their own score.
|
|
return strength
|
|
|
|
total = 0.0
|
|
count = 0
|
|
for samples in (harmful, harmless):
|
|
# One scalar per stored activation: no stacked copy, so DISTILL's
|
|
# admitted peak memory is unchanged by selection scoring.
|
|
for sample in samples:
|
|
norm = float(torch.linalg.vector_norm(sample).item())
|
|
if math.isfinite(norm):
|
|
total += norm
|
|
count += 1
|
|
|
|
if count == 0 or total == 0:
|
|
return 0.0
|
|
normalized = strength / (total / count) ** scale_power
|
|
return normalized if math.isfinite(normalized) else 0.0
|
|
|
|
|
|
def cap_fused_layers(
|
|
ranked_layers: list[int],
|
|
knee_candidates: list[int],
|
|
cosmic_candidates: list[int],
|
|
cap: int,
|
|
) -> tuple[list[int], int]:
|
|
"""Reserve up to ceil(cap / 3) slots for eligible COSMIC-only layers."""
|
|
cosmic_only = set(cosmic_candidates).difference(knee_candidates)
|
|
eligible = [idx for idx in ranked_layers if idx in cosmic_only]
|
|
quota = min(len(eligible), (cap + 2) // 3)
|
|
reserved = set(eligible[:quota])
|
|
remaining = [idx for idx in ranked_layers if idx not in reserved][:cap - quota]
|
|
kept = reserved.union(remaining)
|
|
return [idx for idx in ranked_layers if idx in kept], quota
|
|
|
|
|
|
def effective_subspace_ranks(subspaces: Mapping[int, torch.Tensor]) -> dict[str, int]:
|
|
"""Measure the numerical rank of each stored direction basis on CPU."""
|
|
ranks = {}
|
|
for idx, subspace in subspaces.items():
|
|
basis = subspace.detach().to(device="cpu", dtype=torch.float32)
|
|
basis = basis[torch.isfinite(basis).all(dim=-1)]
|
|
ranks[str(idx)] = int(torch.linalg.matrix_rank(basis).item()) if basis.numel() else 0
|
|
return ranks
|