Files
OBLITERATUS/tests/conditional/test_cuda_runtime.py
T
Joseph Magly 21973c780f fix(checkpoint): require validated restore points throughout study lifecycle
(cherry picked from commit 72346504a49f2ceec4102f7f393159e3a6de0687)
2026-10-05 17:54:30 -04:00

88 lines
3.5 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)