feat: release V3.5.0
This commit is contained in:
@@ -0,0 +1,515 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import gc
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import weakref
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
|
||||
MODULE_PATH = (
|
||||
Path(__file__).resolve().parents[1] / "tools" / "p20_file_backed_tensor_poc.py"
|
||||
)
|
||||
SPEC = importlib.util.spec_from_file_location("h3c_p20_file_backed_tensor_poc", MODULE_PATH)
|
||||
MODULE = importlib.util.module_from_spec(SPEC)
|
||||
assert SPEC.loader is not None
|
||||
sys.modules[SPEC.name] = MODULE
|
||||
SPEC.loader.exec_module(MODULE)
|
||||
|
||||
|
||||
def _known_files(root: Path) -> list[Path]:
|
||||
return sorted(path for path in root.iterdir() if path.name.startswith(MODULE.FILE_PREFIX))
|
||||
|
||||
|
||||
def test_shared_mapping_is_bit_exact_contiguous_and_write_through(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
reference = torch.arange(120, dtype=torch.float32).reshape(2, 3, 4, 5)
|
||||
|
||||
with manager.allocate(tuple(reference.shape), dtype=reference.dtype) as allocation:
|
||||
allocation.tensor.copy_(reference)
|
||||
record = allocation.record
|
||||
output = allocation.publish()
|
||||
|
||||
assert output.shape == reference.shape
|
||||
assert output.dtype == reference.dtype
|
||||
assert output.device.type == "cpu"
|
||||
assert output.is_contiguous()
|
||||
assert output.contiguous().data_ptr() == output.data_ptr()
|
||||
assert record.data_path.stat().st_size == reference.numel() * reference.element_size()
|
||||
assert record.data_path.read_bytes() == reference.numpy().tobytes(order="C")
|
||||
assert torch.equal(output, reference)
|
||||
|
||||
storage_pointer = output.untyped_storage().data_ptr()
|
||||
assert output[1].untyped_storage().data_ptr() == storage_pointer
|
||||
assert output.detach().untyped_storage().data_ptr() == storage_pointer
|
||||
assert output.reshape(6, 4, 5).untyped_storage().data_ptr() == storage_pointer
|
||||
assert output.clone().untyped_storage().data_ptr() != storage_pointer
|
||||
|
||||
del output
|
||||
gc.collect()
|
||||
result = manager.collect_ready()
|
||||
assert result.reclaimed == 1
|
||||
assert result.deferred == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_lifetime_waits_for_last_storage_alias_and_not_root_tensor(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
with manager.allocate((4, 3, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.copy_(torch.arange(24, dtype=torch.float32).reshape(4, 3, 2))
|
||||
record = allocation.record
|
||||
output = allocation.publish()
|
||||
|
||||
view = output[1:]
|
||||
cache = {"images": view}
|
||||
root_reference = weakref.ref(output)
|
||||
del output
|
||||
gc.collect()
|
||||
|
||||
assert root_reference() is None
|
||||
assert float(cache["images"].sum()) == pytest.approx(261.0)
|
||||
first = manager.collect_ready()
|
||||
assert first.reclaimed == 0
|
||||
assert first.deferred == 1
|
||||
assert record.data_path.exists()
|
||||
assert record.metadata_path.exists()
|
||||
|
||||
del view
|
||||
cache.clear()
|
||||
gc.collect()
|
||||
second = manager.collect_ready()
|
||||
assert second.reclaimed == 1
|
||||
assert second.deferred == 0
|
||||
assert not record.data_path.exists()
|
||||
assert not record.metadata_path.exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exception_type", [RuntimeError, MODULE.ProbeCancelled])
|
||||
def test_unpublished_exception_and_cancel_remove_provisional_files(
|
||||
tmp_path,
|
||||
exception_type,
|
||||
):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
with pytest.raises(exception_type):
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.fill_(1.0)
|
||||
raise exception_type("stop before publish")
|
||||
|
||||
assert manager.tracked_count() == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_mapping_creation_failure_removes_reserved_file(tmp_path, monkeypatch):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
|
||||
def fail_from_file(*args, **kwargs):
|
||||
raise RuntimeError("synthetic mapping failure")
|
||||
|
||||
monkeypatch.setattr(MODULE.torch, "from_file", fail_from_file)
|
||||
with pytest.raises(RuntimeError, match="synthetic mapping failure"):
|
||||
manager.allocate((2, 2), dtype=torch.float32)
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_temporary_unlink_failure_preserves_cancel_and_is_retried(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
original_unlink = MODULE._unlink_known
|
||||
fail_unlink = {"enabled": True}
|
||||
|
||||
def sharing_violation(root, path):
|
||||
if fail_unlink["enabled"]:
|
||||
raise PermissionError(32, "synthetic sharing violation", str(path))
|
||||
return original_unlink(root, path)
|
||||
|
||||
monkeypatch.setattr(MODULE, "_unlink_known", sharing_violation)
|
||||
with pytest.raises(MODULE.ProbeCancelled, match="original cancel"):
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.fill_(1.0)
|
||||
raise MODULE.ProbeCancelled("original cancel")
|
||||
|
||||
assert manager.pending_count() == 1
|
||||
assert len(_known_files(tmp_path)) == 2
|
||||
fail_unlink["enabled"] = False
|
||||
result = manager.collect_ready()
|
||||
assert result.reclaimed == 1
|
||||
assert result.deferred == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_cancel_after_backing_file_create_does_not_leave_orphan(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
original_close = MODULE.os.close
|
||||
injected = False
|
||||
sentinel_path = tmp_path / "unrelated-live-descriptor.bin"
|
||||
sentinel_descriptor = None
|
||||
|
||||
def cancel_after_real_close(descriptor):
|
||||
nonlocal injected, sentinel_descriptor
|
||||
original_close(descriptor)
|
||||
if not injected:
|
||||
injected = True
|
||||
opened = MODULE.os.open(
|
||||
sentinel_path,
|
||||
MODULE.os.O_CREAT | MODULE.os.O_EXCL | MODULE.os.O_RDWR,
|
||||
0o600,
|
||||
)
|
||||
if opened != descriptor:
|
||||
MODULE.os.dup2(opened, descriptor)
|
||||
original_close(opened)
|
||||
opened = descriptor
|
||||
sentinel_descriptor = opened
|
||||
raise MODULE.ProbeCancelled("cancel after backing file create")
|
||||
|
||||
try:
|
||||
monkeypatch.setattr(MODULE.os, "close", cancel_after_real_close)
|
||||
with pytest.raises(MODULE.ProbeCancelled, match="after backing file create"):
|
||||
manager.allocate((2, 2), dtype=torch.float32)
|
||||
finally:
|
||||
monkeypatch.setattr(MODULE.os, "close", original_close)
|
||||
|
||||
assert sentinel_descriptor is not None
|
||||
MODULE.os.write(sentinel_descriptor, b"still-open")
|
||||
original_close(sentinel_descriptor)
|
||||
assert sentinel_path.read_bytes() == b"still-open"
|
||||
manager.collect_ready()
|
||||
assert manager.pending_count() == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_dropped_unpublished_allocation_is_finalized(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
allocation = manager.allocate((2, 2), dtype=torch.float32)
|
||||
allocation.tensor.fill_(3.0)
|
||||
allocation_reference = weakref.ref(allocation)
|
||||
del allocation
|
||||
gc.collect()
|
||||
|
||||
assert allocation_reference() is None
|
||||
manager.collect_ready()
|
||||
assert manager.pending_count() == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_cache_requeue_keeps_backing_file_count_bounded(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
cache = {}
|
||||
|
||||
for index in range(20):
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.fill_(float(index))
|
||||
output = allocation.publish()
|
||||
cache["same_node_output"] = output
|
||||
del output
|
||||
gc.collect()
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.bin"))) <= 2
|
||||
|
||||
same_requeue = cache["same_node_output"]
|
||||
assert same_requeue is cache["same_node_output"]
|
||||
manager.collect_ready()
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.bin"))) == 1
|
||||
cache.clear()
|
||||
del same_requeue
|
||||
gc.collect()
|
||||
result = manager.collect_ready()
|
||||
assert result.reclaimed == 1
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_published_downstream_error_keeps_cache_then_reclaims_after_eviction(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
cache = {}
|
||||
try:
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.fill_(4.0)
|
||||
output = allocation.publish()
|
||||
cache["assembler_output"] = output
|
||||
raise RuntimeError("downstream node failed")
|
||||
except RuntimeError:
|
||||
pass
|
||||
|
||||
del output
|
||||
gc.collect()
|
||||
held = manager.collect_ready()
|
||||
assert held.reclaimed == 0
|
||||
assert held.deferred == 1
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.bin"))) == 1
|
||||
|
||||
cache.clear()
|
||||
gc.collect()
|
||||
released = manager.collect_ready()
|
||||
assert released.reclaimed == 1
|
||||
assert released.deferred == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_process_global_manager_survives_node_local_scope(tmp_path):
|
||||
first = MODULE.get_process_lifetime_manager(tmp_path)
|
||||
second = MODULE.get_process_lifetime_manager(tmp_path)
|
||||
assert first is second
|
||||
|
||||
with first.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
output = allocation.publish()
|
||||
manager_reference = weakref.ref(first)
|
||||
del first
|
||||
del second
|
||||
gc.collect()
|
||||
assert manager_reference() is not None
|
||||
|
||||
del output
|
||||
gc.collect()
|
||||
manager = MODULE.get_process_lifetime_manager(tmp_path)
|
||||
assert manager.collect_ready().reclaimed == 1
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_stale_child_files_are_reclaimed_but_live_and_unknown_files_are_kept(tmp_path):
|
||||
subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
str(MODULE_PATH),
|
||||
"--root",
|
||||
str(tmp_path),
|
||||
"--crash-child",
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.bin"))) == 1
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.json"))) == 1
|
||||
|
||||
unknown = tmp_path / "unrelated.bin"
|
||||
unknown.write_bytes(b"do not delete")
|
||||
outside = tmp_path.parent / "outside.bin"
|
||||
outside.write_bytes(b"outside root")
|
||||
malicious_id = "f" * 32
|
||||
malicious_metadata = tmp_path / f"{MODULE.FILE_PREFIX}{malicious_id}.json"
|
||||
malicious_data = tmp_path / f"{MODULE.FILE_PREFIX}{malicious_id}.bin"
|
||||
malicious_data.write_bytes(b"protected")
|
||||
malicious_metadata.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"schema": MODULE.SCHEMA,
|
||||
"schema_version": MODULE.SCHEMA_VERSION,
|
||||
"allocation_id": malicious_id,
|
||||
"data_file": "..\\outside.bin",
|
||||
"owner_pid": -1,
|
||||
"created_at": 0,
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = MODULE.cleanup_stale_files(tmp_path, grace_seconds=0.0)
|
||||
assert result.removed_pairs == 1
|
||||
assert result.invalid_metadata == 1
|
||||
assert unknown.read_bytes() == b"do not delete"
|
||||
assert outside.read_bytes() == b"outside root"
|
||||
assert malicious_data.read_bytes() == b"protected"
|
||||
assert malicious_metadata.exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field", "value", "remove"),
|
||||
[
|
||||
("owner_pid", None, True),
|
||||
("owner_pid", -1, False),
|
||||
("owner_create_time", float("nan"), False),
|
||||
("created_at", float("nan"), False),
|
||||
],
|
||||
)
|
||||
def test_invalid_ownership_metadata_never_deletes_live_mapping(
|
||||
tmp_path,
|
||||
field,
|
||||
value,
|
||||
remove,
|
||||
):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
record = allocation.record
|
||||
output = allocation.publish()
|
||||
|
||||
payload = json.loads(record.metadata_path.read_text(encoding="utf-8"))
|
||||
if remove:
|
||||
payload.pop(field)
|
||||
else:
|
||||
payload[field] = value
|
||||
record.metadata_path.write_text(json.dumps(payload), encoding="utf-8")
|
||||
|
||||
result = MODULE.cleanup_stale_files(tmp_path, grace_seconds=0.0)
|
||||
assert result.invalid_metadata == 1
|
||||
assert result.removed_pairs == 0
|
||||
assert record.data_path.exists()
|
||||
assert record.metadata_path.exists()
|
||||
assert float(output.sum()) == pytest.approx(0.0)
|
||||
|
||||
del output
|
||||
gc.collect()
|
||||
assert manager.collect_ready().reclaimed == 1
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_cleanup_waits_for_in_progress_allocation_root_lock(tmp_path):
|
||||
allocation_entered = threading.Event()
|
||||
allow_allocation = threading.Event()
|
||||
output_holder = []
|
||||
cleanup_holder = []
|
||||
|
||||
def allocation_hook(_path):
|
||||
allocation_entered.set()
|
||||
assert allow_allocation.wait(timeout=5.0)
|
||||
|
||||
manager = MODULE.StorageLifetimeManager(
|
||||
tmp_path,
|
||||
allocation_hook=allocation_hook,
|
||||
)
|
||||
|
||||
def allocate_output():
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
output_holder.append(allocation.publish())
|
||||
|
||||
def cleanup():
|
||||
cleanup_holder.append(
|
||||
MODULE.cleanup_stale_files(tmp_path, grace_seconds=0.0)
|
||||
)
|
||||
|
||||
allocation_thread = threading.Thread(target=allocate_output)
|
||||
allocation_thread.start()
|
||||
assert allocation_entered.wait(timeout=5.0)
|
||||
cleanup_thread = threading.Thread(target=cleanup)
|
||||
cleanup_thread.start()
|
||||
assert cleanup_thread.is_alive()
|
||||
allow_allocation.set()
|
||||
allocation_thread.join(timeout=5.0)
|
||||
cleanup_thread.join(timeout=5.0)
|
||||
|
||||
assert not allocation_thread.is_alive()
|
||||
assert not cleanup_thread.is_alive()
|
||||
assert cleanup_holder[0].retained_live == 1
|
||||
assert len(list(tmp_path.glob(f"{MODULE.FILE_PREFIX}*.bin"))) == 1
|
||||
|
||||
output_holder.clear()
|
||||
gc.collect()
|
||||
assert manager.collect_ready().reclaimed == 1
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_root_lock_is_reentrant_when_finalizer_runs_during_allocation(tmp_path):
|
||||
first_manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
abandoned = first_manager.allocate((2, 2), dtype=torch.float32)
|
||||
cycle = [abandoned]
|
||||
cycle.append(cycle)
|
||||
del abandoned
|
||||
del cycle
|
||||
|
||||
def collect_abandoned_allocation(_path):
|
||||
gc.collect()
|
||||
|
||||
second_manager = MODULE.StorageLifetimeManager(
|
||||
tmp_path,
|
||||
allocation_hook=collect_abandoned_allocation,
|
||||
)
|
||||
started = time.perf_counter()
|
||||
with second_manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
output = allocation.publish()
|
||||
elapsed = time.perf_counter() - started
|
||||
|
||||
assert elapsed < 2.0
|
||||
first_manager.collect_ready()
|
||||
del output
|
||||
gc.collect()
|
||||
second_manager.collect_ready()
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_cleanup_collect_and_gc_finalizer_have_no_lock_order_deadlock(tmp_path):
|
||||
subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
str(MODULE_PATH),
|
||||
"--root",
|
||||
str(tmp_path),
|
||||
"--lock-order-child",
|
||||
],
|
||||
check=True,
|
||||
timeout=10.0,
|
||||
)
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_live_sidecar_is_retained_and_cleanup_is_idempotent(tmp_path):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
with manager.allocate((2, 2), dtype=torch.float32) as allocation:
|
||||
allocation.tensor.fill_(2.0)
|
||||
record = allocation.record
|
||||
output = allocation.publish()
|
||||
|
||||
live = MODULE.cleanup_stale_files(tmp_path, grace_seconds=0.0)
|
||||
assert live.retained_live == 1
|
||||
assert record.data_path.exists()
|
||||
assert record.metadata_path.exists()
|
||||
|
||||
del output
|
||||
gc.collect()
|
||||
assert manager.collect_ready().reclaimed == 1
|
||||
empty = MODULE.cleanup_stale_files(tmp_path, grace_seconds=0.0)
|
||||
assert empty.removed_pairs == 0
|
||||
assert empty.removed_orphans == 0
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_orphan_and_partial_cleanup_respects_grace_period(tmp_path):
|
||||
allocation_id = "a" * 32
|
||||
orphan = tmp_path / f"{MODULE.FILE_PREFIX}{allocation_id}.bin"
|
||||
partial = tmp_path / f"{MODULE.FILE_PREFIX}{allocation_id}.json.partial"
|
||||
orphan.write_bytes(b"orphan")
|
||||
partial.write_text("partial", encoding="utf-8")
|
||||
now = time.time()
|
||||
|
||||
young = MODULE.cleanup_stale_files(
|
||||
tmp_path,
|
||||
grace_seconds=60.0,
|
||||
now=now,
|
||||
)
|
||||
assert young.retained_young == 2
|
||||
assert orphan.exists()
|
||||
assert partial.exists()
|
||||
|
||||
old = MODULE.cleanup_stale_files(
|
||||
tmp_path,
|
||||
grace_seconds=60.0,
|
||||
now=now + 61.0,
|
||||
)
|
||||
assert old.removed_orphans == 2
|
||||
assert not orphan.exists()
|
||||
assert not partial.exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shape", [(2.5, 2), (True, 2), ("2", 2), ()])
|
||||
def test_shape_contract_rejects_lossy_or_empty_dimensions(tmp_path, shape):
|
||||
manager = MODULE.StorageLifetimeManager(tmp_path)
|
||||
with pytest.raises((TypeError, ValueError)):
|
||||
manager.allocate(shape, dtype=torch.float32)
|
||||
assert not _known_files(tmp_path)
|
||||
|
||||
|
||||
def test_standalone_probe_passes(tmp_path):
|
||||
result = MODULE.run_probe(tmp_path)
|
||||
assert result.bit_exact
|
||||
assert result.file_size == result.expected_bytes
|
||||
assert result.root_released_while_alias_alive
|
||||
assert result.deferred_while_alias_alive == 1
|
||||
assert result.reclaimed_after_last_alias == 1
|
||||
assert result.files_remaining == 0
|
||||
@@ -0,0 +1,503 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
DIAGNOSTICS_BASIC,
|
||||
DIAGNOSTICS_FULL,
|
||||
DIAGNOSTICS_OFF,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join import hardening
|
||||
from ComfyUI_H3_Continuum_Join.hardening import enrich_assembly_plan
|
||||
from ComfyUI_H3_Continuum_Join.v3 import memory_attribution as memory
|
||||
from ComfyUI_H3_Continuum_Join.v3 import driving_nodes as driving_nodes_module
|
||||
from ComfyUI_H3_Continuum_Join.v3.driving_nodes import (
|
||||
H3ContinuumAssembleSeamV34,
|
||||
H3ContinuumAssembleSeamV35,
|
||||
NODE_CLASS_MAPPINGS,
|
||||
NODE_DISPLAY_NAME_MAPPINGS,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import ASSEMBLY_PLAN_MAGIC
|
||||
|
||||
|
||||
def _terminal_plan():
|
||||
plan = enrich_assembly_plan(
|
||||
{
|
||||
"magic": ASSEMBLY_PLAN_MAGIC,
|
||||
"schema_version": 1,
|
||||
"fps": 24,
|
||||
"width": 96,
|
||||
"height": 64,
|
||||
"chunk_seconds": 5.0,
|
||||
"target_frames": 360,
|
||||
"preserve_final_frame": True,
|
||||
"chunks": [
|
||||
{
|
||||
"sequence_index": 1,
|
||||
"chunk_index": 1,
|
||||
"total_frames": 124,
|
||||
"trim_frames": 0,
|
||||
"net_frames": 124,
|
||||
"context_frames": 0,
|
||||
"expected_video_latent_t": 37,
|
||||
"expected_audio_latent_t": 207,
|
||||
},
|
||||
{
|
||||
"sequence_index": 2,
|
||||
"chunk_index": 2,
|
||||
"total_frames": 141,
|
||||
"trim_frames": 22,
|
||||
"net_frames": 119,
|
||||
"context_frames": 22,
|
||||
"expected_video_latent_t": 42,
|
||||
"expected_audio_latent_t": 235,
|
||||
},
|
||||
{
|
||||
"sequence_index": 3,
|
||||
"chunk_index": 3,
|
||||
"total_frames": 141,
|
||||
"trim_frames": 22,
|
||||
"net_frames": 119,
|
||||
"context_frames": 22,
|
||||
"expected_video_latent_t": 42,
|
||||
"expected_audio_latent_t": 235,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
plan["decode_groups"] = [
|
||||
{
|
||||
"logical_chunk_indices": [1],
|
||||
"terminal_merged": False,
|
||||
"total_frames": 124,
|
||||
"trim_frames": 0,
|
||||
"net_frames": 124,
|
||||
"context_frames": 0,
|
||||
"expected_video_latent_t": 37,
|
||||
"expected_audio_latent_t": 207,
|
||||
"frame_start": 0,
|
||||
"frame_stop": 124,
|
||||
},
|
||||
{
|
||||
"logical_chunk_indices": [2, 3],
|
||||
"terminal_merged": True,
|
||||
"total_frames": 260,
|
||||
"trim_frames": 22,
|
||||
"net_frames": 238,
|
||||
"context_frames": 22,
|
||||
"expected_video_latent_t": 77,
|
||||
"expected_audio_latent_t": 433,
|
||||
"frame_start": 124,
|
||||
"frame_stop": 362,
|
||||
},
|
||||
]
|
||||
plan["physical_decode_group_count"] = 2
|
||||
return plan
|
||||
|
||||
|
||||
def _decoded(*, image_dtype=torch.float16, device=None):
|
||||
images = [
|
||||
torch.empty((124, 4, 6, 3), dtype=image_dtype, device=device),
|
||||
torch.empty((260, 4, 6, 3), dtype=image_dtype, device=device),
|
||||
]
|
||||
audio = [
|
||||
{
|
||||
"waveform": torch.empty(
|
||||
(1, 2, 500000),
|
||||
dtype=torch.float32,
|
||||
device=device,
|
||||
),
|
||||
"sample_rate": 32000,
|
||||
},
|
||||
{
|
||||
"waveform": torch.empty(
|
||||
(1, 2, 500000),
|
||||
dtype=torch.float32,
|
||||
device=device,
|
||||
),
|
||||
"sample_rate": 32000,
|
||||
},
|
||||
]
|
||||
return images, audio
|
||||
|
||||
|
||||
def test_snapshot_is_cpu_safe_read_only_and_deduplicates_retained_storage(
|
||||
monkeypatch,
|
||||
):
|
||||
video_storage = torch.arange(24, dtype=torch.float32)
|
||||
audio_storage = torch.arange(12, dtype=torch.float32)
|
||||
entries = [
|
||||
{"video": video_storage[:16], "audio": audio_storage[:8]},
|
||||
{"video": video_storage[8:], "audio": audio_storage[4:]},
|
||||
]
|
||||
monkeypatch.setattr(memory.torch.cuda, "is_available", lambda: False)
|
||||
for name in (
|
||||
"memory_allocated",
|
||||
"memory_reserved",
|
||||
"max_memory_allocated",
|
||||
"synchronize",
|
||||
"reset_peak_memory_stats",
|
||||
"empty_cache",
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
memory.torch.cuda,
|
||||
name,
|
||||
lambda *args, _name=name, **kwargs: (_ for _ in ()).throw(
|
||||
AssertionError(f"unexpected CUDA call: {_name}")
|
||||
),
|
||||
)
|
||||
|
||||
line = memory.format_attribution_snapshot(
|
||||
"test",
|
||||
retained_entries=entries,
|
||||
)
|
||||
assert f"retained_video={video_storage.untyped_storage().nbytes()} bytes" in line
|
||||
assert f"retained_audio={audio_storage.untyped_storage().nbytes()} bytes" in line
|
||||
assert "CUDA_allocated=n/a" in line
|
||||
assert "CUDA_peak_allocated_since_external_reset=n/a" in line
|
||||
|
||||
|
||||
def test_snapshot_counter_failure_is_reported_as_unavailable(monkeypatch):
|
||||
monkeypatch.setattr(memory, "_process_counters", lambda: (None, None))
|
||||
monkeypatch.setattr(memory, "_cuda_counters", lambda: (None, None, None))
|
||||
line = memory.format_attribution_snapshot("failure-safe")
|
||||
assert "RSS=n/a" in line
|
||||
assert "system_available=n/a" in line
|
||||
assert "CUDA_reserved=n/a" in line
|
||||
|
||||
|
||||
def test_snapshot_counts_refine_context_storage_once():
|
||||
shared = torch.arange(32, dtype=torch.float32)
|
||||
refine_context = {
|
||||
"groups": (
|
||||
{
|
||||
"conditioning": (
|
||||
(shared[:8], {"reference": shared[8:16]}),
|
||||
),
|
||||
"first_image": shared[16:24],
|
||||
},
|
||||
),
|
||||
"alias": shared[4:28],
|
||||
}
|
||||
|
||||
line = memory.format_attribution_snapshot(
|
||||
"refine-context",
|
||||
retained_refine_context=refine_context,
|
||||
)
|
||||
|
||||
expected = shared.untyped_storage().nbytes()
|
||||
assert f"retained_refine_context_CPU={expected} bytes" in line
|
||||
assert "retained_refine_context_GPU=0 bytes" in line
|
||||
|
||||
|
||||
def test_cuda_snapshot_reads_counters_without_allocator_mutation(monkeypatch):
|
||||
monkeypatch.setattr(memory.torch.cuda, "is_available", lambda: True)
|
||||
monkeypatch.setattr(memory.torch.cuda, "memory_allocated", lambda: 11)
|
||||
monkeypatch.setattr(memory.torch.cuda, "memory_reserved", lambda: 22)
|
||||
monkeypatch.setattr(memory.torch.cuda, "max_memory_allocated", lambda: 33)
|
||||
for name in (
|
||||
"synchronize",
|
||||
"reset_peak_memory_stats",
|
||||
"empty_cache",
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
memory.torch.cuda,
|
||||
name,
|
||||
lambda *args, _name=name, **kwargs: (_ for _ in ()).throw(
|
||||
AssertionError(f"unexpected CUDA mutation: {_name}")
|
||||
),
|
||||
)
|
||||
|
||||
line = memory.format_attribution_snapshot("cuda-read-only")
|
||||
|
||||
assert "CUDA_allocated=0.0 MiB" in line
|
||||
assert "CUDA_reserved=0.0 MiB" in line
|
||||
assert "CUDA_peak_allocated_since_external_reset=0.0 MiB" in line
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method",
|
||||
(
|
||||
"capture_sequence_start",
|
||||
"capture_group",
|
||||
"capture_sequence_complete",
|
||||
),
|
||||
)
|
||||
def test_each_collector_stage_is_fail_soft(method):
|
||||
class ExplodingCollector:
|
||||
def __init__(self):
|
||||
self.lines = []
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name.startswith("capture_"):
|
||||
return lambda **_kwargs: (_ for _ in ()).throw(
|
||||
RuntimeError(f"{name} failed")
|
||||
)
|
||||
raise AttributeError(name)
|
||||
|
||||
collector = ExplodingCollector()
|
||||
memory.capture_attribution_fail_soft(collector, method)
|
||||
|
||||
assert collector.lines == [
|
||||
f"Memory attribution [{method}]: unavailable "
|
||||
f"(RuntimeError: {method} failed)"
|
||||
]
|
||||
|
||||
|
||||
def test_sequence_start_and_complete_failures_do_not_replace_sampling_result():
|
||||
class ExplodingCollector:
|
||||
def __init__(self):
|
||||
self.lines = []
|
||||
|
||||
def capture_sequence_start(self):
|
||||
raise RuntimeError("start failed")
|
||||
|
||||
def capture_sequence_complete(self, **_kwargs):
|
||||
raise RuntimeError("complete failed")
|
||||
|
||||
collector = ExplodingCollector()
|
||||
entries = [{"video": torch.zeros(1), "audio": torch.zeros(1)}]
|
||||
session = {"chunks": entries}
|
||||
refine_context = {"groups": ()}
|
||||
base_result = (entries, "state", session, "base report", refine_context)
|
||||
base_calls = []
|
||||
|
||||
def base(
|
||||
*,
|
||||
video_vae,
|
||||
latent_only,
|
||||
diagnostics_mode,
|
||||
memory_attribution,
|
||||
_memory_attribution_collector=None,
|
||||
):
|
||||
base_calls.append(_memory_attribution_collector)
|
||||
return base_result
|
||||
|
||||
result = hardening.run_sequence_with_hardening(
|
||||
base,
|
||||
(),
|
||||
{
|
||||
"video_vae": object(),
|
||||
"latent_only": True,
|
||||
"diagnostics_mode": DIAGNOSTICS_FULL,
|
||||
"memory_attribution": True,
|
||||
"_memory_attribution_collector": collector,
|
||||
},
|
||||
)
|
||||
|
||||
assert base_calls == [collector]
|
||||
assert result[:3] == base_result[:3]
|
||||
assert result[4] is refine_context
|
||||
assert "base report" in result[3]
|
||||
assert "capture_sequence_start" in result[3]
|
||||
assert "capture_sequence_complete" in result[3]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("diagnostics", (DIAGNOSTICS_BASIC, DIAGNOSTICS_OFF))
|
||||
def test_v35_attribution_is_absent_outside_detailed_report(diagnostics):
|
||||
base_result = ("entries", "state", "session", "base report", "context")
|
||||
calls = []
|
||||
|
||||
def base(
|
||||
*,
|
||||
video_vae,
|
||||
latent_only,
|
||||
diagnostics_mode,
|
||||
memory_attribution,
|
||||
_memory_attribution_collector=None,
|
||||
):
|
||||
calls.append(_memory_attribution_collector)
|
||||
return base_result
|
||||
|
||||
result = hardening.run_sequence_with_hardening(
|
||||
base,
|
||||
(),
|
||||
{
|
||||
"video_vae": object(),
|
||||
"latent_only": True,
|
||||
"diagnostics_mode": diagnostics,
|
||||
"memory_attribution": True,
|
||||
},
|
||||
)
|
||||
|
||||
assert result is base_result
|
||||
assert calls == [None]
|
||||
assert "Memory [V3.5" not in result[3]
|
||||
|
||||
|
||||
def test_v34_private_flag_off_keeps_existing_detailed_report_path():
|
||||
entries = [{"video": torch.zeros(1), "audio": torch.zeros(1)}]
|
||||
session = {"chunks": entries}
|
||||
calls = []
|
||||
|
||||
def base(
|
||||
*,
|
||||
video_vae,
|
||||
latent_only,
|
||||
diagnostics_mode,
|
||||
memory_attribution=False,
|
||||
_memory_attribution_collector=None,
|
||||
):
|
||||
calls.append((memory_attribution, _memory_attribution_collector))
|
||||
return entries, "state", session, "base report"
|
||||
|
||||
result = hardening.run_sequence_with_hardening(
|
||||
base,
|
||||
(),
|
||||
{
|
||||
"video_vae": object(),
|
||||
"latent_only": True,
|
||||
"diagnostics_mode": DIAGNOSTICS_FULL,
|
||||
"memory_attribution": False,
|
||||
},
|
||||
)
|
||||
|
||||
assert calls == [(False, None)]
|
||||
assert result[0] is entries
|
||||
assert result[2] is session
|
||||
assert "Memory [sequence start]" in result[3]
|
||||
assert "Memory [sequence complete]" in result[3]
|
||||
assert "Memory [V3.5" not in result[3]
|
||||
|
||||
|
||||
def test_projection_uses_physical_groups_actual_dtypes_and_exact_target():
|
||||
images, audio = _decoded()
|
||||
off = memory.project_assembly_buffers(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=_terminal_plan(),
|
||||
exact_total_duration=False,
|
||||
)
|
||||
assert off.natural_frames == 362
|
||||
assert off.target_frames == 362
|
||||
assert off.natural_image_bytes == 52128
|
||||
assert off.natural_audio_bytes == 3861336
|
||||
assert off.final_image_bytes == 52128
|
||||
assert off.final_audio_bytes == 3861336
|
||||
assert off.frame_count_adjustment_required is False
|
||||
|
||||
exact = memory.project_assembly_buffers(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=_terminal_plan(),
|
||||
exact_total_duration=True,
|
||||
)
|
||||
assert exact.natural_frames == 362
|
||||
assert exact.target_frames == 360
|
||||
assert exact.natural_image_bytes == 52128
|
||||
assert exact.natural_audio_bytes == 3861336
|
||||
assert exact.final_image_bytes == 51840
|
||||
assert exact.final_audio_bytes == 3840000
|
||||
assert exact.frame_count_adjustment_required is True
|
||||
|
||||
|
||||
def test_projection_uses_python_int_for_real_1152_square_output():
|
||||
images, audio = _decoded(device="meta")
|
||||
images = [
|
||||
torch.empty((124, 1152, 1152, 3), dtype=torch.float32, device="meta"),
|
||||
torch.empty((260, 1152, 1152, 3), dtype=torch.float32, device="meta"),
|
||||
]
|
||||
projection = memory.project_assembly_buffers(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=_terminal_plan(),
|
||||
exact_total_duration=True,
|
||||
)
|
||||
assert projection.final_image_bytes == 5733089280
|
||||
assert isinstance(projection.final_image_bytes, int)
|
||||
|
||||
|
||||
def test_v35_assembler_adds_projection_only_for_detailed_report(monkeypatch):
|
||||
input_images, input_audio = _decoded()
|
||||
output_images = torch.zeros((1, 1, 1, 3))
|
||||
output_audio = {"waveform": torch.zeros((1, 2, 1)), "sample_rate": 32000}
|
||||
base_calls = []
|
||||
|
||||
def fake_v35_assemble(**kwargs):
|
||||
base_calls.append(kwargs)
|
||||
return (
|
||||
output_images,
|
||||
output_audio,
|
||||
"base report\nMemory [assemble preflight]: test\n"
|
||||
"Memory [assemble complete]: test",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
driving_nodes_module,
|
||||
"assemble_decoded_chunks_v35",
|
||||
fake_v35_assemble,
|
||||
)
|
||||
node = H3ContinuumAssembleSeamV35()
|
||||
detailed_images, detailed_audio, detailed_report = node.assemble(
|
||||
images=input_images,
|
||||
audio=input_audio,
|
||||
assembly_plan=_terminal_plan(),
|
||||
exact_total_duration=True,
|
||||
audio_seam="Auto",
|
||||
video_seam="Auto",
|
||||
buffer_backend="RAM",
|
||||
diagnostics=DIAGNOSTICS_FULL,
|
||||
)
|
||||
assert detailed_images is output_images
|
||||
assert detailed_audio is output_audio
|
||||
assert "Projected assembly buffers [V3.5]" in detailed_report
|
||||
assert detailed_report.index("Memory [assemble preflight]") < detailed_report.index(
|
||||
"Projected assembly buffers [V3.5]"
|
||||
)
|
||||
assert detailed_report.index("Projected assembly buffers [V3.5]") < (
|
||||
detailed_report.index("Memory [assemble complete]")
|
||||
)
|
||||
|
||||
_, _, basic_report = node.assemble(
|
||||
images=input_images,
|
||||
audio=input_audio,
|
||||
assembly_plan=_terminal_plan(),
|
||||
exact_total_duration=True,
|
||||
audio_seam="Auto",
|
||||
video_seam="Auto",
|
||||
buffer_backend="RAM",
|
||||
diagnostics=DIAGNOSTICS_BASIC,
|
||||
)
|
||||
assert "Projected assembly buffers [V3.5]" not in basic_report
|
||||
assert len(base_calls) == 2
|
||||
|
||||
|
||||
def test_v35_assembler_projection_failure_never_replaces_base_execution(monkeypatch):
|
||||
output_images = torch.zeros((1, 1, 1, 3))
|
||||
output_audio = {"waveform": torch.zeros((1, 2, 1)), "sample_rate": 32000}
|
||||
monkeypatch.setattr(
|
||||
driving_nodes_module,
|
||||
"assemble_decoded_chunks_v35",
|
||||
lambda **kwargs: (output_images, output_audio, "base report"),
|
||||
)
|
||||
images, audio, report = H3ContinuumAssembleSeamV35().assemble(
|
||||
images=[],
|
||||
audio=[],
|
||||
assembly_plan={},
|
||||
exact_total_duration=True,
|
||||
audio_seam="Auto",
|
||||
video_seam="Auto",
|
||||
buffer_backend="RAM",
|
||||
diagnostics=DIAGNOSTICS_FULL,
|
||||
)
|
||||
assert images is output_images
|
||||
assert audio is output_audio
|
||||
assert "Projected assembly buffers [V3.5]: unavailable" in report
|
||||
|
||||
|
||||
def test_v35_assembler_is_registered_without_replacing_v34():
|
||||
from ComfyUI_H3_Continuum_Join import nodes as root_nodes
|
||||
|
||||
assert NODE_CLASS_MAPPINGS["H3ContinuumAssembleSeamV34"] is (
|
||||
H3ContinuumAssembleSeamV34
|
||||
)
|
||||
assert NODE_CLASS_MAPPINGS["H3ContinuumAssembleSeamV35"] is (
|
||||
H3ContinuumAssembleSeamV35
|
||||
)
|
||||
assert root_nodes.NODE_CLASS_MAPPINGS["H3ContinuumAssembleSeamV35"] is (
|
||||
H3ContinuumAssembleSeamV35
|
||||
)
|
||||
assert NODE_DISPLAY_NAME_MAPPINGS["H3ContinuumAssembleSeamV35"] == (
|
||||
"H3 Continuum Assemble + Seam V3.5"
|
||||
)
|
||||
@@ -0,0 +1,269 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import gc
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v3 import file_backed_buffer as buffer
|
||||
|
||||
|
||||
def _managed_files(root: Path) -> list[Path]:
|
||||
return sorted(
|
||||
path
|
||||
for path in root.iterdir()
|
||||
if path.suffix in {".bin", ".json", ".partial"}
|
||||
)
|
||||
|
||||
|
||||
def test_backend_contract_accepts_auto_and_rejects_unknown_values():
|
||||
assert buffer.BUFFER_BACKEND_OPTIONS == (
|
||||
buffer.BUFFER_BACKEND_AUTO,
|
||||
buffer.BUFFER_BACKEND_RAM,
|
||||
buffer.BUFFER_BACKEND_DISK,
|
||||
)
|
||||
assert buffer.validate_buffer_backend("Auto") == "Auto"
|
||||
assert buffer.validate_buffer_backend("RAM") == "RAM"
|
||||
assert buffer.validate_buffer_backend("Disk-backed") == "Disk-backed"
|
||||
for value in ("disk-backed", "", None):
|
||||
with pytest.raises(ValueError, match="unknown Buffer Backend"):
|
||||
buffer.validate_buffer_backend(value)
|
||||
|
||||
|
||||
def test_auto_selects_ram_only_with_size_and_memory_headroom(tmp_path, monkeypatch):
|
||||
gib = 1024**3
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"_system_memory_bytes",
|
||||
lambda: (16 * gib, 64 * gib),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"_disk_free_bytes",
|
||||
lambda _path: (_ for _ in ()).throw(
|
||||
AssertionError("disk must not be queried for a safe RAM decision")
|
||||
),
|
||||
)
|
||||
|
||||
decision = buffer.select_auto_buffer_backend(gib, backing_root=tmp_path)
|
||||
|
||||
assert decision.selected_backend == buffer.BUFFER_BACKEND_RAM
|
||||
assert decision.memory_reserve_bytes == int(64 * gib * 0.10)
|
||||
assert decision.disk_free_bytes is None
|
||||
assert "fits" in decision.reason
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("final_bytes", "available", "reason"),
|
||||
(
|
||||
(5 * 1024**3, 48 * 1024**3, "exceeds"),
|
||||
(1 * 1024**3, 5 * 1024**3, "below"),
|
||||
),
|
||||
)
|
||||
def test_auto_selects_disk_for_large_output_or_memory_pressure(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
final_bytes,
|
||||
available,
|
||||
reason,
|
||||
):
|
||||
gib = 1024**3
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"_system_memory_bytes",
|
||||
lambda: (available, 64 * gib),
|
||||
)
|
||||
monkeypatch.setattr(buffer, "_disk_free_bytes", lambda _path: 100 * gib)
|
||||
|
||||
decision = buffer.select_auto_buffer_backend(
|
||||
final_bytes,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
assert decision.selected_backend == buffer.BUFFER_BACKEND_DISK
|
||||
assert decision.disk_free_bytes == 100 * gib
|
||||
assert reason in decision.reason
|
||||
|
||||
|
||||
def test_auto_uses_disk_when_memory_counters_are_unavailable(tmp_path, monkeypatch):
|
||||
gib = 1024**3
|
||||
monkeypatch.setattr(buffer, "_system_memory_bytes", lambda: (None, None))
|
||||
monkeypatch.setattr(buffer, "_disk_free_bytes", lambda _path: 100 * gib)
|
||||
|
||||
decision = buffer.select_auto_buffer_backend(gib, backing_root=tmp_path)
|
||||
|
||||
assert decision.selected_backend == buffer.BUFFER_BACKEND_DISK
|
||||
assert decision.memory_reserve_bytes is None
|
||||
assert "unavailable" in decision.reason
|
||||
|
||||
|
||||
def test_auto_disk_shortage_is_explicit_and_never_falls_back(tmp_path, monkeypatch):
|
||||
gib = 1024**3
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"_system_memory_bytes",
|
||||
lambda: (48 * gib, 64 * gib),
|
||||
)
|
||||
monkeypatch.setattr(buffer, "_disk_free_bytes", lambda _path: 6 * gib)
|
||||
|
||||
with pytest.raises(RuntimeError, match="selected Disk-backed"):
|
||||
buffer.select_auto_buffer_backend(5 * gib, backing_root=tmp_path)
|
||||
|
||||
|
||||
def test_auto_decision_report_records_selection_inputs(tmp_path, monkeypatch):
|
||||
gib = 1024**3
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"_system_memory_bytes",
|
||||
lambda: (16 * gib, 64 * gib),
|
||||
)
|
||||
decision = buffer.select_auto_buffer_backend(gib, backing_root=tmp_path)
|
||||
|
||||
report = buffer.format_auto_buffer_decision(decision)
|
||||
|
||||
assert "selected=RAM" in report
|
||||
assert f"final_IMAGE={gib} bytes" in report
|
||||
assert "RAM_IMAGE_limit=4294967296 bytes" in report
|
||||
assert "reason=" in report
|
||||
|
||||
|
||||
def test_default_root_is_resolved_lazily_from_comfyui_temp(tmp_path, monkeypatch):
|
||||
calls = []
|
||||
comfy_temp = tmp_path / "comfy-temp"
|
||||
fake_folder_paths = SimpleNamespace(
|
||||
get_temp_directory=lambda: calls.append(True) or str(comfy_temp)
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "folder_paths", fake_folder_paths)
|
||||
|
||||
assert calls == []
|
||||
resolved = buffer.resolve_backing_root()
|
||||
assert calls == [True]
|
||||
assert resolved == (comfy_temp / "h3-continuum-v35-file-backed").resolve()
|
||||
|
||||
|
||||
def test_default_root_has_a_non_comfy_test_fallback(tmp_path, monkeypatch):
|
||||
monkeypatch.setitem(sys.modules, "folder_paths", None)
|
||||
monkeypatch.setattr(buffer.tempfile, "gettempdir", lambda: str(tmp_path))
|
||||
|
||||
assert buffer.resolve_backing_root() == (
|
||||
tmp_path / "h3-continuum-v35-file-backed"
|
||||
).resolve()
|
||||
|
||||
|
||||
def test_explicit_root_bypasses_comfyui_resolution(tmp_path, monkeypatch):
|
||||
fake_folder_paths = SimpleNamespace(
|
||||
get_temp_directory=lambda: (_ for _ in ()).throw(
|
||||
AssertionError("folder_paths must not be evaluated")
|
||||
)
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "folder_paths", fake_folder_paths)
|
||||
|
||||
assert buffer.resolve_backing_root(tmp_path) == tmp_path.resolve()
|
||||
|
||||
|
||||
def test_file_backed_allocation_uses_process_global_manager_and_alias_lifetime(
|
||||
tmp_path,
|
||||
):
|
||||
manager = buffer.get_file_backed_image_manager(tmp_path)
|
||||
assert manager is buffer.get_file_backed_image_manager(tmp_path)
|
||||
reference = torch.arange(4 * 3 * 2 * 3, dtype=torch.float32).reshape(4, 3, 2, 3)
|
||||
|
||||
with buffer.allocate_file_backed_image(
|
||||
tuple(reference.shape),
|
||||
dtype=reference.dtype,
|
||||
backing_root=tmp_path,
|
||||
) as allocation:
|
||||
allocation.tensor.copy_(reference)
|
||||
record = allocation.record
|
||||
output = allocation.publish()
|
||||
|
||||
assert output.device.type == "cpu"
|
||||
assert output.is_contiguous()
|
||||
assert torch.equal(output, reference)
|
||||
assert record.data_path.parent == tmp_path.resolve()
|
||||
assert record.data_path.stat().st_size == reference.numel() * reference.element_size()
|
||||
assert record.data_path.exists()
|
||||
assert record.metadata_path.exists()
|
||||
|
||||
alias = output[1:]
|
||||
del output
|
||||
gc.collect()
|
||||
held = manager.collect_ready()
|
||||
assert held.reclaimed == 0
|
||||
assert held.deferred == 1
|
||||
assert float(alias.sum()) == pytest.approx(float(reference[1:].sum()))
|
||||
assert record.data_path.exists()
|
||||
|
||||
del alias
|
||||
gc.collect()
|
||||
released = manager.collect_ready()
|
||||
assert released.reclaimed == 1
|
||||
assert released.deferred == 0
|
||||
assert not _managed_files(tmp_path)
|
||||
|
||||
|
||||
def test_unpublished_error_aborts_provisional_files(tmp_path):
|
||||
manager = buffer.get_file_backed_image_manager(tmp_path)
|
||||
|
||||
with pytest.raises(RuntimeError, match="assembly failed"):
|
||||
with buffer.allocate_file_backed_image(
|
||||
(2, 2, 2, 3),
|
||||
dtype=torch.float32,
|
||||
backing_root=tmp_path,
|
||||
) as allocation:
|
||||
allocation.tensor.fill_(1.0)
|
||||
raise RuntimeError("assembly failed")
|
||||
|
||||
assert manager.tracked_count() == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not _managed_files(tmp_path)
|
||||
|
||||
|
||||
def test_disk_backend_failure_is_propagated_without_ram_fallback(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
class FailingManager:
|
||||
def allocate(self, *args, **kwargs):
|
||||
raise OSError("synthetic disk failure")
|
||||
|
||||
monkeypatch.setattr(
|
||||
buffer,
|
||||
"get_file_backed_image_manager",
|
||||
lambda backing_root=None: FailingManager(),
|
||||
)
|
||||
with pytest.raises(OSError, match="synthetic disk failure"):
|
||||
buffer.allocate_file_backed_image(
|
||||
(2, 2, 2, 3),
|
||||
dtype=torch.float32,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
|
||||
def test_first_manager_access_reclaims_prior_dead_process_files(tmp_path):
|
||||
probe = (
|
||||
Path(__file__).resolve().parents[1]
|
||||
/ "tools"
|
||||
/ "p20_file_backed_tensor_poc.py"
|
||||
)
|
||||
subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
str(probe),
|
||||
"--root",
|
||||
str(tmp_path),
|
||||
"--crash-child",
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
assert list(tmp_path.glob("*.bin"))
|
||||
assert list(tmp_path.glob("*.json"))
|
||||
|
||||
buffer.get_file_backed_image_manager(tmp_path)
|
||||
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
@@ -0,0 +1,796 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import gc
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
DIAGNOSTICS_BASIC,
|
||||
DIAGNOSTICS_FULL,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.hardening import enrich_assembly_plan
|
||||
from ComfyUI_H3_Continuum_Join.temporal import audio_latent_t, video_latent_t
|
||||
from ComfyUI_H3_Continuum_Join.v3 import assembly_v35
|
||||
from ComfyUI_H3_Continuum_Join.v3.assembly import (
|
||||
AUDIO_SEAM_AUTO,
|
||||
AUDIO_SEAM_OFF,
|
||||
VIDEO_SEAM_ANALYZE,
|
||||
VIDEO_SEAM_AUTO,
|
||||
VIDEO_SEAM_AUTO_2,
|
||||
VIDEO_SEAM_OFF,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.driving_nodes import (
|
||||
H3ContinuumAssembleSeamV34,
|
||||
H3ContinuumAssembleSeamV35,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.file_backed_buffer import (
|
||||
AutoBufferDecision,
|
||||
BUFFER_BACKEND_AUTO,
|
||||
BUFFER_BACKEND_DISK,
|
||||
BUFFER_BACKEND_RAM,
|
||||
get_file_backed_image_manager,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import ASSEMBLY_PLAN_MAGIC
|
||||
|
||||
|
||||
_FPS = 24
|
||||
_SAMPLE_RATE = 240
|
||||
|
||||
|
||||
def _logical_chunk(
|
||||
sequence_index: int,
|
||||
*,
|
||||
total_frames: int,
|
||||
trim_frames: int,
|
||||
) -> dict:
|
||||
return {
|
||||
"sequence_index": sequence_index,
|
||||
"chunk_index": sequence_index,
|
||||
"total_frames": total_frames,
|
||||
"trim_frames": trim_frames,
|
||||
"net_frames": total_frames - trim_frames,
|
||||
"context_frames": trim_frames,
|
||||
"expected_video_latent_t": video_latent_t(total_frames),
|
||||
"expected_audio_latent_t": audio_latent_t(total_frames),
|
||||
}
|
||||
|
||||
|
||||
def _physical_group(
|
||||
logical_chunks: list[int],
|
||||
*,
|
||||
total_frames: int,
|
||||
trim_frames: int,
|
||||
frame_start: int,
|
||||
terminal_merged: bool,
|
||||
) -> dict:
|
||||
net_frames = total_frames - trim_frames
|
||||
return {
|
||||
"logical_chunk_indices": logical_chunks,
|
||||
"terminal_merged": terminal_merged,
|
||||
"total_frames": total_frames,
|
||||
"trim_frames": trim_frames,
|
||||
"net_frames": net_frames,
|
||||
"context_frames": trim_frames,
|
||||
"expected_video_latent_t": video_latent_t(total_frames),
|
||||
"expected_audio_latent_t": audio_latent_t(total_frames),
|
||||
"frame_start": frame_start,
|
||||
"frame_stop": frame_start + net_frames,
|
||||
}
|
||||
|
||||
|
||||
def _plan(kind: str, *, target_frames: int | None = None, preserve=True) -> dict:
|
||||
if kind == "single":
|
||||
chunks = [_logical_chunk(1, total_frames=124, trim_frames=0)]
|
||||
groups = None
|
||||
default_target = 120
|
||||
elif kind == "single_pad":
|
||||
chunks = [_logical_chunk(1, total_frames=124, trim_frames=5)]
|
||||
groups = None
|
||||
default_target = 120
|
||||
elif kind == "terminal_2x5":
|
||||
chunks = [
|
||||
_logical_chunk(1, total_frames=124, trim_frames=0),
|
||||
_logical_chunk(2, total_frames=141, trim_frames=22),
|
||||
]
|
||||
groups = [
|
||||
_physical_group(
|
||||
[1, 2],
|
||||
total_frames=243,
|
||||
trim_frames=0,
|
||||
frame_start=0,
|
||||
terminal_merged=True,
|
||||
)
|
||||
]
|
||||
default_target = 240
|
||||
elif kind == "long_terminal_3x5":
|
||||
chunks = [
|
||||
_logical_chunk(1, total_frames=124, trim_frames=0),
|
||||
_logical_chunk(2, total_frames=141, trim_frames=22),
|
||||
_logical_chunk(3, total_frames=141, trim_frames=22),
|
||||
]
|
||||
groups = [
|
||||
_physical_group(
|
||||
[1],
|
||||
total_frames=124,
|
||||
trim_frames=0,
|
||||
frame_start=0,
|
||||
terminal_merged=False,
|
||||
),
|
||||
_physical_group(
|
||||
[2, 3],
|
||||
total_frames=260,
|
||||
trim_frames=22,
|
||||
frame_start=124,
|
||||
terminal_merged=True,
|
||||
),
|
||||
]
|
||||
default_target = 360
|
||||
else:
|
||||
raise AssertionError(f"unknown fixture kind: {kind}")
|
||||
|
||||
plan = enrich_assembly_plan(
|
||||
{
|
||||
"magic": ASSEMBLY_PLAN_MAGIC,
|
||||
"schema_version": 1,
|
||||
"fps": _FPS,
|
||||
"width": 8,
|
||||
"height": 8,
|
||||
"chunk_seconds": 5.0,
|
||||
"target_frames": int(
|
||||
default_target if target_frames is None else target_frames
|
||||
),
|
||||
"preserve_final_frame": bool(preserve),
|
||||
"chunks": chunks,
|
||||
}
|
||||
)
|
||||
if groups is not None:
|
||||
plan["decode_groups"] = groups
|
||||
plan["physical_decode_group_count"] = len(groups)
|
||||
return plan
|
||||
|
||||
|
||||
def _units(plan: dict) -> list[dict]:
|
||||
return list(plan.get("decode_groups") or plan["chunks"])
|
||||
|
||||
|
||||
def _decoded(
|
||||
plan: dict,
|
||||
*,
|
||||
seam_case: str = "ordered",
|
||||
sample_rate: int = _SAMPLE_RATE,
|
||||
) -> tuple[list[torch.Tensor], list[dict]]:
|
||||
images: list[torch.Tensor] = []
|
||||
audio: list[dict] = []
|
||||
for group_index, unit in enumerate(_units(plan)):
|
||||
total_frames = int(unit["total_frames"])
|
||||
if seam_case == "ordered":
|
||||
frame_values = (
|
||||
group_index * 0.25
|
||||
+ torch.arange(total_frames, dtype=torch.float32) / 4096.0
|
||||
)
|
||||
image = frame_values.view(-1, 1, 1, 1).expand(-1, 8, 8, 3).clone()
|
||||
else:
|
||||
image = torch.full((total_frames, 8, 8, 3), 0.25)
|
||||
if group_index == 1:
|
||||
trim = int(unit["trim_frames"])
|
||||
if seam_case == "transient":
|
||||
image[trim] = 1.0
|
||||
elif seam_case == "exposure":
|
||||
image[trim:] = 0.33
|
||||
image[trim : trim + 4] = torch.tensor(
|
||||
[0.27, 0.29, 0.31, 0.33]
|
||||
).view(4, 1, 1, 1)
|
||||
elif seam_case != "clean":
|
||||
raise AssertionError(f"unknown seam case: {seam_case}")
|
||||
|
||||
sample_count = int(round(total_frames / _FPS * sample_rate))
|
||||
time_values = (
|
||||
group_index * 10000
|
||||
+ torch.arange(sample_count, dtype=torch.float32)
|
||||
)
|
||||
waveform = torch.stack((time_values, -time_values), dim=0).unsqueeze(0)
|
||||
images.append(image)
|
||||
audio.append({"waveform": waveform, "sample_rate": sample_rate})
|
||||
return images, audio
|
||||
|
||||
|
||||
def _v34(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
*,
|
||||
exact: bool,
|
||||
audio_seam: str = AUDIO_SEAM_OFF,
|
||||
video_seam: str = VIDEO_SEAM_OFF,
|
||||
):
|
||||
return H3ContinuumAssembleSeamV34().assemble(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=plan,
|
||||
exact_total_duration=exact,
|
||||
audio_seam=audio_seam,
|
||||
video_seam=video_seam,
|
||||
diagnostics=DIAGNOSTICS_BASIC,
|
||||
)
|
||||
|
||||
|
||||
def _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
*,
|
||||
exact: bool,
|
||||
backend: str,
|
||||
backing_root: Path,
|
||||
audio_seam: str = AUDIO_SEAM_OFF,
|
||||
video_seam: str = VIDEO_SEAM_OFF,
|
||||
):
|
||||
return assembly_v35.assemble_decoded_chunks_v35(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=plan,
|
||||
exact_total_duration=exact,
|
||||
audio_seam=audio_seam,
|
||||
video_seam=video_seam,
|
||||
diagnostics=DIAGNOSTICS_BASIC,
|
||||
buffer_backend=backend,
|
||||
backing_root=backing_root,
|
||||
)
|
||||
|
||||
|
||||
def _assert_av_equal(left, right) -> None:
|
||||
assert torch.equal(left[0], right[0])
|
||||
assert left[1]["sample_rate"] == right[1]["sample_rate"]
|
||||
assert torch.equal(left[1]["waveform"], right[1]["waveform"])
|
||||
|
||||
|
||||
def _collect_released_disk_tensor(root: Path) -> None:
|
||||
gc.collect()
|
||||
get_file_backed_image_manager(root).collect_ready()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("kind", "exact", "expected_frames"),
|
||||
(
|
||||
("single", False, 124),
|
||||
("single", True, 120),
|
||||
("terminal_2x5", False, 243),
|
||||
("terminal_2x5", True, 240),
|
||||
("long_terminal_3x5", False, 362),
|
||||
("long_terminal_3x5", True, 360),
|
||||
),
|
||||
)
|
||||
def test_ram_and_disk_are_bit_exact_with_v34_required_layouts(
|
||||
tmp_path,
|
||||
kind,
|
||||
exact,
|
||||
expected_frames,
|
||||
):
|
||||
plan = _plan(kind)
|
||||
images, audio = _decoded(plan)
|
||||
baseline = _v34(images, audio, plan, exact=exact)
|
||||
ram = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
backend=BUFFER_BACKEND_RAM,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
disk = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
assert int(baseline[0].shape[0]) == expected_frames
|
||||
_assert_av_equal(ram, baseline)
|
||||
_assert_av_equal(disk, baseline)
|
||||
disk_image = disk[0]
|
||||
del disk
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("video_seam", "seam_case"),
|
||||
(
|
||||
(VIDEO_SEAM_OFF, "transient"),
|
||||
(VIDEO_SEAM_ANALYZE, "transient"),
|
||||
(VIDEO_SEAM_AUTO, "transient"),
|
||||
(VIDEO_SEAM_AUTO_2, "exposure"),
|
||||
),
|
||||
)
|
||||
@pytest.mark.parametrize("backend", (BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK))
|
||||
@pytest.mark.parametrize("exact", (False, True))
|
||||
def test_all_video_seam_modes_are_bit_exact_with_v34(
|
||||
tmp_path,
|
||||
video_seam,
|
||||
seam_case,
|
||||
backend,
|
||||
exact,
|
||||
):
|
||||
plan = _plan("long_terminal_3x5")
|
||||
images, audio = _decoded(plan, seam_case=seam_case)
|
||||
baseline = _v34(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
video_seam=video_seam,
|
||||
)
|
||||
candidate = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
backend=backend,
|
||||
backing_root=tmp_path,
|
||||
video_seam=video_seam,
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
if backend == BUFFER_BACKEND_DISK:
|
||||
disk_image = candidate[0]
|
||||
del candidate
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("backend", (BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK))
|
||||
@pytest.mark.parametrize("exact", (False, True))
|
||||
def test_audio_seam_auto_is_bit_exact_with_v34(tmp_path, backend, exact):
|
||||
plan = _plan("long_terminal_3x5")
|
||||
images, audio = _decoded(plan, seam_case="clean")
|
||||
previous_samples = int(round(124 / _FPS * _SAMPLE_RATE))
|
||||
context_samples = int(round(22 / _FPS * _SAMPLE_RATE))
|
||||
first_time = torch.arange(previous_samples, dtype=torch.float32)
|
||||
second_count = int(round(260 / _FPS * _SAMPLE_RATE))
|
||||
second_time = torch.arange(
|
||||
previous_samples - context_samples,
|
||||
previous_samples - context_samples + second_count,
|
||||
dtype=torch.float32,
|
||||
)
|
||||
first_wave = torch.sin(first_time * (2.0 * torch.pi / 31.0))
|
||||
second_wave = torch.sin(second_time * (2.0 * torch.pi / 31.0))
|
||||
audio[0]["waveform"] = first_wave.view(1, 1, -1).expand(1, 2, -1).clone()
|
||||
audio[1]["waveform"] = second_wave.view(1, 1, -1).expand(1, 2, -1).clone()
|
||||
baseline = _v34(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
audio_seam=AUDIO_SEAM_AUTO,
|
||||
)
|
||||
candidate = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
backend=backend,
|
||||
backing_root=tmp_path,
|
||||
audio_seam=AUDIO_SEAM_AUTO,
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
assert "audio seam 1->2" in baseline[2]
|
||||
assert "fade=0 samples" not in baseline[2]
|
||||
assert "fallback to native boundary" not in baseline[2]
|
||||
if backend == BUFFER_BACKEND_DISK:
|
||||
disk_image = candidate[0]
|
||||
del candidate
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("backend", (BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK))
|
||||
@pytest.mark.parametrize("exact", (False, True))
|
||||
def test_32khz_cumulative_rounding_and_audio_seam_match_v34(
|
||||
tmp_path,
|
||||
backend,
|
||||
exact,
|
||||
):
|
||||
plan = _plan("long_terminal_3x5")
|
||||
images, audio = _decoded(plan, seam_case="clean", sample_rate=32000)
|
||||
baseline = _v34(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
audio_seam=AUDIO_SEAM_AUTO,
|
||||
)
|
||||
candidate = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=exact,
|
||||
backend=backend,
|
||||
backing_root=tmp_path,
|
||||
audio_seam=AUDIO_SEAM_AUTO,
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
expected_frames = 360 if exact else 362
|
||||
assert candidate[1]["waveform"].shape[-1] == int(
|
||||
round(expected_frames / _FPS * 32000)
|
||||
)
|
||||
if backend == BUFFER_BACKEND_DISK:
|
||||
disk_image = candidate[0]
|
||||
del candidate
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("backend", (BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK))
|
||||
def test_padding_and_final_frame_preservation_match_v34(tmp_path, backend):
|
||||
trim_plan = _plan("single", target_frames=120, preserve=True)
|
||||
trim_images, trim_audio = _decoded(trim_plan)
|
||||
trim_baseline = _v34(trim_images, trim_audio, trim_plan, exact=True)
|
||||
trim_candidate = _v35(
|
||||
trim_images,
|
||||
trim_audio,
|
||||
trim_plan,
|
||||
exact=True,
|
||||
backend=backend,
|
||||
backing_root=tmp_path / "trim",
|
||||
)
|
||||
_assert_av_equal(trim_candidate, trim_baseline)
|
||||
assert torch.equal(trim_candidate[0][-1], trim_images[0][-1])
|
||||
|
||||
pad_plan = _plan("single_pad", preserve=False)
|
||||
pad_images, pad_audio = _decoded(pad_plan)
|
||||
pad_baseline = _v34(pad_images, pad_audio, pad_plan, exact=True)
|
||||
pad_candidate = _v35(
|
||||
pad_images,
|
||||
pad_audio,
|
||||
pad_plan,
|
||||
exact=True,
|
||||
backend=backend,
|
||||
backing_root=tmp_path / "pad",
|
||||
)
|
||||
_assert_av_equal(pad_candidate, pad_baseline)
|
||||
assert torch.equal(pad_candidate[0][-1], pad_images[0][-1])
|
||||
|
||||
if backend == BUFFER_BACKEND_DISK:
|
||||
trim_image = trim_candidate[0]
|
||||
pad_image = pad_candidate[0]
|
||||
del trim_candidate, pad_candidate
|
||||
del trim_image, pad_image
|
||||
_collect_released_disk_tensor(tmp_path / "trim")
|
||||
_collect_released_disk_tensor(tmp_path / "pad")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("backend", (BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK))
|
||||
def test_tail_trim_without_final_anchor_matches_v34(tmp_path, backend):
|
||||
plan = _plan("single", target_frames=120, preserve=False)
|
||||
images, audio = _decoded(plan)
|
||||
baseline = _v34(images, audio, plan, exact=True)
|
||||
candidate = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=True,
|
||||
backend=backend,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
assert torch.equal(candidate[0][-1], images[0][119])
|
||||
assert not torch.equal(candidate[0][-1], images[0][-1])
|
||||
if backend == BUFFER_BACKEND_DISK:
|
||||
disk_image = candidate[0]
|
||||
del candidate
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
def test_long_terminal_uses_physical_group_order_and_trim_contract(tmp_path):
|
||||
plan = _plan("long_terminal_3x5")
|
||||
images, audio = _decoded(plan)
|
||||
result = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=False,
|
||||
backend=BUFFER_BACKEND_RAM,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
assert result[0].shape[0] == 362
|
||||
assert torch.equal(result[0][:124], images[0])
|
||||
assert torch.equal(result[0][124:], images[1][22:260])
|
||||
expected_samples = int(round(362 / _FPS * _SAMPLE_RATE))
|
||||
assert result[1]["waveform"].shape[-1] == expected_samples
|
||||
|
||||
|
||||
def test_disk_storage_alias_keeps_backing_file_alive_until_last_view(tmp_path):
|
||||
plan = _plan("single")
|
||||
images, audio = _decoded(plan)
|
||||
output, _, _ = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=True,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
manager = get_file_backed_image_manager(tmp_path)
|
||||
managed = list(tmp_path.glob("*.bin"))
|
||||
assert len(managed) == 1
|
||||
assert managed[0].stat().st_size == output.numel() * output.element_size()
|
||||
|
||||
alias = output[1:]
|
||||
del output
|
||||
gc.collect()
|
||||
held = manager.collect_ready()
|
||||
assert held.reclaimed == 0
|
||||
assert held.deferred == 1
|
||||
assert managed[0].exists()
|
||||
assert torch.equal(alias[0], images[0][1])
|
||||
|
||||
del alias
|
||||
gc.collect()
|
||||
released = manager.collect_ready()
|
||||
assert released.reclaimed == 1
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
|
||||
|
||||
def test_disk_write_error_aborts_unpublished_allocation(tmp_path, monkeypatch):
|
||||
plan = _plan("single")
|
||||
images, audio = _decoded(plan)
|
||||
original = assembly_v35._copy_natural_interval
|
||||
|
||||
def fail_after_allocation(*args, **kwargs):
|
||||
original(*args, **kwargs)
|
||||
raise RuntimeError("synthetic direct-write failure")
|
||||
|
||||
monkeypatch.setattr(assembly_v35, "_copy_natural_interval", fail_after_allocation)
|
||||
with pytest.raises(RuntimeError, match="synthetic direct-write failure"):
|
||||
_v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=True,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
|
||||
manager = get_file_backed_image_manager(tmp_path)
|
||||
assert manager.tracked_count() == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
|
||||
|
||||
def test_invalid_driving_audio_aborts_before_disk_publish(tmp_path, monkeypatch):
|
||||
plan = _plan("single")
|
||||
images, audio = _decoded(plan)
|
||||
original_allocate = assembly_v35.allocate_file_backed_image
|
||||
|
||||
def allocate_at_test_root(shape, *, dtype, backing_root=None):
|
||||
return original_allocate(shape, dtype=dtype, backing_root=tmp_path)
|
||||
|
||||
monkeypatch.setattr(
|
||||
assembly_v35,
|
||||
"allocate_file_backed_image",
|
||||
allocate_at_test_root,
|
||||
)
|
||||
invalid_driving = {
|
||||
"waveform": torch.zeros((1, 1, 1240), dtype=torch.float32),
|
||||
"sample_rate": 0,
|
||||
}
|
||||
with pytest.raises(ValueError, match="sample_rate"):
|
||||
H3ContinuumAssembleSeamV35().assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
True,
|
||||
AUDIO_SEAM_OFF,
|
||||
VIDEO_SEAM_OFF,
|
||||
BUFFER_BACKEND_DISK,
|
||||
DIAGNOSTICS_BASIC,
|
||||
driving_audio=invalid_driving,
|
||||
)
|
||||
|
||||
manager = get_file_backed_image_manager(tmp_path)
|
||||
assert manager.tracked_count() == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
|
||||
|
||||
def test_auto_backend_is_default_while_manual_values_remain_available():
|
||||
schema = H3ContinuumAssembleSeamV35.INPUT_TYPES()
|
||||
backend_options = schema["required"]["buffer_backend"][0]
|
||||
backend_config = schema["required"]["buffer_backend"][1]
|
||||
assert tuple(backend_options) == (
|
||||
BUFFER_BACKEND_AUTO,
|
||||
BUFFER_BACKEND_RAM,
|
||||
BUFFER_BACKEND_DISK,
|
||||
)
|
||||
assert backend_config["default"] == BUFFER_BACKEND_AUTO
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"selected_backend",
|
||||
(BUFFER_BACKEND_RAM, BUFFER_BACKEND_DISK),
|
||||
)
|
||||
def test_auto_backend_uses_selected_writer_and_reports_decision(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
selected_backend,
|
||||
):
|
||||
gib = 1024**3
|
||||
plan = _plan("single")
|
||||
images, audio = _decoded(plan)
|
||||
decision = AutoBufferDecision(
|
||||
selected_backend=selected_backend,
|
||||
final_image_bytes=120 * 8 * 8 * 3 * 4,
|
||||
backing_root=tmp_path,
|
||||
available_memory_bytes=16 * gib,
|
||||
total_memory_bytes=64 * gib,
|
||||
memory_reserve_bytes=int(64 * gib * 0.10),
|
||||
ram_image_limit_bytes=4 * gib,
|
||||
disk_free_bytes=(100 * gib if selected_backend == BUFFER_BACKEND_DISK else None),
|
||||
disk_reserve_bytes=2 * gib,
|
||||
reason="synthetic test decision",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
assembly_v35,
|
||||
"select_auto_buffer_backend",
|
||||
lambda final_image_bytes, backing_root=None: decision,
|
||||
)
|
||||
|
||||
candidate = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=True,
|
||||
backend=BUFFER_BACKEND_AUTO,
|
||||
backing_root=tmp_path,
|
||||
)
|
||||
baseline = _v35(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
exact=True,
|
||||
backend=selected_backend,
|
||||
backing_root=tmp_path / "manual",
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
assert f"Buffer Backend: Auto -> {selected_backend}" in candidate[2]
|
||||
assert f"selected={selected_backend}" in candidate[2]
|
||||
assert "reason=synthetic test decision" in candidate[2]
|
||||
|
||||
candidate_image = candidate[0]
|
||||
baseline_image = baseline[0]
|
||||
del candidate, baseline, candidate_image, baseline_image
|
||||
if selected_backend == BUFFER_BACKEND_DISK:
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
_collect_released_disk_tensor(tmp_path / "manual")
|
||||
|
||||
|
||||
def test_direct_image_path_has_no_full_materialization_operations():
|
||||
source = inspect.getsource(assembly_v35)
|
||||
for forbidden in (
|
||||
"torch.cat(",
|
||||
"torch.stack(",
|
||||
".clone(",
|
||||
".contiguous(",
|
||||
"correct_decoded_boundaries",
|
||||
):
|
||||
assert forbidden not in source
|
||||
|
||||
|
||||
def test_small_patch_copy_intersects_prefix_and_final_anchor_spans_only():
|
||||
destination = torch.full((5, 1, 1, 1), -1.0)
|
||||
spans = (
|
||||
assembly_v35.CopySpan(0, 4, 0),
|
||||
assembly_v35.CopySpan(9, 10, 4),
|
||||
)
|
||||
crossing_patch = torch.tensor([30.0, 40.0, 50.0]).reshape(3, 1, 1, 1)
|
||||
assembly_v35._copy_natural_interval(
|
||||
destination,
|
||||
crossing_patch,
|
||||
natural_start=3,
|
||||
spans=spans,
|
||||
)
|
||||
anchor_patch = torch.tensor([90.0]).reshape(1, 1, 1, 1)
|
||||
assembly_v35._copy_natural_interval(
|
||||
destination,
|
||||
anchor_patch,
|
||||
natural_start=9,
|
||||
spans=spans,
|
||||
)
|
||||
|
||||
assert destination[:, 0, 0, 0].tolist() == [-1.0, -1.0, -1.0, 30.0, 90.0]
|
||||
|
||||
|
||||
def test_node_accepts_comfy_positional_list_inputs(tmp_path, monkeypatch):
|
||||
plan = _plan("single")
|
||||
images, audio = _decoded(plan)
|
||||
original_allocate = assembly_v35.allocate_file_backed_image
|
||||
|
||||
def allocate_at_test_root(shape, *, dtype, backing_root=None):
|
||||
return original_allocate(shape, dtype=dtype, backing_root=tmp_path)
|
||||
|
||||
monkeypatch.setattr(
|
||||
assembly_v35,
|
||||
"allocate_file_backed_image",
|
||||
allocate_at_test_root,
|
||||
)
|
||||
node = H3ContinuumAssembleSeamV35()
|
||||
result = node.assemble(
|
||||
images,
|
||||
audio,
|
||||
[plan],
|
||||
[True],
|
||||
[AUDIO_SEAM_OFF],
|
||||
[VIDEO_SEAM_OFF],
|
||||
[BUFFER_BACKEND_DISK],
|
||||
[DIAGNOSTICS_FULL],
|
||||
)
|
||||
baseline = _v34(images, audio, plan, exact=True)
|
||||
|
||||
assert node.INPUT_IS_LIST is True
|
||||
_assert_av_equal(result, baseline)
|
||||
assert "Buffer Backend: Disk-backed" in result[2]
|
||||
assert "Projected assembly buffers [V3.5]" in result[2]
|
||||
disk_image = result[0]
|
||||
del result
|
||||
del disk_image
|
||||
_collect_released_disk_tensor(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stored_in_plan", (False, True))
|
||||
def test_driving_audio_selection_is_bit_exact_with_v34(
|
||||
tmp_path,
|
||||
stored_in_plan,
|
||||
):
|
||||
plan = _plan("single", preserve=True)
|
||||
images, audio = _decoded(plan)
|
||||
driving_samples = int(round(124 / _FPS * _SAMPLE_RATE))
|
||||
driving = {
|
||||
"waveform": torch.arange(
|
||||
driving_samples,
|
||||
dtype=torch.float32,
|
||||
).view(1, 1, -1),
|
||||
"sample_rate": _SAMPLE_RATE,
|
||||
}
|
||||
direct = None if stored_in_plan else driving
|
||||
if stored_in_plan:
|
||||
plan = dict(plan)
|
||||
plan["_h3_continuum_driving_audio_v1"] = driving
|
||||
|
||||
baseline = H3ContinuumAssembleSeamV34().assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
True,
|
||||
AUDIO_SEAM_AUTO,
|
||||
VIDEO_SEAM_OFF,
|
||||
DIAGNOSTICS_BASIC,
|
||||
driving_audio=direct,
|
||||
)
|
||||
candidate = H3ContinuumAssembleSeamV35().assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
True,
|
||||
AUDIO_SEAM_AUTO,
|
||||
VIDEO_SEAM_OFF,
|
||||
BUFFER_BACKEND_RAM,
|
||||
DIAGNOSTICS_BASIC,
|
||||
driving_audio=direct,
|
||||
)
|
||||
|
||||
_assert_av_equal(candidate, baseline)
|
||||
assert "Driving Audio: preserved source selected" in candidate[2]
|
||||
@@ -0,0 +1,187 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.hardening import enrich_assembly_plan
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import ASSEMBLY_PLAN_MAGIC
|
||||
from ComfyUI_H3_Continuum_Join.v3.video_seam import (
|
||||
analyze_decoded_boundaries,
|
||||
correct_decoded_boundaries,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.video_seam_v35 import (
|
||||
build_boundary_patches,
|
||||
)
|
||||
|
||||
|
||||
def _plan():
|
||||
return enrich_assembly_plan(
|
||||
{
|
||||
"magic": ASSEMBLY_PLAN_MAGIC,
|
||||
"schema_version": 1,
|
||||
"fps": 24,
|
||||
"width": 8,
|
||||
"height": 8,
|
||||
"chunk_seconds": 5.0,
|
||||
"target_frames": 240,
|
||||
"preserve_final_frame": True,
|
||||
"chunks": [
|
||||
{
|
||||
"sequence_index": 1,
|
||||
"chunk_index": 1,
|
||||
"total_frames": 124,
|
||||
"trim_frames": 0,
|
||||
"net_frames": 124,
|
||||
"context_frames": 0,
|
||||
"expected_video_latent_t": 37,
|
||||
"expected_audio_latent_t": 207,
|
||||
},
|
||||
{
|
||||
"sequence_index": 2,
|
||||
"chunk_index": 2,
|
||||
"total_frames": 141,
|
||||
"trim_frames": 22,
|
||||
"net_frames": 119,
|
||||
"context_frames": 22,
|
||||
"expected_video_latent_t": 42,
|
||||
"expected_audio_latent_t": 235,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _decoded(case: str) -> list[torch.Tensor]:
|
||||
images = [
|
||||
torch.full((124, 8, 8, 3), 0.25, dtype=torch.float32),
|
||||
torch.full((141, 8, 8, 3), 0.25, dtype=torch.float32),
|
||||
]
|
||||
if case == "transient":
|
||||
images[1][22] = 1.0
|
||||
elif case == "micro":
|
||||
images[1][22] = 0.258
|
||||
elif case == "exposure":
|
||||
images[1][22:] = 0.33
|
||||
images[1][22:26] = torch.tensor(
|
||||
[0.27, 0.29, 0.31, 0.33],
|
||||
dtype=torch.float32,
|
||||
).view(4, 1, 1, 1)
|
||||
elif case != "clean":
|
||||
raise AssertionError(f"unknown test case: {case}")
|
||||
return images
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("case", "enable_exposure_ramp", "expected_patch_frames"),
|
||||
(
|
||||
("transient", False, 1),
|
||||
("micro", False, 1),
|
||||
("exposure", False, 0),
|
||||
("exposure", True, 4),
|
||||
("clean", False, 0),
|
||||
),
|
||||
)
|
||||
def test_small_patches_are_bit_exact_with_v34_correction(
|
||||
case,
|
||||
enable_exposure_ramp,
|
||||
expected_patch_frames,
|
||||
):
|
||||
plan = _plan()
|
||||
images = _decoded(case)
|
||||
originals = [value.clone() for value in images]
|
||||
analyses = analyze_decoded_boundaries(images=images, assembly_plan=plan)
|
||||
|
||||
legacy, legacy_actions = correct_decoded_boundaries(
|
||||
images=images,
|
||||
assembly_plan=plan,
|
||||
analyses=analyses,
|
||||
enable_exposure_ramp=enable_exposure_ramp,
|
||||
)
|
||||
patches, actions = build_boundary_patches(
|
||||
images,
|
||||
plan,
|
||||
analyses,
|
||||
enable_exposure_ramp=enable_exposure_ramp,
|
||||
)
|
||||
|
||||
assert actions == legacy_actions
|
||||
assert all(torch.equal(value, original) for value, original in zip(images, originals))
|
||||
if expected_patch_frames == 0:
|
||||
assert patches == {}
|
||||
assert torch.equal(legacy[1], images[1])
|
||||
return
|
||||
|
||||
assert set(patches) == {1}
|
||||
patch = patches[1]
|
||||
assert patch.device.type == "cpu"
|
||||
assert patch.shape == (expected_patch_frames, 8, 8, 3)
|
||||
assert 1 <= int(patch.shape[0]) <= 4
|
||||
trim = int(plan["chunks"][1]["trim_frames"])
|
||||
assert torch.equal(
|
||||
patch,
|
||||
legacy[1][trim : trim + expected_patch_frames],
|
||||
)
|
||||
assert torch.equal(legacy[1][:trim], images[1][:trim])
|
||||
assert torch.equal(
|
||||
legacy[1][trim + expected_patch_frames :],
|
||||
images[1][trim + expected_patch_frames :],
|
||||
)
|
||||
|
||||
|
||||
def test_guarded_candidate_returns_native_action_without_a_patch():
|
||||
plan = _plan()
|
||||
images = _decoded("transient")
|
||||
analysis = analyze_decoded_boundaries(
|
||||
images=images,
|
||||
assembly_plan=plan,
|
||||
)[0]
|
||||
guarded = replace(analysis, scene_cut=True)
|
||||
|
||||
legacy, legacy_actions = correct_decoded_boundaries(
|
||||
images=images,
|
||||
assembly_plan=plan,
|
||||
analyses=[guarded],
|
||||
)
|
||||
patches, actions = build_boundary_patches(images, plan, [guarded])
|
||||
|
||||
assert patches == {}
|
||||
assert actions == legacy_actions == {
|
||||
1: "kept native boundary; correction guard rejected candidate"
|
||||
}
|
||||
assert legacy == images
|
||||
|
||||
|
||||
def test_patch_storage_is_independent_and_inputs_remain_unchanged():
|
||||
plan = _plan()
|
||||
images = _decoded("transient")
|
||||
original = images[1].clone()
|
||||
analysis = analyze_decoded_boundaries(
|
||||
images=images,
|
||||
assembly_plan=plan,
|
||||
)[0]
|
||||
|
||||
patches, _ = build_boundary_patches(images, plan, [analysis])
|
||||
patches[1].zero_()
|
||||
|
||||
assert torch.equal(images[1], original)
|
||||
|
||||
|
||||
def test_planning_error_does_not_mutate_inputs_or_return_a_partial_result():
|
||||
plan = _plan()
|
||||
images = _decoded("transient")
|
||||
originals = [value.clone() for value in images]
|
||||
analysis = analyze_decoded_boundaries(
|
||||
images=images,
|
||||
assembly_plan=plan,
|
||||
)[0]
|
||||
invalid = replace(analysis, boundary_index=2)
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="boundary index is outside decoded chunks",
|
||||
):
|
||||
build_boundary_patches(images, plan, [analysis, invalid])
|
||||
|
||||
assert all(torch.equal(value, original) for value, original in zip(images, originals))
|
||||
@@ -0,0 +1,179 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import gc
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import DIAGNOSTICS_BASIC
|
||||
from ComfyUI_H3_Continuum_Join.hardening import enrich_assembly_plan
|
||||
from ComfyUI_H3_Continuum_Join.temporal import audio_latent_t, video_latent_t
|
||||
from ComfyUI_H3_Continuum_Join.v3.assembly import (
|
||||
AUDIO_SEAM_OFF,
|
||||
VIDEO_SEAM_OFF,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.assembly_v35 import (
|
||||
assemble_decoded_chunks_v35,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.file_backed_buffer import (
|
||||
BUFFER_BACKEND_DISK,
|
||||
BUFFER_BACKEND_RAM,
|
||||
get_file_backed_image_manager,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import ASSEMBLY_PLAN_MAGIC
|
||||
|
||||
|
||||
class ProbeCancelled(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
def _fixture():
|
||||
total_frames = 124
|
||||
target_frames = 120
|
||||
sample_rate = 240
|
||||
chunk = {
|
||||
"sequence_index": 1,
|
||||
"chunk_index": 1,
|
||||
"total_frames": total_frames,
|
||||
"trim_frames": 0,
|
||||
"net_frames": total_frames,
|
||||
"context_frames": 0,
|
||||
"expected_video_latent_t": video_latent_t(total_frames),
|
||||
"expected_audio_latent_t": audio_latent_t(total_frames),
|
||||
}
|
||||
plan = enrich_assembly_plan(
|
||||
{
|
||||
"magic": ASSEMBLY_PLAN_MAGIC,
|
||||
"schema_version": 1,
|
||||
"fps": 24,
|
||||
"width": 8,
|
||||
"height": 8,
|
||||
"chunk_seconds": 5.0,
|
||||
"target_frames": target_frames,
|
||||
"preserve_final_frame": True,
|
||||
"chunks": [chunk],
|
||||
}
|
||||
)
|
||||
values = torch.arange(total_frames, dtype=torch.float32) / total_frames
|
||||
images = [values.reshape(-1, 1, 1, 1).expand(-1, 8, 8, 3).clone()]
|
||||
samples = int(round(total_frames / 24 * sample_rate))
|
||||
audio = [
|
||||
{
|
||||
"waveform": torch.linspace(0.0, 1.0, samples).reshape(1, 1, -1),
|
||||
"sample_rate": sample_rate,
|
||||
}
|
||||
]
|
||||
return images, audio, plan
|
||||
|
||||
|
||||
def _assemble(images, audio, plan, *, backend, root, interrupt_check):
|
||||
return assemble_decoded_chunks_v35(
|
||||
images=images,
|
||||
audio=audio,
|
||||
assembly_plan=plan,
|
||||
exact_total_duration=True,
|
||||
audio_seam=AUDIO_SEAM_OFF,
|
||||
video_seam=VIDEO_SEAM_OFF,
|
||||
diagnostics=DIAGNOSTICS_BASIC,
|
||||
buffer_backend=backend,
|
||||
backing_root=root,
|
||||
interrupt_check=interrupt_check,
|
||||
)
|
||||
|
||||
|
||||
def _collect(root, *values):
|
||||
del values
|
||||
gc.collect()
|
||||
manager = get_file_backed_image_manager(root)
|
||||
manager.collect_ready()
|
||||
gc.collect()
|
||||
manager.collect_ready()
|
||||
|
||||
|
||||
def test_interrupt_polling_keeps_ram_and_disk_outputs_bit_exact(tmp_path):
|
||||
images, audio, plan = _fixture()
|
||||
checks = 0
|
||||
|
||||
def check():
|
||||
nonlocal checks
|
||||
checks += 1
|
||||
|
||||
ram = _assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
backend=BUFFER_BACKEND_RAM,
|
||||
root=tmp_path,
|
||||
interrupt_check=check,
|
||||
)
|
||||
disk = _assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
root=tmp_path,
|
||||
interrupt_check=check,
|
||||
)
|
||||
|
||||
assert checks >= 20
|
||||
assert torch.equal(disk[0], ram[0])
|
||||
assert torch.equal(disk[1]["waveform"], ram[1]["waveform"])
|
||||
assert disk[1]["sample_rate"] == ram[1]["sample_rate"]
|
||||
|
||||
disk_image = disk[0]
|
||||
del disk
|
||||
del disk_image
|
||||
_collect(tmp_path)
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
|
||||
|
||||
def test_mid_copy_cancel_aborts_unpublished_disk_pair(tmp_path):
|
||||
images, audio, plan = _fixture()
|
||||
checks = 0
|
||||
|
||||
def cancel_during_copy():
|
||||
nonlocal checks
|
||||
checks += 1
|
||||
if checks == 4:
|
||||
raise ProbeCancelled("P2-4 synthetic ESC")
|
||||
|
||||
with pytest.raises(ProbeCancelled, match="synthetic ESC"):
|
||||
_assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
root=tmp_path,
|
||||
interrupt_check=cancel_during_copy,
|
||||
)
|
||||
|
||||
manager = get_file_backed_image_manager(tmp_path)
|
||||
assert checks == 4
|
||||
assert manager.tracked_count() == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
|
||||
|
||||
def test_cancel_immediately_after_allocation_aborts_disk_pair(tmp_path):
|
||||
images, audio, plan = _fixture()
|
||||
|
||||
def cancel_after_allocation():
|
||||
raise ProbeCancelled("P2-4 pre-copy ESC")
|
||||
|
||||
with pytest.raises(ProbeCancelled, match="pre-copy ESC"):
|
||||
_assemble(
|
||||
images,
|
||||
audio,
|
||||
plan,
|
||||
backend=BUFFER_BACKEND_DISK,
|
||||
root=tmp_path,
|
||||
interrupt_check=cancel_after_allocation,
|
||||
)
|
||||
|
||||
manager = get_file_backed_image_manager(tmp_path)
|
||||
assert manager.tracked_count() == 0
|
||||
assert manager.pending_count() == 0
|
||||
assert not list(tmp_path.glob("*.bin"))
|
||||
assert not list(tmp_path.glob("*.json"))
|
||||
@@ -0,0 +1,49 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def _probe_module():
|
||||
path = Path(__file__).resolve().parents[1] / "tools" / "p25_gpu_acceptance_probe.py"
|
||||
spec = importlib.util.spec_from_file_location("p25_gpu_acceptance_probe_test", path)
|
||||
assert spec is not None and spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_p25_bounded_finite_check_and_hash_are_deterministic():
|
||||
probe = _probe_module()
|
||||
value = torch.arange(48, dtype=torch.float32).reshape(4, 3, 4)
|
||||
|
||||
assert probe._tensor_is_finite_batched(value, batch=1) is True
|
||||
assert probe._hash_tensor(value, batch=1) == probe._hash_tensor(value, batch=3)
|
||||
|
||||
invalid = value.clone()
|
||||
invalid[2, 1, 1] = float("nan")
|
||||
assert probe._tensor_is_finite_batched(invalid, batch=1) is False
|
||||
|
||||
|
||||
def test_p25_stress_prompt_bypasses_vae_and_preserves_assembler_contract():
|
||||
probe = _probe_module()
|
||||
prompt = probe._prompt("Disk-backed", stress=True)
|
||||
|
||||
assert set(prompt) == {"1", "6", "7"}
|
||||
assert prompt["1"]["class_type"] == "P25SyntheticDecodedStressFixture"
|
||||
assert prompt["6"]["class_type"] == "H3ContinuumAssembleSeamV35"
|
||||
assert prompt["6"]["inputs"]["images"] == ["1", 0]
|
||||
assert prompt["6"]["inputs"]["audio"] == ["1", 1]
|
||||
assert prompt["6"]["inputs"]["assembly_plan"] == ["1", 2]
|
||||
assert prompt["6"]["inputs"]["buffer_backend"] == "Disk-backed"
|
||||
assert prompt["6"]["inputs"]["exact_total_duration"] is True
|
||||
assert prompt["6"]["inputs"]["video_seam"] == "Auto"
|
||||
|
||||
|
||||
def test_p25_process_memory_snapshot_reports_windows_commit_fields():
|
||||
probe = _probe_module()
|
||||
snapshot = probe._process_memory_snapshot()
|
||||
|
||||
assert snapshot["rss"] > 0
|
||||
assert snapshot["private"] > 0
|
||||
assert snapshot["uss"] > 0
|
||||
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import configparser
|
||||
from pathlib import Path
|
||||
import tomllib
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EXPECTED_VERSION = "3.5.0"
|
||||
|
||||
|
||||
def test_v35_release_metadata_is_consistent():
|
||||
version_source = (ROOT / "version.py").read_text(encoding="utf-8")
|
||||
pyproject = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))
|
||||
metadata = configparser.ConfigParser()
|
||||
metadata.read(ROOT / "metadata.ini", encoding="utf-8")
|
||||
|
||||
assert f'PACKAGE_VERSION = "{EXPECTED_VERSION}"' in version_source
|
||||
assert pyproject["project"]["version"] == EXPECTED_VERSION
|
||||
assert metadata["project"]["version"] == EXPECTED_VERSION
|
||||
|
||||
|
||||
def test_v35_public_documents_identify_the_current_release():
|
||||
readme = (ROOT / "README.md").read_text(encoding="utf-8")
|
||||
readme_ja = (ROOT / "README_JA.md").read_text(encoding="utf-8")
|
||||
changelog = (ROOT / "CHANGELOG.md").read_text(encoding="utf-8")
|
||||
|
||||
assert readme.startswith(f"# ComfyUI-H3-Continuum {EXPECTED_VERSION}\n")
|
||||
assert readme_ja.startswith(f"# ComfyUI-H3-Continuum {EXPECTED_VERSION}\n")
|
||||
assert f"## {EXPECTED_VERSION}" in changelog
|
||||
@@ -145,4 +145,4 @@ def test_v217_result_pack_expands_legacy_outputs():
|
||||
def test_v217_has_no_frontend_display_controller():
|
||||
assert not (ROOT / "web" / "h3_continuum_v2.js").exists()
|
||||
version_source = (ROOT / "version.py").read_text(encoding="utf-8")
|
||||
assert 'PACKAGE_VERSION = "3.4.0"' in version_source
|
||||
assert 'PACKAGE_VERSION = "3.5.0"' in version_source
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
PROJECT_ID_JS = ROOT / "web" / "project_id.js"
|
||||
|
||||
|
||||
def _function_source(source: str, name: str, next_name: str) -> str:
|
||||
start = source.index(f"function {name}")
|
||||
end = source.index(f"function {next_name}", start)
|
||||
return source[start:end]
|
||||
|
||||
|
||||
def test_v35_sampler_uses_existing_compact_runtime_settings_path():
|
||||
source = PROJECT_ID_JS.read_text(encoding="utf-8")
|
||||
configure = _function_source(source, "configureNode", "configureNodeAfterSetup")
|
||||
before_queue = source[source.index("async beforeQueuePrompt") :]
|
||||
|
||||
assert 'const V35_NODE_CLASS = "H3ContinuumSamplerV35";' in source
|
||||
assert "const isV35 = node.comfyClass === V35_NODE_CLASS;" in configure
|
||||
assert "!isProduction && !isTimeline && !isV34 && !isV35" in configure
|
||||
assert "isProduction || isV34 || isV35" in configure
|
||||
assert "node.comfyClass === V35_NODE_CLASS" in before_queue
|
||||
|
||||
for widget_name in (
|
||||
'"diagnostics"',
|
||||
'"strict_compatibility"',
|
||||
'"debug"',
|
||||
'"show_preview"',
|
||||
):
|
||||
assert f"hidePersistentWidget(findWidget(node, {widget_name}))" in configure
|
||||
|
||||
|
||||
def test_v35_assembler_uses_existing_compact_runtime_settings_path():
|
||||
source = PROJECT_ID_JS.read_text(encoding="utf-8")
|
||||
configure = _function_source(source, "configureAssembler", "configureNode")
|
||||
before_queue = source[source.index("async beforeQueuePrompt") :]
|
||||
|
||||
assert (
|
||||
'const V35_ASSEMBLE_SEAM_NODE_CLASS = "H3ContinuumAssembleSeamV35";'
|
||||
in source
|
||||
)
|
||||
assert "node.comfyClass !== V35_ASSEMBLE_SEAM_NODE_CLASS" in configure
|
||||
assert "node.comfyClass === V35_ASSEMBLE_SEAM_NODE_CLASS" in before_queue
|
||||
assert 'hidePersistentWidget(findWidget(node, "diagnostics"))' in configure
|
||||
@@ -0,0 +1,324 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
DIAGNOSTICS_BASIC,
|
||||
DIAGNOSTICS_FULL,
|
||||
PROMPT_MODE_FIXED,
|
||||
PROMPT_MODE_LIST,
|
||||
V2_CONTINUITY_OPTIONS,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.temporal import audio_latent_t, video_latent_t
|
||||
from ComfyUI_H3_Continuum_Join.v2 import sequence
|
||||
from ComfyUI_H3_Continuum_Join.v2.h3_builder import IdentityAssets
|
||||
from ComfyUI_H3_Continuum_Join.v2.prompts import make_prompt_plan
|
||||
from ComfyUI_H3_Continuum_Join.v3.driving_nodes import (
|
||||
H3ContinuumSamplerV34,
|
||||
H3ContinuumSamplerV35,
|
||||
NODE_CLASS_MAPPINGS,
|
||||
NODE_DISPLAY_NAME_MAPPINGS,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.nodes import H3ContinuumSamplerProduction
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import prepare_physical_decode_entries
|
||||
from ComfyUI_H3_Continuum_Join.v3.refine_context import validate_refine_context
|
||||
|
||||
|
||||
class _Nested:
|
||||
def __init__(self, parts):
|
||||
self.parts = list(parts)
|
||||
|
||||
def unbind(self):
|
||||
return self.parts
|
||||
|
||||
|
||||
class _Clip:
|
||||
def tokenize(self, prompt, **_kwargs):
|
||||
return prompt
|
||||
|
||||
def encode_from_tokens_scheduled(self, _tokens):
|
||||
return [[torch.zeros((1, 1, 2)), {}]]
|
||||
|
||||
|
||||
def _latent(width: int, height: int, frames: int):
|
||||
return {
|
||||
"samples": _Nested(
|
||||
(
|
||||
torch.zeros(
|
||||
(1, 24, video_latent_t(frames), height // 16, width // 16)
|
||||
),
|
||||
torch.zeros((1, 32, 2, audio_latent_t(frames))),
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
def _model():
|
||||
return SimpleNamespace(
|
||||
model=SimpleNamespace(diffusion_model=SimpleNamespace()),
|
||||
model_options={},
|
||||
wrappers={},
|
||||
model_dtype=lambda: torch.bfloat16,
|
||||
model_size=lambda: 0,
|
||||
)
|
||||
|
||||
|
||||
def _capture_sequence(
|
||||
monkeypatch,
|
||||
*,
|
||||
terminal: bool,
|
||||
session=None,
|
||||
diagnostics=DIAGNOSTICS_BASIC,
|
||||
memory_attribution=False,
|
||||
):
|
||||
width, height = 96, 64
|
||||
first_image = torch.zeros((1, height, width, 3)) if terminal else None
|
||||
last_image = torch.ones((1, height, width, 3)) if terminal else None
|
||||
first_latent = torch.zeros((1, 24, 1, height // 16, width // 16)) if terminal else None
|
||||
last_latent = torch.ones((1, 24, 1, height // 16, width // 16)) if terminal else None
|
||||
assets = IdentityAssets(
|
||||
first_image,
|
||||
first_latent,
|
||||
last_image,
|
||||
last_latent,
|
||||
"identity" if terminal else "none",
|
||||
)
|
||||
monkeypatch.setattr(sequence, "check_comfy_h3_runtime", lambda: [])
|
||||
monkeypatch.setattr(sequence, "prepare_identity_assets", lambda *a, **k: assets)
|
||||
monkeypatch.setattr(sequence, "encode_identity_latents", lambda *a, **k: assets)
|
||||
monkeypatch.setattr(sequence, "empty_h3_latent", _latent)
|
||||
monkeypatch.setattr(
|
||||
sequence,
|
||||
"latent_from_cpu",
|
||||
lambda video, audio: {"samples": _Nested((video, audio))},
|
||||
)
|
||||
monkeypatch.setattr(sequence, "accelerator_summary", lambda _model: "accelerators")
|
||||
monkeypatch.setattr(sequence, "sample_chunk", lambda **kwargs: kwargs["latent"])
|
||||
monkeypatch.setattr(
|
||||
sequence,
|
||||
"clone_model_for_chunk",
|
||||
lambda model, **_kwargs: model,
|
||||
)
|
||||
chunks = 3 if terminal else 2
|
||||
mode = PROMPT_MODE_LIST if terminal else PROMPT_MODE_FIXED
|
||||
script = "first\n---\nmiddle\n---\nlast" if terminal else "same prompt"
|
||||
plan = make_prompt_plan(
|
||||
mode=mode,
|
||||
script=script,
|
||||
chunks=chunks,
|
||||
chunk_seconds=5.0,
|
||||
)
|
||||
return sequence.run_sequence(
|
||||
model=_model(),
|
||||
clip=_Clip(),
|
||||
video_vae=object(),
|
||||
audio_vae=None,
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([1.0, 0.0]),
|
||||
first_frame=first_image,
|
||||
last_frame=last_image,
|
||||
prompt_plan=plan,
|
||||
width=width,
|
||||
height=height,
|
||||
continuity=V2_CONTINUITY_OPTIONS[1],
|
||||
base_seed=42,
|
||||
audio_continuity=True,
|
||||
exact_total_duration=False,
|
||||
diagnostics_mode=diagnostics,
|
||||
reroll_from_chunk=0,
|
||||
reroll_nonce=0,
|
||||
strict_compatibility=False,
|
||||
debug=False,
|
||||
session=session,
|
||||
latent_only=True,
|
||||
capture_refine_context=True,
|
||||
memory_attribution=memory_attribution,
|
||||
)
|
||||
|
||||
|
||||
def test_v34_public_output_schema_is_unchanged():
|
||||
assert H3ContinuumSamplerV34.RETURN_TYPES == (
|
||||
"LATENT",
|
||||
"LATENT",
|
||||
"H3_CONTINUUM_ASSEMBLY_PLAN",
|
||||
"STRING",
|
||||
"AUDIO",
|
||||
)
|
||||
assert H3ContinuumSamplerV34.RETURN_NAMES == (
|
||||
"video_latents",
|
||||
"audio_latents",
|
||||
"assembly_plan",
|
||||
"status",
|
||||
"driving_audio",
|
||||
)
|
||||
assert H3ContinuumSamplerV34.OUTPUT_IS_LIST == (True, True, False, False, False)
|
||||
|
||||
|
||||
def test_v35_is_registered_with_v34_outputs_then_refine_context():
|
||||
from ComfyUI_H3_Continuum_Join import nodes as root_nodes
|
||||
|
||||
assert NODE_CLASS_MAPPINGS["H3ContinuumSamplerV35"] is H3ContinuumSamplerV35
|
||||
assert NODE_DISPLAY_NAME_MAPPINGS["H3ContinuumSamplerV35"] == (
|
||||
"H3 Continuum Sampler V3.5"
|
||||
)
|
||||
assert root_nodes.NODE_CLASS_MAPPINGS["H3ContinuumSamplerV35"] is (
|
||||
H3ContinuumSamplerV35
|
||||
)
|
||||
assert H3ContinuumSamplerV35.RETURN_TYPES[:5] == H3ContinuumSamplerV34.RETURN_TYPES
|
||||
assert H3ContinuumSamplerV35.RETURN_NAMES[:5] == H3ContinuumSamplerV34.RETURN_NAMES
|
||||
assert H3ContinuumSamplerV35.RETURN_TYPES[5] == "H3_CONTINUUM_REFINE_CONTEXT"
|
||||
assert H3ContinuumSamplerV35.RETURN_NAMES[5] == "refine_context"
|
||||
assert H3ContinuumSamplerV35.OUTPUT_IS_LIST == (
|
||||
True,
|
||||
True,
|
||||
False,
|
||||
False,
|
||||
False,
|
||||
False,
|
||||
)
|
||||
|
||||
|
||||
def test_v35_runtime_keeps_driving_audio_fifth_and_context_sixth(monkeypatch):
|
||||
captured = {}
|
||||
|
||||
def fake_production_run(_self, **kwargs):
|
||||
captured["capture"] = kwargs["capture_refine_context"]
|
||||
captured["memory"] = kwargs.get("memory_attribution", False)
|
||||
base = ("video", "audio", {"plan": True}, "status")
|
||||
return (*base, "context") if kwargs["capture_refine_context"] else base
|
||||
|
||||
monkeypatch.setattr(H3ContinuumSamplerProduction, "run", fake_production_run)
|
||||
v34_outputs = H3ContinuumSamplerV34().run(
|
||||
chunks=1,
|
||||
chunk_seconds=5.0,
|
||||
width=96,
|
||||
height=64,
|
||||
)
|
||||
assert captured["capture"] is False
|
||||
assert captured["memory"] is False
|
||||
assert v34_outputs == (
|
||||
"video",
|
||||
"audio",
|
||||
{"plan": True},
|
||||
"status",
|
||||
None,
|
||||
)
|
||||
outputs = H3ContinuumSamplerV35().run(
|
||||
chunks=1,
|
||||
chunk_seconds=5.0,
|
||||
width=96,
|
||||
height=64,
|
||||
)
|
||||
assert captured["capture"] is True
|
||||
assert captured["memory"] is True
|
||||
assert outputs == (
|
||||
"video",
|
||||
"audio",
|
||||
{"plan": True},
|
||||
"status",
|
||||
None,
|
||||
"context",
|
||||
)
|
||||
|
||||
|
||||
def test_capture_records_normal_physical_groups_in_sampling_order(monkeypatch):
|
||||
outputs = _capture_sequence(monkeypatch, terminal=False)
|
||||
assert len(outputs) == 5
|
||||
context = outputs[4]
|
||||
assert context["complete"] is True
|
||||
assert context["conditioning_mode"] == "t2va"
|
||||
groups = context["groups"]
|
||||
assert [group["logical_chunks"] for group in groups] == [(1,), (2,)]
|
||||
assert [group["physical_clip_index"] for group in groups] == [1, 2]
|
||||
assert [group["context_frames"] for group in groups] == [0, 22]
|
||||
assert [group["prompt_policy"] for group in groups] == ["single", "single"]
|
||||
assert [group["source_video_shape"] for group in groups] == [
|
||||
(1, 24, 37, 4, 6),
|
||||
(1, 24, 42, 4, 6),
|
||||
]
|
||||
_, assembly_plan = prepare_physical_decode_entries(
|
||||
outputs[0],
|
||||
chunk_seconds=5.0,
|
||||
preserve_final_frame=False,
|
||||
terminal_merged=False,
|
||||
)
|
||||
assert validate_refine_context(context, assembly_plan) is context
|
||||
|
||||
|
||||
def test_capture_records_terminal_pair_as_one_physical_group(monkeypatch):
|
||||
outputs = _capture_sequence(monkeypatch, terminal=True)
|
||||
context = outputs[4]
|
||||
assert context["complete"] is True
|
||||
assert context["conditioning_mode"] == "fl2va"
|
||||
groups = context["groups"]
|
||||
assert len(groups) == 2
|
||||
assert groups[0]["logical_chunks"] == (1,)
|
||||
assert groups[1]["logical_chunks"] == (2, 3)
|
||||
assert groups[1]["physical_frames"] == 260
|
||||
assert groups[1]["physical_clip_index"] == 2
|
||||
assert groups[1]["context_frames"] == 22
|
||||
assert groups[1]["prompt_policy"] == "paired_timeline_v1"
|
||||
assert groups[1]["physical_prompt"] == (
|
||||
"[0-5s]\nmiddle\n\n[5-10s]\nlast"
|
||||
)
|
||||
assert groups[1]["source_video_shape"] == (1, 24, 77, 4, 6)
|
||||
assert torch.equal(groups[1]["first_image"], torch.zeros((1, 64, 96, 3)))
|
||||
assert torch.equal(groups[1]["last_image"], torch.ones((1, 64, 96, 3)))
|
||||
_, assembly_plan = prepare_physical_decode_entries(
|
||||
outputs[0],
|
||||
chunk_seconds=5.0,
|
||||
preserve_final_frame=True,
|
||||
terminal_merged=True,
|
||||
)
|
||||
assert validate_refine_context(context, assembly_plan) is context
|
||||
|
||||
|
||||
def test_reused_session_returns_incomplete_context_without_stopping(monkeypatch):
|
||||
first_outputs = _capture_sequence(monkeypatch, terminal=False)
|
||||
reused_outputs = _capture_sequence(
|
||||
monkeypatch,
|
||||
terminal=False,
|
||||
session=first_outputs[2],
|
||||
)
|
||||
context = reused_outputs[4]
|
||||
assert context["complete"] is False
|
||||
assert context["groups"] == ()
|
||||
assert len(reused_outputs[0]) == 2
|
||||
assert any("Run Storage" in note for note in context["notes"])
|
||||
|
||||
|
||||
def test_v35_full_memory_events_follow_physical_terminal_group_order(monkeypatch):
|
||||
outputs = _capture_sequence(
|
||||
monkeypatch,
|
||||
terminal=True,
|
||||
diagnostics=DIAGNOSTICS_FULL,
|
||||
memory_attribution=True,
|
||||
)
|
||||
memory_lines = [
|
||||
line for line in outputs[3].splitlines() if line.startswith("Memory [V3.5")
|
||||
]
|
||||
labels = [line.split("]: RSS", 1)[0] + "]" for line in memory_lines]
|
||||
assert labels == [
|
||||
"Memory [V3.5 sequence start]",
|
||||
"Memory [V3.5 physical group 1 logical=[1] before sampling]",
|
||||
"Memory [V3.5 physical group 1 logical=[1] after sampling]",
|
||||
"Memory [V3.5 physical group 1 logical=[1] after CPU commit]",
|
||||
"Memory [V3.5 physical group 2 logical=[2,3] before sampling]",
|
||||
"Memory [V3.5 physical group 2 logical=[2,3] after sampling]",
|
||||
"Memory [V3.5 physical group 2 logical=[2,3] after CPU commit]",
|
||||
"Memory [V3.5 sequence complete]",
|
||||
]
|
||||
|
||||
|
||||
def test_memory_attribution_is_absent_when_private_flag_is_off(monkeypatch):
|
||||
outputs = _capture_sequence(
|
||||
monkeypatch,
|
||||
terminal=False,
|
||||
diagnostics=DIAGNOSTICS_FULL,
|
||||
memory_attribution=False,
|
||||
)
|
||||
assert "Memory [V3.5" not in outputs[3]
|
||||
assert "Memory [sequence start]" in outputs[3]
|
||||
assert "Memory [sequence complete]" in outputs[3]
|
||||
@@ -0,0 +1,443 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.nodes import (
|
||||
NODE_CLASS_MAPPINGS,
|
||||
NODE_DISPLAY_NAME_MAPPINGS,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes import (
|
||||
H3ContinuumHiResFixV35,
|
||||
resolve_h3_native_latent_target,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.second_pass import SecondPassContractError
|
||||
|
||||
|
||||
def _video(marker: str, *, height: int = 4, width: int = 4) -> dict:
|
||||
return {
|
||||
"samples": torch.zeros((1, 24, 3, height, width)),
|
||||
"marker": marker,
|
||||
}
|
||||
|
||||
|
||||
def _audio(marker: str) -> dict:
|
||||
return {
|
||||
"samples": torch.zeros((1, 8, 12)),
|
||||
"marker": marker,
|
||||
}
|
||||
|
||||
|
||||
def _plan(
|
||||
*,
|
||||
width: int = 64,
|
||||
height: int = 64,
|
||||
latent_width: int = 4,
|
||||
latent_height: int = 4,
|
||||
) -> dict:
|
||||
return {
|
||||
"width": width,
|
||||
"height": height,
|
||||
"second_pass_contract": {
|
||||
"version": 1,
|
||||
"physical_groups": [
|
||||
{
|
||||
"group_id": 0,
|
||||
"source_width": width,
|
||||
"source_height": height,
|
||||
"source_batch": 1,
|
||||
"latent_channels": 24,
|
||||
"source_latent_t": 3,
|
||||
"source_latent_w": latent_width,
|
||||
"source_latent_h": latent_height,
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_disabled_returns_first_pass_objects_without_resize_or_sampling(monkeypatch):
|
||||
def unexpected_call(*_args, **_kwargs):
|
||||
raise AssertionError("disabled Hi-res fix must not resize or sample")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.resize_video_latents_via_vae",
|
||||
unexpected_call,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.run_second_pass_groups",
|
||||
unexpected_call,
|
||||
)
|
||||
videos = [_video("video")]
|
||||
audios = [_audio("audio")]
|
||||
plan = {"width": 640, "height": 640}
|
||||
|
||||
output_video, output_audio, output_plan, status = H3ContinuumHiResFixV35().apply(
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
videos,
|
||||
audios,
|
||||
[plan],
|
||||
[False],
|
||||
["bislerp"],
|
||||
[2.0],
|
||||
[7],
|
||||
["must", "not be unwrapped"],
|
||||
["must", "not be unwrapped"],
|
||||
)
|
||||
|
||||
assert output_video is videos
|
||||
assert output_audio is audios
|
||||
assert output_plan is plan
|
||||
assert "disabled" in status
|
||||
|
||||
|
||||
def test_enabled_pixel_vae_roundtrip_then_reuses_second_pass_contract(monkeypatch):
|
||||
videos = [_video("first-pass", height=40, width=40)]
|
||||
resized = [_video("reencoded", height=80, width=80)]
|
||||
audios = [_audio("first-pass")]
|
||||
plan = _plan(width=640, height=640, latent_width=40, latent_height=40)
|
||||
sentinel = (
|
||||
[_video("refined")],
|
||||
audios,
|
||||
{"width": 1280, "height": 1280},
|
||||
"sampled",
|
||||
)
|
||||
calls = []
|
||||
|
||||
def fake_roundtrip(video_latents, assembly_plan, **kwargs):
|
||||
calls.append(("roundtrip", video_latents, assembly_plan, kwargs))
|
||||
return resized
|
||||
|
||||
def fake_second_pass(**kwargs):
|
||||
calls.append(("second_pass", kwargs))
|
||||
return sentinel
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.resize_video_latents_via_vae",
|
||||
fake_roundtrip,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.run_second_pass_groups",
|
||||
fake_second_pass,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.h3_core_native_canvas",
|
||||
lambda *_args, **_kwargs: (768, 768),
|
||||
)
|
||||
|
||||
result = H3ContinuumHiResFixV35().apply(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
videos,
|
||||
audios,
|
||||
[plan],
|
||||
[True],
|
||||
["lanczos"],
|
||||
[2.0],
|
||||
[17],
|
||||
[{"context": True}],
|
||||
["video-vae"],
|
||||
)
|
||||
|
||||
assert result[:3] == sentinel[:3]
|
||||
assert "mode=manual_scale_by" in result[3]
|
||||
assert "backend=sequential_pixel_vae_roundtrip" in result[3]
|
||||
assert "target=1280x1280" in result[3]
|
||||
assert "INFO" in result[3]
|
||||
assert calls[0] == (
|
||||
"roundtrip",
|
||||
videos,
|
||||
plan,
|
||||
{
|
||||
"video_vae": "video-vae",
|
||||
"target_width": 1280,
|
||||
"target_height": 1280,
|
||||
"upscale_method": "lanczos",
|
||||
},
|
||||
)
|
||||
second_pass = calls[1][1]
|
||||
assert second_pass["model"] == "model"
|
||||
assert second_pass["clip"] == "clip"
|
||||
assert second_pass["sampler"] == "sampler"
|
||||
assert second_pass["video_latents"] is resized
|
||||
assert second_pass["audio_latents"] is audios
|
||||
assert second_pass["assembly_plan"] is plan
|
||||
assert second_pass["refine_seed"] == 17
|
||||
assert second_pass["refine_context"] == {"context": True}
|
||||
assert second_pass["video_vae"] == "video-vae"
|
||||
assert second_pass["conditioning_upscale_method"] == "bilinear"
|
||||
|
||||
|
||||
def test_native_auto_resizes_to_exact_core_target_then_samples(monkeypatch):
|
||||
videos = [_video("first-pass")]
|
||||
resized = [_video("native", height=6, width=6)]
|
||||
audios = [_audio("first-pass")]
|
||||
plan = _plan()
|
||||
calls = []
|
||||
|
||||
def fake_roundtrip(video_latents, assembly_plan, **kwargs):
|
||||
calls.append(("roundtrip", video_latents, assembly_plan, kwargs))
|
||||
return resized
|
||||
|
||||
def fake_second_pass(**kwargs):
|
||||
calls.append(("second_pass", kwargs))
|
||||
return (
|
||||
[_video("refined", height=6, width=6)],
|
||||
audios,
|
||||
{"width": 96, "height": 96},
|
||||
"sampled",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.h3_core_native_canvas",
|
||||
lambda *_args, **_kwargs: (96, 96),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.resize_video_latents_via_vae",
|
||||
fake_roundtrip,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.run_second_pass_groups",
|
||||
fake_second_pass,
|
||||
)
|
||||
|
||||
result = H3ContinuumHiResFixV35().apply(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
videos,
|
||||
audios,
|
||||
[plan],
|
||||
[True],
|
||||
["bilinear"],
|
||||
[0.0],
|
||||
[17],
|
||||
None,
|
||||
["video-vae"],
|
||||
)
|
||||
|
||||
assert calls[0] == (
|
||||
"roundtrip",
|
||||
videos,
|
||||
plan,
|
||||
{
|
||||
"video_vae": "video-vae",
|
||||
"upscale_method": "bilinear",
|
||||
"target_width": 96,
|
||||
"target_height": 96,
|
||||
},
|
||||
)
|
||||
assert calls[1][1]["video_latents"] is resized
|
||||
assert calls[1][1]["conditioning_upscale_method"] == "bilinear"
|
||||
assert result[2]["width"] == 96
|
||||
assert "mode=h3_native_canvas_auto" in result[3]
|
||||
assert "source=64x64" in result[3]
|
||||
assert "target=96x96" in result[3]
|
||||
assert "WARNING" not in result[3]
|
||||
|
||||
|
||||
def test_native_target_uses_core_canvas_and_never_shrinks_existing_latent():
|
||||
source = [_video("source", height=36, width=36)]
|
||||
plan = _plan(width=576, height=576, latent_width=36, latent_height=36)
|
||||
|
||||
native = resolve_h3_native_latent_target(
|
||||
source,
|
||||
plan,
|
||||
adapt_canvas_fn=lambda _width, _height: (768, 768),
|
||||
)
|
||||
|
||||
assert native["target_latent_width"] == 48
|
||||
assert native["target_latent_height"] == 48
|
||||
assert native["target_width"] == 768
|
||||
assert native["target_height"] == 768
|
||||
assert native["preserved_current"] is False
|
||||
|
||||
oversized = [_video("oversized", height=64, width=64)]
|
||||
preserved = resolve_h3_native_latent_target(
|
||||
oversized,
|
||||
plan,
|
||||
adapt_canvas_fn=lambda _width, _height: (768, 768),
|
||||
)
|
||||
|
||||
assert preserved["target_latent_width"] == 64
|
||||
assert preserved["target_latent_height"] == 64
|
||||
assert preserved["target_width"] == 1024
|
||||
assert preserved["target_height"] == 1024
|
||||
assert preserved["preserved_current"] is True
|
||||
|
||||
|
||||
def test_native_target_uses_exact_core_landscape_geometry():
|
||||
source = [_video("landscape", height=36, width=64)]
|
||||
plan = _plan(width=1024, height=576, latent_width=64, latent_height=36)
|
||||
|
||||
native = resolve_h3_native_latent_target(
|
||||
source,
|
||||
plan,
|
||||
adapt_canvas_fn=lambda _width, _height: (1344, 768),
|
||||
)
|
||||
|
||||
assert native["target_latent_width"] == 84
|
||||
assert native["target_latent_height"] == 48
|
||||
assert native["target_width"] == 1344
|
||||
assert native["target_height"] == 768
|
||||
|
||||
|
||||
def test_enabled_requires_video_vae_without_legacy_latent_fallback(monkeypatch):
|
||||
def unexpected_call(*_args, **_kwargs):
|
||||
raise AssertionError("missing VAE must fail before resize or sampling")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.resize_video_latents_via_vae",
|
||||
unexpected_call,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.run_second_pass_groups",
|
||||
unexpected_call,
|
||||
)
|
||||
|
||||
with pytest.raises(SecondPassContractError, match="requires video_vae"):
|
||||
H3ContinuumHiResFixV35().apply(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
[_video("first-pass")],
|
||||
[_audio("first-pass")],
|
||||
[_plan()],
|
||||
[True],
|
||||
["lanczos"],
|
||||
[2.0],
|
||||
[17],
|
||||
)
|
||||
|
||||
|
||||
def test_manual_scale_below_one_rejects_before_vae_or_sampling(monkeypatch):
|
||||
def unexpected_call(*_args, **_kwargs):
|
||||
raise AssertionError("invalid manual scale must fail before expensive work")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.resize_video_latents_via_vae",
|
||||
unexpected_call,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.hires_fix_nodes.run_second_pass_groups",
|
||||
unexpected_call,
|
||||
)
|
||||
|
||||
with pytest.raises(SecondPassContractError, match="at least 1.0"):
|
||||
H3ContinuumHiResFixV35().apply(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
[_video("first-pass")],
|
||||
[_audio("first-pass")],
|
||||
[_plan()],
|
||||
[True],
|
||||
["lanczos"],
|
||||
[0.5],
|
||||
[17],
|
||||
None,
|
||||
["video-vae"],
|
||||
)
|
||||
|
||||
|
||||
def test_lazy_inputs_are_requested_only_when_enabled():
|
||||
node = H3ContinuumHiResFixV35()
|
||||
|
||||
assert node.check_lazy_status([False]) == []
|
||||
assert node.check_lazy_status(
|
||||
[False],
|
||||
refine_context=[None],
|
||||
video_vae=[None],
|
||||
) == []
|
||||
assert node.check_lazy_status([True]) == [
|
||||
"model",
|
||||
"clip",
|
||||
"sampler",
|
||||
"sigmas",
|
||||
]
|
||||
assert node.check_lazy_status(
|
||||
[True],
|
||||
model=(None,),
|
||||
clip=(None,),
|
||||
sampler=(None,),
|
||||
sigmas=(None,),
|
||||
) == ["model", "clip", "sampler", "sigmas"]
|
||||
assert node.check_lazy_status(
|
||||
[True],
|
||||
model=["model"],
|
||||
clip=["clip"],
|
||||
sampler=["sampler"],
|
||||
sigmas=["sigmas"],
|
||||
) == []
|
||||
assert node.check_lazy_status(
|
||||
[True],
|
||||
model=["model"],
|
||||
clip=["clip"],
|
||||
sampler=["sampler"],
|
||||
sigmas=["sigmas"],
|
||||
refine_context=[None],
|
||||
video_vae=[None],
|
||||
) == ["refine_context", "video_vae"]
|
||||
|
||||
|
||||
def test_three_v35_nodes_are_registered_with_final_display_names():
|
||||
assert NODE_CLASS_MAPPINGS["H3ContinuumHiResFixV35"] is H3ContinuumHiResFixV35
|
||||
assert NODE_DISPLAY_NAME_MAPPINGS["H3ContinuumHiResFixV35"] == (
|
||||
"H3 Continuum Hi-Res Fix V3.5"
|
||||
)
|
||||
assert NODE_DISPLAY_NAME_MAPPINGS["H3ContinuumSecondPassV35"] == (
|
||||
"H3 Continuum Second Pass V3.5"
|
||||
)
|
||||
assert NODE_DISPLAY_NAME_MAPPINGS["H3ContinuumLatentResizeV35"] == (
|
||||
"H3 Continuum Latent Resize V3.5"
|
||||
)
|
||||
assert H3ContinuumHiResFixV35.INPUT_IS_LIST is True
|
||||
assert H3ContinuumHiResFixV35.OUTPUT_IS_LIST == (True, True, False, False)
|
||||
|
||||
|
||||
def test_hires_fix_schema_exposes_one_node_controls_and_lazy_sampling_inputs():
|
||||
schema = H3ContinuumHiResFixV35.INPUT_TYPES()
|
||||
required = schema["required"]
|
||||
|
||||
assert required["enabled"][1]["default"] is True
|
||||
assert required["upscale_method"][0][0] == "lanczos"
|
||||
assert required["upscale_method"][1]["default"] == "lanczos"
|
||||
assert required["scale_by"][1] == {
|
||||
"default": 2.0,
|
||||
"min": 0.0,
|
||||
"max": 4.0,
|
||||
"step": 0.01,
|
||||
"tooltip": (
|
||||
"2.0 = validated 2x Pixel/VAE Hi-res Fix. 0 uses the "
|
||||
"ComfyUI Core H3 native canvas automatically. Manual "
|
||||
"values follow the existing Second Pass contract and must "
|
||||
"be 1.0 or greater."
|
||||
),
|
||||
}
|
||||
assert all(required[name][1]["lazy"] is True for name in (
|
||||
"model",
|
||||
"clip",
|
||||
"sampler",
|
||||
"sigmas",
|
||||
))
|
||||
assert schema["optional"] == {
|
||||
"refine_context": ("H3_CONTINUUM_REFINE_CONTEXT", {"lazy": True}),
|
||||
"video_vae": (
|
||||
"VAE",
|
||||
{
|
||||
"lazy": True,
|
||||
"tooltip": (
|
||||
"Required while enabled for the safe Pixel/VAE roundtrip; "
|
||||
"not evaluated while disabled."
|
||||
),
|
||||
},
|
||||
),
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.nodes import NODE_CLASS_MAPPINGS
|
||||
from ComfyUI_H3_Continuum_Join.v3.latent_resize_nodes import (
|
||||
H3ContinuumLatentResizeV35,
|
||||
UPSCALE_METHODS,
|
||||
resize_video_latents,
|
||||
resize_video_latents_to_size,
|
||||
)
|
||||
|
||||
|
||||
def _latent(shape: tuple[int, ...], *, marker: str) -> dict:
|
||||
return {
|
||||
"samples": torch.arange(torch.tensor(shape).prod().item(), dtype=torch.float32).reshape(shape),
|
||||
"marker": marker,
|
||||
}
|
||||
|
||||
|
||||
def _test_upscale(samples, width, height, upscale_method, crop):
|
||||
assert crop == "disabled"
|
||||
original = samples.shape
|
||||
flattened = samples.movedim(2, 1).reshape(
|
||||
-1,
|
||||
original[1],
|
||||
original[-2],
|
||||
original[-1],
|
||||
)
|
||||
mode = "bilinear" if upscale_method == "bislerp" else upscale_method
|
||||
output = F.interpolate(flattened, size=(height, width), mode=mode)
|
||||
return output.reshape((original[0], -1, original[1], height, width)).movedim(
|
||||
2,
|
||||
1,
|
||||
)
|
||||
|
||||
|
||||
def test_resize_preserves_physical_groups_bct_order_and_metadata() -> None:
|
||||
inputs = [
|
||||
_latent((1, 24, 37, 4, 5), marker="group-1"),
|
||||
_latent((1, 24, 77, 4, 5), marker="group-2"),
|
||||
]
|
||||
|
||||
outputs = resize_video_latents(
|
||||
inputs,
|
||||
upscale_method="bilinear",
|
||||
scale_by=2.0,
|
||||
upscale_fn=_test_upscale,
|
||||
)
|
||||
|
||||
assert [item["samples"].shape for item in outputs] == [
|
||||
(1, 24, 37, 8, 10),
|
||||
(1, 24, 77, 8, 10),
|
||||
]
|
||||
assert [item["marker"] for item in outputs] == ["group-1", "group-2"]
|
||||
assert all(after is not before for before, after in zip(inputs, outputs, strict=True))
|
||||
assert [item["samples"].shape for item in inputs] == [
|
||||
(1, 24, 37, 4, 5),
|
||||
(1, 24, 77, 4, 5),
|
||||
]
|
||||
|
||||
|
||||
def test_resize_delegates_dimensions_method_and_crop_to_core_boundary() -> None:
|
||||
latent = _latent((1, 24, 3, 4, 5), marker="core")
|
||||
calls = []
|
||||
|
||||
def capture(samples, width, height, upscale_method, crop):
|
||||
calls.append((samples, width, height, upscale_method, crop))
|
||||
return _test_upscale(samples, width, height, upscale_method, crop)
|
||||
|
||||
output = resize_video_latents(
|
||||
[latent],
|
||||
upscale_method="bicubic",
|
||||
scale_by=1.5,
|
||||
upscale_fn=capture,
|
||||
)[0]
|
||||
|
||||
assert calls == [(latent["samples"], 8, 6, "bicubic", "disabled")]
|
||||
assert output["samples"].shape == (1, 24, 3, 6, 8)
|
||||
|
||||
|
||||
def test_resize_to_exact_latent_geometry_preserves_groups_and_metadata() -> None:
|
||||
inputs = [
|
||||
_latent((1, 24, 37, 4, 5), marker="group-1"),
|
||||
_latent((1, 24, 77, 4, 5), marker="group-2"),
|
||||
]
|
||||
|
||||
outputs = resize_video_latents_to_size(
|
||||
inputs,
|
||||
upscale_method="bilinear",
|
||||
target_width=9,
|
||||
target_height=7,
|
||||
upscale_fn=_test_upscale,
|
||||
)
|
||||
|
||||
assert [item["samples"].shape for item in outputs] == [
|
||||
(1, 24, 37, 7, 9),
|
||||
(1, 24, 77, 7, 9),
|
||||
]
|
||||
assert [item["marker"] for item in outputs] == ["group-1", "group-2"]
|
||||
|
||||
|
||||
def test_bislerp_preserves_dtype_and_returns_finite_video_latent() -> None:
|
||||
latent = {"samples": torch.randn((1, 24, 2, 3, 3), dtype=torch.float32)}
|
||||
output = resize_video_latents(
|
||||
[latent],
|
||||
upscale_method="bislerp",
|
||||
scale_by=2.0,
|
||||
upscale_fn=_test_upscale,
|
||||
)[0]["samples"]
|
||||
|
||||
assert output.shape == (1, 24, 2, 6, 6)
|
||||
assert output.dtype == latent["samples"].dtype
|
||||
assert torch.isfinite(output).all()
|
||||
|
||||
|
||||
def test_latent_resize_node_is_registered_and_core_compatible() -> None:
|
||||
node_class = NODE_CLASS_MAPPINGS["H3ContinuumLatentResizeV35"]
|
||||
assert node_class is H3ContinuumLatentResizeV35
|
||||
assert node_class.DEPRECATED is False
|
||||
assert node_class.INPUT_IS_LIST is True
|
||||
assert node_class.OUTPUT_IS_LIST == (True,)
|
||||
assert list(UPSCALE_METHODS) == [
|
||||
"nearest-exact",
|
||||
"bilinear",
|
||||
"area",
|
||||
"bicubic",
|
||||
"bislerp",
|
||||
]
|
||||
|
||||
|
||||
def test_node_unwraps_widget_lists_but_preserves_physical_latent_list(monkeypatch) -> None:
|
||||
monkeypatch.setitem(
|
||||
H3ContinuumLatentResizeV35.resize.__globals__,
|
||||
"resize_video_latents",
|
||||
lambda video_latents, **kwargs: resize_video_latents(
|
||||
video_latents,
|
||||
upscale_fn=_test_upscale,
|
||||
**kwargs,
|
||||
),
|
||||
)
|
||||
inputs = [_latent((1, 24, 2, 2, 2), marker="one")]
|
||||
(outputs,) = H3ContinuumLatentResizeV35().resize(
|
||||
inputs,
|
||||
["nearest-exact"],
|
||||
[2.0],
|
||||
)
|
||||
|
||||
assert len(outputs) == 1
|
||||
assert outputs[0]["samples"].shape == (1, 24, 2, 4, 4)
|
||||
assert outputs[0]["marker"] == "one"
|
||||
@@ -0,0 +1,341 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v3.pixel_vae_resize import (
|
||||
PIXEL_UPSCALE_METHODS,
|
||||
PixelVaeResizeError,
|
||||
resolve_pixel_latent_scale,
|
||||
resize_video_latents_via_vae,
|
||||
)
|
||||
|
||||
|
||||
def _plan(
|
||||
*,
|
||||
temporals=(3, 5),
|
||||
batch=1,
|
||||
scale_width=6,
|
||||
scale_height=4,
|
||||
) -> dict:
|
||||
latent_width = 3
|
||||
latent_height = 2
|
||||
width = latent_width * scale_width
|
||||
height = latent_height * scale_height
|
||||
return {
|
||||
"width": width,
|
||||
"height": height,
|
||||
"second_pass_contract": {
|
||||
"version": 1,
|
||||
"physical_groups": [
|
||||
{
|
||||
"group_id": position,
|
||||
"source_width": width,
|
||||
"source_height": height,
|
||||
"source_batch": batch,
|
||||
"latent_channels": 24,
|
||||
"source_latent_t": temporal,
|
||||
"source_latent_h": latent_height,
|
||||
"source_latent_w": latent_width,
|
||||
}
|
||||
for position, temporal in enumerate(temporals)
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _latents(temporals=(3, 5), *, batch=1) -> list[dict]:
|
||||
return [
|
||||
{
|
||||
"samples": torch.full(
|
||||
(batch, 24, temporal, 2, 3),
|
||||
float(position + 1),
|
||||
),
|
||||
"marker": f"group-{position}",
|
||||
}
|
||||
for position, temporal in enumerate(temporals)
|
||||
]
|
||||
|
||||
|
||||
class _RoundTripVae:
|
||||
def __init__(self, log, *, scale_width=6, scale_height=4):
|
||||
self.log = log
|
||||
self.scale_width = scale_width
|
||||
self.scale_height = scale_height
|
||||
self.pending_temporal = None
|
||||
self.pending_batches = 0
|
||||
|
||||
def decode(self, samples):
|
||||
temporal = int(samples.shape[2])
|
||||
assert self.pending_temporal is None
|
||||
assert self.pending_batches == 0
|
||||
self.pending_temporal = temporal
|
||||
self.pending_batches = int(samples.shape[0])
|
||||
self.log.append(("decode", temporal, tuple(samples.shape)))
|
||||
frames = temporal * 2 + 1
|
||||
return torch.full(
|
||||
(int(samples.shape[0]), frames, 8, 18, 3),
|
||||
float(temporal),
|
||||
)
|
||||
|
||||
def encode(self, pixels):
|
||||
temporal = self.pending_temporal
|
||||
assert temporal is not None
|
||||
assert pixels.ndim == 4
|
||||
assert self.pending_batches > 0
|
||||
self.log.append(("encode", temporal, tuple(pixels.shape)))
|
||||
self.pending_batches -= 1
|
||||
if self.pending_batches == 0:
|
||||
self.pending_temporal = None
|
||||
return torch.full(
|
||||
(
|
||||
1,
|
||||
24,
|
||||
temporal,
|
||||
int(pixels.shape[1]) // self.scale_height,
|
||||
int(pixels.shape[2]) // self.scale_width,
|
||||
),
|
||||
float(temporal),
|
||||
)
|
||||
|
||||
|
||||
def test_plan_derives_non_hardcoded_pixel_latent_scale() -> None:
|
||||
assert resolve_pixel_latent_scale(
|
||||
_plan(scale_width=6, scale_height=4)
|
||||
) == (6, 4)
|
||||
|
||||
|
||||
def test_sequential_roundtrip_preserves_groups_bct_metadata_and_input() -> None:
|
||||
log = []
|
||||
vae = _RoundTripVae(log)
|
||||
inputs = _latents()
|
||||
original_samples = [item["samples"] for item in inputs]
|
||||
|
||||
def upscale(samples, width, height, method, crop):
|
||||
log.append(("upscale", tuple(samples.shape), width, height, method, crop))
|
||||
assert samples.ndim == 4
|
||||
return F.interpolate(samples, size=(height, width), mode="bilinear")
|
||||
|
||||
outputs = resize_video_latents_via_vae(
|
||||
inputs,
|
||||
_plan(),
|
||||
video_vae=vae,
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_method="lanczos",
|
||||
upscale_fn=upscale,
|
||||
)
|
||||
|
||||
assert [tuple(item["samples"].shape) for item in outputs] == [
|
||||
(1, 24, 3, 4, 6),
|
||||
(1, 24, 5, 4, 6),
|
||||
]
|
||||
assert [item["marker"] for item in outputs] == ["group-0", "group-1"]
|
||||
assert all(
|
||||
output is not source
|
||||
for output, source in zip(outputs, inputs, strict=True)
|
||||
)
|
||||
assert all(
|
||||
item["samples"] is original
|
||||
for item, original in zip(inputs, original_samples, strict=True)
|
||||
)
|
||||
assert all(output["samples"].device.type == "cpu" for output in outputs)
|
||||
assert all(torch.isfinite(output["samples"]).all() for output in outputs)
|
||||
assert log == [
|
||||
("decode", 3, (1, 24, 3, 2, 3)),
|
||||
("upscale", (7, 3, 8, 18), 36, 16, "lanczos", "disabled"),
|
||||
("encode", 3, (7, 16, 36, 3)),
|
||||
("decode", 5, (1, 24, 5, 2, 3)),
|
||||
("upscale", (8, 3, 8, 18), 36, 16, "lanczos", "disabled"),
|
||||
("upscale", (3, 3, 8, 18), 36, 16, "lanczos", "disabled"),
|
||||
("encode", 5, (11, 16, 36, 3)),
|
||||
]
|
||||
|
||||
|
||||
def test_core_4d_encode_boundary_preserves_continuum_batch() -> None:
|
||||
log = []
|
||||
vae = _RoundTripVae(log)
|
||||
|
||||
outputs = resize_video_latents_via_vae(
|
||||
_latents((3,), batch=2),
|
||||
_plan(temporals=(3,), batch=2),
|
||||
video_vae=vae,
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_method="lanczos",
|
||||
upscale_fn=lambda samples, width, height, *_args: F.interpolate(
|
||||
samples, size=(height, width), mode="bilinear"
|
||||
),
|
||||
)
|
||||
|
||||
assert tuple(outputs[0]["samples"].shape) == (2, 24, 3, 4, 6)
|
||||
assert [entry for entry in log if entry[0] == "encode"] == [
|
||||
("encode", 3, (7, 16, 36, 3)),
|
||||
("encode", 3, (7, 16, 36, 3)),
|
||||
]
|
||||
|
||||
|
||||
def test_each_core_encode_batch_is_validated_before_concat() -> None:
|
||||
class BadSecondBatchVae(_RoundTripVae):
|
||||
def __init__(self):
|
||||
super().__init__([])
|
||||
self.encode_calls = 0
|
||||
|
||||
def encode(self, pixels):
|
||||
encoded = super().encode(pixels)
|
||||
self.encode_calls += 1
|
||||
if self.encode_calls == 2:
|
||||
return torch.zeros((1, 24, 4, 4, 6))
|
||||
return encoded
|
||||
|
||||
with pytest.raises(PixelVaeResizeError, match="for batch 1"):
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,), batch=2),
|
||||
_plan(temporals=(3,), batch=2),
|
||||
video_vae=BadSecondBatchVae(),
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_fn=lambda samples, width, height, *_args: F.interpolate(
|
||||
samples, size=(height, width), mode="bilinear"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_lanczos_is_a_supported_pixel_method() -> None:
|
||||
assert PIXEL_UPSCALE_METHODS == (
|
||||
"nearest-exact",
|
||||
"bilinear",
|
||||
"area",
|
||||
"bicubic",
|
||||
"bislerp",
|
||||
"lanczos",
|
||||
)
|
||||
|
||||
|
||||
def test_decode_requires_core_video_image_layout() -> None:
|
||||
class BadDecode:
|
||||
def decode(self, _samples):
|
||||
return torch.zeros((1, 8, 18, 3))
|
||||
|
||||
def encode(self, _pixels):
|
||||
raise AssertionError("encode must not run")
|
||||
|
||||
with pytest.raises(PixelVaeResizeError, match=r"\[B,F,H,W,C\]"):
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=BadDecode(),
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_fn=lambda *args: args[0],
|
||||
)
|
||||
|
||||
|
||||
def test_encode_rejects_changed_bct_or_target_geometry() -> None:
|
||||
class BadEncode(_RoundTripVae):
|
||||
def encode(self, pixels):
|
||||
temporal = self.pending_temporal
|
||||
assert temporal is not None
|
||||
return torch.zeros((1, 24, temporal + 1, 4, 6))
|
||||
|
||||
with pytest.raises(PixelVaeResizeError, match="changed B/C/T"):
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=BadEncode([]),
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_fn=lambda samples, width, height, *_args: F.interpolate(
|
||||
samples, size=(height, width), mode="bilinear"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_target_geometry_uses_plan_ratio_and_must_be_aligned() -> None:
|
||||
class MustNotRun:
|
||||
def decode(self, _samples):
|
||||
raise AssertionError("decode must not run")
|
||||
|
||||
def encode(self, _pixels):
|
||||
raise AssertionError("encode must not run")
|
||||
|
||||
with pytest.raises(PixelVaeResizeError, match="plan-derived latent scale"):
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=MustNotRun(),
|
||||
target_width=35,
|
||||
target_height=16,
|
||||
upscale_fn=lambda *args: args[0],
|
||||
)
|
||||
|
||||
|
||||
def test_manual_target_is_not_independently_restricted_to_upscale_only() -> None:
|
||||
outputs = resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=_RoundTripVae([]),
|
||||
target_width=12,
|
||||
target_height=4,
|
||||
upscale_method="bilinear",
|
||||
upscale_fn=lambda samples, width, height, *_args: F.interpolate(
|
||||
samples, size=(height, width), mode="bilinear"
|
||||
),
|
||||
)
|
||||
|
||||
assert tuple(outputs[0]["samples"].shape) == (1, 24, 3, 1, 2)
|
||||
|
||||
|
||||
def test_vae_failure_propagates_without_latent_fallback() -> None:
|
||||
sentinel = RuntimeError("VAE encode sentinel")
|
||||
|
||||
class FailingVae(_RoundTripVae):
|
||||
def encode(self, _pixels):
|
||||
raise sentinel
|
||||
|
||||
with pytest.raises(RuntimeError) as captured:
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=FailingVae([]),
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_fn=lambda samples, width, height, *_args: F.interpolate(
|
||||
samples, size=(height, width), mode="bilinear"
|
||||
),
|
||||
)
|
||||
|
||||
assert captured.value is sentinel
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stage", ["decode", "resize", "encode"])
|
||||
def test_nonfinite_values_are_rejected_at_each_stage(stage) -> None:
|
||||
class NonFiniteVae(_RoundTripVae):
|
||||
def decode(self, samples):
|
||||
pixels = super().decode(samples)
|
||||
if stage == "decode":
|
||||
pixels[0, 0, 0, 0, 0] = torch.nan
|
||||
return pixels
|
||||
|
||||
def encode(self, pixels):
|
||||
encoded = super().encode(pixels)
|
||||
if stage == "encode":
|
||||
encoded[0, 0, 0, 0, 0] = torch.inf
|
||||
return encoded
|
||||
|
||||
def upscale(samples, width, height, *_args):
|
||||
output = F.interpolate(samples, size=(height, width), mode="bilinear")
|
||||
if stage == "resize":
|
||||
output[0, 0, 0, 0] = torch.nan
|
||||
return output
|
||||
|
||||
with pytest.raises(PixelVaeResizeError, match="NaN or Inf"):
|
||||
resize_video_latents_via_vae(
|
||||
_latents((3,)),
|
||||
_plan(temporals=(3,)),
|
||||
video_vae=NonFiniteVae([]),
|
||||
target_width=36,
|
||||
target_height=16,
|
||||
upscale_fn=upscale,
|
||||
)
|
||||
@@ -0,0 +1,248 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import MARK_VIDEO_CONTEXT
|
||||
from ComfyUI_H3_Continuum_Join.v3 import refine_context as rc
|
||||
|
||||
|
||||
def _upscale(samples, width, height, method, crop):
|
||||
del method, crop
|
||||
return torch.nn.functional.interpolate(
|
||||
samples, size=(height, width), mode="nearest"
|
||||
)
|
||||
|
||||
|
||||
def _conditioning():
|
||||
text = torch.arange(6, dtype=torch.float32, device="cpu").reshape(1, 2, 3)
|
||||
first = torch.full((1, 24, 1, 2, 3), 1.0)
|
||||
last = torch.full((1, 24, 1, 2, 3), 2.0)
|
||||
ordinary_video = torch.full((1, 24, 2, 4, 5), 3.0)
|
||||
ordinary_audio = torch.full((1, 32, 2, 7), 4.0)
|
||||
context_video = torch.arange(1 * 24 * 2 * 2 * 3, dtype=torch.float32).reshape(
|
||||
1, 24, 2, 2, 3
|
||||
)
|
||||
context_audio = torch.full((1, 32, 2, 9), 5.0)
|
||||
return [
|
||||
[
|
||||
text,
|
||||
{
|
||||
"minimax_token_tags": torch.tensor([1, 0]),
|
||||
"minimax_frame_count": 124,
|
||||
"minimax_keyframes": [
|
||||
{"resolved_frame_index": 0, "latent": first},
|
||||
{"resolved_frame_index": 123, "latent": last},
|
||||
],
|
||||
"minimax_refs": [
|
||||
{
|
||||
"kind": "video_audio",
|
||||
"latent_t": 2,
|
||||
"latent_h": 4,
|
||||
"latent_w": 5,
|
||||
"latent": ordinary_video,
|
||||
"ref_audio_t": 7,
|
||||
"audio_latent": ordinary_audio,
|
||||
},
|
||||
{
|
||||
"kind": "video_audio",
|
||||
"latent_t": 2,
|
||||
"latent_h": 2,
|
||||
"latent_w": 3,
|
||||
"latent": context_video,
|
||||
"ref_audio_t": 9,
|
||||
"audio_latent": context_audio,
|
||||
MARK_VIDEO_CONTEXT: True,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
def _group(**overrides):
|
||||
values = {
|
||||
"conditioning": _conditioning(),
|
||||
"group_id": 0,
|
||||
"logical_chunks": [1],
|
||||
"physical_frames": 124,
|
||||
"prompt_policy": "single",
|
||||
"physical_prompt": "prompt",
|
||||
"source_video_shape": (1, 24, 37, 2, 3),
|
||||
"physical_clip_index": 1,
|
||||
"context_frames": 0,
|
||||
"first_image": torch.ones((1, 32, 48, 3)),
|
||||
"last_image": torch.zeros((1, 32, 48, 3)),
|
||||
}
|
||||
values.update(overrides)
|
||||
return rc.make_refine_group(**values)
|
||||
|
||||
|
||||
def test_freeze_conditioning_owns_cpu_tensors_and_immutable_containers():
|
||||
source = _conditioning()
|
||||
frozen = rc.freeze_conditioning(source)
|
||||
assert isinstance(frozen, tuple)
|
||||
assert isinstance(frozen[0], tuple)
|
||||
assert isinstance(frozen[0][1], Mapping)
|
||||
assert isinstance(frozen[0][1]["minimax_refs"], tuple)
|
||||
assert frozen[0][0].device.type == "cpu"
|
||||
assert frozen[0][0] is not source[0][0]
|
||||
assert frozen[0][0].data_ptr() != source[0][0].data_ptr()
|
||||
source[0][0].fill_(99)
|
||||
source[0][1]["minimax_refs"][0]["audio_latent"].fill_(99)
|
||||
assert not bool((frozen[0][0] == 99).any())
|
||||
assert not bool(
|
||||
(frozen[0][1]["minimax_refs"][0]["audio_latent"] == 99).any()
|
||||
)
|
||||
with pytest.raises(TypeError):
|
||||
frozen[0][1]["new"] = True
|
||||
|
||||
|
||||
def test_adapt_resizes_keyframes_and_only_marked_video_context(monkeypatch):
|
||||
monkeypatch.setattr(rc, "_common_upscale", _upscale)
|
||||
group = _group(first_image=None, last_image=None)
|
||||
frozen_meta = group["conditioning"][0][1]
|
||||
ordinary_ref = frozen_meta["minimax_refs"][0]
|
||||
context_ref = frozen_meta["minimax_refs"][1]
|
||||
ordinary_video = ordinary_ref["latent"]
|
||||
ordinary_audio = ordinary_ref["audio_latent"]
|
||||
context_audio = context_ref["audio_latent"]
|
||||
|
||||
adapted, stats = rc.adapt_group_conditioning(group, 4, 6)
|
||||
metadata = adapted[0][1]
|
||||
assert adapted[0][0] is group["conditioning"][0][0]
|
||||
assert [tuple(item["latent"].shape[-2:]) for item in metadata["minimax_keyframes"]] == [
|
||||
(4, 6),
|
||||
(4, 6),
|
||||
]
|
||||
assert metadata["minimax_refs"][0] is ordinary_ref
|
||||
assert metadata["minimax_refs"][0]["latent"] is ordinary_video
|
||||
assert metadata["minimax_refs"][0]["audio_latent"] is ordinary_audio
|
||||
assert tuple(metadata["minimax_refs"][1]["latent"].shape[-2:]) == (4, 6)
|
||||
expected_context = torch.nn.functional.interpolate(
|
||||
context_ref["latent"].permute(0, 2, 1, 3, 4).reshape(2, 24, 2, 3),
|
||||
size=(4, 6),
|
||||
mode="nearest",
|
||||
).reshape(1, 2, 24, 4, 6).permute(0, 2, 1, 3, 4)
|
||||
assert torch.equal(metadata["minimax_refs"][1]["latent"], expected_context)
|
||||
assert metadata["minimax_refs"][1]["latent_h"] == 4
|
||||
assert metadata["minimax_refs"][1]["latent_w"] == 6
|
||||
assert metadata["minimax_refs"][1]["audio_latent"] is context_audio
|
||||
assert context_ref["latent_h"] == 2
|
||||
assert context_ref["latent_w"] == 3
|
||||
assert tuple(context_ref["latent"].shape[-2:]) == (2, 3)
|
||||
assert stats["keyframes_latent_fallback"] == 2
|
||||
assert stats["context_refs_resized"] == 1
|
||||
assert len(stats["warnings"]) == 2
|
||||
|
||||
|
||||
class _FakeVAE:
|
||||
def __init__(self):
|
||||
self.inputs = []
|
||||
|
||||
def encode(self, image):
|
||||
self.inputs.append(image.clone())
|
||||
batch, height, width, _ = image.shape
|
||||
return torch.full((batch, 24, 1, height // 16, width // 16), len(self.inputs))
|
||||
|
||||
|
||||
def test_keyframes_reencode_images_with_core_first_last_crop_rules(monkeypatch):
|
||||
calls = []
|
||||
|
||||
def upscale(samples, width, height, method, crop):
|
||||
calls.append((width, height, method, crop))
|
||||
return torch.nn.functional.interpolate(samples, size=(height, width), mode="nearest")
|
||||
|
||||
monkeypatch.setattr(rc, "_common_upscale", upscale)
|
||||
vae = _FakeVAE()
|
||||
adapted, stats = rc.adapt_group_conditioning(_group(), 4, 6, video_vae=vae)
|
||||
keyframes = adapted[0][1]["minimax_keyframes"]
|
||||
assert torch.all(keyframes[0]["latent"] == 1)
|
||||
assert torch.all(keyframes[1]["latent"] == 2)
|
||||
assert calls[:2] == [
|
||||
(96, 64, "lanczos", "disabled"),
|
||||
(96, 64, "lanczos", "center"),
|
||||
]
|
||||
assert stats["keyframes_reencoded"] == 2
|
||||
assert stats["keyframes_latent_fallback"] == 0
|
||||
assert stats["warnings"] == ()
|
||||
|
||||
|
||||
def test_context_validation_matches_physical_group_assembly_contract():
|
||||
group = _group()
|
||||
context = rc.make_refine_context(
|
||||
[group],
|
||||
source_width=48,
|
||||
source_height=32,
|
||||
conditioning_mode="fl2va",
|
||||
)
|
||||
plan = {
|
||||
"width": 48,
|
||||
"height": 32,
|
||||
"second_pass_contract": {
|
||||
"physical_groups": [
|
||||
{
|
||||
"group_id": 0,
|
||||
"logical_chunks": [1],
|
||||
"physical_frames": 124,
|
||||
"prompt_policy": "single",
|
||||
"physical_prompt": "prompt",
|
||||
"trim_prefix_frames": 0,
|
||||
"source_batch": 1,
|
||||
"latent_channels": 24,
|
||||
"source_latent_t": 37,
|
||||
"source_latent_h": 2,
|
||||
"source_latent_w": 3,
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
assert rc.validate_refine_context(context, plan) is context
|
||||
assert context["groups"] == (group,)
|
||||
assert context["complete"] is True
|
||||
bad = {**plan, "width": 64}
|
||||
with pytest.raises(rc.RefineContextError, match="dimensions differ"):
|
||||
rc.validate_refine_context(context, bad)
|
||||
|
||||
|
||||
def test_incomplete_context_may_be_empty_but_complete_context_may_not():
|
||||
context = rc.make_refine_context(
|
||||
[],
|
||||
source_width=48,
|
||||
source_height=32,
|
||||
conditioning_mode="t2va",
|
||||
complete=False,
|
||||
notes=("Run Storage reused groups were not recaptured",),
|
||||
)
|
||||
assert context["groups"] == ()
|
||||
assert context["notes"]
|
||||
partial = rc.make_refine_context(
|
||||
[
|
||||
_group(
|
||||
logical_chunks=[3],
|
||||
physical_clip_index=3,
|
||||
)
|
||||
],
|
||||
source_width=48,
|
||||
source_height=32,
|
||||
conditioning_mode="t2va",
|
||||
complete=False,
|
||||
)
|
||||
# Partial ids are capture-local. They must not be paired by index with a
|
||||
# plan group, because an earlier Run Storage group may be absent.
|
||||
plan = {
|
||||
"width": 48,
|
||||
"height": 32,
|
||||
"second_pass_contract": {"physical_groups": [{"logical_chunks": [1]}]},
|
||||
}
|
||||
assert rc.validate_refine_context(partial, plan) is partial
|
||||
with pytest.raises(rc.RefineContextError, match="no physical groups"):
|
||||
rc.make_refine_context(
|
||||
[],
|
||||
source_width=48,
|
||||
source_height=32,
|
||||
conditioning_mode="t2va",
|
||||
complete=True,
|
||||
)
|
||||
@@ -0,0 +1,112 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v3 import refine_sampling
|
||||
|
||||
|
||||
class _Nested:
|
||||
is_nested = True
|
||||
|
||||
def __init__(self, tensors):
|
||||
self.tensors = list(tensors)
|
||||
|
||||
def unbind(self):
|
||||
return self.tensors
|
||||
|
||||
def to(self, *_args, **_kwargs):
|
||||
return self
|
||||
|
||||
|
||||
def test_refine_noise_is_random_for_video_zero_for_audio_with_minimal_masks():
|
||||
video = torch.zeros((1, 24, 3, 4, 5), dtype=torch.float16)
|
||||
audio = torch.ones((1, 32, 2, 12), dtype=torch.float16)
|
||||
calls = []
|
||||
|
||||
def prepare_noise(samples, seed, batch_inds):
|
||||
calls.append((samples, seed, batch_inds))
|
||||
return torch.full_like(samples, 0.75, device="cpu")
|
||||
|
||||
noise, mask = refine_sampling._prepare_refine_noise_and_mask(
|
||||
video,
|
||||
audio,
|
||||
seed=123,
|
||||
batch_inds=[0],
|
||||
prepare_noise_fn=prepare_noise,
|
||||
nested_builder=_Nested,
|
||||
)
|
||||
|
||||
video_noise, audio_noise = noise.unbind()
|
||||
video_mask, audio_mask = mask.unbind()
|
||||
assert calls == [(video, 123, [0])]
|
||||
assert torch.equal(video_noise, torch.full_like(video_noise, 0.75))
|
||||
assert torch.count_nonzero(audio_noise) == 0
|
||||
assert audio_noise.dtype == audio.dtype
|
||||
assert tuple(video_mask.shape) == (1, 1, 1, 1, 1)
|
||||
assert tuple(audio_mask.shape) == (1, 1, 1, 1)
|
||||
assert torch.all(video_mask == 1)
|
||||
assert torch.all(audio_mask == 0)
|
||||
|
||||
|
||||
def test_refine_sampler_passes_original_av_latent_and_workflow_sigmas_to_core(monkeypatch):
|
||||
video = torch.randn((1, 24, 3, 4, 4))
|
||||
audio = torch.randn((1, 32, 2, 12))
|
||||
latent_samples = _Nested((video, audio))
|
||||
latent = {"samples": latent_samples}
|
||||
sigmas = torch.tensor([0.35, 0.2, 0.0])
|
||||
captured = {}
|
||||
|
||||
class _Guider:
|
||||
def sample(self, noise, latent_image, sampler, supplied_sigmas, **kwargs):
|
||||
captured.update(
|
||||
noise=noise,
|
||||
latent_image=latent_image,
|
||||
sampler=sampler,
|
||||
sigmas=supplied_sigmas,
|
||||
**kwargs,
|
||||
)
|
||||
return latent_image
|
||||
|
||||
def prepare_noise(samples, seed, batch_inds):
|
||||
captured["prepare_noise"] = (samples, seed, batch_inds)
|
||||
return torch.full_like(samples, 0.5, device="cpu")
|
||||
|
||||
fake_sample = SimpleNamespace(
|
||||
fix_empty_latent_channels=lambda _model, samples, _spatial, _temporal: samples,
|
||||
prepare_noise=prepare_noise,
|
||||
)
|
||||
fake_runtime = (
|
||||
SimpleNamespace(intermediate_device=lambda: torch.device("cpu")),
|
||||
SimpleNamespace(NestedTensor=_Nested),
|
||||
fake_sample,
|
||||
SimpleNamespace(PROGRESS_BAR_ENABLED=False),
|
||||
SimpleNamespace(prepare_callback=lambda *_args: None),
|
||||
)
|
||||
monkeypatch.setattr(refine_sampling, "_load_runtime_modules", lambda: fake_runtime)
|
||||
monkeypatch.setattr(refine_sampling, "_make_basic_guider", lambda *_args: _Guider())
|
||||
|
||||
sampler = object()
|
||||
output = refine_sampling.sample_refine_chunk(
|
||||
model=object(),
|
||||
conditioning=["conditioning"],
|
||||
latent=latent,
|
||||
sampler=sampler,
|
||||
sigmas=sigmas,
|
||||
seed=987,
|
||||
enable_preview=False,
|
||||
)
|
||||
|
||||
assert captured["prepare_noise"] == (video, 987, None)
|
||||
assert captured["latent_image"] is latent_samples
|
||||
assert captured["latent_image"].unbind()[1] is audio
|
||||
assert captured["sigmas"] is sigmas
|
||||
assert captured["sampler"] is sampler
|
||||
video_noise, audio_noise = captured["noise"].unbind()
|
||||
video_mask, audio_mask = captured["denoise_mask"].unbind()
|
||||
assert torch.equal(video_noise, torch.full_like(video_noise, 0.5))
|
||||
assert torch.count_nonzero(audio_noise) == 0
|
||||
assert torch.all(video_mask == 1)
|
||||
assert torch.all(audio_mask == 0)
|
||||
assert output["samples"] is latent_samples
|
||||
@@ -0,0 +1,145 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v3.plan import _attach_second_pass_contract
|
||||
from ComfyUI_H3_Continuum_Join.v3.second_pass import (
|
||||
SecondPassContractError,
|
||||
derive_refine_seed,
|
||||
passthrough_audio_latents,
|
||||
update_second_pass_geometry,
|
||||
validate_second_pass_inputs,
|
||||
)
|
||||
|
||||
|
||||
def _latent(samples: torch.Tensor) -> dict[str, torch.Tensor]:
|
||||
return {"samples": samples}
|
||||
|
||||
|
||||
def _physical_entry(t: int, audio_t: int, *, prompt: str = "") -> dict:
|
||||
return {
|
||||
"video": torch.zeros((1, 24, t, 40, 40)),
|
||||
"audio": torch.zeros((1, 8, audio_t)),
|
||||
"prompt": prompt,
|
||||
}
|
||||
|
||||
|
||||
def _plan_and_latents(*, terminal: bool = False):
|
||||
if terminal:
|
||||
logical = [
|
||||
_physical_entry(31, 100, prompt="first"),
|
||||
_physical_entry(35, 110, prompt="middle"),
|
||||
_physical_entry(35, 110, prompt="last"),
|
||||
]
|
||||
physical = [logical[0], _physical_entry(65, 220)]
|
||||
plan = {
|
||||
"width": 640,
|
||||
"height": 640,
|
||||
"chunks": [
|
||||
{"total_frames": 124, "trim_frames": 0},
|
||||
{"total_frames": 141, "trim_frames": 22},
|
||||
{"total_frames": 141, "trim_frames": 22},
|
||||
],
|
||||
"decode_groups": [
|
||||
{
|
||||
"logical_chunk_indices": [1],
|
||||
"terminal_merged": False,
|
||||
"total_frames": 124,
|
||||
"trim_frames": 0,
|
||||
},
|
||||
{
|
||||
"logical_chunk_indices": [2, 3],
|
||||
"terminal_merged": True,
|
||||
"total_frames": 260,
|
||||
"trim_frames": 22,
|
||||
},
|
||||
],
|
||||
}
|
||||
else:
|
||||
logical = [_physical_entry(31, 100, prompt="single")]
|
||||
physical = list(logical)
|
||||
plan = {
|
||||
"width": 640,
|
||||
"height": 640,
|
||||
"chunks": [{"total_frames": 124, "trim_frames": 0}],
|
||||
}
|
||||
_attach_second_pass_contract(
|
||||
plan,
|
||||
logical_entries=logical,
|
||||
physical_entries=physical,
|
||||
chunk_seconds=5.0,
|
||||
)
|
||||
videos = [_latent(entry["video"]) for entry in physical]
|
||||
audios = [_latent(entry["audio"]) for entry in physical]
|
||||
return plan, videos, audios
|
||||
|
||||
|
||||
def test_terminal_contract_preserves_physical_group_and_prompt() -> None:
|
||||
plan, videos, audios = _plan_and_latents(terminal=True)
|
||||
result = validate_second_pass_inputs(videos, audios, plan)
|
||||
groups = plan["second_pass_contract"]["physical_groups"]
|
||||
assert result["physical_group_count"] == 2
|
||||
assert groups[1]["logical_chunks"] == [2, 3]
|
||||
assert groups[1]["physical_frames"] == 260
|
||||
assert groups[1]["trim_prefix_frames"] == 22
|
||||
assert groups[1]["prompt_policy"] == "paired_timeline_v1"
|
||||
assert groups[1]["physical_prompt"] == "[0-5s]\nmiddle\n\n[5-10s]\nlast"
|
||||
|
||||
|
||||
def test_spatial_upscale_updates_only_target_geometry() -> None:
|
||||
plan, videos, audios = _plan_and_latents(terminal=True)
|
||||
upscaled = [
|
||||
_latent(item["samples"].new_zeros((1, 24, item["samples"].shape[2], 60, 60)))
|
||||
for item in videos
|
||||
]
|
||||
validate_second_pass_inputs(upscaled, audios, plan)
|
||||
updated = update_second_pass_geometry(plan, upscaled)
|
||||
assert (plan["width"], plan["height"]) == (640, 640)
|
||||
assert (updated["width"], updated["height"]) == (960, 960)
|
||||
assert updated["second_pass_contract"]["target_width"] == 960
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channels", [23, 25])
|
||||
def test_rejects_non_h3_video_channels(channels: int) -> None:
|
||||
plan, videos, audios = _plan_and_latents()
|
||||
videos[0] = _latent(torch.zeros((1, channels, 31, 40, 40)))
|
||||
with pytest.raises(SecondPassContractError, match="24 channels"):
|
||||
validate_second_pass_inputs(videos, audios, plan)
|
||||
|
||||
|
||||
def test_rejects_temporal_or_group_count_changes() -> None:
|
||||
plan, videos, audios = _plan_and_latents(terminal=True)
|
||||
changed_t = copy.deepcopy(videos)
|
||||
changed_t[1] = _latent(torch.zeros((1, 24, 64, 40, 40)))
|
||||
with pytest.raises(SecondPassContractError, match="temporal length"):
|
||||
validate_second_pass_inputs(changed_t, audios, plan)
|
||||
with pytest.raises(SecondPassContractError, match="group count"):
|
||||
validate_second_pass_inputs(videos[:1], audios, plan)
|
||||
|
||||
|
||||
def test_rejects_group_order_and_inconsistent_spatial_sizes() -> None:
|
||||
plan, videos, audios = _plan_and_latents(terminal=True)
|
||||
with pytest.raises(SecondPassContractError):
|
||||
validate_second_pass_inputs(list(reversed(videos)), list(reversed(audios)), plan)
|
||||
videos[1] = _latent(torch.zeros((1, 24, 65, 48, 48)))
|
||||
with pytest.raises(SecondPassContractError, match="same target H/W"):
|
||||
validate_second_pass_inputs(videos, audios, plan)
|
||||
|
||||
|
||||
def test_audio_passthrough_is_bit_exact_and_keeps_latent_objects() -> None:
|
||||
_, _, audios = _plan_and_latents(terminal=True)
|
||||
output = passthrough_audio_latents(audios)
|
||||
assert all(before is after for before, after in zip(audios, output, strict=True))
|
||||
assert all(
|
||||
torch.equal(before["samples"], after["samples"])
|
||||
for before, after in zip(audios, output, strict=True)
|
||||
)
|
||||
|
||||
|
||||
def test_refine_seed_is_deterministic_and_namespaced_per_group() -> None:
|
||||
assert derive_refine_seed(42, 0) == derive_refine_seed(42, 0)
|
||||
assert derive_refine_seed(42, 0) != derive_refine_seed(42, 1)
|
||||
assert derive_refine_seed(42, 0) != derive_refine_seed(43, 0)
|
||||
@@ -0,0 +1,499 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import torch
|
||||
import pytest
|
||||
|
||||
import ComfyUI_H3_Continuum_Join.v3.second_pass as second_pass_module
|
||||
from ComfyUI_H3_Continuum_Join.nodes import NODE_CLASS_MAPPINGS
|
||||
from ComfyUI_H3_Continuum_Join.v3.second_pass import (
|
||||
SecondPassContractError,
|
||||
derive_refine_seed,
|
||||
run_second_pass_groups,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.refine_context import (
|
||||
MAGIC as REFINE_CONTEXT_MAGIC,
|
||||
RefineConditioningAdaptationError,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v3.second_pass_nodes import (
|
||||
H3ContinuumSecondPassV35,
|
||||
)
|
||||
|
||||
|
||||
def _latent(samples: torch.Tensor) -> dict[str, torch.Tensor]:
|
||||
return {"samples": samples}
|
||||
|
||||
|
||||
def _plan(groups: list[dict]) -> dict:
|
||||
return {
|
||||
"width": 640,
|
||||
"height": 640,
|
||||
"second_pass_contract": {
|
||||
"version": 1,
|
||||
"physical_groups": groups,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _group(group_id: int, *, temporal: int, prompt: str, terminal: bool = False) -> dict:
|
||||
return {
|
||||
"group_id": group_id,
|
||||
"logical_chunks": [group_id + 1] if not terminal else [2, 3],
|
||||
"physical_prompt": prompt,
|
||||
"prompt_policy": "single" if not terminal else "paired_timeline_v1",
|
||||
"physical_frames": 124 if not terminal else 260,
|
||||
"trim_prefix_frames": 0 if not terminal else 22,
|
||||
"terminal_merged": terminal,
|
||||
"source_width": 640,
|
||||
"source_height": 640,
|
||||
"source_batch": 1,
|
||||
"latent_channels": 24,
|
||||
"source_latent_t": temporal,
|
||||
"source_latent_h": 40,
|
||||
"source_latent_w": 40,
|
||||
"source_audio_shape": [1, 8, temporal * 4],
|
||||
}
|
||||
|
||||
|
||||
def _run_fixture(groups: list[dict]):
|
||||
videos = [
|
||||
_latent(torch.zeros((1, 24, group["source_latent_t"], 48, 48)))
|
||||
for group in groups
|
||||
]
|
||||
audios = [
|
||||
_latent(torch.full(tuple(group["source_audio_shape"]), float(index + 1)))
|
||||
for index, group in enumerate(groups)
|
||||
]
|
||||
calls: list[dict] = []
|
||||
|
||||
def encode_prompt(_clip, prompt, **kwargs):
|
||||
assert kwargs["first_image"] is None
|
||||
assert kwargs["last_image"] is None
|
||||
calls.append({"prompt": prompt})
|
||||
return [prompt]
|
||||
|
||||
def build_latent(video, audio):
|
||||
return {"video": video, "audio": audio}
|
||||
|
||||
def clone_model(model, **kwargs):
|
||||
calls[-1]["clone"] = kwargs
|
||||
return model
|
||||
|
||||
def sample(**kwargs):
|
||||
calls[-1]["seed"] = kwargs["seed"]
|
||||
calls[-1]["conditioning"] = kwargs["conditioning"]
|
||||
nested = kwargs["latent"]
|
||||
return {
|
||||
"video": nested["video"] + 1,
|
||||
"audio": nested["audio"] + 99,
|
||||
}
|
||||
|
||||
def extract(sampled):
|
||||
return sampled["video"], sampled["audio"]
|
||||
|
||||
result = run_second_pass_groups(
|
||||
model=object(),
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=videos,
|
||||
audio_latents=audios,
|
||||
assembly_plan=_plan(groups),
|
||||
refine_seed=42,
|
||||
encode_prompt_fn=encode_prompt,
|
||||
latent_builder=build_latent,
|
||||
sample_fn=sample,
|
||||
stream_extractor=extract,
|
||||
clone_model_fn=clone_model,
|
||||
)
|
||||
return videos, audios, calls, result
|
||||
|
||||
|
||||
def test_second_pass_uses_plan_prompts_one_seed_per_physical_group_and_audio_passthrough():
|
||||
groups = [
|
||||
_group(0, temporal=31, prompt="first physical prompt"),
|
||||
_group(1, temporal=65, prompt="terminal physical prompt", terminal=True),
|
||||
]
|
||||
videos, audios, calls, (refined, output_audio, updated, report) = _run_fixture(groups)
|
||||
|
||||
assert [call["prompt"] for call in calls] == [
|
||||
"first physical prompt",
|
||||
"terminal physical prompt",
|
||||
]
|
||||
assert [call["seed"] for call in calls] == [
|
||||
derive_refine_seed(42, 0),
|
||||
derive_refine_seed(42, 1),
|
||||
]
|
||||
assert [call["clone"] for call in calls] == [
|
||||
{
|
||||
"strict": False,
|
||||
"debug": False,
|
||||
"chunk_index": 1,
|
||||
"context_frames": None,
|
||||
},
|
||||
{
|
||||
"strict": False,
|
||||
"debug": False,
|
||||
"chunk_index": 2,
|
||||
"context_frames": 22,
|
||||
},
|
||||
]
|
||||
assert all(after is before for before, after in zip(audios, output_audio, strict=True))
|
||||
assert all(torch.equal(item["samples"], torch.ones_like(item["samples"])) for item in refined)
|
||||
assert [item["samples"].shape[2] for item in refined] == [31, 65]
|
||||
assert updated["width"] == 768
|
||||
assert updated["height"] == 768
|
||||
assert updated["second_pass_contract"]["audio_output"] == (
|
||||
"bit_exact_first_pass_passthrough"
|
||||
)
|
||||
assert updated["second_pass_contract"]["execution"] == (
|
||||
"t2va_physical_groups_audio_locked_v2"
|
||||
)
|
||||
assert updated["second_pass_contract"]["audio_sampling"] == (
|
||||
"zero_noise_mask_locked"
|
||||
)
|
||||
assert "temporary_audio_discarded=true" in report
|
||||
assert "audio zero noise/mask=0" in report
|
||||
assert "refine_seed=" in report
|
||||
assert videos[1]["samples"].shape[2] == 65
|
||||
|
||||
|
||||
def test_second_pass_defaults_to_audio_locked_refine_sampler(monkeypatch):
|
||||
group = _group(0, temporal=31, prompt="physical prompt")
|
||||
video = _latent(torch.zeros((1, 24, 31, 48, 48)))
|
||||
audio = _latent(torch.ones(tuple(group["source_audio_shape"])))
|
||||
captured = {}
|
||||
|
||||
def fake_refine_sample(**kwargs):
|
||||
captured.update(kwargs)
|
||||
return kwargs["latent"]
|
||||
|
||||
monkeypatch.setattr(
|
||||
second_pass_module,
|
||||
"sample_refine_chunk",
|
||||
fake_refine_sample,
|
||||
)
|
||||
run_second_pass_groups(
|
||||
model=object(),
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=[video],
|
||||
audio_latents=[audio],
|
||||
assembly_plan=_plan([group]),
|
||||
refine_seed=17,
|
||||
encode_prompt_fn=lambda *_args, **_kwargs: ["conditioning"],
|
||||
latent_builder=lambda video_samples, audio_samples: {
|
||||
"video": video_samples,
|
||||
"audio": audio_samples,
|
||||
},
|
||||
stream_extractor=lambda sampled: (sampled["video"], sampled["audio"]),
|
||||
clone_model_fn=lambda source, **_kwargs: source,
|
||||
)
|
||||
|
||||
assert captured["latent"]["video"] is video["samples"]
|
||||
assert captured["latent"]["audio"] is audio["samples"]
|
||||
assert captured["seed"] == derive_refine_seed(17, 0)
|
||||
|
||||
|
||||
def test_complete_refine_context_skips_prompt_encode_and_clones_each_group_model():
|
||||
groups = [
|
||||
_group(0, temporal=31, prompt="unused first prompt"),
|
||||
_group(1, temporal=65, prompt="unused terminal prompt", terminal=True),
|
||||
]
|
||||
videos = [
|
||||
_latent(torch.zeros((1, 24, group["source_latent_t"], 48, 48)))
|
||||
for group in groups
|
||||
]
|
||||
audios = [
|
||||
_latent(torch.ones(tuple(group["source_audio_shape"]))) for group in groups
|
||||
]
|
||||
context_groups = (
|
||||
{
|
||||
"group_id": 0,
|
||||
"logical_chunks": (1,),
|
||||
"physical_clip_index": 4,
|
||||
"context_frames": 0,
|
||||
},
|
||||
{
|
||||
"group_id": 1,
|
||||
"logical_chunks": (2, 3),
|
||||
"physical_clip_index": 5,
|
||||
"context_frames": 22,
|
||||
},
|
||||
)
|
||||
refine_context = {
|
||||
"magic": REFINE_CONTEXT_MAGIC,
|
||||
"complete": True,
|
||||
"groups": context_groups,
|
||||
}
|
||||
adapted_calls = []
|
||||
clone_calls = []
|
||||
sample_calls = []
|
||||
video_vae = object()
|
||||
|
||||
def unexpected_prompt_encode(*_args, **_kwargs):
|
||||
raise AssertionError("complete refine_context must bypass prompt encoding")
|
||||
|
||||
def validate_context(value, *, assembly_plan):
|
||||
assert value is refine_context
|
||||
assert assembly_plan is plan
|
||||
return value
|
||||
|
||||
def adapt(group, **kwargs):
|
||||
adapted_calls.append((group, kwargs))
|
||||
return [f"captured-{group['group_id']}"], {
|
||||
"keyframes_resized": group["group_id"],
|
||||
"keyframes_reencoded": 0,
|
||||
"context_refs_resized": 0,
|
||||
"warnings": (),
|
||||
}
|
||||
|
||||
def clone(source, **kwargs):
|
||||
clone_calls.append((source, kwargs))
|
||||
return f"group-model-{kwargs['chunk_index']}"
|
||||
|
||||
def sample(**kwargs):
|
||||
sample_calls.append(kwargs)
|
||||
return kwargs["latent"]
|
||||
|
||||
plan = _plan(groups)
|
||||
refined, output_audio, updated, report = run_second_pass_groups(
|
||||
model="source-model",
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=videos,
|
||||
audio_latents=audios,
|
||||
assembly_plan=plan,
|
||||
refine_seed=42,
|
||||
refine_context=refine_context,
|
||||
video_vae=video_vae,
|
||||
encode_prompt_fn=unexpected_prompt_encode,
|
||||
latent_builder=lambda video, audio: {"video": video, "audio": audio},
|
||||
sample_fn=sample,
|
||||
stream_extractor=lambda value: (value["video"], value["audio"]),
|
||||
clone_model_fn=clone,
|
||||
validate_refine_context_fn=validate_context,
|
||||
adapt_group_conditioning_fn=adapt,
|
||||
)
|
||||
|
||||
assert [call[0] for call in adapted_calls] == list(context_groups)
|
||||
assert all(call[1] == {
|
||||
"target_latent_h": 48,
|
||||
"target_latent_w": 48,
|
||||
"video_vae": video_vae,
|
||||
"upscale_method": "bilinear",
|
||||
} for call in adapted_calls)
|
||||
assert [call[1] for call in clone_calls] == [
|
||||
{
|
||||
"strict": False,
|
||||
"debug": False,
|
||||
"chunk_index": 4,
|
||||
"context_frames": None,
|
||||
},
|
||||
{
|
||||
"strict": False,
|
||||
"debug": False,
|
||||
"chunk_index": 5,
|
||||
"context_frames": 22,
|
||||
},
|
||||
]
|
||||
assert [call["model"] for call in sample_calls] == [
|
||||
"group-model-4",
|
||||
"group-model-5",
|
||||
]
|
||||
assert [call["conditioning"] for call in sample_calls] == [
|
||||
["captured-0"],
|
||||
["captured-1"],
|
||||
]
|
||||
assert all(before is after for before, after in zip(audios, output_audio, strict=True))
|
||||
assert all(
|
||||
item["samples"] is before["samples"]
|
||||
for item, before in zip(refined, videos, strict=True)
|
||||
)
|
||||
assert updated["second_pass_contract"]["execution"] == (
|
||||
"context_aware_physical_groups_audio_locked_v3"
|
||||
)
|
||||
assert "conditioning_source=refine_context" in report
|
||||
|
||||
|
||||
def test_unadaptable_complete_context_warns_and_uses_prompt_only_fallback():
|
||||
group = _group(0, temporal=31, prompt="fallback prompt")
|
||||
video = _latent(torch.zeros((1, 24, 31, 48, 48)))
|
||||
audio = _latent(torch.ones(tuple(group["source_audio_shape"])))
|
||||
refine_context = {
|
||||
"magic": REFINE_CONTEXT_MAGIC,
|
||||
"complete": True,
|
||||
"groups": ({"group_id": 0, "logical_chunks": (1,)},),
|
||||
}
|
||||
prompts = []
|
||||
|
||||
def unavailable(*_args, **_kwargs):
|
||||
raise RefineConditioningAdaptationError("target conversion unavailable")
|
||||
|
||||
_refined, output_audio, updated, report = run_second_pass_groups(
|
||||
model=object(),
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=[video],
|
||||
audio_latents=[audio],
|
||||
assembly_plan=_plan([group]),
|
||||
refine_seed=5,
|
||||
refine_context=refine_context,
|
||||
encode_prompt_fn=lambda _clip, prompt, **_kwargs: prompts.append(prompt) or [prompt],
|
||||
latent_builder=lambda video_samples, audio_samples: {
|
||||
"video": video_samples,
|
||||
"audio": audio_samples,
|
||||
},
|
||||
sample_fn=lambda **kwargs: kwargs["latent"],
|
||||
stream_extractor=lambda value: (value["video"], value["audio"]),
|
||||
clone_model_fn=lambda source, **_kwargs: source,
|
||||
validate_refine_context_fn=lambda value, **_kwargs: value,
|
||||
adapt_group_conditioning_fn=unavailable,
|
||||
)
|
||||
|
||||
assert prompts == ["fallback prompt"]
|
||||
assert output_audio[0] is audio
|
||||
assert updated["second_pass_contract"]["execution"] == (
|
||||
"t2va_physical_groups_audio_locked_v2"
|
||||
)
|
||||
assert "target adaptation was unavailable" in report
|
||||
assert "conditioning_source=prompt_only_fallback" in report
|
||||
|
||||
|
||||
def test_incomplete_refine_context_warns_and_preserves_legacy_prompt_path():
|
||||
group = _group(0, temporal=31, prompt="legacy prompt")
|
||||
video = _latent(torch.zeros((1, 24, 31, 48, 48)))
|
||||
audio = _latent(torch.ones(tuple(group["source_audio_shape"])))
|
||||
incomplete_context = {
|
||||
"magic": REFINE_CONTEXT_MAGIC,
|
||||
"complete": False,
|
||||
"groups": (),
|
||||
}
|
||||
prompts = []
|
||||
|
||||
def unexpected_adaptation(*_args, **_kwargs):
|
||||
raise AssertionError("incomplete context must not be adapted")
|
||||
|
||||
_refined, output_audio, updated, report = run_second_pass_groups(
|
||||
model=object(),
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=[video],
|
||||
audio_latents=[audio],
|
||||
assembly_plan=_plan([group]),
|
||||
refine_seed=5,
|
||||
refine_context=incomplete_context,
|
||||
encode_prompt_fn=lambda _clip, prompt, **_kwargs: prompts.append(prompt) or [prompt],
|
||||
latent_builder=lambda video_samples, audio_samples: {
|
||||
"video": video_samples,
|
||||
"audio": audio_samples,
|
||||
},
|
||||
sample_fn=lambda **kwargs: kwargs["latent"],
|
||||
stream_extractor=lambda value: (value["video"], value["audio"]),
|
||||
clone_model_fn=lambda source, **_kwargs: source,
|
||||
validate_refine_context_fn=lambda value, **_kwargs: value,
|
||||
adapt_group_conditioning_fn=unexpected_adaptation,
|
||||
)
|
||||
|
||||
assert prompts == ["legacy prompt"]
|
||||
assert output_audio[0] is audio
|
||||
assert updated["second_pass_contract"]["execution"] == (
|
||||
"t2va_physical_groups_audio_locked_v2"
|
||||
)
|
||||
assert "capture is incomplete" in report
|
||||
|
||||
|
||||
def test_corrupt_typed_refine_context_is_a_contract_error():
|
||||
group = _group(0, temporal=31, prompt="prompt")
|
||||
with pytest.raises(SecondPassContractError, match="refine_context contract"):
|
||||
run_second_pass_groups(
|
||||
model=object(),
|
||||
clip=object(),
|
||||
sampler=object(),
|
||||
sigmas=torch.tensor([0.3, 0.0]),
|
||||
video_latents=[_latent(torch.zeros((1, 24, 31, 48, 48)))],
|
||||
audio_latents=[_latent(torch.ones(tuple(group["source_audio_shape"])))],
|
||||
assembly_plan=_plan([group]),
|
||||
refine_seed=5,
|
||||
refine_context={"magic": REFINE_CONTEXT_MAGIC, "version": 999},
|
||||
)
|
||||
|
||||
|
||||
def test_experimental_node_unwraps_single_inputs_but_preserves_physical_lists(monkeypatch):
|
||||
captured = {}
|
||||
sentinel = (["video"], ["audio"], {"plan": True}, "status")
|
||||
|
||||
def fake_run(**kwargs):
|
||||
captured.update(kwargs)
|
||||
return sentinel
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.second_pass_nodes.run_second_pass_groups",
|
||||
fake_run,
|
||||
)
|
||||
node = H3ContinuumSecondPassV35()
|
||||
videos = [{"samples": torch.zeros((1, 24, 31, 40, 40))}]
|
||||
audios = [{"samples": torch.zeros((1, 8, 124))}]
|
||||
result = node.refine(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
videos,
|
||||
audios,
|
||||
[{"second_pass_contract": {}}],
|
||||
[7],
|
||||
)
|
||||
assert result == sentinel
|
||||
assert captured["video_latents"] is videos
|
||||
assert captured["audio_latents"] is audios
|
||||
assert captured["refine_seed"] == 7
|
||||
assert captured["refine_context"] is None
|
||||
assert captured["video_vae"] is None
|
||||
|
||||
|
||||
def test_second_pass_node_forwards_optional_refine_context_and_video_vae(monkeypatch):
|
||||
captured = {}
|
||||
sentinel = ([], [], {"plan": True}, "status")
|
||||
|
||||
def fake_run(**kwargs):
|
||||
captured.update(kwargs)
|
||||
return sentinel
|
||||
|
||||
monkeypatch.setattr(
|
||||
"ComfyUI_H3_Continuum_Join.v3.second_pass_nodes.run_second_pass_groups",
|
||||
fake_run,
|
||||
)
|
||||
context = {"context": True}
|
||||
video_vae = object()
|
||||
result = H3ContinuumSecondPassV35().refine(
|
||||
["model"],
|
||||
["clip"],
|
||||
["sampler"],
|
||||
[torch.tensor([0.3, 0.0])],
|
||||
[{"samples": torch.zeros((1, 24, 31, 40, 40))}],
|
||||
[{"samples": torch.zeros((1, 8, 124))}],
|
||||
[{"second_pass_contract": {}}],
|
||||
[7],
|
||||
[context],
|
||||
[video_vae],
|
||||
)
|
||||
|
||||
assert result == sentinel
|
||||
assert captured["refine_context"] is context
|
||||
assert captured["video_vae"] is video_vae
|
||||
|
||||
|
||||
def test_second_pass_node_is_registered_and_not_deprecated():
|
||||
node_class = NODE_CLASS_MAPPINGS["H3ContinuumSecondPassV35"]
|
||||
assert node_class is H3ContinuumSecondPassV35
|
||||
assert node_class.DEPRECATED is False
|
||||
assert node_class.INPUT_IS_LIST is True
|
||||
assert node_class.OUTPUT_IS_LIST == (True, True, False, False)
|
||||
assert node_class.INPUT_TYPES()["optional"] == {
|
||||
"refine_context": ("H3_CONTINUUM_REFINE_CONTEXT",),
|
||||
"video_vae": ("VAE",),
|
||||
}
|
||||
@@ -8,6 +8,8 @@ from ComfyUI_H3_Continuum_Join.v3 import plan as plan_module
|
||||
|
||||
def _logical_plan():
|
||||
return {
|
||||
"width": 1,
|
||||
"height": 1,
|
||||
"chunks": [
|
||||
{
|
||||
"sequence_index": 0,
|
||||
@@ -116,6 +118,8 @@ def test_three_logical_chunks_use_two_physical_decode_groups(monkeypatch):
|
||||
"audio": torch.zeros((1, 1, 207), dtype=torch.float32),
|
||||
}
|
||||
logical_plan = {
|
||||
"width": 1,
|
||||
"height": 1,
|
||||
"chunks": [
|
||||
{"sequence_index": 0, "chunk_index": 1, "total_frames": 124, "trim_frames": 0, "net_frames": 124, "context_frames": 0, "expected_video_latent_t": 37, "expected_audio_latent_t": 207, "frame_start": 0, "frame_stop": 124},
|
||||
{"sequence_index": 1, "chunk_index": 2, "total_frames": 141, "trim_frames": 22, "net_frames": 119, "context_frames": 22, "expected_video_latent_t": 42, "expected_audio_latent_t": 235, "frame_start": 124, "frame_stop": 243},
|
||||
|
||||
Reference in New Issue
Block a user