Files
OBLITERATUS/tests/test_phase_edit_admission.py
Joseph MaglyandClaude Opus 5.5 278b285525 Bound device temporaries of concept weight interventions
Finiteness checks use scalar reductions, and large embeddings and residual
writers are projected in bounded row chunks with CPU staging, so a faithful
edit including the embedding fits next to a model that nearly fills the
device. Coordinate-direction edits stay bit-exact; general chunked kernels
can differ by compute-dtype rounding.

Fixes #325

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 01:49:50 -04:00

450 lines
20 KiB
Python

"""Edit peak bounds checked against tensor lifetimes and a weight-free GPU calibration."""
from __future__ import annotations
from types import SimpleNamespace
import weakref
import pytest
import torch
from torch._subclasses.fake_tensor import FakeTensorMode
from torch.utils._python_dispatch import TorchDispatchMode
from torch.utils._pytree import tree_flatten
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.analysis.numerical_contracts import all_finite, validated_weight_update
from obliteratus.phase_admission import GIB, _edit_workspace
from tests.test_phase_memory_helpers import cpu_memory_profile, cpu_peak_bytes
from tests.test_phase_memory_helpers import cpu_threads as cpu_threads
class _LiveTensorBytes(TorchDispatchMode):
"""Inventory live output storage; native-kernel scratch needs allocator profiling."""
def __init__(self, resident):
super().__init__()
self.resident = {tensor.untyped_storage()._cdata for tensor in resident}
self.storages = {}
self.peak = 0
def __torch_dispatch__(self, func, types, args=(), kwargs=None):
result = func(*args, **(kwargs or {}))
for tensor in tree_flatten(result)[0]:
if not isinstance(tensor, torch.Tensor):
continue
storage = tensor.untyped_storage()
key = storage._cdata
if key in self.resident:
continue
_, refs = self.storages.get(key, (0, []))
refs = [reference for reference in refs if reference() is not None]
refs.append(weakref.ref(tensor))
self.storages[key] = (storage.nbytes(), refs)
# Forget freed outputs as we go. Keeping every historical allocation
# makes long gate refits quadratic in operation count under coverage;
# only live storage can contribute to this inventory's current peak.
self.storages = {
key: (size, live_refs)
for key, (size, refs) in self.storages.items()
if (live_refs := [ref for ref in refs if ref() is not None])
}
live = sum(size for size, _ in self.storages.values())
self.peak = max(self.peak, live)
return result
def _matrix(rows, columns, *, dtype=torch.float32, device="cpu"):
module = torch.nn.Module()
module.in_features = columns
module.out_features = rows
module.weight = torch.nn.Parameter(torch.empty(rows, columns, dtype=dtype, device=device))
return module
def _group(dtype=torch.float32, device="cpu"):
model = torch.nn.Module()
model.block = torch.nn.Module()
model.block.q_proj = _matrix(128, 64, dtype=dtype, device=device)
model.block.k_proj = _matrix(32, 64, dtype=dtype, device=device)
model.block.v_proj = _matrix(32, 64, dtype=dtype, device=device)
return SimpleNamespace(model=model)
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("layout", ["contiguous", "transposed", "strided"])
def test_storage_validation_uses_scalar_workspace(dtype, layout, cpu_threads):
source = torch.ones(128, 128, dtype=dtype)
value = source.T if layout == "transposed" else source[:, ::2] if layout == "strided" else source
tracker = _LiveTensorBytes([source])
with cpu_memory_profile() as profile, tracker:
updated = validated_weight_update(value, value)
assert updated is value
assert 0 < tracker.peak <= value.element_size()
assert 0 < cpu_peak_bytes(profile) <= 1024
@pytest.mark.parametrize("bad_value", [None, float("nan"), float("inf"), -float("inf")])
def test_shared_finite_check_never_allocates_a_weight_mask(bad_value, cpu_threads):
value = torch.ones(512, 128)
if bad_value is not None:
value[-1, -1] = bad_value
tracker = _LiveTensorBytes([value])
with cpu_memory_profile() as profile, tracker:
finite = all_finite(value.T)
assert finite == (bad_value is None)
assert 0 < tracker.peak <= value.element_size()
assert 0 < cpu_peak_bytes(profile) <= 1024
@pytest.mark.parametrize("rows", [1024, 4096])
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("norm_preserve", [False, True])
def test_embedding_projection_temporary_memory_is_chunk_bounded(
monkeypatch, rows, dtype, norm_preserve, cpu_threads,
):
import obliteratus.concepts.intervention as intervention
weight = torch.linspace(-1, 1, rows * 64, dtype=dtype).reshape(rows, 64)
direction = torch.linspace(-1, 1, 64, dtype=torch.float64 if dtype == torch.float64 else torch.float32)
budget = 16 * 1024
monkeypatch.setattr(intervention, "_PROJECTION_CHUNK_BYTES", budget)
target = (SimpleNamespace(tensor=weight), weight, direction, False)
tracker = _LiveTensorBytes([weight, direction])
with cpu_memory_profile() as profile, tracker:
updated = intervention._project_writer(
"embedding.weight", target, None, norm_preserve=norm_preserve, regularization=0.2,
)
# The full result is deliberately CPU storage; exclude it from scratch.
# This bound stays fixed as vocabulary grows, and covers native reductions.
result_bytes = updated.numel() * updated.element_size()
scratch_bound = 6 * budget + 8 * direction.numel() * direction.element_size()
assert updated.device.type == "cpu"
assert 0 < tracker.peak - result_bytes <= scratch_bound
assert 0 < cpu_peak_bytes(profile) - result_bytes <= scratch_bound
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32])
def test_embedding_lora_conversion_is_chunk_bounded(monkeypatch, dtype, cpu_threads):
import obliteratus.concepts.intervention as intervention
weight = torch.linspace(-1, 1, 4096 * 64, dtype=dtype).reshape(4096, 64)
direction = torch.linspace(-1, 1, 64)
expected = weight.float() @ direction
budget = 16 * 1024
monkeypatch.setattr(intervention, "_PROJECTION_CHUNK_BYTES", budget)
tracker = _LiveTensorBytes([weight, direction])
with tracker:
actual = intervention._chunked_matvec(weight, direction)
torch.testing.assert_close(actual, expected)
assert tracker.peak <= actual.numel() * actual.element_size() + 2 * budget
def test_resident_norm_restoration_needs_no_scaled_matrix_copy(cpu_threads):
layer = torch.nn.Sequential(torch.nn.Linear(128, 128, bias=False))
saved = AbliterationPipeline._capture_layer_weight_norms(layer)
saved = {name: norm * 1.05 for name, norm in saved.items()}
expected = layer[0].weight.detach().clone() * 1.05
tracker = _LiveTensorBytes(list(layer.parameters()))
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
AbliterationPipeline._restore_layer_weight_norms(layer, saved)
torch.testing.assert_close(layer[0].weight, expected)
assert 0 < tracker.peak <= 1024
assert 0 < cpu_peak_bytes(profile) <= 1024
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("fold_post_norms", [False, True])
@pytest.mark.parametrize("use_lora", [False, True])
def test_sandwich_excise_peak_fits_selected_folding_workspace(
dtype, fold_post_norms, use_lora, cpu_threads,
):
from tests.test_concept_intervention import _tiny_residual_model
from tests.test_pipeline_post_norms import _pipeline
model = _tiny_residual_model("gemma2").to(dtype)
pipeline = _pipeline(
model, fold_post_norms=fold_post_norms, use_lora_ablation=use_lora,
norm_preserve=not use_lora, projection_target="all", lora_rank=2,
)
for idx, direction in pipeline.concept_directions.items():
pipeline.concept_subspaces[idx] = torch.stack((direction, direction.roll(1)))
ranks = pipeline._lora_adapter_ranks()
ordinary = _edit_workspace(
pipeline.handle, pipeline._excise_parameter_names(), adapter_ranks=ranks or None,
)
_, sequential, _ = pipeline._optional_phase_workspaces("excise", [])
bound = max(ordinary["cpu"], sequential.get("cpu", 0))
if not fold_post_norms:
assert sequential == {} and bound == ordinary["cpu"]
tracker = _LiveTensorBytes([
*model.parameters(), *model.buffers(), *pipeline.concept_directions.values(),
*pipeline.concept_subspaces.values(),
])
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
pipeline._excise()
assert 0 < tracker.peak <= bound
assert 0 < cpu_peak_bytes(profile) <= bound
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("norm_preserve", [False, True])
def test_dense_transaction_peak_covers_actual_projection_lifetimes(dtype, norm_preserve, cpu_threads):
handle = _group(dtype=dtype)
for parameter in handle.model.parameters():
torch.nn.init.normal_(parameter, std=0.1)
direction = torch.linspace(
-1, 1, 64, dtype=torch.float64 if dtype == torch.float64 else torch.float32
)
names = list(dict(handle.model.named_parameters()))
bound = _edit_workspace(handle, names)["cpu"]
tracker = _LiveTensorBytes([*handle.model.parameters(), direction])
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
modified = AbliterationPipeline._project_out_advanced(
handle.model.block,
direction,
["q_proj", "k_proj", "v_proj"],
norm_preserve=norm_preserve,
)
assert modified == 3
assert tracker.peak > handle.model.block.q_proj.weight.numel() * 4
assert tracker.peak <= bound
assert 0 < cpu_peak_bytes(profile) <= bound
def test_fp32_source_is_resident_and_does_not_need_another_full_conversion():
with FakeTensorMode():
full = _group(dtype=torch.float32, device="cuda:0")
half = _group(dtype=torch.bfloat16, device="cuda:0")
name = ["block.q_proj.weight"]
full_bound = _edit_workspace(full, name)["cuda:0"]
half_bound = _edit_workspace(half, name)["cuda:0"]
logical_bytes = full.model.block.q_proj.weight.numel() * 4
assert half_bound - full_bound == logical_bytes
def test_tiny_cuda_edits_include_one_allocator_allowance_per_device():
with FakeTensorMode():
host = _group()
cuda = _group(device="cuda:0")
names = list(dict(host.model.named_parameters()))
host_bound = _edit_workspace(host, names)["cpu"]
cuda_bound = _edit_workspace(cuda, names)["cuda:0"]
assert cuda_bound - host_bound == 1024**2
assert _edit_workspace(cuda, []) == {}
def test_ffn_output_and_input_calls_do_not_accumulate_one_transaction():
with FakeTensorMode():
model = torch.nn.Module()
model.mlp = torch.nn.Module()
model.mlp.down_proj = _matrix(64, 512, dtype=torch.bfloat16, device="cuda:0")
model.mlp.up_proj = _matrix(512, 64, dtype=torch.bfloat16, device="cuda:0")
model.mlp.gate_proj = _matrix(512, 64, dtype=torch.bfloat16, device="cuda:0")
handle = SimpleNamespace(model=model)
all_names = list(dict(model.named_parameters()))
input_names = [name for name in all_names if "down_proj" not in name]
# up/gate coexist, but the down-projection call has completed first.
assert _edit_workspace(handle, all_names) == _edit_workspace(handle, input_names)
@pytest.mark.parametrize("expert_name", ["experts", "shared_expert", "shared_experts"])
def test_combined_expert_ffn_transaction_covers_retained_updates_and_rollback(expert_name, cpu_threads):
model = torch.nn.Module()
expert = torch.nn.Module()
expert.down_proj = _matrix(64, 128)
expert.up_proj = _matrix(128, 64)
expert.gate_proj = _matrix(128, 64)
setattr(model, expert_name, expert)
for parameter in model.parameters():
torch.nn.init.normal_(parameter, std=0.1)
direction = torch.linspace(-1, 1, 64)
handle = SimpleNamespace(model=model)
bound = _edit_workspace(handle, list(dict(model.named_parameters())))["cpu"]
tracker = _LiveTensorBytes([*model.parameters(), direction])
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
AbliterationPipeline._project_out_advanced(
expert, direction, ["down_proj", "up_proj", "gate_proj"],
)
assert bound >= 6 * 128 * 64 * 4
assert tracker.peak <= bound
assert 0 < cpu_peak_bytes(profile) <= bound
def test_bias_only_transaction_has_a_nonzero_bound_covering_all_siblings(cpu_threads):
handle = _group()
for child in handle.model.block.children():
child.bias = torch.nn.Parameter(torch.ones(64))
names = [name for name in dict(handle.model.named_parameters()) if name.endswith(".bias")]
direction = torch.linspace(-1, 1, 64)
bound = _edit_workspace(handle, names)["cpu"]
tracker = _LiveTensorBytes([*handle.model.parameters(), direction])
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
modified = AbliterationPipeline._project_bias(
handle.model.block, direction, ["q_proj", "k_proj", "v_proj"],
)
assert modified == 3
assert bound >= tracker.peak > 0
assert 0 < cpu_peak_bytes(profile) <= bound
def test_qwen_1_5b_bf16_edit_bound_matches_observed_peak_without_allocating_weights():
# User GPU evidence: Qwen2.5-1.5B, H=1536, V=151936, BF16, basic method:
# additional EXCISE CUDA peak2.61GiB. FakeTensorMode allocates no real weights.
with FakeTensorMode():
model = torch.nn.Module()
model.layers = torch.nn.ModuleList()
for _ in range(28):
layer = torch.nn.Module()
layer.self_attn = torch.nn.Module()
for name, rows in (
("q_proj", 1536),
("k_proj", 256),
("v_proj", 256),
("o_proj", 1536),
):
setattr(
layer.self_attn,
name,
_matrix(
rows,
1536,
dtype=torch.bfloat16,
device="cuda:0",
),
)
layer.mlp = torch.nn.Module()
layer.mlp.up_proj = _matrix(8960, 1536, dtype=torch.bfloat16, device="cuda:0")
layer.mlp.gate_proj = _matrix(8960, 1536, dtype=torch.bfloat16, device="cuda:0")
layer.mlp.down_proj = _matrix(1536, 8960, dtype=torch.bfloat16, device="cuda:0")
model.layers.append(layer)
model.lm_head = _matrix(151936, 1536, dtype=torch.bfloat16, device="cuda:0")
handle = SimpleNamespace(model=model)
bound = _edit_workspace(handle, list(dict(model.named_parameters())))["cuda:0"]
head_elements = 151936 * 1536
assert 3 * head_elements * 4 <= bound
assert 2.61 <= bound / GIB <= 5.5
assert bound / GIB < 2.7
def test_unselected_head_and_other_layers_do_not_inflate_edit_peak():
with FakeTensorMode():
handle = _group(dtype=torch.bfloat16, device="cuda:0")
handle.model.lm_head = _matrix(151936, 1536, dtype=torch.bfloat16, device="cuda:0")
names = ["block.q_proj.weight"]
assert _edit_workspace(handle, names)["cuda:0"] < 2 * 1024**2
assert _edit_workspace(handle, []) == {}
with pytest.raises(ValueError, match="unknown surgery parameter names"):
_edit_workspace(handle, ["missing.weight"])
def test_root_owned_projection_parameter_has_a_valid_transaction_group():
model = _matrix(32, 64)
assert _edit_workspace(SimpleNamespace(model=model), ["weight"])["cpu"] > 0
def test_edit_budget_stays_on_each_tensors_actual_device():
with FakeTensorMode():
handle = _group(dtype=torch.bfloat16, device="cuda:0")
handle.model.other = _matrix(32, 16, dtype=torch.bfloat16, device="cuda:1")
names = list(dict(handle.model.named_parameters()))
together = _edit_workspace(handle, names)
assert (
together["cuda:0"]
== _edit_workspace(
handle,
[name for name in names if name.startswith("block.")],
)["cuda:0"]
)
assert together["cuda:1"] == _edit_workspace(handle, ["other.weight"])["cuda:1"]
@pytest.mark.parametrize("source_dtype", [torch.bfloat16, torch.float32, None])
def test_packed_projection_includes_dequantized_sources_conversion_and_updates(source_dtype):
with FakeTensorMode():
model = torch.nn.Module()
model.proj = torch.nn.Module()
model.proj.weight = torch.nn.Parameter(
torch.empty(128, 32, dtype=torch.uint8, device="cuda:0"),
requires_grad=False,
)
model.proj.weight.quant_state = SimpleNamespace(shape=(128, 64), dtype=source_dtype)
bound = _edit_workspace(SimpleNamespace(model=model), ["proj.weight"])["cuda:0"]
logical = 128 * 64 * 4
if source_dtype == torch.bfloat16:
# FP16 source coexists with work conversion, product and result.
assert bound >= logical // 2 + 3 * logical
elif source_dtype == torch.float32:
assert bound >= 3 * logical
else:
assert bound >= 4 * logical
def test_meta_projection_charges_host_materialization_without_reading_backing_store():
handle = _group(dtype=torch.bfloat16, device="meta")
names = list(dict(handle.model.named_parameters()))
bound = _edit_workspace(handle, names)
assert set(bound) == {"cpu"}
with FakeTensorMode():
resident = _group(dtype=torch.bfloat16)
source_bytes = sum(
parameter.numel() * parameter.element_size() for parameter in handle.model.parameters()
)
assert bound["cpu"] >= _edit_workspace(resident, names)["cpu"] + source_bytes
def test_meta_commits_keep_host_backing_updates_across_later_groups():
model = torch.nn.Module()
model.first = _matrix(128, 64, device="meta")
model.second = _matrix(128, 64, device="meta")
handle = SimpleNamespace(model=model)
first = _edit_workspace(handle, ["first.weight"])["cpu"]
both = _edit_workspace(handle, ["first.weight", "second.weight"])["cpu"]
assert both - first == 128 * 64 * 4
@pytest.mark.parametrize("device", ["cpu", "meta"])
def test_fused_experts_use_one_slice_but_offload_needs_full_clones(device):
with FakeTensorMode():
model = torch.nn.Module()
model.experts = torch.nn.Module()
model.experts.down_proj = torch.nn.Parameter(
torch.empty(16, 128, 64, dtype=torch.float32, device=device),
)
bound = _edit_workspace(SimpleNamespace(model=model), ["experts.down_proj"])["cpu"]
whole = 16 * 128 * 64 * 4
if device == "cpu":
assert bound == 3 * whole // 16
else:
assert bound >= 3 * whole + 3 * whole // 16
def test_lora_pairs_remain_resident_across_groups_and_add_to_peak():
with FakeTensorMode():
handle = _group(dtype=torch.bfloat16, device="cuda:0")
handle.model.other = _matrix(32, 64, dtype=torch.bfloat16, device="cuda:0")
names = list(dict(handle.model.named_parameters()))
lora = _edit_workspace(handle, names, adapter_rank=8)["cuda:0"]
retained = sum(2 * 8 * sum(p.shape) for p in handle.model.parameters())
conversions = sorted((p.numel() * 4 for p in handle.model.parameters()), reverse=True)
vectors = max(16 * 8 * sum(p.shape) for p in handle.model.parameters())
merge = 3 * max(p.numel() * 2 for p in handle.model.parameters())
assert lora == retained + max(sum(conversions[:2]) + vectors, merge) + 1024**2
def test_generic_integer_float_replacements_survive_later_projection_groups():
with FakeTensorMode():
model = torch.nn.Module()
for name in ("first", "second"):
module = torch.nn.Module()
module.in_features = 64
module.out_features = 128
module.weight = torch.nn.Parameter(
torch.empty(128, 64, dtype=torch.uint8, device="cuda:0"),
requires_grad=False,
)
setattr(model, name, module)
handle = SimpleNamespace(model=model)
single = _edit_workspace(handle, ["first.weight"])["cuda:0"]
both = _edit_workspace(handle, ["first.weight", "second.weight"])["cuda:0"]
assert both - single == 128 * 64 * (4 - 1)