Files
OBLITERATUS/tests/test_projection_chunked.py
Joseph Magly be5da03024 fix(numerical): project large weights in bounded slices
project_weight_against_direction widened the whole weight to the compute
dtype and then materialised the outer product and the projected result as
further full-size temporaries. For a 128256 x 4096 LM head that is about
5.9 GiB of extra peak memory, which made 8B bf16 abliteration run out of
memory on a 24 GB GPU after every transformer layer had been edited.

Weights whose compute-dtype copy exceeds PROJECTION_CHUNK_BYTES (256 MiB)
are now projected slice by slice along the coefficient axis: one pass
collects coefficients and the norm, the second writes each projected
slice into a weight-dtype output. Each element uses the same formula and
compute dtype; the norm-preserve ratio is derived before the write pass,
and non-finite and degenerate inputs keep their fail-closed results.
Smaller weights keep the dense path unchanged.

On an RTX 4090 the LM-head projection's extra peak falls from 5.88 GiB to
1.73 GiB, with outputs within one bf16 rounding step of the dense path.

Fixes #535
2026-11-18 20:20:16 -05:00

140 lines
5.6 KiB
Python

"""Chunked large-weight projection matches the dense formula with bounded scratch."""
from __future__ import annotations
import pytest
import torch
from obliteratus.analysis import numerical_contracts as nc
from obliteratus.analysis.numerical_contracts import project_weight_against_direction
pytestmark = pytest.mark.cpu
DENSE = 1 << 40
def _pair(weight, direction, **options):
dense = project_weight_against_direction(weight, direction, max_chunk_bytes=DENSE, **options)
chunked = project_weight_against_direction(weight, direction, max_chunk_bytes=64, **options)
return dense, chunked
def _close(dense, chunked, dtype):
assert chunked.projected == dense.projected
assert chunked.layout == dense.layout
assert chunked.weight.dtype == dense.weight.dtype
assert chunked.weight.shape == dense.weight.shape
atol = {torch.float64: 1e-12, torch.float32: 1e-5}.get(dtype, 1e-2)
torch.testing.assert_close(chunked.weight.float(), dense.weight.float(), atol=atol, rtol=atol)
assert chunked.coefficient_norm_sq == pytest.approx(dense.coefficient_norm_sq, rel=1e-5, abs=1e-6)
@pytest.mark.parametrize("dtype", [torch.float64, torch.float32, torch.bfloat16])
@pytest.mark.parametrize("layout", ["standard", "transposed"])
@pytest.mark.parametrize("norm_preserve", [False, True])
@pytest.mark.parametrize("fraction", [1.0, 0.4])
@pytest.mark.parametrize("regularization", [0.0, 0.3])
def test_chunked_projection_matches_dense(dtype, layout, norm_preserve, fraction, regularization):
gen = torch.Generator().manual_seed(7)
hidden = 12
weight = torch.randn(37, hidden, generator=gen, dtype=torch.float64)
if layout == "transposed":
weight = weight.T.contiguous()
weight = weight.to(dtype)
direction = torch.randn(hidden, generator=gen, dtype=torch.float64) * 3
axis = 1 if layout == "standard" else 0
dense, chunked = _pair(
weight, direction, direction_axis=axis, norm_preserve=norm_preserve,
projection_row_fraction=fraction, regularization=regularization,
)
assert dense.projected
_close(dense, chunked, dtype)
def test_chunked_projection_handles_strided_transposed_views():
gen = torch.Generator().manual_seed(3)
base = torch.randn(9, 20, generator=gen, dtype=torch.float32)
view = base.T # non-contiguous (20, 9); direction over rows of the view
direction = torch.randn(20, generator=gen)
dense, chunked = _pair(view, direction, direction_axis=0)
_close(dense, chunked, torch.float32)
assert chunked.weight.is_contiguous()
assert torch.equal(base, base.clone()) # input untouched
def test_chunked_full_removal_is_orthogonal_to_direction():
gen = torch.Generator().manual_seed(11)
weight = torch.randn(50, 8, generator=gen, dtype=torch.float64)
direction = torch.randn(8, generator=gen, dtype=torch.float64)
result = project_weight_against_direction(weight, direction, max_chunk_bytes=64)
unit = direction / direction.norm()
assert (result.weight @ unit).abs().max() < 1e-12
@pytest.mark.parametrize("layout", ["standard", "transposed"])
def test_chunked_non_finite_coefficients_fail_closed(layout):
big = torch.full((6, 4), 3.0e38, dtype=torch.float32)
weight = big if layout == "standard" else big.T.contiguous()
direction = torch.ones(4)
result = project_weight_against_direction(weight, direction, max_chunk_bytes=8)
assert not result.projected
assert result.layout == layout
assert torch.equal(result.weight, weight)
assert result.weight.data_ptr() != weight.data_ptr()
@pytest.mark.parametrize(
"weight, direction",
[
(torch.tensor([[float("nan"), 1.0], [0.0, 1.0]]), torch.tensor([1.0, 0.0])),
(torch.ones(4, 2), torch.zeros(2)),
(torch.ones(4, 3), torch.ones(2)),
],
ids=["non-finite-weight", "zero-direction", "unsupported-layout"],
)
def test_chunked_noops_return_metadata_clone(weight, direction):
result = project_weight_against_direction(weight, direction, max_chunk_bytes=4)
assert not result.projected
assert result.layout is None
assert torch.equal(torch.nan_to_num(result.weight), torch.nan_to_num(weight))
assert result.weight.data_ptr() != weight.data_ptr()
@pytest.mark.parametrize("limit", [0, -1, 1.5, True])
def test_invalid_chunk_limit_rejected(limit):
with pytest.raises(ValueError, match="max_chunk_bytes"):
project_weight_against_direction(torch.ones(2, 2), torch.ones(2), max_chunk_bytes=limit)
def test_default_threshold_routes_only_large_weights(monkeypatch):
calls = []
real = nc._project_weight_chunked
def spy(*args, **kwargs):
calls.append(kwargs["max_chunk_bytes"])
return real(*args, **kwargs)
monkeypatch.setattr(nc, "_project_weight_chunked", spy)
monkeypatch.setattr(nc, "PROJECTION_CHUNK_BYTES", 4 * 16 * 8)
project_weight_against_direction(torch.ones(16, 8), torch.ones(8)) # exactly at limit: dense
assert calls == []
project_weight_against_direction(torch.ones(17, 8), torch.ones(8))
assert calls == [4 * 16 * 8]
def test_chunk_slices_respect_byte_limit(monkeypatch):
seen = []
real_to = torch.Tensor.to
def tracking_to(self, *args, **kwargs):
out = real_to(self, *args, **kwargs)
if out.dtype == torch.float32 and self.dtype == torch.bfloat16 and out.dim() == 2:
seen.append(out.numel() * out.element_size())
return out
monkeypatch.setattr(torch.Tensor, "to", tracking_to)
weight = torch.ones(100, 10, dtype=torch.bfloat16)
project_weight_against_direction(weight, torch.ones(10), max_chunk_bytes=160)
assert seen and max(seen) <= 160
assert len(seen) == 2 * 25 # coefficient pass + projection pass, 4 rows each