MMnode/scripts/test_t2v_r2v_and_loader.py
Z User 88849ced65 refactor: remove the LoRA pipeline; distill weights ship baked into the model
The 3-step distill (speedup) LoRA is now baked into the diffusion model
itself, so the Moxie Multimedia Loader no longer applies any LoRA:

- moxie_loader.py: drop the models/loras scan, _auto_lora(), the module
  level LoRA cache and _apply_lora(); the load chain is now
  diffusion model -> attention backend -> CLIP -> VAEs. A leftover file
  in models/loras is silently ignored and the folder can be deleted.
- moxie_project.py / moxie_preview.py: tooltips reference the baked-in
  distill instead of the LoRA.
- tests + standalone loader script: assert no load_lora_for_models call
  happens (the comfy.sd stub now raises if the loader tries), and fix
  the pre-existing MMnode.tiny_vae stub gap (missing pack_vae_approx_files)
  that broke the whole test module at fixture setup.
- README: models table drops the Turbo LoRA row; CHANGELOG entry added.

Node schema, outputs and sampling behavior are unchanged; workflows
saved before this change keep working.
2026-10-13 03:34:47 +00:00

448 lines
18 KiB
Python

#!/usr/bin/env python3
"""Standalone verification: native T2V resolution + one-model loader.
No ComfyUI required. Checks (matching the standing verification rules):
1. utils/h3_project.py - h3_task_type / h3_generation_mode resolve T2V
natively (t2v stays t2v; multi_frames generation mode).
2. nodes/basic.py - _multitrack_task_type mirrors the native values.
3. utils/llm_api.py - reference-mode with zero media raises; t2v maps
to the cloud-native text-only mode.
4. moxie_loader.py - schema shows a single model picker; execute()
auto-picks LoRA/VAE/CLIP from the pack's models/ folders and still
applies the LoRA to model+clip at strength 1.0.
Run: python3 scripts/test_t2v_r2v_and_loader.py
"""
from __future__ import annotations
import ast
import enum
import importlib.util
import os
import sys
import tempfile
import types
FAILED = []
def check(name, condition, detail=""):
status = "PASS" if condition else "FAIL"
print(f"[{status}] {name}" + (f" - {detail}" if detail and not condition else ""))
if not condition:
FAILED.append(name)
# ---------------------------------------------------------------------------
# AST extraction helpers
# ---------------------------------------------------------------------------
def extract_functions(path, names, constants=()):
"""Return {name: function} for top-level (or class-level) defs.
All requested functions plus any requested module-level constants are
compiled into ONE shared namespace so they can reference each other.
"""
with open(path, "r", encoding="utf-8") as handle:
tree = ast.parse(handle.read())
body = []
def visit(nodes):
for node in nodes:
if isinstance(node, ast.FunctionDef) and node.name in names:
body.append(ast.FunctionDef(
name=node.name, args=node.args, body=node.body,
decorator_list=[], returns=node.returns,
type_comment=None, type_params=[],
))
elif isinstance(node, ast.ClassDef):
visit(node.body)
elif (
isinstance(node, ast.Assign) and constants and any(
isinstance(target, ast.Name) and target.id in constants
for target in node.targets
)
):
body.append(node)
visit(tree.body)
ast.fix_missing_locations(ast.Module(body=body, type_ignores=[]))
namespace = {"__name__": "extracted"}
exec(compile(ast.Module(body=body, type_ignores=[]), path, "exec"), namespace)
missing = set(names) - set(namespace)
if missing:
raise RuntimeError(f"could not extract from {path}: {sorted(missing)}")
return namespace
def extract_constant(path, name):
with open(path, "r", encoding="utf-8") as handle:
tree = ast.parse(handle.read())
for node in tree.body:
if isinstance(node, ast.Assign) and any(
isinstance(target, ast.Name) and target.id == name for target in node.targets
):
return ast.literal_eval(node.value)
raise RuntimeError(f"constant {name} not found in {path}")
# ---------------------------------------------------------------------------
# 1. utils/h3_project.py - T2V resolves natively (upstream behavior)
# ---------------------------------------------------------------------------
def verify_h3_project():
print("\n== 1. utils/h3_project.py: h3_task_type / h3_generation_mode ==")
h3_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "utils")
h3_file = os.path.join(h3_dir, "h3_project.py")
h3_ns = extract_functions(
h3_file,
["h3_task_type", "h3_generation_mode", "_frame_value"],
)
def muted_image(image):
return bool(isinstance(image, dict) and image.get("muted"))
h3_ns["multitrack_is_muted_image"] = muted_image
h3_task_type = h3_ns["h3_task_type"]
h3_generation_mode = h3_ns["h3_generation_mode"]
def make_info(with_video=False):
tracks = [{"type": "task", "segments": [{"start_frame": 0, "end_frame": 48, "content": {}}]}]
if with_video:
tracks.append({
"type": "video",
"segments": [{"start_frame": 0, "end_frame": 48, "content": {}}],
})
return {"tracks": tracks}
def entry_for(mode=None, task_type=None, images=None, info=None):
content = {}
if mode is not None:
content["task_mode"] = mode
if task_type is not None:
content["task_type"] = task_type
if images is not None:
content["images"] = images
return {"start_frame": 0, "end_frame": 48, "task": {"content": content}}
check("default mode, no images -> t2v",
h3_task_type(entry_for(mode="default"), make_info()) == "t2v")
check("no task_mode, no images -> t2v",
h3_task_type(entry_for(), make_info()) == "t2v")
check("explicit t2v -> t2v",
h3_task_type(entry_for(task_type="t2v"), make_info()) == "t2v")
check("explicit T2V (uppercase) -> t2v",
h3_task_type(entry_for(task_type="T2V"), make_info()) == "t2v")
check("explicit custom type passes through",
h3_task_type(entry_for(task_type="wan-2.2-fun"), make_info()) == "wan-2.2-fun")
check("muted images ignored -> t2v",
h3_task_type(entry_for(images=[{"muted": True}]), make_info()) == "t2v")
check("one active image -> i2v unchanged",
h3_task_type(entry_for(images=[{"id": "a"}]), make_info()) == "i2v")
check("ref mode, no video -> r2v",
h3_task_type(entry_for(mode="ref"), make_info()) == "r2v")
check("ref mode, video in range -> rv2v",
h3_task_type(entry_for(mode="ref"), make_info(with_video=True)) == "rv2v")
check("l2v mode -> l2v",
h3_task_type(entry_for(mode="l2v"), make_info()) == "l2v")
check("edit mode, no images -> v2v",
h3_task_type(entry_for(mode="edit"), make_info()) == "v2v")
check("generation_mode(t2v) -> multi_frames",
h3_generation_mode("t2v") == "multi_frames")
check("generation_mode(r2v) -> reference",
h3_generation_mode("r2v") == "reference")
check("generation_mode(rv2v) -> reference",
h3_generation_mode("rv2v") == "reference")
check("generation_mode(i2v) -> multi_frames",
h3_generation_mode("i2v") == "multi_frames")
check("generation_mode(l2v) -> last_frame",
h3_generation_mode("l2v") == "last_frame")
# ---------------------------------------------------------------------------
# 2. nodes/basic.py - editor task output mirrors the normalization
# ---------------------------------------------------------------------------
def verify_basic_node():
print("\n== 2. nodes/basic.py: _multitrack_task_type ==")
basic_file = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "nodes", "basic.py"
)
resolver = extract_functions(basic_file, ["_multitrack_task_type"])["_multitrack_task_type"]
check("explicit t2v -> t2v",
resolver({"content": {"task_type": "t2v"}}, 0, False) == "t2v")
check("default mode, no images -> t2v",
resolver({"content": {"task_mode": "default"}}, 0, False) == "t2v")
check("no content -> t2v", resolver({}, 0, False) == "t2v")
check("custom type kept",
resolver({"content": {"task_type": "wan-2.2-fun"}}, 0, False) == "wan-2.2-fun")
check("ref mode no video -> r2v",
resolver({"content": {"task_mode": "ref"}}, 0, False) == "r2v")
check("one image -> i2v",
resolver({"content": {"task_mode": "default"}}, 1, False) == "i2v")
check("edit mode no images -> v2v",
resolver({"content": {"task_mode": "edit"}}, 0, False) == "v2v")
# ---------------------------------------------------------------------------
# 3. utils/llm_api.py - reference-mode needs media; t2v is the text-only path
# ---------------------------------------------------------------------------
def verify_llm_api():
print("\n== 3. utils/llm_api.py: _h3_media_for_task_type ==")
llm_file = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "utils", "llm_api.py"
)
media_resolver = extract_functions(llm_file, ["_h3_media_for_task_type"])["_h3_media_for_task_type"]
def raises_requires_media(task_type):
try:
media_resolver(task_type, [], [], [])
except ValueError as exc:
return "requires reference media" in str(exc)
return False
check("r2v with zero media raises (upstream behavior)",
raises_requires_media("r2v"))
check("rv2v with zero media raises",
raises_requires_media("rv2v"))
check("r2v with media -> r2va",
media_resolver("r2v", ["img1", "img2"], ["vid"], ["aud"]) == ("r2va", ["img1", "img2"], ["vid"], ["aud"]))
check("direct t2v still -> t2va",
media_resolver("t2v", [], [], []) == ("t2va", [], [], []))
check("i2v with image -> i2va",
media_resolver("i2v", ["img"], [], []) == ("i2va", ["img"], [], []))
# ---------------------------------------------------------------------------
# 4. moxie_loader.py - one-model UI + auto-picked auxiliary models
# ---------------------------------------------------------------------------
def _install_loader_stubs(state):
comfy_api = types.ModuleType("comfy_api")
comfy_latest = types.ModuleType("comfy_api.latest")
class _FakeDiffusionModel:
def clone(self):
return _FakeDiffusionModel()
def set_model_optimized_attention(self, attention_function):
state.calls.append(("set_model_optimized_attention",))
return None
state.model = _FakeDiffusionModel()
class _Port:
def __init__(self, name, **kwargs):
self.name = name
self.kwargs = kwargs
class _Schema:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class _NodeOutput:
def __init__(self, *values):
self.values = values
io = types.SimpleNamespace(
Schema=_Schema,
NodeOutput=_NodeOutput,
ComfyNode=type("ComfyNode", (), {}),
Combo=types.SimpleNamespace(Input=staticmethod(lambda name, **kw: _Port(name, **kw))),
Float=types.SimpleNamespace(Input=staticmethod(lambda name, **kw: _Port(name, **kw))),
Model=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))),
Clip=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))),
Vae=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))),
)
comfy_latest.io = io
comfy_api.latest = comfy_latest
sys.modules["comfy_api"] = comfy_api
sys.modules["comfy_api.latest"] = comfy_latest
folder_paths = types.ModuleType("folder_paths")
folder_paths.get_folder_paths = lambda kind: []
sys.modules["folder_paths"] = folder_paths
comfy = types.ModuleType("comfy")
class _FakeVAE:
def __init__(self, sd=None, metadata=None):
self.sd = sd
def throw_exception_if_invalid(self):
return None
class _CLIPType(enum.Enum):
MINIMAX = "minimax"
def load_diffusion_model(path, model_options=None):
state.calls.append(("load_diffusion_model", os.path.basename(path)))
return state.model
def load_clip(ckpt_paths=None, embedding_directory=None, clip_type=None, model_options=None):
state.calls.append(("load_clip", clip_type))
return state.clip
def load_torch_file(path, safe_load=False, return_metadata=False):
state.calls.append(("load_torch_file", os.path.basename(path)))
sd = {"dummy": 1}
return (sd, {"meta": True}) if return_metadata else sd
def load_lora_for_models(model, clip, lora_sd, strength_model, strength_clip, lora_metadata=None):
raise AssertionError("MoxieMultimediaLoader must not load LoRAs anymore")
def get_attention_function(name, *args):
state.calls.append(("get_attention_function", name))
return state.attention_fn
def state_dict_prefix_replace(sd, mapping, filter_keys=False):
return sd
comfy.sd = types.SimpleNamespace(
load_diffusion_model=load_diffusion_model,
load_clip=load_clip,
load_lora_for_models=load_lora_for_models,
VAE=_FakeVAE,
CLIPType=_CLIPType,
)
comfy.utils = types.SimpleNamespace(
load_torch_file=load_torch_file,
state_dict_prefix_replace=state_dict_prefix_replace,
)
comfy_ldm = types.ModuleType("comfy.ldm")
comfy_ldm_modules = types.ModuleType("comfy.ldm.modules")
comfy_attention = types.ModuleType("comfy.ldm.modules.attention")
comfy_attention.get_attention_function = get_attention_function
comfy.ldm = comfy_ldm
comfy.ldm.modules = comfy_ldm_modules
comfy.ldm.modules.attention = comfy_attention
comfy.__path__ = [] # make "comfy" importable as a package
sys.modules["comfy"] = comfy
sys.modules["comfy.sd"] = comfy.sd
sys.modules["comfy.utils"] = comfy.utils
sys.modules["comfy.ldm"] = comfy_ldm
sys.modules["comfy.ldm.modules"] = comfy_ldm_modules
sys.modules["comfy.ldm.modules.attention"] = comfy_attention
# Minimal torch stand-in: moxie_loader only touches dtype constants and
# torch.device("cpu") at execution time.
sys.modules["torch"] = types.SimpleNamespace(
float8_e4m3fn="float8_e4m3fn",
float8_e5m2="float8_e5m2",
device=lambda *args, **kwargs: ("device",) + tuple(args),
)
def _load_loader_module():
pack_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
loader_file = os.path.join(pack_dir, "moxie_loader.py")
spec = importlib.util.spec_from_file_location("moxie_loader_standalone", loader_file)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def verify_loader():
print("\n== 4. moxie_loader.py: single-model UI + auto-picked aux models ==")
state = types.SimpleNamespace(
calls=[], clip=object(), attention_fn=object(),
)
_install_loader_stubs(state)
loader = _load_loader_module()
schema = loader.MoxieMultimediaLoader.define_schema()
input_names = [port.name for port in schema.inputs]
output_names = [port.name for port in schema.outputs]
check("schema shows exactly the single-model inputs",
input_names == ["diffusion_model", "weight_dtype", "attention_backend"],
repr(input_names))
for hidden in ("lora_name", "lora_strength_model", "lora_strength_clip",
"video_vae", "audio_vae", "clip", "clip_device"):
check(f"widget hidden: {hidden}", hidden not in input_names)
check("outputs still model/clip/video_vae/audio_vae",
[port.name or port.kwargs.get("display_name") for port in schema.outputs]
== ["model", "clip", "video_vae", "audio_vae"])
with tempfile.TemporaryDirectory() as tmp:
models = os.path.join(tmp, "models")
for subdir, name in (
("diffusion", "moxie.safetensors"),
("vae", "video_vae.safetensors"),
("audio_vae", "audio_vae.safetensors"),
("clip", "MM-VL.safetensors"),
# A leftover loras file must be ignored entirely: the distill
# weights ship baked into the diffusion model now.
("loras", "MM3step.safetensors"),
):
os.makedirs(os.path.join(models, subdir), exist_ok=True)
with open(os.path.join(models, subdir, name), "wb") as handle:
handle.write(b"stub")
loader.MODELS_DIR = models
result = loader.MoxieMultimediaLoader.execute(
diffusion_model="moxie.safetensors",
weight_dtype="default",
attention_backend="comfy_kitchen_attention",
)
names = [call[0] for call in state.calls]
check("full chain runs: model -> attention -> clip -> vaes (no LoRA)",
names == [
"load_diffusion_model", "get_attention_function",
"set_model_optimized_attention", "load_clip",
"load_torch_file", "load_torch_file",
], repr(names))
clip_calls = [call for call in state.calls if call[0] == "load_clip"]
check("CLIP auto-picked and loaded with the locked recipe",
len(clip_calls) == 1 and clip_calls[0][1] is not None, repr(clip_calls))
check("four outputs returned",
isinstance(result, loader.io.NodeOutput) and len(result.values) == 4)
check("model output is the attention clone (no LoRA on top)",
result.values[0] is not state.model)
check("clip output is the loaded clip", result.values[1] is state.clip)
# Missing clip folder -> loud setup error (same message as before).
state.calls.clear()
os.rename(os.path.join(models, "clip"), os.path.join(models, "clip_backup"))
try:
loader.MoxieMultimediaLoader.execute(
diffusion_model="moxie.safetensors",
weight_dtype="default",
attention_backend="pytorch_attention",
)
check("missing clip raises setup error", False)
except RuntimeError as error:
check("missing clip raises setup error",
"no model files found in models/clip" in str(error), str(error))
os.rename(os.path.join(models, "clip_backup"), os.path.join(models, "clip"))
# Multiple files -> deterministic first-sorted pick, no crash.
with open(os.path.join(models, "clip", "AAA-other.safetensors"), "wb") as handle:
handle.write(b"stub2")
picked = loader._auto_pick("clip")
check("multiple clip files pick first sorted deterministically",
picked == "AAA-other.safetensors", repr(picked))
def main():
verify_h3_project()
verify_basic_node()
verify_llm_api()
verify_loader()
print()
if FAILED:
print(f"FAILED CHECKS ({len(FAILED)}): {FAILED}")
return 1
print("ALL CHECKS PASSED")
return 0
if __name__ == "__main__":
sys.exit(main())