Files
OBLITERATUS/tests/test_numerical_contracts.py
0xreconlionandClaude Opus 5 2520dc5818 fix(numerics): bound norm-capture scratch to a chunk budget
Capturing and restoring layer weight norms called ``data.float().norm()``,
which allocates a float32 copy of the entire weight to produce one scalar.
The copy is unconditional, so no offload plan, device map, or allocator
setting avoids it, and for a large-vocabulary LM head it is gigabytes.

Add ``frobenius_norm_bounded`` beside the module's other bounded-scratch
contracts and route the four norm reductions in ``_capture_layer_weight_norms``
and ``_restore_layer_weight_norms`` through it. It splits the largest axis
until a chunk's float32 scratch fits the budget, accumulates sums of squares
in float64, and takes one square root. Storage already at least as wide as
float32 reduces in place, so widening never allocates for it.

Measured on a [152064, 5120] float16 weight, reducing scratch from a
multiple of the weight to a multiple of the budget:

  budget    4 MB   16 MB   64 MB
  scratch  12 MB   38 MB   96 MB

The whole-weight promotion cost 2971 MB for the same reduction. Scratch is
now a function of the budget, not of the weight.

The folded post-norm branch is left alone: folding materializes a full
writer copy by construction, so bounding its norm would require changing
the fold itself.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-11-09 17:08:40 -05:00

312 lines
12 KiB
Python

