mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
482 lines
21 KiB
Python
482 lines
21 KiB
Python
"""Outcome tests for bounded logical restore points."""
|
|
import pytest
|
|
import torch
|
|
from accelerate import cpu_offload, disk_offload
|
|
from obliteratus.models.snapshot import (
|
|
DEFAULT_SNAPSHOT_MAX_BYTES, DiskSnapshot, SnapshotError, plan_snapshot,
|
|
)
|
|
|
|
|
|
def test_large_snapshot_requires_explicit_cap_or_disable_reason():
|
|
total, largest, count = 145_000_000_000, 2_500_000_000, 964
|
|
refused = plan_snapshot(total, largest, count)
|
|
assert refused['status'] == 'refused'
|
|
assert refused['restore_available'] is False
|
|
assert refused['max_bytes'] == DEFAULT_SNAPSHOT_MAX_BYTES
|
|
assert str(DEFAULT_SNAPSHOT_MAX_BYTES) in refused['reason']
|
|
assert 'larger admitted disk cap' in refused['reason']
|
|
with pytest.raises(ValueError, match='explicit reason'):
|
|
plan_snapshot(total, largest, count, enabled=False)
|
|
disabled = plan_snapshot(
|
|
total, largest, count, enabled=False,
|
|
disabled_reason='independent arms reload the immutable source; rollback is unavailable',
|
|
)
|
|
assert disabled['status'] == 'disabled_with_reason'
|
|
assert disabled['enabled'] is False and disabled['restore_available'] is False
|
|
assert 'rollback is unavailable' in disabled['reason']
|
|
|
|
|
|
def test_large_disk_snapshot_admits_repeated_restore_backing_and_host_staging():
|
|
total, largest, count = 145_000_000_000, 2_500_000_000, 964
|
|
policy = plan_snapshot(
|
|
total, largest, count, disk_restore_bytes=total, max_bytes=500_000_000_000,
|
|
available_host=32_000_000_000, available_disk=500_000_000_000,
|
|
)
|
|
assert policy['status'] == 'disk_backed' and policy['restore_available'] is True
|
|
assert policy['required_disk_bytes'] == 3 * total + count * 12288
|
|
assert policy['host_peak_bytes'] == largest * 6
|
|
assert policy['admitted_host_bytes'] == 16_000_000_000
|
|
assert policy['runtime_check_required'] is False
|
|
assert plan_snapshot(
|
|
total, largest, count, overlay_bytes=total, max_bytes=500_000_000_000,
|
|
available_host=32_000_000_000, available_disk=500_000_000_000,
|
|
)['status'] == 'refused'
|
|
|
|
|
|
@pytest.mark.parametrize('overrides', [
|
|
{'total_bytes': -1}, {'largest_tensor_bytes': True}, {'tensor_count': 1.5},
|
|
{'disk_restore_bytes': 101}, {'overlay_bytes': 101}, {'max_bytes': 0},
|
|
{'staging_bytes': False}, {'available_host': -1}, {'available_disk': '100'},
|
|
{'enabled': 0},
|
|
])
|
|
def test_snapshot_planner_rejects_invalid_metadata(overrides):
|
|
values = {'total_bytes': 100, 'largest_tensor_bytes': 10, 'tensor_count': 10, **overrides}
|
|
with pytest.raises(ValueError):
|
|
plan_snapshot(**values)
|
|
|
|
|
|
def test_runtime_snapshot_records_policy_and_cap_failure_reason(tmp_path):
|
|
model = torch.nn.Linear(8, 8, bias=False)
|
|
with pytest.raises(SnapshotError, match='larger admitted disk cap'):
|
|
DiskSnapshot(model, directory=tmp_path, max_bytes=1)
|
|
assert not list(tmp_path.iterdir())
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
assert snapshot.policy['status'] == 'disk_backed'
|
|
assert snapshot.policy['restore_available'] is True
|
|
assert snapshot.policy['required_disk_bytes'] == 256 + 12288
|
|
assert snapshot.policy['host_peak_bytes'] == 256 * 6
|
|
assert snapshot.policy['runtime_check_required'] is False
|
|
|
|
|
|
@pytest.mark.parametrize("use_context", [False, True])
|
|
def test_snapshot_close_records_unavailable_restore_policy(tmp_path, use_context):
|
|
model = torch.nn.Linear(1, 1)
|
|
snapshot = DiskSnapshot(model, directory=tmp_path)
|
|
policy = dict(snapshot.policy)
|
|
assert policy["status"] == "disk_backed"
|
|
assert policy["restore_available"] is True
|
|
|
|
if use_context:
|
|
with snapshot:
|
|
pass
|
|
else:
|
|
snapshot.close()
|
|
|
|
expected = {**policy, "status": "closed", "restore_available": False,
|
|
"reason": "restore point closed"}
|
|
assert not snapshot.path.exists()
|
|
assert snapshot.policy == expected
|
|
with pytest.raises(SnapshotError, match="identity"):
|
|
snapshot.restore(model)
|
|
snapshot.close()
|
|
assert snapshot.policy == expected
|
|
|
|
|
|
def test_nonfinite_snapshot_is_rejected_before_any_restore(tmp_path):
|
|
model = torch.nn.Sequential(*[
|
|
torch.nn.Linear(1, 1, bias=False, dtype=torch.float16) for _ in range(2)
|
|
])
|
|
with torch.no_grad():
|
|
model[0].weight.fill_(2)
|
|
model[1].weight.fill_(float("inf"))
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
with torch.no_grad():
|
|
for module in model:
|
|
module.weight.fill_(1)
|
|
with pytest.raises(ValueError, match="Nonfinite snapshot restore 1.weight"):
|
|
snapshot.restore(model)
|
|
assert snapshot.state == "ready"
|
|
assert all(torch.equal(module.weight, torch.ones_like(module.weight)) for module in model)
|
|
|
|
|
|
class _CudaStorage(torch.Tensor):
|
|
"""CPU storage labelled CUDA to detect snapshot device materialization."""
|
|
|
|
@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('kind', ['kl', 'refinement', 'bayesian', 'disk'])
|
|
def test_snapshot_capture_bounds_strided_readback(tmp_path, monkeypatch, kind):
|
|
from types import SimpleNamespace
|
|
from safetensors.torch import load_file
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.bayesian_execution import GenericBayesianExecution, tensor_sha256
|
|
|
|
pristine = torch.arange(120, dtype=torch.bfloat16).reshape(3, 5, 8).transpose(0, 2)
|
|
model = torch.nn.Module()
|
|
model.weight = torch.nn.Parameter(_CudaStorage(pristine.clone()), requires_grad=False)
|
|
original_to, original_cpu = torch.Tensor.to, torch.Tensor.cpu
|
|
|
|
def guarded_to(value, *args, **kwargs):
|
|
device = kwargs.get('device', args[0] if args and isinstance(args[0], (str, torch.device)) else None)
|
|
if value.device.type == 'cuda' and device is not None and torch.device(device).type == 'cpu':
|
|
pytest.fail('unbounded snapshot readback')
|
|
return original_to(value, *args, **kwargs)
|
|
|
|
def guarded_cpu(value, *args, **kwargs):
|
|
if value.device.type == 'cuda':
|
|
pytest.fail('unbounded snapshot readback')
|
|
return original_cpu(value, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(torch.Tensor, 'to', guarded_to)
|
|
monkeypatch.setattr(torch.Tensor, 'cpu', guarded_cpu)
|
|
if kind == 'kl':
|
|
value = AbliterationPipeline._snapshot_layer_state(model)['weight']
|
|
elif kind == 'refinement':
|
|
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
value = pipeline._refinement_snapshot_factory(('weight',)).tensors['weight']
|
|
elif kind == 'bayesian':
|
|
execution = GenericBayesianExecution.__new__(GenericBayesianExecution)
|
|
execution._released = False
|
|
execution._snapshots = []
|
|
execution._selected = [('weight', model.weight)]
|
|
execution.capture()
|
|
value = execution._snapshots[0][2]
|
|
assert execution._live_sha256() == execution._scope_sha256
|
|
assert tensor_sha256(value) == tensor_sha256(model.weight)
|
|
else:
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
value = load_file(str(snapshot.path / snapshot.records[0][1]))['tensor']
|
|
assert value.device.type == 'cpu' and value.is_contiguous()
|
|
assert value.data_ptr() != model.weight.data_ptr()
|
|
assert torch.equal(value, pristine)
|
|
with torch.no_grad():
|
|
model.weight.fill_(-1)
|
|
assert torch.equal(value, pristine)
|
|
|
|
|
|
@pytest.mark.parametrize('kind', ['kl', 'refinement', 'bayesian', 'disk'])
|
|
@pytest.mark.parametrize('strided', [False, True])
|
|
def test_snapshot_restore_writes_resident_storage_without_device_clone(
|
|
tmp_path, monkeypatch, kind, strided,
|
|
):
|
|
from types import SimpleNamespace
|
|
from obliteratus.abliterate import AbliterationPipeline, _ResidentRefinementSnapshot
|
|
from obliteratus.bayesian_execution import GenericBayesianExecution
|
|
|
|
pristine = torch.arange(120, dtype=torch.bfloat16).reshape(3, 5, 8)
|
|
if strided:
|
|
pristine = pristine.transpose(0, 2)
|
|
model = torch.nn.Module()
|
|
model.weight = torch.nn.Parameter(pristine.clone())
|
|
disk = DiskSnapshot(model, directory=tmp_path) if kind == 'disk' else None
|
|
model.weight = torch.nn.Parameter(_CudaStorage(torch.full_like(pristine, -1)), requires_grad=False)
|
|
pointer = model.weight.data_ptr()
|
|
original_to = torch.Tensor.to
|
|
|
|
def guarded_to(value, *args, **kwargs):
|
|
device = kwargs.get('device')
|
|
if device is not None and torch.device(device).type == 'cuda':
|
|
raise torch.OutOfMemoryError('snapshot device clone exceeds headroom')
|
|
return original_to(value, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(torch.Tensor, 'to', guarded_to)
|
|
try:
|
|
if kind == 'kl':
|
|
AbliterationPipeline._restore_layer_state(model, {'weight': pristine})
|
|
elif kind == 'refinement':
|
|
snapshot = _ResidentRefinementSnapshot(model, ('weight',), {'weight': pristine}, 'test')
|
|
snapshot.restore()
|
|
elif kind == 'bayesian':
|
|
backend = GenericBayesianExecution.__new__(GenericBayesianExecution)
|
|
backend._released = False
|
|
backend.pipeline = SimpleNamespace(handle=SimpleNamespace(model=model))
|
|
backend._snapshots = [('weight', model.weight, pristine)]
|
|
backend._scope_sha256 = backend._snapshot_sha256()
|
|
backend.restore()
|
|
else:
|
|
disk.restore(model)
|
|
assert disk.state == 'ready'
|
|
assert torch.equal(model.weight, pristine)
|
|
assert model.weight.data_ptr() == pointer
|
|
assert not getattr(model, '_obliteratus_surgery_invalid', False)
|
|
finally:
|
|
if disk is not None:
|
|
disk.close()
|
|
|
|
|
|
def test_default_admission_helper_allows_500mb_tensor_estimate_without_allocation(monkeypatch):
|
|
import obliteratus.models.snapshot as backend
|
|
from obliteratus.config import SnapshotConfig
|
|
|
|
# Test the byte estimate only; no large tensor is allocated or serialized.
|
|
required = 500_000_000 * 6
|
|
available = 8_000_000_000
|
|
monkeypatch.setattr(backend, "available_host_bytes", lambda: available)
|
|
assert SnapshotConfig().staging_bytes is None
|
|
assert backend._admitted_staging_bytes(None, available) == 4_000_000_000
|
|
backend._check_staging_admission(required, SnapshotConfig().staging_bytes)
|
|
|
|
# The former 1 GiB default would reject the same six-copy estimate.
|
|
assert required > 1024**3
|
|
with pytest.raises(SnapshotError, match="explicit staging_bytes cap"):
|
|
backend._check_staging_admission(required, 1024**3)
|
|
|
|
|
|
@pytest.mark.parametrize("limits", [{}, {"staging_bytes": None}])
|
|
def test_default_snapshot_admits_tiny_model_at_scaled_host_fraction_boundary(
|
|
tmp_path, monkeypatch, limits,
|
|
):
|
|
import obliteratus.models.snapshot as backend
|
|
|
|
model = torch.nn.Linear(8, 8, bias=False)
|
|
required = model.weight.numel() * model.weight.element_size() * 6
|
|
monkeypatch.setattr(backend, "available_host_bytes", lambda: required * 2)
|
|
before = model.weight.clone()
|
|
with DiskSnapshot(model, directory=tmp_path, **limits) as snapshot:
|
|
with torch.no_grad():
|
|
model.weight.zero_()
|
|
snapshot.restore(model)
|
|
torch.testing.assert_close(model.weight, before)
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("staging_bytes, available, admitted", [
|
|
(1024, 10**12, 1024), # Explicit cap remains binding with abundant memory.
|
|
(10**12, 1024, 1024), # A cap cannot override actual host availability.
|
|
(None, 3071, 1535), # Default fraction rounds down and rejects fail-closed.
|
|
(None, 0, 0),
|
|
])
|
|
def test_staging_rejection_reports_budget_source_and_remedies(
|
|
tmp_path, monkeypatch, staging_bytes, available, admitted,
|
|
):
|
|
import obliteratus.models.snapshot as backend
|
|
|
|
model = torch.nn.Linear(8, 8, bias=False)
|
|
before = model.weight.clone()
|
|
monkeypatch.setattr(backend, "available_host_bytes", lambda: available)
|
|
with pytest.raises(SnapshotError, match="staging") as exc:
|
|
DiskSnapshot(model, directory=tmp_path, staging_bytes=staging_bytes)
|
|
message = str(exc.value)
|
|
assert "requires 1536 bytes" in message
|
|
assert f"admits {admitted} bytes" in message
|
|
assert f"{available} bytes of available host memory" in message
|
|
source = "default host fraction 0.5" if staging_bytes is None else "explicit staging_bytes cap"
|
|
assert source in message
|
|
assert "snapshot.staging_bytes in study config" in message
|
|
assert "staging_bytes= argument" in message
|
|
assert "limited to available host memory" in message
|
|
assert "free host memory" in message
|
|
assert not list(tmp_path.iterdir())
|
|
torch.testing.assert_close(model.weight, before)
|
|
|
|
|
|
def test_explicit_snapshot_cap_can_use_more_than_default_host_fraction(tmp_path, monkeypatch):
|
|
import obliteratus.models.snapshot as backend
|
|
|
|
model = torch.nn.Linear(8, 8, bias=False)
|
|
monkeypatch.setattr(backend, "available_host_bytes", lambda: 1536)
|
|
with DiskSnapshot(model, directory=tmp_path, staging_bytes=1536) as snapshot:
|
|
assert snapshot.state == "ready"
|
|
|
|
|
|
@pytest.mark.parametrize("name, value", [
|
|
*(('staging_bytes', value) for value in (0, -1, True, False, 1.5, "1024")),
|
|
*(('max_bytes', value) for value in (None, 0, -1, True, False, 1.5, "1024")),
|
|
])
|
|
def test_snapshot_rejects_invalid_byte_limits_before_creating_storage(tmp_path, name, value):
|
|
with pytest.raises(ValueError, match="positive byte counts"):
|
|
DiskSnapshot(torch.nn.Linear(1, 1), directory=tmp_path, **{name: value})
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("offload", ["resident", "cpu", "disk"])
|
|
def test_snapshot_restores_pristine_forward_repeatedly(tmp_path, offload):
|
|
model = torch.nn.Sequential(torch.nn.Linear(3, 4), torch.nn.Linear(4, 2)).eval()
|
|
inputs = torch.ones(1, 3)
|
|
expected = model(inputs).detach()
|
|
if offload == "cpu":
|
|
cpu_offload(model, execution_device=torch.device("cpu"))
|
|
elif offload == "disk":
|
|
disk_offload(model, str(tmp_path / "weights"), execution_device=torch.device("cpu"))
|
|
from obliteratus.models.offload_surgery import zero_logical_parameters
|
|
snapshot = DiskSnapshot(model, directory=tmp_path)
|
|
for _ in range(2):
|
|
zero_logical_parameters(model, [(model[1], "weight", Ellipsis)])
|
|
assert not torch.allclose(model(inputs), expected)
|
|
snapshot.restore(model)
|
|
torch.testing.assert_close(model(inputs), expected)
|
|
snapshot.close()
|
|
assert not snapshot.path.exists()
|
|
|
|
|
|
def test_snapshot_admission_preserves_model_and_removes_partial_storage(tmp_path):
|
|
model = torch.nn.Linear(8, 8)
|
|
before = model.weight.clone()
|
|
with pytest.raises(SnapshotError, match="staging"):
|
|
DiskSnapshot(model, directory=tmp_path, staging_bytes=16)
|
|
assert not list(tmp_path.iterdir())
|
|
torch.testing.assert_close(model.weight, before)
|
|
|
|
|
|
def test_snapshot_identity_and_integrity_checked_before_restore(tmp_path):
|
|
model = torch.nn.Linear(3, 2)
|
|
snapshot = DiskSnapshot(model, directory=tmp_path)
|
|
with pytest.raises(SnapshotError, match="identity"):
|
|
snapshot.restore(torch.nn.Linear(3, 2))
|
|
payload = snapshot.path / snapshot.records[0][1]
|
|
payload.write_bytes(b"corrupt")
|
|
before = model.weight.clone()
|
|
with pytest.raises(SnapshotError, match="integrity"):
|
|
snapshot.restore(model)
|
|
torch.testing.assert_close(model.weight, before)
|
|
snapshot.close()
|
|
|
|
|
|
def test_offload_overlay_is_admitted_as_aggregate_host_memory(tmp_path):
|
|
model = torch.nn.Sequential(*(torch.nn.Linear(8, 8, bias=False) for _ in range(8)))
|
|
cpu_offload(model, execution_device=torch.device("cpu"))
|
|
with pytest.raises(SnapshotError, match="staging"):
|
|
DiskSnapshot(model, directory=tmp_path, staging_bytes=1024)
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
def test_resident_buffers_ties_and_context_cleanup(tmp_path):
|
|
model = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 3))
|
|
model[1].weight = model[0].weight
|
|
model.register_buffer("count", torch.tensor(7))
|
|
before = model[0].weight.clone()
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
with torch.no_grad():
|
|
model[0].weight.zero_()
|
|
model.count.fill_(42)
|
|
snapshot.restore(model)
|
|
torch.testing.assert_close(model[0].weight, before)
|
|
assert model[0].weight is model[1].weight
|
|
assert model.count.item() == 7
|
|
assert not snapshot.path.exists()
|
|
with pytest.raises(SnapshotError, match="identity"):
|
|
snapshot.restore(model)
|
|
|
|
|
|
def test_partial_restore_invalidates_model(tmp_path, monkeypatch):
|
|
import obliteratus.models.snapshot as backend
|
|
model = torch.nn.Linear(3, 2)
|
|
snapshot = DiskSnapshot(model, directory=tmp_path)
|
|
original = backend.load_file
|
|
count = 0
|
|
|
|
def interrupt_second(path):
|
|
nonlocal count
|
|
count += 1
|
|
if count == len(snapshot.records) + 2:
|
|
raise KeyboardInterrupt("interrupted restore")
|
|
return original(path)
|
|
|
|
monkeypatch.setattr(backend, "load_file", interrupt_second)
|
|
with pytest.raises(SnapshotError, match="incomplete"):
|
|
snapshot.restore(model)
|
|
assert snapshot.state == "invalid"
|
|
assert model._obliteratus_surgery_invalid
|
|
with pytest.raises(SnapshotError, match="identity"):
|
|
snapshot.restore(model)
|
|
snapshot.close()
|
|
|
|
|
|
def test_disk_restore_does_not_accumulate_host_overlays_or_owned_files(tmp_path):
|
|
from obliteratus.models.offload_surgery import resolve_logical_parameter, zero_logical_parameters
|
|
model = torch.nn.Sequential(*(torch.nn.Linear(8, 8, bias=False) for _ in range(12))).eval()
|
|
inputs = torch.ones(1, 8)
|
|
expected = model(inputs).detach()
|
|
source = tmp_path / "source"
|
|
disk_offload(model, str(source), execution_device=torch.device("cpu"))
|
|
original_files = {p.name: p.read_bytes() for p in source.iterdir()}
|
|
transaction = resolve_logical_parameter(model[0], "weight", search_roots=(model,))
|
|
backing = transaction._targets[0].container
|
|
del transaction
|
|
# Total model bytes exceed staging. Only bounded per-tensor staging is needed.
|
|
with DiskSnapshot(model, directory=tmp_path, staging_bytes=1600) as snapshot:
|
|
for _ in range(3):
|
|
zero_logical_parameters(model, [(model[0], "weight", Ellipsis)])
|
|
snapshot.restore(model)
|
|
assert not backing.state_dict
|
|
assert len(list(backing._obliteratus_restore_directory.iterdir())) == 12
|
|
torch.testing.assert_close(model(inputs), expected)
|
|
# Snapshot deletion must not delete the new authoritative backing files.
|
|
torch.testing.assert_close(model(inputs), expected)
|
|
assert {p.name: p.read_bytes() for p in source.iterdir()} == original_files
|
|
|
|
|
|
def test_failed_disk_payload_write_preserves_previous_forward(tmp_path, monkeypatch):
|
|
from obliteratus.models.offload_surgery import resolve_logical_parameter
|
|
import safetensors.torch
|
|
model = torch.nn.Linear(3, 2).eval()
|
|
inputs = torch.ones(1, 3)
|
|
expected = model(inputs).detach()
|
|
disk_offload(model, str(tmp_path / "source"), execution_device=torch.device("cpu"))
|
|
transaction = resolve_logical_parameter(model, "weight", search_roots=(model,))
|
|
backing = transaction._targets[0].container
|
|
old_index = dict(backing.index)
|
|
|
|
def fail(*_):
|
|
raise OSError("disk full")
|
|
|
|
monkeypatch.setattr(safetensors.torch, "save_file", fail)
|
|
with pytest.raises(OSError, match="disk full"):
|
|
transaction.restore_to_disk(torch.zeros_like(transaction.tensor), directory=tmp_path)
|
|
assert backing.index == old_index
|
|
assert not list(backing._obliteratus_restore_directory.iterdir())
|
|
torch.testing.assert_close(model(inputs), expected)
|
|
|
|
|
|
def test_restore_rechecks_current_host_headroom_before_mutation(tmp_path, monkeypatch):
|
|
import obliteratus.models.snapshot as backend
|
|
model = torch.nn.Linear(3, 2)
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
with torch.no_grad():
|
|
model.weight.zero_()
|
|
monkeypatch.setattr(backend, 'available_host_bytes', lambda: 0)
|
|
with pytest.raises(SnapshotError, match='currently available'):
|
|
snapshot.restore(model)
|
|
assert not model.weight.count_nonzero()
|
|
assert snapshot.state == 'ready'
|
|
|
|
|
|
@pytest.mark.parametrize('offload', ['cpu', 'disk'])
|
|
def test_offloaded_float_and_integer_buffers_return_to_pristine(tmp_path, offload):
|
|
from obliteratus.models.offload_surgery import resolve_logical_restore_tensor
|
|
model = torch.nn.BatchNorm1d(3).eval()
|
|
inputs = torch.ones(2, 3)
|
|
expected = model(inputs).detach()
|
|
if offload == 'cpu':
|
|
cpu_offload(model, execution_device=torch.device('cpu'), offload_buffers=True)
|
|
else:
|
|
disk_offload(model, str(tmp_path / 'weights'), execution_device=torch.device('cpu'), offload_buffers=True)
|
|
with DiskSnapshot(model, directory=tmp_path) as snapshot:
|
|
mean = resolve_logical_restore_tensor(model, 'running_mean')
|
|
mean.commit(torch.ones_like(mean.tensor) * 10)
|
|
count = resolve_logical_restore_tensor(model, 'num_batches_tracked')
|
|
count.commit(torch.ones_like(count.tensor) * 7)
|
|
assert not torch.allclose(model(inputs), expected)
|
|
snapshot.restore(model)
|
|
torch.testing.assert_close(model(inputs), expected)
|
|
restored_count = resolve_logical_restore_tensor(model, 'num_batches_tracked').tensor
|
|
assert restored_count.item() == 0
|