MMnode/scripts/test_rtx_upscale_integration.py
Z User 563322a3e2 feat: simple hidden half-res sampling, restored at the video combine
Single-pass projects condition and sample at exactly half the
editor-selected resolution (plain integer halving, no dimension
policies). Moxie Project Video Combine restores the exact editor
resolution on the combined frames right before its images output,
driven by the fresh project manifest so a stale frontend widget can
never misgate it.

- utils/rtx_vsr.py: RTX VSR core (Apache 2.0, from
  Comfy-Org/Nvidia_RTX_Nodes_ComfyUI) + Lanczos fallback; the gate is
  simply 'frames differ from the manifest resolution'
- nodes/project.py: halve first-pass dims for single mode only;
  dual-pass, SelfLift, passthrough and audio-only keep the editor size
- moxie_combine.py: restore before the images output
- scripts/test_rtx_upscale_integration.py: 17 standalone checks
2026-09-27 01:18:33 +00:00

185 lines
5.8 KiB
Python

"""Standalone sanity checks for the hidden half-res output restore.
Runs without ComfyUI or torch: minimal stubs exercise the gating logic of
utils/rtx_vsr.py and verify the plain-halving gate in nodes/project.py plus
the MoxieCombine wiring.
"""
from __future__ import annotations
import ast
import importlib
import py_compile
import sys
import types
from pathlib import Path
PACK = Path(__file__).resolve().parents[1]
failures: list[str] = []
def check(name: str, condition: bool) -> None:
status = "PASS" if condition else "FAIL"
print(f"[{status}] {name}")
if not condition:
failures.append(name)
# --- stub module surface ----------------------------------------------------
class FakeTensor:
def __init__(self, shape):
self.shape = tuple(shape)
self.ndim = len(self.shape)
self.device = "cpu"
self.dtype = "float32"
def __getitem__(self, item):
return self
def movedim(self, *args, **kwargs):
return self
torch_stub = types.ModuleType("torch")
torch_stub.Tensor = FakeTensor
torch_stub.empty = lambda *a, **k: FakeTensor(a[0])
sys.modules["torch"] = torch_stub
upscale_calls: list[tuple] = []
comfy_stub = types.ModuleType("comfy")
comfy_utils_stub = types.ModuleType("comfy.utils")
def _fake_common_upscale(samples, width, height, method, crop):
upscale_calls.append((width, height, method, crop))
return samples
comfy_utils_stub.common_upscale = _fake_common_upscale
comfy_stub.utils = comfy_utils_stub
sys.modules["comfy"] = comfy_stub
sys.modules["comfy.utils"] = comfy_utils_stub
MANIFEST: dict = {"width": 0, "height": 0}
h3_stub = types.ModuleType("utils.h3_project")
h3_stub.safe_h3_project_name = lambda value: str(value or "default")
def _fake_load(project_name):
if MANIFEST.get("missing"):
raise FileNotFoundError("no project")
return dict(MANIFEST)
h3_stub.load_h3_project_data = _fake_load
utils_pkg = types.ModuleType("utils")
utils_pkg.__path__ = [str(PACK / "utils")]
sys.modules["utils"] = utils_pkg
sys.modules["utils.h3_project"] = h3_stub
rtx_vsr = importlib.import_module("utils.rtx_vsr")
# 1. half-res frames are restored to the exact manifest size (lanczos here,
# since nvidia-vfx is absent in the sandbox)
MANIFEST.update({"width": 1920, "height": 1080})
upscale_calls.clear()
rtx_vsr.upscale_images_to_project_resolution(FakeTensor((4, 528, 960, 3)), "proj")
check(
"half-res frames restored to manifest size",
upscale_calls == [(1920, 1080, "lanczos", "disabled")],
)
# 2. frames already at the project resolution pass through untouched
upscale_calls.clear()
rtx_vsr.upscale_images_to_project_resolution(FakeTensor((4, 1080, 1920, 3)), "proj")
check("full-res frames pass through", upscale_calls == [])
# 3. missing manifest passes through
MANIFEST.update({"missing": True})
upscale_calls.clear()
rtx_vsr.upscale_images_to_project_resolution(FakeTensor((4, 528, 960, 3)), "proj")
check("missing manifest passes through", upscale_calls == [])
MANIFEST.pop("missing")
# 4. zero and audio-only sentinel manifests pass through
for width, height in ((0, 0), (32, 32)):
MANIFEST.update({"width": width, "height": height})
upscale_calls.clear()
rtx_vsr.upscale_images_to_project_resolution(FakeTensor((4, 528, 960, 3)), "proj")
check(f"manifest {width}x{height} passes through", upscale_calls == [])
MANIFEST.update({"width": 1920, "height": 1080})
# 5. non-tensor and empty-batch inputs pass through
check(
"non-tensor passthrough",
rtx_vsr.upscale_images_to_project_resolution("nope", "proj") == "nope",
)
check(
"empty batch passthrough",
rtx_vsr.upscale_images_to_project_resolution(
FakeTensor((0, 528, 960, 3)), "proj"
).shape[0] == 0,
)
# 6. rtx_vsr_upscale identity early-returns (no upscale machinery touched)
same = FakeTensor((2, 1080, 1920, 3))
check("vsr same-size identity", rtx_vsr.rtx_vsr_upscale(same, 1920, 1080) is same)
zero = FakeTensor((0, 528, 960, 3))
check("vsr empty identity", rtx_vsr.rtx_vsr_upscale(zero, 1920, 1080) is zero)
# 7. nodes/project.py carries the plain-halving gate for single-pass only
project_source = (PACK / "nodes" / "project.py").read_text(encoding="utf-8")
check(
"single-pass half-res gate present",
"editor_width // 2" in project_source
and 'sampling_mode == "single"' in project_source,
)
check(
"no 16px snap policy left behind",
"half_up" not in project_source
and "h3_halfres_sampling_dimensions" not in project_source,
)
# 8. MoxieCombine wires the restore right before the images output
combine_source = (PACK / "moxie_combine.py").read_text(encoding="utf-8")
check(
"combine restores before images output",
"upscale_images_to_project_resolution" in combine_source,
)
check(
"combine passes the safe project name",
"safe_h3_project_name(project_name)" in combine_source,
)
# 9. heavy imports stay lazy in rtx_vsr (pack import stays light)
tree = ast.parse((PACK / "utils" / "rtx_vsr.py").read_text(encoding="utf-8"))
module_level: list[str] = []
for node in tree.body:
if isinstance(node, ast.Import):
module_level.extend(alias.name for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.level == 0:
module_level.append(node.module or "")
check(
"no torch/nvvfx/comfy imports at module level",
not any(
name.split(".")[0] in {"torch", "nvvfx", "comfy"} for name in module_level
),
)
# 10. everything touched compiles
for rel in ("nodes/project.py", "moxie_combine.py", "utils/rtx_vsr.py"):
try:
py_compile.compile(str(PACK / rel), doraise=True)
check(f"py_compile {rel}", True)
except py_compile.PyCompileError as error:
check(f"py_compile {rel}", False)
print(f" {error}")
print()
if failures:
print(f"{len(failures)} CHECK(S) FAILED")
sys.exit(1)
print("ALL CHECKS PASSED")