Files
OBLITERATUS/tests/conditional/test_cuda_runtime.py
Joseph MaglyandClaude Opus 5.5 5b4291eebe fix(numerics): accumulate bounded norms on the host
frobenius_norm_bounded added each chunk's CUDA result into a CPU
float64 tensor, which raises a device-mismatch RuntimeError for any
weight not on the CPU. Norm capture and restoration run whenever
norm_preserve is on, so every GPU run would have failed.

Accumulate into a Python float instead. That keeps float64
accumulation and works on CUDA and MPS (which has no float64). Widen
chunks straight to contiguous memory so a transposed chunk is not
copied a second time by reshape, and run the reduction under no_grad.

Add a CUDA conditional test that fails on the previous head and covers
the helper and the real capture/restore path on a CUDA layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:32:46 -05:00

112 lines
4.6 KiB
Python

"""Real CUDA and bitsandbytes placement/operation probes."""
from __future__ import annotations
import pytest
import torch
import torch.nn as nn
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
from obliteratus import device
from obliteratus.abliterate import AbliterationPipeline
pytestmark = pytest.mark.gpu
def test_cuda_discovery_dtype_placement_and_operation():
if not torch.cuda.is_available():
pytest.skip("requires a CUDA runner; set ENABLE_CUDA_GATE and attach the cuda label")
assert device.is_cuda()
assert device.get_device("auto") == "cuda"
assert device.default_dtype("cuda") is torch.float16
tensor = torch.arange(16, device="cuda", dtype=torch.float32).reshape(4, 4)
result = tensor @ tensor.T
assert result.device.type == "cuda"
assert torch.isfinite(result).all()
def test_cuda_execution_observes_offloaded_surgery_and_restores_meta_state():
if not torch.cuda.is_available():
pytest.skip("requires a CUDA runner; set ENABLE_CUDA_GATE and attach the cuda label")
module = nn.Module()
module.proj = nn.Linear(4, 4, bias=False)
original = module.proj.weight.detach().clone()
hook = AlignDevicesHook(execution_device="cuda", offload=True)
add_hook_to_module(module.proj, hook)
count = AbliterationPipeline._project_out_advanced(
module,
torch.tensor([[1.0], [0.0], [0.0], [0.0]], device="cuda"),
["proj"],
)
output = module.proj(torch.ones(1, 4, device="cuda"))
expected = original.clone()
expected[:, 0] = 0
assert count == 1
assert output.device.type == "cuda"
assert module.proj.weight.device.type == "meta"
torch.testing.assert_close(hook.weights_map["weight"], expected)
def test_bitsandbytes_quantization_operation():
if not torch.cuda.is_available():
pytest.skip("requires a CUDA runner; set ENABLE_CUDA_GATE and attach the cuda label")
bnb = pytest.importorskip("bitsandbytes", reason="install locked bitsandbytes>=0.46.1")
assert device.supports_bitsandbytes("cuda")
source = torch.randn(16, 16, device="cuda", dtype=torch.float16)
quantized, state = bnb.functional.quantize_4bit(source, quant_type="nf4")
restored = bnb.functional.dequantize_4bit(quantized, state)
assert restored.device.type == "cuda"
assert restored.shape == source.shape
assert torch.isfinite(restored).all()
@pytest.mark.parametrize("offload", ["resident", "cpu", "disk"])
def test_cuda_snapshot_restores_authoritative_weights(tmp_path, offload):
if not torch.cuda.is_available():
pytest.skip("requires a leased CUDA runner")
from accelerate import cpu_offload, disk_offload
from obliteratus.models.snapshot import DiskSnapshot
from obliteratus.models.offload_surgery import zero_logical_parameters
model = nn.Sequential(nn.Linear(4, 4), nn.Linear(4, 2)).eval().to("cuda")
inputs = torch.ones(1, 4, device="cuda")
expected = model(inputs).detach().cpu()
if offload == "cpu":
cpu_offload(model, execution_device=torch.device("cuda"))
elif offload == "disk":
disk_offload(model, str(tmp_path / "weights"), execution_device=torch.device("cuda"))
with DiskSnapshot(model, directory=tmp_path) as snapshot:
for _ in range(2):
zero_logical_parameters(model, [(model[1], "weight", Ellipsis)])
assert not torch.allclose(model(inputs).cpu(), expected)
snapshot.restore(model)
torch.testing.assert_close(model(inputs).cpu(), expected)
def test_bounded_norm_capture_and_restore_on_cuda_weights():
if not torch.cuda.is_available():
pytest.skip("requires a CUDA runner; set ENABLE_CUDA_GATE and attach the cuda label")
from obliteratus.analysis.numerical_contracts import frobenius_norm_bounded
torch.manual_seed(0)
weight = torch.randn(512, 96, device="cuda", dtype=torch.float16)
reference = weight.double().norm().item()
for view in (weight, weight.T, weight[:, ::3]):
expected = view.double().norm().item()
assert frobenius_norm_bounded(view, max_chunk_bytes=4096) == pytest.approx(expected, rel=1e-5)
layer = nn.Module()
layer.head = nn.Linear(96, 512, bias=False).to("cuda", torch.float16)
with torch.no_grad():
layer.head.weight.copy_(weight)
saved = AbliterationPipeline._capture_layer_weight_norms(layer)
assert saved["head.weight"] == pytest.approx(reference, rel=1e-5)
AbliterationPipeline._restore_layer_weight_norms(layer, {"head.weight": reference * 1.05})
assert layer.head.weight.device.type == "cuda"
assert layer.head.weight.double().norm().item() == pytest.approx(reference * 1.05, rel=1e-3)