Squashed 'vendor/dumas_h3_continuum/' content from commit d07ed31
git-subtree-dir: vendor/dumas_h3_continuum git-subtree-split: d07ed31b442d283d6a1740f875a843b9feda4c91
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
sys.modules["decode_cache_test_fixtures"] = sys.modules[__name__]
|
||||
|
||||
|
||||
class NativeVideoModel(torch.nn.Module):
|
||||
__module__ = "comfy.ldm.minimax.vae"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.register_parameter("weight", torch.nn.Parameter(torch.ones(1), requires_grad=False))
|
||||
self.register_buffer("offset", torch.zeros(1))
|
||||
self.tile_size = 256
|
||||
self.eval()
|
||||
|
||||
def decode(self, tensor):
|
||||
return tensor * self.weight + self.offset
|
||||
|
||||
|
||||
class NativeAudioModel(NativeVideoModel):
|
||||
__module__ = "comfy.ldm.minimax.audio_vae"
|
||||
|
||||
|
||||
class Patcher:
|
||||
def __init__(self):
|
||||
self.patches_uuid = uuid.uuid4()
|
||||
self.object_patches = {}
|
||||
|
||||
|
||||
class FakeVAE:
|
||||
"""Controlled delegate fixture, NOT a real MiniMax model/GPU execution."""
|
||||
__module__ = "comfy.sd"
|
||||
|
||||
def __init__(self, stream="video"):
|
||||
self.first_stage_model = NativeVideoModel() if stream == "video" else NativeAudioModel()
|
||||
self.patcher = Patcher()
|
||||
self.vae_dtype = torch.float32
|
||||
self.output_device = torch.device("cpu")
|
||||
self.device = torch.device("cpu")
|
||||
self.audio_sample_rate = 32000
|
||||
self.calls = 0
|
||||
|
||||
def decode(self, tensor):
|
||||
self.calls += 1
|
||||
return self.first_stage_model.decode(tensor)
|
||||
|
||||
def process_output(self, x):
|
||||
return x
|
||||
|
||||
|
||||
class NestedFixture:
|
||||
is_nested = True
|
||||
|
||||
def __init__(self, video, audio):
|
||||
self.parts = video, audio
|
||||
|
||||
def unbind(self):
|
||||
return self.parts
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vae():
|
||||
return FakeVAE()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def samples():
|
||||
return {"samples": torch.arange(120, dtype=torch.float32).reshape(5, 3, 4, 2) / 120}
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Isolated disposable-cache fault worker; never opens Run Storage or a ComfyUI backend."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
import torch
|
||||
from decode_cache import store as module
|
||||
from decode_cache.store import DecodeStore, MiB
|
||||
|
||||
parent, marker, point = Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3]
|
||||
store = DecodeStore(temp_parent=parent, memory_probe=lambda: 10**12, headroom_bytes=0)
|
||||
store.configure("Auto", MiB, 8*MiB, 0)
|
||||
if point == "reader":
|
||||
print(json.dumps({"hit": store.get("same-key") is not None, "root_created": store.root is not None}))
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def pause():
|
||||
marker.write_text(point, encoding="utf-8")
|
||||
while True:
|
||||
time.sleep(0.05)
|
||||
|
||||
|
||||
if point == "during_raw":
|
||||
original_blocks = module.tensor_blocks
|
||||
def blocks(tensor):
|
||||
iterator = iter(original_blocks(tensor))
|
||||
first = next(iterator)
|
||||
yield first[:16]
|
||||
pause()
|
||||
module.tensor_blocks = blocks
|
||||
elif point == "before_publish":
|
||||
replace = os.replace
|
||||
def before_replace(src, dst):
|
||||
pause()
|
||||
return replace(src, dst)
|
||||
os.replace = before_replace
|
||||
|
||||
result = store.put("same-key", torch.ones(64, 64, 3), "video", {}, (), store.epoch)
|
||||
if point == "after_publish":
|
||||
pause()
|
||||
raise RuntimeError("fault point was not reached")
|
||||
@@ -0,0 +1,273 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from decode_cache.service import DecodeService, native_delegate
|
||||
from decode_cache.identity import selected_latent, vae_signature
|
||||
from decode_cache.nodes import H3DecodeCacheHelper, NODE_CLASS_MAPPINGS
|
||||
from decode_cache.store import DecodeStore, MiB
|
||||
from decode_cache_test_fixtures import FakeVAE, NestedFixture
|
||||
|
||||
|
||||
def fake_video(vae, samples):
|
||||
return vae.decode(selected_latent(samples, "video"))
|
||||
|
||||
|
||||
def fake_audio(vae, samples):
|
||||
# A controlled Core-helper surrogate returns sample-rate metadata as Core does.
|
||||
return {"waveform": vae.decode(selected_latent(samples, "audio")),
|
||||
"sample_rate": samples.get("sample_rate", vae.audio_sample_rate)}
|
||||
|
||||
|
||||
def resolver(stream):
|
||||
f = fake_video if stream == "video" else fake_audio
|
||||
return f, f
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service(tmp_path):
|
||||
store = DecodeStore(temp_parent=tmp_path, memory_probe=lambda: 10**12,
|
||||
disk_probe=lambda p: SimpleNamespace(free=10**12), headroom_bytes=0)
|
||||
store.configure("Auto", MiB, 8*MiB, 0)
|
||||
service = DecodeService(store=store, delegate_resolver=resolver, interrupt=lambda: None)
|
||||
yield service
|
||||
service.store.close()
|
||||
|
||||
|
||||
def test_delegate_called_once_warm(service, vae, samples):
|
||||
a, e = service.decode(samples, vae, "video")
|
||||
b, f = service.decode(samples, vae, "video")
|
||||
assert e["status"] == "miss" and f["status"] == "hit"
|
||||
assert vae.calls == 1 and torch.equal(a, b)
|
||||
|
||||
|
||||
def test_audio_metadata_override_and_mutation(service):
|
||||
vae = FakeVAE("audio")
|
||||
samples = {"samples": torch.ones(1, 2, 40), "sample_rate": 22050}
|
||||
a, _ = service.decode(samples, vae, "audio")
|
||||
a["waveform"].zero_()
|
||||
a["sample_rate"] = 5
|
||||
b, e = service.decode(samples, vae, "audio")
|
||||
assert e["status"] == "hit"
|
||||
assert b["sample_rate"] == 22050 and b["waveform"].sum() == 80
|
||||
samples["sample_rate"] = 32000
|
||||
_, e = service.decode(samples, vae, "audio")
|
||||
assert e["status"] == "miss"
|
||||
|
||||
|
||||
def test_core_error_not_retried(service, vae, samples):
|
||||
calls = []
|
||||
def broken(vae, samples):
|
||||
calls.append(1)
|
||||
raise RuntimeError("native decode failed")
|
||||
service.delegate_resolver = lambda stream: (broken, broken)
|
||||
with pytest.raises(RuntimeError, match="native decode failed"):
|
||||
service.decode(samples, vae, "video")
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("point", ["signature", "get", "put"])
|
||||
def test_cache_fail_open(service, vae, samples, monkeypatch, point):
|
||||
def broken(*args, **kwargs):
|
||||
raise OSError("fixture-only cache failure")
|
||||
if point == "signature":
|
||||
service.signature_fn = broken
|
||||
else:
|
||||
monkeypatch.setattr(service.store, point, broken)
|
||||
result, event = service.decode(samples, vae, "video")
|
||||
assert torch.equal(result, samples["samples"])
|
||||
assert vae.calls == 1
|
||||
|
||||
|
||||
def test_interrupt_not_swallowed(service, vae, samples, monkeypatch):
|
||||
class InterruptProcessingException(Exception):
|
||||
pass
|
||||
def interrupted(*args):
|
||||
raise InterruptProcessingException()
|
||||
monkeypatch.setattr(service.store, "get", interrupted)
|
||||
with pytest.raises(InterruptProcessingException):
|
||||
service.decode(samples, vae, "video")
|
||||
assert vae.calls == 0
|
||||
|
||||
|
||||
def test_unsupported_vae_still_decodes(service, samples):
|
||||
class Unknown:
|
||||
def decode(self, tensor):
|
||||
return tensor.clone()
|
||||
result, event = service.decode(samples, Unknown(), "video")
|
||||
assert event["status"] == "bypass" and torch.equal(result, samples["samples"])
|
||||
|
||||
|
||||
def test_vae_change_during_decode_skips_store(service, vae, samples):
|
||||
def changing(vae, samples):
|
||||
vae.first_stage_model.tile_size += 1
|
||||
return vae.decode(selected_latent(samples, "video"))
|
||||
service.delegate_resolver = lambda stream: (changing, changing)
|
||||
_, event = service.decode(samples, vae, "video")
|
||||
assert "store_skipped" in event and not service.store.entries
|
||||
|
||||
|
||||
def test_q1_q2_q3_q4_physical_lists(service):
|
||||
vae = FakeVAE()
|
||||
groups = [{"samples": torch.ones(124 if i == 0 else 141, 2, 2, 3)*i} for i in range(3)]
|
||||
statuses = []
|
||||
for sequence in ([groups[0]], [groups[1]], groups, groups):
|
||||
statuses.append([service.decode(s, vae, "video")[1]["status"] for s in sequence])
|
||||
assert statuses == [["miss"], ["miss"], ["hit", "hit", "miss"], ["hit", "hit", "hit"]]
|
||||
assert vae.calls == 3
|
||||
|
||||
|
||||
def test_terminal_entry_never_sliced(service):
|
||||
vae = FakeVAE()
|
||||
x = torch.rand(260, 2, 2, 3)
|
||||
for _ in range(2):
|
||||
out, _ = service.decode({"samples": x}, vae, "video")
|
||||
assert torch.equal(out, x)
|
||||
assert vae.calls == 1
|
||||
|
||||
|
||||
def test_retry_changed_group_only_misses(service):
|
||||
vae = FakeVAE()
|
||||
a = {"samples": torch.ones(8, 2, 2, 3)}
|
||||
b = {"samples": torch.ones(8, 2, 2, 3)*2}
|
||||
service.decode(a, vae, "video")
|
||||
service.decode(b, vae, "video")
|
||||
b["samples"].add_(1)
|
||||
assert service.decode(a, vae, "video")[1]["status"] == "hit"
|
||||
assert service.decode(b, vae, "video")[1]["status"] == "miss"
|
||||
|
||||
|
||||
def test_native_functions_are_delegated(monkeypatch, vae, samples):
|
||||
calls = []
|
||||
mod = ModuleType("nodes")
|
||||
class VAEDecode:
|
||||
def decode(self, vae, samples):
|
||||
calls.append(("video", vae, samples))
|
||||
return (samples["samples"],)
|
||||
mod.VAEDecode = VAEDecode
|
||||
monkeypatch.setitem(sys.modules, "nodes", mod)
|
||||
audio = ModuleType("comfy_extras.nodes_audio")
|
||||
def audio_helper(vae, samples):
|
||||
calls.append(("audio", vae, samples))
|
||||
return {"waveform": samples["samples"], "sample_rate": 12345}
|
||||
audio.vae_decode_audio = audio_helper
|
||||
monkeypatch.setitem(sys.modules, "comfy_extras.nodes_audio", audio)
|
||||
assert native_delegate("video")[1](vae, samples) is samples["samples"]
|
||||
assert native_delegate("audio")[1](vae, samples)["sample_rate"] == 12345
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
def test_one_public_node_and_nan():
|
||||
assert list(NODE_CLASS_MAPPINGS) == ["H3DecodeCacheHelper"]
|
||||
assert H3DecodeCacheHelper.INPUT_IS_LIST
|
||||
assert H3DecodeCacheHelper.OUTPUT_IS_LIST == (True, True, False)
|
||||
assert math.isnan(H3DecodeCacheHelper.IS_CHANGED())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("streams", ["video", "audio", "both", "none"])
|
||||
def test_node_optional_independent_lists(service, streams):
|
||||
node = H3DecodeCacheHelper()
|
||||
node.service = service
|
||||
kwargs = {}
|
||||
if streams in ("video", "both"):
|
||||
kwargs.update(video_samples=[{"samples": torch.ones(4, 2, 2, 3)}]*2, video_vae=[FakeVAE()])
|
||||
if streams in ("audio", "both"):
|
||||
kwargs.update(audio_samples=[{"samples": torch.ones(1, 2, 16)}], audio_vae=[FakeVAE("audio")])
|
||||
images, audio, report = node.decode(["Auto"], [1], [1], [0], **kwargs)
|
||||
assert len(images) == (2 if streams in ("video", "both") else 0)
|
||||
assert len(audio) == (1 if streams in ("audio", "both") else 0)
|
||||
assert json.loads(report)["mode"] == "Auto"
|
||||
|
||||
|
||||
def test_none_position_no_broadcast(service):
|
||||
node = H3DecodeCacheHelper()
|
||||
node.service = service
|
||||
images, _, _ = node.decode(["Auto"], [1], [1], [0],
|
||||
video_samples=[None, {"samples": torch.ones(4, 2, 2, 3)}], video_vae=[FakeVAE()])
|
||||
assert images[0] is None and images[1].shape == (4, 2, 2, 3)
|
||||
|
||||
|
||||
def test_off_disables_content_cache(service, vae, samples):
|
||||
node = H3DecodeCacheHelper()
|
||||
node.service = service
|
||||
for _ in range(2):
|
||||
node.decode(["Off"], [1], [0], [0], video_samples=[samples], video_vae=[vae])
|
||||
assert vae.calls == 2 and not service.store.entries
|
||||
|
||||
|
||||
def test_cache_configuration_fault_is_nonfatal(service, vae, samples, monkeypatch):
|
||||
node = H3DecodeCacheHelper()
|
||||
node.service = service
|
||||
def bad(*a):
|
||||
raise OSError("memory probe unavailable")
|
||||
monkeypatch.setattr(service.store, "configure", bad)
|
||||
result = node.decode(["Auto"], [1], [1], [0], video_samples=[samples], video_vae=[vae])
|
||||
assert vae.calls == 1
|
||||
assert "cache_disabled_reason" in json.loads(result[2])
|
||||
|
||||
|
||||
def test_builtin_helper_is_publicly_registered():
|
||||
from ComfyUI_H3_Continuum_Join import nodes as continuum_nodes
|
||||
|
||||
assert "H3DecodeCacheHelper" in continuum_nodes.NODE_CLASS_MAPPINGS
|
||||
assert continuum_nodes.NODE_DISPLAY_NAME_MAPPINGS["H3DecodeCacheHelper"] == "Decode Cache Helper"
|
||||
|
||||
|
||||
def test_forbidden_runtime_imports():
|
||||
import ast
|
||||
root = Path(__file__).resolve().parents[2]/"decode_cache"
|
||||
forbidden = {"run_storage", "runtime_coordinator", "execution_planner", "sampling_engine"}
|
||||
for path in root.glob("*.py"):
|
||||
tree = ast.parse(path.read_text())
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.ImportFrom):
|
||||
assert not forbidden.intersection((node.module or "").split("."))
|
||||
|
||||
|
||||
def test_cache_memory_error_returns_already_decoded_result(service, vae, samples, monkeypatch):
|
||||
def no_memory(*args, **kwargs):
|
||||
raise MemoryError("cache allocation failed")
|
||||
monkeypatch.setattr(service.store, "put", no_memory)
|
||||
out, event = service.decode(samples, vae, "video")
|
||||
assert torch.equal(out, samples["samples"])
|
||||
assert vae.calls == 1 and "cache_write_error" in event
|
||||
|
||||
|
||||
def test_native_oom_propagates_exactly_once(service, vae, samples):
|
||||
calls = []
|
||||
def oom(*args, **kwargs):
|
||||
calls.append(1)
|
||||
raise torch.OutOfMemoryError("native fixture OOM")
|
||||
service.delegate_resolver = lambda stream: (oom, oom)
|
||||
with pytest.raises(torch.OutOfMemoryError):
|
||||
service.decode(samples, vae, "video")
|
||||
assert calls == [1]
|
||||
|
||||
|
||||
def test_cancel_during_cache_store_is_not_a_native_retry(service, vae, samples, monkeypatch):
|
||||
class InterruptProcessingException(Exception):
|
||||
pass
|
||||
def canceled(*args, **kwargs):
|
||||
raise InterruptProcessingException("cancel fixture")
|
||||
monkeypatch.setattr(service.store, "put", canceled)
|
||||
with pytest.raises(InterruptProcessingException):
|
||||
service.decode(samples, vae, "video")
|
||||
assert vae.calls == 1
|
||||
|
||||
|
||||
def test_two_simultaneous_node_calls_serialize_one_miss(service, vae, samples):
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
node = H3DecodeCacheHelper()
|
||||
node.service = service
|
||||
def run():
|
||||
return node.decode(["Auto"], [1], [1], [0], video_samples=[samples], video_vae=[vae])
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
result = list(pool.map(lambda _: run(), range(2)))
|
||||
assert vae.calls == 1
|
||||
assert torch.equal(result[0][0][0], result[1][0][0])
|
||||
@@ -0,0 +1,164 @@
|
||||
import gc
|
||||
import uuid
|
||||
import weakref
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from decode_cache.identity import CacheBypass, ObjectTokens, make_key, selected_latent, tensor_digest, vae_signature
|
||||
from decode_cache_test_fixtures import FakeVAE, NestedFixture
|
||||
|
||||
|
||||
def delegate():
|
||||
pass
|
||||
|
||||
|
||||
def test_same_content_different_object_same_key(samples):
|
||||
other = {"samples": samples["samples"].clone()}
|
||||
assert make_key(samples, "video", "s") == make_key(other, "video", "s")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dtype", [torch.float16, torch.float32, torch.float64, torch.bfloat16, torch.int16])
|
||||
def test_tensor_bytes_all_dtypes(dtype):
|
||||
x = torch.arange(60).to(dtype).reshape(3, 4, 5)
|
||||
assert tensor_digest(x) == tensor_digest(x.clone())
|
||||
x[0, 0, 0] += 1
|
||||
assert tensor_digest(x) != tensor_digest(torch.arange(60).to(dtype).reshape(3, 4, 5))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shape", [(), (0,), (1,), (3, 4), (1, 2, 8), (2, 1, 3, 4)])
|
||||
def test_block_hash_matches_raw(shape):
|
||||
import hashlib
|
||||
from decode_cache.identity import tensor_blocks
|
||||
x = torch.ones(shape)
|
||||
actual = hashlib.sha256(b"".join(bytes(b) for b in tensor_blocks(x, 9))).hexdigest()
|
||||
expected = hashlib.sha256(x.contiguous().reshape(-1).view(torch.uint8).numpy().tobytes()).hexdigest()
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_noncontiguous_order():
|
||||
x = torch.arange(120).reshape(4, 5, 6).transpose(0, 2)
|
||||
assert tensor_digest(x) == tensor_digest(x.contiguous())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["content", "dtype", "shape", "vae", "stream", "reset", "sample_rate", "extra"])
|
||||
def test_key_invalidations(samples, change):
|
||||
kwargs = {"samples": samples, "stream": "video", "signature": "s", "reset_token": 0}
|
||||
before = make_key(**kwargs)
|
||||
if change == "content":
|
||||
samples["samples"][0, 0, 0, 0] += 1
|
||||
elif change == "dtype":
|
||||
samples["samples"] = samples["samples"].double()
|
||||
elif change == "shape":
|
||||
samples["samples"] = samples["samples"].reshape(6, 2, 5, 2)
|
||||
elif change == "vae":
|
||||
kwargs["signature"] = "other"
|
||||
elif change == "stream":
|
||||
kwargs["stream"] = "audio"
|
||||
elif change == "reset":
|
||||
kwargs["reset_token"] = 1
|
||||
elif change == "sample_rate":
|
||||
samples["sample_rate"] = 22050
|
||||
else:
|
||||
samples["noise_mask"] = torch.zeros(1)
|
||||
assert make_key(**kwargs) != before
|
||||
|
||||
|
||||
def test_audio_rate_override_key():
|
||||
x = torch.zeros(1, 2, 8)
|
||||
assert make_key({"samples": x, "sample_rate": 16000}, "audio", "s") != make_key({"samples": x, "sample_rate": 32000}, "audio", "s")
|
||||
|
||||
|
||||
def test_nested_only_selected_stream():
|
||||
x, y = torch.ones(1, 24, 2, 2, 2), torch.ones(1, 32, 2, 4)
|
||||
s = {"samples": NestedFixture(x, y)}
|
||||
v = make_key(s, "video", "s")
|
||||
a = make_key(s, "audio", "s")
|
||||
y.add_(3)
|
||||
assert make_key(s, "video", "s") == v
|
||||
assert make_key(s, "audio", "s") != a
|
||||
assert selected_latent(s, "audio") is y
|
||||
|
||||
|
||||
def test_opaque_metadata_bypass(samples):
|
||||
samples["opaque"] = object()
|
||||
with pytest.raises(CacheBypass):
|
||||
make_key(samples, "video", "s")
|
||||
|
||||
|
||||
def test_inference_tensor_hash_no_version_required():
|
||||
with torch.inference_mode():
|
||||
x = torch.ones(4)
|
||||
assert tensor_digest(x) == tensor_digest(x.clone())
|
||||
|
||||
|
||||
def test_identity_does_not_keep_vae_alive():
|
||||
vae = FakeVAE() # no pytest fixture owner remains alive
|
||||
signature, refs = vae_signature(vae, "video", delegate)
|
||||
ref = weakref.ref(vae)
|
||||
del vae
|
||||
gc.collect()
|
||||
assert ref() is None
|
||||
assert refs[0]() is None
|
||||
|
||||
|
||||
def test_object_tokens_not_address_only():
|
||||
tokens = ObjectTokens()
|
||||
a, b = FakeVAE(), FakeVAE()
|
||||
assert tokens.token(a) == tokens.token(a)
|
||||
assert tokens.token(a) != tokens.token(b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["patch_uuid", "weight_copy", "buffer_copy", "tiling", "dtype", "new_vae"])
|
||||
def test_vae_signature_changes(vae, change):
|
||||
a = vae_signature(vae, "video", delegate)[0]
|
||||
if change == "patch_uuid":
|
||||
vae.patcher.patches_uuid = uuid.uuid4()
|
||||
elif change == "weight_copy":
|
||||
vae.first_stage_model.weight.copy_(torch.ones(1)*2)
|
||||
elif change == "buffer_copy":
|
||||
vae.first_stage_model.offset.add_(1)
|
||||
elif change == "tiling":
|
||||
vae.first_stage_model.tile_size = 512
|
||||
elif change == "dtype":
|
||||
vae.vae_dtype = torch.float16
|
||||
else:
|
||||
vae = FakeVAE()
|
||||
assert vae_signature(vae, "video", delegate)[0] != a
|
||||
|
||||
|
||||
def test_unknown_vae_and_training_bypass(vae):
|
||||
with pytest.raises(CacheBypass):
|
||||
vae_signature(object(), "video", delegate)
|
||||
vae.first_stage_model.train()
|
||||
with pytest.raises(CacheBypass):
|
||||
vae_signature(vae, "video", delegate)
|
||||
|
||||
|
||||
def test_relocation_buffer_identity_not_used(vae):
|
||||
a = vae_signature(vae, "video", delegate)[0]
|
||||
vae.first_stage_model.offset = vae.first_stage_model.offset.clone()
|
||||
assert vae_signature(vae, "video", delegate)[0] == a
|
||||
|
||||
|
||||
def test_empty_native_model_options_allowed_but_hooks_bypass(vae):
|
||||
vae.patcher.model_options = {"transformer_options": {}}
|
||||
vae_signature(vae, "video", delegate)
|
||||
vae.patcher.model_options["transformer_options"]["wrapper"] = object()
|
||||
with pytest.raises(CacheBypass):
|
||||
vae_signature(vae, "video", delegate)
|
||||
|
||||
|
||||
def test_forward_hook_disables_cache_only(vae):
|
||||
handle = vae.first_stage_model.register_forward_hook(lambda *args: None)
|
||||
try:
|
||||
with pytest.raises(CacheBypass):
|
||||
vae_signature(vae, "video", delegate)
|
||||
finally:
|
||||
handle.remove()
|
||||
|
||||
|
||||
def test_inference_context_has_separate_identity(vae):
|
||||
before = vae_signature(vae, "video", delegate)[0]
|
||||
with torch.inference_mode():
|
||||
assert vae_signature(vae, "video", delegate)[0] != before
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Process termination tests on the host OS, not a Windows/power-loss claim."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
WORKER = Path(__file__).parent / "helpers" / "cache_process_worker.py"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("point", ["during_raw", "before_publish", "after_publish"])
|
||||
def test_process_kill_leaves_only_disposable_private_files(tmp_path, point):
|
||||
parent = tmp_path / "private-cache-parent"
|
||||
parent.mkdir()
|
||||
marker = tmp_path / "ready.txt"
|
||||
user_file = tmp_path / "untouched-run-storage.txt"
|
||||
user_file.write_text("existing accepted data")
|
||||
env = {**os.environ, "PYTHONDONTWRITEBYTECODE": "1", "OMP_NUM_THREADS": "1"}
|
||||
p = subprocess.Popen([sys.executable, str(WORKER), str(parent), str(marker), point],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, env=env)
|
||||
try:
|
||||
deadline = time.monotonic() + 20
|
||||
while not marker.exists() and time.monotonic() < deadline:
|
||||
if p.poll() is not None:
|
||||
raise AssertionError(p.stderr.read().decode())
|
||||
time.sleep(0.03)
|
||||
assert marker.exists(), "worker never reached the kill boundary"
|
||||
p.kill()
|
||||
p.wait(timeout=5)
|
||||
finally:
|
||||
if p.poll() is None:
|
||||
p.kill()
|
||||
p.wait(timeout=5)
|
||||
if p.stderr:
|
||||
p.stderr.close()
|
||||
raw = list(parent.rglob("*.npy"))
|
||||
temporary = list(parent.rglob("*.tmp"))
|
||||
if point == "after_publish":
|
||||
assert len(raw) == 1 and not temporary
|
||||
assert np.array_equal(np.load(raw[0], allow_pickle=False), np.ones((64, 64, 3), dtype=np.float32))
|
||||
else:
|
||||
assert not raw and len(temporary) == 1
|
||||
reader = subprocess.run([sys.executable, str(WORKER), str(parent), str(marker), "reader"],
|
||||
capture_output=True, text=True, env=env, check=True, timeout=20)
|
||||
assert json.loads(reader.stdout.splitlines()[-1]) == {"hit": False, "root_created": False}
|
||||
assert user_file.read_text() == "existing accepted data"
|
||||
@@ -0,0 +1,295 @@
|
||||
import gc
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import weakref
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from decode_cache.identity import CacheBypass
|
||||
from decode_cache.store import DecodeStore, MiB
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path):
|
||||
s = DecodeStore(temp_parent=tmp_path, memory_probe=lambda: 10**12,
|
||||
disk_probe=lambda p: SimpleNamespace(free=10**12), headroom_bytes=0)
|
||||
s.configure("Auto", 1024**2, 1024**2, 0)
|
||||
yield s
|
||||
s.close()
|
||||
|
||||
|
||||
def put(store, key, x, stream="video"):
|
||||
return store.put(key, x, stream, {}, (), store.epoch)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", ["Auto", "RAM"])
|
||||
def test_hit_bit_exact_mutation_isolation(store, mode):
|
||||
store.configure(mode, MiB, MiB, 0)
|
||||
x = torch.arange(120).float().reshape(5, 3, 4, 2)
|
||||
replacement = put(store, "a", x)
|
||||
first = replacement[0] if replacement else x
|
||||
first.add_(7)
|
||||
hit, _ = store.get("a")
|
||||
assert torch.equal(hit, torch.arange(120).float().reshape(5, 3, 4, 2))
|
||||
hit.zero_()
|
||||
again, _ = store.get("a")
|
||||
assert again.sum().item() > 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", ["Auto", "RAM"])
|
||||
def test_view_mutation_does_not_poison(store, mode):
|
||||
store.configure(mode, MiB, MiB, 0)
|
||||
put(store, "a", torch.ones(4, 3, 2, 1))
|
||||
hit, _ = store.get("a")
|
||||
hit[1:3].mul_(0)
|
||||
other, _ = store.get("a")
|
||||
assert other.sum() == 24
|
||||
|
||||
|
||||
def test_noncontiguous_serialization(store):
|
||||
x = torch.arange(120).float().reshape(5, 3, 4, 2).transpose(0, 2)
|
||||
put(store, "a", x)
|
||||
y, _ = store.get("a")
|
||||
assert y.dtype == x.dtype and torch.equal(x, y)
|
||||
|
||||
|
||||
def test_dtype_unchanged(store):
|
||||
for dtype in (torch.float16, torch.float32, torch.float64):
|
||||
x = torch.arange(12).to(dtype)
|
||||
put(store, str(dtype), x)
|
||||
y, _ = store.get(str(dtype))
|
||||
assert y.dtype == dtype and torch.equal(x, y)
|
||||
|
||||
|
||||
def test_ram_lru_and_budget(store):
|
||||
store.configure("RAM", 32, MiB, 0)
|
||||
put(store, "a", torch.ones(4))
|
||||
put(store, "b", torch.ones(4)*2)
|
||||
assert store.get("a") is not None
|
||||
put(store, "c", torch.ones(4)*3)
|
||||
assert store.get("b") is None
|
||||
assert store.ram_bytes == 32
|
||||
|
||||
|
||||
def test_ram_pressure_evicts(store):
|
||||
store.configure("RAM", MiB, MiB, 0)
|
||||
put(store, "a", torch.ones(4))
|
||||
store.headroom_bytes = 1024
|
||||
store.memory_probe = lambda: 0
|
||||
store.configure("RAM", MiB, MiB, 0)
|
||||
assert store.get("a") is None
|
||||
assert store.ram_bytes == 0
|
||||
|
||||
|
||||
def test_auto_under_ram_pressure_uses_disk(store):
|
||||
store.memory_probe = lambda: 0
|
||||
put(store, "a", torch.ones(24), "audio")
|
||||
assert store.entries["a"].path is not None
|
||||
|
||||
|
||||
def test_disk_full_skips_cache(store):
|
||||
store.disk_probe = lambda p: SimpleNamespace(free=0)
|
||||
assert put(store, "a", torch.ones(4)) is None
|
||||
assert store.get("a") is None
|
||||
|
||||
|
||||
def test_oversized_skips(store):
|
||||
store.configure("Auto", 0, 8, 0)
|
||||
assert put(store, "a", torch.ones(4)) is None
|
||||
assert store.root is None
|
||||
|
||||
|
||||
def test_living_mmap_survives_clear_and_accounts_bytes(store):
|
||||
hit = put(store, "a", torch.ones(4))[0]
|
||||
path = store.entries["a"].path
|
||||
store.clear()
|
||||
assert path.exists()
|
||||
assert store.disk_bytes > 0
|
||||
hit.add_(2)
|
||||
assert torch.equal(hit, torch.ones(4)*3)
|
||||
del hit
|
||||
gc.collect()
|
||||
store.stats()
|
||||
assert not path.exists()
|
||||
assert store.disk_bytes == 0
|
||||
|
||||
|
||||
def test_no_cross_process_store_reuse(store, tmp_path):
|
||||
put(store, "a", torch.ones(4))
|
||||
other = DecodeStore(temp_parent=tmp_path)
|
||||
assert other.get("a") is None
|
||||
other.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("damage", ["truncate", "header", "data", "delete"])
|
||||
def test_corrupt_disk_invalidates(store, damage):
|
||||
result = put(store, "a", torch.ones(20))
|
||||
del result
|
||||
gc.collect()
|
||||
path = store.entries["a"].path
|
||||
if damage == "delete":
|
||||
path.unlink()
|
||||
elif damage == "truncate":
|
||||
path.write_bytes(b"bad")
|
||||
else:
|
||||
with open(path, "r+b") as f:
|
||||
f.seek(-1 if damage == "data" else 0, 2 if damage == "data" else 0)
|
||||
b = f.read(1)
|
||||
f.seek(-1, 1)
|
||||
f.write(bytes([b[0] ^ 1]))
|
||||
with pytest.raises((CacheBypass, OSError, ValueError)):
|
||||
store.get("a")
|
||||
assert store.get("a") is None
|
||||
|
||||
|
||||
def test_corrupt_ram_invalidates(store):
|
||||
store.configure("RAM", MiB, MiB, 0)
|
||||
put(store, "a", torch.ones(4))
|
||||
store.entries["a"].tensor.zero_()
|
||||
with pytest.raises(CacheBypass):
|
||||
store.get("a")
|
||||
assert store.get("a") is None
|
||||
|
||||
|
||||
def test_dead_vae_eviction(store):
|
||||
class Owner:
|
||||
pass
|
||||
owner = Owner()
|
||||
store.put("a", torch.ones(4), "audio", {}, (weakref.ref(owner),), store.epoch)
|
||||
del owner
|
||||
gc.collect()
|
||||
assert store.get("a") is None
|
||||
|
||||
|
||||
def test_write_fault_no_published_entry(store, monkeypatch):
|
||||
def fail(*args):
|
||||
raise OSError("read-only test cache")
|
||||
monkeypatch.setattr(os, "replace", fail)
|
||||
with pytest.raises(OSError):
|
||||
put(store, "a", torch.ones(4))
|
||||
assert not store.entries
|
||||
assert not list(store.root.iterdir())
|
||||
|
||||
|
||||
def test_unknown_temp_is_never_loaded_or_removed(store):
|
||||
root = store._directory()
|
||||
stale = root / "foreign.tmp"
|
||||
stale.write_text("not a cache entry")
|
||||
store.close()
|
||||
assert stale.exists()
|
||||
|
||||
|
||||
def test_reset_and_disabled_no_storage(store):
|
||||
put(store, "a", torch.ones(4))
|
||||
store.configure("Off", MiB, MiB, 1)
|
||||
assert store.get("a") is None
|
||||
assert put(store, "b", torch.ones(4)) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", ["Auto", "RAM"])
|
||||
def test_token_change_clears_once_then_same_token_keeps_cache(store, mode):
|
||||
store.configure(mode, MiB, MiB, 0)
|
||||
put(store, "before", torch.ones(4))
|
||||
epoch = store.epoch
|
||||
store.configure(mode, MiB, MiB, 0)
|
||||
assert store.get("before") is not None
|
||||
assert store.epoch == epoch
|
||||
# The UI increments only the submitted INT; configure acts on next Queue.
|
||||
store.configure(mode, MiB, MiB, 1)
|
||||
assert store.get("before") is None
|
||||
assert store.epoch > epoch
|
||||
put(store, "after", torch.ones(4))
|
||||
epoch = store.epoch
|
||||
store.configure(mode, MiB, MiB, 1)
|
||||
assert store.get("after") is not None
|
||||
assert store.epoch == epoch
|
||||
|
||||
|
||||
def test_epoch_prevents_inflight_reinsertion(store):
|
||||
epoch = store.epoch
|
||||
store.clear()
|
||||
assert store.put("a", torch.ones(4), "video", {}, (), epoch) is None
|
||||
assert store.get("a") is None
|
||||
|
||||
|
||||
def test_pending_disk_files_are_in_quota(store):
|
||||
store.configure("Auto", 0, 6000, 0)
|
||||
x = torch.ones(1000) # 4000 bytes + header; next insert must not delete a live mmap
|
||||
first = put(store, "a", x)
|
||||
assert first is None # conservative header reservation is > 6000
|
||||
store.configure("Auto", 0, 10000, 0)
|
||||
first = put(store, "a", x)
|
||||
assert first is not None
|
||||
store.clear()
|
||||
# 4128 + conservative 8096 > quota; pinned pending file counts
|
||||
assert put(store, "b", x) is None
|
||||
del first
|
||||
gc.collect()
|
||||
assert put(store, "b", x) is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", ["Auto", "RAM"])
|
||||
def test_output_metadata_is_defensively_copied(store, mode):
|
||||
store.configure(mode, MiB, MiB, 0)
|
||||
extra = {"sample_rate": 32000, "future_metadata": {"labels": ["original"]}}
|
||||
store.put("a", torch.ones(12), "video", extra, (), store.epoch)
|
||||
extra["future_metadata"]["labels"][0] = "caller mutation"
|
||||
_, first = store.get("a")
|
||||
assert first["future_metadata"]["labels"] == ["original"]
|
||||
first["future_metadata"]["labels"][0] = "downstream mutation"
|
||||
_, second = store.get("a")
|
||||
assert second["future_metadata"]["labels"] == ["original"]
|
||||
|
||||
|
||||
def test_delete_failure_is_accounted_and_retried(store, monkeypatch):
|
||||
put(store, "a", torch.ones(12))
|
||||
path = store.entries["a"].path
|
||||
original_unlink = Path.unlink
|
||||
def denied(self, *args, **kwargs):
|
||||
if self == path:
|
||||
raise PermissionError("simulated Windows sharing violation")
|
||||
return original_unlink(self, *args, **kwargs)
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(Path, "unlink", denied)
|
||||
store.clear()
|
||||
assert store.disk_bytes > 0 and len(store.retired) == 1
|
||||
store.stats()
|
||||
assert not path.exists() and store.disk_bytes == 0
|
||||
|
||||
|
||||
def test_symlink_cache_file_is_not_opened(store, tmp_path):
|
||||
put(store, "a", torch.ones(12))
|
||||
path = store.entries["a"].path
|
||||
outside = tmp_path / "outside-user-file.bin"
|
||||
outside.write_bytes(b"DO NOT TOUCH")
|
||||
path.unlink()
|
||||
try:
|
||||
path.symlink_to(outside)
|
||||
except OSError:
|
||||
pytest.skip("symbolic links not available")
|
||||
with pytest.raises(CacheBypass):
|
||||
store.get("a")
|
||||
assert outside.read_bytes() == b"DO NOT TOUCH"
|
||||
|
||||
|
||||
def test_store_garbage_collection_cleans_unleased_private_files(tmp_path):
|
||||
s = DecodeStore(temp_parent=tmp_path, memory_probe=lambda: 10**12, headroom_bytes=0)
|
||||
put(s, "a", torch.ones(12))
|
||||
root = s.root
|
||||
assert root.exists()
|
||||
ref = weakref.ref(s)
|
||||
del s
|
||||
gc.collect()
|
||||
assert ref() is None and not root.exists()
|
||||
|
||||
|
||||
def test_explicit_private_cache_parent_uses_only_child_directory(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("H3_DECODE_CACHE_TEMP_DIR", str(tmp_path))
|
||||
s = DecodeStore(memory_probe=lambda: 10**12, headroom_bytes=0)
|
||||
put(s, "a", torch.ones(12))
|
||||
assert s.root.parent == tmp_path and s.root.name.startswith("h3-decode-cache-")
|
||||
s.close()
|
||||
assert tmp_path.exists()
|
||||
Reference in New Issue
Block a user