feat: finalize FL2VA terminal merge
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
"""Tests-only capture and comparison helpers for Terminal Merge diagnostics.
|
||||
|
||||
This module deliberately has no production imports or runtime hooks. Tests can
|
||||
monkeypatch Core and Continuum boundaries, feed the captured values here, and
|
||||
identify the first pipeline stage that differs without changing generation
|
||||
semantics.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from hashlib import sha256
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any, Mapping
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
STAGE_ORDER = (
|
||||
"A_pre_sampler",
|
||||
"B_first_model_call",
|
||||
"C_sampled_physical_av",
|
||||
"D_split_recombine",
|
||||
"E_decode_assembly",
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TensorFingerprint:
|
||||
shape: tuple[int, ...]
|
||||
dtype: str
|
||||
sha256: str
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"shape": list(self.shape),
|
||||
"dtype": self.dtype,
|
||||
"sha256": self.sha256,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TensorDifference:
|
||||
same_shape: bool
|
||||
same_dtype: bool
|
||||
bit_exact: bool
|
||||
allclose: bool
|
||||
max_abs_diff: float | None
|
||||
mean_abs_diff: float | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StageSnapshot:
|
||||
name: str
|
||||
sha256: str
|
||||
payload: Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StageComparison:
|
||||
name: str
|
||||
equal: bool
|
||||
core_sha256: str | None
|
||||
continuum_sha256: str | None
|
||||
missing_from: str | None = None
|
||||
|
||||
|
||||
def _tensor_raw_bytes(tensor: torch.Tensor) -> bytes:
|
||||
value = tensor.detach().to("cpu").contiguous()
|
||||
if value.numel() == 0:
|
||||
return b""
|
||||
return value.reshape(-1).view(torch.uint8).numpy().tobytes()
|
||||
|
||||
|
||||
def tensor_fingerprint(tensor: torch.Tensor) -> TensorFingerprint:
|
||||
if not torch.is_tensor(tensor):
|
||||
raise TypeError("tensor_fingerprint expects a torch.Tensor")
|
||||
return TensorFingerprint(
|
||||
shape=tuple(int(size) for size in tensor.shape),
|
||||
dtype=str(tensor.dtype),
|
||||
sha256=sha256(_tensor_raw_bytes(tensor)).hexdigest(),
|
||||
)
|
||||
|
||||
|
||||
def tensor_difference(
|
||||
core: torch.Tensor,
|
||||
continuum: torch.Tensor,
|
||||
*,
|
||||
rtol: float = 1.0e-5,
|
||||
atol: float = 1.0e-6,
|
||||
) -> TensorDifference:
|
||||
if not torch.is_tensor(core) or not torch.is_tensor(continuum):
|
||||
raise TypeError("tensor_difference expects two torch.Tensor values")
|
||||
|
||||
same_shape = tuple(core.shape) == tuple(continuum.shape)
|
||||
same_dtype = core.dtype == continuum.dtype
|
||||
bit_exact = (
|
||||
same_shape
|
||||
and same_dtype
|
||||
and tensor_fingerprint(core).sha256 == tensor_fingerprint(continuum).sha256
|
||||
)
|
||||
if not same_shape:
|
||||
return TensorDifference(
|
||||
same_shape=False,
|
||||
same_dtype=same_dtype,
|
||||
bit_exact=False,
|
||||
allclose=False,
|
||||
max_abs_diff=None,
|
||||
mean_abs_diff=None,
|
||||
)
|
||||
|
||||
core_value = core.detach().to("cpu")
|
||||
continuum_value = continuum.detach().to("cpu")
|
||||
if core_value.is_complex() or continuum_value.is_complex():
|
||||
core_numeric = core_value.to(torch.complex128)
|
||||
continuum_numeric = continuum_value.to(torch.complex128)
|
||||
else:
|
||||
core_numeric = core_value.to(torch.float64)
|
||||
continuum_numeric = continuum_value.to(torch.float64)
|
||||
|
||||
if core_numeric.numel() == 0:
|
||||
max_abs_diff = 0.0
|
||||
mean_abs_diff = 0.0
|
||||
else:
|
||||
absolute = (core_numeric - continuum_numeric).abs()
|
||||
max_abs_diff = float(absolute.max().item())
|
||||
mean_abs_diff = float(absolute.mean().item())
|
||||
|
||||
return TensorDifference(
|
||||
same_shape=True,
|
||||
same_dtype=same_dtype,
|
||||
bit_exact=bit_exact,
|
||||
allclose=bool(
|
||||
torch.allclose(
|
||||
core_numeric,
|
||||
continuum_numeric,
|
||||
rtol=rtol,
|
||||
atol=atol,
|
||||
equal_nan=True,
|
||||
)
|
||||
),
|
||||
max_abs_diff=max_abs_diff,
|
||||
mean_abs_diff=mean_abs_diff,
|
||||
)
|
||||
|
||||
|
||||
def packed_layout_snapshot(layout: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"signature": stable_snapshot(getattr(layout, "signature", None)),
|
||||
"segments": stable_snapshot(getattr(layout, "segments", None)),
|
||||
"position_ids": stable_snapshot(getattr(layout, "position_ids", None)),
|
||||
}
|
||||
|
||||
|
||||
def stable_snapshot(value: Any) -> Any:
|
||||
"""Convert diagnostic data to deterministic JSON-compatible state."""
|
||||
|
||||
if torch.is_tensor(value):
|
||||
return {"kind": "tensor", **tensor_fingerprint(value).as_dict()}
|
||||
if isinstance(value, TensorFingerprint):
|
||||
return {"kind": "tensor", **value.as_dict()}
|
||||
if value is None or isinstance(value, (bool, int, str)):
|
||||
return value
|
||||
if isinstance(value, float):
|
||||
return {"kind": "float", "hex": value.hex()}
|
||||
if isinstance(value, bytes):
|
||||
return {
|
||||
"kind": "bytes",
|
||||
"length": len(value),
|
||||
"sha256": sha256(value).hexdigest(),
|
||||
}
|
||||
if isinstance(value, (Path, torch.device, torch.dtype)):
|
||||
return str(value)
|
||||
if isinstance(value, Enum):
|
||||
return {
|
||||
"kind": "enum",
|
||||
"type": f"{type(value).__module__}.{type(value).__qualname__}",
|
||||
"value": stable_snapshot(value.value),
|
||||
}
|
||||
if isinstance(value, slice):
|
||||
return {
|
||||
"kind": "slice",
|
||||
"start": value.start,
|
||||
"stop": value.stop,
|
||||
"step": value.step,
|
||||
}
|
||||
if isinstance(value, Mapping):
|
||||
return {
|
||||
"kind": "mapping",
|
||||
"items": [
|
||||
[str(key), stable_snapshot(item)]
|
||||
for key, item in sorted(value.items(), key=lambda pair: str(pair[0]))
|
||||
],
|
||||
}
|
||||
if isinstance(value, tuple):
|
||||
return {"kind": "tuple", "items": [stable_snapshot(item) for item in value]}
|
||||
if isinstance(value, list):
|
||||
return {"kind": "list", "items": [stable_snapshot(item) for item in value]}
|
||||
if isinstance(value, (set, frozenset)):
|
||||
items = [stable_snapshot(item) for item in value]
|
||||
items.sort(key=lambda item: json.dumps(item, sort_keys=True, separators=(",", ":")))
|
||||
return {"kind": type(value).__name__, "items": items}
|
||||
if callable(value):
|
||||
return {
|
||||
"kind": "callable",
|
||||
"module": getattr(value, "__module__", type(value).__module__),
|
||||
"qualname": getattr(value, "__qualname__", type(value).__qualname__),
|
||||
}
|
||||
if all(hasattr(value, name) for name in ("signature", "segments", "position_ids")):
|
||||
return {"kind": "packed_layout", **packed_layout_snapshot(value)}
|
||||
if hasattr(value, "__dict__"):
|
||||
public_state = {
|
||||
key: item
|
||||
for key, item in vars(value).items()
|
||||
if not key.startswith("_") and not callable(item)
|
||||
}
|
||||
return {
|
||||
"kind": "object",
|
||||
"type": f"{type(value).__module__}.{type(value).__qualname__}",
|
||||
"state": stable_snapshot(public_state),
|
||||
}
|
||||
raise TypeError(f"Unsupported diagnostic value: {type(value)!r}")
|
||||
|
||||
|
||||
def snapshot_sha256(payload: Any) -> str:
|
||||
normalized = stable_snapshot(payload)
|
||||
encoded = json.dumps(
|
||||
normalized,
|
||||
ensure_ascii=True,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
).encode("utf-8")
|
||||
return sha256(encoded).hexdigest()
|
||||
|
||||
|
||||
class DiagnosticCapture:
|
||||
def __init__(self) -> None:
|
||||
self._stages: dict[str, StageSnapshot] = {}
|
||||
|
||||
@property
|
||||
def stages(self) -> Mapping[str, StageSnapshot]:
|
||||
return self._stages
|
||||
|
||||
def capture(self, stage: str, **payload: Any) -> StageSnapshot:
|
||||
if stage not in STAGE_ORDER:
|
||||
raise ValueError(f"Unknown diagnostic stage: {stage}")
|
||||
if stage in self._stages:
|
||||
raise ValueError(f"Diagnostic stage already captured: {stage}")
|
||||
normalized = stable_snapshot(payload)
|
||||
snapshot = StageSnapshot(
|
||||
name=stage,
|
||||
sha256=snapshot_sha256(normalized),
|
||||
payload=normalized,
|
||||
)
|
||||
self._stages[stage] = snapshot
|
||||
return snapshot
|
||||
|
||||
def capture_pre_sampler(
|
||||
self,
|
||||
*,
|
||||
physical_frames: int,
|
||||
seed: int,
|
||||
conditioning: Any,
|
||||
latent: Any,
|
||||
noise: torch.Tensor,
|
||||
keyframes: Any = None,
|
||||
qwen_images: Any = None,
|
||||
minimax_refs: Any = None,
|
||||
) -> StageSnapshot:
|
||||
return self.capture(
|
||||
"A_pre_sampler",
|
||||
physical_frames=physical_frames,
|
||||
seed=seed,
|
||||
conditioning=conditioning,
|
||||
latent=latent,
|
||||
noise=noise,
|
||||
keyframes=keyframes,
|
||||
qwen_images=qwen_images,
|
||||
minimax_refs=minimax_refs,
|
||||
)
|
||||
|
||||
def capture_first_model_call(
|
||||
self,
|
||||
*,
|
||||
minimax_payload: Any,
|
||||
transformer_options: Any,
|
||||
) -> StageSnapshot:
|
||||
return self.capture(
|
||||
"B_first_model_call",
|
||||
minimax_payload=minimax_payload,
|
||||
transformer_options=transformer_options,
|
||||
)
|
||||
|
||||
def capture_sampled_physical_av(
|
||||
self,
|
||||
*,
|
||||
video: torch.Tensor,
|
||||
audio: torch.Tensor,
|
||||
) -> StageSnapshot:
|
||||
return self.capture(
|
||||
"C_sampled_physical_av",
|
||||
video=video,
|
||||
audio=audio,
|
||||
)
|
||||
|
||||
def capture_split_recombine(
|
||||
self,
|
||||
*,
|
||||
physical_video: torch.Tensor,
|
||||
physical_audio: torch.Tensor,
|
||||
recombined_video: torch.Tensor,
|
||||
recombined_audio: torch.Tensor,
|
||||
) -> StageSnapshot:
|
||||
return self.capture(
|
||||
"D_split_recombine",
|
||||
physical_video=physical_video,
|
||||
physical_audio=physical_audio,
|
||||
recombined_video=recombined_video,
|
||||
recombined_audio=recombined_audio,
|
||||
video_difference=tensor_difference(physical_video, recombined_video).__dict__,
|
||||
audio_difference=tensor_difference(physical_audio, recombined_audio).__dict__,
|
||||
)
|
||||
|
||||
def capture_decode_assembly(
|
||||
self,
|
||||
*,
|
||||
physical_decoded: torch.Tensor,
|
||||
assembled_decoded: torch.Tensor,
|
||||
) -> StageSnapshot:
|
||||
return self.capture(
|
||||
"E_decode_assembly",
|
||||
physical_decoded=physical_decoded,
|
||||
assembled_decoded=assembled_decoded,
|
||||
difference=tensor_difference(physical_decoded, assembled_decoded).__dict__,
|
||||
)
|
||||
|
||||
|
||||
def compare_captures(
|
||||
core: DiagnosticCapture,
|
||||
continuum: DiagnosticCapture,
|
||||
) -> tuple[StageComparison, ...]:
|
||||
comparisons = []
|
||||
for stage in STAGE_ORDER:
|
||||
core_stage = core.stages.get(stage)
|
||||
continuum_stage = continuum.stages.get(stage)
|
||||
missing = None
|
||||
if core_stage is None and continuum_stage is None:
|
||||
missing = "both"
|
||||
elif core_stage is None:
|
||||
missing = "core"
|
||||
elif continuum_stage is None:
|
||||
missing = "continuum"
|
||||
comparisons.append(
|
||||
StageComparison(
|
||||
name=stage,
|
||||
equal=(
|
||||
core_stage is not None
|
||||
and continuum_stage is not None
|
||||
and core_stage.sha256 == continuum_stage.sha256
|
||||
),
|
||||
core_sha256=None if core_stage is None else core_stage.sha256,
|
||||
continuum_sha256=None if continuum_stage is None else continuum_stage.sha256,
|
||||
missing_from=missing,
|
||||
)
|
||||
)
|
||||
return tuple(comparisons)
|
||||
|
||||
|
||||
def first_divergent_stage(
|
||||
core: DiagnosticCapture,
|
||||
continuum: DiagnosticCapture,
|
||||
) -> str | None:
|
||||
for comparison in compare_captures(core, continuum):
|
||||
if not comparison.equal:
|
||||
return comparison.name
|
||||
return None
|
||||
@@ -0,0 +1,81 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
MODULE_PATH = Path(__file__).parents[1] / "tools" / "gpu_diagnostic_node" / "capture.py"
|
||||
SPEC = importlib.util.spec_from_file_location("h3_gpu_diagnostic_capture", MODULE_PATH)
|
||||
capture = importlib.util.module_from_spec(SPEC)
|
||||
assert SPEC.loader is not None
|
||||
sys.modules[SPEC.name] = capture
|
||||
SPEC.loader.exec_module(capture)
|
||||
|
||||
|
||||
def test_tensor_fingerprint_is_deterministic():
|
||||
value = torch.arange(12, dtype=torch.float32).reshape(1, 3, 4)
|
||||
assert capture.tensor_fingerprint(value) == capture.tensor_fingerprint(value.clone())
|
||||
|
||||
|
||||
def test_tensor_comparison_records_numerical_difference():
|
||||
left = torch.tensor([1.0, 2.0], dtype=torch.float32)
|
||||
right = torch.tensor([1.0, 2.000001], dtype=torch.float32)
|
||||
result = capture.compare_tensors(left, right)
|
||||
assert not result.bit_exact
|
||||
assert result.allclose
|
||||
assert result.matches
|
||||
assert result.max_abs_diff > 0.0
|
||||
|
||||
|
||||
def test_terminal_video_recombine_is_bit_exact():
|
||||
physical = torch.arange(72, dtype=torch.float32).reshape(1, 1, 72, 1, 1)
|
||||
first = physical[:, :, 0:37]
|
||||
second = physical[:, :, 30:72]
|
||||
rebuilt = capture.recombine_terminal_streams(
|
||||
first,
|
||||
second,
|
||||
first_range=(0, 37),
|
||||
second_range=(30, 72),
|
||||
dim=2,
|
||||
)
|
||||
assert torch.equal(rebuilt, physical)
|
||||
|
||||
|
||||
def test_terminal_audio_recombine_is_bit_exact():
|
||||
physical = torch.arange(405, dtype=torch.float32).reshape(1, 1, 405)
|
||||
first = physical[..., 0:207]
|
||||
second = physical[..., 170:405]
|
||||
rebuilt = capture.recombine_terminal_streams(
|
||||
first,
|
||||
second,
|
||||
first_range=(0, 207),
|
||||
second_range=(170, 405),
|
||||
dim=-1,
|
||||
)
|
||||
assert torch.equal(rebuilt, physical)
|
||||
|
||||
|
||||
def test_stage_comparison_reports_first_field():
|
||||
left = capture.DiagnosticCapture("core")
|
||||
right = capture.DiagnosticCapture("continuum")
|
||||
left.add_value("A_pre_sampler", "seed", 42)
|
||||
right.add_value("A_pre_sampler", "seed", 43)
|
||||
left.add_tensor("A_pre_sampler", "sigmas", torch.ones(2))
|
||||
right.add_tensor("A_pre_sampler", "sigmas", torch.ones(2))
|
||||
result = capture.compare_stage(left, right, "A_pre_sampler")
|
||||
assert result["status"] == "DIFFER"
|
||||
assert result["first_field"] == "seed"
|
||||
|
||||
|
||||
def test_summary_uses_stage_order_and_first_divergence(tmp_path):
|
||||
comparison = {
|
||||
"A_pre_sampler": {"status": "MATCH", "first_field": None},
|
||||
"B_first_model_call": {"status": "DIFFER", "first_field": "position_ids"},
|
||||
}
|
||||
text = capture.format_comparison(comparison, tmp_path)
|
||||
assert "A_pre_sampler: MATCH" in text
|
||||
assert "B_first_model_call: DIFFER" in text
|
||||
assert "First divergence: B_first_model_call.position_ids" in text
|
||||
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.conditioning import (
|
||||
conditioning_display_label,
|
||||
conditioning_mode_from_presence,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("has_first", "has_last", "has_reference", "expected"),
|
||||
[
|
||||
(False, False, False, "T2VA + Continuation"),
|
||||
(True, False, False, "I2VA + Continuation"),
|
||||
(False, True, False, "Last Frame + Continuation"),
|
||||
(True, True, False, "FL2VA + Continuation"),
|
||||
(False, False, True, "Reference + Continuation"),
|
||||
(True, False, True, "Hybrid I2VA + Reference + Continuation"),
|
||||
(False, True, True, "Hybrid L2VA + Reference + Continuation"),
|
||||
(True, True, True, "Hybrid FL2VA + Reference + Continuation"),
|
||||
],
|
||||
)
|
||||
def test_conditioning_display_label_matrix(
|
||||
has_first: bool,
|
||||
has_last: bool,
|
||||
has_reference: bool,
|
||||
expected: str,
|
||||
) -> None:
|
||||
assert (
|
||||
conditioning_display_label(
|
||||
has_first=has_first,
|
||||
has_last=has_last,
|
||||
has_reference=has_reference,
|
||||
)
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("has_first", "has_last"),
|
||||
[
|
||||
(True, False),
|
||||
(False, True),
|
||||
(True, True),
|
||||
],
|
||||
)
|
||||
def test_hybrid_display_does_not_change_internal_reference_mode(
|
||||
has_first: bool,
|
||||
has_last: bool,
|
||||
) -> None:
|
||||
assert (
|
||||
conditioning_mode_from_presence(
|
||||
has_first=has_first,
|
||||
has_last=has_last,
|
||||
has_reference=True,
|
||||
)
|
||||
== "reference"
|
||||
)
|
||||
@@ -0,0 +1,183 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v2.sequence import (
|
||||
_split_terminal_merged_latents,
|
||||
_terminal_pair_contract,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.tests.terminal_merge_diagnostics import (
|
||||
DiagnosticCapture,
|
||||
first_divergent_stage,
|
||||
tensor_difference,
|
||||
tensor_fingerprint,
|
||||
)
|
||||
|
||||
|
||||
def _capture_all_stages(*, sampled_offset: float = 0.0) -> DiagnosticCapture:
|
||||
capture = DiagnosticCapture()
|
||||
capture.capture_pre_sampler(
|
||||
physical_frames=243,
|
||||
seed=42,
|
||||
conditioning=[[torch.tensor([1.0]), {"minimax_frame_count": 243}]],
|
||||
latent={"video": torch.zeros(1, 24, 72, 2, 2)},
|
||||
noise=torch.arange(8, dtype=torch.float32),
|
||||
keyframes=(0, 242),
|
||||
qwen_images=("first", "last"),
|
||||
minimax_refs=None,
|
||||
)
|
||||
layout = SimpleNamespace(
|
||||
signature=(1, 2, 3),
|
||||
segments=(("target_video", 0, 72),),
|
||||
position_ids=torch.arange(12, dtype=torch.float32).reshape(6, 2),
|
||||
)
|
||||
capture.capture_first_model_call(
|
||||
minimax_payload={
|
||||
"layout": layout,
|
||||
"cond_video_latents": torch.ones(1, 24, 2, 2, 2),
|
||||
"target_video_shape": (1, 24, 72, 2, 2),
|
||||
"target_audio_shape": (1, 32, 2, 405),
|
||||
},
|
||||
transformer_options={"patches": {}},
|
||||
)
|
||||
video = torch.full((1, 24, 72, 2, 2), sampled_offset)
|
||||
audio = torch.zeros(1, 32, 2, 405)
|
||||
capture.capture_sampled_physical_av(video=video, audio=audio)
|
||||
capture.capture_split_recombine(
|
||||
physical_video=video,
|
||||
physical_audio=audio,
|
||||
recombined_video=video.clone(),
|
||||
recombined_audio=audio.clone(),
|
||||
)
|
||||
decoded = torch.zeros(243, 2, 2, 3)
|
||||
capture.capture_decode_assembly(
|
||||
physical_decoded=decoded,
|
||||
assembled_decoded=decoded.clone(),
|
||||
)
|
||||
return capture
|
||||
|
||||
|
||||
def test_tensor_fingerprint_is_deterministic_and_dtype_sensitive():
|
||||
value = torch.arange(12, dtype=torch.float32).reshape(3, 4)
|
||||
first = tensor_fingerprint(value)
|
||||
second = tensor_fingerprint(value.clone())
|
||||
|
||||
assert first == second
|
||||
assert first.shape == (3, 4)
|
||||
assert first.dtype == "torch.float32"
|
||||
assert first.sha256 != tensor_fingerprint(value.to(torch.float64)).sha256
|
||||
|
||||
|
||||
def test_tensor_difference_keeps_hash_mismatch_distinct_from_allclose():
|
||||
core = torch.tensor([1.0], dtype=torch.float32)
|
||||
continuum = torch.tensor([1.0 + 1.0e-7], dtype=torch.float32)
|
||||
|
||||
difference = tensor_difference(core, continuum)
|
||||
|
||||
assert difference.bit_exact is False
|
||||
assert difference.allclose is True
|
||||
assert difference.max_abs_diff is not None
|
||||
assert difference.max_abs_diff > 0.0
|
||||
assert difference.mean_abs_diff == difference.max_abs_diff
|
||||
|
||||
|
||||
def test_pre_sampler_capture_includes_initial_noise():
|
||||
core = _capture_all_stages()
|
||||
continuum = _capture_all_stages()
|
||||
assert first_divergent_stage(core, continuum) is None
|
||||
|
||||
changed = DiagnosticCapture()
|
||||
changed.capture_pre_sampler(
|
||||
physical_frames=243,
|
||||
seed=42,
|
||||
conditioning=[[torch.tensor([1.0]), {"minimax_frame_count": 243}]],
|
||||
latent={"video": torch.zeros(1, 24, 72, 2, 2)},
|
||||
noise=torch.arange(8, dtype=torch.float32) + 1.0,
|
||||
keyframes=(0, 242),
|
||||
qwen_images=("first", "last"),
|
||||
minimax_refs=None,
|
||||
)
|
||||
|
||||
assert first_divergent_stage(core, changed) == "A_pre_sampler"
|
||||
|
||||
|
||||
def test_first_model_call_capture_detects_packed_layout_difference():
|
||||
core = DiagnosticCapture()
|
||||
continuum = DiagnosticCapture()
|
||||
shared = dict(
|
||||
physical_frames=243,
|
||||
seed=42,
|
||||
conditioning=[torch.zeros(1)],
|
||||
latent=torch.zeros(1),
|
||||
noise=torch.zeros(1),
|
||||
)
|
||||
core.capture_pre_sampler(**shared)
|
||||
continuum.capture_pre_sampler(**shared)
|
||||
|
||||
core_layout = SimpleNamespace(
|
||||
signature=(1, 2),
|
||||
segments=(("target_video", 0, 72),),
|
||||
position_ids=torch.zeros(4, 2),
|
||||
)
|
||||
continuum_layout = SimpleNamespace(
|
||||
signature=(1, 2),
|
||||
segments=(("target_video", 0, 72),),
|
||||
position_ids=torch.ones(4, 2),
|
||||
)
|
||||
core.capture_first_model_call(
|
||||
minimax_payload={"layout": core_layout},
|
||||
transformer_options={},
|
||||
)
|
||||
continuum.capture_first_model_call(
|
||||
minimax_payload={"layout": continuum_layout},
|
||||
transformer_options={},
|
||||
)
|
||||
|
||||
assert first_divergent_stage(core, continuum) == "B_first_model_call"
|
||||
|
||||
|
||||
def test_first_divergent_stage_uses_pipeline_order():
|
||||
core = _capture_all_stages(sampled_offset=0.0)
|
||||
continuum = _capture_all_stages(sampled_offset=1.0)
|
||||
|
||||
assert first_divergent_stage(core, continuum) == "C_sampled_physical_av"
|
||||
|
||||
|
||||
def test_terminal_split_recombine_is_bit_exact():
|
||||
contract = _terminal_pair_contract(initial_pair=True, chunk_seconds=5.0)
|
||||
video = torch.arange(
|
||||
1 * 24 * contract["video_slices"][-1][1] * 2 * 2,
|
||||
dtype=torch.float32,
|
||||
).reshape(1, 24, contract["video_slices"][-1][1], 2, 2)
|
||||
audio = torch.arange(
|
||||
1 * 32 * 2 * contract["audio_slices"][-1][1],
|
||||
dtype=torch.float32,
|
||||
).reshape(1, 32, 2, contract["audio_slices"][-1][1])
|
||||
|
||||
(video_1, audio_1), (video_2, audio_2) = _split_terminal_merged_latents(
|
||||
video,
|
||||
audio,
|
||||
contract,
|
||||
)
|
||||
video_overlap = contract["video_slices"][0][1] - contract["video_slices"][1][0]
|
||||
audio_overlap = contract["audio_slices"][0][1] - contract["audio_slices"][1][0]
|
||||
recombined_video = torch.cat((video_1, video_2[:, :, video_overlap:]), dim=2)
|
||||
recombined_audio = torch.cat((audio_1, audio_2[..., audio_overlap:]), dim=-1)
|
||||
|
||||
assert contract["physical_frames"] == 243
|
||||
assert contract["logical_frames"] == (124, 141)
|
||||
assert torch.equal(recombined_video, video)
|
||||
assert torch.equal(recombined_audio, audio)
|
||||
assert tensor_difference(video, recombined_video).bit_exact is True
|
||||
assert tensor_difference(audio, recombined_audio).bit_exact is True
|
||||
|
||||
capture = DiagnosticCapture()
|
||||
stage = capture.capture_split_recombine(
|
||||
physical_video=video,
|
||||
physical_audio=audio,
|
||||
recombined_video=recombined_video,
|
||||
recombined_audio=recombined_audio,
|
||||
)
|
||||
assert stage.name == "D_split_recombine"
|
||||
@@ -0,0 +1,165 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v2.sequence import (
|
||||
_atomic_terminal_prefix,
|
||||
_split_terminal_merged_latents,
|
||||
_terminal_flf_merge_enabled,
|
||||
_terminal_pair_prompt,
|
||||
_terminal_physical_seed_plan,
|
||||
_terminal_pair_contract,
|
||||
_terminal_sampling_plan,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v2.seeds import derive_chunk_seed
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("initial_pair", "physical_frames", "logical_frames", "video_slices", "audio_slices"),
|
||||
[
|
||||
(True, 243, (124, 141), ((0, 37), (30, 72)), ((0, 207), (170, 405))),
|
||||
(False, 260, (141, 141), ((0, 42), (35, 77)), ((0, 235), (198, 433))),
|
||||
],
|
||||
)
|
||||
def test_terminal_pair_contract_uses_native_temporal_grid(
|
||||
initial_pair,
|
||||
physical_frames,
|
||||
logical_frames,
|
||||
video_slices,
|
||||
audio_slices,
|
||||
):
|
||||
contract = _terminal_pair_contract(initial_pair=initial_pair, chunk_seconds=5.0)
|
||||
assert contract["physical_frames"] == physical_frames
|
||||
assert contract["logical_frames"] == logical_frames
|
||||
assert contract["video_slices"] == video_slices
|
||||
assert contract["audio_slices"] == audio_slices
|
||||
|
||||
|
||||
@pytest.mark.parametrize("initial_pair", [True, False])
|
||||
def test_terminal_pair_split_preserves_expected_overlap(initial_pair):
|
||||
contract = _terminal_pair_contract(initial_pair=initial_pair, chunk_seconds=5.0)
|
||||
video_t = contract["video_slices"][-1][1]
|
||||
audio_t = contract["audio_slices"][-1][1]
|
||||
video = torch.arange(video_t, dtype=torch.float32).reshape(1, 1, video_t, 1, 1)
|
||||
audio = torch.arange(audio_t, dtype=torch.float32).reshape(1, 1, 1, audio_t)
|
||||
first, second = _split_terminal_merged_latents(video, audio, contract)
|
||||
assert first[0].shape[2] == contract["video_slices"][0][1]
|
||||
assert first[1].shape[-1] == contract["audio_slices"][0][1]
|
||||
assert second[0][0, 0, 0, 0, 0].item() == contract["video_slices"][1][0]
|
||||
assert second[1][0, 0, 0, 0].item() == contract["audio_slices"][1][0]
|
||||
|
||||
|
||||
def test_two_by_five_uses_one_physical_sample():
|
||||
normal, terminal = _terminal_sampling_plan(chunks=2, completed=0, merge_enabled=True)
|
||||
assert normal == ()
|
||||
assert terminal is True
|
||||
assert len(normal) + int(terminal) == 1
|
||||
|
||||
|
||||
def test_three_by_five_uses_one_normal_and_one_terminal_sample():
|
||||
normal, terminal = _terminal_sampling_plan(chunks=3, completed=0, merge_enabled=True)
|
||||
assert normal == (0,)
|
||||
assert terminal is True
|
||||
assert len(normal) + int(terminal) == 2
|
||||
|
||||
|
||||
def test_terminal_merge_accepts_distinct_prompts_but_not_timeline_video():
|
||||
common = dict(multi_chunk_flf=True, chunks=2, chunk_seconds=5.0)
|
||||
assert _terminal_flf_merge_enabled(**common, prompt_hashes=["same", "same"], timeline_video_source=None)
|
||||
assert _terminal_flf_merge_enabled(**common, prompt_hashes=["a", "b"], timeline_video_source=None)
|
||||
assert not _terminal_flf_merge_enabled(**common, prompt_hashes=["same", "same"], timeline_video_source=object())
|
||||
|
||||
|
||||
def test_terminal_pair_prompt_preserves_shared_prompt_exactly():
|
||||
prompt, policy = _terminal_pair_prompt(["same", "same"], pair_start=0, chunk_seconds=5.0)
|
||||
assert prompt == "same"
|
||||
assert policy == "shared_prompt_v1"
|
||||
|
||||
|
||||
def test_terminal_pair_prompt_rebases_distinct_sections_to_local_timeline():
|
||||
prompt, policy = _terminal_pair_prompt(["first action", "second action"], pair_start=0, chunk_seconds=5.0)
|
||||
assert prompt == "[0-5s]\nfirst action\n\n[5-10s]\nsecond action"
|
||||
assert policy == "paired_timeline_v1"
|
||||
|
||||
|
||||
def test_partial_terminal_pair_is_discarded_for_atomic_resume():
|
||||
preserved = [{"chunk": 1}, {"chunk": 2}]
|
||||
result, reset = _atomic_terminal_prefix(preserved, chunks=3, reroll_from_chunk=0)
|
||||
assert result == preserved[:1]
|
||||
assert reset is True
|
||||
|
||||
|
||||
def test_rerolling_final_chunk_regenerates_both_terminal_chunks():
|
||||
preserved = [{"chunk": 1}, {"chunk": 2}, {"chunk": 3}]
|
||||
result, reset = _atomic_terminal_prefix(preserved, chunks=3, reroll_from_chunk=3)
|
||||
assert result == preserved[:1]
|
||||
assert reset is True
|
||||
|
||||
|
||||
def test_complete_terminal_pair_remains_reusable_without_reroll():
|
||||
preserved = [{"chunk": 1}, {"chunk": 2}, {"chunk": 3}]
|
||||
result, reset = _atomic_terminal_prefix(preserved, chunks=3, reroll_from_chunk=0)
|
||||
assert result == preserved
|
||||
assert reset is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("base_seed", [42, 123456789])
|
||||
def test_initial_terminal_pair_uses_base_seed_directly(base_seed):
|
||||
plan = _terminal_physical_seed_plan(
|
||||
base_seed=base_seed,
|
||||
physical_window_start_index=0,
|
||||
reroll_nonce=0,
|
||||
)
|
||||
assert plan["physical_seed"] == base_seed
|
||||
assert plan["logical_entry_seeds"] == (base_seed, base_seed)
|
||||
|
||||
|
||||
def test_later_terminal_pair_derives_once_from_pair_start():
|
||||
plan = _terminal_physical_seed_plan(
|
||||
base_seed=42,
|
||||
physical_window_start_index=2,
|
||||
reroll_nonce=0,
|
||||
)
|
||||
assert plan["physical_seed"] == derive_chunk_seed(42, 2, 0)
|
||||
assert plan["physical_seed"] == 1808285636802909398
|
||||
assert plan["logical_entry_seeds"] == (plan["physical_seed"],) * 2
|
||||
|
||||
|
||||
def test_terminal_pair_reroll_derives_once_and_shares_new_seed():
|
||||
original = _terminal_physical_seed_plan(
|
||||
base_seed=42,
|
||||
physical_window_start_index=2,
|
||||
reroll_nonce=0,
|
||||
)
|
||||
rerolled = _terminal_physical_seed_plan(
|
||||
base_seed=42,
|
||||
physical_window_start_index=2,
|
||||
reroll_nonce=1,
|
||||
)
|
||||
assert rerolled["physical_seed"] == derive_chunk_seed(42, 2, 1)
|
||||
assert rerolled["physical_seed"] == 4306926667627841302
|
||||
assert rerolled["physical_seed"] != original["physical_seed"]
|
||||
assert rerolled["logical_entry_seeds"] == (rerolled["physical_seed"],) * 2
|
||||
|
||||
|
||||
def test_non_terminal_seed_derivation_golden_values_are_unchanged():
|
||||
assert derive_chunk_seed(42, 0, 0) == 15348518480342951850
|
||||
assert derive_chunk_seed(42, 1, 0) == 13021120982267964226
|
||||
|
||||
|
||||
def test_last_frame_absent_does_not_enable_terminal_merge():
|
||||
assert not _terminal_flf_merge_enabled(
|
||||
multi_chunk_flf=False,
|
||||
chunks=2,
|
||||
chunk_seconds=5.0,
|
||||
prompt_hashes=["same", "same"],
|
||||
timeline_video_source=None,
|
||||
)
|
||||
|
||||
|
||||
def test_one_chunk_keeps_normal_sampling_and_seed_behavior():
|
||||
normal, terminal = _terminal_sampling_plan(chunks=1, completed=0, merge_enabled=False)
|
||||
assert normal == (0,)
|
||||
assert terminal is False
|
||||
assert derive_chunk_seed(42, normal[0], 0) == 15348518480342951850
|
||||
@@ -283,6 +283,7 @@ def _sampling_contract(
|
||||
last_frame_hash="", reference_contract=None,
|
||||
upstream_graph_contract=None, upstream_graph_safe=None,
|
||||
upstream_graph_reasons=None,
|
||||
execution_semantics=None,
|
||||
):
|
||||
return build_sampling_contract(
|
||||
model=model or _Model(),
|
||||
@@ -312,9 +313,69 @@ def _sampling_contract(
|
||||
upstream_graph_contract=upstream_graph_contract,
|
||||
upstream_graph_safe=upstream_graph_safe,
|
||||
upstream_graph_reasons=upstream_graph_reasons,
|
||||
execution_semantics=execution_semantics,
|
||||
)
|
||||
|
||||
|
||||
def test_terminal_seed_v2_revision_is_scoped_and_distinct():
|
||||
old_contract, old_safe, old_reasons = _sampling_contract(
|
||||
conditioning_mode="fl2va",
|
||||
first_frame_hash="a" * 64,
|
||||
last_frame_hash="b" * 64,
|
||||
)
|
||||
new_contract, new_safe, new_reasons = _sampling_contract(
|
||||
conditioning_mode="fl2va",
|
||||
first_frame_hash="a" * 64,
|
||||
last_frame_hash="b" * 64,
|
||||
execution_semantics={
|
||||
"flf_execution": "terminal_merged_10s_seed_v2",
|
||||
"terminal_seed_policy": "physical_window_seed_v2",
|
||||
},
|
||||
)
|
||||
assert old_safe, old_reasons
|
||||
assert new_safe, new_reasons
|
||||
assert "execution_semantics" not in old_contract["global"]
|
||||
assert new_contract["global"]["execution_semantics"] == {
|
||||
"flf_execution": "terminal_merged_10s_seed_v2",
|
||||
"terminal_seed_policy": "physical_window_seed_v2",
|
||||
}
|
||||
assert revision_identity(old_contract) != revision_identity(new_contract)
|
||||
|
||||
|
||||
def test_terminal_seed_v2_revision_identity_is_deterministic():
|
||||
semantics = {
|
||||
"flf_execution": "terminal_merged_10s_seed_v2",
|
||||
"terminal_seed_policy": "physical_window_seed_v2",
|
||||
}
|
||||
model = _Model()
|
||||
clip = _Clip()
|
||||
video_vae = _VideoVAE()
|
||||
sampler = _Sampler()
|
||||
first, first_safe, first_reasons = _sampling_contract(
|
||||
model=model,
|
||||
clip=clip,
|
||||
video_vae=video_vae,
|
||||
sampler=sampler,
|
||||
conditioning_mode="fl2va",
|
||||
first_frame_hash="a" * 64,
|
||||
last_frame_hash="b" * 64,
|
||||
execution_semantics=semantics,
|
||||
)
|
||||
second, second_safe, second_reasons = _sampling_contract(
|
||||
model=model,
|
||||
clip=clip,
|
||||
video_vae=video_vae,
|
||||
sampler=sampler,
|
||||
conditioning_mode="fl2va",
|
||||
first_frame_hash="a" * 64,
|
||||
last_frame_hash="b" * 64,
|
||||
execution_semantics=semantics,
|
||||
)
|
||||
assert first_safe, first_reasons
|
||||
assert second_safe, second_reasons
|
||||
assert revision_identity(first) == revision_identity(second)
|
||||
|
||||
|
||||
def test_v322_golden_reference_contract_preserves_revision_identity():
|
||||
model = _Model()
|
||||
clip = _Clip()
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v3 import plan as plan_module
|
||||
|
||||
|
||||
def _logical_plan():
|
||||
return {
|
||||
"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,
|
||||
},
|
||||
],
|
||||
"target_frames": 240,
|
||||
}
|
||||
|
||||
|
||||
def _terminal_entries():
|
||||
physical_video = torch.arange(72, dtype=torch.float32).reshape(1, 1, 72, 1, 1)
|
||||
physical_audio = torch.arange(405, dtype=torch.float32).reshape(1, 1, 405)
|
||||
entries = [
|
||||
{
|
||||
"video": physical_video[:, :, 0:37].clone(),
|
||||
"audio": physical_audio[..., 0:207].clone(),
|
||||
},
|
||||
{
|
||||
"video": physical_video[:, :, 30:72].clone(),
|
||||
"audio": physical_audio[..., 170:405].clone(),
|
||||
},
|
||||
]
|
||||
return entries, physical_video, physical_audio
|
||||
|
||||
|
||||
def test_terminal_logical_entries_recombine_bit_exact(monkeypatch):
|
||||
entries, physical_video, physical_audio = _terminal_entries()
|
||||
monkeypatch.setattr(
|
||||
plan_module,
|
||||
"make_assembly_plan",
|
||||
lambda *args, **kwargs: _logical_plan(),
|
||||
)
|
||||
monkeypatch.setattr(plan_module, "validate_assembly_plan", lambda plan: plan)
|
||||
|
||||
decode_entries, assembly_plan = plan_module.prepare_physical_decode_entries(
|
||||
entries,
|
||||
chunk_seconds=5.0,
|
||||
preserve_final_frame=True,
|
||||
terminal_merged=True,
|
||||
)
|
||||
|
||||
assert len(decode_entries) == 1
|
||||
assert torch.equal(decode_entries[0]["video"], physical_video)
|
||||
assert torch.equal(decode_entries[0]["audio"], physical_audio)
|
||||
assert assembly_plan["logical_chunk_count"] == 2
|
||||
assert assembly_plan["physical_decode_group_count"] == 1
|
||||
assert assembly_plan["decode_groups"][0]["logical_chunk_indices"] == [1, 2]
|
||||
assert assembly_plan["decode_groups"][0]["total_frames"] == 243
|
||||
assert assembly_plan["decode_groups"][0]["trim_frames"] == 0
|
||||
assert assembly_plan["decode_groups"][0]["net_frames"] == 243
|
||||
|
||||
|
||||
def test_terminal_recombine_rejects_changed_overlap(monkeypatch):
|
||||
entries, _, _ = _terminal_entries()
|
||||
entries[1]["video"][:, :, 0].add_(1.0)
|
||||
monkeypatch.setattr(
|
||||
plan_module,
|
||||
"make_assembly_plan",
|
||||
lambda *args, **kwargs: _logical_plan(),
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="overlap differs"):
|
||||
plan_module.prepare_physical_decode_entries(
|
||||
entries,
|
||||
chunk_seconds=5.0,
|
||||
preserve_final_frame=True,
|
||||
terminal_merged=True,
|
||||
)
|
||||
|
||||
|
||||
def test_three_logical_chunks_use_two_physical_decode_groups(monkeypatch):
|
||||
from ComfyUI_H3_Continuum_Join.v2.sequence import (
|
||||
_split_terminal_merged_latents,
|
||||
_terminal_pair_contract,
|
||||
)
|
||||
|
||||
contract = _terminal_pair_contract(initial_pair=False, chunk_seconds=5.0)
|
||||
video_t = contract["video_slices"][-1][1]
|
||||
audio_t = contract["audio_slices"][-1][1]
|
||||
physical_video = torch.arange(video_t, dtype=torch.float32).reshape(1, 1, video_t, 1, 1)
|
||||
physical_audio = torch.arange(audio_t, dtype=torch.float32).reshape(1, 1, audio_t)
|
||||
terminal_parts = _split_terminal_merged_latents(physical_video, physical_audio, contract)
|
||||
first_entry = {
|
||||
"video": torch.zeros((1, 1, 37, 1, 1), dtype=torch.float32),
|
||||
"audio": torch.zeros((1, 1, 207), dtype=torch.float32),
|
||||
}
|
||||
logical_plan = {
|
||||
"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},
|
||||
{"sequence_index": 2, "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, "frame_start": 243, "frame_stop": 362},
|
||||
],
|
||||
"target_frames": 360,
|
||||
}
|
||||
entries = [
|
||||
first_entry,
|
||||
{"video": terminal_parts[0][0], "audio": terminal_parts[0][1]},
|
||||
{"video": terminal_parts[1][0], "audio": terminal_parts[1][1]},
|
||||
]
|
||||
monkeypatch.setattr(plan_module, "make_assembly_plan", lambda *args, **kwargs: logical_plan)
|
||||
monkeypatch.setattr(plan_module, "validate_assembly_plan", lambda plan: plan)
|
||||
|
||||
decode_entries, assembly_plan = plan_module.prepare_physical_decode_entries(
|
||||
entries,
|
||||
chunk_seconds=5.0,
|
||||
preserve_final_frame=True,
|
||||
terminal_merged=True,
|
||||
)
|
||||
|
||||
assert len(decode_entries) == 2
|
||||
assert torch.equal(decode_entries[1]["video"], physical_video)
|
||||
assert torch.equal(decode_entries[1]["audio"], physical_audio)
|
||||
assert assembly_plan["physical_decode_group_count"] == 2
|
||||
terminal_group = assembly_plan["decode_groups"][1]
|
||||
assert terminal_group["logical_chunk_indices"] == [2, 3]
|
||||
assert terminal_group["total_frames"] == 260
|
||||
assert terminal_group["trim_frames"] == 22
|
||||
assert terminal_group["net_frames"] == 238
|
||||
Reference in New Issue
Block a user