"""Mutation-focused tests for pure numerical input contracts."""
from __future__ import annotations
import pytest
import torch
from obliteratus.analysis.numerical_contracts import (
NonfiniteWeightError,
validated_weight_update,
validate_whitened_parameters,
validate_whitened_request,
)
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("bad_value", [float("nan"), float("inf"), -float("inf")])
def test_weight_validation_rejects_nonfinite_strided_values(dtype, bad_value):
source = torch.ones(4, 8, dtype=dtype)
source[2, 4] = bad_value
value = source[:, ::2].T
before = source.clone()
with pytest.raises(NonfiniteWeightError, match="Nonfinite regression"):
validated_weight_update(value, value, context="regression")
torch.testing.assert_close(source, before, equal_nan=True)
@pytest.mark.parametrize("sign", [-1, 1])
def test_weight_validation_checks_final_storage_dtype(sign):
storage = torch.ones(2, dtype=torch.float16)
value = torch.tensor([sign * 70000., 1.])
with pytest.raises(NonfiniteWeightError, match="torch.float16"):
validated_weight_update(value, storage)
assert torch.equal(storage, torch.ones_like(storage))
value[0] = sign * 65504.
updated = validated_weight_update(value, storage)
assert updated.dtype == storage.dtype
assert torch.equal(updated.float(), value)
def test_weight_validation_accepts_empty_updates():
value = torch.empty(0, 3)
assert validated_weight_update(value, value) is value
class _CudaStorage(torch.Tensor):
"""CPU storage with a CUDA device label for transfer-planning tests."""
@staticmethod
def __new__(cls, value):
return torch.Tensor._make_subclass(cls, value, False)
@property
def device(self):
return torch.device('cuda:5')
@pytest.mark.parametrize('layout', ['dense', 'transpose', 'slice', 'scalar', 'empty'])
@pytest.mark.parametrize('dtype', [torch.bfloat16, torch.float32, torch.int64])
@pytest.mark.parametrize('direction', ['to_device', 'to_cpu'])
def test_bounded_copy_preserves_values_and_storage_with_limited_transfer_scratch(
monkeypatch, layout, dtype, direction,
):
from obliteratus.analysis.numerical_contracts import copy_tensor_bounded_
source = torch.arange(120, dtype=dtype).reshape(3, 5, 8)
storage = torch.full_like(source, -1)
if layout == 'transpose':
source, storage = source.transpose(0, 2), storage.transpose(0, 2)
elif layout == 'slice':
source, storage = source[:, :, ::2], storage[:, :, ::2]
elif layout == 'scalar':
source, storage = source[0, 0, 0], storage[0, 0, 0]
elif layout == 'empty':
source, storage = source[:0], storage[:0]
if direction == 'to_cpu':
source, target = _CudaStorage(source), storage
else:
target = _CudaStorage(storage)
pointer = target.data_ptr()
copy = torch.Tensor.copy_
transfers = []
def bounded_transfer(destination, value, **kwargs):
size = destination.numel() * destination.element_size()
if not (destination.is_contiguous() and value.is_contiguous()):
assert size <= 64, 'strided transfer exceeds available scratch'
transfers.append(size)
return copy(destination.as_subclass(torch.Tensor), value.as_subclass(torch.Tensor), **kwargs)
monkeypatch.setattr(torch.Tensor, 'copy_', bounded_transfer)
assert copy_tensor_bounded_(target, source, max_chunk_bytes=64) is target
assert torch.equal(target, source)
assert target.data_ptr() == pointer
assert sum(transfers) == source.numel() * source.element_size()
if layout in ('transpose', 'slice'):
assert len(transfers) > 1
else:
assert len(transfers) == 1
@pytest.mark.parametrize('chunk_bytes', [0, -1, 1, True, 1.5])
def test_bounded_copy_rejects_invalid_byte_limits_before_writing(chunk_bytes):
from obliteratus.analysis.numerical_contracts import copy_tensor_bounded_
target, source = torch.zeros(3), torch.ones(3)
with pytest.raises(ValueError, match='chunk'):
copy_tensor_bounded_(target, source, max_chunk_bytes=chunk_bytes)
assert not target.count_nonzero()
@pytest.mark.parametrize('mismatch', ['shape', 'dtype', 'source_meta', 'target_meta'])
def test_bounded_copy_rejects_incompatible_tensors_before_writing(mismatch):
from obliteratus.analysis.numerical_contracts import copy_tensor_bounded_
target, source = torch.zeros(3), torch.ones(3)
if mismatch == 'shape':
source = source[:1]
elif mismatch == 'dtype':
source = source.double()
elif mismatch == 'source_meta':
source = source.to('meta')
else:
target = target.to('meta')
with pytest.raises(ValueError, match='shape|dtype|materialized'):
copy_tensor_bounded_(target, source)
if not target.is_meta:
assert not target.count_nonzero()
def test_same_device_strided_copy_needs_no_transfer_slices(monkeypatch):
from obliteratus.analysis.numerical_contracts import copy_tensor_bounded_
target = torch.zeros(10, 10).T
source = torch.arange(100.).reshape(10, 10).T
monkeypatch.setattr(torch.Tensor, 'narrow', lambda *_args: pytest.fail('unneeded slicing'))
copy_tensor_bounded_(target, source, max_chunk_bytes=4)
assert torch.equal(target, source)
@pytest.mark.parametrize('device', ['cpu', 'cuda'])
@pytest.mark.parametrize('dtype', [torch.bfloat16, torch.float32, torch.int64])
def test_cpu_clone_is_detached_contiguous_and_independent(monkeypatch, device, dtype):
from obliteratus.analysis.numerical_contracts import clone_tensor_to_cpu
source = torch.arange(120, dtype=dtype).reshape(3, 5, 8).transpose(0, 2)
if device == 'cuda':
source = _CudaStorage(source)
if source.is_floating_point():
source.requires_grad_(True)
monkeypatch.setattr(torch.Tensor, 'to', lambda *_args, **_kwargs: pytest.fail('unbounded transfer'))
monkeypatch.setattr(torch.Tensor, 'cpu', lambda *_args, **_kwargs: pytest.fail('unbounded transfer'))
snapshot = clone_tensor_to_cpu(source)
assert snapshot.device.type == 'cpu' and snapshot.is_contiguous()
assert not snapshot.requires_grad and snapshot.dtype == dtype
assert snapshot.data_ptr() != source.data_ptr()
assert torch.equal(snapshot, source)
snapshot.fill_(-1)
assert source[0, 0, 0] == 0
def test_cpu_clone_rejects_meta_before_allocating(monkeypatch):
from obliteratus.analysis.numerical_contracts import clone_tensor_to_cpu
source = torch.empty(2, device='meta')
monkeypatch.setattr(torch, 'empty', lambda *_args, **_kwargs: pytest.fail('allocated before validation'))
with pytest.raises(ValueError, match='materialized'):
clone_tensor_to_cpu(source)
@pytest.mark.parametrize("regularization_eps", [0, -1e-4, float("nan"), float("inf"), True, "1e-4"])
def test_regularization_must_be_a_finite_positive_number(regularization_eps):
with pytest.raises(ValueError, match="finite positive"):
validate_whitened_parameters(regularization_eps, 0.01)
@pytest.mark.parametrize(
"min_variance_ratio",
[-0.1, 1.0, float("nan"), float("inf"), True, "0.01"],
)
def test_variance_ratio_must_be_in_the_half_open_unit_interval(min_variance_ratio):
with pytest.raises(ValueError, match=r"interval \[0, 1\)"):
validate_whitened_parameters(1e-4, min_variance_ratio)
@pytest.mark.parametrize(
("regularization_eps", "min_variance_ratio", "expected"),
[(1, 0, (1.0, 0.0)), (1e-4, 0.999, (1e-4, 0.999))],
)
def test_valid_parameters_are_normalized_to_floats(
regularization_eps,
min_variance_ratio,
expected,
):
assert validate_whitened_parameters(regularization_eps, min_variance_ratio) == expected
@pytest.mark.parametrize(
("harmful_count", "harmless_count", "message"),
[(0, 0, "both be non-empty"), (1, 0, "both be non-empty"), (0, 1, "both be non-empty")],
)
def test_activation_sets_must_both_be_nonempty(harmful_count, harmless_count, message):
with pytest.raises(ValueError, match=message):
validate_whitened_request(harmful_count, harmless_count, 1)
@pytest.mark.parametrize(("harmful_count", "harmless_count"), [(1, 2), (3, 1)])
def test_activation_sets_must_have_equal_sample_counts(harmful_count, harmless_count):
with pytest.raises(ValueError, match=f"got {harmful_count} and {harmless_count}"):
validate_whitened_request(harmful_count, harmless_count, 1)
@pytest.mark.parametrize("n_directions", [-1, 0, 1.5, True, "1"])
def test_direction_count_must_be_a_positive_integer(n_directions):
with pytest.raises(ValueError, match="positive integer"):
validate_whitened_request(2, 2, n_directions)
@pytest.mark.parametrize("n_directions", [-1, 0])
def test_non_positive_direction_count_uses_public_error_message(n_directions):
with pytest.raises(ValueError) as excinfo:
validate_whitened_request(2, 2, n_directions)
assert str(excinfo.value) == "n_directions must be a positive integer"
@pytest.mark.parametrize("n_directions", [1, 2, 100])
def test_valid_direction_count_is_returned(n_directions):
assert validate_whitened_request(2, 2, n_directions) == n_directions
def _record_float32_promotions(monkeypatch):
"""Record the byte size of every float32 promotion a reduction performs."""
promotions = []
original_to, original_float = torch.Tensor.to, torch.Tensor.float
def tracked_to(self, *args, **kwargs):
result = original_to(self, *args, **kwargs)
if result.dtype == torch.float32 and result is not self:
promotions.append(result.numel() * result.element_size())
return result
def tracked_float(self, *args, **kwargs):
result = original_float(self, *args, **kwargs)
if result is not self:
promotions.append(result.numel() * result.element_size())
return result
monkeypatch.setattr(torch.Tensor, 'to', tracked_to)
monkeypatch.setattr(torch.Tensor, 'float', tracked_float)
return promotions
@pytest.mark.parametrize('layout', ['dense', 'transpose', 'slice', 'scalar', 'empty', 'expert'])
@pytest.mark.parametrize(
'dtype', [torch.float16, torch.bfloat16, torch.float32, torch.float64, torch.int64],
)
def test_bounded_norm_matches_reference_with_limited_promotion_scratch(monkeypatch, layout, dtype):
from obliteratus.analysis.numerical_contracts import frobenius_norm_bounded
value = torch.arange(120, dtype=dtype).reshape(3, 5, 8)
if layout == 'transpose':
value = value.transpose(0, 2)
elif layout == 'slice':
value = value[:, :, ::2]
elif layout == 'scalar':
value = value[0, 0, 1]
elif layout == 'empty':
value = value[:0]
elif layout == 'expert':
value = value[1]
reference = value.float().norm().item()
before = value.clone()
promotions = _record_float32_promotions(monkeypatch)
result = frobenius_norm_bounded(value, max_chunk_bytes=64)
# Whole-tensor promotion is the defect: a float32 copy of the entire weight
# allocated only to produce one scalar. Every promotion must fit the budget.
assert all(size <= 64 for size in promotions), f'unbounded promotion: {promotions}'
assert result == pytest.approx(reference, rel=1e-4, abs=1e-6)
assert torch.equal(value, before)
def test_bounded_norm_splits_large_reductions_but_not_small_ones(monkeypatch):
from obliteratus.analysis.numerical_contracts import frobenius_norm_bounded
promotions = _record_float32_promotions(monkeypatch)
frobenius_norm_bounded(torch.ones(4, dtype=torch.float16), max_chunk_bytes=64)
assert len(promotions) == 1
promotions.clear()
frobenius_norm_bounded(torch.ones(4096, dtype=torch.float16), max_chunk_bytes=64)
assert len(promotions) > 1
# Scratch is bounded by the budget, not by the size of the input.
assert max(promotions) <= 64
@pytest.mark.parametrize('chunk_bytes', [0, -1, 1, True, 1.5])
def test_bounded_norm_rejects_invalid_byte_limits(chunk_bytes):
from obliteratus.analysis.numerical_contracts import frobenius_norm_bounded
with pytest.raises(ValueError, match='chunk'):
frobenius_norm_bounded(torch.ones(3), max_chunk_bytes=chunk_bytes)
def test_bounded_norm_rejects_unmaterialized_tensors():
from obliteratus.analysis.numerical_contracts import frobenius_norm_bounded
with pytest.raises(ValueError, match='materialized'):
frobenius_norm_bounded(torch.ones(3).to('meta'))