mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Propagate memory exhaustion through the public cleanup boundary and fail partial projection passes before verification or export. Exercise attention, FFN, SAE and module lookup failures with real tiny-model projections. Update the rollback CPU-allocation guard to follow the bounded snapshot helper; the old Tensor.to spy failed on unchanged main. Fixes #389
217 lines
8.8 KiB
Python
217 lines
8.8 KiB
Python
"""Fail-closed guards of transactional offload surgery on CPU-only Accelerate hooks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
import torch
|
|
import torch.nn as nn
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
|
|
import obliteratus.models.offload_surgery as offload_surgery
|
|
from obliteratus.models.offload_surgery import (
|
|
OffloadSurgeryError,
|
|
UnsupportedOffloadLayoutError,
|
|
UnsupportedOffloadedQuantizationError,
|
|
logical_module_device,
|
|
resolve_logical_parameter,
|
|
zero_logical_parameters,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_logical_commit_validates_final_storage_dtype_before_writing(offloaded):
|
|
module = nn.Linear(1, 1, bias=False, dtype=torch.float16)
|
|
if offloaded:
|
|
add_hook_to_module(module, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
transaction = resolve_logical_parameter(module)
|
|
before = transaction.tensor.clone()
|
|
with pytest.raises(ValueError, match="Nonfinite logical weight.*torch.float16"):
|
|
transaction.commit(torch.tensor([[70000.]]))
|
|
assert torch.equal(resolve_logical_parameter(module).tensor, before)
|
|
assert module.weight.is_meta == offloaded
|
|
|
|
|
|
def test_logical_commit_checks_current_resident_dtype():
|
|
module = nn.Linear(1, 1, bias=False)
|
|
transaction = resolve_logical_parameter(module)
|
|
module.half()
|
|
before = module.weight.detach().clone()
|
|
with pytest.raises(ValueError, match="Nonfinite logical weight.*torch.float16"):
|
|
transaction.commit(torch.tensor([[70000.]]))
|
|
assert torch.equal(module.weight, before)
|
|
|
|
|
|
def test_short_accelerate_release_is_zero_padded(monkeypatch):
|
|
monkeypatch.setattr(offload_surgery, "version", lambda name: "1.0+local")
|
|
assert offload_surgery._accelerate_release() == (1, 0, 0)
|
|
|
|
|
|
def test_write_target_rejects_non_tensor_backing_and_tracks_loader_keys():
|
|
with pytest.raises(UnsupportedOffloadLayoutError, match="is not a tensor"):
|
|
offload_surgery._WriteTarget({"w": "not a tensor"}, "w").read()
|
|
|
|
class Loader:
|
|
state_dict = {}
|
|
all_keys = ["existing"]
|
|
|
|
loader = Loader()
|
|
target = offload_surgery._WriteTarget(loader, "w", loader=True)
|
|
value = torch.ones(2)
|
|
target.write(value)
|
|
target.write(value)
|
|
assert loader.state_dict["w"] is value
|
|
assert loader.all_keys == ["existing", "w"]
|
|
|
|
|
|
def test_meta_tensors_never_share_storage():
|
|
meta = torch.empty(2, device="meta")
|
|
assert offload_surgery._same_storage(meta, meta) is True # identical object
|
|
assert offload_surgery._same_storage(meta, torch.empty(2, device="meta")) is False
|
|
assert offload_surgery._same_storage(torch.zeros(2), meta) is False
|
|
|
|
|
|
def test_missing_parameter_is_rejected():
|
|
with pytest.raises(OffloadSurgeryError, match="no tensor parameter named 'gate'"):
|
|
resolve_logical_parameter(nn.Linear(2, 2), "gate")
|
|
|
|
|
|
def test_resident_commit_rejects_identity_shape_and_dtype_changes():
|
|
module = nn.Linear(3, 2, bias=False)
|
|
transaction = resolve_logical_parameter(module)
|
|
with pytest.raises(OffloadSurgeryError, match="changed shape"):
|
|
transaction.commit(torch.zeros(3, 3))
|
|
with pytest.raises(UnsupportedOffloadedQuantizationError, match="floating-point"):
|
|
transaction.commit(torch.zeros(2, 3, dtype=torch.int8))
|
|
original = module.weight
|
|
module.weight = nn.Parameter(torch.zeros(2, 3))
|
|
with pytest.raises(OffloadSurgeryError, match="identity changed during logical surgery"):
|
|
transaction.commit(torch.zeros(2, 3))
|
|
module.weight = original
|
|
transaction.rollback() # Nothing committed: a no-op.
|
|
assert module.weight is original
|
|
|
|
|
|
def test_resident_commit_and_rollback_restore_values():
|
|
module = nn.Linear(3, 2, bias=False)
|
|
before = module.weight.detach().clone()
|
|
transaction = resolve_logical_parameter(module)
|
|
transaction.commit(torch.zeros(2, 3))
|
|
assert torch.count_nonzero(module.weight) == 0
|
|
transaction.rollback()
|
|
torch.testing.assert_close(module.weight.detach(), before)
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
|
|
def test_resident_rollback_snapshots_explicitly_copy_to_cpu(monkeypatch, dtype):
|
|
module = nn.Linear(3, 2, bias=False).to(dtype=dtype)
|
|
before = module.weight.detach().clone()
|
|
transaction = resolve_logical_parameter(module)
|
|
original_empty = torch.empty
|
|
snapshots = []
|
|
|
|
def empty(*args, **kwargs):
|
|
# The bounded snapshot helper allocates its CPU destination before
|
|
# copying. Requiring that explicit destination catches a device-local
|
|
# clone regression even though this test uses only CPU tensors.
|
|
assert kwargs.get("device") == "cpu"
|
|
result = original_empty(*args, **kwargs)
|
|
snapshots.append(result)
|
|
return result
|
|
|
|
monkeypatch.setattr(torch, "empty", empty)
|
|
transaction.commit(torch.zeros_like(before))
|
|
assert len(snapshots) == 1
|
|
assert transaction._rollback_values[0][1] is snapshots[0]
|
|
assert snapshots[0].device.type == "cpu"
|
|
assert snapshots[0].dtype == dtype
|
|
assert snapshots[0].data_ptr() != module.weight.data_ptr()
|
|
assert torch.equal(snapshots[0], before)
|
|
transaction.rollback()
|
|
assert torch.equal(module.weight, before)
|
|
assert not transaction._rollback_values
|
|
|
|
|
|
@pytest.mark.parametrize("rollback_fails", [False, True])
|
|
def test_resident_partial_copy_failure_rolls_back(monkeypatch, rollback_fails):
|
|
module = nn.Linear(3, 2, bias=False)
|
|
before = module.weight.detach().clone()
|
|
transaction = resolve_logical_parameter(module)
|
|
original_copy = torch.Tensor.copy_
|
|
calls = []
|
|
|
|
def copy(tensor, value, *args, **kwargs):
|
|
if tensor.is_set_to(module.weight):
|
|
calls.append(value)
|
|
if len(calls) == 1:
|
|
tensor.zero_()
|
|
raise RuntimeError("partial copy failed")
|
|
if rollback_fails:
|
|
raise RuntimeError("restore failed")
|
|
return original_copy(tensor, value, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(torch.Tensor, "copy_", copy)
|
|
error = offload_surgery.OffloadRollbackError if rollback_fails else RuntimeError
|
|
with pytest.raises(error, match="resident rollback failed" if rollback_fails else "partial copy"):
|
|
transaction.commit(torch.ones_like(before))
|
|
assert len(calls) == 2
|
|
assert not transaction._committed
|
|
if not rollback_fails:
|
|
assert torch.equal(module.weight, before)
|
|
|
|
|
|
def test_rollback_refuses_after_parameter_replacement():
|
|
module = nn.Linear(3, 2, bias=False)
|
|
transaction = resolve_logical_parameter(module)
|
|
transaction.commit(torch.zeros(2, 3))
|
|
module.weight = nn.Parameter(torch.ones(2, 3))
|
|
with pytest.raises(OffloadSurgeryError, match="cannot roll back changed weight identity"):
|
|
transaction.rollback()
|
|
|
|
|
|
def test_disk_restore_requires_a_writable_offload_loader():
|
|
transaction = resolve_logical_parameter(nn.Linear(3, 2, bias=False))
|
|
assert transaction.supports_disk_restore is False
|
|
with pytest.raises(UnsupportedOffloadLayoutError, match="one writable offload loader"):
|
|
transaction.restore_to_disk(torch.zeros(2, 3))
|
|
|
|
|
|
def test_logical_device_requires_a_parameter():
|
|
with pytest.raises(OffloadSurgeryError, match="no parameters"):
|
|
logical_module_device(nn.ReLU())
|
|
|
|
|
|
@pytest.mark.parametrize("ties, edits_for, error, message", [
|
|
([], lambda m: [(m, "weight", slice(0, 1))], UnsupportedOffloadLayoutError,
|
|
"unsupported model tie metadata"),
|
|
({"weight": "ghost.weight"}, lambda m: [(m, "weight", slice(0, 1))],
|
|
UnsupportedOffloadLayoutError, "stale model tie metadata"),
|
|
({}, lambda m: [(m, "gate", slice(0, 1))], OffloadSurgeryError, "missing tensor parameter"),
|
|
({}, lambda m: [(m, "weight", slice(0, 0))], OffloadSurgeryError, "selection is empty"),
|
|
])
|
|
def test_zero_ablation_validates_before_any_write(ties, edits_for, error, message):
|
|
module = nn.Linear(3, 2, bias=False)
|
|
module.all_tied_weights_keys = ties
|
|
before = module.weight.detach().clone()
|
|
with pytest.raises(error, match=message):
|
|
zero_logical_parameters(module, edits_for(module))
|
|
torch.testing.assert_close(module.weight.detach(), before)
|
|
|
|
|
|
def test_zero_ablation_shares_resident_and_offloaded_tied_storage():
|
|
"""A resident alias joins the offloaded owner's transaction, not the reverse."""
|
|
root = nn.Module()
|
|
root.resident = nn.Linear(3, 2, bias=False)
|
|
root.offloaded = nn.Linear(3, 2, bias=False)
|
|
hook = AlignDevicesHook(execution_device="cpu", offload=True)
|
|
add_hook_to_module(root.offloaded, hook)
|
|
root.resident.weight = nn.Parameter(hook.weights_map["weight"])
|
|
root.all_tied_weights_keys = {"offloaded.weight": "resident.weight"}
|
|
|
|
zero_logical_parameters(root, [(root.resident, "weight", (slice(0, 1),))])
|
|
|
|
assert torch.count_nonzero(hook.weights_map["weight"][0]) == 0
|
|
assert torch.count_nonzero(root.resident.weight[0]) == 0
|
|
assert root.offloaded.weight.device.type == "meta"
|