fix: reuse conditioning cache for reference-free Fixed V3.9 runs

Reuse the existing bounded prompt-conditioning cache only for safe reference-free Fixed groups. Preserve Reference, List/Timeline, terminal-pair, Sampling and Run Storage paths; add 22 regression cases and bilingual update guidance.

Local validation: 1558 CPU passed, 1 skipped; eight successful GPU generations. Post-fix same-session warm total medians: V3.8 273.579s vs V3.9 273.779s, with overlapping ranges. This scoped result is not a universal speed or GPU-kernel claim. Official workflows/ZIPs and historical Releases are unchanged.
This commit is contained in:
ukr8b3g-cmyk
2026-10-01 16:13:48 +09:00
parent 29e0ce3dd3
commit 21a857115d
8 changed files with 316 additions and 74 deletions
+156
View File
@@ -0,0 +1,156 @@
"""R0/Fixed-only cache parity; no Sampling or CUDA is used by these tests."""
from types import SimpleNamespace
import pytest
import torch
from ComfyUI_H3_Continuum_Join.v2 import h3_builder, sequence
from ComfyUI_H3_Continuum_Join.v3 import reference_runtime
from ComfyUI_H3_Continuum_Join.v3.reference_storage_contract import (
ReferenceStorageContractError, build_reference_storage_plan,
)
from .test_v39_rr_r4_reference_runtime import _runtime, _image, _Clip, _VAE
def _clip():
clip = _Clip()
clip.patcher = SimpleNamespace(patches_uuid="patch-a")
return clip
def _prepare(runtime, clip, *, group=1, prompt="running", first=None, last=None, **kwargs):
return runtime.prepare_group(
physical_group=group, prompt=prompt, clip=clip, video_vae=_VAE(),
first_image=first, last_image=last, prompt_conditioning_cache=True,
**kwargs,
)
@pytest.mark.parametrize("first", [None, _image(0.1)])
def test_same_fixed_payload_hits_and_preserves_group_specific_contract(first):
router = _runtime(slots={}, selectors={}, chunks=2)
clip, events = _clip(), []
one = _prepare(router, clip, first=first, prompt_cache_event=events.append)
two = _prepare(router, clip, group=2, first=first, prompt_cache_event=events.append)
assert len(clip.calls) == 1 and events == ["miss", "hit"]
assert one.conditioning is not two.conditioning
assert torch.equal(one.conditioning[0][0], two.conditioning[0][0])
assert one.conditioning[0][1] == two.conditioning[0][1]
assert one.physical_group == 1 and two.physical_group == 2
assert router.observed_group_contracts[1] != router.observed_group_contracts[2]
def test_cache_is_identical_to_v38_and_shares_existing_clip_cache():
first = _image(0.1)
clip, events = _clip(), []
assets = h3_builder.IdentityAssets(first, None, None, None, h3_builder._tensor_fingerprint(first))
legacy = sequence._conditioning_cache(
clip=clip, prompts=["running"], assets=assets, final_has_last_frame=False,
prompt_conditioning_cache=True, prompt_cache_event=events.append,
)[("running", False)]
result = _prepare(_runtime(slots={}, selectors={}), clip, first=first,
prompt_cache_event=events.append)
assert len(clip.calls) == 1 and events == ["miss", "hit"]
assert torch.equal(legacy[0][0], result.conditioning[0][0])
assert legacy[0][1] == result.conditioning[0][1]
def test_returned_metadata_cannot_poison_cache_or_next_group():
router, clip = _runtime(slots={}, selectors={}), _clip()
one = _prepare(router, clip)
one.conditioning[0][1]["minimax_keyframes"] = [{"bad": True}]
two = _prepare(router, clip, group=2)
assert "minimax_keyframes" not in two.conditioning[0][1]
assert len(clip.calls) == 1
@pytest.mark.parametrize("change", ["prompt", "first", "last", "patch", "layer", "clip"])
def test_changed_payload_or_clip_never_reuses_old_conditioning(change):
router, clip = _runtime(slots={}, selectors={}, chunks=2), _clip()
first, last = _image(0.1), _image(0.9)
_prepare(router, clip, first=first, last=last)
prompt, target = "running", clip
if change == "prompt": prompt = "walking"
if change == "first": first.fill_(0.2)
if change == "last": last.fill_(0.8)
if change == "patch": clip.patcher.patches_uuid = "patch-b"
if change == "layer": clip.layer_idx = -2
if change == "clip": target = _clip()
events = []
_prepare(router, target, group=2, prompt=prompt, first=first, last=last,
prompt_cache_event=events.append)
assert events == ["miss"]
assert len(clip.calls) + (len(target.calls) if target is not clip else 0) == 2
def test_last_image_is_cached_by_actual_presentation_not_just_presence():
router, clip = _runtime(slots={}, selectors={}, chunks=3), _clip()
first, last = _image(0.1), _image(0.9)
events = []
for group in (1, 2, 3):
_prepare(router, clip, group=group, first=first, last=last,
include_last_image=group == 3, prompt_cache_event=events.append)
assert events == ["miss", "hit", "miss"]
assert len(clip.calls[0][1]["images"]) == 1
assert len(clip.calls[1][1]["images"]) == 2
def test_any_selected_reference_in_schedule_keeps_existing_direct_path():
router = _runtime(slots={"R1": _image(0.2)}, selectors={"R1": "2"}, chunks=3)
clip = _clip()
for group in (1, 2, 3):
_prepare(router, clip, group=group)
assert len(clip.calls) == 3
assert not hasattr(clip, h3_builder._PROMPT_CACHE_ATTR)
@pytest.mark.parametrize("asset", ["reference_audio_assets", "timeline_video_assets"])
def test_multimodal_inputs_bypass_without_changing_direct_encode_kwargs(monkeypatch, asset):
calls = []
marker = object()
def direct(clip, prompt, **kwargs):
calls.append(kwargs)
return [[torch.zeros(1), {"prompt": prompt}]]
monkeypatch.setattr(reference_runtime, "encode_prompt_conditioning", direct)
router, clip = _runtime(slots={}, selectors={}), _clip()
for group in (1, 2):
_prepare(router, clip, group=group, **{asset: marker})
assert len(calls) == 2 and all(call[asset] is marker for call in calls)
assert not hasattr(clip, h3_builder._PROMPT_CACHE_ATTR)
def test_special_clip_modes_fall_back_without_new_error_or_stop():
router, clip = _runtime(slots={}, selectors={}), _clip()
clip.use_clip_schedule = True
events = []
for group in (1, 2):
_prepare(router, clip, group=group, prompt_cache_event=events.append)
assert len(clip.calls) == 2 and events == ["bypass_special_clip"] * 2
def test_cuda_conditioning_is_not_retained_by_routed_cache(monkeypatch):
monkeypatch.setattr(h3_builder, "_contains_cuda_tensor", lambda _: True)
router, clip = _runtime(slots={}, selectors={}), _clip()
events = []
for group in (1, 2):
_prepare(router, clip, group=group, prompt_cache_event=events.append)
assert len(clip.calls) == 2 and events == ["bypass_cuda_output"] * 2
def test_saved_contract_preflight_is_not_skipped_by_cache_hit():
router, clip = _runtime(slots={}, selectors={}, chunks=2), _clip()
plan = build_reference_storage_plan(runtime=router, prompts=["running"] * 2,
first_frame_hash="none", last_frame_hash="none")
_prepare(router, clip, expected_group_contract=plan.group_for(1))
with pytest.raises(ReferenceStorageContractError, match="changed after"):
_prepare(router, clip, group=2, prompt="changed",
expected_group_contract=plan.group_for(2))
assert len(clip.calls) == 1
def test_default_disabled_retains_existing_two_encodes():
router, clip = _runtime(slots={}, selectors={}, chunks=2), _clip()
for group in (1, 2):
router.prepare_group(physical_group=group, prompt="running", clip=clip,
video_vae=_VAE(), first_image=None, last_image=None)
assert len(clip.calls) == 2
+48 -2
View File
@@ -12,6 +12,8 @@ from ComfyUI_H3_Continuum_Join.constants import (
DIAGNOSTICS_BASIC,
DIAGNOSTICS_OFF,
PROMPT_MODE_FIXED,
PROMPT_MODE_LIST,
PROMPT_MODE_TIMELINE,
V2_CONTINUITY_OPTIONS,
)
from ComfyUI_H3_Continuum_Join.temporal import audio_latent_t, video_latent_t
@@ -117,9 +119,10 @@ def _router(*, slots, selected_by_slot, chunks=3, terminal=False):
def _run(runtime, router, *, chunks=3, limit=None, first=None, last=None,
session=None, script="running", diagnostics=DIAGNOSTICS_BASIC):
session=None, script="running", diagnostics=DIAGNOSTICS_BASIC,
prompt_cache=False, prompt_mode=PROMPT_MODE_FIXED):
prompt_plan = make_prompt_plan(
mode=PROMPT_MODE_FIXED, script=script, chunks=chunks,
mode=prompt_mode, script=script, chunks=chunks,
chunk_seconds=5.0,
)
return sequence.run_sequence_with_reference_routing(
@@ -132,9 +135,52 @@ def _run(runtime, router, *, chunks=3, limit=None, first=None, last=None,
diagnostics_mode=diagnostics, reroll_from_chunk=0,
reroll_nonce=0, strict_compatibility=False, debug=False,
session=session, latent_only=True, max_new_physical_groups=limit,
capture_refine_context=prompt_cache, prompt_conditioning_cache=prompt_cache,
)
@pytest.mark.parametrize("mode,script,cached", [
(PROMPT_MODE_FIXED, "running", True),
(PROMPT_MODE_TIMELINE, "[0-5s]\nrunning\n[5-10s]\nrunning", False),
(PROMPT_MODE_LIST, '["running", "running"]', False),
])
def test_r0_prompt_cache_is_fixed_only_and_keeps_sampling_contract(monkeypatch, mode, script, cached):
first = _image(0.1)
runtime = _runtime(monkeypatch, first=first)
runtime.clip.patcher = SimpleNamespace(patches_uuid="patch-a")
router = _router(slots={}, selected_by_slot={}, chunks=2)
entries, _, session, *_ = _run(
runtime, router, chunks=2, first=first, script=script,
prompt_cache=True, prompt_mode=mode,
)
assert len(entries) == 2 and len(runtime.samples) == 2
assert len(runtime.clip.calls) == (1 if cached else 2)
assert [item["prompt"] for item in runtime.samples] == ["running", "running"]
assert [item["refs"] for item in runtime.samples] == [[], []]
assert [entry["seed"] for entry in entries] == [entry["seed"] for entry in session["chunks"]]
assert len(router.observed_group_contracts) == 2
def test_r0_cache_on_off_cpu_replay_preserves_latents_seeds_and_storage_descriptors(monkeypatch):
from .test_audit_boundary_repairs import assert_nested_equal
from ComfyUI_H3_Continuum_Join.v2 import session as session_module
import uuid
# Fix only fresh-session bookkeeping; compare all execution data unchanged.
monkeypatch.setattr(session_module.uuid, "uuid4", lambda: uuid.UUID(int=42))
monkeypatch.setattr(session_module, "_now_iso", lambda: "2026-10-01T00:00:00+00:00")
first = _image(0.1)
outcomes = []
for enabled in (False, True):
runtime = _runtime(monkeypatch, first=first)
runtime.clip.patcher = SimpleNamespace(patches_uuid="patch-a")
router = _router(slots={}, selected_by_slot={}, chunks=2)
entries, state, session, *_ = _run(
runtime, router, chunks=2, first=first, prompt_cache=enabled,
)
outcomes.append((entries, state, session, router.observed_group_contracts))
assert_nested_equal(outcomes[1], outcomes[0])
def test_review_limited_sequence_prepares_only_groups_to_generate(monkeypatch):
runtime = _runtime(monkeypatch)
router = _router(