Stabilize Continuum cloning and workflow guidance
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
CONTINUUM_INTEROP_KEY,
|
||||
MARK_AUDIO_CONTEXT,
|
||||
MARK_AUDIO_END_FRAME,
|
||||
MARK_AUDIO_OVERHANG,
|
||||
MARK_CONTEXT_FRAMES,
|
||||
MARK_VIDEO_CONTEXT,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.model_patch import _wrapper_factory, patch_model
|
||||
from ComfyUI_H3_Continuum_Join.model_patch import (
|
||||
_wrapper_factory,
|
||||
clone_model_for_chunk,
|
||||
patch_model,
|
||||
)
|
||||
|
||||
|
||||
class Executor:
|
||||
@@ -75,6 +81,36 @@ def test_apply_model_wrapper_repairs_payload_before_executor():
|
||||
assert torch.equal(result["layout"].position_ids[3:4], result["layout"].position_ids[6:7])
|
||||
|
||||
|
||||
def test_apply_model_wrapper_fails_when_actual_topology_changes():
|
||||
video = torch.zeros(1, 24, 1, 1, 1)
|
||||
audio = torch.zeros(1, 32, 2, 1)
|
||||
ref = {
|
||||
"kind": "video_audio",
|
||||
"latent_t": 1,
|
||||
"latent_h": 1,
|
||||
"latent_w": 1,
|
||||
"ref_audio_t": 1,
|
||||
"latent": video,
|
||||
"audio_latent": audio,
|
||||
MARK_VIDEO_CONTEXT: True,
|
||||
MARK_CONTEXT_FRAMES: 1,
|
||||
MARK_AUDIO_CONTEXT: True,
|
||||
MARK_AUDIO_END_FRAME: 1.0,
|
||||
MARK_AUDIO_OVERHANG: 0.0,
|
||||
}
|
||||
payload = {"layout": _layout(), "keyframes": [], "refs": [ref]}
|
||||
wrapper = _wrapper_factory(strict=True, debug=False)
|
||||
options = {
|
||||
"spectrum_h3_actual": True,
|
||||
"cond_or_uncond": [0],
|
||||
"uuids": ["positive"],
|
||||
}
|
||||
wrapper(Executor(), torch.zeros(1), transformer_options=options, minimax_payload=payload)
|
||||
payload["layout"].position_ids[-1, 1] = 1.0
|
||||
with pytest.raises(RuntimeError, match="topology changed"):
|
||||
wrapper(Executor(), torch.zeros(1), transformer_options=options, minimax_payload=payload)
|
||||
|
||||
|
||||
class FakeModelPatcher:
|
||||
def __init__(self):
|
||||
inner_type = type(
|
||||
@@ -122,3 +158,43 @@ def test_patch_model_validates_and_installs_per_model_wrapper(monkeypatch):
|
||||
assert patched.removed[0][1] == "h3_continuum_join.apply_model.v1"
|
||||
assert patched.added[0][1] == "h3_continuum_join.apply_model.v1"
|
||||
assert patched.model_options["h3_continuum_join"]["api_version"] == 1
|
||||
|
||||
|
||||
def test_chunk_model_copy_on_write_and_context_scoped_hint(monkeypatch):
|
||||
import sys
|
||||
from types import ModuleType
|
||||
|
||||
extension = ModuleType("comfy.patcher_extension")
|
||||
extension.WrappersMP = SimpleNamespace(APPLY_MODEL="apply_model")
|
||||
comfy = sys.modules.get("comfy") or ModuleType("comfy")
|
||||
monkeypatch.setitem(sys.modules, "comfy", comfy)
|
||||
monkeypatch.setitem(sys.modules, "comfy.patcher_extension", extension)
|
||||
|
||||
source = FakeModelPatcher()
|
||||
clone_calls = 0
|
||||
original_clone = FakeModelPatcher.clone
|
||||
|
||||
def counted_clone(self):
|
||||
nonlocal clone_calls
|
||||
clone_calls += 1
|
||||
return original_clone(self)
|
||||
|
||||
monkeypatch.setattr(FakeModelPatcher, "clone", counted_clone)
|
||||
source.model_options = {"transformer_options": {"existing": "kept"}}
|
||||
first = clone_model_for_chunk(source, strict=True, debug=False, chunk_index=1, context_frames=None)
|
||||
second = clone_model_for_chunk(source, strict=True, debug=False, chunk_index=2, context_frames=22)
|
||||
third = clone_model_for_chunk(source, strict=True, debug=False, chunk_index=3, context_frames=22)
|
||||
|
||||
assert clone_calls == 3
|
||||
assert source.model_options == {"transformer_options": {"existing": "kept"}}
|
||||
assert CONTINUUM_INTEROP_KEY not in first.model_options["transformer_options"]
|
||||
request = second.model_options["transformer_options"][CONTINUUM_INTEROP_KEY]
|
||||
assert request == {
|
||||
"api": 1,
|
||||
"active": True,
|
||||
"min_actual_prefix_steps": 2,
|
||||
"chunk_index": 2,
|
||||
"context_frames": 22,
|
||||
}
|
||||
assert request is not third.model_options["transformer_options"][CONTINUUM_INTEROP_KEY]
|
||||
assert CONTINUUM_INTEROP_KEY not in source.model_options["transformer_options"]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.temporal import (
|
||||
align_frame_count_nearest,
|
||||
audio_grid_offset,
|
||||
@@ -5,6 +7,7 @@ from ComfyUI_H3_Continuum_Join.temporal import (
|
||||
context_slots,
|
||||
latent_slot_offsets,
|
||||
largest_context_capacity,
|
||||
make_av_context_window,
|
||||
make_extension_shape,
|
||||
pixel_frames_for_latent_t,
|
||||
video_latent_t,
|
||||
@@ -47,6 +50,23 @@ def test_audio_grid_offset_is_signed_and_bounded_on_valid_h3_lengths():
|
||||
assert any(value > 0 for value in offsets)
|
||||
|
||||
|
||||
def test_native_context_window_uses_source_av_grid_and_cycle_phase():
|
||||
balanced = make_av_context_window(141, audio_latent_t(141), 22)
|
||||
assert (balanced.video_start_slot, balanced.video_stop_slot) == (35, 42)
|
||||
assert (balanced.audio_start_tick, balanced.audio_stop_tick) == (198, 235)
|
||||
assert balanced.audio_steps == 37
|
||||
|
||||
shifted = make_av_context_window(158, audio_latent_t(158), 22)
|
||||
assert (shifted.video_start_slot, shifted.video_stop_slot) == (40, 47)
|
||||
assert (shifted.audio_start_tick, shifted.audio_stop_tick) == (226, 263)
|
||||
assert shifted.audio_grid_offset < 0
|
||||
|
||||
|
||||
def test_native_context_window_rejects_unknown_audio_grid_phase():
|
||||
with pytest.raises(ValueError, match="audio-grid offset"):
|
||||
make_av_context_window(141, audio_latent_t(141) + 2, 22)
|
||||
|
||||
|
||||
def test_dynamic_state_capacity_for_short_extensions():
|
||||
assert largest_context_capacity(119) == 39
|
||||
assert largest_context_capacity(34) == 22
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import PROMPT_FORMAT_OPTIONS
|
||||
from ComfyUI_H3_Continuum_Join.v2.nodes import (
|
||||
H3ContinuumAdvanced,
|
||||
@@ -9,6 +11,7 @@ from ComfyUI_H3_Continuum_Join.v2.nodes import (
|
||||
H3ContinuumSamplerV2,
|
||||
NODE_CLASS_MAPPINGS,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v2.prompts import PromptPlanError
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -88,6 +91,45 @@ def test_v217_facade_delegates_to_legacy_core(monkeypatch):
|
||||
assert captured["show_preview"] is True
|
||||
|
||||
|
||||
def test_v217_facade_passes_only_sparse_override_clips(monkeypatch):
|
||||
captured = {}
|
||||
|
||||
def fake_run(_self, **kwargs):
|
||||
captured.update(kwargs)
|
||||
return "images", "audio", "state", "session", "report"
|
||||
|
||||
monkeypatch.setattr(H3ContinuumSamplerV2, "run", fake_run)
|
||||
pack = H3ContinuumClipOverrides().build(
|
||||
PROMPT_FORMAT_OPTIONS[0],
|
||||
"[Clip 2]\nclose-up\n\n[Clip 5]\nfinish naturally",
|
||||
)[0]
|
||||
H3ContinuumSampler().run(
|
||||
model="model", clip="clip", video_vae="video_vae", audio_vae="audio_vae",
|
||||
sampler="sampler", sigmas="sigmas", sequence_prompt="base",
|
||||
prompt_mode=PROMPT_FORMAT_OPTIONS[0], chunks=5, chunk_seconds=5.0,
|
||||
width=1344, height=768, continuity="Balanced - 22 frames", base_seed=123,
|
||||
seam_correction="Auto", prompt_overrides=pack,
|
||||
)
|
||||
assert captured["clip_2_prompt"] == "close-up"
|
||||
assert captured["clip_5_prompt"] == "finish naturally"
|
||||
assert not any(f"clip_{index}_prompt" in captured for index in (1, 3, 4))
|
||||
|
||||
|
||||
def test_v217_facade_rejects_sparse_override_outside_chunks(monkeypatch):
|
||||
monkeypatch.setattr(H3ContinuumSamplerV2, "run", lambda _self, **kwargs: None)
|
||||
pack = H3ContinuumClipOverrides().build(
|
||||
PROMPT_FORMAT_OPTIONS[0], "[Clip 6]\noutside"
|
||||
)[0]
|
||||
with pytest.raises(PromptPlanError, match="outside"):
|
||||
H3ContinuumSampler().run(
|
||||
model="model", clip="clip", video_vae="video_vae", audio_vae="audio_vae",
|
||||
sampler="sampler", sigmas="sigmas", sequence_prompt="base",
|
||||
prompt_mode=PROMPT_FORMAT_OPTIONS[0], chunks=5, chunk_seconds=5.0,
|
||||
width=1344, height=768, continuity="Balanced - 22 frames", base_seed=123,
|
||||
seam_correction="Auto", prompt_overrides=pack,
|
||||
)
|
||||
|
||||
|
||||
def test_v217_result_pack_expands_legacy_outputs():
|
||||
assert H3ContinuumResult().unpack(
|
||||
{"last_state": "state", "session": "session", "report": "report"}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
import pytest
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
PROMPT_FORMAT_AUTO,
|
||||
PROMPT_FORMAT_FIXED,
|
||||
PROMPT_MODE_FIXED,
|
||||
PROMPT_MODE_LIST,
|
||||
PROMPT_MODE_TIMELINE,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.v2.prompts import (
|
||||
PromptPlanError,
|
||||
apply_prompt_overrides,
|
||||
build_sampler_prompt_plan,
|
||||
detect_prompt_mode,
|
||||
make_prompt_plan,
|
||||
parse_sparse_prompt_overrides,
|
||||
validate_sparse_prompt_overrides,
|
||||
validate_prompt_plan,
|
||||
)
|
||||
|
||||
@@ -19,6 +26,31 @@ def test_fixed_prompt_repeats_without_mutation():
|
||||
assert validate_prompt_plan(plan) is plan
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("script", "expected"),
|
||||
[
|
||||
("one continuous prompt", PROMPT_MODE_FIXED),
|
||||
("first\n---\nsecond", PROMPT_MODE_LIST),
|
||||
("[0-5s]\nfirst", PROMPT_MODE_TIMELINE),
|
||||
("[Chunk 1]\nfirst", PROMPT_MODE_TIMELINE),
|
||||
],
|
||||
)
|
||||
def test_auto_detects_prompt_format(script, expected):
|
||||
assert detect_prompt_mode(script) == expected
|
||||
|
||||
|
||||
def test_auto_plan_records_detected_format_without_changing_schema_mode():
|
||||
plan = make_prompt_plan(mode=PROMPT_FORMAT_AUTO, script="first\n---\nsecond", chunks=2, chunk_seconds=5)
|
||||
assert plan["mode"] == PROMPT_MODE_LIST
|
||||
assert plan["prompts"] == ["first", "second"]
|
||||
assert plan["notes"][0] == "Auto detected List"
|
||||
|
||||
|
||||
def test_simple_fixed_format_maps_to_stable_fixed_plan_mode():
|
||||
plan = make_prompt_plan(mode=PROMPT_FORMAT_FIXED, script="same", chunks=2, chunk_seconds=5)
|
||||
assert plan["mode"] == PROMPT_MODE_FIXED
|
||||
|
||||
|
||||
def test_list_prompt_uses_separator_and_repeats_last():
|
||||
plan = make_prompt_plan(
|
||||
mode=PROMPT_MODE_LIST,
|
||||
@@ -53,3 +85,66 @@ def test_timeline_requires_coverage():
|
||||
def test_native_h3_chunk_duration_rejects_sub_four_seconds():
|
||||
with pytest.raises(PromptPlanError):
|
||||
make_prompt_plan(mode=PROMPT_MODE_FIXED, script="x", chunks=1, chunk_seconds=3.9)
|
||||
|
||||
|
||||
def test_external_prompt_overrides_only_the_connected_clip_and_rehashes_it():
|
||||
plan = make_prompt_plan(mode=PROMPT_MODE_LIST, script="first\n---\nsecond\n---\nthird", chunks=3, chunk_seconds=5)
|
||||
updated = apply_prompt_overrides(plan, [None, "external second", None])
|
||||
assert updated["prompts"] == ["first", "external second", "third"]
|
||||
assert updated["hashes"][0] == plan["hashes"][0]
|
||||
assert updated["hashes"][1] != plan["hashes"][1]
|
||||
assert updated["hashes"][2] == plan["hashes"][2]
|
||||
assert updated["notes"][-1] == "external Clip Prompt input(s): 2"
|
||||
|
||||
|
||||
def test_external_prompt_with_same_text_keeps_the_same_hash():
|
||||
plan = make_prompt_plan(mode=PROMPT_MODE_FIXED, script="same", chunks=2, chunk_seconds=5)
|
||||
assert apply_prompt_overrides(plan, [None, "same"])["hashes"] == plan["hashes"]
|
||||
|
||||
|
||||
def test_external_prompt_rejects_empty_connected_text():
|
||||
plan = make_prompt_plan(mode=PROMPT_MODE_FIXED, script="fallback", chunks=1, chunk_seconds=5)
|
||||
with pytest.raises(PromptPlanError):
|
||||
apply_prompt_overrides(plan, [" "])
|
||||
|
||||
|
||||
def test_sequence_prompt_precedes_connected_plan_and_legacy_script():
|
||||
connected = make_prompt_plan(mode=PROMPT_FORMAT_FIXED, script="connected plan", chunks=2, chunk_seconds=5)
|
||||
resolved = build_sampler_prompt_plan(prompt_mode=PROMPT_FORMAT_AUTO, prompt_script="legacy", sequence_prompt="first\n---\nsecond", prompt_plan=connected, chunks=2, chunk_seconds=5)
|
||||
assert resolved["prompts"] == ["first", "second"]
|
||||
|
||||
|
||||
def test_connected_plan_precedes_legacy_script_without_sequence_input():
|
||||
connected = make_prompt_plan(mode=PROMPT_FORMAT_FIXED, script="connected plan", chunks=2, chunk_seconds=5)
|
||||
resolved = build_sampler_prompt_plan(prompt_mode=PROMPT_FORMAT_AUTO, prompt_script="legacy", sequence_prompt=None, prompt_plan=connected, chunks=2, chunk_seconds=5)
|
||||
assert resolved is connected
|
||||
|
||||
|
||||
def test_sparse_overrides_replace_only_explicit_clips_and_rehash():
|
||||
plan = make_prompt_plan(mode=PROMPT_FORMAT_FIXED, script="Sequence Prompt", chunks=5, chunk_seconds=5)
|
||||
sparse = validate_sparse_prompt_overrides(parse_sparse_prompt_overrides("[Clip 2]\nclose-up\n\n[Clip 5]\nfinish naturally"), chunks=5)
|
||||
updated = apply_prompt_overrides(plan, [sparse.get(index) for index in range(1, 6)])
|
||||
assert updated["prompts"] == ["Sequence Prompt", "close-up", "Sequence Prompt", "Sequence Prompt", "finish naturally"]
|
||||
assert updated["hashes"][0] == plan["hashes"][0]
|
||||
assert updated["hashes"][1] != plan["hashes"][1]
|
||||
assert updated["hashes"][2] == plan["hashes"][2]
|
||||
assert updated["hashes"][3] == plan["hashes"][3]
|
||||
assert updated["hashes"][4] != plan["hashes"][4]
|
||||
|
||||
|
||||
def test_sparse_override_clip_one_leaves_remaining_clips_unchanged():
|
||||
plan = make_prompt_plan(mode=PROMPT_FORMAT_FIXED, script="base", chunks=5, chunk_seconds=5)
|
||||
sparse = validate_sparse_prompt_overrides(parse_sparse_prompt_overrides("[Clip 1]\nopening override"), chunks=5)
|
||||
updated = apply_prompt_overrides(plan, [sparse.get(index) for index in range(1, 6)])
|
||||
assert updated["prompts"] == ["opening override", "base", "base", "base", "base"]
|
||||
|
||||
|
||||
def test_sparse_override_rejects_out_of_range_clip():
|
||||
sparse = parse_sparse_prompt_overrides("[Clip 6]\noutside")
|
||||
with pytest.raises(PromptPlanError, match="outside"):
|
||||
validate_sparse_prompt_overrides(sparse, chunks=5)
|
||||
|
||||
|
||||
def test_sparse_override_rejects_duplicate_clip():
|
||||
with pytest.raises(PromptPlanError, match="more than once"):
|
||||
parse_sparse_prompt_overrides("[Clip 2]\nfirst\n\n[Chunk 2]\nsecond")
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
from ComfyUI_H3_Continuum_Join.constants import (
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
SEAM_CORRECTION_AUTO,
|
||||
)
|
||||
from ComfyUI_H3_Continuum_Join.nodes import NODE_CLASS_MAPPINGS
|
||||
|
||||
|
||||
@@ -20,3 +24,22 @@ def test_v2_nodes_are_registered_without_removing_v1():
|
||||
def test_v2_sampler_accepts_connectable_prompt_plan():
|
||||
schema = NODE_CLASS_MAPPINGS["H3ContinuumSamplerV2"].INPUT_TYPES()
|
||||
assert "prompt_plan" in schema["optional"]
|
||||
|
||||
|
||||
def test_v2_sampler_registers_preview_setting_bridge_and_sixteen_prompt_inputs():
|
||||
schema = NODE_CLASS_MAPPINGS["H3ContinuumSamplerV2"].INPUT_TYPES()
|
||||
optional = schema["optional"]
|
||||
assert schema["required"]["prompt_mode"][0] == PROMPT_FORMAT_OPTIONS
|
||||
assert schema["required"]["prompt_mode"][1]["default"] == "Auto"
|
||||
assert optional["sequence_prompt"][0] == "STRING"
|
||||
assert optional["sequence_prompt"][1]["forceInput"] is True
|
||||
assert optional["show_preview"][1]["default"] is True
|
||||
for index in range(1, 17):
|
||||
prompt_input = optional[f"clip_{index}_prompt"]
|
||||
assert prompt_input[0] == "STRING"
|
||||
assert prompt_input[1]["forceInput"] is True
|
||||
|
||||
|
||||
def test_v2_sampler_defaults_to_auto_seam_correction():
|
||||
schema = NODE_CLASS_MAPPINGS["H3ContinuumSamplerV2"].INPUT_TYPES()
|
||||
assert schema["required"]["seam_correction"][1]["default"] == SEAM_CORRECTION_AUTO
|
||||
|
||||
@@ -22,7 +22,6 @@ def test_v2_samples_every_chunk_before_any_decode(monkeypatch):
|
||||
fake_model=SimpleNamespace(model=SimpleNamespace(diffusion_model=SimpleNamespace()),model_options={},wrappers={},model_dtype=lambda:torch.bfloat16,model_size=lambda:0)
|
||||
assets=IdentityAssets(None,None,None,None,"none")
|
||||
monkeypatch.setattr(sequence,"check_comfy_h3_runtime",lambda:[])
|
||||
monkeypatch.setattr(sequence,"patch_model",lambda model,strict,debug:model)
|
||||
def clone_model_for_chunk(model,*,strict,debug,chunk_index,context_frames):
|
||||
options=dict(model.model_options); transformer_options=dict(options.get("transformer_options") or {})
|
||||
if context_frames is not None: transformer_options[CONTINUUM_INTEROP_KEY]={"api":1,"active":True,"min_actual_prefix_steps":2}
|
||||
|
||||
@@ -77,3 +77,36 @@ def test_session_roundtrip_and_last_state(tmp_path, monkeypatch):
|
||||
loaded = session_io.load_session(prefix="test", slot=1)
|
||||
assert torch.equal(loaded["chunks"][0]["video"], session["chunks"][0]["video"])
|
||||
assert loaded["session_id"] == session["session_id"]
|
||||
|
||||
def test_model_fingerprint_can_add_continuum_wrapper_without_mutating_model():
|
||||
from types import SimpleNamespace
|
||||
|
||||
from ComfyUI_H3_Continuum_Join.v2.session import model_fingerprint
|
||||
|
||||
base = SimpleNamespace(diffusion_model=object())
|
||||
model = SimpleNamespace(
|
||||
model=base,
|
||||
model_options={"transformer_options": {}},
|
||||
wrappers={},
|
||||
model_dtype=lambda: "float32",
|
||||
model_size=lambda: 1,
|
||||
)
|
||||
wrapped = SimpleNamespace(
|
||||
model=base,
|
||||
model_options={"transformer_options": {}},
|
||||
wrappers={
|
||||
"apply_model": {
|
||||
"h3_continuum_join.apply_model.v1": [object()],
|
||||
}
|
||||
},
|
||||
model_dtype=model.model_dtype,
|
||||
model_size=model.model_size,
|
||||
)
|
||||
|
||||
logical = model_fingerprint(
|
||||
model,
|
||||
extra_wrapper_keys=("h3_continuum_join.apply_model.v1",),
|
||||
)
|
||||
|
||||
assert logical == model_fingerprint(wrapped)
|
||||
assert model.wrappers == {}
|
||||
|
||||
Reference in New Issue
Block a user