Files
ComfyUI-H3-Continuum-Tiled/tests/test_v38_review_cache.py
T
chris.dumas 3716b02a57
tests / test (push) Canceled after 0s
tests / windows-storage (push) Canceled after 0s
Archive fork in favor of upstream and DUMAS nodes
2026-10-20 10:46:16 +00:00

320 lines
13 KiB
Python

from __future__ import annotations
import json
import math
import os
from pathlib import Path
import subprocess
import sys
import textwrap
import pytest
from ComfyUI_H3_Continuum_Join.v3.driving_nodes import (
H3ContinuumSamplerV37,
H3ContinuumSamplerV38,
H3ContinuumSamplerV39,
)
from ComfyUI_H3_Continuum_Join.v3.easy_nodes import H3ContinuumEasyV38
from ComfyUI_H3_Continuum_Join.v3.review_control import (
GENERATION_MODE_FULL_RUN,
GENERATION_MODE_REVIEW,
REVIEW_ACTION_CONTINUE,
REVIEW_ACTION_FINISH_REMAINING,
REVIEW_ACTION_REGENERATE_CURRENT,
)
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_COMFY_ROOT = Path(
os.environ.get(
"H3_CONTINUUM_COMFY_ROOT",
str(ROOT.parent / "ComfyUI"),
)
)
@pytest.mark.parametrize(
"review_action",
(
REVIEW_ACTION_CONTINUE,
REVIEW_ACTION_REGENERATE_CURRENT,
REVIEW_ACTION_FINISH_REMAINING,
),
)
def test_v38_review_is_always_changed_for_every_review_action(review_action):
first = H3ContinuumSamplerV38.IS_CHANGED(
generation_mode=GENERATION_MODE_REVIEW,
review_action=review_action,
unrelated_current_input="accepted through Core kwargs",
)
second = H3ContinuumSamplerV38.IS_CHANGED(
generation_mode=GENERATION_MODE_REVIEW,
review_action=review_action,
)
assert math.isnan(first)
assert math.isnan(second)
assert first != second
@pytest.mark.parametrize(
"review_action",
(
REVIEW_ACTION_CONTINUE,
REVIEW_ACTION_REGENERATE_CURRENT,
REVIEW_ACTION_FINISH_REMAINING,
),
)
def test_v38_full_run_keeps_stable_core_cache_behavior(review_action):
assert H3ContinuumSamplerV38.IS_CHANGED(
generation_mode=GENERATION_MODE_FULL_RUN,
review_action=review_action,
) is False
@pytest.mark.parametrize("node_class", [H3ContinuumSamplerV38, H3ContinuumSamplerV39])
@pytest.mark.parametrize("boundary,nonce,storage,changed", [
("Chunk 1", 0, "Save + Auto Resume", True),
("Chunk 2", 0, "Save + Auto Resume", True),
(2, 0, "Save + Auto Resume", True),
("Chunk 2", 5, "Save + Auto Resume", False),
("Auto", 0, "Save + Auto Resume", False),
(0, 0, "Save + Auto Resume", False),
("Chunk 2", 0, "Off", False),
])
def test_full_run_automatic_reroll_reaches_storage(node_class, boundary, nonce, storage, changed):
value = node_class.IS_CHANGED(generation_mode=GENERATION_MODE_FULL_RUN,
run_storage=storage, reroll_from_chunk=boundary, reroll_nonce=nonce, chunks=3)
if changed:
assert math.isnan(value)
else:
assert value is False
def test_v37_and_easy_do_not_inherit_the_v38_cache_contract():
assert "IS_CHANGED" not in H3ContinuumSamplerV37.__dict__
assert "IS_CHANGED" not in H3ContinuumEasyV38.__dict__
assert not hasattr(H3ContinuumSamplerV37, "IS_CHANGED")
assert not hasattr(H3ContinuumEasyV38, "IS_CHANGED")
@pytest.mark.parametrize("node_class", [H3ContinuumSamplerV38, H3ContinuumSamplerV39])
@pytest.mark.parametrize("changes,changed", [
({"chunks": None}, True),
({"chunks": 1}, True),
({"chunks": "not resolved"}, True),
({"reroll_nonce": None}, True),
({"run_storage": None}, True),
({"reroll_from_chunk": None}, True),
({"generation_mode": None}, True),
({"chunks": None, "reroll_nonce": None, "run_storage": None}, True),
({"chunks": None, "reroll_nonce": 5}, False),
({"run_storage": None, "reroll_nonce": 5}, False),
({"reroll_from_chunk": None, "reroll_nonce": 5}, False),
({"reroll_from_chunk": "Auto", "reroll_nonce": None}, False),
({"reroll_from_chunk": "Auto", "run_storage": None}, False),
({"run_storage": "Off", "reroll_nonce": None}, False),
({"run_storage": "Off", "reroll_from_chunk": None}, False),
({"run_storage": "invalid"}, False),
({"reroll_nonce": "invalid"}, False),
({"reroll_from_chunk": "invalid"}, False),
({"reroll_from_chunk": "Chunk 17"}, False),
({"generation_mode": GENERATION_MODE_REVIEW, "run_storage": "Off"}, True),
])
def test_cache_intent_handles_unresolved_inputs_without_validating_chunk_range(node_class, changes, changed):
inputs = dict(generation_mode=GENERATION_MODE_FULL_RUN, run_storage="Save + Auto Resume",
reroll_from_chunk="Chunk 2", reroll_nonce=0, chunks=3)
inputs.update(changes)
value = node_class.IS_CHANGED(**inputs)
assert math.isnan(value) if changed else value is False
def test_real_comfy_cache_signature_is_stable_only_for_full_run(tmp_path):
comfy_root = DEFAULT_COMFY_ROOT
if not (comfy_root / "execution.py").is_file():
pytest.skip("A real ComfyUI runtime is required for the Core cache harness")
harness = tmp_path / "v38-review-core-cache-harness.py"
harness.write_text(
textwrap.dedent(
f"""
import asyncio
import importlib.util
import json
import os
from pathlib import Path
import sys
def block_cuda(*args, **kwargs):
raise BaseException("CPU cache harness requested CUDA initialization")
def early_profile(frame, event, arg):
if event == "c_call" and getattr(arg, "__name__", "") == "_cuda_init":
block_cuda()
sys.setprofile(early_profile)
import torch
assert not torch.cuda.is_initialized()
torch.cuda._lazy_init = block_cuda
torch._C._cuda_init = block_cuda
sys.setprofile(None)
comfy_root = Path({str(comfy_root)!r})
plugin_root = Path({str(ROOT)!r})
os.chdir(comfy_root)
sys.path.insert(0, str(comfy_root))
# Core imports must stay CPU-only; Kitchen's import-time INT8
# capability check otherwise asks for GPU capability even in CPU mode.
sys.argv = [str(__file__), "--cpu"]
import comfy.options
comfy.options.enable_args_parsing()
from comfy.cli_args import args
assert args.cpu
# This harness tests Core cache keys, not a GPU attention kernel.
# Optional SageAttention probes GPU capability at import time even
# with Core --cpu. Model an absent optional extension in this child
# only; leave installed packages and all CUDA tripwires unchanged.
sys.modules["sageattention"] = None
if importlib.util.find_spec("comfy_kitchen") is not None:
import comfy_kitchen
comfy_kitchen.int8_attention_is_available = lambda *a, **k: False
import nodes as comfy_nodes
from comfy_execution.caching import BasicCache, CacheKeySetInputSignature
from comfy_execution.graph import DynamicPrompt
from execution import IsChangedCache
package_name = "H3ContinuumPhaseFR1Source"
spec = importlib.util.spec_from_file_location(
package_name,
plugin_root / "__init__.py",
submodule_search_locations=[str(plugin_root)],
)
package = importlib.util.module_from_spec(spec)
package.__path__ = [str(plugin_root)]
sys.modules[package_name] = package
spec.loader.exec_module(package)
class_type = "H3ContinuumSamplerV38PhaseFR1Probe"
comfy_nodes.NODE_CLASS_MAPPINGS[class_type] = (
package.NODE_CLASS_MAPPINGS["H3ContinuumSamplerV38"]
)
async def signature(mode, action):
prompt = {{
"305": {{
"class_type": class_type,
"inputs": {{
"generation_mode": mode,
"review_action": action,
}},
}}
}}
dynprompt = DynamicPrompt(prompt)
changed = IsChangedCache("phase-f-r1", dynprompt, None)
keys = CacheKeySetInputSignature(dynprompt, ["305"], changed)
await keys.add_keys(["305"])
return keys.get_data_key("305")
async def main():
full_1 = await signature("Full Run", "Continue / Next")
full_2 = await signature("Full Run", "Continue / Next")
review_continue_1 = await signature(
"Review Each Chunk", "Continue / Next"
)
review_continue_2 = await signature(
"Review Each Chunk", "Continue / Next"
)
review_regen_1 = await signature(
"Review Each Chunk", "Regenerate Current"
)
review_regen_2 = await signature(
"Review Each Chunk", "Regenerate Current"
)
review_finish_1 = await signature(
"Review Each Chunk", "Finish Remaining"
)
review_finish_2 = await signature(
"Review Each Chunk", "Finish Remaining"
)
cache_results = {{}}
for node_name in ["H3ContinuumSamplerV38", "H3ContinuumSamplerV39"]:
comfy_nodes.NODE_CLASS_MAPPINGS[class_type] = package.NODE_CLASS_MAPPINGS[node_name]
observed_counts = {{}}
for label, boundary, nonce, linked in [
("ordinary", "Auto", 0, None),
("automatic", "Chunk 2", 0, None),
("fixed", "Chunk 2", 5, None),
("linked_chunks", "Chunk 2", 0, "chunks"),
("linked_nonce", "Chunk 2", 0, "reroll_nonce"),
("linked_storage", "Chunk 2", 0, "run_storage"),
("linked_boundary", "Chunk 2", 0, "reroll_from_chunk"),
("linked_mode", "Chunk 2", 5, "generation_mode"),
("fixed_linked_chunks", "Chunk 2", 5, "chunks"),
("ordinary_linked_nonce", "Auto", 0, "reroll_nonce"),
("fixed_linked_nonce", "Chunk 2", 5, "reroll_nonce"),
]:
cache = BasicCache(CacheKeySetInputSignature)
executions = 0
for queue in range(2):
prompt = {{"305": {{"class_type": class_type, "inputs": {{
"generation_mode": "Full Run", "review_action": "Continue / Next",
"run_storage": "Save + Auto Resume", "reroll_from_chunk": boundary,
"reroll_nonce": nonce, "chunks": 3}}}}}}
if linked:
class ScalarProbe:
@classmethod
def INPUT_TYPES(cls):
return {{"required": {{"value": ("*",)}}}}
comfy_nodes.NODE_CLASS_MAPPINGS["H3CacheScalarProbe"] = ScalarProbe
value = prompt["305"]["inputs"][linked]
prompt["900"] = {{"class_type": "H3CacheScalarProbe", "inputs": {{"value": value}}}}
prompt["305"]["inputs"][linked] = ["900", 0]
dynprompt = DynamicPrompt(prompt)
await cache.set_prompt(dynprompt, ["305"], IsChangedCache(str(queue), dynprompt, None))
if cache.get_local("305") is None:
executions += 1
cache.set_local("305", ("CPU fixture output",))
observed_counts[label] = executions
cache_results[node_name] = observed_counts
print(json.dumps({{
"cache_executions": cache_results,
"full_stable": full_1 == full_2,
"review_continue_changed": review_continue_1 != review_continue_2,
"review_regenerate_changed": review_regen_1 != review_regen_2,
"review_finish_changed": review_finish_1 != review_finish_2,
"cpu_mode": args.cpu,
"cuda_initialized": torch.cuda.is_initialized(),
}}))
asyncio.run(main())
"""
),
encoding="utf-8",
)
result = subprocess.run(
[sys.executable, str(harness)],
cwd=comfy_root,
check=True,
capture_output=True,
text=True,
)
observed = json.loads(result.stdout.strip().splitlines()[-1])
assert observed == {
"cache_executions": {
name: {"ordinary": 1, "automatic": 2, "fixed": 1,
"linked_chunks": 2, "linked_nonce": 2, "linked_storage": 2,
"linked_boundary": 2, "linked_mode": 2, "fixed_linked_chunks": 1,
"ordinary_linked_nonce": 1, "fixed_linked_nonce": 2}
for name in ("H3ContinuumSamplerV38", "H3ContinuumSamplerV39")
},
"full_stable": True,
"review_continue_changed": True,
"review_regenerate_changed": True,
"review_finish_changed": True,
"cpu_mode": True,
"cuda_initialized": False,
}