Files
ComfyUI-H3-Continuum-Tiled/model_patch.py
T

164 lines
6.4 KiB
Python

"""Per-MODEL ComfyUI wrapper for H3 Continuum Join."""
from __future__ import annotations
import logging
from typing import Any, Callable
from .compatibility import CompatibilityError, ensure_native_h3_base_model
from .constants import (
CONTINUUM_ACTUAL_PREFIX_STEPS,
CONTINUUM_INTEROP_API,
CONTINUUM_INTEROP_KEY,
MODEL_WRAPPER_KEY,
)
from .layout_adapter import (
LayoutCompatibilityError,
materialize_continuum_latents,
normalize_condition_latents,
patch_layout_in_place,
payload_has_continuum,
validate_native_continuity_layout,
)
def _wrapper_factory(*, strict: bool, debug: bool) -> Callable[..., Any]:
branch_baselines: dict[tuple[Any, ...], tuple[Any, ...]] = {}
def apply_model_wrapper(executor, *args, **kwargs):
payload = kwargs.get("minimax_payload")
has_continuum = payload_has_continuum(payload)
has_mixed_keyframe_refs = (
isinstance(payload, dict)
and bool(payload.get("keyframes"))
and payload.get("refs") is not None
)
if not has_continuum and not has_mixed_keyframe_refs:
return executor(*args, **kwargs)
try:
try:
ensure_native_h3_base_model(executor.class_obj)
except CompatibilityError as exc:
logging.getLogger(__name__).warning(
"H3 Continuum could not pre-classify this model; continuing with "
"ComfyUI runtime validation: %s",
exc,
)
patched_payload = dict(payload)
if has_continuum:
patch_layout_in_place(patched_payload, strict=True, debug=debug)
transformer_options = kwargs.get("transformer_options")
validate_native_continuity_layout(
patched_payload,
transformer_options=(
transformer_options if isinstance(transformer_options, dict) else {}
),
branch_baselines=branch_baselines,
)
input_x = args[0] if args else None
materialize_continuum_latents(patched_payload, input_x, debug=debug)
# ComfyUI Core 0.33.1 replaces keyframe condition latents with
# reference latents when both contracts are present. Rebuild the
# combined list for First/Last Frame + standalone Reference Audio.
normalize_condition_latents(patched_payload)
kwargs["minimax_payload"] = patched_payload
except (CompatibilityError, LayoutCompatibilityError, KeyError, TypeError, ValueError) as exc:
raise RuntimeError(f"H3 Continuum Join compatibility failure: {exc}") from exc
return executor(*args, **kwargs)
return apply_model_wrapper
def configure_continuum_model(model: Any, *, strict: bool, debug: bool):
try:
from comfy.patcher_extension import WrappersMP
except Exception as exc: # pragma: no cover
raise RuntimeError(f"ComfyUI model-wrapper API unavailable: {exc}") from exc
required_api = ("add_wrapper_with_key", "remove_wrappers_with_key")
missing_api = [name for name in required_api if not hasattr(model, name)]
if missing_api:
raise RuntimeError(
"MODEL lacks the wrapper API required by H3 Continuum Join: " + ", ".join(missing_api)
)
base_model = getattr(model, "model", None)
if base_model is None:
raise RuntimeError("MODEL does not expose the ComfyUI BaseModel")
try:
ensure_native_h3_base_model(base_model)
except CompatibilityError as exc:
logging.getLogger(__name__).warning(
"H3 Continuum could not pre-classify this model; installing the wrapper "
"and deferring compatibility to ComfyUI runtime validation: %s",
exc,
)
patched = model
patched.remove_wrappers_with_key(WrappersMP.APPLY_MODEL, MODEL_WRAPPER_KEY)
patched.add_wrapper_with_key(
WrappersMP.APPLY_MODEL,
MODEL_WRAPPER_KEY,
_wrapper_factory(strict=bool(strict), debug=bool(debug)),
)
model_options = dict(getattr(patched, "model_options", None) or {})
join_options = dict(model_options.get("h3_continuum_join") or {})
join_options.update({"api_version": 1, "strict": bool(strict)})
model_options["h3_continuum_join"] = join_options
patched.model_options = model_options
return patched
def patch_model(model: Any, *, strict: bool, debug: bool):
"""Clone once, then install the Continuum wrapper on that clone."""
if not hasattr(model, "clone"):
raise RuntimeError(
"MODEL lacks the wrapper API required by H3 Continuum Join: clone"
)
return configure_continuum_model(model.clone(), strict=strict, debug=debug)
def continuum_interop_request(*, chunk_index: int, context_frames: int) -> dict[str, Any]:
return {
"api": CONTINUUM_INTEROP_API,
"active": True,
"min_actual_prefix_steps": CONTINUUM_ACTUAL_PREFIX_STEPS,
"chunk_index": int(chunk_index),
"context_frames": int(context_frames),
}
def clone_model_for_chunk(
model: Any,
*,
strict: bool,
debug: bool,
chunk_index: int,
context_frames: int | None,
):
"""Create a call-local MODEL and attach an optional read-only Spectrum hint."""
if not hasattr(model, "clone"):
raise RuntimeError(
"MODEL lacks the wrapper API required by H3 Continuum Join: clone"
)
chunk_model = model.clone()
configure_continuum_model(
chunk_model, strict=bool(strict), debug=bool(debug)
)
if debug:
parent = getattr(chunk_model, "parent", None)
logging.getLogger(__name__).info(
"Continuum model lifetime: input=%s chunk=%s parent=%s base=%s",
id(model),
id(chunk_model),
id(parent) if parent is not None else None,
id(getattr(chunk_model, "model", None)),
)
model_options = dict(getattr(chunk_model, "model_options", None) or {})
transformer_options = dict(model_options.get("transformer_options") or {})
if context_frames is None:
transformer_options.pop(CONTINUUM_INTEROP_KEY, None)
else:
transformer_options[CONTINUUM_INTEROP_KEY] = continuum_interop_request(
chunk_index=int(chunk_index), context_frames=int(context_frames)
)
model_options["transformer_options"] = transformer_options
chunk_model.model_options = model_options
return chunk_model